s3:libsmb: make use of cli_session_setup_anon()
[Samba.git] / source3 / libsmb / clidfs.c
blob86a74ec874c525a8c9109061a6f299ef19d9c590
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,
200 domain);
201 if (!NT_STATUS_IS_OK(status)) {
202 /* If a password was not supplied then
203 * try again with a null username. */
204 if (password[0] || !username[0] ||
205 get_cmdline_auth_info_use_kerberos(auth_info) ||
206 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
208 d_printf("session setup failed: %s\n",
209 nt_errstr(status));
210 if (NT_STATUS_EQUAL(status,
211 NT_STATUS_MORE_PROCESSING_REQUIRED))
212 d_printf("did you forget to run kinit?\n");
213 cli_shutdown(c);
214 return status;
216 d_printf("Anonymous login successful\n");
219 if (!NT_STATUS_IS_OK(status)) {
220 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
221 cli_shutdown(c);
222 return status;
225 if ( show_sessetup ) {
226 if (*c->server_domain) {
227 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
228 c->server_domain,c->server_os,c->server_type));
229 } else if (*c->server_os || *c->server_type) {
230 DEBUG(0,("OS=[%s] Server=[%s]\n",
231 c->server_os,c->server_type));
234 DEBUG(4,(" session setup ok\n"));
236 /* here's the fun part....to support 'msdfs proxy' shares
237 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
238 here before trying to connect to the original share.
239 cli_check_msdfs_proxy() will fail if it is a normal share. */
241 if (smbXcli_conn_dfs_supported(c->conn) &&
242 cli_check_msdfs_proxy(ctx, c, sharename,
243 &newserver, &newshare,
244 force_encrypt,
245 username,
246 password,
247 domain)) {
248 cli_shutdown(c);
249 return do_connect(ctx, newserver,
250 newshare, auth_info, false,
251 force_encrypt, max_protocol,
252 port, name_type, pcli);
255 /* must be a normal share */
257 status = cli_tree_connect(c, sharename, "?????",
258 password, strlen(password)+1);
259 if (!NT_STATUS_IS_OK(status)) {
260 d_printf("tree connect failed: %s\n", nt_errstr(status));
261 cli_shutdown(c);
262 return status;
265 if (force_encrypt) {
266 status = cli_cm_force_encryption(c,
267 username,
268 password,
269 domain,
270 sharename);
271 if (!NT_STATUS_IS_OK(status)) {
272 cli_shutdown(c);
273 return status;
277 DEBUG(4,(" tconx ok\n"));
278 *pcli = c;
279 return NT_STATUS_OK;
282 /****************************************************************************
283 ****************************************************************************/
285 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
287 TALLOC_CTX *frame = talloc_stackframe();
288 char *name = clean_name(frame, mnt);
289 if (!name) {
290 TALLOC_FREE(frame);
291 return;
293 TALLOC_FREE(cli->dfs_mountpoint);
294 cli->dfs_mountpoint = talloc_strdup(cli, name);
295 TALLOC_FREE(frame);
298 /********************************************************************
299 Add a new connection to the list.
300 referring_cli == NULL means a new initial connection.
301 ********************************************************************/
303 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
304 struct cli_state *referring_cli,
305 const char *server,
306 const char *share,
307 const struct user_auth_info *auth_info,
308 bool show_hdr,
309 bool force_encrypt,
310 int max_protocol,
311 int port,
312 int name_type,
313 struct cli_state **pcli)
315 struct cli_state *cli;
316 NTSTATUS status;
318 status = do_connect(ctx, server, share,
319 auth_info,
320 show_hdr, force_encrypt, max_protocol,
321 port, name_type, &cli);
323 if (!NT_STATUS_IS_OK(status)) {
324 return status;
327 /* Enter into the list. */
328 if (referring_cli) {
329 DLIST_ADD_END(referring_cli, cli);
332 if (referring_cli && referring_cli->requested_posix_capabilities) {
333 uint16_t major, minor;
334 uint32_t caplow, caphigh;
335 status = cli_unix_extensions_version(cli, &major, &minor,
336 &caplow, &caphigh);
337 if (NT_STATUS_IS_OK(status)) {
338 cli_set_unix_extensions_capabilities(cli,
339 major, minor,
340 caplow, caphigh);
344 *pcli = cli;
345 return NT_STATUS_OK;
348 /********************************************************************
349 Return a connection to a server on a particular share.
350 ********************************************************************/
352 static struct cli_state *cli_cm_find(struct cli_state *cli,
353 const char *server,
354 const char *share)
356 struct cli_state *p;
358 if (cli == NULL) {
359 return NULL;
362 /* Search to the start of the list. */
363 for (p = cli; p; p = DLIST_PREV(p)) {
364 const char *remote_name =
365 smbXcli_conn_remote_name(p->conn);
367 if (strequal(server, remote_name) &&
368 strequal(share,p->share)) {
369 return p;
373 /* Search to the end of the list. */
374 for (p = cli->next; p; p = p->next) {
375 const char *remote_name =
376 smbXcli_conn_remote_name(p->conn);
378 if (strequal(server, remote_name) &&
379 strequal(share,p->share)) {
380 return p;
384 return NULL;
387 /****************************************************************************
388 Open a client connection to a \\server\share.
389 ****************************************************************************/
391 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
392 struct cli_state *referring_cli,
393 const char *server,
394 const char *share,
395 const struct user_auth_info *auth_info,
396 bool show_hdr,
397 bool force_encrypt,
398 int max_protocol,
399 int port,
400 int name_type,
401 struct cli_state **pcli)
403 /* Try to reuse an existing connection in this list. */
404 struct cli_state *c = cli_cm_find(referring_cli, server, share);
405 NTSTATUS status;
407 if (c) {
408 *pcli = c;
409 return NT_STATUS_OK;
412 if (auth_info == NULL) {
413 /* Can't do a new connection
414 * without auth info. */
415 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
416 "without auth info\n",
417 server, share );
418 return NT_STATUS_INVALID_PARAMETER;
421 status = cli_cm_connect(ctx,
422 referring_cli,
423 server,
424 share,
425 auth_info,
426 show_hdr,
427 force_encrypt,
428 max_protocol,
429 port,
430 name_type,
431 &c);
432 if (!NT_STATUS_IS_OK(status)) {
433 return status;
435 *pcli = c;
436 return NT_STATUS_OK;
439 /****************************************************************************
440 ****************************************************************************/
442 void cli_cm_display(struct cli_state *cli)
444 int i;
446 for (i=0; cli; cli = cli->next,i++ ) {
447 d_printf("%d:\tserver=%s, share=%s\n",
448 i, smbXcli_conn_remote_name(cli->conn), cli->share);
452 /****************************************************************************
453 ****************************************************************************/
455 /****************************************************************************
456 ****************************************************************************/
458 #if 0
459 void cli_cm_set_credentials(struct user_auth_info *auth_info)
461 SAFE_FREE(cm_creds.username);
462 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
463 auth_info));
465 if (get_cmdline_auth_info_got_pass(auth_info)) {
466 cm_set_password(get_cmdline_auth_info_password(auth_info));
469 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
470 cm_creds.fallback_after_kerberos = false;
471 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
473 #endif
475 /**********************************************************************
476 split a dfs path into the server, share name, and extrapath components
477 **********************************************************************/
479 static bool split_dfs_path(TALLOC_CTX *ctx,
480 const char *nodepath,
481 char **pp_server,
482 char **pp_share,
483 char **pp_extrapath)
485 char *p, *q;
486 char *path;
488 *pp_server = NULL;
489 *pp_share = NULL;
490 *pp_extrapath = NULL;
492 path = talloc_strdup(ctx, nodepath);
493 if (!path) {
494 goto fail;
497 if ( path[0] != '\\' ) {
498 goto fail;
501 p = strchr_m( path + 1, '\\' );
502 if ( !p ) {
503 goto fail;
506 *p = '\0';
507 p++;
509 /* Look for any extra/deep path */
510 q = strchr_m(p, '\\');
511 if (q != NULL) {
512 *q = '\0';
513 q++;
514 *pp_extrapath = talloc_strdup(ctx, q);
515 } else {
516 *pp_extrapath = talloc_strdup(ctx, "");
518 if (*pp_extrapath == NULL) {
519 goto fail;
522 *pp_share = talloc_strdup(ctx, p);
523 if (*pp_share == NULL) {
524 goto fail;
527 *pp_server = talloc_strdup(ctx, &path[1]);
528 if (*pp_server == NULL) {
529 goto fail;
532 TALLOC_FREE(path);
533 return true;
535 fail:
536 TALLOC_FREE(*pp_share);
537 TALLOC_FREE(*pp_extrapath);
538 TALLOC_FREE(path);
539 return false;
542 /****************************************************************************
543 Return the original path truncated at the directory component before
544 the first wildcard character. Trust the caller to provide a NULL
545 terminated string
546 ****************************************************************************/
548 static char *clean_path(TALLOC_CTX *ctx, const char *path)
550 size_t len;
551 char *p1, *p2, *p;
552 char *path_out;
554 /* No absolute paths. */
555 while (IS_DIRECTORY_SEP(*path)) {
556 path++;
559 path_out = talloc_strdup(ctx, path);
560 if (!path_out) {
561 return NULL;
564 p1 = strchr_m(path_out, '*');
565 p2 = strchr_m(path_out, '?');
567 if (p1 || p2) {
568 if (p1 && p2) {
569 p = MIN(p1,p2);
570 } else if (!p1) {
571 p = p2;
572 } else {
573 p = p1;
575 *p = '\0';
577 /* Now go back to the start of this component. */
578 p1 = strrchr_m(path_out, '/');
579 p2 = strrchr_m(path_out, '\\');
580 p = MAX(p1,p2);
581 if (p) {
582 *p = '\0';
586 /* Strip any trailing separator */
588 len = strlen(path_out);
589 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
590 path_out[len-1] = '\0';
593 return path_out;
596 /****************************************************************************
597 ****************************************************************************/
599 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
600 struct cli_state *cli,
601 const char *dir)
603 char path_sep = '\\';
605 /* Ensure the extrapath doesn't start with a separator. */
606 while (IS_DIRECTORY_SEP(*dir)) {
607 dir++;
610 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
611 path_sep = '/';
613 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
614 path_sep,
615 smbXcli_conn_remote_name(cli->conn),
616 path_sep,
617 cli->share,
618 path_sep,
619 dir);
622 /********************************************************************
623 check for dfs referral
624 ********************************************************************/
626 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
627 NTSTATUS status)
629 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
631 if (!(smbXcli_conn_use_unicode(cli->conn))) {
632 return false;
634 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
635 return false;
637 if (NT_STATUS_EQUAL(status, expected)) {
638 return true;
640 return false;
643 /********************************************************************
644 Get the dfs referral link.
645 ********************************************************************/
647 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
648 struct cli_state *cli,
649 const char *path,
650 struct client_dfs_referral **refs,
651 size_t *num_refs,
652 size_t *consumed)
654 unsigned int param_len = 0;
655 uint16_t recv_flags2;
656 uint8_t *param = NULL;
657 uint8_t *rdata = NULL;
658 char *p;
659 char *endp;
660 smb_ucs2_t *path_ucs;
661 char *consumed_path = NULL;
662 uint16_t consumed_ucs;
663 uint16_t num_referrals;
664 struct client_dfs_referral *referrals = NULL;
665 NTSTATUS status;
666 TALLOC_CTX *frame = talloc_stackframe();
668 *num_refs = 0;
669 *refs = NULL;
671 param = talloc_array(talloc_tos(), uint8_t, 2);
672 if (!param) {
673 status = NT_STATUS_NO_MEMORY;
674 goto out;
676 SSVAL(param, 0, 0x03); /* max referral level */
678 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
679 path, strlen(path)+1,
680 NULL);
681 if (!param) {
682 status = NT_STATUS_NO_MEMORY;
683 goto out;
685 param_len = talloc_get_size(param);
686 path_ucs = (smb_ucs2_t *)&param[2];
688 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
689 DATA_BLOB in_input_buffer;
690 DATA_BLOB in_output_buffer = data_blob_null;
691 DATA_BLOB out_input_buffer = data_blob_null;
692 DATA_BLOB out_output_buffer = data_blob_null;
694 in_input_buffer.data = param;
695 in_input_buffer.length = param_len;
697 status = smb2cli_ioctl(cli->conn,
698 cli->timeout,
699 cli->smb2.session,
700 cli->smb2.tcon,
701 UINT64_MAX, /* in_fid_persistent */
702 UINT64_MAX, /* in_fid_volatile */
703 FSCTL_DFS_GET_REFERRALS,
704 0, /* in_max_input_length */
705 &in_input_buffer,
706 CLI_BUFFER_SIZE, /* in_max_output_length */
707 &in_output_buffer,
708 SMB2_IOCTL_FLAG_IS_FSCTL,
709 talloc_tos(),
710 &out_input_buffer,
711 &out_output_buffer);
712 if (!NT_STATUS_IS_OK(status)) {
713 goto out;
716 if (out_output_buffer.length < 4) {
717 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
718 goto out;
721 recv_flags2 = FLAGS2_UNICODE_STRINGS;
722 rdata = out_output_buffer.data;
723 endp = (char *)rdata + out_output_buffer.length;
724 } else {
725 unsigned int data_len = 0;
726 uint16_t setup[1];
728 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
730 status = cli_trans(talloc_tos(), cli, SMBtrans2,
731 NULL, 0xffff, 0, 0,
732 setup, 1, 0,
733 param, param_len, 2,
734 NULL, 0, CLI_BUFFER_SIZE,
735 &recv_flags2,
736 NULL, 0, NULL, /* rsetup */
737 NULL, 0, NULL,
738 &rdata, 4, &data_len);
739 if (!NT_STATUS_IS_OK(status)) {
740 goto out;
743 endp = (char *)rdata + data_len;
746 consumed_ucs = SVAL(rdata, 0);
747 num_referrals = SVAL(rdata, 2);
749 /* consumed_ucs is the number of bytes
750 * of the UCS2 path consumed not counting any
751 * terminating null. We need to convert
752 * back to unix charset and count again
753 * to get the number of bytes consumed from
754 * the incoming path. */
756 errno = 0;
757 if (pull_string_talloc(talloc_tos(),
758 NULL,
760 &consumed_path,
761 path_ucs,
762 consumed_ucs,
763 STR_UNICODE) == 0) {
764 if (errno != 0) {
765 status = map_nt_error_from_unix(errno);
766 } else {
767 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
769 goto out;
771 if (consumed_path == NULL) {
772 status = map_nt_error_from_unix(errno);
773 goto out;
775 *consumed = strlen(consumed_path);
777 if (num_referrals != 0) {
778 uint16_t ref_version;
779 uint16_t ref_size;
780 int i;
781 uint16_t node_offset;
783 referrals = talloc_array(ctx, struct client_dfs_referral,
784 num_referrals);
786 if (!referrals) {
787 status = NT_STATUS_NO_MEMORY;
788 goto out;
790 /* start at the referrals array */
792 p = (char *)rdata+8;
793 for (i=0; i<num_referrals && p < endp; i++) {
794 if (p + 18 > endp) {
795 goto out;
797 ref_version = SVAL(p, 0);
798 ref_size = SVAL(p, 2);
799 node_offset = SVAL(p, 16);
801 if (ref_version != 3) {
802 p += ref_size;
803 continue;
806 referrals[i].proximity = SVAL(p, 8);
807 referrals[i].ttl = SVAL(p, 10);
809 if (p + node_offset > endp) {
810 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
811 goto out;
813 clistr_pull_talloc(referrals,
814 (const char *)rdata,
815 recv_flags2,
816 &referrals[i].dfspath,
817 p+node_offset,
818 PTR_DIFF(endp, p+node_offset),
819 STR_TERMINATE|STR_UNICODE);
821 if (!referrals[i].dfspath) {
822 status = map_nt_error_from_unix(errno);
823 goto out;
825 p += ref_size;
827 if (i < num_referrals) {
828 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
829 goto out;
833 *num_refs = num_referrals;
834 *refs = referrals;
836 out:
838 TALLOC_FREE(frame);
839 return status;
842 /********************************************************************
843 ********************************************************************/
844 struct cli_dfs_path_split {
845 char *server;
846 char *share;
847 char *extrapath;
850 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
851 const char *mountpt,
852 const struct user_auth_info *dfs_auth_info,
853 struct cli_state *rootcli,
854 const char *path,
855 struct cli_state **targetcli,
856 char **pp_targetpath)
858 struct client_dfs_referral *refs = NULL;
859 size_t num_refs = 0;
860 size_t consumed = 0;
861 struct cli_state *cli_ipc = NULL;
862 char *dfs_path = NULL;
863 char *cleanpath = NULL;
864 char *extrapath = NULL;
865 int pathlen;
866 struct cli_state *newcli = NULL;
867 struct cli_state *ccli = NULL;
868 int count = 0;
869 char *newpath = NULL;
870 char *newmount = NULL;
871 char *ppath = NULL;
872 SMB_STRUCT_STAT sbuf;
873 uint32_t attributes;
874 NTSTATUS status;
875 struct smbXcli_tcon *root_tcon = NULL;
876 struct smbXcli_tcon *target_tcon = NULL;
877 struct cli_dfs_path_split *dfs_refs = NULL;
879 if ( !rootcli || !path || !targetcli ) {
880 return NT_STATUS_INVALID_PARAMETER;
883 /* Don't do anything if this is not a DFS root. */
885 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
886 root_tcon = rootcli->smb2.tcon;
887 } else {
888 root_tcon = rootcli->smb1.tcon;
891 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
892 *targetcli = rootcli;
893 *pp_targetpath = talloc_strdup(ctx, path);
894 if (!*pp_targetpath) {
895 return NT_STATUS_NO_MEMORY;
897 return NT_STATUS_OK;
900 *targetcli = NULL;
902 /* Send a trans2_query_path_info to check for a referral. */
904 cleanpath = clean_path(ctx, path);
905 if (!cleanpath) {
906 return NT_STATUS_NO_MEMORY;
909 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
910 if (!dfs_path) {
911 return NT_STATUS_NO_MEMORY;
914 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
915 if (NT_STATUS_IS_OK(status)) {
916 /* This is an ordinary path, just return it. */
917 *targetcli = rootcli;
918 *pp_targetpath = talloc_strdup(ctx, path);
919 if (!*pp_targetpath) {
920 return NT_STATUS_NO_MEMORY;
922 goto done;
925 /* Special case where client asked for a path that does not exist */
927 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
928 status)) {
929 *targetcli = rootcli;
930 *pp_targetpath = talloc_strdup(ctx, path);
931 if (!*pp_targetpath) {
932 return NT_STATUS_NO_MEMORY;
934 goto done;
937 /* We got an error, check for DFS referral. */
939 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
940 status)) {
941 return status;
944 /* Check for the referral. */
946 status = cli_cm_open(ctx,
947 rootcli,
948 smbXcli_conn_remote_name(rootcli->conn),
949 "IPC$",
950 dfs_auth_info,
951 false,
952 smb1cli_conn_encryption_on(rootcli->conn),
953 smbXcli_conn_protocol(rootcli->conn),
955 0x20,
956 &cli_ipc);
957 if (!NT_STATUS_IS_OK(status)) {
958 return status;
961 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
962 &num_refs, &consumed);
963 if (!NT_STATUS_IS_OK(status)) {
964 return status;
967 if (!num_refs || !refs[0].dfspath) {
968 return NT_STATUS_NOT_FOUND;
972 * Bug#10123 - DFS referal entries can be provided in a random order,
973 * so check the connection cache for each item to avoid unnecessary
974 * reconnections.
976 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
977 if (dfs_refs == NULL) {
978 return NT_STATUS_NO_MEMORY;
981 for (count = 0; count < num_refs; count++) {
982 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
983 &dfs_refs[count].server,
984 &dfs_refs[count].share,
985 &dfs_refs[count].extrapath)) {
986 TALLOC_FREE(dfs_refs);
987 return NT_STATUS_NOT_FOUND;
990 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
991 dfs_refs[count].share);
992 if (ccli != NULL) {
993 extrapath = dfs_refs[count].extrapath;
994 *targetcli = ccli;
995 break;
1000 * If no cached connection was found, then connect to the first live
1001 * referral server in the list.
1003 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1004 /* Connect to the target server & share */
1005 status = cli_cm_connect(ctx, rootcli,
1006 dfs_refs[count].server,
1007 dfs_refs[count].share,
1008 dfs_auth_info,
1009 false,
1010 smb1cli_conn_encryption_on(rootcli->conn),
1011 smbXcli_conn_protocol(rootcli->conn),
1013 0x20,
1014 targetcli);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1017 dfs_refs[count].server,
1018 dfs_refs[count].share);
1019 continue;
1020 } else {
1021 extrapath = dfs_refs[count].extrapath;
1022 break;
1026 /* No available referral server for the connection */
1027 if (*targetcli == NULL) {
1028 TALLOC_FREE(dfs_refs);
1029 return status;
1032 /* Make sure to recreate the original string including any wildcards. */
1034 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1035 if (!dfs_path) {
1036 TALLOC_FREE(dfs_refs);
1037 return NT_STATUS_NO_MEMORY;
1039 pathlen = strlen(dfs_path);
1040 consumed = MIN(pathlen, consumed);
1041 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1042 if (!*pp_targetpath) {
1043 TALLOC_FREE(dfs_refs);
1044 return NT_STATUS_NO_MEMORY;
1046 dfs_path[consumed] = '\0';
1049 * *pp_targetpath is now the unconsumed part of the path.
1050 * dfs_path is now the consumed part of the path
1051 * (in \server\share\path format).
1054 if (extrapath && strlen(extrapath) > 0) {
1055 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1056 /* put the trailing \ on the path, so to be save we put one in if needed */
1057 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1058 *pp_targetpath = talloc_asprintf(ctx,
1059 "%s\\%s",
1060 extrapath,
1061 *pp_targetpath);
1062 } else {
1063 *pp_targetpath = talloc_asprintf(ctx,
1064 "%s%s",
1065 extrapath,
1066 *pp_targetpath);
1068 if (!*pp_targetpath) {
1069 TALLOC_FREE(dfs_refs);
1070 return NT_STATUS_NO_MEMORY;
1074 /* parse out the consumed mount path */
1075 /* trim off the \server\share\ */
1077 ppath = dfs_path;
1079 if (*ppath != '\\') {
1080 d_printf("cli_resolve_path: "
1081 "dfs_path (%s) not in correct format.\n",
1082 dfs_path );
1083 TALLOC_FREE(dfs_refs);
1084 return NT_STATUS_NOT_FOUND;
1087 ppath++; /* Now pointing at start of server name. */
1089 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1090 TALLOC_FREE(dfs_refs);
1091 return NT_STATUS_NOT_FOUND;
1094 ppath++; /* Now pointing at start of share name. */
1096 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1097 TALLOC_FREE(dfs_refs);
1098 return NT_STATUS_NOT_FOUND;
1101 ppath++; /* Now pointing at path component. */
1103 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1104 if (!newmount) {
1105 TALLOC_FREE(dfs_refs);
1106 return NT_STATUS_NOT_FOUND;
1109 cli_set_mntpoint(*targetcli, newmount);
1111 /* Check for another dfs referral, note that we are not
1112 checking for loops here. */
1114 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1115 status = cli_resolve_path(ctx,
1116 newmount,
1117 dfs_auth_info,
1118 *targetcli,
1119 *pp_targetpath,
1120 &newcli,
1121 &newpath);
1122 if (NT_STATUS_IS_OK(status)) {
1124 * When cli_resolve_path returns true here it's always
1125 * returning the complete path in newpath, so we're done
1126 * here.
1128 *targetcli = newcli;
1129 *pp_targetpath = newpath;
1130 TALLOC_FREE(dfs_refs);
1131 return status;
1135 done:
1137 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1138 target_tcon = (*targetcli)->smb2.tcon;
1139 } else {
1140 target_tcon = (*targetcli)->smb1.tcon;
1143 /* If returning true ensure we return a dfs root full path. */
1144 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1145 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1146 if (!dfs_path) {
1147 TALLOC_FREE(dfs_refs);
1148 return NT_STATUS_NO_MEMORY;
1150 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1151 if (*pp_targetpath == NULL) {
1152 TALLOC_FREE(dfs_refs);
1153 return NT_STATUS_NO_MEMORY;
1157 TALLOC_FREE(dfs_refs);
1158 return NT_STATUS_OK;
1161 /********************************************************************
1162 ********************************************************************/
1164 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1165 struct cli_state *cli,
1166 const char *sharename,
1167 char **pp_newserver,
1168 char **pp_newshare,
1169 bool force_encrypt,
1170 const char *username,
1171 const char *password,
1172 const char *domain)
1174 struct client_dfs_referral *refs = NULL;
1175 size_t num_refs = 0;
1176 size_t consumed = 0;
1177 char *fullpath = NULL;
1178 bool res;
1179 uint16_t cnum;
1180 char *newextrapath = NULL;
1181 NTSTATUS status;
1182 const char *remote_name;
1184 if (!cli || !sharename) {
1185 return false;
1188 remote_name = smbXcli_conn_remote_name(cli->conn);
1189 cnum = cli_state_get_tid(cli);
1191 /* special case. never check for a referral on the IPC$ share */
1193 if (strequal(sharename, "IPC$")) {
1194 return false;
1197 /* send a trans2_query_path_info to check for a referral */
1199 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1200 if (!fullpath) {
1201 return false;
1204 /* check for the referral */
1206 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1207 return false;
1210 if (force_encrypt) {
1211 status = cli_cm_force_encryption(cli,
1212 username,
1213 password,
1214 domain,
1215 "IPC$");
1216 if (!NT_STATUS_IS_OK(status)) {
1217 return false;
1221 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1222 &num_refs, &consumed);
1223 res = NT_STATUS_IS_OK(status);
1225 status = cli_tdis(cli);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 return false;
1230 cli_state_set_tid(cli, cnum);
1232 if (!res || !num_refs) {
1233 return false;
1236 if (!refs[0].dfspath) {
1237 return false;
1240 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1241 pp_newshare, &newextrapath)) {
1242 return false;
1245 /* check that this is not a self-referral */
1247 if (strequal(remote_name, *pp_newserver) &&
1248 strequal(sharename, *pp_newshare)) {
1249 return false;
1252 return true;