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/>.
23 #include "libsmb/libsmb.h"
24 #include "libsmb/clirap.h"
27 #include "libsmb/nmblib.h"
28 #include "../libcli/smb/smbXcli_base.h"
29 #include "auth/credentials/credentials.h"
31 /********************************************************************
34 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
35 are not C escaped here).
37 - but if we're using POSIX paths then <pathname> may contain
38 '/' separators, not '\\' separators. So cope with '\\' or '/'
39 as a separator when looking at the pathname part.... JRA.
40 ********************************************************************/
42 /********************************************************************
43 Ensure a connection is encrypted.
44 ********************************************************************/
46 NTSTATUS cli_cm_force_encryption_creds(struct cli_state *c,
47 struct cli_credentials *creds,
48 const char *sharename)
50 uint16_t major, minor;
51 uint32_t caplow, caphigh;
54 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
55 status = smb2cli_session_encryption_on(c->smb2.session);
56 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
57 d_printf("Encryption required and "
58 "server doesn't support "
59 "SMB3 encryption - failing connect\n");
60 } else if (!NT_STATUS_IS_OK(status)) {
61 d_printf("Encryption required and "
62 "setup failed with error %s.\n",
68 if (!SERVER_HAS_UNIX_CIFS(c)) {
69 d_printf("Encryption required and "
70 "server that doesn't support "
71 "UNIX extensions - failing connect\n");
72 return NT_STATUS_NOT_SUPPORTED;
75 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
77 if (!NT_STATUS_IS_OK(status)) {
78 d_printf("Encryption required and "
79 "can't get UNIX CIFS extensions "
80 "version from server.\n");
81 return NT_STATUS_UNKNOWN_REVISION;
84 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
85 d_printf("Encryption required and "
86 "share %s doesn't support "
87 "encryption.\n", sharename);
88 return NT_STATUS_UNSUPPORTED_COMPRESSION;
91 status = cli_smb1_setup_encryption(c, creds);
92 if (!NT_STATUS_IS_OK(status)) {
93 d_printf("Encryption required and "
94 "setup failed with error %s.\n",
102 NTSTATUS cli_cm_force_encryption(struct cli_state *c,
103 const char *username,
104 const char *password,
106 const char *sharename)
108 struct cli_credentials *creds = NULL;
111 creds = cli_session_creds_init(c,
114 NULL, /* default realm */
117 c->fallback_after_kerberos,
121 return NT_STATUS_NO_MEMORY;
124 status = cli_cm_force_encryption_creds(c, creds, sharename);
125 /* gensec currently references the creds so we can't free them here */
126 talloc_unlink(c, creds);
130 /********************************************************************
131 Return a connection to a server.
132 ********************************************************************/
134 static NTSTATUS do_connect(TALLOC_CTX *ctx,
136 const struct sockaddr_storage *dest_ss,
138 const struct user_auth_info *auth_info,
144 struct cli_state **pcli)
146 struct cli_state *c = NULL;
149 char *newserver, *newshare;
152 enum protocol_types protocol = PROTOCOL_NONE;
153 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
154 struct cli_credentials *creds = NULL;
157 signing_state = SMB_SIGNING_REQUIRED;
160 /* make a copy so we don't modify the global string 'service' */
161 servicename = talloc_strdup(ctx,share);
163 return NT_STATUS_NO_MEMORY;
165 sharename = servicename;
166 if (*sharename == '\\') {
168 if (server == NULL) {
171 sharename = strchr_m(sharename,'\\');
173 return NT_STATUS_NO_MEMORY;
178 if (server == NULL) {
179 return NT_STATUS_INVALID_PARAMETER;
182 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
183 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
185 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
186 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
188 if (get_cmdline_auth_info_use_ccache(auth_info)) {
189 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
191 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
192 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
195 status = cli_connect_nb(
196 server, dest_ss, port, name_type, NULL,
200 if (!NT_STATUS_IS_OK(status)) {
201 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
202 DBG_ERR("NetBIOS support disabled, unable to connect");
205 DBG_WARNING("Connection to %s failed (Error %s)\n",
211 if (max_protocol == 0) {
212 max_protocol = PROTOCOL_LATEST;
214 DEBUG(4,(" session request ok\n"));
216 status = smbXcli_negprot(c->conn, c->timeout,
217 lp_client_min_protocol(),
220 if (!NT_STATUS_IS_OK(status)) {
221 d_printf("protocol negotiation failed: %s\n",
226 protocol = smbXcli_conn_protocol(c->conn);
227 DEBUG(4,(" negotiated dialect[%s] against server[%s]\n",
228 smb_protocol_types_string(protocol),
229 smbXcli_conn_remote_name(c->conn)));
231 if (protocol >= PROTOCOL_SMB2_02) {
232 /* Ensure we ask for some initial credits. */
233 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
236 creds = get_cmdline_auth_info_creds(auth_info);
238 status = cli_session_setup_creds(c, creds);
239 if (!NT_STATUS_IS_OK(status)) {
240 /* If a password was not supplied then
241 * try again with a null username. */
242 if (force_encrypt || smbXcli_conn_signing_mandatory(c->conn) ||
243 cli_credentials_authentication_requested(creds) ||
244 cli_credentials_is_anonymous(creds) ||
245 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
247 d_printf("session setup failed: %s\n",
249 if (NT_STATUS_EQUAL(status,
250 NT_STATUS_MORE_PROCESSING_REQUIRED))
251 d_printf("did you forget to run kinit?\n");
255 d_printf("Anonymous login successful\n");
258 if (!NT_STATUS_IS_OK(status)) {
259 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
264 if ( show_sessetup ) {
265 const char *server_name = NULL;
266 enum protocol_types protocol;
267 const char *protocol_name = NULL;
269 server_name = smbXcli_conn_remote_name(c->conn);
270 protocol = smbXcli_conn_protocol(c->conn);
271 protocol_name = smb_protocol_types_string(protocol);
273 DEBUG(0,("Server=[%s] Protocol=[%s]\n",
274 server_name, protocol_name));
276 DEBUG(4,(" session setup ok\n"));
278 /* here's the fun part....to support 'msdfs proxy' shares
279 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
280 here before trying to connect to the original share.
281 cli_check_msdfs_proxy() will fail if it is a normal share. */
283 if (smbXcli_conn_dfs_supported(c->conn) &&
284 cli_check_msdfs_proxy(ctx, c, sharename,
285 &newserver, &newshare,
286 force_encrypt, creds)) {
288 return do_connect(ctx, newserver, NULL,
289 newshare, auth_info, false,
290 force_encrypt, max_protocol,
291 port, name_type, pcli);
294 /* must be a normal share */
296 status = cli_tree_connect_creds(c, sharename, "?????", creds);
297 if (!NT_STATUS_IS_OK(status)) {
298 d_printf("tree connect failed: %s\n", nt_errstr(status));
304 status = cli_cm_force_encryption_creds(c,
307 if (!NT_STATUS_IS_OK(status)) {
313 DEBUG(4,(" tconx ok\n"));
318 /****************************************************************************
319 ****************************************************************************/
321 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
323 TALLOC_CTX *frame = talloc_stackframe();
324 char *name = clean_name(frame, mnt);
329 TALLOC_FREE(cli->dfs_mountpoint);
330 cli->dfs_mountpoint = talloc_strdup(cli, name);
334 /********************************************************************
335 Add a new connection to the list.
336 referring_cli == NULL means a new initial connection.
337 ********************************************************************/
339 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
340 struct cli_state *referring_cli,
342 const struct sockaddr_storage *dest_ss,
344 const struct user_auth_info *auth_info,
349 struct cli_state **pcli)
351 struct cli_state *cli;
354 status = do_connect(ctx, server, dest_ss, share,
356 show_hdr, force_encrypt, max_protocol,
357 port, name_type, &cli);
359 if (!NT_STATUS_IS_OK(status)) {
363 /* Enter into the list. */
365 DLIST_ADD_END(referring_cli, cli);
368 if (referring_cli && referring_cli->requested_posix_capabilities) {
369 uint16_t major, minor;
370 uint32_t caplow, caphigh;
371 status = cli_unix_extensions_version(cli, &major, &minor,
373 if (NT_STATUS_IS_OK(status)) {
374 cli_set_unix_extensions_capabilities(cli,
384 /********************************************************************
385 Return a connection to a server on a particular share.
386 ********************************************************************/
388 static struct cli_state *cli_cm_find(struct cli_state *cli,
398 /* Search to the start of the list. */
399 for (p = cli; p; p = DLIST_PREV(p)) {
400 const char *remote_name =
401 smbXcli_conn_remote_name(p->conn);
403 if (strequal(server, remote_name) &&
404 strequal(share,p->share)) {
409 /* Search to the end of the list. */
410 for (p = cli->next; p; p = p->next) {
411 const char *remote_name =
412 smbXcli_conn_remote_name(p->conn);
414 if (strequal(server, remote_name) &&
415 strequal(share,p->share)) {
423 /****************************************************************************
424 Open a client connection to a \\server\share.
425 ****************************************************************************/
427 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
428 struct cli_state *referring_cli,
430 const struct sockaddr_storage *dest_ss,
432 const struct user_auth_info *auth_info,
437 struct cli_state **pcli)
439 /* Try to reuse an existing connection in this list. */
440 struct cli_state *c = cli_cm_find(referring_cli, server, share);
448 if (auth_info == NULL) {
449 /* Can't do a new connection
450 * without auth info. */
451 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
452 "without auth info\n",
454 return NT_STATUS_INVALID_PARAMETER;
457 status = cli_cm_connect(ctx,
468 if (!NT_STATUS_IS_OK(status)) {
475 /****************************************************************************
476 ****************************************************************************/
478 void cli_cm_display(struct cli_state *cli)
482 for (i=0; cli; cli = cli->next,i++ ) {
483 d_printf("%d:\tserver=%s, share=%s\n",
484 i, smbXcli_conn_remote_name(cli->conn), cli->share);
488 /****************************************************************************
489 ****************************************************************************/
491 /****************************************************************************
492 ****************************************************************************/
495 void cli_cm_set_credentials(struct user_auth_info *auth_info)
497 SAFE_FREE(cm_creds.username);
498 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
501 if (get_cmdline_auth_info_got_pass(auth_info)) {
502 cm_set_password(get_cmdline_auth_info_password(auth_info));
505 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
506 cm_creds.fallback_after_kerberos = false;
507 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
511 /**********************************************************************
512 split a dfs path into the server, share name, and extrapath components
513 **********************************************************************/
515 static bool split_dfs_path(TALLOC_CTX *ctx,
516 const char *nodepath,
526 *pp_extrapath = NULL;
528 path = talloc_strdup(ctx, nodepath);
533 if ( path[0] != '\\' ) {
537 p = strchr_m( path + 1, '\\' );
545 /* Look for any extra/deep path */
546 q = strchr_m(p, '\\');
550 *pp_extrapath = talloc_strdup(ctx, q);
552 *pp_extrapath = talloc_strdup(ctx, "");
554 if (*pp_extrapath == NULL) {
558 *pp_share = talloc_strdup(ctx, p);
559 if (*pp_share == NULL) {
563 *pp_server = talloc_strdup(ctx, &path[1]);
564 if (*pp_server == NULL) {
572 TALLOC_FREE(*pp_share);
573 TALLOC_FREE(*pp_extrapath);
578 /****************************************************************************
579 Return the original path truncated at the directory component before
580 the first wildcard character. Trust the caller to provide a NULL
582 ****************************************************************************/
584 static char *clean_path(TALLOC_CTX *ctx, const char *path)
590 /* No absolute paths. */
591 while (IS_DIRECTORY_SEP(*path)) {
595 path_out = talloc_strdup(ctx, path);
600 p1 = strchr_m(path_out, '*');
601 p2 = strchr_m(path_out, '?');
613 /* Now go back to the start of this component. */
614 p1 = strrchr_m(path_out, '/');
615 p2 = strrchr_m(path_out, '\\');
622 /* Strip any trailing separator */
624 len = strlen(path_out);
625 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
626 path_out[len-1] = '\0';
632 /****************************************************************************
633 ****************************************************************************/
635 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
636 struct cli_state *cli,
639 char path_sep = '\\';
641 /* Ensure the extrapath doesn't start with a separator. */
642 while (IS_DIRECTORY_SEP(*dir)) {
646 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
649 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
651 smbXcli_conn_remote_name(cli->conn),
658 /********************************************************************
659 check for dfs referral
660 ********************************************************************/
662 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
665 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
667 if (!(smbXcli_conn_use_unicode(cli->conn))) {
670 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
673 if (NT_STATUS_EQUAL(status, expected)) {
679 /********************************************************************
680 Get the dfs referral link.
681 ********************************************************************/
683 NTSTATUS cli_dfs_get_referral_ex(TALLOC_CTX *ctx,
684 struct cli_state *cli,
686 uint16_t max_referral_level,
687 struct client_dfs_referral **refs,
691 unsigned int param_len = 0;
692 uint16_t recv_flags2;
693 uint8_t *param = NULL;
694 uint8_t *rdata = NULL;
697 smb_ucs2_t *path_ucs;
698 char *consumed_path = NULL;
699 uint16_t consumed_ucs;
700 uint16_t num_referrals;
701 struct client_dfs_referral *referrals = NULL;
703 TALLOC_CTX *frame = talloc_stackframe();
708 param = talloc_array(talloc_tos(), uint8_t, 2);
710 status = NT_STATUS_NO_MEMORY;
713 SSVAL(param, 0, max_referral_level);
715 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
716 path, strlen(path)+1,
719 status = NT_STATUS_NO_MEMORY;
722 param_len = talloc_get_size(param);
723 path_ucs = (smb_ucs2_t *)¶m[2];
725 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
726 DATA_BLOB in_input_buffer;
727 DATA_BLOB in_output_buffer = data_blob_null;
728 DATA_BLOB out_input_buffer = data_blob_null;
729 DATA_BLOB out_output_buffer = data_blob_null;
731 in_input_buffer.data = param;
732 in_input_buffer.length = param_len;
734 status = smb2cli_ioctl(cli->conn,
738 UINT64_MAX, /* in_fid_persistent */
739 UINT64_MAX, /* in_fid_volatile */
740 FSCTL_DFS_GET_REFERRALS,
741 0, /* in_max_input_length */
743 CLI_BUFFER_SIZE, /* in_max_output_length */
745 SMB2_IOCTL_FLAG_IS_FSCTL,
749 if (!NT_STATUS_IS_OK(status)) {
753 if (out_output_buffer.length < 4) {
754 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
758 recv_flags2 = FLAGS2_UNICODE_STRINGS;
759 rdata = out_output_buffer.data;
760 endp = (char *)rdata + out_output_buffer.length;
762 unsigned int data_len = 0;
765 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
767 status = cli_trans(talloc_tos(), cli, SMBtrans2,
771 NULL, 0, CLI_BUFFER_SIZE,
773 NULL, 0, NULL, /* rsetup */
775 &rdata, 4, &data_len);
776 if (!NT_STATUS_IS_OK(status)) {
780 endp = (char *)rdata + data_len;
783 consumed_ucs = SVAL(rdata, 0);
784 num_referrals = SVAL(rdata, 2);
786 /* consumed_ucs is the number of bytes
787 * of the UCS2 path consumed not counting any
788 * terminating null. We need to convert
789 * back to unix charset and count again
790 * to get the number of bytes consumed from
791 * the incoming path. */
794 if (pull_string_talloc(talloc_tos(),
802 status = map_nt_error_from_unix(errno);
804 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
808 if (consumed_path == NULL) {
809 status = map_nt_error_from_unix(errno);
812 *consumed = strlen(consumed_path);
814 if (num_referrals != 0) {
815 uint16_t ref_version;
818 uint16_t node_offset;
820 referrals = talloc_array(ctx, struct client_dfs_referral,
824 status = NT_STATUS_NO_MEMORY;
827 /* start at the referrals array */
830 for (i=0; i<num_referrals && p < endp; i++) {
834 ref_version = SVAL(p, 0);
835 ref_size = SVAL(p, 2);
836 node_offset = SVAL(p, 16);
838 if (ref_version != 3) {
843 referrals[i].proximity = SVAL(p, 8);
844 referrals[i].ttl = SVAL(p, 10);
846 if (p + node_offset > endp) {
847 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
850 clistr_pull_talloc(referrals,
853 &referrals[i].dfspath,
855 PTR_DIFF(endp, p+node_offset),
856 STR_TERMINATE|STR_UNICODE);
858 if (!referrals[i].dfspath) {
859 status = map_nt_error_from_unix(errno);
864 if (i < num_referrals) {
865 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
870 *num_refs = num_referrals;
879 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
880 struct cli_state *cli,
882 struct client_dfs_referral **refs,
886 return cli_dfs_get_referral_ex(ctx,
890 refs, /* Max referral level we want */
895 /********************************************************************
896 ********************************************************************/
897 struct cli_dfs_path_split {
903 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
905 const struct user_auth_info *dfs_auth_info,
906 struct cli_state *rootcli,
908 struct cli_state **targetcli,
909 char **pp_targetpath)
911 struct client_dfs_referral *refs = NULL;
914 struct cli_state *cli_ipc = NULL;
915 char *dfs_path = NULL;
916 char *cleanpath = NULL;
917 char *extrapath = NULL;
919 struct cli_state *newcli = NULL;
920 struct cli_state *ccli = NULL;
922 char *newpath = NULL;
923 char *newmount = NULL;
925 SMB_STRUCT_STAT sbuf;
928 struct cli_credentials *dfs_creds = NULL;
929 bool force_encryption = true;
930 struct smbXcli_tcon *root_tcon = NULL;
931 struct smbXcli_tcon *target_tcon = NULL;
932 struct cli_dfs_path_split *dfs_refs = NULL;
934 if ( !rootcli || !path || !targetcli ) {
935 return NT_STATUS_INVALID_PARAMETER;
938 /* Don't do anything if this is not a DFS root. */
940 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
941 root_tcon = rootcli->smb2.tcon;
942 force_encryption = smb2cli_tcon_is_encryption_on(root_tcon);
944 root_tcon = rootcli->smb1.tcon;
945 force_encryption = smb1cli_conn_encryption_on(rootcli->conn);
949 * Avoid more than one leading directory separator
951 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
955 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
956 *targetcli = rootcli;
957 *pp_targetpath = talloc_strdup(ctx, path);
958 if (!*pp_targetpath) {
959 return NT_STATUS_NO_MEMORY;
966 /* Send a trans2_query_path_info to check for a referral. */
968 cleanpath = clean_path(ctx, path);
970 return NT_STATUS_NO_MEMORY;
973 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
975 return NT_STATUS_NO_MEMORY;
978 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
979 if (NT_STATUS_IS_OK(status)) {
980 /* This is an ordinary path, just return it. */
981 *targetcli = rootcli;
982 *pp_targetpath = talloc_strdup(ctx, path);
983 if (!*pp_targetpath) {
984 return NT_STATUS_NO_MEMORY;
989 /* Special case where client asked for a path that does not exist */
991 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
993 *targetcli = rootcli;
994 *pp_targetpath = talloc_strdup(ctx, path);
995 if (!*pp_targetpath) {
996 return NT_STATUS_NO_MEMORY;
1001 /* We got an error, check for DFS referral. */
1003 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
1008 /* Check for the referral. */
1010 cli_ipc = cli_state_dup(ctx, rootcli);
1011 if (cli_ipc == NULL) {
1012 return NT_STATUS_NO_MEMORY;
1015 dfs_creds = get_cmdline_auth_info_creds(auth_info);
1017 status = cli_tree_connect_creds(cli_ipc, "IPC$", "IPC", dfs_creds);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 d_printf("tree connect failed: %s\n", nt_errstr(status));
1020 cli_shutdown(cli_ipc);
1024 if (force_encrypt) {
1025 status = cli_cm_force_encryption_creds(cli_ipc,
1028 if (!NT_STATUS_IS_OK(status)) {
1029 cli_shutdown(cli_ipc);
1034 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
1035 &num_refs, &consumed);
1036 if (!NT_STATUS_IS_OK(status)) {
1040 if (!num_refs || !refs[0].dfspath) {
1041 return NT_STATUS_NOT_FOUND;
1045 * Bug#10123 - DFS referal entries can be provided in a random order,
1046 * so check the connection cache for each item to avoid unnecessary
1049 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
1050 if (dfs_refs == NULL) {
1051 return NT_STATUS_NO_MEMORY;
1054 for (count = 0; count < num_refs; count++) {
1055 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1056 &dfs_refs[count].server,
1057 &dfs_refs[count].share,
1058 &dfs_refs[count].extrapath)) {
1059 TALLOC_FREE(dfs_refs);
1060 return NT_STATUS_NOT_FOUND;
1063 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1064 dfs_refs[count].share);
1066 extrapath = dfs_refs[count].extrapath;
1073 * If no cached connection was found, then connect to the first live
1074 * referral server in the list.
1076 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1077 /* Connect to the target server & share */
1078 status = cli_cm_connect(ctx, rootcli,
1079 dfs_refs[count].server,
1081 dfs_refs[count].share,
1083 cli_state_is_encryption_on(rootcli),
1084 smbXcli_conn_protocol(rootcli->conn),
1088 if (!NT_STATUS_IS_OK(status)) {
1089 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1090 dfs_refs[count].server,
1091 dfs_refs[count].share);
1094 extrapath = dfs_refs[count].extrapath;
1099 /* No available referral server for the connection */
1100 if (*targetcli == NULL) {
1101 TALLOC_FREE(dfs_refs);
1105 /* Make sure to recreate the original string including any wildcards. */
1107 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1109 TALLOC_FREE(dfs_refs);
1110 return NT_STATUS_NO_MEMORY;
1112 pathlen = strlen(dfs_path);
1113 consumed = MIN(pathlen, consumed);
1114 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1115 if (!*pp_targetpath) {
1116 TALLOC_FREE(dfs_refs);
1117 return NT_STATUS_NO_MEMORY;
1119 dfs_path[consumed] = '\0';
1122 * *pp_targetpath is now the unconsumed part of the path.
1123 * dfs_path is now the consumed part of the path
1124 * (in \server\share\path format).
1127 if (extrapath && strlen(extrapath) > 0) {
1128 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1129 /* put the trailing \ on the path, so to be save we put one in if needed */
1130 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1131 *pp_targetpath = talloc_asprintf(ctx,
1136 *pp_targetpath = talloc_asprintf(ctx,
1141 if (!*pp_targetpath) {
1142 TALLOC_FREE(dfs_refs);
1143 return NT_STATUS_NO_MEMORY;
1147 /* parse out the consumed mount path */
1148 /* trim off the \server\share\ */
1152 if (*ppath != '\\') {
1153 d_printf("cli_resolve_path: "
1154 "dfs_path (%s) not in correct format.\n",
1156 TALLOC_FREE(dfs_refs);
1157 return NT_STATUS_NOT_FOUND;
1160 ppath++; /* Now pointing at start of server name. */
1162 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1163 TALLOC_FREE(dfs_refs);
1164 return NT_STATUS_NOT_FOUND;
1167 ppath++; /* Now pointing at start of share name. */
1169 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1170 TALLOC_FREE(dfs_refs);
1171 return NT_STATUS_NOT_FOUND;
1174 ppath++; /* Now pointing at path component. */
1176 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1178 TALLOC_FREE(dfs_refs);
1179 return NT_STATUS_NOT_FOUND;
1182 cli_set_mntpoint(*targetcli, newmount);
1184 /* Check for another dfs referral, note that we are not
1185 checking for loops here. */
1187 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1188 status = cli_resolve_path(ctx,
1195 if (NT_STATUS_IS_OK(status)) {
1197 * When cli_resolve_path returns true here it's always
1198 * returning the complete path in newpath, so we're done
1201 *targetcli = newcli;
1202 *pp_targetpath = newpath;
1203 TALLOC_FREE(dfs_refs);
1210 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1211 target_tcon = (*targetcli)->smb2.tcon;
1213 target_tcon = (*targetcli)->smb1.tcon;
1216 /* If returning true ensure we return a dfs root full path. */
1217 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1218 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1220 TALLOC_FREE(dfs_refs);
1221 return NT_STATUS_NO_MEMORY;
1223 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1224 if (*pp_targetpath == NULL) {
1225 TALLOC_FREE(dfs_refs);
1226 return NT_STATUS_NO_MEMORY;
1230 TALLOC_FREE(dfs_refs);
1231 return NT_STATUS_OK;
1234 /********************************************************************
1235 ********************************************************************/
1237 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1238 struct cli_state *cli,
1239 const char *sharename,
1240 char **pp_newserver,
1243 struct cli_credentials *creds)
1245 struct client_dfs_referral *refs = NULL;
1246 size_t num_refs = 0;
1247 size_t consumed = 0;
1248 char *fullpath = NULL;
1250 struct smbXcli_tcon *orig_tcon = NULL;
1251 char *newextrapath = NULL;
1253 const char *remote_name;
1255 if (!cli || !sharename) {
1259 remote_name = smbXcli_conn_remote_name(cli->conn);
1261 /* special case. never check for a referral on the IPC$ share */
1263 if (strequal(sharename, "IPC$")) {
1267 /* send a trans2_query_path_info to check for a referral */
1269 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1274 /* Store tcon state. */
1275 if (cli_state_has_tcon(cli)) {
1276 orig_tcon = cli_state_save_tcon(cli);
1277 if (orig_tcon == NULL) {
1282 /* check for the referral */
1284 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1285 cli_state_restore_tcon(cli, orig_tcon);
1289 if (force_encrypt) {
1290 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1291 if (!NT_STATUS_IS_OK(status)) {
1292 cli_state_restore_tcon(cli, orig_tcon);
1297 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1298 &num_refs, &consumed);
1299 res = NT_STATUS_IS_OK(status);
1301 status = cli_tdis(cli);
1303 cli_state_restore_tcon(cli, orig_tcon);
1305 if (!NT_STATUS_IS_OK(status)) {
1309 if (!res || !num_refs) {
1313 if (!refs[0].dfspath) {
1317 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1318 pp_newshare, &newextrapath)) {
1322 /* check that this is not a self-referral */
1324 if (strequal(remote_name, *pp_newserver) &&
1325 strequal(sharename, *pp_newshare)) {