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 /********************************************************************
103 Return a connection to a server.
104 ********************************************************************/
106 static NTSTATUS do_connect(TALLOC_CTX *ctx,
109 const struct user_auth_info *auth_info,
112 const struct sockaddr_storage *dest_ss,
115 struct cli_state **pcli)
117 struct cli_state *c = NULL;
120 char *newserver, *newshare;
123 enum protocol_types protocol = PROTOCOL_NONE;
124 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
125 struct cli_credentials *creds = NULL;
128 signing_state = SMB_SIGNING_REQUIRED;
131 /* make a copy so we don't modify the global string 'service' */
132 servicename = talloc_strdup(ctx,share);
134 return NT_STATUS_NO_MEMORY;
136 sharename = servicename;
137 if (*sharename == '\\') {
139 if (server == NULL) {
142 sharename = strchr_m(sharename,'\\');
144 return NT_STATUS_NO_MEMORY;
149 if (server == NULL) {
150 return NT_STATUS_INVALID_PARAMETER;
153 if (get_cmdline_auth_info_use_kerberos(auth_info)) {
154 flags |= CLI_FULL_CONNECTION_USE_KERBEROS;
156 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info)) {
157 flags |= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
159 if (get_cmdline_auth_info_use_ccache(auth_info)) {
160 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
162 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info)) {
163 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
166 status = cli_connect_nb(
167 server, dest_ss, port, name_type, NULL,
171 if (!NT_STATUS_IS_OK(status)) {
172 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
173 DBG_ERR("NetBIOS support disabled, unable to connect");
176 DBG_WARNING("Connection to %s failed (Error %s)\n",
182 if (max_protocol == 0) {
183 max_protocol = PROTOCOL_LATEST;
185 DEBUG(4,(" session request ok\n"));
187 status = smbXcli_negprot(c->conn, c->timeout,
188 lp_client_min_protocol(),
191 if (!NT_STATUS_IS_OK(status)) {
192 d_printf("protocol negotiation failed: %s\n",
197 protocol = smbXcli_conn_protocol(c->conn);
198 DEBUG(4,(" negotiated dialect[%s] against server[%s]\n",
199 smb_protocol_types_string(protocol),
200 smbXcli_conn_remote_name(c->conn)));
202 if (protocol >= PROTOCOL_SMB2_02) {
203 /* Ensure we ask for some initial credits. */
204 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
207 creds = get_cmdline_auth_info_creds(auth_info);
209 status = cli_session_setup_creds(c, creds);
210 if (!NT_STATUS_IS_OK(status)) {
211 /* If a password was not supplied then
212 * try again with a null username. */
213 if (force_encrypt || smbXcli_conn_signing_mandatory(c->conn) ||
214 cli_credentials_authentication_requested(creds) ||
215 cli_credentials_is_anonymous(creds) ||
216 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
218 d_printf("session setup failed: %s\n",
220 if (NT_STATUS_EQUAL(status,
221 NT_STATUS_MORE_PROCESSING_REQUIRED))
222 d_printf("did you forget to run kinit?\n");
226 d_printf("Anonymous login successful\n");
229 if (!NT_STATUS_IS_OK(status)) {
230 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
235 DEBUG(4,(" session setup ok\n"));
237 /* here's the fun part....to support 'msdfs proxy' shares
238 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
239 here before trying to connect to the original share.
240 cli_check_msdfs_proxy() will fail if it is a normal share. */
242 if (smbXcli_conn_dfs_supported(c->conn) &&
243 cli_check_msdfs_proxy(ctx, c, sharename,
244 &newserver, &newshare,
245 force_encrypt, creds)) {
247 return do_connect(ctx, newserver,
249 force_encrypt, max_protocol,
250 NULL, port, name_type, pcli);
253 /* must be a normal share */
255 status = cli_tree_connect_creds(c, sharename, "?????", creds);
256 if (!NT_STATUS_IS_OK(status)) {
257 d_printf("tree connect failed: %s\n", nt_errstr(status));
263 status = cli_cm_force_encryption_creds(c,
266 if (!NT_STATUS_IS_OK(status)) {
272 DEBUG(4,(" tconx ok\n"));
277 /****************************************************************************
278 ****************************************************************************/
280 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
282 TALLOC_CTX *frame = talloc_stackframe();
283 char *name = clean_name(frame, mnt);
288 TALLOC_FREE(cli->dfs_mountpoint);
289 cli->dfs_mountpoint = talloc_strdup(cli, name);
293 /********************************************************************
294 Add a new connection to the list.
295 referring_cli == NULL means a new initial connection.
296 ********************************************************************/
298 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
299 struct cli_state *referring_cli,
302 const struct user_auth_info *auth_info,
305 const struct sockaddr_storage *dest_ss,
308 struct cli_state **pcli)
310 struct cli_state *cli = NULL;
313 status = do_connect(ctx, server, share,
315 force_encrypt, max_protocol,
316 dest_ss, port, name_type, &cli);
318 if (!NT_STATUS_IS_OK(status)) {
323 * This can't happen, this test is to satisfy static
327 return NT_STATUS_NO_MEMORY;
330 /* Enter into the list. */
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,
340 if (NT_STATUS_IS_OK(status)) {
341 cli_set_unix_extensions_capabilities(cli,
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,
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)) {
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)) {
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,
398 const struct user_auth_info *auth_info,
401 const struct sockaddr_storage *dest_ss,
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);
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",
421 return NT_STATUS_INVALID_PARAMETER;
424 status = cli_cm_connect(ctx,
435 if (!NT_STATUS_IS_OK(status)) {
442 /****************************************************************************
443 ****************************************************************************/
445 void cli_cm_display(struct cli_state *cli)
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 ****************************************************************************/
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(
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);
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,
493 *pp_extrapath = NULL;
495 path = talloc_strdup(ctx, nodepath);
500 if ( path[0] != '\\' ) {
504 p = strchr_m( path + 1, '\\' );
512 /* Look for any extra/deep path */
513 q = strchr_m(p, '\\');
517 *pp_extrapath = talloc_strdup(ctx, q);
519 *pp_extrapath = talloc_strdup(ctx, "");
521 if (*pp_extrapath == NULL) {
525 *pp_share = talloc_strdup(ctx, p);
526 if (*pp_share == NULL) {
530 *pp_server = talloc_strdup(ctx, &path[1]);
531 if (*pp_server == NULL) {
539 TALLOC_FREE(*pp_share);
540 TALLOC_FREE(*pp_extrapath);
545 /****************************************************************************
546 Return the original path truncated at the directory component before
547 the first wildcard character. Trust the caller to provide a NULL
549 ****************************************************************************/
551 static char *clean_path(TALLOC_CTX *ctx, const char *path)
557 /* No absolute paths. */
558 while (IS_DIRECTORY_SEP(*path)) {
562 path_out = talloc_strdup(ctx, path);
567 p1 = strchr_m(path_out, '*');
568 p2 = strchr_m(path_out, '?');
580 /* Now go back to the start of this component. */
581 p1 = strrchr_m(path_out, '/');
582 p2 = strrchr_m(path_out, '\\');
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';
599 /****************************************************************************
600 ****************************************************************************/
602 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
603 struct cli_state *cli,
606 char path_sep = '\\';
608 /* Ensure the extrapath doesn't start with a separator. */
609 while (IS_DIRECTORY_SEP(*dir)) {
613 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
616 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
618 smbXcli_conn_remote_name(cli->conn),
625 /********************************************************************
626 check for dfs referral
627 ********************************************************************/
629 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
632 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
634 if (!(smbXcli_conn_use_unicode(cli->conn))) {
637 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
640 if (NT_STATUS_EQUAL(status, expected)) {
646 /********************************************************************
647 Get the dfs referral link.
648 ********************************************************************/
650 NTSTATUS cli_dfs_get_referral_ex(TALLOC_CTX *ctx,
651 struct cli_state *cli,
653 uint16_t max_referral_level,
654 struct client_dfs_referral **refs,
658 unsigned int param_len = 0;
659 uint16_t recv_flags2;
660 uint8_t *param = NULL;
661 uint8_t *rdata = NULL;
664 smb_ucs2_t *path_ucs;
665 char *consumed_path = NULL;
666 uint16_t consumed_ucs;
667 uint16_t num_referrals;
668 struct client_dfs_referral *referrals = NULL;
670 TALLOC_CTX *frame = talloc_stackframe();
675 param = talloc_array(talloc_tos(), uint8_t, 2);
677 status = NT_STATUS_NO_MEMORY;
680 SSVAL(param, 0, max_referral_level);
682 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
683 path, strlen(path)+1,
686 status = NT_STATUS_NO_MEMORY;
689 param_len = talloc_get_size(param);
690 path_ucs = (smb_ucs2_t *)¶m[2];
692 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
693 DATA_BLOB in_input_buffer;
694 DATA_BLOB in_output_buffer = data_blob_null;
695 DATA_BLOB out_input_buffer = data_blob_null;
696 DATA_BLOB out_output_buffer = data_blob_null;
698 in_input_buffer.data = param;
699 in_input_buffer.length = param_len;
701 status = smb2cli_ioctl(cli->conn,
705 UINT64_MAX, /* in_fid_persistent */
706 UINT64_MAX, /* in_fid_volatile */
707 FSCTL_DFS_GET_REFERRALS,
708 0, /* in_max_input_length */
710 CLI_BUFFER_SIZE, /* in_max_output_length */
712 SMB2_IOCTL_FLAG_IS_FSCTL,
716 if (!NT_STATUS_IS_OK(status)) {
720 if (out_output_buffer.length < 4) {
721 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
725 recv_flags2 = FLAGS2_UNICODE_STRINGS;
726 rdata = out_output_buffer.data;
727 endp = (char *)rdata + out_output_buffer.length;
729 unsigned int data_len = 0;
732 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
734 status = cli_trans(talloc_tos(), cli, SMBtrans2,
738 NULL, 0, CLI_BUFFER_SIZE,
740 NULL, 0, NULL, /* rsetup */
742 &rdata, 4, &data_len);
743 if (!NT_STATUS_IS_OK(status)) {
747 endp = (char *)rdata + data_len;
750 consumed_ucs = SVAL(rdata, 0);
751 num_referrals = SVAL(rdata, 2);
753 /* consumed_ucs is the number of bytes
754 * of the UCS2 path consumed not counting any
755 * terminating null. We need to convert
756 * back to unix charset and count again
757 * to get the number of bytes consumed from
758 * the incoming path. */
761 if (pull_string_talloc(talloc_tos(),
769 status = map_nt_error_from_unix(errno);
771 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
775 if (consumed_path == NULL) {
776 status = map_nt_error_from_unix(errno);
779 *consumed = strlen(consumed_path);
781 if (num_referrals != 0) {
782 uint16_t ref_version;
785 uint16_t node_offset;
787 referrals = talloc_array(ctx, struct client_dfs_referral,
791 status = NT_STATUS_NO_MEMORY;
794 /* start at the referrals array */
797 for (i=0; i<num_referrals && p < endp; i++) {
801 ref_version = SVAL(p, 0);
802 ref_size = SVAL(p, 2);
803 node_offset = SVAL(p, 16);
805 if (ref_version != 3) {
810 referrals[i].proximity = SVAL(p, 8);
811 referrals[i].ttl = SVAL(p, 10);
813 if (p + node_offset > endp) {
814 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
817 clistr_pull_talloc(referrals,
820 &referrals[i].dfspath,
822 PTR_DIFF(endp, p+node_offset),
823 STR_TERMINATE|STR_UNICODE);
825 if (!referrals[i].dfspath) {
826 status = map_nt_error_from_unix(errno);
831 if (i < num_referrals) {
832 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
837 *num_refs = num_referrals;
846 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
847 struct cli_state *cli,
849 struct client_dfs_referral **refs,
853 return cli_dfs_get_referral_ex(ctx,
857 refs, /* Max referral level we want */
862 /********************************************************************
863 ********************************************************************/
864 struct cli_dfs_path_split {
870 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
872 const struct user_auth_info *dfs_auth_info,
873 struct cli_state *rootcli,
875 struct cli_state **targetcli,
876 char **pp_targetpath)
878 struct client_dfs_referral *refs = NULL;
881 struct cli_state *cli_ipc = NULL;
882 char *dfs_path = NULL;
883 char *cleanpath = NULL;
884 char *extrapath = NULL;
886 struct cli_state *newcli = NULL;
887 struct cli_state *ccli = NULL;
889 char *newpath = NULL;
890 char *newmount = NULL;
892 SMB_STRUCT_STAT sbuf;
895 struct smbXcli_tcon *root_tcon = NULL;
896 struct smbXcli_tcon *target_tcon = NULL;
897 struct cli_dfs_path_split *dfs_refs = NULL;
899 if ( !rootcli || !path || !targetcli ) {
900 return NT_STATUS_INVALID_PARAMETER;
903 /* Don't do anything if this is not a DFS root. */
905 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
906 root_tcon = rootcli->smb2.tcon;
908 root_tcon = rootcli->smb1.tcon;
912 * Avoid more than one leading directory separator
914 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
918 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
919 *targetcli = rootcli;
920 *pp_targetpath = talloc_strdup(ctx, path);
921 if (!*pp_targetpath) {
922 return NT_STATUS_NO_MEMORY;
929 /* Send a trans2_query_path_info to check for a referral. */
931 cleanpath = clean_path(ctx, path);
933 return NT_STATUS_NO_MEMORY;
936 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
938 return NT_STATUS_NO_MEMORY;
941 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
942 if (NT_STATUS_IS_OK(status)) {
943 /* This is an ordinary path, just return it. */
944 *targetcli = rootcli;
945 *pp_targetpath = talloc_strdup(ctx, path);
946 if (!*pp_targetpath) {
947 return NT_STATUS_NO_MEMORY;
952 /* Special case where client asked for a path that does not exist */
954 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
956 *targetcli = rootcli;
957 *pp_targetpath = talloc_strdup(ctx, path);
958 if (!*pp_targetpath) {
959 return NT_STATUS_NO_MEMORY;
964 /* We got an error, check for DFS referral. */
966 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
971 /* Check for the referral. */
973 status = cli_cm_open(ctx,
975 smbXcli_conn_remote_name(rootcli->conn),
978 cli_state_is_encryption_on(rootcli),
979 smbXcli_conn_protocol(rootcli->conn),
980 NULL, /* dest_ss not needed, we reuse the transport */
984 if (!NT_STATUS_IS_OK(status)) {
988 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
989 &num_refs, &consumed);
990 if (!NT_STATUS_IS_OK(status)) {
994 if (!num_refs || !refs[0].dfspath) {
995 return NT_STATUS_NOT_FOUND;
999 * Bug#10123 - DFS referal entries can be provided in a random order,
1000 * so check the connection cache for each item to avoid unnecessary
1003 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
1004 if (dfs_refs == NULL) {
1005 return NT_STATUS_NO_MEMORY;
1008 for (count = 0; count < num_refs; count++) {
1009 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1010 &dfs_refs[count].server,
1011 &dfs_refs[count].share,
1012 &dfs_refs[count].extrapath)) {
1013 TALLOC_FREE(dfs_refs);
1014 return NT_STATUS_NOT_FOUND;
1017 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1018 dfs_refs[count].share);
1020 extrapath = dfs_refs[count].extrapath;
1027 * If no cached connection was found, then connect to the first live
1028 * referral server in the list.
1030 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1031 /* Connect to the target server & share */
1032 status = cli_cm_connect(ctx, rootcli,
1033 dfs_refs[count].server,
1034 dfs_refs[count].share,
1036 cli_state_is_encryption_on(rootcli),
1037 smbXcli_conn_protocol(rootcli->conn),
1042 if (!NT_STATUS_IS_OK(status)) {
1043 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1044 dfs_refs[count].server,
1045 dfs_refs[count].share);
1048 extrapath = dfs_refs[count].extrapath;
1053 /* No available referral server for the connection */
1054 if (*targetcli == NULL) {
1055 TALLOC_FREE(dfs_refs);
1059 /* Make sure to recreate the original string including any wildcards. */
1061 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1063 TALLOC_FREE(dfs_refs);
1064 return NT_STATUS_NO_MEMORY;
1066 pathlen = strlen(dfs_path);
1067 consumed = MIN(pathlen, consumed);
1068 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1069 if (!*pp_targetpath) {
1070 TALLOC_FREE(dfs_refs);
1071 return NT_STATUS_NO_MEMORY;
1073 dfs_path[consumed] = '\0';
1076 * *pp_targetpath is now the unconsumed part of the path.
1077 * dfs_path is now the consumed part of the path
1078 * (in \server\share\path format).
1081 if (extrapath && strlen(extrapath) > 0) {
1082 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1083 /* put the trailing \ on the path, so to be save we put one in if needed */
1084 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1085 *pp_targetpath = talloc_asprintf(ctx,
1090 *pp_targetpath = talloc_asprintf(ctx,
1095 if (!*pp_targetpath) {
1096 TALLOC_FREE(dfs_refs);
1097 return NT_STATUS_NO_MEMORY;
1101 /* parse out the consumed mount path */
1102 /* trim off the \server\share\ */
1106 if (*ppath != '\\') {
1107 d_printf("cli_resolve_path: "
1108 "dfs_path (%s) not in correct format.\n",
1110 TALLOC_FREE(dfs_refs);
1111 return NT_STATUS_NOT_FOUND;
1114 ppath++; /* Now pointing at start of server name. */
1116 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1117 TALLOC_FREE(dfs_refs);
1118 return NT_STATUS_NOT_FOUND;
1121 ppath++; /* Now pointing at start of share name. */
1123 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1124 TALLOC_FREE(dfs_refs);
1125 return NT_STATUS_NOT_FOUND;
1128 ppath++; /* Now pointing at path component. */
1130 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1132 TALLOC_FREE(dfs_refs);
1133 return NT_STATUS_NOT_FOUND;
1136 cli_set_mntpoint(*targetcli, newmount);
1138 /* Check for another dfs referral, note that we are not
1139 checking for loops here. */
1141 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1142 status = cli_resolve_path(ctx,
1149 if (NT_STATUS_IS_OK(status)) {
1151 * When cli_resolve_path returns true here it's always
1152 * returning the complete path in newpath, so we're done
1155 *targetcli = newcli;
1156 *pp_targetpath = newpath;
1157 TALLOC_FREE(dfs_refs);
1164 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1165 target_tcon = (*targetcli)->smb2.tcon;
1167 target_tcon = (*targetcli)->smb1.tcon;
1170 /* If returning true ensure we return a dfs root full path. */
1171 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1172 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1174 TALLOC_FREE(dfs_refs);
1175 return NT_STATUS_NO_MEMORY;
1177 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1178 if (*pp_targetpath == NULL) {
1179 TALLOC_FREE(dfs_refs);
1180 return NT_STATUS_NO_MEMORY;
1184 TALLOC_FREE(dfs_refs);
1185 return NT_STATUS_OK;
1188 /********************************************************************
1189 ********************************************************************/
1191 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1192 struct cli_state *cli,
1193 const char *sharename,
1194 char **pp_newserver,
1197 struct cli_credentials *creds)
1199 struct client_dfs_referral *refs = NULL;
1200 size_t num_refs = 0;
1201 size_t consumed = 0;
1202 char *fullpath = NULL;
1204 struct smbXcli_tcon *orig_tcon = NULL;
1205 char *newextrapath = NULL;
1207 const char *remote_name;
1209 if (!cli || !sharename) {
1213 remote_name = smbXcli_conn_remote_name(cli->conn);
1215 /* special case. never check for a referral on the IPC$ share */
1217 if (strequal(sharename, "IPC$")) {
1221 /* send a trans2_query_path_info to check for a referral */
1223 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1228 /* Store tcon state. */
1229 if (cli_state_has_tcon(cli)) {
1230 orig_tcon = cli_state_save_tcon(cli);
1231 if (orig_tcon == NULL) {
1236 /* check for the referral */
1238 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1239 cli_state_restore_tcon(cli, orig_tcon);
1243 if (force_encrypt) {
1244 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1245 if (!NT_STATUS_IS_OK(status)) {
1246 cli_state_restore_tcon(cli, orig_tcon);
1251 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1252 &num_refs, &consumed);
1253 res = NT_STATUS_IS_OK(status);
1255 status = cli_tdis(cli);
1257 cli_state_restore_tcon(cli, orig_tcon);
1259 if (!NT_STATUS_IS_OK(status)) {
1263 if (!res || !num_refs) {
1267 if (!refs[0].dfspath) {
1271 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1272 pp_newshare, &newextrapath)) {
1276 /* check that this is not a self-referral */
1278 if (strequal(remote_name, *pp_newserver) &&
1279 strequal(sharename, *pp_newshare)) {