CVE-2020-25718 tests/krb5: Fix indentation
[Samba.git] / source3 / libsmb / clientgen.c
blob8ded372d0ba8aef256477a2539b43dba6aa53d77
1 /*
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/>.
21 #include "includes.h"
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 "../librpc/ndr/libndr.h"
29 #include "../include/client.h"
31 /****************************************************************************
32 Change the timeout (in milliseconds).
33 ****************************************************************************/
35 unsigned int cli_set_timeout(struct cli_state *cli, unsigned int timeout)
37 unsigned int old_timeout = cli->timeout;
38 cli->timeout = timeout;
39 return old_timeout;
42 /****************************************************************************
43 Set the 'backup_intent' flag.
44 ****************************************************************************/
46 bool cli_set_backup_intent(struct cli_state *cli, bool flag)
48 bool old_state = cli->backup_intent;
49 cli->backup_intent = flag;
50 return old_state;
53 /****************************************************************************
54 Initialise a client structure. Always returns a talloc'ed struct.
55 Set the signing state (used from the command line).
56 ****************************************************************************/
58 struct GUID cli_state_client_guid;
60 struct cli_state *cli_state_create(TALLOC_CTX *mem_ctx,
61 int fd,
62 const char *remote_name,
63 int signing_state, int flags)
65 struct cli_state *cli = NULL;
66 bool use_spnego = lp_client_use_spnego();
67 bool force_dos_errors = false;
68 bool force_ascii = false;
69 bool use_level_II_oplocks = false;
70 uint32_t smb1_capabilities = 0;
71 uint32_t smb2_capabilities = 0;
72 struct GUID client_guid;
74 if (!GUID_all_zero(&cli_state_client_guid)) {
75 client_guid = cli_state_client_guid;
76 } else {
77 client_guid = GUID_random();
80 /* Check the effective uid - make sure we are not setuid */
81 if (is_setuid_root()) {
82 DEBUG(0,("libsmb based programs must *NOT* be setuid root.\n"));
83 return NULL;
86 cli = talloc_zero(mem_ctx, struct cli_state);
87 if (!cli) {
88 return NULL;
91 cli->server_domain = talloc_strdup(cli, "");
92 if (!cli->server_domain) {
93 goto error;
95 cli->server_os = talloc_strdup(cli, "");
96 if (!cli->server_os) {
97 goto error;
99 cli->server_type = talloc_strdup(cli, "");
100 if (!cli->server_type) {
101 goto error;
104 cli->dfs_mountpoint = talloc_strdup(cli, "");
105 if (!cli->dfs_mountpoint) {
106 goto error;
108 cli->raw_status = NT_STATUS_INTERNAL_ERROR;
109 cli->map_dos_errors = true; /* remove this */
110 cli->timeout = CLIENT_TIMEOUT;
112 /* Set the CLI_FORCE_DOSERR environment variable to test
113 client routines using DOS errors instead of STATUS32
114 ones. This intended only as a temporary hack. */
115 if (getenv("CLI_FORCE_DOSERR")) {
116 force_dos_errors = true;
118 if (flags & CLI_FULL_CONNECTION_FORCE_DOS_ERRORS) {
119 force_dos_errors = true;
122 if (getenv("CLI_FORCE_ASCII")) {
123 force_ascii = true;
125 if (!lp_unicode()) {
126 force_ascii = true;
128 if (flags & CLI_FULL_CONNECTION_FORCE_ASCII) {
129 force_ascii = true;
132 if (flags & CLI_FULL_CONNECTION_DONT_SPNEGO) {
133 use_spnego = false;
136 if (flags & CLI_FULL_CONNECTION_OPLOCKS) {
137 cli->use_oplocks = true;
139 if (flags & CLI_FULL_CONNECTION_LEVEL_II_OPLOCKS) {
140 use_level_II_oplocks = true;
143 if (signing_state == SMB_SIGNING_IPC_DEFAULT) {
145 * Ensure for IPC/RPC the default is to require
146 * signing unless explicitly turned off by the
147 * administrator.
149 signing_state = lp_client_ipc_signing();
152 if (signing_state == SMB_SIGNING_DEFAULT) {
153 signing_state = lp_client_signing();
156 smb1_capabilities = 0;
157 smb1_capabilities |= CAP_LARGE_FILES;
158 smb1_capabilities |= CAP_NT_SMBS | CAP_RPC_REMOTE_APIS;
159 smb1_capabilities |= CAP_LOCK_AND_READ | CAP_NT_FIND;
160 smb1_capabilities |= CAP_DFS | CAP_W2K_SMBS;
161 smb1_capabilities |= CAP_LARGE_READX|CAP_LARGE_WRITEX;
162 smb1_capabilities |= CAP_LWIO;
164 if (!force_dos_errors) {
165 smb1_capabilities |= CAP_STATUS32;
168 if (!force_ascii) {
169 smb1_capabilities |= CAP_UNICODE;
172 if (use_spnego) {
173 smb1_capabilities |= CAP_EXTENDED_SECURITY;
176 if (use_level_II_oplocks) {
177 smb1_capabilities |= CAP_LEVEL_II_OPLOCKS;
180 smb2_capabilities = SMB2_CAP_ALL;
182 cli->conn = smbXcli_conn_create(cli, fd, remote_name,
183 signing_state,
184 smb1_capabilities,
185 &client_guid,
186 smb2_capabilities);
187 if (cli->conn == NULL) {
188 goto error;
191 cli->smb1.pid = (uint32_t)getpid();
192 cli->smb1.vc_num = cli->smb1.pid;
193 cli->smb1.session = smbXcli_session_create(cli, cli->conn);
194 if (cli->smb1.session == NULL) {
195 goto error;
198 cli->initialised = 1;
199 return cli;
201 /* Clean up after malloc() error */
203 error:
205 TALLOC_FREE(cli);
206 return NULL;
209 /****************************************************************************
210 Close all pipes open on this session.
211 ****************************************************************************/
213 void cli_nt_pipes_close(struct cli_state *cli)
215 while (cli->pipe_list != NULL) {
217 * No TALLOC_FREE here!
219 talloc_free(cli->pipe_list);
223 /****************************************************************************
224 Shutdown a client structure.
225 ****************************************************************************/
227 static void _cli_shutdown(struct cli_state *cli)
229 cli_nt_pipes_close(cli);
232 * tell our peer to free his resources. Wihtout this, when an
233 * application attempts to do a graceful shutdown and calls
234 * smbc_free_context() to clean up all connections, some connections
235 * can remain active on the peer end, until some (long) timeout period
236 * later. This tree disconnect forces the peer to clean up, since the
237 * connection will be going away.
239 if (cli_state_has_tcon(cli)) {
240 cli_tdis(cli);
243 smbXcli_conn_disconnect(cli->conn, NT_STATUS_OK);
245 TALLOC_FREE(cli);
248 void cli_shutdown(struct cli_state *cli)
250 struct cli_state *cli_head;
251 if (cli == NULL) {
252 return;
254 DLIST_HEAD(cli, cli_head);
255 if (cli_head == cli) {
257 * head of a DFS list, shutdown all subsidiary DFS
258 * connections.
260 struct cli_state *p, *next;
262 for (p = cli_head->next; p; p = next) {
263 next = p->next;
264 DLIST_REMOVE(cli_head, p);
265 _cli_shutdown(p);
267 } else {
268 DLIST_REMOVE(cli_head, cli);
271 _cli_shutdown(cli);
274 uint16_t cli_state_get_vc_num(struct cli_state *cli)
276 return cli->smb1.vc_num;
279 /****************************************************************************
280 Set the PID to use for smb messages. Return the old pid.
281 ****************************************************************************/
283 uint32_t cli_setpid(struct cli_state *cli, uint32_t pid)
285 uint32_t ret = cli->smb1.pid;
286 cli->smb1.pid = pid;
287 return ret;
290 uint32_t cli_getpid(struct cli_state *cli)
292 return cli->smb1.pid;
295 bool cli_state_is_encryption_on(struct cli_state *cli)
297 if (smbXcli_conn_protocol(cli->conn) < PROTOCOL_SMB2_02) {
298 return smb1cli_conn_encryption_on(cli->conn);
301 if (cli->smb2.tcon == NULL) {
302 return false;
305 return smb2cli_tcon_is_encryption_on(cli->smb2.tcon);
308 bool cli_state_has_tcon(struct cli_state *cli)
310 uint32_t tid;
311 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
312 if (cli->smb2.tcon == NULL) {
313 return false;
315 tid = cli_state_get_tid(cli);
316 if (tid == UINT32_MAX) {
317 return false;
319 } else {
320 if (cli->smb1.tcon == NULL) {
321 return false;
323 tid = cli_state_get_tid(cli);
324 if (tid == UINT16_MAX) {
325 return false;
328 return true;
331 uint32_t cli_state_get_tid(struct cli_state *cli)
333 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
334 return smb2cli_tcon_current_id(cli->smb2.tcon);
335 } else {
336 return (uint32_t)smb1cli_tcon_current_id(cli->smb1.tcon);
340 uint32_t cli_state_set_tid(struct cli_state *cli, uint32_t tid)
342 uint32_t ret;
343 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
344 ret = smb2cli_tcon_current_id(cli->smb2.tcon);
345 smb2cli_tcon_set_id(cli->smb2.tcon, tid);
346 } else {
347 ret = smb1cli_tcon_current_id(cli->smb1.tcon);
348 smb1cli_tcon_set_id(cli->smb1.tcon, tid);
350 return ret;
353 struct smbXcli_tcon *cli_state_save_tcon(struct cli_state *cli)
356 * Note. This used to make a deep copy of either
357 * cli->smb2.tcon or cli->smb1.tcon, but this leaves
358 * the original pointer in place which will then get
359 * TALLOC_FREE()'d when the new connection is made on
360 * this cli_state.
362 * As there may be pipes open on the old connection with
363 * talloc'ed state allocated using the tcon pointer as a
364 * parent we can't deep copy and then free this as that
365 * closes the open pipes.
367 * This call is used to temporarily swap out a tcon pointer
368 * to allow a new tcon on the same cli_state.
370 * Just return the raw pointer and set the old value to NULL.
371 * We know we MUST be calling cli_state_restore_tcon() below
372 * to restore before closing the session.
374 * See BUG: https://bugzilla.samba.org/show_bug.cgi?id=13992
376 struct smbXcli_tcon *tcon_ret = NULL;
378 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
379 tcon_ret = cli->smb2.tcon;
380 cli->smb2.tcon = NULL; /* *Not* TALLOC_FREE(). */
381 } else {
382 tcon_ret = cli->smb1.tcon;
383 cli->smb1.tcon = NULL; /* *Not* TALLOC_FREE(). */
385 return tcon_ret;
388 void cli_state_restore_tcon(struct cli_state *cli, struct smbXcli_tcon *tcon)
390 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
391 TALLOC_FREE(cli->smb2.tcon);
392 cli->smb2.tcon = tcon;
393 } else {
394 TALLOC_FREE(cli->smb1.tcon);
395 cli->smb1.tcon = tcon;
399 uint16_t cli_state_get_uid(struct cli_state *cli)
401 return smb1cli_session_current_id(cli->smb1.session);
404 uint16_t cli_state_set_uid(struct cli_state *cli, uint16_t uid)
406 uint16_t ret = smb1cli_session_current_id(cli->smb1.session);
407 smb1cli_session_set_id(cli->smb1.session, uid);
408 return ret;
411 /****************************************************************************
412 Set the case sensitivity flag on the packets. Returns old state.
413 ****************************************************************************/
415 bool cli_set_case_sensitive(struct cli_state *cli, bool case_sensitive)
417 bool ret;
418 uint32_t fs_attrs;
419 struct smbXcli_tcon *tcon;
421 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
422 tcon = cli->smb2.tcon;
423 } else {
424 tcon = cli->smb1.tcon;
427 fs_attrs = smbXcli_tcon_get_fs_attributes(tcon);
428 if (fs_attrs & FILE_CASE_SENSITIVE_SEARCH) {
429 ret = true;
430 } else {
431 ret = false;
433 if (case_sensitive) {
434 fs_attrs |= FILE_CASE_SENSITIVE_SEARCH;
435 } else {
436 fs_attrs &= ~FILE_CASE_SENSITIVE_SEARCH;
438 smbXcli_tcon_set_fs_attributes(tcon, fs_attrs);
440 return ret;
443 uint32_t cli_state_available_size(struct cli_state *cli, uint32_t ofs)
445 uint32_t ret = smb1cli_conn_max_xmit(cli->conn);
447 if (ofs >= ret) {
448 return 0;
451 ret -= ofs;
453 return ret;
456 time_t cli_state_server_time(struct cli_state *cli)
458 NTTIME nt;
459 time_t t;
461 nt = smbXcli_conn_server_system_time(cli->conn);
462 t = nt_time_to_unix(nt);
464 return t;
467 struct cli_echo_state {
468 bool is_smb2;
471 static void cli_echo_done(struct tevent_req *subreq);
473 struct tevent_req *cli_echo_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
474 struct cli_state *cli, uint16_t num_echos,
475 DATA_BLOB data)
477 struct tevent_req *req, *subreq;
478 struct cli_echo_state *state;
480 req = tevent_req_create(mem_ctx, &state, struct cli_echo_state);
481 if (req == NULL) {
482 return NULL;
485 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
486 state->is_smb2 = true;
487 subreq = smb2cli_echo_send(state, ev,
488 cli->conn,
489 cli->timeout);
490 } else {
491 subreq = smb1cli_echo_send(state, ev,
492 cli->conn,
493 cli->timeout,
494 num_echos,
495 data);
497 if (tevent_req_nomem(subreq, req)) {
498 return tevent_req_post(req, ev);
500 tevent_req_set_callback(subreq, cli_echo_done, req);
502 return req;
505 static void cli_echo_done(struct tevent_req *subreq)
507 struct tevent_req *req = tevent_req_callback_data(
508 subreq, struct tevent_req);
509 struct cli_echo_state *state = tevent_req_data(
510 req, struct cli_echo_state);
511 NTSTATUS status;
513 if (state->is_smb2) {
514 status = smb2cli_echo_recv(subreq);
515 } else {
516 status = smb1cli_echo_recv(subreq);
518 TALLOC_FREE(subreq);
519 if (!NT_STATUS_IS_OK(status)) {
520 tevent_req_nterror(req, status);
521 return;
524 tevent_req_done(req);
528 * Get the result out from an echo request
529 * @param[in] req The async_req from cli_echo_send
530 * @retval Did the server reply correctly?
533 NTSTATUS cli_echo_recv(struct tevent_req *req)
535 return tevent_req_simple_recv_ntstatus(req);
539 * @brief Send/Receive SMBEcho requests
540 * @param[in] mem_ctx The memory context to put the async_req on
541 * @param[in] ev The event context that will call us back
542 * @param[in] cli The connection to send the echo to
543 * @param[in] num_echos How many times do we want to get the reply?
544 * @param[in] data The data we want to get back
545 * @retval Did the server reply correctly?
548 NTSTATUS cli_echo(struct cli_state *cli, uint16_t num_echos, DATA_BLOB data)
550 TALLOC_CTX *frame = talloc_stackframe();
551 struct tevent_context *ev;
552 struct tevent_req *req;
553 NTSTATUS status = NT_STATUS_OK;
555 if (smbXcli_conn_has_async_calls(cli->conn)) {
557 * Can't use sync call while an async call is in flight
559 status = NT_STATUS_INVALID_PARAMETER;
560 goto fail;
563 ev = samba_tevent_context_init(frame);
564 if (ev == NULL) {
565 status = NT_STATUS_NO_MEMORY;
566 goto fail;
569 req = cli_echo_send(frame, ev, cli, num_echos, data);
570 if (req == NULL) {
571 status = NT_STATUS_NO_MEMORY;
572 goto fail;
575 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
576 goto fail;
579 status = cli_echo_recv(req);
580 fail:
581 TALLOC_FREE(frame);
582 return status;
585 NTSTATUS cli_smb(TALLOC_CTX *mem_ctx, struct cli_state *cli,
586 uint8_t smb_command, uint8_t additional_flags,
587 uint8_t wct, uint16_t *vwv,
588 uint32_t num_bytes, const uint8_t *bytes,
589 struct tevent_req **result_parent,
590 uint8_t min_wct, uint8_t *pwct, uint16_t **pvwv,
591 uint32_t *pnum_bytes, uint8_t **pbytes)
593 struct tevent_context *ev;
594 struct tevent_req *req = NULL;
595 NTSTATUS status = NT_STATUS_NO_MEMORY;
597 if (smbXcli_conn_has_async_calls(cli->conn)) {
598 return NT_STATUS_INVALID_PARAMETER;
600 ev = samba_tevent_context_init(mem_ctx);
601 if (ev == NULL) {
602 goto fail;
604 req = cli_smb_send(mem_ctx, ev, cli, smb_command, additional_flags, 0,
605 wct, vwv, num_bytes, bytes);
606 if (req == NULL) {
607 goto fail;
609 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
610 goto fail;
612 status = cli_smb_recv(req, NULL, NULL, min_wct, pwct, pvwv,
613 pnum_bytes, pbytes);
614 fail:
615 TALLOC_FREE(ev);
616 if (NT_STATUS_IS_OK(status) && (result_parent != NULL)) {
617 *result_parent = req;
619 return status;