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"
30 #include "lib/param/param.h"
32 /********************************************************************
35 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
36 are not C escaped here).
38 - but if we're using POSIX paths then <pathname> may contain
39 '/' separators, not '\\' separators. So cope with '\\' or '/'
40 as a separator when looking at the pathname part.... JRA.
41 ********************************************************************/
43 /********************************************************************
44 Ensure a connection is encrypted.
45 ********************************************************************/
47 static NTSTATUS cli_cm_force_encryption_creds(struct cli_state *c,
48 struct cli_credentials *creds,
49 const char *sharename)
51 uint16_t major, minor;
52 uint32_t caplow, caphigh;
54 bool temp_ipc = false;
56 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
57 status = smb2cli_session_encryption_on(c->smb2.session);
58 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_SUPPORTED)) {
59 d_printf("Encryption required and "
60 "server doesn't support "
61 "SMB3 encryption - failing connect\n");
62 } else if (!NT_STATUS_IS_OK(status)) {
63 d_printf("Encryption required and "
64 "setup failed with error %s.\n",
70 if (!SERVER_HAS_UNIX_CIFS(c)) {
71 d_printf("Encryption required and "
72 "server that doesn't support "
73 "UNIX extensions - failing connect\n");
74 return NT_STATUS_NOT_SUPPORTED;
77 if (c->smb1.tcon == NULL) {
78 status = cli_tree_connect_creds(c, "IPC$", "IPC", creds);
79 if (!NT_STATUS_IS_OK(status)) {
80 d_printf("Encryption required and "
81 "can't connect to IPC$ to check "
82 "UNIX CIFS extensions.\n");
83 return NT_STATUS_UNKNOWN_REVISION;
88 status = cli_unix_extensions_version(c, &major, &minor, &caplow,
90 if (!NT_STATUS_IS_OK(status)) {
91 d_printf("Encryption required and "
92 "can't get UNIX CIFS extensions "
93 "version from server.\n");
97 return NT_STATUS_UNKNOWN_REVISION;
100 if (!(caplow & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)) {
101 d_printf("Encryption required and "
102 "share %s doesn't support "
103 "encryption.\n", sharename);
107 return NT_STATUS_UNSUPPORTED_COMPRESSION;
110 status = cli_smb1_setup_encryption(c, creds);
111 if (!NT_STATUS_IS_OK(status)) {
112 d_printf("Encryption required and "
113 "setup failed with error %s.\n",
127 /********************************************************************
128 Return a connection to a server.
129 ********************************************************************/
131 static NTSTATUS do_connect(TALLOC_CTX *ctx,
134 struct cli_credentials *creds,
135 const struct sockaddr_storage *dest_ss,
138 struct cli_state **pcli)
140 struct cli_state *c = NULL;
143 char *newserver, *newshare;
146 enum protocol_types protocol = PROTOCOL_NONE;
147 enum smb_signing_setting signing_state =
148 cli_credentials_get_smb_signing(creds);
149 enum smb_encryption_setting encryption_state =
150 cli_credentials_get_smb_encryption(creds);
152 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
153 signing_state = SMB_SIGNING_REQUIRED;
156 /* make a copy so we don't modify the global string 'service' */
157 servicename = talloc_strdup(ctx,share);
159 return NT_STATUS_NO_MEMORY;
161 sharename = servicename;
162 if (*sharename == '\\') {
164 if (server == NULL) {
167 sharename = strchr_m(sharename,'\\');
169 return NT_STATUS_NO_MEMORY;
174 if (server == NULL) {
175 return NT_STATUS_INVALID_PARAMETER;
178 status = cli_connect_nb(
179 server, dest_ss, port, name_type, NULL,
183 if (!NT_STATUS_IS_OK(status)) {
184 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
185 DBG_ERR("NetBIOS support disabled, unable to connect\n");
188 DBG_WARNING("Connection to %s failed (Error %s)\n",
194 DEBUG(4,(" session request ok\n"));
196 status = smbXcli_negprot(c->conn, c->timeout,
197 lp_client_min_protocol(),
198 lp_client_max_protocol());
200 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
201 d_printf("Protocol negotiation (with timeout %d ms) timed out against server %s\n",
203 smbXcli_conn_remote_name(c->conn));
205 } else if (!NT_STATUS_IS_OK(status)) {
206 d_printf("Protocol negotiation to server %s (for a protocol between %s and %s) failed: %s\n",
207 smbXcli_conn_remote_name(c->conn),
208 lpcfg_get_smb_protocol(lp_client_min_protocol()),
209 lpcfg_get_smb_protocol(lp_client_max_protocol()),
214 protocol = smbXcli_conn_protocol(c->conn);
215 DEBUG(4,(" negotiated dialect[%s] against server[%s]\n",
216 smb_protocol_types_string(protocol),
217 smbXcli_conn_remote_name(c->conn)));
219 if (protocol >= PROTOCOL_SMB2_02) {
220 /* Ensure we ask for some initial credits. */
221 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
224 status = cli_session_setup_creds(c, creds);
225 if (!NT_STATUS_IS_OK(status)) {
226 /* If a password was not supplied then
227 * try again with a null username. */
228 if (encryption_state == SMB_ENCRYPTION_REQUIRED ||
229 smbXcli_conn_signing_mandatory(c->conn) ||
230 cli_credentials_authentication_requested(creds) ||
231 cli_credentials_is_anonymous(creds) ||
232 !NT_STATUS_IS_OK(status = cli_session_setup_anon(c)))
234 d_printf("session setup failed: %s\n",
236 if (NT_STATUS_EQUAL(status,
237 NT_STATUS_MORE_PROCESSING_REQUIRED))
238 d_printf("did you forget to run kinit?\n");
242 d_printf("Anonymous login successful\n");
245 if (!NT_STATUS_IS_OK(status)) {
246 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
251 DEBUG(4,(" session setup ok\n"));
253 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
254 status = cli_cm_force_encryption_creds(c,
257 if (!NT_STATUS_IS_OK(status)) {
258 switch (encryption_state) {
259 case SMB_ENCRYPTION_DESIRED:
261 case SMB_ENCRYPTION_REQUIRED:
269 /* here's the fun part....to support 'msdfs proxy' shares
270 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
271 here before trying to connect to the original share.
272 cli_check_msdfs_proxy() will fail if it is a normal share. */
274 if (smbXcli_conn_dfs_supported(c->conn) &&
275 cli_check_msdfs_proxy(ctx, c, sharename,
276 &newserver, &newshare,
279 return do_connect(ctx, newserver,
281 NULL, port, name_type, pcli);
284 /* must be a normal share */
286 status = cli_tree_connect_creds(c, sharename, "?????", creds);
287 if (!NT_STATUS_IS_OK(status)) {
288 d_printf("tree connect failed: %s\n", nt_errstr(status));
293 DEBUG(4,(" tconx ok\n"));
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,
307 struct cli_credentials *creds,
308 const struct sockaddr_storage *dest_ss,
311 struct cli_state **pcli)
313 struct cli_state *cli = NULL;
316 status = do_connect(ctx, server, share,
318 dest_ss, port, name_type, &cli);
320 if (!NT_STATUS_IS_OK(status)) {
325 * This can't happen, this test is to satisfy static
329 return NT_STATUS_NO_MEMORY;
332 /* Enter into the list. */
334 DLIST_ADD_END(referring_cli, cli);
337 if (referring_cli && referring_cli->requested_posix_capabilities) {
338 uint16_t major, minor;
339 uint32_t caplow, caphigh;
340 status = cli_unix_extensions_version(cli, &major, &minor,
342 if (NT_STATUS_IS_OK(status)) {
343 cli_set_unix_extensions_capabilities(cli,
353 /********************************************************************
354 Return a connection to a server on a particular share.
355 ********************************************************************/
357 static struct cli_state *cli_cm_find(struct cli_state *cli,
367 /* Search to the start of the list. */
368 for (p = cli; p; p = DLIST_PREV(p)) {
369 const char *remote_name =
370 smbXcli_conn_remote_name(p->conn);
372 if (strequal(server, remote_name) &&
373 strequal(share,p->share)) {
378 /* Search to the end of the list. */
379 for (p = cli->next; p; p = p->next) {
380 const char *remote_name =
381 smbXcli_conn_remote_name(p->conn);
383 if (strequal(server, remote_name) &&
384 strequal(share,p->share)) {
392 /****************************************************************************
393 Open a client connection to a \\server\share.
394 ****************************************************************************/
396 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
397 struct cli_state *referring_cli,
400 struct cli_credentials *creds,
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);
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 client credentials\n",
421 return NT_STATUS_INVALID_PARAMETER;
424 status = cli_cm_connect(ctx,
433 if (!NT_STATUS_IS_OK(status)) {
440 /****************************************************************************
441 ****************************************************************************/
443 void cli_cm_display(struct cli_state *cli)
447 for (i=0; cli; cli = cli->next,i++ ) {
448 d_printf("%d:\tserver=%s, share=%s\n",
449 i, smbXcli_conn_remote_name(cli->conn), cli->share);
453 /**********************************************************************
454 split a dfs path into the server, share name, and extrapath components
455 **********************************************************************/
457 static bool split_dfs_path(TALLOC_CTX *ctx,
458 const char *nodepath,
468 *pp_extrapath = NULL;
470 path = talloc_strdup(ctx, nodepath);
475 if ( path[0] != '\\' ) {
479 p = strchr_m( path + 1, '\\' );
487 /* Look for any extra/deep path */
488 q = strchr_m(p, '\\');
492 *pp_extrapath = talloc_strdup(ctx, q);
494 *pp_extrapath = talloc_strdup(ctx, "");
496 if (*pp_extrapath == NULL) {
500 *pp_share = talloc_strdup(ctx, p);
501 if (*pp_share == NULL) {
505 *pp_server = talloc_strdup(ctx, &path[1]);
506 if (*pp_server == NULL) {
514 TALLOC_FREE(*pp_share);
515 TALLOC_FREE(*pp_extrapath);
520 /****************************************************************************
521 Return the original path truncated at the directory component before
522 the first wildcard character. Trust the caller to provide a NULL
524 ****************************************************************************/
526 static char *clean_path(TALLOC_CTX *ctx, const char *path)
532 /* No absolute paths. */
533 while (IS_DIRECTORY_SEP(*path)) {
537 path_out = talloc_strdup(ctx, path);
542 p1 = strchr_m(path_out, '*');
543 p2 = strchr_m(path_out, '?');
555 /* Now go back to the start of this component. */
556 p1 = strrchr_m(path_out, '/');
557 p2 = strrchr_m(path_out, '\\');
564 /* Strip any trailing separator */
566 len = strlen(path_out);
567 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
568 path_out[len-1] = '\0';
574 /****************************************************************************
575 ****************************************************************************/
577 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
578 struct cli_state *cli,
581 char path_sep = '\\';
583 /* Ensure the extrapath doesn't start with a separator. */
584 while (IS_DIRECTORY_SEP(*dir)) {
588 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
591 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
593 smbXcli_conn_remote_name(cli->conn),
600 /********************************************************************
601 Check if a path has already been converted to DFS.
602 ********************************************************************/
604 bool cli_dfs_is_already_full_path(struct cli_state *cli, const char *path)
606 const char *server = smbXcli_conn_remote_name(cli->conn);
607 size_t server_len = strlen(server);
608 bool found_server = false;
609 const char *share = cli->share;
610 size_t share_len = strlen(share);
611 bool found_share = false;
613 if (!IS_DIRECTORY_SEP(path[0])) {
617 found_server = (strncasecmp_m(path, server, server_len) == 0);
622 if (!IS_DIRECTORY_SEP(path[0])) {
626 found_share = (strncasecmp_m(path, share, share_len) == 0);
631 if (path[0] == '\0') {
634 if (IS_DIRECTORY_SEP(path[0])) {
640 /********************************************************************
641 Get the dfs referral link.
642 ********************************************************************/
644 NTSTATUS cli_dfs_get_referral_ex(TALLOC_CTX *ctx,
645 struct cli_state *cli,
647 uint16_t max_referral_level,
648 struct client_dfs_referral **refs,
652 unsigned int param_len = 0;
653 uint16_t recv_flags2;
654 uint8_t *param = NULL;
655 uint8_t *rdata = NULL;
658 smb_ucs2_t *path_ucs;
659 char *consumed_path = NULL;
660 uint16_t consumed_ucs;
661 uint16_t num_referrals;
662 struct client_dfs_referral *referrals = NULL;
664 TALLOC_CTX *frame = talloc_stackframe();
669 param = talloc_array(talloc_tos(), uint8_t, 2);
671 status = NT_STATUS_NO_MEMORY;
674 SSVAL(param, 0, max_referral_level);
676 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
677 path, strlen(path)+1,
680 status = NT_STATUS_NO_MEMORY;
683 param_len = talloc_get_size(param);
684 path_ucs = (smb_ucs2_t *)¶m[2];
686 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
687 DATA_BLOB in_input_buffer;
688 DATA_BLOB in_output_buffer = data_blob_null;
689 DATA_BLOB out_input_buffer = data_blob_null;
690 DATA_BLOB out_output_buffer = data_blob_null;
692 in_input_buffer.data = param;
693 in_input_buffer.length = param_len;
695 status = smb2cli_ioctl(cli->conn,
699 UINT64_MAX, /* in_fid_persistent */
700 UINT64_MAX, /* in_fid_volatile */
701 FSCTL_DFS_GET_REFERRALS,
702 0, /* in_max_input_length */
704 CLI_BUFFER_SIZE, /* in_max_output_length */
706 SMB2_IOCTL_FLAG_IS_FSCTL,
710 if (!NT_STATUS_IS_OK(status)) {
714 if (out_output_buffer.length < 4) {
715 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
719 recv_flags2 = FLAGS2_UNICODE_STRINGS;
720 rdata = out_output_buffer.data;
721 endp = (char *)rdata + out_output_buffer.length;
723 unsigned int data_len = 0;
726 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
728 status = cli_trans(talloc_tos(), cli, SMBtrans2,
732 NULL, 0, CLI_BUFFER_SIZE,
734 NULL, 0, NULL, /* rsetup */
736 &rdata, 4, &data_len);
737 if (!NT_STATUS_IS_OK(status)) {
741 endp = (char *)rdata + data_len;
744 consumed_ucs = SVAL(rdata, 0);
745 num_referrals = SVAL(rdata, 2);
747 /* consumed_ucs is the number of bytes
748 * of the UCS2 path consumed not counting any
749 * terminating null. We need to convert
750 * back to unix charset and count again
751 * to get the number of bytes consumed from
752 * the incoming path. */
755 if (pull_string_talloc(talloc_tos(),
763 status = map_nt_error_from_unix(errno);
765 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
769 if (consumed_path == NULL) {
770 status = map_nt_error_from_unix(errno);
773 *consumed = strlen(consumed_path);
775 if (num_referrals != 0) {
776 uint16_t ref_version;
779 uint16_t node_offset;
781 referrals = talloc_array(ctx, struct client_dfs_referral,
785 status = NT_STATUS_NO_MEMORY;
788 /* start at the referrals array */
791 for (i=0; i<num_referrals && p < endp; i++) {
795 ref_version = SVAL(p, 0);
796 ref_size = SVAL(p, 2);
797 node_offset = SVAL(p, 16);
799 if (ref_version != 3) {
804 referrals[i].proximity = SVAL(p, 8);
805 referrals[i].ttl = SVAL(p, 10);
807 if (p + node_offset > endp) {
808 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
811 pull_string_talloc(referrals,
814 &referrals[i].dfspath,
816 PTR_DIFF(endp, p+node_offset),
817 STR_TERMINATE|STR_UNICODE);
819 if (!referrals[i].dfspath) {
820 status = map_nt_error_from_unix(errno);
825 if (i < num_referrals) {
826 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
831 *num_refs = num_referrals;
840 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
841 struct cli_state *cli,
843 struct client_dfs_referral **refs,
847 return cli_dfs_get_referral_ex(ctx,
851 refs, /* Max referral level we want */
856 static bool cli_conn_have_dfs(struct cli_state *cli)
858 struct smbXcli_conn *conn = cli->conn;
859 struct smbXcli_tcon *tcon = NULL;
862 if (smbXcli_conn_protocol(conn) < PROTOCOL_SMB2_02) {
863 uint32_t capabilities = smb1cli_conn_capabilities(conn);
865 if ((capabilities & CAP_STATUS32) == 0) {
868 if ((capabilities & CAP_UNICODE) == 0) {
872 tcon = cli->smb1.tcon;
874 tcon = cli->smb2.tcon;
877 ok = smbXcli_tcon_is_dfs_share(tcon);
881 /********************************************************************
882 ********************************************************************/
883 struct cli_dfs_path_split {
889 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
891 struct cli_credentials *creds,
892 struct cli_state *rootcli,
894 struct cli_state **targetcli,
895 char **pp_targetpath)
897 struct client_dfs_referral *refs = NULL;
900 struct cli_state *cli_ipc = NULL;
901 char *dfs_path = NULL;
902 char *cleanpath = NULL;
903 char *extrapath = NULL;
905 struct cli_state *newcli = NULL;
906 struct cli_state *ccli = NULL;
908 char *newpath = NULL;
909 char *newmount = NULL;
911 SMB_STRUCT_STAT sbuf;
914 struct smbXcli_tcon *target_tcon = NULL;
915 struct cli_dfs_path_split *dfs_refs = NULL;
917 bool is_already_dfs = false;
919 if ( !rootcli || !path || !targetcli ) {
920 return NT_STATUS_INVALID_PARAMETER;
924 * Avoid more than one leading directory separator
926 while (IS_DIRECTORY_SEP(path[0]) && IS_DIRECTORY_SEP(path[1])) {
930 ok = cli_conn_have_dfs(rootcli);
932 *targetcli = rootcli;
933 *pp_targetpath = talloc_strdup(ctx, path);
934 if (!*pp_targetpath) {
935 return NT_STATUS_NO_MEMORY;
942 is_already_dfs = cli_dfs_is_already_full_path(rootcli, path);
943 if (is_already_dfs) {
944 const char *localpath = NULL;
946 * Given path is already converted to DFS.
947 * Convert to a local path so clean_path()
948 * can correctly strip any wildcards.
950 status = cli_dfs_target_check(ctx,
954 if (!NT_STATUS_IS_OK(status)) {
960 /* Send a trans2_query_path_info to check for a referral. */
962 cleanpath = clean_path(ctx, path);
964 return NT_STATUS_NO_MEMORY;
967 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
969 return NT_STATUS_NO_MEMORY;
972 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
973 if (NT_STATUS_IS_OK(status)) {
974 /* This is an ordinary path, just return it. */
975 *targetcli = rootcli;
976 *pp_targetpath = talloc_strdup(ctx, path);
977 if (!*pp_targetpath) {
978 return NT_STATUS_NO_MEMORY;
983 /* Special case where client asked for a path that does not exist */
985 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
986 *targetcli = rootcli;
987 *pp_targetpath = talloc_strdup(ctx, path);
988 if (!*pp_targetpath) {
989 return NT_STATUS_NO_MEMORY;
994 /* We got an error, check for DFS referral. */
996 if (!NT_STATUS_EQUAL(status, NT_STATUS_PATH_NOT_COVERED)) {
1000 /* Check for the referral. */
1002 status = cli_cm_open(ctx,
1004 smbXcli_conn_remote_name(rootcli->conn),
1007 NULL, /* dest_ss not needed, we reuse the transport */
1011 if (!NT_STATUS_IS_OK(status)) {
1015 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
1016 &num_refs, &consumed);
1017 if (!NT_STATUS_IS_OK(status)) {
1021 if (!num_refs || !refs[0].dfspath) {
1022 return NT_STATUS_NOT_FOUND;
1026 * Bug#10123 - DFS referral entries can be provided in a random order,
1027 * so check the connection cache for each item to avoid unnecessary
1030 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
1031 if (dfs_refs == NULL) {
1032 return NT_STATUS_NO_MEMORY;
1035 for (count = 0; count < num_refs; count++) {
1036 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
1037 &dfs_refs[count].server,
1038 &dfs_refs[count].share,
1039 &dfs_refs[count].extrapath)) {
1040 TALLOC_FREE(dfs_refs);
1041 return NT_STATUS_NOT_FOUND;
1044 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
1045 dfs_refs[count].share);
1047 extrapath = dfs_refs[count].extrapath;
1054 * If no cached connection was found, then connect to the first live
1055 * referral server in the list.
1057 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1058 /* Connect to the target server & share */
1059 status = cli_cm_connect(ctx, rootcli,
1060 dfs_refs[count].server,
1061 dfs_refs[count].share,
1067 if (!NT_STATUS_IS_OK(status)) {
1068 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1069 dfs_refs[count].server,
1070 dfs_refs[count].share);
1073 extrapath = dfs_refs[count].extrapath;
1078 /* No available referral server for the connection */
1079 if (*targetcli == NULL) {
1080 TALLOC_FREE(dfs_refs);
1084 /* Make sure to recreate the original string including any wildcards. */
1086 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1088 TALLOC_FREE(dfs_refs);
1089 return NT_STATUS_NO_MEMORY;
1091 pathlen = strlen(dfs_path);
1092 consumed = MIN(pathlen, consumed);
1093 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1094 if (!*pp_targetpath) {
1095 TALLOC_FREE(dfs_refs);
1096 return NT_STATUS_NO_MEMORY;
1098 dfs_path[consumed] = '\0';
1101 * *pp_targetpath is now the unconsumed part of the path.
1102 * dfs_path is now the consumed part of the path
1103 * (in \server\share\path format).
1106 if (extrapath && strlen(extrapath) > 0) {
1107 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1108 /* put the trailing \ on the path, so to be safe we put one in if needed */
1109 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1110 *pp_targetpath = talloc_asprintf(ctx,
1115 *pp_targetpath = talloc_asprintf(ctx,
1120 if (!*pp_targetpath) {
1121 TALLOC_FREE(dfs_refs);
1122 return NT_STATUS_NO_MEMORY;
1126 /* parse out the consumed mount path */
1127 /* trim off the \server\share\ */
1131 if (*ppath != '\\') {
1132 d_printf("cli_resolve_path: "
1133 "dfs_path (%s) not in correct format.\n",
1135 TALLOC_FREE(dfs_refs);
1136 return NT_STATUS_NOT_FOUND;
1139 ppath++; /* Now pointing at start of server name. */
1141 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1142 TALLOC_FREE(dfs_refs);
1143 return NT_STATUS_NOT_FOUND;
1146 ppath++; /* Now pointing at start of share name. */
1148 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1149 TALLOC_FREE(dfs_refs);
1150 return NT_STATUS_NOT_FOUND;
1153 ppath++; /* Now pointing at path component. */
1155 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1157 TALLOC_FREE(dfs_refs);
1158 return NT_STATUS_NOT_FOUND;
1161 /* Check for another dfs referral, note that we are not
1162 checking for loops here. */
1164 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1165 status = cli_resolve_path(ctx,
1172 if (NT_STATUS_IS_OK(status)) {
1174 * When cli_resolve_path returns true here it's always
1175 * returning the complete path in newpath, so we're done
1178 *targetcli = newcli;
1179 *pp_targetpath = newpath;
1180 TALLOC_FREE(dfs_refs);
1187 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1188 target_tcon = (*targetcli)->smb2.tcon;
1190 target_tcon = (*targetcli)->smb1.tcon;
1193 /* If returning true ensure we return a dfs root full path. */
1194 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1195 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1197 TALLOC_FREE(dfs_refs);
1198 return NT_STATUS_NO_MEMORY;
1200 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1201 if (*pp_targetpath == NULL) {
1202 TALLOC_FREE(dfs_refs);
1203 return NT_STATUS_NO_MEMORY;
1207 TALLOC_FREE(dfs_refs);
1208 return NT_STATUS_OK;
1211 /********************************************************************
1212 ********************************************************************/
1214 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1215 struct cli_state *cli,
1216 const char *sharename,
1217 char **pp_newserver,
1219 struct cli_credentials *creds)
1221 struct client_dfs_referral *refs = NULL;
1222 size_t num_refs = 0;
1223 size_t consumed = 0;
1224 char *fullpath = NULL;
1226 struct smbXcli_tcon *orig_tcon = NULL;
1227 char *orig_share = NULL;
1228 char *newextrapath = NULL;
1230 const char *remote_name;
1231 enum smb_encryption_setting encryption_state =
1232 cli_credentials_get_smb_encryption(creds);
1234 if (!cli || !sharename) {
1238 remote_name = smbXcli_conn_remote_name(cli->conn);
1240 /* special case. never check for a referral on the IPC$ share */
1242 if (strequal(sharename, "IPC$")) {
1246 /* send a trans2_query_path_info to check for a referral */
1248 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1253 /* Store tcon state. */
1254 if (cli_state_has_tcon(cli)) {
1255 cli_state_save_tcon_share(cli, &orig_tcon, &orig_share);
1258 /* check for the referral */
1260 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL))) {
1261 cli_state_restore_tcon_share(cli, orig_tcon, orig_share);
1265 if (encryption_state >= SMB_ENCRYPTION_DESIRED) {
1266 status = cli_cm_force_encryption_creds(cli, creds, "IPC$");
1267 if (!NT_STATUS_IS_OK(status)) {
1268 switch (encryption_state) {
1269 case SMB_ENCRYPTION_DESIRED:
1271 case SMB_ENCRYPTION_REQUIRED:
1274 * Failed to set up encryption.
1275 * Disconnect the temporary IPC$
1276 * tcon before restoring the original
1277 * tcon so we don't leak it.
1280 cli_state_restore_tcon_share(cli,
1288 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1289 &num_refs, &consumed);
1290 res = NT_STATUS_IS_OK(status);
1292 status = cli_tdis(cli);
1294 cli_state_restore_tcon_share(cli, orig_tcon, orig_share);
1296 if (!NT_STATUS_IS_OK(status)) {
1300 if (!res || !num_refs) {
1304 if (!refs[0].dfspath) {
1308 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1309 pp_newshare, &newextrapath)) {
1313 /* check that this is not a self-referral */
1315 if (strequal(remote_name, *pp_newserver) &&
1316 strequal(sharename, *pp_newshare)) {
1323 /********************************************************************
1324 Windows and NetApp (and arguably the SMB1/2/3 specs) expect a non-DFS
1325 path for the targets of rename and hardlink. If we have been given
1326 a DFS path for these calls, convert it back into a local path by
1327 stripping off the DFS prefix.
1328 ********************************************************************/
1330 NTSTATUS cli_dfs_target_check(TALLOC_CTX *mem_ctx,
1331 struct cli_state *cli,
1332 const char *fname_dst,
1333 const char **fname_dst_out)
1335 char *dfs_prefix = NULL;
1336 size_t prefix_len = 0;
1337 struct smbXcli_tcon *tcon = NULL;
1339 if (!smbXcli_conn_dfs_supported(cli->conn)) {
1340 goto copy_fname_out;
1342 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
1343 tcon = cli->smb2.tcon;
1345 tcon = cli->smb1.tcon;
1347 if (!smbXcli_tcon_is_dfs_share(tcon)) {
1348 goto copy_fname_out;
1350 dfs_prefix = cli_dfs_make_full_path(mem_ctx, cli, "");
1351 if (dfs_prefix == NULL) {
1352 return NT_STATUS_NO_MEMORY;
1354 prefix_len = strlen(dfs_prefix);
1355 if (strncmp(fname_dst, dfs_prefix, prefix_len) != 0) {
1357 * Prefix doesn't match. Assume it was
1358 * already stripped or not added in the
1361 goto copy_fname_out;
1363 /* Return the trailing name after the prefix. */
1364 *fname_dst_out = &fname_dst[prefix_len];
1365 TALLOC_FREE(dfs_prefix);
1366 return NT_STATUS_OK;
1371 * No change to the destination name. Just
1372 * point it at the incoming destination name.
1374 *fname_dst_out = fname_dst;
1375 TALLOC_FREE(dfs_prefix);
1376 return NT_STATUS_OK;
1379 /********************************************************************
1380 Convert a pathname into a DFS path if it hasn't already been converted.
1381 Always returns a talloc'ed path, makes it easy to pass const paths in.
1382 ********************************************************************/
1384 char *smb1_dfs_share_path(TALLOC_CTX *ctx,
1385 struct cli_state *cli,
1388 bool is_dfs = smbXcli_conn_dfs_supported(cli->conn) &&
1389 smbXcli_tcon_is_dfs_share(cli->smb1.tcon);
1390 bool is_already_dfs_path = false;
1391 bool posix = (cli->requested_posix_capabilities &
1392 CIFS_UNIX_POSIX_PATHNAMES_CAP);
1393 char sepchar = (posix ? '/' : '\\');
1396 return talloc_strdup(ctx, path);
1398 is_already_dfs_path = cli_dfs_is_already_full_path(cli, path);
1399 if (is_already_dfs_path) {
1400 return talloc_strdup(ctx, path);
1403 * We don't use cli_dfs_make_full_path() as,
1404 * when given a null path, cli_dfs_make_full_path
1405 * deliberately adds a trailing '\\' (this is by
1406 * design to check for an existing DFS prefix match).
1408 if (path[0] == '\0') {
1409 return talloc_asprintf(ctx,
1412 smbXcli_conn_remote_name(cli->conn),
1416 while (*path == sepchar) {
1419 return talloc_asprintf(ctx,
1422 smbXcli_conn_remote_name(cli->conn),