smbXcli: Add "force_channel_sequence"
[Samba.git] / source3 / libsmb / clidfs.c
blob0d851d2cef893cfdae66d4ce5afbfe3c2344a98a
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
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.
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.
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/>.
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"
31 /********************************************************************
32 Important point.
34 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
35 are not C escaped here).
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 ********************************************************************/
42 /********************************************************************
43 Ensure a connection is encrypted.
44 ********************************************************************/
46 NTSTATUS cli_cm_force_encryption_creds(struct cli_state *c,
47 struct cli_credentials *creds,
48 const char *sharename)
50 uint16_t major, minor;
51 uint32_t caplow, caphigh;
52 NTSTATUS status;
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));
65 return status;
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;
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;
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;
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;
99 return NT_STATUS_OK;
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)
108 struct cli_credentials *creds = NULL;
109 NTSTATUS status;
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;
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;
130 /********************************************************************
131 Return a connection to a server.
132 ********************************************************************/
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)
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;
154 if (force_encrypt) {
155 signing_state = SMB_SIGNING_REQUIRED;
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;
163 sharename = servicename;
164 if (*sharename == '\\') {
165 sharename += 2;
166 if (server == NULL) {
167 server = sharename;
169 sharename = strchr_m(sharename,'\\');
170 if (!sharename) {
171 return NT_STATUS_NO_MEMORY;
173 *sharename = 0;
174 sharename++;
176 if (server == NULL) {
177 return NT_STATUS_INVALID_PARAMETER;
180 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
181 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
183 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
184 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
186 if (get_cmdline_auth_info_use_ccache(auth_info)) {
187 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
189 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
190 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
193 status = cli_connect_nb(
194 server, NULL, port, name_type, NULL,
195 signing_state,
196 flags, &c);
198 if (!NT_STATUS_IS_OK(status)) {
199 d_printf("Connection to %s failed (Error %s)\n",
200 server,
201 nt_errstr(status));
202 return status;
205 if (max_protocol == 0) {
206 max_protocol = PROTOCOL_LATEST;
208 DEBUG(4,(" session request ok\n"));
210 status = smbXcli_negprot(c->conn, c->timeout,
211 lp_client_min_protocol(),
212 max_protocol);
214 if (!NT_STATUS_IS_OK(status)) {
215 d_printf("protocol negotiation failed: %s\n",
216 nt_errstr(status));
217 cli_shutdown(c);
218 return status;
220 protocol = smbXcli_conn_protocol(c->conn);
221 DEBUG(4,(" negotiated dialect[%s] against server[%s]\n",
222 smb_protocol_types_string(protocol),
223 smbXcli_conn_remote_name(c->conn)));
225 if (protocol >= PROTOCOL_SMB2_02) {
226 /* Ensure we ask for some initial credits. */
227 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
230 creds = get_cmdline_auth_info_creds(auth_info);
232 status = cli_session_setup_creds(c, creds);
233 if (!NT_STATUS_IS_OK(status)) {
234 /* If a password was not supplied then
235 * try again with a null username. */
236 if (force_encrypt || smbXcli_conn_signing_mandatory(c->conn) ||
237 cli_credentials_authentication_requested(creds) ||
238 cli_credentials_is_anonymous(creds) ||
239 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
241 d_printf("session setup failed: %s\n",
242 nt_errstr(status));
243 if (NT_STATUS_EQUAL(status,
244 NT_STATUS_MORE_PROCESSING_REQUIRED))
245 d_printf("did you forget to run kinit?\n");
246 cli_shutdown(c);
247 return status;
249 d_printf("Anonymous login successful\n");
252 if (!NT_STATUS_IS_OK(status)) {
253 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
254 cli_shutdown(c);
255 return status;
258 DEBUG(4,(" session setup ok\n"));
260 /* here's the fun part....to support 'msdfs proxy' shares
261 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
262 here before trying to connect to the original share.
263 cli_check_msdfs_proxy() will fail if it is a normal share. */
265 if (smbXcli_conn_dfs_supported(c->conn) &&
266 cli_check_msdfs_proxy(ctx, c, sharename,
267 &newserver, &newshare,
268 force_encrypt, creds)) {
269 cli_shutdown(c);
270 return do_connect(ctx, newserver,
271 newshare, auth_info,
272 force_encrypt, max_protocol,
273 port, name_type, pcli);
276 /* must be a normal share */
278 status = cli_tree_connect_creds(c, sharename, "?????", creds);
279 if (!NT_STATUS_IS_OK(status)) {
280 d_printf("tree connect failed: %s\n", nt_errstr(status));
281 cli_shutdown(c);
282 return status;
285 if (force_encrypt) {
286 status = cli_cm_force_encryption_creds(c,
287 creds,
288 sharename);
289 if (!NT_STATUS_IS_OK(status)) {
290 cli_shutdown(c);
291 return status;
295 DEBUG(4,(" tconx ok\n"));
296 *pcli = c;
297 return NT_STATUS_OK;
300 /****************************************************************************
301 ****************************************************************************/
303 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
305 TALLOC_CTX *frame = talloc_stackframe();
306 char *name = clean_name(frame, mnt);
307 if (!name) {
308 TALLOC_FREE(frame);
309 return;
311 TALLOC_FREE(cli->dfs_mountpoint);
312 cli->dfs_mountpoint = talloc_strdup(cli, name);
313 TALLOC_FREE(frame);
316 /********************************************************************
317 Add a new connection to the list.
318 referring_cli == NULL means a new initial connection.
319 ********************************************************************/
321 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
322 struct cli_state *referring_cli,
323 const char *server,
324 const char *share,
325 const struct user_auth_info *auth_info,
326 bool force_encrypt,
327 int max_protocol,
328 int port,
329 int name_type,
330 struct cli_state **pcli)
332 struct cli_state *cli;
333 NTSTATUS status;
335 status = do_connect(ctx, server, share,
336 auth_info,
337 force_encrypt, max_protocol,
338 port, name_type, &cli);
340 if (!NT_STATUS_IS_OK(status)) {
341 return status;
344 /* Enter into the list. */
345 if (referring_cli) {
346 DLIST_ADD_END(referring_cli, cli);
349 if (referring_cli && referring_cli->requested_posix_capabilities) {
350 uint16_t major, minor;
351 uint32_t caplow, caphigh;
352 status = cli_unix_extensions_version(cli, &major, &minor,
353 &caplow, &caphigh);
354 if (NT_STATUS_IS_OK(status)) {
355 cli_set_unix_extensions_capabilities(cli,
356 major, minor,
357 caplow, caphigh);
361 *pcli = cli;
362 return NT_STATUS_OK;
365 /********************************************************************
366 Return a connection to a server on a particular share.
367 ********************************************************************/
369 static struct cli_state *cli_cm_find(struct cli_state *cli,
370 const char *server,
371 const char *share)
373 struct cli_state *p;
375 if (cli == NULL) {
376 return NULL;
379 /* Search to the start of the list. */
380 for (p = cli; p; p = DLIST_PREV(p)) {
381 const char *remote_name =
382 smbXcli_conn_remote_name(p->conn);
384 if (strequal(server, remote_name) &&
385 strequal(share,p->share)) {
386 return p;
390 /* Search to the end of the list. */
391 for (p = cli->next; p; p = p->next) {
392 const char *remote_name =
393 smbXcli_conn_remote_name(p->conn);
395 if (strequal(server, remote_name) &&
396 strequal(share,p->share)) {
397 return p;
401 return NULL;
404 /****************************************************************************
405 Open a client connection to a \\server\share.
406 ****************************************************************************/
408 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
409 struct cli_state *referring_cli,
410 const char *server,
411 const char *share,
412 const struct user_auth_info *auth_info,
413 bool force_encrypt,
414 int max_protocol,
415 int port,
416 int name_type,
417 struct cli_state **pcli)
419 /* Try to reuse an existing connection in this list. */
420 struct cli_state *c = cli_cm_find(referring_cli, server, share);
421 NTSTATUS status;
423 if (c) {
424 *pcli = c;
425 return NT_STATUS_OK;
428 if (auth_info == NULL) {
429 /* Can't do a new connection
430 * without auth info. */
431 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
432 "without auth info\n",
433 server, share );
434 return NT_STATUS_INVALID_PARAMETER;
437 status = cli_cm_connect(ctx,
438 referring_cli,
439 server,
440 share,
441 auth_info,
442 force_encrypt,
443 max_protocol,
444 port,
445 name_type,
446 &c);
447 if (!NT_STATUS_IS_OK(status)) {
448 return status;
450 *pcli = c;
451 return NT_STATUS_OK;
454 /****************************************************************************
455 ****************************************************************************/
457 void cli_cm_display(struct cli_state *cli)
459 int i;
461 for (i=0; cli; cli = cli->next,i++ ) {
462 d_printf("%d:\tserver=%s, share=%s\n",
463 i, smbXcli_conn_remote_name(cli->conn), cli->share);
467 /****************************************************************************
468 ****************************************************************************/
470 /****************************************************************************
471 ****************************************************************************/
473 #if 0
474 void cli_cm_set_credentials(struct user_auth_info *auth_info)
476 SAFE_FREE(cm_creds.username);
477 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
478 auth_info));
480 if (get_cmdline_auth_info_got_pass(auth_info)) {
481 cm_set_password(get_cmdline_auth_info_password(auth_info));
484 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
485 cm_creds.fallback_after_kerberos = false;
486 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
488 #endif
490 /**********************************************************************
491 split a dfs path into the server, share name, and extrapath components
492 **********************************************************************/
494 static bool split_dfs_path(TALLOC_CTX *ctx,
495 const char *nodepath,
496 char **pp_server,
497 char **pp_share,
498 char **pp_extrapath)
500 char *p, *q;
501 char *path;
503 *pp_server = NULL;
504 *pp_share = NULL;
505 *pp_extrapath = NULL;
507 path = talloc_strdup(ctx, nodepath);
508 if (!path) {
509 goto fail;
512 if ( path[0] != '\\' ) {
513 goto fail;
516 p = strchr_m( path + 1, '\\' );
517 if ( !p ) {
518 goto fail;
521 *p = '\0';
522 p++;
524 /* Look for any extra/deep path */
525 q = strchr_m(p, '\\');
526 if (q != NULL) {
527 *q = '\0';
528 q++;
529 *pp_extrapath = talloc_strdup(ctx, q);
530 } else {
531 *pp_extrapath = talloc_strdup(ctx, "");
533 if (*pp_extrapath == NULL) {
534 goto fail;
537 *pp_share = talloc_strdup(ctx, p);
538 if (*pp_share == NULL) {
539 goto fail;
542 *pp_server = talloc_strdup(ctx, &path[1]);
543 if (*pp_server == NULL) {
544 goto fail;
547 TALLOC_FREE(path);
548 return true;
550 fail:
551 TALLOC_FREE(*pp_share);
552 TALLOC_FREE(*pp_extrapath);
553 TALLOC_FREE(path);
554 return false;
557 /****************************************************************************
558 Return the original path truncated at the directory component before
559 the first wildcard character. Trust the caller to provide a NULL
560 terminated string
561 ****************************************************************************/
563 static char *clean_path(TALLOC_CTX *ctx, const char *path)
565 size_t len;
566 char *p1, *p2, *p;
567 char *path_out;
569 /* No absolute paths. */
570 while (IS_DIRECTORY_SEP(*path)) {
571 path++;
574 path_out = talloc_strdup(ctx, path);
575 if (!path_out) {
576 return NULL;
579 p1 = strchr_m(path_out, '*');
580 p2 = strchr_m(path_out, '?');
582 if (p1 || p2) {
583 if (p1 && p2) {
584 p = MIN(p1,p2);
585 } else if (!p1) {
586 p = p2;
587 } else {
588 p = p1;
590 *p = '\0';
592 /* Now go back to the start of this component. */
593 p1 = strrchr_m(path_out, '/');
594 p2 = strrchr_m(path_out, '\\');
595 p = MAX(p1,p2);
596 if (p) {
597 *p = '\0';
601 /* Strip any trailing separator */
603 len = strlen(path_out);
604 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
605 path_out[len-1] = '\0';
608 return path_out;
611 /****************************************************************************
612 ****************************************************************************/
614 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
615 struct cli_state *cli,
616 const char *dir)
618 char path_sep = '\\';
620 /* Ensure the extrapath doesn't start with a separator. */
621 while (IS_DIRECTORY_SEP(*dir)) {
622 dir++;
625 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
626 path_sep = '/';
628 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
629 path_sep,
630 smbXcli_conn_remote_name(cli->conn),
631 path_sep,
632 cli->share,
633 path_sep,
634 dir);
637 /********************************************************************
638 check for dfs referral
639 ********************************************************************/
641 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
642 NTSTATUS status)
644 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
646 if (!(smbXcli_conn_use_unicode(cli->conn))) {
647 return false;
649 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
650 return false;
652 if (NT_STATUS_EQUAL(status, expected)) {
653 return true;
655 return false;
658 /********************************************************************
659 Get the dfs referral link.
660 ********************************************************************/
662 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
663 struct cli_state *cli,
664 const char *path,
665 struct client_dfs_referral **refs,
666 size_t *num_refs,
667 size_t *consumed)
669 unsigned int param_len = 0;
670 uint16_t recv_flags2;
671 uint8_t *param = NULL;
672 uint8_t *rdata = NULL;
673 char *p;
674 char *endp;
675 smb_ucs2_t *path_ucs;
676 char *consumed_path = NULL;
677 uint16_t consumed_ucs;
678 uint16_t num_referrals;
679 struct client_dfs_referral *referrals = NULL;
680 NTSTATUS status;
681 TALLOC_CTX *frame = talloc_stackframe();
683 *num_refs = 0;
684 *refs = NULL;
686 param = talloc_array(talloc_tos(), uint8_t, 2);
687 if (!param) {
688 status = NT_STATUS_NO_MEMORY;
689 goto out;
691 SSVAL(param, 0, 0x03); /* max referral level */
693 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
694 path, strlen(path)+1,
695 NULL);
696 if (!param) {
697 status = NT_STATUS_NO_MEMORY;
698 goto out;
700 param_len = talloc_get_size(param);
701 path_ucs = (smb_ucs2_t *)&param[2];
703 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
704 DATA_BLOB in_input_buffer;
705 DATA_BLOB in_output_buffer = data_blob_null;
706 DATA_BLOB out_input_buffer = data_blob_null;
707 DATA_BLOB out_output_buffer = data_blob_null;
709 in_input_buffer.data = param;
710 in_input_buffer.length = param_len;
712 status = smb2cli_ioctl(cli->conn,
713 cli->timeout,
714 cli->smb2.session,
715 cli->smb2.tcon,
716 UINT64_MAX, /* in_fid_persistent */
717 UINT64_MAX, /* in_fid_volatile */
718 FSCTL_DFS_GET_REFERRALS,
719 0, /* in_max_input_length */
720 &in_input_buffer,
721 CLI_BUFFER_SIZE, /* in_max_output_length */
722 &in_output_buffer,
723 SMB2_IOCTL_FLAG_IS_FSCTL,
724 talloc_tos(),
725 &out_input_buffer,
726 &out_output_buffer);
727 if (!NT_STATUS_IS_OK(status)) {
728 goto out;
731 if (out_output_buffer.length < 4) {
732 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
733 goto out;
736 recv_flags2 = FLAGS2_UNICODE_STRINGS;
737 rdata = out_output_buffer.data;
738 endp = (char *)rdata + out_output_buffer.length;
739 } else {
740 unsigned int data_len = 0;
741 uint16_t setup[1];
743 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
745 status = cli_trans(talloc_tos(), cli, SMBtrans2,
746 NULL, 0xffff, 0, 0,
747 setup, 1, 0,
748 param, param_len, 2,
749 NULL, 0, CLI_BUFFER_SIZE,
750 &recv_flags2,
751 NULL, 0, NULL, /* rsetup */
752 NULL, 0, NULL,
753 &rdata, 4, &data_len);
754 if (!NT_STATUS_IS_OK(status)) {
755 goto out;
758 endp = (char *)rdata + data_len;
761 consumed_ucs = SVAL(rdata, 0);
762 num_referrals = SVAL(rdata, 2);
764 /* consumed_ucs is the number of bytes
765 * of the UCS2 path consumed not counting any
766 * terminating null. We need to convert
767 * back to unix charset and count again
768 * to get the number of bytes consumed from
769 * the incoming path. */
771 errno = 0;
772 if (pull_string_talloc(talloc_tos(),
773 NULL,
775 &consumed_path,
776 path_ucs,
777 consumed_ucs,
778 STR_UNICODE) == 0) {
779 if (errno != 0) {
780 status = map_nt_error_from_unix(errno);
781 } else {
782 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
784 goto out;
786 if (consumed_path == NULL) {
787 status = map_nt_error_from_unix(errno);
788 goto out;
790 *consumed = strlen(consumed_path);
792 if (num_referrals != 0) {
793 uint16_t ref_version;
794 uint16_t ref_size;
795 int i;
796 uint16_t node_offset;
798 referrals = talloc_array(ctx, struct client_dfs_referral,
799 num_referrals);
801 if (!referrals) {
802 status = NT_STATUS_NO_MEMORY;
803 goto out;
805 /* start at the referrals array */
807 p = (char *)rdata+8;
808 for (i=0; i<num_referrals && p < endp; i++) {
809 if (p + 18 > endp) {
810 goto out;
812 ref_version = SVAL(p, 0);
813 ref_size = SVAL(p, 2);
814 node_offset = SVAL(p, 16);
816 if (ref_version != 3) {
817 p += ref_size;
818 continue;
821 referrals[i].proximity = SVAL(p, 8);
822 referrals[i].ttl = SVAL(p, 10);
824 if (p + node_offset > endp) {
825 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
826 goto out;
828 clistr_pull_talloc(referrals,
829 (const char *)rdata,
830 recv_flags2,
831 &referrals[i].dfspath,
832 p+node_offset,
833 PTR_DIFF(endp, p+node_offset),
834 STR_TERMINATE|STR_UNICODE);
836 if (!referrals[i].dfspath) {
837 status = map_nt_error_from_unix(errno);
838 goto out;
840 p += ref_size;
842 if (i < num_referrals) {
843 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
844 goto out;
848 *num_refs = num_referrals;
849 *refs = referrals;
851 out:
853 TALLOC_FREE(frame);
854 return status;
857 /********************************************************************
858 ********************************************************************/
859 struct cli_dfs_path_split {
860 char *server;
861 char *share;
862 char *extrapath;
865 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
866 const char *mountpt,
867 const struct user_auth_info *dfs_auth_info,
868 struct cli_state *rootcli,
869 const char *path,
870 struct cli_state **targetcli,
871 char **pp_targetpath)
873 struct client_dfs_referral *refs = NULL;
874 size_t num_refs = 0;
875 size_t consumed = 0;
876 struct cli_state *cli_ipc = NULL;
877 char *dfs_path = NULL;
878 char *cleanpath = NULL;
879 char *extrapath = NULL;
880 int pathlen;
881 struct cli_state *newcli = NULL;
882 struct cli_state *ccli = NULL;
883 int count = 0;
884 char *newpath = NULL;
885 char *newmount = NULL;
886 char *ppath = NULL;
887 SMB_STRUCT_STAT sbuf;
888 uint32_t attributes;
889 NTSTATUS status;
890 struct smbXcli_tcon *root_tcon = NULL;
891 struct smbXcli_tcon *target_tcon = NULL;
892 struct cli_dfs_path_split *dfs_refs = NULL;
894 if ( !rootcli || !path || !targetcli ) {
895 return NT_STATUS_INVALID_PARAMETER;
898 /* Don't do anything if this is not a DFS root. */
900 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
901 root_tcon = rootcli->smb2.tcon;
902 } else {
903 root_tcon = rootcli->smb1.tcon;
907 * Avoid more than one leading directory separator
909 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
910 path++;
913 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
914 *targetcli = rootcli;
915 *pp_targetpath = talloc_strdup(ctx, path);
916 if (!*pp_targetpath) {
917 return NT_STATUS_NO_MEMORY;
919 return NT_STATUS_OK;
922 *targetcli = NULL;
924 /* Send a trans2_query_path_info to check for a referral. */
926 cleanpath = clean_path(ctx, path);
927 if (!cleanpath) {
928 return NT_STATUS_NO_MEMORY;
931 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
932 if (!dfs_path) {
933 return NT_STATUS_NO_MEMORY;
936 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
937 if (NT_STATUS_IS_OK(status)) {
938 /* This is an ordinary path, just return it. */
939 *targetcli = rootcli;
940 *pp_targetpath = talloc_strdup(ctx, path);
941 if (!*pp_targetpath) {
942 return NT_STATUS_NO_MEMORY;
944 goto done;
947 /* Special case where client asked for a path that does not exist */
949 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
950 status)) {
951 *targetcli = rootcli;
952 *pp_targetpath = talloc_strdup(ctx, path);
953 if (!*pp_targetpath) {
954 return NT_STATUS_NO_MEMORY;
956 goto done;
959 /* We got an error, check for DFS referral. */
961 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
962 status)) {
963 return status;
966 /* Check for the referral. */
968 status = cli_cm_open(ctx,
969 rootcli,
970 smbXcli_conn_remote_name(rootcli->conn),
971 "IPC$",
972 dfs_auth_info,
973 cli_state_is_encryption_on(rootcli),
974 smbXcli_conn_protocol(rootcli->conn),
976 0x20,
977 &cli_ipc);
978 if (!NT_STATUS_IS_OK(status)) {
979 return status;
982 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
983 &num_refs, &consumed);
984 if (!NT_STATUS_IS_OK(status)) {
985 return status;
988 if (!num_refs || !refs[0].dfspath) {
989 return NT_STATUS_NOT_FOUND;
993 * Bug#10123 - DFS referal entries can be provided in a random order,
994 * so check the connection cache for each item to avoid unnecessary
995 * reconnections.
997 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
998 if (dfs_refs == NULL) {
999 return NT_STATUS_NO_MEMORY;
1002 for (count = 0; count < num_refs; count++) {
1003 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1004 &dfs_refs[count].server,
1005 &dfs_refs[count].share,
1006 &dfs_refs[count].extrapath)) {
1007 TALLOC_FREE(dfs_refs);
1008 return NT_STATUS_NOT_FOUND;
1011 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1012 dfs_refs[count].share);
1013 if (ccli != NULL) {
1014 extrapath = dfs_refs[count].extrapath;
1015 *targetcli = ccli;
1016 break;
1021 * If no cached connection was found, then connect to the first live
1022 * referral server in the list.
1024 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1025 /* Connect to the target server & share */
1026 status = cli_cm_connect(ctx, rootcli,
1027 dfs_refs[count].server,
1028 dfs_refs[count].share,
1029 dfs_auth_info,
1030 cli_state_is_encryption_on(rootcli),
1031 smbXcli_conn_protocol(rootcli->conn),
1033 0x20,
1034 targetcli);
1035 if (!NT_STATUS_IS_OK(status)) {
1036 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1037 dfs_refs[count].server,
1038 dfs_refs[count].share);
1039 continue;
1040 } else {
1041 extrapath = dfs_refs[count].extrapath;
1042 break;
1046 /* No available referral server for the connection */
1047 if (*targetcli == NULL) {
1048 TALLOC_FREE(dfs_refs);
1049 return status;
1052 /* Make sure to recreate the original string including any wildcards. */
1054 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1055 if (!dfs_path) {
1056 TALLOC_FREE(dfs_refs);
1057 return NT_STATUS_NO_MEMORY;
1059 pathlen = strlen(dfs_path);
1060 consumed = MIN(pathlen, consumed);
1061 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1062 if (!*pp_targetpath) {
1063 TALLOC_FREE(dfs_refs);
1064 return NT_STATUS_NO_MEMORY;
1066 dfs_path[consumed] = '\0';
1069 * *pp_targetpath is now the unconsumed part of the path.
1070 * dfs_path is now the consumed part of the path
1071 * (in \server\share\path format).
1074 if (extrapath && strlen(extrapath) > 0) {
1075 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1076 /* put the trailing \ on the path, so to be save we put one in if needed */
1077 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1078 *pp_targetpath = talloc_asprintf(ctx,
1079 "%s\\%s",
1080 extrapath,
1081 *pp_targetpath);
1082 } else {
1083 *pp_targetpath = talloc_asprintf(ctx,
1084 "%s%s",
1085 extrapath,
1086 *pp_targetpath);
1088 if (!*pp_targetpath) {
1089 TALLOC_FREE(dfs_refs);
1090 return NT_STATUS_NO_MEMORY;
1094 /* parse out the consumed mount path */
1095 /* trim off the \server\share\ */
1097 ppath = dfs_path;
1099 if (*ppath != '\\') {
1100 d_printf("cli_resolve_path: "
1101 "dfs_path (%s) not in correct format.\n",
1102 dfs_path );
1103 TALLOC_FREE(dfs_refs);
1104 return NT_STATUS_NOT_FOUND;
1107 ppath++; /* Now pointing at start of server name. */
1109 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1110 TALLOC_FREE(dfs_refs);
1111 return NT_STATUS_NOT_FOUND;
1114 ppath++; /* Now pointing at start of share name. */
1116 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1117 TALLOC_FREE(dfs_refs);
1118 return NT_STATUS_NOT_FOUND;
1121 ppath++; /* Now pointing at path component. */
1123 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1124 if (!newmount) {
1125 TALLOC_FREE(dfs_refs);
1126 return NT_STATUS_NOT_FOUND;
1129 cli_set_mntpoint(*targetcli, newmount);
1131 /* Check for another dfs referral, note that we are not
1132 checking for loops here. */
1134 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1135 status = cli_resolve_path(ctx,
1136 newmount,
1137 dfs_auth_info,
1138 *targetcli,
1139 *pp_targetpath,
1140 &newcli,
1141 &newpath);
1142 if (NT_STATUS_IS_OK(status)) {
1144 * When cli_resolve_path returns true here it's always
1145 * returning the complete path in newpath, so we're done
1146 * here.
1148 *targetcli = newcli;
1149 *pp_targetpath = newpath;
1150 TALLOC_FREE(dfs_refs);
1151 return status;
1155 done:
1157 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1158 target_tcon = (*targetcli)->smb2.tcon;
1159 } else {
1160 target_tcon = (*targetcli)->smb1.tcon;
1163 /* If returning true ensure we return a dfs root full path. */
1164 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1165 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1166 if (!dfs_path) {
1167 TALLOC_FREE(dfs_refs);
1168 return NT_STATUS_NO_MEMORY;
1170 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1171 if (*pp_targetpath == NULL) {
1172 TALLOC_FREE(dfs_refs);
1173 return NT_STATUS_NO_MEMORY;
1177 TALLOC_FREE(dfs_refs);
1178 return NT_STATUS_OK;
1181 /********************************************************************
1182 ********************************************************************/
1184 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1185 struct cli_state *cli,
1186 const char *sharename,
1187 char **pp_newserver,
1188 char **pp_newshare,
1189 bool force_encrypt,
1190 struct cli_credentials *creds)
1192 struct client_dfs_referral *refs = NULL;
1193 size_t num_refs = 0;
1194 size_t consumed = 0;
1195 char *fullpath = NULL;
1196 bool res;
1197 struct smbXcli_tcon *orig_tcon = NULL;
1198 char *newextrapath = NULL;
1199 NTSTATUS status;
1200 const char *remote_name;
1202 if (!cli || !sharename) {
1203 return false;
1206 remote_name = smbXcli_conn_remote_name(cli->conn);
1208 /* special case. never check for a referral on the IPC$ share */
1210 if (strequal(sharename, "IPC$")) {
1211 return false;
1214 /* send a trans2_query_path_info to check for a referral */
1216 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1217 if (!fullpath) {
1218 return false;
1221 /* Store tcon state. */
1222 if (cli_state_has_tcon(cli)) {
1223 orig_tcon = cli_state_save_tcon(cli);
1224 if (orig_tcon == NULL) {
1225 return false;
1229 /* check for the referral */
1231 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1232 cli_state_restore_tcon(cli, orig_tcon);
1233 return false;
1236 if (force_encrypt) {
1237 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1238 if (!NT_STATUS_IS_OK(status)) {
1239 cli_state_restore_tcon(cli, orig_tcon);
1240 return false;
1244 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1245 &num_refs, &consumed);
1246 res = NT_STATUS_IS_OK(status);
1248 status = cli_tdis(cli);
1250 cli_state_restore_tcon(cli, orig_tcon);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 return false;
1256 if (!res || !num_refs) {
1257 return false;
1260 if (!refs[0].dfspath) {
1261 return false;
1264 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1265 pp_newshare, &newextrapath)) {
1266 return false;
1269 /* check that this is not a self-referral */
1271 if (strequal(remote_name, *pp_newserver) &&
1272 strequal(sharename, *pp_newshare)) {
1273 return false;
1276 return true;