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"
30 /********************************************************************
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,
49 const char *sharename)
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",
67 status = cli_force_encryption(c,
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",
93 /********************************************************************
94 Return a connection to a server.
95 ********************************************************************/
97 static NTSTATUS do_connect(TALLOC_CTX *ctx,
100 const struct user_auth_info *auth_info,
106 struct cli_state **pcli)
108 struct cli_state *c = NULL;
111 char *newserver, *newshare;
112 const char *username;
113 const char *password;
117 int signing_state = get_cmdline_auth_info_signing_state(auth_info);
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);
126 return NT_STATUS_NO_MEMORY;
128 sharename = servicename;
129 if (*sharename == '\\') {
131 if (server == NULL) {
134 sharename = strchr_m(sharename,'\\');
136 return NT_STATUS_NO_MEMORY;
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,
163 if (!NT_STATUS_IS_OK(status)) {
164 d_printf("Connection to %s failed (Error %s)\n",
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_cli_minprotocol(),
179 if (!NT_STATUS_IS_OK(status)) {
180 d_printf("protocol negotiation failed: %s\n",
186 if (smbXcli_conn_protocol(c->conn) >= PROTOCOL_SMB2_02) {
187 /* Ensure we ask for some initial credits. */
188 smb2cli_conn_set_max_credits(c->conn, DEFAULT_SMB2_MAX_CREDITS);
191 username = get_cmdline_auth_info_username(auth_info);
192 password = get_cmdline_auth_info_password(auth_info);
193 domain = get_cmdline_auth_info_domain(auth_info);
194 if ((domain == NULL) || (domain[0] == '\0')) {
195 domain = lp_workgroup();
198 status = cli_session_setup(c, username,
199 password, strlen(password),
200 password, strlen(password),
202 if (!NT_STATUS_IS_OK(status)) {
203 /* If a password was not supplied then
204 * try again with a null username. */
205 if (password[0] || !username[0] ||
206 get_cmdline_auth_info_use_kerberos(auth_info) ||
207 !NT_STATUS_IS_OK(status = cli_session_setup(c, "",
211 d_printf("session setup failed: %s\n",
213 if (NT_STATUS_EQUAL(status,
214 NT_STATUS_MORE_PROCESSING_REQUIRED))
215 d_printf("did you forget to run kinit?\n");
219 d_printf("Anonymous login successful\n");
220 status = cli_init_creds(c, "", lp_workgroup(), "");
222 status = cli_init_creds(c, username, domain, password);
225 if (!NT_STATUS_IS_OK(status)) {
226 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status)));
231 if ( show_sessetup ) {
232 if (*c->server_domain) {
233 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
234 c->server_domain,c->server_os,c->server_type));
235 } else if (*c->server_os || *c->server_type) {
236 DEBUG(0,("OS=[%s] Server=[%s]\n",
237 c->server_os,c->server_type));
240 DEBUG(4,(" session setup ok\n"));
242 /* here's the fun part....to support 'msdfs proxy' shares
243 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
244 here before trying to connect to the original share.
245 cli_check_msdfs_proxy() will fail if it is a normal share. */
247 if (smbXcli_conn_dfs_supported(c->conn) &&
248 cli_check_msdfs_proxy(ctx, c, sharename,
249 &newserver, &newshare,
255 return do_connect(ctx, newserver,
256 newshare, auth_info, false,
257 force_encrypt, max_protocol,
258 port, name_type, pcli);
261 /* must be a normal share */
263 status = cli_tree_connect(c, sharename, "?????",
264 password, strlen(password)+1);
265 if (!NT_STATUS_IS_OK(status)) {
266 d_printf("tree connect failed: %s\n", nt_errstr(status));
272 status = cli_cm_force_encryption(c,
277 if (!NT_STATUS_IS_OK(status)) {
283 DEBUG(4,(" tconx ok\n"));
288 /****************************************************************************
289 ****************************************************************************/
291 static void cli_set_mntpoint(struct cli_state *cli, const char *mnt)
293 TALLOC_CTX *frame = talloc_stackframe();
294 char *name = clean_name(frame, mnt);
299 TALLOC_FREE(cli->dfs_mountpoint);
300 cli->dfs_mountpoint = talloc_strdup(cli, name);
304 /********************************************************************
305 Add a new connection to the list.
306 referring_cli == NULL means a new initial connection.
307 ********************************************************************/
309 static NTSTATUS cli_cm_connect(TALLOC_CTX *ctx,
310 struct cli_state *referring_cli,
313 const struct user_auth_info *auth_info,
319 struct cli_state **pcli)
321 struct cli_state *cli;
324 status = do_connect(ctx, server, share,
326 show_hdr, force_encrypt, max_protocol,
327 port, name_type, &cli);
329 if (!NT_STATUS_IS_OK(status)) {
333 /* Enter into the list. */
335 DLIST_ADD_END(referring_cli, cli, struct cli_state *);
338 if (referring_cli && referring_cli->requested_posix_capabilities) {
340 uint32 caplow, caphigh;
341 status = cli_unix_extensions_version(cli, &major, &minor,
343 if (NT_STATUS_IS_OK(status)) {
344 cli_set_unix_extensions_capabilities(cli,
354 /********************************************************************
355 Return a connection to a server on a particular share.
356 ********************************************************************/
358 static struct cli_state *cli_cm_find(struct cli_state *cli,
368 /* Search to the start of the list. */
369 for (p = cli; p; p = DLIST_PREV(p)) {
370 const char *remote_name =
371 smbXcli_conn_remote_name(p->conn);
373 if (strequal(server, remote_name) &&
374 strequal(share,p->share)) {
379 /* Search to the end of the list. */
380 for (p = cli->next; p; p = p->next) {
381 const char *remote_name =
382 smbXcli_conn_remote_name(p->conn);
384 if (strequal(server, remote_name) &&
385 strequal(share,p->share)) {
393 /****************************************************************************
394 Open a client connection to a \\server\share.
395 ****************************************************************************/
397 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
398 struct cli_state *referring_cli,
401 const struct user_auth_info *auth_info,
407 struct cli_state **pcli)
409 /* Try to reuse an existing connection in this list. */
410 struct cli_state *c = cli_cm_find(referring_cli, server, share);
418 if (auth_info == NULL) {
419 /* Can't do a new connection
420 * without auth info. */
421 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
422 "without auth info\n",
424 return NT_STATUS_INVALID_PARAMETER;
427 status = cli_cm_connect(ctx,
438 if (!NT_STATUS_IS_OK(status)) {
445 /****************************************************************************
446 ****************************************************************************/
448 void cli_cm_display(struct cli_state *cli)
452 for (i=0; cli; cli = cli->next,i++ ) {
453 d_printf("%d:\tserver=%s, share=%s\n",
454 i, smbXcli_conn_remote_name(cli->conn), cli->share);
458 /****************************************************************************
459 ****************************************************************************/
461 /****************************************************************************
462 ****************************************************************************/
465 void cli_cm_set_credentials(struct user_auth_info *auth_info)
467 SAFE_FREE(cm_creds.username);
468 cm_creds.username = SMB_STRDUP(get_cmdline_auth_info_username(
471 if (get_cmdline_auth_info_got_pass(auth_info)) {
472 cm_set_password(get_cmdline_auth_info_password(auth_info));
475 cm_creds.use_kerberos = get_cmdline_auth_info_use_kerberos(auth_info);
476 cm_creds.fallback_after_kerberos = false;
477 cm_creds.signing_state = get_cmdline_auth_info_signing_state(auth_info);
481 /**********************************************************************
482 split a dfs path into the server, share name, and extrapath components
483 **********************************************************************/
485 static bool split_dfs_path(TALLOC_CTX *ctx,
486 const char *nodepath,
496 *pp_extrapath = NULL;
498 path = talloc_strdup(ctx, nodepath);
503 if ( path[0] != '\\' ) {
507 p = strchr_m( path + 1, '\\' );
515 /* Look for any extra/deep path */
516 q = strchr_m(p, '\\');
520 *pp_extrapath = talloc_strdup(ctx, q);
522 *pp_extrapath = talloc_strdup(ctx, "");
524 if (*pp_extrapath == NULL) {
528 *pp_share = talloc_strdup(ctx, p);
529 if (*pp_share == NULL) {
533 *pp_server = talloc_strdup(ctx, &path[1]);
534 if (*pp_server == NULL) {
542 TALLOC_FREE(*pp_share);
543 TALLOC_FREE(*pp_extrapath);
548 /****************************************************************************
549 Return the original path truncated at the directory component before
550 the first wildcard character. Trust the caller to provide a NULL
552 ****************************************************************************/
554 static char *clean_path(TALLOC_CTX *ctx, const char *path)
560 /* No absolute paths. */
561 while (IS_DIRECTORY_SEP(*path)) {
565 path_out = talloc_strdup(ctx, path);
570 p1 = strchr_m(path_out, '*');
571 p2 = strchr_m(path_out, '?');
583 /* Now go back to the start of this component. */
584 p1 = strrchr_m(path_out, '/');
585 p2 = strrchr_m(path_out, '\\');
592 /* Strip any trailing separator */
594 len = strlen(path_out);
595 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
596 path_out[len-1] = '\0';
602 /****************************************************************************
603 ****************************************************************************/
605 static char *cli_dfs_make_full_path(TALLOC_CTX *ctx,
606 struct cli_state *cli,
609 char path_sep = '\\';
611 /* Ensure the extrapath doesn't start with a separator. */
612 while (IS_DIRECTORY_SEP(*dir)) {
616 if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
619 return talloc_asprintf(ctx, "%c%s%c%s%c%s",
621 smbXcli_conn_remote_name(cli->conn),
628 /********************************************************************
629 check for dfs referral
630 ********************************************************************/
632 static bool cli_dfs_check_error(struct cli_state *cli, NTSTATUS expected,
635 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
637 if (!(smbXcli_conn_use_unicode(cli->conn))) {
640 if (!(smb1cli_conn_capabilities(cli->conn) & CAP_STATUS32)) {
643 if (NT_STATUS_EQUAL(status, expected)) {
649 /********************************************************************
650 Get the dfs referral link.
651 ********************************************************************/
653 NTSTATUS cli_dfs_get_referral(TALLOC_CTX *ctx,
654 struct cli_state *cli,
656 struct client_dfs_referral **refs,
660 unsigned int param_len = 0;
661 uint16_t recv_flags2;
662 uint8_t *param = NULL;
663 uint8_t *rdata = NULL;
666 smb_ucs2_t *path_ucs;
667 char *consumed_path = NULL;
668 uint16_t consumed_ucs;
669 uint16 num_referrals;
670 struct client_dfs_referral *referrals = NULL;
672 TALLOC_CTX *frame = talloc_stackframe();
677 param = talloc_array(talloc_tos(), uint8_t, 2);
679 status = NT_STATUS_NO_MEMORY;
682 SSVAL(param, 0, 0x03); /* max referral level */
684 param = trans2_bytes_push_str(param, smbXcli_conn_use_unicode(cli->conn),
685 path, strlen(path)+1,
688 status = NT_STATUS_NO_MEMORY;
691 param_len = talloc_get_size(param);
692 path_ucs = (smb_ucs2_t *)¶m[2];
694 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
695 DATA_BLOB in_input_buffer;
696 DATA_BLOB in_output_buffer = data_blob_null;
697 DATA_BLOB out_input_buffer = data_blob_null;
698 DATA_BLOB out_output_buffer = data_blob_null;
700 in_input_buffer.data = param;
701 in_input_buffer.length = param_len;
703 status = smb2cli_ioctl(cli->conn,
707 UINT64_MAX, /* in_fid_persistent */
708 UINT64_MAX, /* in_fid_volatile */
709 FSCTL_DFS_GET_REFERRALS,
710 0, /* in_max_input_length */
712 CLI_BUFFER_SIZE, /* in_max_output_length */
714 SMB2_IOCTL_FLAG_IS_FSCTL,
718 if (!NT_STATUS_IS_OK(status)) {
722 if (out_output_buffer.length < 4) {
723 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
727 recv_flags2 = FLAGS2_UNICODE_STRINGS;
728 rdata = out_output_buffer.data;
729 endp = (char *)rdata + out_output_buffer.length;
731 unsigned int data_len = 0;
734 SSVAL(setup, 0, TRANSACT2_GET_DFS_REFERRAL);
736 status = cli_trans(talloc_tos(), cli, SMBtrans2,
740 NULL, 0, CLI_BUFFER_SIZE,
742 NULL, 0, NULL, /* rsetup */
744 &rdata, 4, &data_len);
745 if (!NT_STATUS_IS_OK(status)) {
749 endp = (char *)rdata + data_len;
752 consumed_ucs = SVAL(rdata, 0);
753 num_referrals = SVAL(rdata, 2);
755 /* consumed_ucs is the number of bytes
756 * of the UCS2 path consumed not counting any
757 * terminating null. We need to convert
758 * back to unix charset and count again
759 * to get the number of bytes consumed from
760 * the incoming path. */
763 if (pull_string_talloc(talloc_tos(),
771 status = map_nt_error_from_unix(errno);
773 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
777 if (consumed_path == NULL) {
778 status = map_nt_error_from_unix(errno);
781 *consumed = strlen(consumed_path);
783 if (num_referrals != 0) {
789 referrals = talloc_array(ctx, struct client_dfs_referral,
793 status = NT_STATUS_NO_MEMORY;
796 /* start at the referrals array */
799 for (i=0; i<num_referrals && p < endp; i++) {
803 ref_version = SVAL(p, 0);
804 ref_size = SVAL(p, 2);
805 node_offset = SVAL(p, 16);
807 if (ref_version != 3) {
812 referrals[i].proximity = SVAL(p, 8);
813 referrals[i].ttl = SVAL(p, 10);
815 if (p + node_offset > endp) {
816 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
819 clistr_pull_talloc(referrals,
822 &referrals[i].dfspath,
824 PTR_DIFF(endp, p+node_offset),
825 STR_TERMINATE|STR_UNICODE);
827 if (!referrals[i].dfspath) {
828 status = map_nt_error_from_unix(errno);
833 if (i < num_referrals) {
834 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
839 *num_refs = num_referrals;
848 /********************************************************************
849 ********************************************************************/
850 struct cli_dfs_path_split {
856 NTSTATUS cli_resolve_path(TALLOC_CTX *ctx,
858 const struct user_auth_info *dfs_auth_info,
859 struct cli_state *rootcli,
861 struct cli_state **targetcli,
862 char **pp_targetpath)
864 struct client_dfs_referral *refs = NULL;
867 struct cli_state *cli_ipc = NULL;
868 char *dfs_path = NULL;
869 char *cleanpath = NULL;
870 char *extrapath = NULL;
872 struct cli_state *newcli = NULL;
873 struct cli_state *ccli = NULL;
875 char *newpath = NULL;
876 char *newmount = NULL;
878 SMB_STRUCT_STAT sbuf;
881 struct smbXcli_tcon *root_tcon = NULL;
882 struct smbXcli_tcon *target_tcon = NULL;
883 struct cli_dfs_path_split *dfs_refs = NULL;
885 if ( !rootcli || !path || !targetcli ) {
886 return NT_STATUS_INVALID_PARAMETER;
889 /* Don't do anything if this is not a DFS root. */
891 if (smbXcli_conn_protocol(rootcli->conn) >= PROTOCOL_SMB2_02) {
892 root_tcon = rootcli->smb2.tcon;
894 root_tcon = rootcli->smb1.tcon;
897 if (!smbXcli_tcon_is_dfs_share(root_tcon)) {
898 *targetcli = rootcli;
899 *pp_targetpath = talloc_strdup(ctx, path);
900 if (!*pp_targetpath) {
901 return NT_STATUS_NO_MEMORY;
908 /* Send a trans2_query_path_info to check for a referral. */
910 cleanpath = clean_path(ctx, path);
912 return NT_STATUS_NO_MEMORY;
915 dfs_path = cli_dfs_make_full_path(ctx, rootcli, cleanpath);
917 return NT_STATUS_NO_MEMORY;
920 status = cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes);
921 if (NT_STATUS_IS_OK(status)) {
922 /* This is an ordinary path, just return it. */
923 *targetcli = rootcli;
924 *pp_targetpath = talloc_strdup(ctx, path);
925 if (!*pp_targetpath) {
926 return NT_STATUS_NO_MEMORY;
931 /* Special case where client asked for a path that does not exist */
933 if (cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND,
935 *targetcli = rootcli;
936 *pp_targetpath = talloc_strdup(ctx, path);
937 if (!*pp_targetpath) {
938 return NT_STATUS_NO_MEMORY;
943 /* We got an error, check for DFS referral. */
945 if (!cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED,
950 /* Check for the referral. */
952 status = cli_cm_open(ctx,
954 smbXcli_conn_remote_name(rootcli->conn),
958 smb1cli_conn_encryption_on(rootcli->conn),
959 smbXcli_conn_protocol(rootcli->conn),
963 if (!NT_STATUS_IS_OK(status)) {
967 status = cli_dfs_get_referral(ctx, cli_ipc, dfs_path, &refs,
968 &num_refs, &consumed);
969 if (!NT_STATUS_IS_OK(status) || !num_refs) {
973 if (!refs[0].dfspath) {
974 return NT_STATUS_NOT_FOUND;
978 * Bug#10123 - DFS referal entries can be provided in a random order,
979 * so check the connection cache for each item to avoid unnecessary
982 dfs_refs = talloc_array(ctx, struct cli_dfs_path_split, num_refs);
983 if (dfs_refs == NULL) {
984 return NT_STATUS_NO_MEMORY;
987 for (count = 0; count < num_refs; count++) {
988 if (!split_dfs_path(dfs_refs, refs[count].dfspath,
989 &dfs_refs[count].server,
990 &dfs_refs[count].share,
991 &dfs_refs[count].extrapath)) {
992 TALLOC_FREE(dfs_refs);
993 return NT_STATUS_NOT_FOUND;
996 ccli = cli_cm_find(rootcli, dfs_refs[count].server,
997 dfs_refs[count].share);
999 extrapath = dfs_refs[count].extrapath;
1006 * If no cached connection was found, then connect to the first live
1007 * referral server in the list.
1009 for (count = 0; (ccli == NULL) && (count < num_refs); count++) {
1010 /* Connect to the target server & share */
1011 status = cli_cm_connect(ctx, rootcli,
1012 dfs_refs[count].server,
1013 dfs_refs[count].share,
1016 smb1cli_conn_encryption_on(rootcli->conn),
1017 smbXcli_conn_protocol(rootcli->conn),
1021 if (!NT_STATUS_IS_OK(status)) {
1022 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1023 dfs_refs[count].server,
1024 dfs_refs[count].share);
1027 extrapath = dfs_refs[count].extrapath;
1032 /* No available referral server for the connection */
1033 if (*targetcli == NULL) {
1034 TALLOC_FREE(dfs_refs);
1038 /* Make sure to recreate the original string including any wildcards. */
1040 dfs_path = cli_dfs_make_full_path(ctx, rootcli, path);
1042 TALLOC_FREE(dfs_refs);
1043 return NT_STATUS_NO_MEMORY;
1045 pathlen = strlen(dfs_path);
1046 consumed = MIN(pathlen, consumed);
1047 *pp_targetpath = talloc_strdup(ctx, &dfs_path[consumed]);
1048 if (!*pp_targetpath) {
1049 TALLOC_FREE(dfs_refs);
1050 return NT_STATUS_NO_MEMORY;
1052 dfs_path[consumed] = '\0';
1055 * *pp_targetpath is now the unconsumed part of the path.
1056 * dfs_path is now the consumed part of the path
1057 * (in \server\share\path format).
1060 if (extrapath && strlen(extrapath) > 0) {
1061 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1062 /* put the trailing \ on the path, so to be save we put one in if needed */
1063 if (extrapath[strlen(extrapath)-1] != '\\' && **pp_targetpath != '\\') {
1064 *pp_targetpath = talloc_asprintf(ctx,
1069 *pp_targetpath = talloc_asprintf(ctx,
1074 if (!*pp_targetpath) {
1075 TALLOC_FREE(dfs_refs);
1076 return NT_STATUS_NO_MEMORY;
1080 /* parse out the consumed mount path */
1081 /* trim off the \server\share\ */
1085 if (*ppath != '\\') {
1086 d_printf("cli_resolve_path: "
1087 "dfs_path (%s) not in correct format.\n",
1089 TALLOC_FREE(dfs_refs);
1090 return NT_STATUS_NOT_FOUND;
1093 ppath++; /* Now pointing at start of server name. */
1095 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
1096 TALLOC_FREE(dfs_refs);
1097 return NT_STATUS_NOT_FOUND;
1100 ppath++; /* Now pointing at start of share name. */
1102 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
1103 TALLOC_FREE(dfs_refs);
1104 return NT_STATUS_NOT_FOUND;
1107 ppath++; /* Now pointing at path component. */
1109 newmount = talloc_asprintf(ctx, "%s\\%s", mountpt, ppath );
1111 TALLOC_FREE(dfs_refs);
1112 return NT_STATUS_NOT_FOUND;
1115 cli_set_mntpoint(*targetcli, newmount);
1117 /* Check for another dfs referral, note that we are not
1118 checking for loops here. */
1120 if (!strequal(*pp_targetpath, "\\") && !strequal(*pp_targetpath, "/")) {
1121 status = cli_resolve_path(ctx,
1128 if (NT_STATUS_IS_OK(status)) {
1130 * When cli_resolve_path returns true here it's always
1131 * returning the complete path in newpath, so we're done
1134 *targetcli = newcli;
1135 *pp_targetpath = newpath;
1136 TALLOC_FREE(dfs_refs);
1143 if (smbXcli_conn_protocol((*targetcli)->conn) >= PROTOCOL_SMB2_02) {
1144 target_tcon = (*targetcli)->smb2.tcon;
1146 target_tcon = (*targetcli)->smb1.tcon;
1149 /* If returning true ensure we return a dfs root full path. */
1150 if (smbXcli_tcon_is_dfs_share(target_tcon)) {
1151 dfs_path = talloc_strdup(ctx, *pp_targetpath);
1153 TALLOC_FREE(dfs_refs);
1154 return NT_STATUS_NO_MEMORY;
1156 *pp_targetpath = cli_dfs_make_full_path(ctx, *targetcli, dfs_path);
1157 if (*pp_targetpath == NULL) {
1158 TALLOC_FREE(dfs_refs);
1159 return NT_STATUS_NO_MEMORY;
1163 TALLOC_FREE(dfs_refs);
1164 return NT_STATUS_OK;
1167 /********************************************************************
1168 ********************************************************************/
1170 bool cli_check_msdfs_proxy(TALLOC_CTX *ctx,
1171 struct cli_state *cli,
1172 const char *sharename,
1173 char **pp_newserver,
1176 const char *username,
1177 const char *password,
1180 struct client_dfs_referral *refs = NULL;
1181 size_t num_refs = 0;
1182 size_t consumed = 0;
1183 char *fullpath = NULL;
1186 char *newextrapath = NULL;
1188 const char *remote_name;
1190 if (!cli || !sharename) {
1194 remote_name = smbXcli_conn_remote_name(cli->conn);
1195 cnum = cli_state_get_tid(cli);
1197 /* special case. never check for a referral on the IPC$ share */
1199 if (strequal(sharename, "IPC$")) {
1203 /* send a trans2_query_path_info to check for a referral */
1205 fullpath = talloc_asprintf(ctx, "\\%s\\%s", remote_name, sharename);
1210 /* check for the referral */
1212 if (!NT_STATUS_IS_OK(cli_tree_connect(cli, "IPC$", "IPC", NULL, 0))) {
1216 if (force_encrypt) {
1217 status = cli_cm_force_encryption(cli,
1222 if (!NT_STATUS_IS_OK(status)) {
1227 status = cli_dfs_get_referral(ctx, cli, fullpath, &refs,
1228 &num_refs, &consumed);
1229 res = NT_STATUS_IS_OK(status);
1231 status = cli_tdis(cli);
1232 if (!NT_STATUS_IS_OK(status)) {
1236 cli_state_set_tid(cli, cnum);
1238 if (!res || !num_refs) {
1242 if (!refs[0].dfspath) {
1246 if (!split_dfs_path(ctx, refs[0].dfspath, pp_newserver,
1247 pp_newshare, &newextrapath)) {
1251 /* check that this is not a self-referral */
1253 if (strequal(remote_name, *pp_newserver) &&
1254 strequal(sharename, *pp_newshare)) {