1 /*
2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
6 Copyright (C) Jeremy Allison 2007-2009
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "libsmb/clirap.h"
25 #include "msdfs.h"
26 #include "trans2.h"
27 #include "libsmb/nmblib.h"
28 #include "../libcli/smb/smbXcli_base.h"
29 #include "auth/credentials/credentials.h"
30
31 /********************************************************************
32 Important point.
33
34 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
35 are not C escaped here).
36
37 - but if we're using POSIX paths then <pathname> may contain
38 '/' separators, not '\\' separators. So cope with '\\' or '/'
39 as a separator when looking at the pathname part.... JRA.
40 ********************************************************************/
41
42 /********************************************************************
43 Ensure a connection is encrypted.
44 ********************************************************************/
45
cli_cm_force_encryption_creds(struct cli_state * c,struct cli_credentials * creds,const char * sharename)46 NTSTATUS cli_cm_force_encryption_creds(struct cli_state *c,
47 struct cli_credentials *creds,
48 const char *sharename)
49 {
50 uint16_t major, minor;
51 uint32_t caplow, caphigh;
52 NTSTATUS status;
53
54 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
55 status = smb2cli_session_encryption_on(c->smb2.session);
56 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
57 d_printf("Encryption required and "
58 "server doesn't support "
59 "SMB3 encryption - failing connect\n");
60 } else if (!NT_STATUS_IS_OK(status)) {
61 d_printf("Encryption required and "
62 "setup failed with error %s.\n",
63 nt_errstr(status));
64 }
65 return status;
66 }
67
68 if (!SERVER_HAS_UNIX_CIFS(c)) {
69 d_printf("Encryption required and "
70 "server that doesn't support "
71 "UNIX extensions - failing connect\n");
72 return NT_STATUS_NOT_SUPPORTED;
73 }
74
75 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
76 &caphigh);
77 if (!NT_STATUS_IS_OK(status)) {
78 d_printf("Encryption required and "
79 "can't get UNIX CIFS extensions "
80 "version from server.\n");
81 return NT_STATUS_UNKNOWN_REVISION;
82 }
83
84 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
85 d_printf("Encryption required and "
86 "share %s doesn't support "
87 "encryption.\n", sharename);
88 return NT_STATUS_UNSUPPORTED_COMPRESSION;
89 }
90
91 status = cli_smb1_setup_encryption(c, creds);
92 if (!NT_STATUS_IS_OK(status)) {
93 d_printf("Encryption required and "
94 "setup failed with error %s.\n",
95 nt_errstr(status));
96 return status;
97 }
98
99 return NT_STATUS_OK;
100 }
101
cli_cm_force_encryption(struct cli_state * c,const char * username,const char * password,const char * domain,const char * sharename)102 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
103 const char *username,
104 const char *password,
105 const char *domain,
106 const char *sharename)
107 {
108 struct cli_credentials *creds = NULL;
109 NTSTATUS status;
110
111 creds = cli_session_creds_init(c,
112 username,
113 domain,
114 NULL, /* default realm */
115 password,
116 c->use_kerberos,
117 c->fallback_after_kerberos,
118 c->use_ccache,
119 c->pw_nt_hash);
120 if (creds == NULL) {
121 return NT_STATUS_NO_MEMORY;
122 }
123
124 status = cli_cm_force_encryption_creds(c, creds, sharename);
125 /* gensec currently references the creds so we can't free them here */
126 talloc_unlink(c, creds);
127 return status;
128 }
129
130 /********************************************************************
131 Return a connection to a server.
132 ********************************************************************/
133
do_connect(TALLOC_CTX * ctx,const char * server,const char * share,const struct user_auth_info * auth_info,bool force_encrypt,int max_protocol,int port,int name_type,struct cli_state ** pcli)134 static NTSTATUS do_connect(TALLOC_CTX *ctx,
135 const char *server,
136 const char *share,
137 const struct user_auth_info *auth_info,
138 bool force_encrypt,
139 int max_protocol,
140 int port,
141 int name_type,
142 struct cli_state **pcli)
143 {
144 struct cli_state *c = NULL;
145 char *servicename;
146 char *sharename;
147 char *newserver, *newshare;
148 NTSTATUS status;
149 int flags = 0;
150 enum protocol_types protocol = PROTOCOL_NONE;
151 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
152 struct cli_credentials *creds = NULL;
153
154 if (force_encrypt) {
155 signing_state = SMB_SIGNING_REQUIRED;
156 }
157
158 /* make a copy so we don't modify the global string 'service' */
159 servicename = talloc_strdup(ctx,share);
160 if (!servicename) {
161 return NT_STATUS_NO_MEMORY;
162 }
163 sharename = servicename;
164 if (*sharename == '\\') {
165 sharename += 2;
166 if (server == NULL) {
167 server = sharename;
168 }
169 sharename = strchr_m(sharename,'\\');
170 if (!sharename) {
171 return NT_STATUS_NO_MEMORY;
172 }
173 *sharename = 0;
174 sharename++;
175 }
176 if (server == NULL) {
177 return NT_STATUS_INVALID_PARAMETER;
178 }
179
180 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
181 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
182 }
183 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
184 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
185 }
186 if (get_cmdline_auth_info_use_ccache(auth_info)) {
187 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
188 }
189 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
190 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
191 }
192
193 status = cli_connect_nb(
194 server, NULL, port, name_type, NULL,
195 signing_state,
196 flags, &c);
197
198 if (!NT_STATUS_IS_OK(status)) {
199 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
200 DBG_ERR("NetBIOS support disabled, unable to connect");
201 }
202
203 DBG_WARNING("Connection to %s failed (Error %s)\n",
204 server,
205 nt_errstr(status));
206 return status;
207 }
208
209 if (max_protocol == 0) {
210 max_protocol = PROTOCOL_LATEST;
211 }
212 DEBUG(4,(" session request ok\n"));
213
214 status = smbXcli_negprot(c->conn, c->timeout,
215 lp_client_min_protocol(),
216 max_protocol);
217
218 if (!NT_STATUS_IS_OK(status)) {
219 d_printf("protocol negotiation failed: %s\n",
220 nt_errstr(status));
221 cli_shutdown(c);
222 return status;
223 }
224 protocol = smbXcli_conn_protocol(c->conn);
225 DEBUG(4,(" negotiated dialect[%s] against server[%s]\n",
226 smb_protocol_types_string(protocol),
227 smbXcli_conn_remote_name(c->conn)));
228
229 if (protocol >= PROTOCOL_SMB2_02) {
230 /* Ensure we ask for some initial credits. */
231 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
232 }
233
234 creds = get_cmdline_auth_info_creds(auth_info);
235
236 status = cli_session_setup_creds(c, creds);
237 if (!NT_STATUS_IS_OK(status)) {
238 /* If a password was not supplied then
239 * try again with a null username. */
240 if (force_encrypt || smbXcli_conn_signing_mandatory(c->conn) ||
241 cli_credentials_authentication_requested(creds) ||
242 cli_credentials_is_anonymous(creds) ||
243 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
244 {
245 d_printf("session setup failed: %s\n",
246 nt_errstr(status));
247 if (NT_STATUS_EQUAL(status,
248 NT_STATUS_MORE_PROCESSING_REQUIRED))
249 d_printf("did you forget to run kinit?\n");
250 cli_shutdown(c);
251 return status;
252 }
253 d_printf("Anonymous login successful\n");
254 }
255
256 if (!NT_STATUS_IS_OK(status)) {
257 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
258 cli_shutdown(c);
259 return status;
260 }
261
262 DEBUG(4,(" session setup ok\n"));
263
264 /* here's the fun part....to support 'msdfs proxy' shares
265 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
266 here before trying to connect to the original share.
267 cli_check_msdfs_proxy() will fail if it is a normal share. */
268
269 if (smbXcli_conn_dfs_supported(c->conn) &&
270 cli_check_msdfs_proxy(ctx, c, sharename,
271 &newserver, &newshare,
272 force_encrypt, creds)) {
273 cli_shutdown(c);
274 return do_connect(ctx, newserver,
275 newshare, auth_info,
276 force_encrypt, max_protocol,
277 port, name_type, pcli);
278 }
279
280 /* must be a normal share */
281
282 status = cli_tree_connect_creds(c, sharename, "?????", creds);
283 if (!NT_STATUS_IS_OK(status)) {
284 d_printf("tree connect failed: %s\n", nt_errstr(status));
285 cli_shutdown(c);
286 return status;
287 }
288
289 if (force_encrypt) {
290 status = cli_cm_force_encryption_creds(c,
291 creds,
292 sharename);
293 if (!NT_STATUS_IS_OK(status)) {
294 cli_shutdown(c);
295 return status;
296 }
297 }
298
299 DEBUG(4,(" tconx ok\n"));
300 *pcli = c;
301 return NT_STATUS_OK;
302 }
303
304 /****************************************************************************
305 ****************************************************************************/
306
cli_set_mntpoint(struct cli_state * cli,const char * mnt)307 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
308 {
309 TALLOC_CTX *frame = talloc_stackframe();
310 char *name = clean_name(frame, mnt);
311 if (!name) {
312 TALLOC_FREE(frame);
313 return;
314 }
315 TALLOC_FREE(cli->dfs_mountpoint);
316 cli->dfs_mountpoint = talloc_strdup(cli, name);
317 TALLOC_FREE(frame);
318 }
319
320 /********************************************************************
321 Add a new connection to the list.
322 referring_cli == NULL means a new initial connection.
323 ********************************************************************/
324
cli_cm_connect(TALLOC_CTX * ctx,struct cli_state * referring_cli,const char * server,const char * share,const struct user_auth_info * auth_info,bool force_encrypt,int max_protocol,int port,int name_type,struct cli_state ** pcli)325 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
326 struct cli_state *referring_cli,
327 const char *server,
328 const char *share,
329 const struct user_auth_info *auth_info,
330 bool force_encrypt,
331 int max_protocol,
332 int port,
333 int name_type,
334 struct cli_state **pcli)
335 {
336 struct cli_state *cli = NULL;
337 NTSTATUS status;
338
339 status = do_connect(ctx, server, share,
340 auth_info,
341 force_encrypt, max_protocol,
342 port, name_type, &cli);
343
344 if (!NT_STATUS_IS_OK(status)) {
345 return status;
346 }
347
348 /*
349 * This can't happen, this test is to satisfy static
350 * checkers (clang)
351 */
352 if (cli == NULL) {
353 return NT_STATUS_NO_MEMORY;
354 }
355
356 /* Enter into the list. */
357 if (referring_cli) {
358 DLIST_ADD_END(referring_cli, cli);
359 }
360
361 if (referring_cli && referring_cli->requested_posix_capabilities) {
362 uint16_t major, minor;
363 uint32_t caplow, caphigh;
364 status = cli_unix_extensions_version(cli, &major, &minor,
365 &caplow, &caphigh);
366 if (NT_STATUS_IS_OK(status)) {
367 cli_set_unix_extensions_capabilities(cli,
368 major, minor,
369 caplow, caphigh);
370 }
371 }
372
373 *pcli = cli;
374 return NT_STATUS_OK;
375 }
376
377 /********************************************************************
378 Return a connection to a server on a particular share.
379 ********************************************************************/
380
cli_cm_find(struct cli_state * cli,const char * server,const char * share)381 static struct cli_state *cli_cm_find(struct cli_state *cli,
382 const char *server,
383 const char *share)
384 {
385 struct cli_state *p;
386
387 if (cli == NULL) {
388 return NULL;
389 }
390
391 /* Search to the start of the list. */
392 for (p = cli; p; p = DLIST_PREV(p)) {
393 const char *remote_name =
394 smbXcli_conn_remote_name(p->conn);
395
396 if (strequal(server, remote_name) &&
397 strequal(share,p->share)) {
398 return p;
399 }
400 }
401
402 /* Search to the end of the list. */
403 for (p = cli->next; p; p = p->next) {
404 const char *remote_name =
405 smbXcli_conn_remote_name(p->conn);
406
407 if (strequal(server, remote_name) &&
408 strequal(share,p->share)) {
409 return p;
410 }
411 }
412
413 return NULL;
414 }
415
416 /****************************************************************************
417 Open a client connection to a \\server\share.
418 ****************************************************************************/
419
cli_cm_open(TALLOC_CTX * ctx,struct cli_state * referring_cli,const char * server,const char * share,const struct user_auth_info * auth_info,bool force_encrypt,int max_protocol,int port,int name_type,struct cli_state ** pcli)420 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
421 struct cli_state *referring_cli,
422 const char *server,
423 const char *share,
424 const struct user_auth_info *auth_info,
425 bool force_encrypt,
426 int max_protocol,
427 int port,
428 int name_type,
429 struct cli_state **pcli)
430 {
431 /* Try to reuse an existing connection in this list. */
432 struct cli_state *c = cli_cm_find(referring_cli, server, share);
433 NTSTATUS status;
434
435 if (c) {
436 *pcli = c;
437 return NT_STATUS_OK;
438 }
439
440 if (auth_info == NULL) {
441 /* Can't do a new connection
442 * without auth info. */
443 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
444 "without auth info\n",
445 server, share );
446 return NT_STATUS_INVALID_PARAMETER;
447 }
448
449 status = cli_cm_connect(ctx,
450 referring_cli,
451 server,
452 share,
453 auth_info,
454 force_encrypt,
455 max_protocol,
456 port,
457 name_type,
458 &c);
459 if (!NT_STATUS_IS_OK(status)) {
460 return status;
461 }
462 *pcli = c;
463 return NT_STATUS_OK;
464 }
465
466 /****************************************************************************
467 ****************************************************************************/
468
cli_cm_display(struct cli_state * cli)469 void cli_cm_display(struct cli_state *cli)
470 {
471 int i;
472
473 for (i=0; cli; cli = cli->next,i++ ) {
474 d_printf("%d:\tserver=%s, share=%s\n",
475 i, smbXcli_conn_remote_name(cli->conn), cli->share);
476 }
477 }
478
479 /****************************************************************************
480 ****************************************************************************/
481
482 /****************************************************************************
483 ****************************************************************************/
484
485 #if 0
486 void cli_cm_set_credentials(struct user_auth_info *auth_info)
487 {
488 SAFE_FREE(cm_creds.username);
489 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
490 auth_info));
491
492 if (get_cmdline_auth_info_got_pass(auth_info)) {
493 cm_set_password(get_cmdline_auth_info_password(auth_info));
494 }
495
496 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
497 cm_creds.fallback_after_kerberos = false;
498 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
499 }
500 #endif
501
502 /**********************************************************************
503 split a dfs path into the server, share name, and extrapath components
504 **********************************************************************/
505
split_dfs_path(TALLOC_CTX * ctx,const char * nodepath,char ** pp_server,char ** pp_share,char ** pp_extrapath)506 static bool split_dfs_path(TALLOC_CTX *ctx,
507 const char *nodepath,
508 char **pp_server,
509 char **pp_share,
510 char **pp_extrapath)
511 {
512 char *p, *q;
513 char *path;
514
515 *pp_server = NULL;
516 *pp_share = NULL;
517 *pp_extrapath = NULL;
518
519 path = talloc_strdup(ctx, nodepath);
520 if (!path) {
521 goto fail;
522 }
523
524 if ( path[0] != '\\' ) {
525 goto fail;
526 }
527
528 p = strchr_m( path + 1, '\\' );
529 if ( !p ) {
530 goto fail;
531 }
532
533 *p = '\0';
534 p++;
535
536 /* Look for any extra/deep path */
537 q = strchr_m(p, '\\');
538 if (q != NULL) {
539 *q = '\0';
540 q++;
541 *pp_extrapath = talloc_strdup(ctx, q);
542 } else {
543 *pp_extrapath = talloc_strdup(ctx, "");
544 }
545 if (*pp_extrapath == NULL) {
546 goto fail;
547 }
548
549 *pp_share = talloc_strdup(ctx, p);
550 if (*pp_share == NULL) {
551 goto fail;
552 }
553
554 *pp_server = talloc_strdup(ctx, &path[1]);
555 if (*pp_server == NULL) {
556 goto fail;
557 }
558
559 TALLOC_FREE(path);
560 return true;
561
562 fail:
563 TALLOC_FREE(*pp_share);
564 TALLOC_FREE(*pp_extrapath);
565 TALLOC_FREE(path);
566 return false;
567 }
568
569 /****************************************************************************
570 Return the original path truncated at the directory component before
571 the first wildcard character. Trust the caller to provide a NULL
572 terminated string
573 ****************************************************************************/
574
clean_path(TALLOC_CTX * ctx,const char * path)575 static char *clean_path(TALLOC_CTX *ctx, const char *path)
576 {
577 size_t len;
578 char *p1, *p2, *p;
579 char *path_out;
580
581 /* No absolute paths. */
582 while (IS_DIRECTORY_SEP(*path)) {
583 path++;
584 }
585
586 path_out = talloc_strdup(ctx, path);
587 if (!path_out) {
588 return NULL;
589 }
590
591 p1 = strchr_m(path_out, '*');
592 p2 = strchr_m(path_out, '?');
593
594 if (p1 || p2) {
595 if (p1 && p2) {
596 p = MIN(p1,p2);
597 } else if (!p1) {
598 p = p2;
599 } else {
600 p = p1;
601 }
602 *p = '\0';
603
604 /* Now go back to the start of this component. */
605 p1 = strrchr_m(path_out, '/');
606 p2 = strrchr_m(path_out, '\\');
607 p = MAX(p1,p2);
608 if (p) {
609 *p = '\0';
610 }
611 }
612
613 /* Strip any trailing separator */
614
615 len = strlen(path_out);
616 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
617 path_out[len-1] = '\0';
618 }
619
620 return path_out;
621 }
622
623 /****************************************************************************
624 ****************************************************************************/
625
cli_dfs_make_full_path(TALLOC_CTX * ctx,struct cli_state * cli,const char * dir)626 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
627 struct cli_state *cli,
628 const char *dir)
629 {
630 char path_sep = '\\';
631
632 /* Ensure the extrapath doesn't start with a separator. */
633 while (IS_DIRECTORY_SEP(*dir)) {
634 dir++;
635 }
636
637 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
638 path_sep = '/';
639 }
640 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
641 path_sep,
642 smbXcli_conn_remote_name(cli->conn),
643 path_sep,
644 cli->share,
645 path_sep,
646 dir);
647 }
648
649 /********************************************************************
650 check for dfs referral
651 ********************************************************************/
652
cli_dfs_check_error(struct cli_state * cli,NTSTATUS expected,NTSTATUS status)653 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
654 NTSTATUS status)
655 {
656 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
657
658 if (!(smbXcli_conn_use_unicode(cli->conn))) {
659 return false;
660 }
661 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
662 return false;
663 }
664 if (NT_STATUS_EQUAL(status, expected)) {
665 return true;
666 }
667 return false;
668 }
669
670 /********************************************************************
671 Get the dfs referral link.
672 ********************************************************************/
673
cli_dfs_get_referral_ex(TALLOC_CTX * ctx,struct cli_state * cli,const char * path,uint16_t max_referral_level,struct client_dfs_referral ** refs,size_t * num_refs,size_t * consumed)674 NTSTATUS cli_dfs_get_referral_ex(TALLOC_CTX *ctx,
675 struct cli_state *cli,
676 const char *path,
677 uint16_t max_referral_level,
678 struct client_dfs_referral **refs,
679 size_t *num_refs,
680 size_t *consumed)
681 {
682 unsigned int param_len = 0;
683 uint16_t recv_flags2;
684 uint8_t *param = NULL;
685 uint8_t *rdata = NULL;
686 char *p;
687 char *endp;
688 smb_ucs2_t *path_ucs;
689 char *consumed_path = NULL;
690 uint16_t consumed_ucs;
691 uint16_t num_referrals;
692 struct client_dfs_referral *referrals = NULL;
693 NTSTATUS status;
694 TALLOC_CTX *frame = talloc_stackframe();
695
696 *num_refs = 0;
697 *refs = NULL;
698
699 param = talloc_array(talloc_tos(), uint8_t, 2);
700 if (!param) {
701 status = NT_STATUS_NO_MEMORY;
702 goto out;
703 }
704 SSVAL(param, 0, max_referral_level);
705
706 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
707 path, strlen(path)+1,
708 NULL);
709 if (!param) {
710 status = NT_STATUS_NO_MEMORY;
711 goto out;
712 }
713 param_len = talloc_get_size(param);
714 path_ucs = (smb_ucs2_t *)¶m[2];
715
716 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
717 DATA_BLOB in_input_buffer;
718 DATA_BLOB in_output_buffer = data_blob_null;
719 DATA_BLOB out_input_buffer = data_blob_null;
720 DATA_BLOB out_output_buffer = data_blob_null;
721
722 in_input_buffer.data = param;
723 in_input_buffer.length = param_len;
724
725 status = smb2cli_ioctl(cli->conn,
726 cli->timeout,
727 cli->smb2.session,
728 cli->smb2.tcon,
729 UINT64_MAX, /* in_fid_persistent */
730 UINT64_MAX, /* in_fid_volatile */
731 FSCTL_DFS_GET_REFERRALS,
732 0, /* in_max_input_length */
733 &in_input_buffer,
734 CLI_BUFFER_SIZE, /* in_max_output_length */
735 &in_output_buffer,
736 SMB2_IOCTL_FLAG_IS_FSCTL,
737 talloc_tos(),
738 &out_input_buffer,
739 &out_output_buffer);
740 if (!NT_STATUS_IS_OK(status)) {
741 goto out;
742 }
743
744 if (out_output_buffer.length < 4) {
745 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
746 goto out;
747 }
748
749 recv_flags2 = FLAGS2_UNICODE_STRINGS;
750 rdata = out_output_buffer.data;
751 endp = (char *)rdata + out_output_buffer.length;
752 } else {
753 unsigned int data_len = 0;
754 uint16_t setup[1];
755
756 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
757
758 status = cli_trans(talloc_tos(), cli, SMBtrans2,
759 NULL, 0xffff, 0, 0,
760 setup, 1, 0,
761 param, param_len, 2,
762 NULL, 0, CLI_BUFFER_SIZE,
763 &recv_flags2,
764 NULL, 0, NULL, /* rsetup */
765 NULL, 0, NULL,
766 &rdata, 4, &data_len);
767 if (!NT_STATUS_IS_OK(status)) {
768 goto out;
769 }
770
771 endp = (char *)rdata + data_len;
772 }
773
774 consumed_ucs = SVAL(rdata, 0);
775 num_referrals = SVAL(rdata, 2);
776
777 /* consumed_ucs is the number of bytes
778 * of the UCS2 path consumed not counting any
779 * terminating null. We need to convert
780 * back to unix charset and count again
781 * to get the number of bytes consumed from
782 * the incoming path. */
783
784 errno = 0;
785 if (pull_string_talloc(talloc_tos(),
786 NULL,
787 0,
788 &consumed_path,
789 path_ucs,
790 consumed_ucs,
791 STR_UNICODE) == 0) {
792 if (errno != 0) {
793 status = map_nt_error_from_unix(errno);
794 } else {
795 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
796 }
797 goto out;
798 }
799 if (consumed_path == NULL) {
800 status = map_nt_error_from_unix(errno);
801 goto out;
802 }
803 *consumed = strlen(consumed_path);
804
805 if (num_referrals != 0) {
806 uint16_t ref_version;
807 uint16_t ref_size;
808 int i;
809 uint16_t node_offset;
810
811 referrals = talloc_array(ctx, struct client_dfs_referral,
812 num_referrals);
813
814 if (!referrals) {
815 status = NT_STATUS_NO_MEMORY;
816 goto out;
817 }
818 /* start at the referrals array */
819
820 p = (char *)rdata+8;
821 for (i=0; i<num_referrals && p < endp; i++) {
822 if (p + 18 > endp) {
823 goto out;
824 }
825 ref_version = SVAL(p, 0);
826 ref_size = SVAL(p, 2);
827 node_offset = SVAL(p, 16);
828
829 if (ref_version != 3) {
830 p += ref_size;
831 continue;
832 }
833
834 referrals[i].proximity = SVAL(p, 8);
835 referrals[i].ttl = SVAL(p, 10);
836
837 if (p + node_offset > endp) {
838 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
839 goto out;
840 }
841 clistr_pull_talloc(referrals,
842 (const char *)rdata,
843 recv_flags2,
844 &referrals[i].dfspath,
845 p+node_offset,
846 PTR_DIFF(endp, p+node_offset),
847 STR_TERMINATE|STR_UNICODE);
848
849 if (!referrals[i].dfspath) {
850 status = map_nt_error_from_unix(errno);
851 goto out;
852 }
853 p += ref_size;
854 }
855 if (i < num_referrals) {
856 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
857 goto out;
858 }
859 }
860
861 *num_refs = num_referrals;
862 *refs = referrals;
863
864 out:
865
866 TALLOC_FREE(frame);
867 return status;
868 }
869
cli_dfs_get_referral(TALLOC_CTX * ctx,struct cli_state * cli,const char * path,struct client_dfs_referral ** refs,size_t * num_refs,size_t * consumed)870 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
871 struct cli_state *cli,
872 const char *path,
873 struct client_dfs_referral **refs,
874 size_t *num_refs,
875 size_t *consumed)
876 {
877 return cli_dfs_get_referral_ex(ctx,
878 cli,
879 path,
880 3,
881 refs, /* Max referral level we want */
882 num_refs,
883 consumed);
884 }
885
886 /********************************************************************
887 ********************************************************************/
888 struct cli_dfs_path_split {
889 char *server;
890 char *share;
891 char *extrapath;
892 };
893
cli_resolve_path(TALLOC_CTX * ctx,const char * mountpt,const struct user_auth_info * dfs_auth_info,struct cli_state * rootcli,const char * path,struct cli_state ** targetcli,char ** pp_targetpath)894 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
895 const char *mountpt,
896 const struct user_auth_info *dfs_auth_info,
897 struct cli_state *rootcli,
898 const char *path,
899 struct cli_state **targetcli,
900 char **pp_targetpath)
901 {
902 struct client_dfs_referral *refs = NULL;
903 size_t num_refs = 0;
904 size_t consumed = 0;
905 struct cli_state *cli_ipc = NULL;
906 char *dfs_path = NULL;
907 char *cleanpath = NULL;
908 char *extrapath = NULL;
909 int pathlen;
910 struct cli_state *newcli = NULL;
911 struct cli_state *ccli = NULL;
912 size_t count = 0;
913 char *newpath = NULL;
914 char *newmount = NULL;
915 char *ppath = NULL;
916 SMB_STRUCT_STAT sbuf;
917 uint32_t attributes;
918 NTSTATUS status;
919 struct smbXcli_tcon *root_tcon = NULL;
920 struct smbXcli_tcon *target_tcon = NULL;
921 struct cli_dfs_path_split *dfs_refs = NULL;
922
923 if ( !rootcli || !path || !targetcli ) {
924 return NT_STATUS_INVALID_PARAMETER;
925 }
926
927 /* Don't do anything if this is not a DFS root. */
928
929 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
930 root_tcon = rootcli->smb2.tcon;
931 } else {
932 root_tcon = rootcli->smb1.tcon;
933 }
934
935 /*
936 * Avoid more than one leading directory separator
937 */
938 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
939 path++;
940 }
941
942 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
943 *targetcli = rootcli;
944 *pp_targetpath = talloc_strdup(ctx, path);
945 if (!*pp_targetpath) {
946 return NT_STATUS_NO_MEMORY;
947 }
948 return NT_STATUS_OK;
949 }
950
951 *targetcli = NULL;
952
953 /* Send a trans2_query_path_info to check for a referral. */
954
955 cleanpath = clean_path(ctx, path);
956 if (!cleanpath) {
957 return NT_STATUS_NO_MEMORY;
958 }
959
960 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
961 if (!dfs_path) {
962 return NT_STATUS_NO_MEMORY;
963 }
964
965 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
966 if (NT_STATUS_IS_OK(status)) {
967 /* This is an ordinary path, just return it. */
968 *targetcli = rootcli;
969 *pp_targetpath = talloc_strdup(ctx, path);
970 if (!*pp_targetpath) {
971 return NT_STATUS_NO_MEMORY;
972 }
973 goto done;
974 }
975
976 /* Special case where client asked for a path that does not exist */
977
978 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
979 status)) {
980 *targetcli = rootcli;
981 *pp_targetpath = talloc_strdup(ctx, path);
982 if (!*pp_targetpath) {
983 return NT_STATUS_NO_MEMORY;
984 }
985 goto done;
986 }
987
988 /* We got an error, check for DFS referral. */
989
990 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
991 status)) {
992 return status;
993 }
994
995 /* Check for the referral. */
996
997 status = cli_cm_open(ctx,
998 rootcli,
999 smbXcli_conn_remote_name(rootcli->conn),
1000 "IPC$",
1001 dfs_auth_info,
1002 cli_state_is_encryption_on(rootcli),
1003 smbXcli_conn_protocol(rootcli->conn),
1004 0,
1005 0x20,
1006 &cli_ipc);
1007 if (!NT_STATUS_IS_OK(status)) {
1008 return status;
1009 }
1010
1011 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
1012 &num_refs, &consumed);
1013 if (!NT_STATUS_IS_OK(status)) {
1014 return status;
1015 }
1016
1017 if (!num_refs || !refs[0].dfspath) {
1018 return NT_STATUS_NOT_FOUND;
1019 }
1020
1021 /*
1022 * Bug#10123 - DFS referal entries can be provided in a random order,
1023 * so check the connection cache for each item to avoid unnecessary
1024 * reconnections.
1025 */
1026 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
1027 if (dfs_refs == NULL) {
1028 return NT_STATUS_NO_MEMORY;
1029 }
1030
1031 for (count = 0; count < num_refs; count++) {
1032 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1033 &dfs_refs[count].server,
1034 &dfs_refs[count].share,
1035 &dfs_refs[count].extrapath)) {
1036 TALLOC_FREE(dfs_refs);
1037 return NT_STATUS_NOT_FOUND;
1038 }
1039
1040 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1041 dfs_refs[count].share);
1042 if (ccli != NULL) {
1043 extrapath = dfs_refs[count].extrapath;
1044 *targetcli = ccli;
1045 break;
1046 }
1047 }
1048
1049 /*
1050 * If no cached connection was found, then connect to the first live
1051 * referral server in the list.
1052 */
1053 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1054 /* Connect to the target server & share */
1055 status = cli_cm_connect(ctx, rootcli,
1056 dfs_refs[count].server,
1057 dfs_refs[count].share,
1058 dfs_auth_info,
1059 cli_state_is_encryption_on(rootcli),
1060 smbXcli_conn_protocol(rootcli->conn),
1061 0,
1062 0x20,
1063 targetcli);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1066 dfs_refs[count].server,
1067 dfs_refs[count].share);
1068 continue;
1069 } else {
1070 extrapath = dfs_refs[count].extrapath;
1071 break;
1072 }
1073 }
1074
1075 /* No available referral server for the connection */
1076 if (*targetcli == NULL) {
1077 TALLOC_FREE(dfs_refs);
1078 return status;
1079 }
1080
1081 /* Make sure to recreate the original string including any wildcards. */
1082
1083 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1084 if (!dfs_path) {
1085 TALLOC_FREE(dfs_refs);
1086 return NT_STATUS_NO_MEMORY;
1087 }
1088 pathlen = strlen(dfs_path);
1089 consumed = MIN(pathlen, consumed);
1090 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1091 if (!*pp_targetpath) {
1092 TALLOC_FREE(dfs_refs);
1093 return NT_STATUS_NO_MEMORY;
1094 }
1095 dfs_path[consumed] = '\0';
1096
1097 /*
1098 * *pp_targetpath is now the unconsumed part of the path.
1099 * dfs_path is now the consumed part of the path
1100 * (in \server\share\path format).
1101 */
1102
1103 if (extrapath && strlen(extrapath) > 0) {
1104 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1105 /* put the trailing \ on the path, so to be save we put one in if needed */
1106 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1107 *pp_targetpath = talloc_asprintf(ctx,
1108 "%s\\%s",
1109 extrapath,
1110 *pp_targetpath);
1111 } else {
1112 *pp_targetpath = talloc_asprintf(ctx,
1113 "%s%s",
1114 extrapath,
1115 *pp_targetpath);
1116 }
1117 if (!*pp_targetpath) {
1118 TALLOC_FREE(dfs_refs);
1119 return NT_STATUS_NO_MEMORY;
1120 }
1121 }
1122
1123 /* parse out the consumed mount path */
1124 /* trim off the \server\share\ */
1125
1126 ppath = dfs_path;
1127
1128 if (*ppath != '\\') {
1129 d_printf("cli_resolve_path: "
1130 "dfs_path (%s) not in correct format.\n",
1131 dfs_path );
1132 TALLOC_FREE(dfs_refs);
1133 return NT_STATUS_NOT_FOUND;
1134 }
1135
1136 ppath++; /* Now pointing at start of server name. */
1137
1138 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1139 TALLOC_FREE(dfs_refs);
1140 return NT_STATUS_NOT_FOUND;
1141 }
1142
1143 ppath++; /* Now pointing at start of share name. */
1144
1145 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1146 TALLOC_FREE(dfs_refs);
1147 return NT_STATUS_NOT_FOUND;
1148 }
1149
1150 ppath++; /* Now pointing at path component. */
1151
1152 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1153 if (!newmount) {
1154 TALLOC_FREE(dfs_refs);
1155 return NT_STATUS_NOT_FOUND;
1156 }
1157
1158 cli_set_mntpoint(*targetcli, newmount);
1159
1160 /* Check for another dfs referral, note that we are not
1161 checking for loops here. */
1162
1163 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1164 status = cli_resolve_path(ctx,
1165 newmount,
1166 dfs_auth_info,
1167 *targetcli,
1168 *pp_targetpath,
1169 &newcli,
1170 &newpath);
1171 if (NT_STATUS_IS_OK(status)) {
1172 /*
1173 * When cli_resolve_path returns true here it's always
1174 * returning the complete path in newpath, so we're done
1175 * here.
1176 */
1177 *targetcli = newcli;
1178 *pp_targetpath = newpath;
1179 TALLOC_FREE(dfs_refs);
1180 return status;
1181 }
1182 }
1183
1184 done:
1185
1186 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1187 target_tcon = (*targetcli)->smb2.tcon;
1188 } else {
1189 target_tcon = (*targetcli)->smb1.tcon;
1190 }
1191
1192 /* If returning true ensure we return a dfs root full path. */
1193 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1194 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1195 if (!dfs_path) {
1196 TALLOC_FREE(dfs_refs);
1197 return NT_STATUS_NO_MEMORY;
1198 }
1199 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1200 if (*pp_targetpath == NULL) {
1201 TALLOC_FREE(dfs_refs);
1202 return NT_STATUS_NO_MEMORY;
1203 }
1204 }
1205
1206 TALLOC_FREE(dfs_refs);
1207 return NT_STATUS_OK;
1208 }
1209
1210 /********************************************************************
1211 ********************************************************************/
1212
cli_check_msdfs_proxy(TALLOC_CTX * ctx,struct cli_state * cli,const char * sharename,char ** pp_newserver,char ** pp_newshare,bool force_encrypt,struct cli_credentials * creds)1213 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1214 struct cli_state *cli,
1215 const char *sharename,
1216 char **pp_newserver,
1217 char **pp_newshare,
1218 bool force_encrypt,
1219 struct cli_credentials *creds)
1220 {
1221 struct client_dfs_referral *refs = NULL;
1222 size_t num_refs = 0;
1223 size_t consumed = 0;
1224 char *fullpath = NULL;
1225 bool res;
1226 struct smbXcli_tcon *orig_tcon = NULL;
1227 char *newextrapath = NULL;
1228 NTSTATUS status;
1229 const char *remote_name;
1230
1231 if (!cli || !sharename) {
1232 return false;
1233 }
1234
1235 remote_name = smbXcli_conn_remote_name(cli->conn);
1236
1237 /* special case. never check for a referral on the IPC$ share */
1238
1239 if (strequal(sharename, "IPC$")) {
1240 return false;
1241 }
1242
1243 /* send a trans2_query_path_info to check for a referral */
1244
1245 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1246 if (!fullpath) {
1247 return false;
1248 }
1249
1250 /* Store tcon state. */
1251 if (cli_state_has_tcon(cli)) {
1252 orig_tcon = cli_state_save_tcon(cli);
1253 if (orig_tcon == NULL) {
1254 return false;
1255 }
1256 }
1257
1258 /* check for the referral */
1259
1260 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1261 cli_state_restore_tcon(cli, orig_tcon);
1262 return false;
1263 }
1264
1265 if (force_encrypt) {
1266 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1267 if (!NT_STATUS_IS_OK(status)) {
1268 cli_state_restore_tcon(cli, orig_tcon);
1269 return false;
1270 }
1271 }
1272
1273 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1274 &num_refs, &consumed);
1275 res = NT_STATUS_IS_OK(status);
1276
1277 status = cli_tdis(cli);
1278
1279 cli_state_restore_tcon(cli, orig_tcon);
1280
1281 if (!NT_STATUS_IS_OK(status)) {
1282 return false;
1283 }
1284
1285 if (!res || !num_refs) {
1286 return false;
1287 }
1288
1289 if (!refs[0].dfspath) {
1290 return false;
1291 }
1292
1293 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1294 pp_newshare, &newextrapath)) {
1295 return false;
1296 }
1297
1298 /* check that this is not a self-referral */
1299
1300 if (strequal(remote_name, *pp_newserver) &&
1301 strequal(sharename, *pp_newshare)) {
1302 return false;
1303 }
1304
1305 return true;
1306 }
1307