ldb: version 1.1.27
[Samba.git] / source3 / libsmb / clidfs.c
blobd2a4c194f44986782c34de18ba334619df80f330
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;
117 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
119 if (force_encrypt) {
120 signing_state = SMB_SIGNING_REQUIRED;
123 /* make a copy so we don't modify the global string 'service' */
124 servicename = talloc_strdup(ctx,share);
125 if (!servicename) {
126 return NT_STATUS_NO_MEMORY;
128 sharename = servicename;
129 if (*sharename == '\\') {
130 sharename += 2;
131 if (server == NULL) {
132 server = sharename;
134 sharename = strchr_m(sharename,'\\');
135 if (!sharename) {
136 return NT_STATUS_NO_MEMORY;
138 *sharename = 0;
139 sharename++;
141 if (server == NULL) {
142 return NT_STATUS_INVALID_PARAMETER;
145 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
146 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
148 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
149 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
151 if (get_cmdline_auth_info_use_ccache(auth_info)) {
152 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
154 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
155 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
158 status = cli_connect_nb(
159 server, NULL, port, name_type, NULL,
160 signing_state,
161 flags, &c);
163 if (!NT_STATUS_IS_OK(status)) {
164 d_printf("Connection to %s failed (Error %s)\n",
165 server,
166 nt_errstr(status));
167 return status;
170 if (max_protocol == 0) {
171 max_protocol = PROTOCOL_NT1;
173 DEBUG(4,(" session request ok\n"));
175 status = smbXcli_negprot(c->conn, c->timeout,
176 lp_client_min_protocol(),
177 max_protocol);
179 if (!NT_STATUS_IS_OK(status)) {
180 d_printf("protocol negotiation failed: %s\n",
181 nt_errstr(status));
182 cli_shutdown(c);
183 return status;
186 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
187 /* Ensure we ask for some initial credits. */
188 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
191 username = get_cmdline_auth_info_username(auth_info);
192 password = get_cmdline_auth_info_password(auth_info);
193 domain = get_cmdline_auth_info_domain(auth_info);
194 if ((domain == NULL) || (domain[0] == '\0')) {
195 domain = lp_workgroup();
198 status = cli_session_setup(c, username,
199 password, strlen(password),
200 password, strlen(password),
201 domain);
202 if (!NT_STATUS_IS_OK(status)) {
203 /* If a password was not supplied then
204 * try again with a null username. */
205 if (password[0] || !username[0] ||
206 get_cmdline_auth_info_use_kerberos(auth_info) ||
207 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
208 "", 0,
209 "", 0,
210 lp_workgroup()))) {
211 d_printf("session setup failed: %s\n",
212 nt_errstr(status));
213 if (NT_STATUS_EQUAL(status,
214 NT_STATUS_MORE_PROCESSING_REQUIRED))
215 d_printf("did you forget to run kinit?\n");
216 cli_shutdown(c);
217 return status;
219 d_printf("Anonymous login successful\n");
222 if (!NT_STATUS_IS_OK(status)) {
223 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
224 cli_shutdown(c);
225 return status;
228 if ( show_sessetup ) {
229 if (*c->server_domain) {
230 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
231 c->server_domain,c->server_os,c->server_type));
232 } else if (*c->server_os || *c->server_type) {
233 DEBUG(0,("OS=[%s] Server=[%s]\n",
234 c->server_os,c->server_type));
237 DEBUG(4,(" session setup ok\n"));
239 /* here's the fun part....to support 'msdfs proxy' shares
240 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
241 here before trying to connect to the original share.
242 cli_check_msdfs_proxy() will fail if it is a normal share. */
244 if (smbXcli_conn_dfs_supported(c->conn) &&
245 cli_check_msdfs_proxy(ctx, c, sharename,
246 &newserver, &newshare,
247 force_encrypt,
248 username,
249 password,
250 domain)) {
251 cli_shutdown(c);
252 return do_connect(ctx, newserver,
253 newshare, auth_info, false,
254 force_encrypt, max_protocol,
255 port, name_type, pcli);
258 /* must be a normal share */
260 status = cli_tree_connect(c, sharename, "?????",
261 password, strlen(password)+1);
262 if (!NT_STATUS_IS_OK(status)) {
263 d_printf("tree connect failed: %s\n", nt_errstr(status));
264 cli_shutdown(c);
265 return status;
268 if (force_encrypt) {
269 status = cli_cm_force_encryption(c,
270 username,
271 password,
272 domain,
273 sharename);
274 if (!NT_STATUS_IS_OK(status)) {
275 cli_shutdown(c);
276 return status;
280 DEBUG(4,(" tconx ok\n"));
281 *pcli = c;
282 return NT_STATUS_OK;
285 /****************************************************************************
286 ****************************************************************************/
288 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
290 TALLOC_CTX *frame = talloc_stackframe();
291 char *name = clean_name(frame, mnt);
292 if (!name) {
293 TALLOC_FREE(frame);
294 return;
296 TALLOC_FREE(cli->dfs_mountpoint);
297 cli->dfs_mountpoint = talloc_strdup(cli, name);
298 TALLOC_FREE(frame);
301 /********************************************************************
302 Add a new connection to the list.
303 referring_cli == NULL means a new initial connection.
304 ********************************************************************/
306 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
307 struct cli_state *referring_cli,
308 const char *server,
309 const char *share,
310 const struct user_auth_info *auth_info,
311 bool show_hdr,
312 bool force_encrypt,
313 int max_protocol,
314 int port,
315 int name_type,
316 struct cli_state **pcli)
318 struct cli_state *cli;
319 NTSTATUS status;
321 status = do_connect(ctx, server, share,
322 auth_info,
323 show_hdr, force_encrypt, max_protocol,
324 port, name_type, &cli);
326 if (!NT_STATUS_IS_OK(status)) {
327 return status;
330 /* Enter into the list. */
331 if (referring_cli) {
332 DLIST_ADD_END(referring_cli, cli);
335 if (referring_cli && referring_cli->requested_posix_capabilities) {
336 uint16_t major, minor;
337 uint32_t caplow, caphigh;
338 status = cli_unix_extensions_version(cli, &major, &minor,
339 &caplow, &caphigh);
340 if (NT_STATUS_IS_OK(status)) {
341 cli_set_unix_extensions_capabilities(cli,
342 major, minor,
343 caplow, caphigh);
347 *pcli = cli;
348 return NT_STATUS_OK;
351 /********************************************************************
352 Return a connection to a server on a particular share.
353 ********************************************************************/
355 static struct cli_state *cli_cm_find(struct cli_state *cli,
356 const char *server,
357 const char *share)
359 struct cli_state *p;
361 if (cli == NULL) {
362 return NULL;
365 /* Search to the start of the list. */
366 for (p = cli; p; p = DLIST_PREV(p)) {
367 const char *remote_name =
368 smbXcli_conn_remote_name(p->conn);
370 if (strequal(server, remote_name) &&
371 strequal(share,p->share)) {
372 return p;
376 /* Search to the end of the list. */
377 for (p = cli->next; p; p = p->next) {
378 const char *remote_name =
379 smbXcli_conn_remote_name(p->conn);
381 if (strequal(server, remote_name) &&
382 strequal(share,p->share)) {
383 return p;
387 return NULL;
390 /****************************************************************************
391 Open a client connection to a \\server\share.
392 ****************************************************************************/
394 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
395 struct cli_state *referring_cli,
396 const char *server,
397 const char *share,
398 const struct user_auth_info *auth_info,
399 bool show_hdr,
400 bool force_encrypt,
401 int max_protocol,
402 int port,
403 int name_type,
404 struct cli_state **pcli)
406 /* Try to reuse an existing connection in this list. */
407 struct cli_state *c = cli_cm_find(referring_cli, server, share);
408 NTSTATUS status;
410 if (c) {
411 *pcli = c;
412 return NT_STATUS_OK;
415 if (auth_info == NULL) {
416 /* Can't do a new connection
417 * without auth info. */
418 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
419 "without auth info\n",
420 server, share );
421 return NT_STATUS_INVALID_PARAMETER;
424 status = cli_cm_connect(ctx,
425 referring_cli,
426 server,
427 share,
428 auth_info,
429 show_hdr,
430 force_encrypt,
431 max_protocol,
432 port,
433 name_type,
434 &c);
435 if (!NT_STATUS_IS_OK(status)) {
436 return status;
438 *pcli = c;
439 return NT_STATUS_OK;
442 /****************************************************************************
443 ****************************************************************************/
445 void cli_cm_display(struct cli_state *cli)
447 int i;
449 for (i=0; cli; cli = cli->next,i++ ) {
450 d_printf("%d:\tserver=%s, share=%s\n",
451 i, smbXcli_conn_remote_name(cli->conn), cli->share);
455 /****************************************************************************
456 ****************************************************************************/
458 /****************************************************************************
459 ****************************************************************************/
461 #if 0
462 void cli_cm_set_credentials(struct user_auth_info *auth_info)
464 SAFE_FREE(cm_creds.username);
465 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
466 auth_info));
468 if (get_cmdline_auth_info_got_pass(auth_info)) {
469 cm_set_password(get_cmdline_auth_info_password(auth_info));
472 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
473 cm_creds.fallback_after_kerberos = false;
474 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
476 #endif
478 /**********************************************************************
479 split a dfs path into the server, share name, and extrapath components
480 **********************************************************************/
482 static bool split_dfs_path(TALLOC_CTX *ctx,
483 const char *nodepath,
484 char **pp_server,
485 char **pp_share,
486 char **pp_extrapath)
488 char *p, *q;
489 char *path;
491 *pp_server = NULL;
492 *pp_share = NULL;
493 *pp_extrapath = NULL;
495 path = talloc_strdup(ctx, nodepath);
496 if (!path) {
497 goto fail;
500 if ( path[0] != '\\' ) {
501 goto fail;
504 p = strchr_m( path + 1, '\\' );
505 if ( !p ) {
506 goto fail;
509 *p = '\0';
510 p++;
512 /* Look for any extra/deep path */
513 q = strchr_m(p, '\\');
514 if (q != NULL) {
515 *q = '\0';
516 q++;
517 *pp_extrapath = talloc_strdup(ctx, q);
518 } else {
519 *pp_extrapath = talloc_strdup(ctx, "");
521 if (*pp_extrapath == NULL) {
522 goto fail;
525 *pp_share = talloc_strdup(ctx, p);
526 if (*pp_share == NULL) {
527 goto fail;
530 *pp_server = talloc_strdup(ctx, &path[1]);
531 if (*pp_server == NULL) {
532 goto fail;
535 TALLOC_FREE(path);
536 return true;
538 fail:
539 TALLOC_FREE(*pp_share);
540 TALLOC_FREE(*pp_extrapath);
541 TALLOC_FREE(path);
542 return false;
545 /****************************************************************************
546 Return the original path truncated at the directory component before
547 the first wildcard character. Trust the caller to provide a NULL
548 terminated string
549 ****************************************************************************/
551 static char *clean_path(TALLOC_CTX *ctx, const char *path)
553 size_t len;
554 char *p1, *p2, *p;
555 char *path_out;
557 /* No absolute paths. */
558 while (IS_DIRECTORY_SEP(*path)) {
559 path++;
562 path_out = talloc_strdup(ctx, path);
563 if (!path_out) {
564 return NULL;
567 p1 = strchr_m(path_out, '*');
568 p2 = strchr_m(path_out, '?');
570 if (p1 || p2) {
571 if (p1 && p2) {
572 p = MIN(p1,p2);
573 } else if (!p1) {
574 p = p2;
575 } else {
576 p = p1;
578 *p = '\0';
580 /* Now go back to the start of this component. */
581 p1 = strrchr_m(path_out, '/');
582 p2 = strrchr_m(path_out, '\\');
583 p = MAX(p1,p2);
584 if (p) {
585 *p = '\0';
589 /* Strip any trailing separator */
591 len = strlen(path_out);
592 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
593 path_out[len-1] = '\0';
596 return path_out;
599 /****************************************************************************
600 ****************************************************************************/
602 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
603 struct cli_state *cli,
604 const char *dir)
606 char path_sep = '\\';
608 /* Ensure the extrapath doesn't start with a separator. */
609 while (IS_DIRECTORY_SEP(*dir)) {
610 dir++;
613 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
614 path_sep = '/';
616 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
617 path_sep,
618 smbXcli_conn_remote_name(cli->conn),
619 path_sep,
620 cli->share,
621 path_sep,
622 dir);
625 /********************************************************************
626 check for dfs referral
627 ********************************************************************/
629 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
630 NTSTATUS status)
632 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
634 if (!(smbXcli_conn_use_unicode(cli->conn))) {
635 return false;
637 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
638 return false;
640 if (NT_STATUS_EQUAL(status, expected)) {
641 return true;
643 return false;
646 /********************************************************************
647 Get the dfs referral link.
648 ********************************************************************/
650 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
651 struct cli_state *cli,
652 const char *path,
653 struct client_dfs_referral **refs,
654 size_t *num_refs,
655 size_t *consumed)
657 unsigned int param_len = 0;
658 uint16_t recv_flags2;
659 uint8_t *param = NULL;
660 uint8_t *rdata = NULL;
661 char *p;
662 char *endp;
663 smb_ucs2_t *path_ucs;
664 char *consumed_path = NULL;
665 uint16_t consumed_ucs;
666 uint16_t num_referrals;
667 struct client_dfs_referral *referrals = NULL;
668 NTSTATUS status;
669 TALLOC_CTX *frame = talloc_stackframe();
671 *num_refs = 0;
672 *refs = NULL;
674 param = talloc_array(talloc_tos(), uint8_t, 2);
675 if (!param) {
676 status = NT_STATUS_NO_MEMORY;
677 goto out;
679 SSVAL(param, 0, 0x03); /* max referral level */
681 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
682 path, strlen(path)+1,
683 NULL);
684 if (!param) {
685 status = NT_STATUS_NO_MEMORY;
686 goto out;
688 param_len = talloc_get_size(param);
689 path_ucs = (smb_ucs2_t *)&param[2];
691 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
692 DATA_BLOB in_input_buffer;
693 DATA_BLOB in_output_buffer = data_blob_null;
694 DATA_BLOB out_input_buffer = data_blob_null;
695 DATA_BLOB out_output_buffer = data_blob_null;
697 in_input_buffer.data = param;
698 in_input_buffer.length = param_len;
700 status = smb2cli_ioctl(cli->conn,
701 cli->timeout,
702 cli->smb2.session,
703 cli->smb2.tcon,
704 UINT64_MAX, /* in_fid_persistent */
705 UINT64_MAX, /* in_fid_volatile */
706 FSCTL_DFS_GET_REFERRALS,
707 0, /* in_max_input_length */
708 &in_input_buffer,
709 CLI_BUFFER_SIZE, /* in_max_output_length */
710 &in_output_buffer,
711 SMB2_IOCTL_FLAG_IS_FSCTL,
712 talloc_tos(),
713 &out_input_buffer,
714 &out_output_buffer);
715 if (!NT_STATUS_IS_OK(status)) {
716 goto out;
719 if (out_output_buffer.length < 4) {
720 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
721 goto out;
724 recv_flags2 = FLAGS2_UNICODE_STRINGS;
725 rdata = out_output_buffer.data;
726 endp = (char *)rdata + out_output_buffer.length;
727 } else {
728 unsigned int data_len = 0;
729 uint16_t setup[1];
731 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
733 status = cli_trans(talloc_tos(), cli, SMBtrans2,
734 NULL, 0xffff, 0, 0,
735 setup, 1, 0,
736 param, param_len, 2,
737 NULL, 0, CLI_BUFFER_SIZE,
738 &recv_flags2,
739 NULL, 0, NULL, /* rsetup */
740 NULL, 0, NULL,
741 &rdata, 4, &data_len);
742 if (!NT_STATUS_IS_OK(status)) {
743 goto out;
746 endp = (char *)rdata + data_len;
749 consumed_ucs = SVAL(rdata, 0);
750 num_referrals = SVAL(rdata, 2);
752 /* consumed_ucs is the number of bytes
753 * of the UCS2 path consumed not counting any
754 * terminating null. We need to convert
755 * back to unix charset and count again
756 * to get the number of bytes consumed from
757 * the incoming path. */
759 errno = 0;
760 if (pull_string_talloc(talloc_tos(),
761 NULL,
763 &consumed_path,
764 path_ucs,
765 consumed_ucs,
766 STR_UNICODE) == 0) {
767 if (errno != 0) {
768 status = map_nt_error_from_unix(errno);
769 } else {
770 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
772 goto out;
774 if (consumed_path == NULL) {
775 status = map_nt_error_from_unix(errno);
776 goto out;
778 *consumed = strlen(consumed_path);
780 if (num_referrals != 0) {
781 uint16_t ref_version;
782 uint16_t ref_size;
783 int i;
784 uint16_t node_offset;
786 referrals = talloc_array(ctx, struct client_dfs_referral,
787 num_referrals);
789 if (!referrals) {
790 status = NT_STATUS_NO_MEMORY;
791 goto out;
793 /* start at the referrals array */
795 p = (char *)rdata+8;
796 for (i=0; i<num_referrals && p < endp; i++) {
797 if (p + 18 > endp) {
798 goto out;
800 ref_version = SVAL(p, 0);
801 ref_size = SVAL(p, 2);
802 node_offset = SVAL(p, 16);
804 if (ref_version != 3) {
805 p += ref_size;
806 continue;
809 referrals[i].proximity = SVAL(p, 8);
810 referrals[i].ttl = SVAL(p, 10);
812 if (p + node_offset > endp) {
813 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
814 goto out;
816 clistr_pull_talloc(referrals,
817 (const char *)rdata,
818 recv_flags2,
819 &referrals[i].dfspath,
820 p+node_offset,
821 PTR_DIFF(endp, p+node_offset),
822 STR_TERMINATE|STR_UNICODE);
824 if (!referrals[i].dfspath) {
825 status = map_nt_error_from_unix(errno);
826 goto out;
828 p += ref_size;
830 if (i < num_referrals) {
831 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
832 goto out;
836 *num_refs = num_referrals;
837 *refs = referrals;
839 out:
841 TALLOC_FREE(frame);
842 return status;
845 /********************************************************************
846 ********************************************************************/
847 struct cli_dfs_path_split {
848 char *server;
849 char *share;
850 char *extrapath;
853 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
854 const char *mountpt,
855 const struct user_auth_info *dfs_auth_info,
856 struct cli_state *rootcli,
857 const char *path,
858 struct cli_state **targetcli,
859 char **pp_targetpath)
861 struct client_dfs_referral *refs = NULL;
862 size_t num_refs = 0;
863 size_t consumed = 0;
864 struct cli_state *cli_ipc = NULL;
865 char *dfs_path = NULL;
866 char *cleanpath = NULL;
867 char *extrapath = NULL;
868 int pathlen;
869 struct cli_state *newcli = NULL;
870 struct cli_state *ccli = NULL;
871 int count = 0;
872 char *newpath = NULL;
873 char *newmount = NULL;
874 char *ppath = NULL;
875 SMB_STRUCT_STAT sbuf;
876 uint32_t attributes;
877 NTSTATUS status;
878 struct smbXcli_tcon *root_tcon = NULL;
879 struct smbXcli_tcon *target_tcon = NULL;
880 struct cli_dfs_path_split *dfs_refs = NULL;
882 if ( !rootcli || !path || !targetcli ) {
883 return NT_STATUS_INVALID_PARAMETER;
886 /* Don't do anything if this is not a DFS root. */
888 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
889 root_tcon = rootcli->smb2.tcon;
890 } else {
891 root_tcon = rootcli->smb1.tcon;
894 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
895 *targetcli = rootcli;
896 *pp_targetpath = talloc_strdup(ctx, path);
897 if (!*pp_targetpath) {
898 return NT_STATUS_NO_MEMORY;
900 return NT_STATUS_OK;
903 *targetcli = NULL;
905 /* Send a trans2_query_path_info to check for a referral. */
907 cleanpath = clean_path(ctx, path);
908 if (!cleanpath) {
909 return NT_STATUS_NO_MEMORY;
912 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
913 if (!dfs_path) {
914 return NT_STATUS_NO_MEMORY;
917 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
918 if (NT_STATUS_IS_OK(status)) {
919 /* This is an ordinary path, just return it. */
920 *targetcli = rootcli;
921 *pp_targetpath = talloc_strdup(ctx, path);
922 if (!*pp_targetpath) {
923 return NT_STATUS_NO_MEMORY;
925 goto done;
928 /* Special case where client asked for a path that does not exist */
930 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
931 status)) {
932 *targetcli = rootcli;
933 *pp_targetpath = talloc_strdup(ctx, path);
934 if (!*pp_targetpath) {
935 return NT_STATUS_NO_MEMORY;
937 goto done;
940 /* We got an error, check for DFS referral. */
942 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
943 status)) {
944 return status;
947 /* Check for the referral. */
949 status = cli_cm_open(ctx,
950 rootcli,
951 smbXcli_conn_remote_name(rootcli->conn),
952 "IPC$",
953 dfs_auth_info,
954 false,
955 smb1cli_conn_encryption_on(rootcli->conn),
956 smbXcli_conn_protocol(rootcli->conn),
958 0x20,
959 &cli_ipc);
960 if (!NT_STATUS_IS_OK(status)) {
961 return status;
964 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
965 &num_refs, &consumed);
966 if (!NT_STATUS_IS_OK(status)) {
967 return status;
970 if (!num_refs || !refs[0].dfspath) {
971 return NT_STATUS_NOT_FOUND;
975 * Bug#10123 - DFS referal entries can be provided in a random order,
976 * so check the connection cache for each item to avoid unnecessary
977 * reconnections.
979 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
980 if (dfs_refs == NULL) {
981 return NT_STATUS_NO_MEMORY;
984 for (count = 0; count < num_refs; count++) {
985 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
986 &dfs_refs[count].server,
987 &dfs_refs[count].share,
988 &dfs_refs[count].extrapath)) {
989 TALLOC_FREE(dfs_refs);
990 return NT_STATUS_NOT_FOUND;
993 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
994 dfs_refs[count].share);
995 if (ccli != NULL) {
996 extrapath = dfs_refs[count].extrapath;
997 *targetcli = ccli;
998 break;
1003 * If no cached connection was found, then connect to the first live
1004 * referral server in the list.
1006 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1007 /* Connect to the target server & share */
1008 status = cli_cm_connect(ctx, rootcli,
1009 dfs_refs[count].server,
1010 dfs_refs[count].share,
1011 dfs_auth_info,
1012 false,
1013 smb1cli_conn_encryption_on(rootcli->conn),
1014 smbXcli_conn_protocol(rootcli->conn),
1016 0x20,
1017 targetcli);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1020 dfs_refs[count].server,
1021 dfs_refs[count].share);
1022 continue;
1023 } else {
1024 extrapath = dfs_refs[count].extrapath;
1025 break;
1029 /* No available referral server for the connection */
1030 if (*targetcli == NULL) {
1031 TALLOC_FREE(dfs_refs);
1032 return status;
1035 /* Make sure to recreate the original string including any wildcards. */
1037 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1038 if (!dfs_path) {
1039 TALLOC_FREE(dfs_refs);
1040 return NT_STATUS_NO_MEMORY;
1042 pathlen = strlen(dfs_path);
1043 consumed = MIN(pathlen, consumed);
1044 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1045 if (!*pp_targetpath) {
1046 TALLOC_FREE(dfs_refs);
1047 return NT_STATUS_NO_MEMORY;
1049 dfs_path[consumed] = '\0';
1052 * *pp_targetpath is now the unconsumed part of the path.
1053 * dfs_path is now the consumed part of the path
1054 * (in \server\share\path format).
1057 if (extrapath && strlen(extrapath) > 0) {
1058 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1059 /* put the trailing \ on the path, so to be save we put one in if needed */
1060 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1061 *pp_targetpath = talloc_asprintf(ctx,
1062 "%s\\%s",
1063 extrapath,
1064 *pp_targetpath);
1065 } else {
1066 *pp_targetpath = talloc_asprintf(ctx,
1067 "%s%s",
1068 extrapath,
1069 *pp_targetpath);
1071 if (!*pp_targetpath) {
1072 TALLOC_FREE(dfs_refs);
1073 return NT_STATUS_NO_MEMORY;
1077 /* parse out the consumed mount path */
1078 /* trim off the \server\share\ */
1080 ppath = dfs_path;
1082 if (*ppath != '\\') {
1083 d_printf("cli_resolve_path: "
1084 "dfs_path (%s) not in correct format.\n",
1085 dfs_path );
1086 TALLOC_FREE(dfs_refs);
1087 return NT_STATUS_NOT_FOUND;
1090 ppath++; /* Now pointing at start of server name. */
1092 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1093 TALLOC_FREE(dfs_refs);
1094 return NT_STATUS_NOT_FOUND;
1097 ppath++; /* Now pointing at start of share name. */
1099 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1100 TALLOC_FREE(dfs_refs);
1101 return NT_STATUS_NOT_FOUND;
1104 ppath++; /* Now pointing at path component. */
1106 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1107 if (!newmount) {
1108 TALLOC_FREE(dfs_refs);
1109 return NT_STATUS_NOT_FOUND;
1112 cli_set_mntpoint(*targetcli, newmount);
1114 /* Check for another dfs referral, note that we are not
1115 checking for loops here. */
1117 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1118 status = cli_resolve_path(ctx,
1119 newmount,
1120 dfs_auth_info,
1121 *targetcli,
1122 *pp_targetpath,
1123 &newcli,
1124 &newpath);
1125 if (NT_STATUS_IS_OK(status)) {
1127 * When cli_resolve_path returns true here it's always
1128 * returning the complete path in newpath, so we're done
1129 * here.
1131 *targetcli = newcli;
1132 *pp_targetpath = newpath;
1133 TALLOC_FREE(dfs_refs);
1134 return status;
1138 done:
1140 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1141 target_tcon = (*targetcli)->smb2.tcon;
1142 } else {
1143 target_tcon = (*targetcli)->smb1.tcon;
1146 /* If returning true ensure we return a dfs root full path. */
1147 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1148 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1149 if (!dfs_path) {
1150 TALLOC_FREE(dfs_refs);
1151 return NT_STATUS_NO_MEMORY;
1153 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1154 if (*pp_targetpath == NULL) {
1155 TALLOC_FREE(dfs_refs);
1156 return NT_STATUS_NO_MEMORY;
1160 TALLOC_FREE(dfs_refs);
1161 return NT_STATUS_OK;
1164 /********************************************************************
1165 ********************************************************************/
1167 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1168 struct cli_state *cli,
1169 const char *sharename,
1170 char **pp_newserver,
1171 char **pp_newshare,
1172 bool force_encrypt,
1173 const char *username,
1174 const char *password,
1175 const char *domain)
1177 struct client_dfs_referral *refs = NULL;
1178 size_t num_refs = 0;
1179 size_t consumed = 0;
1180 char *fullpath = NULL;
1181 bool res;
1182 uint16_t cnum;
1183 char *newextrapath = NULL;
1184 NTSTATUS status;
1185 const char *remote_name;
1187 if (!cli || !sharename) {
1188 return false;
1191 remote_name = smbXcli_conn_remote_name(cli->conn);
1192 cnum = cli_state_get_tid(cli);
1194 /* special case. never check for a referral on the IPC$ share */
1196 if (strequal(sharename, "IPC$")) {
1197 return false;
1200 /* send a trans2_query_path_info to check for a referral */
1202 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1203 if (!fullpath) {
1204 return false;
1207 /* check for the referral */
1209 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1210 return false;
1213 if (force_encrypt) {
1214 status = cli_cm_force_encryption(cli,
1215 username,
1216 password,
1217 domain,
1218 "IPC$");
1219 if (!NT_STATUS_IS_OK(status)) {
1220 return false;
1224 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1225 &num_refs, &consumed);
1226 res = NT_STATUS_IS_OK(status);
1228 status = cli_tdis(cli);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 return false;
1233 cli_state_set_tid(cli, cnum);
1235 if (!res || !num_refs) {
1236 return false;
1239 if (!refs[0].dfspath) {
1240 return false;
1243 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1244 pp_newshare, &newextrapath)) {
1245 return false;
1248 /* check that this is not a self-referral */
1250 if (strequal(remote_name, *pp_newserver) &&
1251 strequal(sharename, *pp_newshare)) {
1252 return false;
1255 return true;