WHATSNEW: Update release notes for Samba 4.3.0rc3
[Samba.git] / source3 / libsmb / clidfs.c
blobbd587bc62b627e08d74b10256323e14d424c4abd
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"
30 /********************************************************************
31 Important point.
33 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
34 are not C escaped here).
36 - but if we're using POSIX paths then <pathname> may contain
37 '/' separators, not '\\' separators. So cope with '\\' or '/'
38 as a separator when looking at the pathname part.... JRA.
39 ********************************************************************/
41 /********************************************************************
42 Ensure a connection is encrypted.
43 ********************************************************************/
45 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
46 const char *username,
47 const char *password,
48 const char *domain,
49 const char *sharename)
51 NTSTATUS status;
53 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
54 status = smb2cli_session_encryption_on(c->smb2.session);
55 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
56 d_printf("Encryption required and "
57 "server doesn't support "
58 "SMB3 encryption - failing connect\n");
59 } else if (!NT_STATUS_IS_OK(status)) {
60 d_printf("Encryption required and "
61 "setup failed with error %s.\n",
62 nt_errstr(status));
64 return status;
67 status = cli_force_encryption(c,
68 username,
69 password,
70 domain);
72 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
73 d_printf("Encryption required and "
74 "server that doesn't support "
75 "UNIX extensions - failing connect\n");
76 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNKNOWN_REVISION)) {
77 d_printf("Encryption required and "
78 "can't get UNIX CIFS extensions "
79 "version from server.\n");
80 } else if (NT_STATUS_EQUAL(status,NT_STATUS_UNSUPPORTED_COMPRESSION)) {
81 d_printf("Encryption required and "
82 "share %s doesn't support "
83 "encryption.\n", sharename);
84 } else if (!NT_STATUS_IS_OK(status)) {
85 d_printf("Encryption required and "
86 "setup failed with error %s.\n",
87 nt_errstr(status));
90 return status;
93 /********************************************************************
94 Return a connection to a server.
95 ********************************************************************/
97 static NTSTATUS do_connect(TALLOC_CTX *ctx,
98 const char *server,
99 const char *share,
100 const struct user_auth_info *auth_info,
101 bool show_sessetup,
102 bool force_encrypt,
103 int max_protocol,
104 int port,
105 int name_type,
106 struct cli_state **pcli)
108 struct cli_state *c = NULL;
109 char *servicename;
110 char *sharename;
111 char *newserver, *newshare;
112 const char *username;
113 const char *password;
114 const char *domain;
115 NTSTATUS status;
116 int flags = 0;
118 /* make a copy so we don't modify the global string 'service' */
119 servicename = talloc_strdup(ctx,share);
120 if (!servicename) {
121 return NT_STATUS_NO_MEMORY;
123 sharename = servicename;
124 if (*sharename == '\\') {
125 sharename += 2;
126 if (server == NULL) {
127 server = sharename;
129 sharename = strchr_m(sharename,'\\');
130 if (!sharename) {
131 return NT_STATUS_NO_MEMORY;
133 *sharename = 0;
134 sharename++;
136 if (server == NULL) {
137 return NT_STATUS_INVALID_PARAMETER;
140 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
141 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
143 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
144 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
146 if (get_cmdline_auth_info_use_ccache(auth_info)) {
147 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
149 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
150 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
153 status = cli_connect_nb(
154 server, NULL, port, name_type, NULL,
155 get_cmdline_auth_info_signing_state(auth_info),
156 flags, &c);
158 if (!NT_STATUS_IS_OK(status)) {
159 d_printf("Connection to %s failed (Error %s)\n",
160 server,
161 nt_errstr(status));
162 return status;
165 if (max_protocol == 0) {
166 max_protocol = PROTOCOL_NT1;
168 DEBUG(4,(" session request ok\n"));
170 status = smbXcli_negprot(c->conn, c->timeout,
171 lp_client_min_protocol(),
172 max_protocol);
174 if (!NT_STATUS_IS_OK(status)) {
175 d_printf("protocol negotiation failed: %s\n",
176 nt_errstr(status));
177 cli_shutdown(c);
178 return status;
181 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
182 /* Ensure we ask for some initial credits. */
183 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
186 username = get_cmdline_auth_info_username(auth_info);
187 password = get_cmdline_auth_info_password(auth_info);
188 domain = get_cmdline_auth_info_domain(auth_info);
189 if ((domain == NULL) || (domain[0] == '\0')) {
190 domain = lp_workgroup();
193 status = cli_session_setup(c, username,
194 password, strlen(password),
195 password, strlen(password),
196 domain);
197 if (!NT_STATUS_IS_OK(status)) {
198 /* If a password was not supplied then
199 * try again with a null username. */
200 if (password[0] || !username[0] ||
201 get_cmdline_auth_info_use_kerberos(auth_info) ||
202 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
203 "", 0,
204 "", 0,
205 lp_workgroup()))) {
206 d_printf("session setup failed: %s\n",
207 nt_errstr(status));
208 if (NT_STATUS_EQUAL(status,
209 NT_STATUS_MORE_PROCESSING_REQUIRED))
210 d_printf("did you forget to run kinit?\n");
211 cli_shutdown(c);
212 return status;
214 d_printf("Anonymous login successful\n");
217 if (!NT_STATUS_IS_OK(status)) {
218 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
219 cli_shutdown(c);
220 return status;
223 if ( show_sessetup ) {
224 if (*c->server_domain) {
225 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
226 c->server_domain,c->server_os,c->server_type));
227 } else if (*c->server_os || *c->server_type) {
228 DEBUG(0,("OS=[%s] Server=[%s]\n",
229 c->server_os,c->server_type));
232 DEBUG(4,(" session setup ok\n"));
234 /* here's the fun part....to support 'msdfs proxy' shares
235 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
236 here before trying to connect to the original share.
237 cli_check_msdfs_proxy() will fail if it is a normal share. */
239 if (smbXcli_conn_dfs_supported(c->conn) &&
240 cli_check_msdfs_proxy(ctx, c, sharename,
241 &newserver, &newshare,
242 force_encrypt,
243 username,
244 password,
245 domain)) {
246 cli_shutdown(c);
247 return do_connect(ctx, newserver,
248 newshare, auth_info, false,
249 force_encrypt, max_protocol,
250 port, name_type, pcli);
253 /* must be a normal share */
255 status = cli_tree_connect(c, sharename, "?????",
256 password, strlen(password)+1);
257 if (!NT_STATUS_IS_OK(status)) {
258 d_printf("tree connect failed: %s\n", nt_errstr(status));
259 cli_shutdown(c);
260 return status;
263 if (force_encrypt) {
264 status = cli_cm_force_encryption(c,
265 username,
266 password,
267 domain,
268 sharename);
269 if (!NT_STATUS_IS_OK(status)) {
270 cli_shutdown(c);
271 return status;
275 DEBUG(4,(" tconx ok\n"));
276 *pcli = c;
277 return NT_STATUS_OK;
280 /****************************************************************************
281 ****************************************************************************/
283 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
285 TALLOC_CTX *frame = talloc_stackframe();
286 char *name = clean_name(frame, mnt);
287 if (!name) {
288 TALLOC_FREE(frame);
289 return;
291 TALLOC_FREE(cli->dfs_mountpoint);
292 cli->dfs_mountpoint = talloc_strdup(cli, name);
293 TALLOC_FREE(frame);
296 /********************************************************************
297 Add a new connection to the list.
298 referring_cli == NULL means a new initial connection.
299 ********************************************************************/
301 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
302 struct cli_state *referring_cli,
303 const char *server,
304 const char *share,
305 const struct user_auth_info *auth_info,
306 bool show_hdr,
307 bool force_encrypt,
308 int max_protocol,
309 int port,
310 int name_type,
311 struct cli_state **pcli)
313 struct cli_state *cli;
314 NTSTATUS status;
316 status = do_connect(ctx, server, share,
317 auth_info,
318 show_hdr, force_encrypt, max_protocol,
319 port, name_type, &cli);
321 if (!NT_STATUS_IS_OK(status)) {
322 return status;
325 /* Enter into the list. */
326 if (referring_cli) {
327 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
330 if (referring_cli && referring_cli->requested_posix_capabilities) {
331 uint16_t major, minor;
332 uint32_t caplow, caphigh;
333 status = cli_unix_extensions_version(cli, &major, &minor,
334 &caplow, &caphigh);
335 if (NT_STATUS_IS_OK(status)) {
336 cli_set_unix_extensions_capabilities(cli,
337 major, minor,
338 caplow, caphigh);
342 *pcli = cli;
343 return NT_STATUS_OK;
346 /********************************************************************
347 Return a connection to a server on a particular share.
348 ********************************************************************/
350 static struct cli_state *cli_cm_find(struct cli_state *cli,
351 const char *server,
352 const char *share)
354 struct cli_state *p;
356 if (cli == NULL) {
357 return NULL;
360 /* Search to the start of the list. */
361 for (p = cli; p; p = DLIST_PREV(p)) {
362 const char *remote_name =
363 smbXcli_conn_remote_name(p->conn);
365 if (strequal(server, remote_name) &&
366 strequal(share,p->share)) {
367 return p;
371 /* Search to the end of the list. */
372 for (p = cli->next; p; p = p->next) {
373 const char *remote_name =
374 smbXcli_conn_remote_name(p->conn);
376 if (strequal(server, remote_name) &&
377 strequal(share,p->share)) {
378 return p;
382 return NULL;
385 /****************************************************************************
386 Open a client connection to a \\server\share.
387 ****************************************************************************/
389 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
390 struct cli_state *referring_cli,
391 const char *server,
392 const char *share,
393 const struct user_auth_info *auth_info,
394 bool show_hdr,
395 bool force_encrypt,
396 int max_protocol,
397 int port,
398 int name_type,
399 struct cli_state **pcli)
401 /* Try to reuse an existing connection in this list. */
402 struct cli_state *c = cli_cm_find(referring_cli, server, share);
403 NTSTATUS status;
405 if (c) {
406 *pcli = c;
407 return NT_STATUS_OK;
410 if (auth_info == NULL) {
411 /* Can't do a new connection
412 * without auth info. */
413 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
414 "without auth info\n",
415 server, share );
416 return NT_STATUS_INVALID_PARAMETER;
419 status = cli_cm_connect(ctx,
420 referring_cli,
421 server,
422 share,
423 auth_info,
424 show_hdr,
425 force_encrypt,
426 max_protocol,
427 port,
428 name_type,
429 &c);
430 if (!NT_STATUS_IS_OK(status)) {
431 return status;
433 *pcli = c;
434 return NT_STATUS_OK;
437 /****************************************************************************
438 ****************************************************************************/
440 void cli_cm_display(struct cli_state *cli)
442 int i;
444 for (i=0; cli; cli = cli->next,i++ ) {
445 d_printf("%d:\tserver=%s, share=%s\n",
446 i, smbXcli_conn_remote_name(cli->conn), cli->share);
450 /****************************************************************************
451 ****************************************************************************/
453 /****************************************************************************
454 ****************************************************************************/
456 #if 0
457 void cli_cm_set_credentials(struct user_auth_info *auth_info)
459 SAFE_FREE(cm_creds.username);
460 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
461 auth_info));
463 if (get_cmdline_auth_info_got_pass(auth_info)) {
464 cm_set_password(get_cmdline_auth_info_password(auth_info));
467 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
468 cm_creds.fallback_after_kerberos = false;
469 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
471 #endif
473 /**********************************************************************
474 split a dfs path into the server, share name, and extrapath components
475 **********************************************************************/
477 static bool split_dfs_path(TALLOC_CTX *ctx,
478 const char *nodepath,
479 char **pp_server,
480 char **pp_share,
481 char **pp_extrapath)
483 char *p, *q;
484 char *path;
486 *pp_server = NULL;
487 *pp_share = NULL;
488 *pp_extrapath = NULL;
490 path = talloc_strdup(ctx, nodepath);
491 if (!path) {
492 goto fail;
495 if ( path[0] != '\\' ) {
496 goto fail;
499 p = strchr_m( path + 1, '\\' );
500 if ( !p ) {
501 goto fail;
504 *p = '\0';
505 p++;
507 /* Look for any extra/deep path */
508 q = strchr_m(p, '\\');
509 if (q != NULL) {
510 *q = '\0';
511 q++;
512 *pp_extrapath = talloc_strdup(ctx, q);
513 } else {
514 *pp_extrapath = talloc_strdup(ctx, "");
516 if (*pp_extrapath == NULL) {
517 goto fail;
520 *pp_share = talloc_strdup(ctx, p);
521 if (*pp_share == NULL) {
522 goto fail;
525 *pp_server = talloc_strdup(ctx, &path[1]);
526 if (*pp_server == NULL) {
527 goto fail;
530 TALLOC_FREE(path);
531 return true;
533 fail:
534 TALLOC_FREE(*pp_share);
535 TALLOC_FREE(*pp_extrapath);
536 TALLOC_FREE(path);
537 return false;
540 /****************************************************************************
541 Return the original path truncated at the directory component before
542 the first wildcard character. Trust the caller to provide a NULL
543 terminated string
544 ****************************************************************************/
546 static char *clean_path(TALLOC_CTX *ctx, const char *path)
548 size_t len;
549 char *p1, *p2, *p;
550 char *path_out;
552 /* No absolute paths. */
553 while (IS_DIRECTORY_SEP(*path)) {
554 path++;
557 path_out = talloc_strdup(ctx, path);
558 if (!path_out) {
559 return NULL;
562 p1 = strchr_m(path_out, '*');
563 p2 = strchr_m(path_out, '?');
565 if (p1 || p2) {
566 if (p1 && p2) {
567 p = MIN(p1,p2);
568 } else if (!p1) {
569 p = p2;
570 } else {
571 p = p1;
573 *p = '\0';
575 /* Now go back to the start of this component. */
576 p1 = strrchr_m(path_out, '/');
577 p2 = strrchr_m(path_out, '\\');
578 p = MAX(p1,p2);
579 if (p) {
580 *p = '\0';
584 /* Strip any trailing separator */
586 len = strlen(path_out);
587 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
588 path_out[len-1] = '\0';
591 return path_out;
594 /****************************************************************************
595 ****************************************************************************/
597 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
598 struct cli_state *cli,
599 const char *dir)
601 char path_sep = '\\';
603 /* Ensure the extrapath doesn't start with a separator. */
604 while (IS_DIRECTORY_SEP(*dir)) {
605 dir++;
608 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
609 path_sep = '/';
611 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
612 path_sep,
613 smbXcli_conn_remote_name(cli->conn),
614 path_sep,
615 cli->share,
616 path_sep,
617 dir);
620 /********************************************************************
621 check for dfs referral
622 ********************************************************************/
624 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
625 NTSTATUS status)
627 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
629 if (!(smbXcli_conn_use_unicode(cli->conn))) {
630 return false;
632 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
633 return false;
635 if (NT_STATUS_EQUAL(status, expected)) {
636 return true;
638 return false;
641 /********************************************************************
642 Get the dfs referral link.
643 ********************************************************************/
645 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
646 struct cli_state *cli,
647 const char *path,
648 struct client_dfs_referral **refs,
649 size_t *num_refs,
650 size_t *consumed)
652 unsigned int param_len = 0;
653 uint16_t recv_flags2;
654 uint8_t *param = NULL;
655 uint8_t *rdata = NULL;
656 char *p;
657 char *endp;
658 smb_ucs2_t *path_ucs;
659 char *consumed_path = NULL;
660 uint16_t consumed_ucs;
661 uint16_t num_referrals;
662 struct client_dfs_referral *referrals = NULL;
663 NTSTATUS status;
664 TALLOC_CTX *frame = talloc_stackframe();
666 *num_refs = 0;
667 *refs = NULL;
669 param = talloc_array(talloc_tos(), uint8_t, 2);
670 if (!param) {
671 status = NT_STATUS_NO_MEMORY;
672 goto out;
674 SSVAL(param, 0, 0x03); /* max referral level */
676 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
677 path, strlen(path)+1,
678 NULL);
679 if (!param) {
680 status = NT_STATUS_NO_MEMORY;
681 goto out;
683 param_len = talloc_get_size(param);
684 path_ucs = (smb_ucs2_t *)&param[2];
686 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
687 DATA_BLOB in_input_buffer;
688 DATA_BLOB in_output_buffer = data_blob_null;
689 DATA_BLOB out_input_buffer = data_blob_null;
690 DATA_BLOB out_output_buffer = data_blob_null;
692 in_input_buffer.data = param;
693 in_input_buffer.length = param_len;
695 status = smb2cli_ioctl(cli->conn,
696 cli->timeout,
697 cli->smb2.session,
698 cli->smb2.tcon,
699 UINT64_MAX, /* in_fid_persistent */
700 UINT64_MAX, /* in_fid_volatile */
701 FSCTL_DFS_GET_REFERRALS,
702 0, /* in_max_input_length */
703 &in_input_buffer,
704 CLI_BUFFER_SIZE, /* in_max_output_length */
705 &in_output_buffer,
706 SMB2_IOCTL_FLAG_IS_FSCTL,
707 talloc_tos(),
708 &out_input_buffer,
709 &out_output_buffer);
710 if (!NT_STATUS_IS_OK(status)) {
711 goto out;
714 if (out_output_buffer.length < 4) {
715 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
716 goto out;
719 recv_flags2 = FLAGS2_UNICODE_STRINGS;
720 rdata = out_output_buffer.data;
721 endp = (char *)rdata + out_output_buffer.length;
722 } else {
723 unsigned int data_len = 0;
724 uint16_t setup[1];
726 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
728 status = cli_trans(talloc_tos(), cli, SMBtrans2,
729 NULL, 0xffff, 0, 0,
730 setup, 1, 0,
731 param, param_len, 2,
732 NULL, 0, CLI_BUFFER_SIZE,
733 &recv_flags2,
734 NULL, 0, NULL, /* rsetup */
735 NULL, 0, NULL,
736 &rdata, 4, &data_len);
737 if (!NT_STATUS_IS_OK(status)) {
738 goto out;
741 endp = (char *)rdata + data_len;
744 consumed_ucs = SVAL(rdata, 0);
745 num_referrals = SVAL(rdata, 2);
747 /* consumed_ucs is the number of bytes
748 * of the UCS2 path consumed not counting any
749 * terminating null. We need to convert
750 * back to unix charset and count again
751 * to get the number of bytes consumed from
752 * the incoming path. */
754 errno = 0;
755 if (pull_string_talloc(talloc_tos(),
756 NULL,
758 &consumed_path,
759 path_ucs,
760 consumed_ucs,
761 STR_UNICODE) == 0) {
762 if (errno != 0) {
763 status = map_nt_error_from_unix(errno);
764 } else {
765 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
767 goto out;
769 if (consumed_path == NULL) {
770 status = map_nt_error_from_unix(errno);
771 goto out;
773 *consumed = strlen(consumed_path);
775 if (num_referrals != 0) {
776 uint16_t ref_version;
777 uint16_t ref_size;
778 int i;
779 uint16_t node_offset;
781 referrals = talloc_array(ctx, struct client_dfs_referral,
782 num_referrals);
784 if (!referrals) {
785 status = NT_STATUS_NO_MEMORY;
786 goto out;
788 /* start at the referrals array */
790 p = (char *)rdata+8;
791 for (i=0; i<num_referrals && p < endp; i++) {
792 if (p + 18 > endp) {
793 goto out;
795 ref_version = SVAL(p, 0);
796 ref_size = SVAL(p, 2);
797 node_offset = SVAL(p, 16);
799 if (ref_version != 3) {
800 p += ref_size;
801 continue;
804 referrals[i].proximity = SVAL(p, 8);
805 referrals[i].ttl = SVAL(p, 10);
807 if (p + node_offset > endp) {
808 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
809 goto out;
811 clistr_pull_talloc(referrals,
812 (const char *)rdata,
813 recv_flags2,
814 &referrals[i].dfspath,
815 p+node_offset,
816 PTR_DIFF(endp, p+node_offset),
817 STR_TERMINATE|STR_UNICODE);
819 if (!referrals[i].dfspath) {
820 status = map_nt_error_from_unix(errno);
821 goto out;
823 p += ref_size;
825 if (i < num_referrals) {
826 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
827 goto out;
831 *num_refs = num_referrals;
832 *refs = referrals;
834 out:
836 TALLOC_FREE(frame);
837 return status;
840 /********************************************************************
841 ********************************************************************/
842 struct cli_dfs_path_split {
843 char *server;
844 char *share;
845 char *extrapath;
848 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
849 const char *mountpt,
850 const struct user_auth_info *dfs_auth_info,
851 struct cli_state *rootcli,
852 const char *path,
853 struct cli_state **targetcli,
854 char **pp_targetpath)
856 struct client_dfs_referral *refs = NULL;
857 size_t num_refs = 0;
858 size_t consumed = 0;
859 struct cli_state *cli_ipc = NULL;
860 char *dfs_path = NULL;
861 char *cleanpath = NULL;
862 char *extrapath = NULL;
863 int pathlen;
864 struct cli_state *newcli = NULL;
865 struct cli_state *ccli = NULL;
866 int count = 0;
867 char *newpath = NULL;
868 char *newmount = NULL;
869 char *ppath = NULL;
870 SMB_STRUCT_STAT sbuf;
871 uint32_t attributes;
872 NTSTATUS status;
873 struct smbXcli_tcon *root_tcon = NULL;
874 struct smbXcli_tcon *target_tcon = NULL;
875 struct cli_dfs_path_split *dfs_refs = NULL;
877 if ( !rootcli || !path || !targetcli ) {
878 return NT_STATUS_INVALID_PARAMETER;
881 /* Don't do anything if this is not a DFS root. */
883 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
884 root_tcon = rootcli->smb2.tcon;
885 } else {
886 root_tcon = rootcli->smb1.tcon;
889 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
890 *targetcli = rootcli;
891 *pp_targetpath = talloc_strdup(ctx, path);
892 if (!*pp_targetpath) {
893 return NT_STATUS_NO_MEMORY;
895 return NT_STATUS_OK;
898 *targetcli = NULL;
900 /* Send a trans2_query_path_info to check for a referral. */
902 cleanpath = clean_path(ctx, path);
903 if (!cleanpath) {
904 return NT_STATUS_NO_MEMORY;
907 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
908 if (!dfs_path) {
909 return NT_STATUS_NO_MEMORY;
912 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
913 if (NT_STATUS_IS_OK(status)) {
914 /* This is an ordinary path, just return it. */
915 *targetcli = rootcli;
916 *pp_targetpath = talloc_strdup(ctx, path);
917 if (!*pp_targetpath) {
918 return NT_STATUS_NO_MEMORY;
920 goto done;
923 /* Special case where client asked for a path that does not exist */
925 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
926 status)) {
927 *targetcli = rootcli;
928 *pp_targetpath = talloc_strdup(ctx, path);
929 if (!*pp_targetpath) {
930 return NT_STATUS_NO_MEMORY;
932 goto done;
935 /* We got an error, check for DFS referral. */
937 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
938 status)) {
939 return status;
942 /* Check for the referral. */
944 status = cli_cm_open(ctx,
945 rootcli,
946 smbXcli_conn_remote_name(rootcli->conn),
947 "IPC$",
948 dfs_auth_info,
949 false,
950 smb1cli_conn_encryption_on(rootcli->conn),
951 smbXcli_conn_protocol(rootcli->conn),
953 0x20,
954 &cli_ipc);
955 if (!NT_STATUS_IS_OK(status)) {
956 return status;
959 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
960 &num_refs, &consumed);
961 if (!NT_STATUS_IS_OK(status) || !num_refs) {
962 return status;
965 if (!refs[0].dfspath) {
966 return NT_STATUS_NOT_FOUND;
970 * Bug#10123 - DFS referal entries can be provided in a random order,
971 * so check the connection cache for each item to avoid unnecessary
972 * reconnections.
974 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
975 if (dfs_refs == NULL) {
976 return NT_STATUS_NO_MEMORY;
979 for (count = 0; count < num_refs; count++) {
980 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
981 &dfs_refs[count].server,
982 &dfs_refs[count].share,
983 &dfs_refs[count].extrapath)) {
984 TALLOC_FREE(dfs_refs);
985 return NT_STATUS_NOT_FOUND;
988 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
989 dfs_refs[count].share);
990 if (ccli != NULL) {
991 extrapath = dfs_refs[count].extrapath;
992 *targetcli = ccli;
993 break;
998 * If no cached connection was found, then connect to the first live
999 * referral server in the list.
1001 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1002 /* Connect to the target server & share */
1003 status = cli_cm_connect(ctx, rootcli,
1004 dfs_refs[count].server,
1005 dfs_refs[count].share,
1006 dfs_auth_info,
1007 false,
1008 smb1cli_conn_encryption_on(rootcli->conn),
1009 smbXcli_conn_protocol(rootcli->conn),
1011 0x20,
1012 targetcli);
1013 if (!NT_STATUS_IS_OK(status)) {
1014 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1015 dfs_refs[count].server,
1016 dfs_refs[count].share);
1017 continue;
1018 } else {
1019 extrapath = dfs_refs[count].extrapath;
1020 break;
1024 /* No available referral server for the connection */
1025 if (*targetcli == NULL) {
1026 TALLOC_FREE(dfs_refs);
1027 return status;
1030 /* Make sure to recreate the original string including any wildcards. */
1032 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1033 if (!dfs_path) {
1034 TALLOC_FREE(dfs_refs);
1035 return NT_STATUS_NO_MEMORY;
1037 pathlen = strlen(dfs_path);
1038 consumed = MIN(pathlen, consumed);
1039 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1040 if (!*pp_targetpath) {
1041 TALLOC_FREE(dfs_refs);
1042 return NT_STATUS_NO_MEMORY;
1044 dfs_path[consumed] = '\0';
1047 * *pp_targetpath is now the unconsumed part of the path.
1048 * dfs_path is now the consumed part of the path
1049 * (in \server\share\path format).
1052 if (extrapath && strlen(extrapath) > 0) {
1053 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1054 /* put the trailing \ on the path, so to be save we put one in if needed */
1055 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1056 *pp_targetpath = talloc_asprintf(ctx,
1057 "%s\\%s",
1058 extrapath,
1059 *pp_targetpath);
1060 } else {
1061 *pp_targetpath = talloc_asprintf(ctx,
1062 "%s%s",
1063 extrapath,
1064 *pp_targetpath);
1066 if (!*pp_targetpath) {
1067 TALLOC_FREE(dfs_refs);
1068 return NT_STATUS_NO_MEMORY;
1072 /* parse out the consumed mount path */
1073 /* trim off the \server\share\ */
1075 ppath = dfs_path;
1077 if (*ppath != '\\') {
1078 d_printf("cli_resolve_path: "
1079 "dfs_path (%s) not in correct format.\n",
1080 dfs_path );
1081 TALLOC_FREE(dfs_refs);
1082 return NT_STATUS_NOT_FOUND;
1085 ppath++; /* Now pointing at start of server name. */
1087 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1088 TALLOC_FREE(dfs_refs);
1089 return NT_STATUS_NOT_FOUND;
1092 ppath++; /* Now pointing at start of share name. */
1094 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1095 TALLOC_FREE(dfs_refs);
1096 return NT_STATUS_NOT_FOUND;
1099 ppath++; /* Now pointing at path component. */
1101 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1102 if (!newmount) {
1103 TALLOC_FREE(dfs_refs);
1104 return NT_STATUS_NOT_FOUND;
1107 cli_set_mntpoint(*targetcli, newmount);
1109 /* Check for another dfs referral, note that we are not
1110 checking for loops here. */
1112 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1113 status = cli_resolve_path(ctx,
1114 newmount,
1115 dfs_auth_info,
1116 *targetcli,
1117 *pp_targetpath,
1118 &newcli,
1119 &newpath);
1120 if (NT_STATUS_IS_OK(status)) {
1122 * When cli_resolve_path returns true here it's always
1123 * returning the complete path in newpath, so we're done
1124 * here.
1126 *targetcli = newcli;
1127 *pp_targetpath = newpath;
1128 TALLOC_FREE(dfs_refs);
1129 return status;
1133 done:
1135 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1136 target_tcon = (*targetcli)->smb2.tcon;
1137 } else {
1138 target_tcon = (*targetcli)->smb1.tcon;
1141 /* If returning true ensure we return a dfs root full path. */
1142 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1143 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1144 if (!dfs_path) {
1145 TALLOC_FREE(dfs_refs);
1146 return NT_STATUS_NO_MEMORY;
1148 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1149 if (*pp_targetpath == NULL) {
1150 TALLOC_FREE(dfs_refs);
1151 return NT_STATUS_NO_MEMORY;
1155 TALLOC_FREE(dfs_refs);
1156 return NT_STATUS_OK;
1159 /********************************************************************
1160 ********************************************************************/
1162 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1163 struct cli_state *cli,
1164 const char *sharename,
1165 char **pp_newserver,
1166 char **pp_newshare,
1167 bool force_encrypt,
1168 const char *username,
1169 const char *password,
1170 const char *domain)
1172 struct client_dfs_referral *refs = NULL;
1173 size_t num_refs = 0;
1174 size_t consumed = 0;
1175 char *fullpath = NULL;
1176 bool res;
1177 uint16_t cnum;
1178 char *newextrapath = NULL;
1179 NTSTATUS status;
1180 const char *remote_name;
1182 if (!cli || !sharename) {
1183 return false;
1186 remote_name = smbXcli_conn_remote_name(cli->conn);
1187 cnum = cli_state_get_tid(cli);
1189 /* special case. never check for a referral on the IPC$ share */
1191 if (strequal(sharename, "IPC$")) {
1192 return false;
1195 /* send a trans2_query_path_info to check for a referral */
1197 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1198 if (!fullpath) {
1199 return false;
1202 /* check for the referral */
1204 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1205 return false;
1208 if (force_encrypt) {
1209 status = cli_cm_force_encryption(cli,
1210 username,
1211 password,
1212 domain,
1213 "IPC$");
1214 if (!NT_STATUS_IS_OK(status)) {
1215 return false;
1219 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1220 &num_refs, &consumed);
1221 res = NT_STATUS_IS_OK(status);
1223 status = cli_tdis(cli);
1224 if (!NT_STATUS_IS_OK(status)) {
1225 return false;
1228 cli_state_set_tid(cli, cnum);
1230 if (!res || !num_refs) {
1231 return false;
1234 if (!refs[0].dfspath) {
1235 return false;
1238 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1239 pp_newshare, &newextrapath)) {
1240 return false;
1243 /* check that this is not a self-referral */
1245 if (strequal(remote_name, *pp_newserver) &&
1246 strequal(sharename, *pp_newshare)) {
1247 return false;
1250 return true;