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 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_restore_tcon(struct cli_state
*cli
, struct smbXcli_tcon
*tcon
)
399 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
400 TALLOC_FREE(cli
->smb2
.tcon
);
401 cli
->smb2
.tcon
= tcon
;
403 TALLOC_FREE(cli
->smb1
.tcon
);
404 cli
->smb1
.tcon
= tcon
;
408 uint16_t cli_state_get_uid(struct cli_state
*cli
)
410 return smb1cli_session_current_id(cli
->smb1
.session
);
413 uint16_t cli_state_set_uid(struct cli_state
*cli
, uint16_t uid
)
415 uint16_t ret
= smb1cli_session_current_id(cli
->smb1
.session
);
416 smb1cli_session_set_id(cli
->smb1
.session
, uid
);
420 /****************************************************************************
421 Set the case sensitivity flag on the packets. Returns old state.
422 ****************************************************************************/
424 bool cli_set_case_sensitive(struct cli_state
*cli
, bool case_sensitive
)
428 struct smbXcli_tcon
*tcon
;
430 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
431 tcon
= cli
->smb2
.tcon
;
433 tcon
= cli
->smb1
.tcon
;
436 fs_attrs
= smbXcli_tcon_get_fs_attributes(tcon
);
437 if (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
) {
442 if (case_sensitive
) {
443 fs_attrs
|= FILE_CASE_SENSITIVE_SEARCH
;
445 fs_attrs
&= ~FILE_CASE_SENSITIVE_SEARCH
;
447 smbXcli_tcon_set_fs_attributes(tcon
, fs_attrs
);
452 uint32_t cli_state_available_size(struct cli_state
*cli
, uint32_t ofs
)
454 uint32_t ret
= smb1cli_conn_max_xmit(cli
->conn
);
465 time_t cli_state_server_time(struct cli_state
*cli
)
470 nt
= smbXcli_conn_server_system_time(cli
->conn
);
471 t
= nt_time_to_unix(nt
);
476 struct cli_echo_state
{
480 static void cli_echo_done(struct tevent_req
*subreq
);
482 struct tevent_req
*cli_echo_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
483 struct cli_state
*cli
, uint16_t num_echos
,
486 struct tevent_req
*req
, *subreq
;
487 struct cli_echo_state
*state
;
489 req
= tevent_req_create(mem_ctx
, &state
, struct cli_echo_state
);
494 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
495 state
->is_smb2
= true;
496 subreq
= smb2cli_echo_send(state
, ev
,
500 subreq
= smb1cli_echo_send(state
, ev
,
506 if (tevent_req_nomem(subreq
, req
)) {
507 return tevent_req_post(req
, ev
);
509 tevent_req_set_callback(subreq
, cli_echo_done
, req
);
514 static void cli_echo_done(struct tevent_req
*subreq
)
516 struct tevent_req
*req
= tevent_req_callback_data(
517 subreq
, struct tevent_req
);
518 struct cli_echo_state
*state
= tevent_req_data(
519 req
, struct cli_echo_state
);
522 if (state
->is_smb2
) {
523 status
= smb2cli_echo_recv(subreq
);
525 status
= smb1cli_echo_recv(subreq
);
528 if (!NT_STATUS_IS_OK(status
)) {
529 tevent_req_nterror(req
, status
);
533 tevent_req_done(req
);
537 * Get the result out from an echo request
538 * @param[in] req The async_req from cli_echo_send
539 * @retval Did the server reply correctly?
542 NTSTATUS
cli_echo_recv(struct tevent_req
*req
)
544 return tevent_req_simple_recv_ntstatus(req
);
548 * @brief Send/Receive SMBEcho requests
549 * @param[in] mem_ctx The memory context to put the async_req on
550 * @param[in] ev The event context that will call us back
551 * @param[in] cli The connection to send the echo to
552 * @param[in] num_echos How many times do we want to get the reply?
553 * @param[in] data The data we want to get back
554 * @retval Did the server reply correctly?
557 NTSTATUS
cli_echo(struct cli_state
*cli
, uint16_t num_echos
, DATA_BLOB data
)
559 TALLOC_CTX
*frame
= talloc_stackframe();
560 struct tevent_context
*ev
;
561 struct tevent_req
*req
;
562 NTSTATUS status
= NT_STATUS_OK
;
564 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
566 * Can't use sync call while an async call is in flight
568 status
= NT_STATUS_INVALID_PARAMETER
;
572 ev
= samba_tevent_context_init(frame
);
574 status
= NT_STATUS_NO_MEMORY
;
578 req
= cli_echo_send(frame
, ev
, cli
, num_echos
, data
);
580 status
= NT_STATUS_NO_MEMORY
;
584 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
588 status
= cli_echo_recv(req
);
594 NTSTATUS
cli_smb(TALLOC_CTX
*mem_ctx
, struct cli_state
*cli
,
595 uint8_t smb_command
, uint8_t additional_flags
,
596 uint8_t wct
, uint16_t *vwv
,
597 uint32_t num_bytes
, const uint8_t *bytes
,
598 struct tevent_req
**result_parent
,
599 uint8_t min_wct
, uint8_t *pwct
, uint16_t **pvwv
,
600 uint32_t *pnum_bytes
, uint8_t **pbytes
)
602 struct tevent_context
*ev
;
603 struct tevent_req
*req
= NULL
;
604 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
606 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
607 return NT_STATUS_INVALID_PARAMETER
;
609 ev
= samba_tevent_context_init(mem_ctx
);
613 req
= cli_smb_send(mem_ctx
, ev
, cli
, smb_command
, additional_flags
, 0,
614 wct
, vwv
, num_bytes
, bytes
);
618 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
621 status
= cli_smb_recv(req
, NULL
, NULL
, min_wct
, pwct
, pvwv
,
625 if (NT_STATUS_IS_OK(status
) && (result_parent
!= NULL
)) {
626 *result_parent
= req
;