2 Unix SMB/CIFS implementation.
3 SMB client generic functions
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "libsmb/libsmb.h"
23 #include "../lib/util/tevent_ntstatus.h"
24 #include "../libcli/smb/smb_signing.h"
25 #include "../libcli/smb/smb_seal.h"
26 #include "async_smb.h"
27 #include "../libcli/smb/smbXcli_base.h"
28 #include "../libcli/smb/smb2_negotiate_context.h"
29 #include "../librpc/ndr/libndr.h"
30 #include "../include/client.h"
32 /****************************************************************************
33 Change the timeout (in milliseconds).
34 ****************************************************************************/
36 unsigned int cli_set_timeout(struct cli_state
*cli
, unsigned int timeout
)
38 unsigned int old_timeout
= cli
->timeout
;
39 cli
->timeout
= timeout
;
43 /****************************************************************************
44 Set the 'backup_intent' flag.
45 ****************************************************************************/
47 bool cli_set_backup_intent(struct cli_state
*cli
, bool flag
)
49 bool old_state
= cli
->backup_intent
;
50 cli
->backup_intent
= flag
;
54 /****************************************************************************
55 Initialise a client structure. Always returns a talloc'ed struct.
56 Set the signing state (used from the command line).
57 ****************************************************************************/
59 struct GUID cli_state_client_guid
;
61 struct cli_state
*cli_state_create(TALLOC_CTX
*mem_ctx
,
63 const char *remote_name
,
64 int signing_state
, int flags
)
66 struct cli_state
*cli
= NULL
;
67 bool use_spnego
= lp_client_use_spnego();
68 bool force_dos_errors
= false;
69 bool force_ascii
= false;
70 bool use_level_II_oplocks
= false;
71 uint32_t smb1_capabilities
= 0;
72 uint32_t smb2_capabilities
= 0;
73 struct smb311_capabilities smb3_capabilities
=
74 smb311_capabilities_parse("client",
75 lp_client_smb3_signing_algorithms(),
76 lp_client_smb3_encryption_algorithms());
77 struct GUID client_guid
;
79 if (!GUID_all_zero(&cli_state_client_guid
)) {
80 client_guid
= cli_state_client_guid
;
82 const char *str
= NULL
;
84 str
= lp_parm_const_string(-1, "libsmb", "client_guid", NULL
);
86 GUID_from_string(str
, &client_guid
);
88 client_guid
= GUID_random();
92 /* Check the effective uid - make sure we are not setuid */
93 if (is_setuid_root()) {
94 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
98 cli
= talloc_zero(mem_ctx
, struct cli_state
);
103 cli
->server_domain
= talloc_strdup(cli
, "");
104 if (!cli
->server_domain
) {
107 cli
->server_os
= talloc_strdup(cli
, "");
108 if (!cli
->server_os
) {
111 cli
->server_type
= talloc_strdup(cli
, "");
112 if (!cli
->server_type
) {
116 cli
->raw_status
= NT_STATUS_INTERNAL_ERROR
;
117 cli
->map_dos_errors
= true; /* remove this */
118 cli
->timeout
= CLIENT_TIMEOUT
;
120 /* Set the CLI_FORCE_DOSERR environment variable to test
121 client routines using DOS errors instead of STATUS32
122 ones. This intended only as a temporary hack. */
123 if (getenv("CLI_FORCE_DOSERR")) {
124 force_dos_errors
= true;
126 if (flags
& CLI_FULL_CONNECTION_FORCE_DOS_ERRORS
) {
127 force_dos_errors
= true;
130 if (getenv("CLI_FORCE_ASCII")) {
136 if (flags
& CLI_FULL_CONNECTION_FORCE_ASCII
) {
140 if (flags
& CLI_FULL_CONNECTION_DONT_SPNEGO
) {
144 if (flags
& CLI_FULL_CONNECTION_OPLOCKS
) {
145 cli
->use_oplocks
= true;
147 if (flags
& CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS
) {
148 use_level_II_oplocks
= true;
151 if (signing_state
== SMB_SIGNING_IPC_DEFAULT
) {
153 * Ensure for IPC/RPC the default is to require
154 * signing unless explicitly turned off by the
157 signing_state
= lp_client_ipc_signing();
160 if (signing_state
== SMB_SIGNING_DEFAULT
) {
161 signing_state
= lp_client_signing();
164 smb1_capabilities
= 0;
165 smb1_capabilities
|= CAP_LARGE_FILES
;
166 smb1_capabilities
|= CAP_NT_SMBS
| CAP_RPC_REMOTE_APIS
;
167 smb1_capabilities
|= CAP_LOCK_AND_READ
| CAP_NT_FIND
;
168 smb1_capabilities
|= CAP_DFS
| CAP_W2K_SMBS
;
169 smb1_capabilities
|= CAP_LARGE_READX
|CAP_LARGE_WRITEX
;
170 smb1_capabilities
|= CAP_LWIO
;
172 if (!force_dos_errors
) {
173 smb1_capabilities
|= CAP_STATUS32
;
177 smb1_capabilities
|= CAP_UNICODE
;
181 smb1_capabilities
|= CAP_EXTENDED_SECURITY
;
184 if (use_level_II_oplocks
) {
185 smb1_capabilities
|= CAP_LEVEL_II_OPLOCKS
;
188 smb2_capabilities
= SMB2_CAP_ALL
;
190 cli
->conn
= smbXcli_conn_create(cli
, fd
, remote_name
,
196 if (cli
->conn
== NULL
) {
200 cli
->smb1
.pid
= (uint32_t)getpid();
201 cli
->smb1
.vc_num
= cli
->smb1
.pid
;
202 cli
->smb1
.session
= smbXcli_session_create(cli
, cli
->conn
);
203 if (cli
->smb1
.session
== NULL
) {
207 cli
->initialised
= 1;
210 /* Clean up after malloc() error */
218 /****************************************************************************
219 Close all pipes open on this session.
220 ****************************************************************************/
222 static void cli_nt_pipes_close(struct cli_state
*cli
)
224 while (cli
->pipe_list
!= NULL
) {
226 * No TALLOC_FREE here!
228 talloc_free(cli
->pipe_list
);
232 /****************************************************************************
233 Shutdown a client structure.
234 ****************************************************************************/
236 static void _cli_shutdown(struct cli_state
*cli
)
238 cli_nt_pipes_close(cli
);
241 * tell our peer to free his resources. Without this, when an
242 * application attempts to do a graceful shutdown and calls
243 * smbc_free_context() to clean up all connections, some connections
244 * can remain active on the peer end, until some (long) timeout period
245 * later. This tree disconnect forces the peer to clean up, since the
246 * connection will be going away.
248 if (cli_state_has_tcon(cli
)) {
252 smbXcli_conn_disconnect(cli
->conn
, NT_STATUS_OK
);
257 void cli_shutdown(struct cli_state
*cli
)
259 struct cli_state
*cli_head
;
263 DLIST_HEAD(cli
, cli_head
);
264 if (cli_head
== cli
) {
266 * head of a DFS list, shutdown all subsidiary DFS
269 struct cli_state
*p
, *next
;
271 for (p
= cli_head
->next
; p
; p
= next
) {
273 DLIST_REMOVE(cli_head
, p
);
277 DLIST_REMOVE(cli_head
, cli
);
283 uint16_t cli_state_get_vc_num(struct cli_state
*cli
)
285 return cli
->smb1
.vc_num
;
288 /****************************************************************************
289 Set the PID to use for smb messages. Return the old pid.
290 ****************************************************************************/
292 uint32_t cli_setpid(struct cli_state
*cli
, uint32_t pid
)
294 uint32_t ret
= cli
->smb1
.pid
;
299 uint32_t cli_getpid(struct cli_state
*cli
)
301 return cli
->smb1
.pid
;
304 bool cli_state_is_encryption_on(struct cli_state
*cli
)
306 if (smbXcli_conn_protocol(cli
->conn
) < PROTOCOL_SMB2_02
) {
307 return smb1cli_conn_encryption_on(cli
->conn
);
310 if (cli
->smb2
.tcon
== NULL
) {
314 return smb2cli_tcon_is_encryption_on(cli
->smb2
.tcon
);
317 bool cli_state_has_tcon(struct cli_state
*cli
)
320 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
321 if (cli
->smb2
.tcon
== NULL
) {
324 tid
= cli_state_get_tid(cli
);
325 if (tid
== UINT32_MAX
) {
329 if (cli
->smb1
.tcon
== NULL
) {
332 tid
= cli_state_get_tid(cli
);
333 if (tid
== UINT16_MAX
) {
340 uint32_t cli_state_get_tid(struct cli_state
*cli
)
342 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
343 return smb2cli_tcon_current_id(cli
->smb2
.tcon
);
345 return (uint32_t)smb1cli_tcon_current_id(cli
->smb1
.tcon
);
349 uint32_t cli_state_set_tid(struct cli_state
*cli
, uint32_t tid
)
352 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
353 ret
= smb2cli_tcon_current_id(cli
->smb2
.tcon
);
354 smb2cli_tcon_set_id(cli
->smb2
.tcon
, tid
);
356 ret
= smb1cli_tcon_current_id(cli
->smb1
.tcon
);
357 smb1cli_tcon_set_id(cli
->smb1
.tcon
, tid
);
362 static struct smbXcli_tcon
*cli_state_save_tcon(struct cli_state
*cli
)
365 * Note. This used to make a deep copy of either
366 * cli->smb2.tcon or cli->smb1.tcon, but this leaves
367 * the original pointer in place which will then get
368 * TALLOC_FREE()'d when the new connection is made on
371 * As there may be pipes open on the old connection with
372 * talloc'ed state allocated using the tcon pointer as a
373 * parent we can't deep copy and then free this as that
374 * closes the open pipes.
376 * This call is used to temporarily swap out a tcon pointer
377 * to allow a new tcon on the same cli_state.
379 * Just return the raw pointer and set the old value to NULL.
380 * We know we MUST be calling cli_state_restore_tcon() below
381 * to restore before closing the session.
383 * See BUG: https://bugzilla.samba.org/show_bug.cgi?id=13992
385 struct smbXcli_tcon
*tcon_ret
= NULL
;
387 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
388 tcon_ret
= cli
->smb2
.tcon
;
389 cli
->smb2
.tcon
= NULL
; /* *Not* TALLOC_FREE(). */
391 tcon_ret
= cli
->smb1
.tcon
;
392 cli
->smb1
.tcon
= NULL
; /* *Not* TALLOC_FREE(). */
397 void cli_state_save_tcon_share(struct cli_state
*cli
,
398 struct smbXcli_tcon
**_tcon_ret
,
399 char **_sharename_ret
)
401 *_tcon_ret
= cli_state_save_tcon(cli
);
403 * No talloc_copy as cli->share is already
406 *_sharename_ret
= cli
->share
;
410 static void cli_state_restore_tcon(struct cli_state
*cli
,
411 struct smbXcli_tcon
*tcon
)
413 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
414 TALLOC_FREE(cli
->smb2
.tcon
);
415 cli
->smb2
.tcon
= tcon
;
417 TALLOC_FREE(cli
->smb1
.tcon
);
418 cli
->smb1
.tcon
= tcon
;
422 void cli_state_restore_tcon_share(struct cli_state
*cli
,
423 struct smbXcli_tcon
*tcon
,
426 /* cli->share will have been replaced by a cli_tree_connect() call. */
427 TALLOC_FREE(cli
->share
);
429 cli_state_restore_tcon(cli
, tcon
);
432 uint16_t cli_state_get_uid(struct cli_state
*cli
)
434 return smb1cli_session_current_id(cli
->smb1
.session
);
437 uint16_t cli_state_set_uid(struct cli_state
*cli
, uint16_t uid
)
439 uint16_t ret
= smb1cli_session_current_id(cli
->smb1
.session
);
440 smb1cli_session_set_id(cli
->smb1
.session
, uid
);
444 /****************************************************************************
445 Set the case sensitivity flag on the packets. Returns old state.
446 ****************************************************************************/
448 bool cli_set_case_sensitive(struct cli_state
*cli
, bool case_sensitive
)
452 struct smbXcli_tcon
*tcon
;
454 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
455 tcon
= cli
->smb2
.tcon
;
457 tcon
= cli
->smb1
.tcon
;
460 fs_attrs
= smbXcli_tcon_get_fs_attributes(tcon
);
461 if (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
) {
466 if (case_sensitive
) {
467 fs_attrs
|= FILE_CASE_SENSITIVE_SEARCH
;
469 fs_attrs
&= ~FILE_CASE_SENSITIVE_SEARCH
;
471 smbXcli_tcon_set_fs_attributes(tcon
, fs_attrs
);
476 uint32_t cli_state_available_size(struct cli_state
*cli
, uint32_t ofs
)
478 uint32_t ret
= smb1cli_conn_max_xmit(cli
->conn
);
489 time_t cli_state_server_time(struct cli_state
*cli
)
494 nt
= smbXcli_conn_server_system_time(cli
->conn
);
495 t
= nt_time_to_unix(nt
);
500 struct cli_echo_state
{
504 static void cli_echo_done(struct tevent_req
*subreq
);
506 struct tevent_req
*cli_echo_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
507 struct cli_state
*cli
, uint16_t num_echos
,
510 struct tevent_req
*req
, *subreq
;
511 struct cli_echo_state
*state
;
513 req
= tevent_req_create(mem_ctx
, &state
, struct cli_echo_state
);
518 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
519 state
->is_smb2
= true;
520 subreq
= smb2cli_echo_send(state
, ev
,
524 subreq
= smb1cli_echo_send(state
, ev
,
530 if (tevent_req_nomem(subreq
, req
)) {
531 return tevent_req_post(req
, ev
);
533 tevent_req_set_callback(subreq
, cli_echo_done
, req
);
538 static void cli_echo_done(struct tevent_req
*subreq
)
540 struct tevent_req
*req
= tevent_req_callback_data(
541 subreq
, struct tevent_req
);
542 struct cli_echo_state
*state
= tevent_req_data(
543 req
, struct cli_echo_state
);
546 if (state
->is_smb2
) {
547 status
= smb2cli_echo_recv(subreq
);
549 status
= smb1cli_echo_recv(subreq
);
552 if (!NT_STATUS_IS_OK(status
)) {
553 tevent_req_nterror(req
, status
);
557 tevent_req_done(req
);
561 * Get the result out from an echo request
562 * @param[in] req The async_req from cli_echo_send
563 * @retval Did the server reply correctly?
566 NTSTATUS
cli_echo_recv(struct tevent_req
*req
)
568 return tevent_req_simple_recv_ntstatus(req
);
572 * @brief Send/Receive SMBEcho requests
573 * @param[in] mem_ctx The memory context to put the async_req on
574 * @param[in] ev The event context that will call us back
575 * @param[in] cli The connection to send the echo to
576 * @param[in] num_echos How many times do we want to get the reply?
577 * @param[in] data The data we want to get back
578 * @retval Did the server reply correctly?
581 NTSTATUS
cli_echo(struct cli_state
*cli
, uint16_t num_echos
, DATA_BLOB data
)
583 TALLOC_CTX
*frame
= talloc_stackframe();
584 struct tevent_context
*ev
;
585 struct tevent_req
*req
;
586 NTSTATUS status
= NT_STATUS_OK
;
588 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
590 * Can't use sync call while an async call is in flight
592 status
= NT_STATUS_INVALID_PARAMETER
;
596 ev
= samba_tevent_context_init(frame
);
598 status
= NT_STATUS_NO_MEMORY
;
602 req
= cli_echo_send(frame
, ev
, cli
, num_echos
, data
);
604 status
= NT_STATUS_NO_MEMORY
;
608 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
612 status
= cli_echo_recv(req
);
618 NTSTATUS
cli_smb(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
619 uint8_t smb_command
, uint8_t additional_flags
,
620 uint8_t wct
, uint16_t *vwv
,
621 uint32_t num_bytes
, const uint8_t *bytes
,
622 struct tevent_req
**result_parent
,
623 uint8_t min_wct
, uint8_t *pwct
, uint16_t **pvwv
,
624 uint32_t *pnum_bytes
, uint8_t **pbytes
)
626 struct tevent_context
*ev
;
627 struct tevent_req
*req
= NULL
;
628 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
630 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
631 return NT_STATUS_INVALID_PARAMETER
;
633 ev
= samba_tevent_context_init(mem_ctx
);
637 req
= cli_smb_send(mem_ctx
, ev
, cli
, smb_command
, additional_flags
, 0,
638 wct
, vwv
, num_bytes
, bytes
);
642 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
645 status
= cli_smb_recv(req
, NULL
, NULL
, min_wct
, pwct
, pvwv
,
649 if (NT_STATUS_IS_OK(status
) && (result_parent
!= NULL
)) {
650 *result_parent
= req
;