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_client_min_protocol(),
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");
222 if (!NT_STATUS_IS_OK(status
)) {
223 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
228 if ( show_sessetup
) {
229 if (*c
->server_domain
) {
230 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
231 c
->server_domain
,c
->server_os
,c
->server_type
));
232 } else if (*c
->server_os
|| *c
->server_type
) {
233 DEBUG(0,("OS=[%s] Server=[%s]\n",
234 c
->server_os
,c
->server_type
));
237 DEBUG(4,(" session setup ok\n"));
239 /* here's the fun part....to support 'msdfs proxy' shares
240 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
241 here before trying to connect to the original share.
242 cli_check_msdfs_proxy() will fail if it is a normal share. */
244 if (smbXcli_conn_dfs_supported(c
->conn
) &&
245 cli_check_msdfs_proxy(ctx
, c
, sharename
,
246 &newserver
, &newshare
,
252 return do_connect(ctx
, newserver
,
253 newshare
, auth_info
, false,
254 force_encrypt
, max_protocol
,
255 port
, name_type
, pcli
);
258 /* must be a normal share */
260 status
= cli_tree_connect(c
, sharename
, "?????",
261 password
, strlen(password
)+1);
262 if (!NT_STATUS_IS_OK(status
)) {
263 d_printf("tree connect failed: %s\n", nt_errstr(status
));
269 status
= cli_cm_force_encryption(c
,
274 if (!NT_STATUS_IS_OK(status
)) {
280 DEBUG(4,(" tconx ok\n"));
285 /****************************************************************************
286 ****************************************************************************/
288 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
290 TALLOC_CTX
*frame
= talloc_stackframe();
291 char *name
= clean_name(frame
, mnt
);
296 TALLOC_FREE(cli
->dfs_mountpoint
);
297 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
301 /********************************************************************
302 Add a new connection to the list.
303 referring_cli == NULL means a new initial connection.
304 ********************************************************************/
306 static NTSTATUS
cli_cm_connect(TALLOC_CTX
*ctx
,
307 struct cli_state
*referring_cli
,
310 const struct user_auth_info
*auth_info
,
316 struct cli_state
**pcli
)
318 struct cli_state
*cli
;
321 status
= do_connect(ctx
, server
, share
,
323 show_hdr
, force_encrypt
, max_protocol
,
324 port
, name_type
, &cli
);
326 if (!NT_STATUS_IS_OK(status
)) {
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
,
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(TALLOC_CTX
*ctx
,
651 struct cli_state
*cli
,
653 struct client_dfs_referral
**refs
,
657 unsigned int param_len
= 0;
658 uint16_t recv_flags2
;
659 uint8_t *param
= NULL
;
660 uint8_t *rdata
= NULL
;
663 smb_ucs2_t
*path_ucs
;
664 char *consumed_path
= NULL
;
665 uint16_t consumed_ucs
;
666 uint16_t num_referrals
;
667 struct client_dfs_referral
*referrals
= NULL
;
669 TALLOC_CTX
*frame
= talloc_stackframe();
674 param
= talloc_array(talloc_tos(), uint8_t, 2);
676 status
= NT_STATUS_NO_MEMORY
;
679 SSVAL(param
, 0, 0x03); /* max referral level */
681 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
682 path
, strlen(path
)+1,
685 status
= NT_STATUS_NO_MEMORY
;
688 param_len
= talloc_get_size(param
);
689 path_ucs
= (smb_ucs2_t
*)¶m
[2];
691 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
692 DATA_BLOB in_input_buffer
;
693 DATA_BLOB in_output_buffer
= data_blob_null
;
694 DATA_BLOB out_input_buffer
= data_blob_null
;
695 DATA_BLOB out_output_buffer
= data_blob_null
;
697 in_input_buffer
.data
= param
;
698 in_input_buffer
.length
= param_len
;
700 status
= smb2cli_ioctl(cli
->conn
,
704 UINT64_MAX
, /* in_fid_persistent */
705 UINT64_MAX
, /* in_fid_volatile */
706 FSCTL_DFS_GET_REFERRALS
,
707 0, /* in_max_input_length */
709 CLI_BUFFER_SIZE
, /* in_max_output_length */
711 SMB2_IOCTL_FLAG_IS_FSCTL
,
715 if (!NT_STATUS_IS_OK(status
)) {
719 if (out_output_buffer
.length
< 4) {
720 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
724 recv_flags2
= FLAGS2_UNICODE_STRINGS
;
725 rdata
= out_output_buffer
.data
;
726 endp
= (char *)rdata
+ out_output_buffer
.length
;
728 unsigned int data_len
= 0;
731 SSVAL(setup
, 0, TRANSACT2_GET_DFS_REFERRAL
);
733 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
737 NULL
, 0, CLI_BUFFER_SIZE
,
739 NULL
, 0, NULL
, /* rsetup */
741 &rdata
, 4, &data_len
);
742 if (!NT_STATUS_IS_OK(status
)) {
746 endp
= (char *)rdata
+ data_len
;
749 consumed_ucs
= SVAL(rdata
, 0);
750 num_referrals
= SVAL(rdata
, 2);
752 /* consumed_ucs is the number of bytes
753 * of the UCS2 path consumed not counting any
754 * terminating null. We need to convert
755 * back to unix charset and count again
756 * to get the number of bytes consumed from
757 * the incoming path. */
760 if (pull_string_talloc(talloc_tos(),
768 status
= map_nt_error_from_unix(errno
);
770 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
774 if (consumed_path
== NULL
) {
775 status
= map_nt_error_from_unix(errno
);
778 *consumed
= strlen(consumed_path
);
780 if (num_referrals
!= 0) {
781 uint16_t ref_version
;
784 uint16_t node_offset
;
786 referrals
= talloc_array(ctx
, struct client_dfs_referral
,
790 status
= NT_STATUS_NO_MEMORY
;
793 /* start at the referrals array */
796 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
800 ref_version
= SVAL(p
, 0);
801 ref_size
= SVAL(p
, 2);
802 node_offset
= SVAL(p
, 16);
804 if (ref_version
!= 3) {
809 referrals
[i
].proximity
= SVAL(p
, 8);
810 referrals
[i
].ttl
= SVAL(p
, 10);
812 if (p
+ node_offset
> endp
) {
813 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
816 clistr_pull_talloc(referrals
,
819 &referrals
[i
].dfspath
,
821 PTR_DIFF(endp
, p
+node_offset
),
822 STR_TERMINATE
|STR_UNICODE
);
824 if (!referrals
[i
].dfspath
) {
825 status
= map_nt_error_from_unix(errno
);
830 if (i
< num_referrals
) {
831 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
836 *num_refs
= num_referrals
;
845 /********************************************************************
846 ********************************************************************/
847 struct cli_dfs_path_split
{
853 NTSTATUS
cli_resolve_path(TALLOC_CTX
*ctx
,
855 const struct user_auth_info
*dfs_auth_info
,
856 struct cli_state
*rootcli
,
858 struct cli_state
**targetcli
,
859 char **pp_targetpath
)
861 struct client_dfs_referral
*refs
= NULL
;
864 struct cli_state
*cli_ipc
= NULL
;
865 char *dfs_path
= NULL
;
866 char *cleanpath
= NULL
;
867 char *extrapath
= NULL
;
869 struct cli_state
*newcli
= NULL
;
870 struct cli_state
*ccli
= NULL
;
872 char *newpath
= NULL
;
873 char *newmount
= NULL
;
875 SMB_STRUCT_STAT sbuf
;
878 struct smbXcli_tcon
*root_tcon
= NULL
;
879 struct smbXcli_tcon
*target_tcon
= NULL
;
880 struct cli_dfs_path_split
*dfs_refs
= NULL
;
882 if ( !rootcli
|| !path
|| !targetcli
) {
883 return NT_STATUS_INVALID_PARAMETER
;
886 /* Don't do anything if this is not a DFS root. */
888 if (smbXcli_conn_protocol(rootcli
->conn
) >= PROTOCOL_SMB2_02
) {
889 root_tcon
= rootcli
->smb2
.tcon
;
891 root_tcon
= rootcli
->smb1
.tcon
;
894 if (!smbXcli_tcon_is_dfs_share(root_tcon
)) {
895 *targetcli
= rootcli
;
896 *pp_targetpath
= talloc_strdup(ctx
, path
);
897 if (!*pp_targetpath
) {
898 return NT_STATUS_NO_MEMORY
;
905 /* Send a trans2_query_path_info to check for a referral. */
907 cleanpath
= clean_path(ctx
, path
);
909 return NT_STATUS_NO_MEMORY
;
912 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
914 return NT_STATUS_NO_MEMORY
;
917 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
918 if (NT_STATUS_IS_OK(status
)) {
919 /* This is an ordinary path, just return it. */
920 *targetcli
= rootcli
;
921 *pp_targetpath
= talloc_strdup(ctx
, path
);
922 if (!*pp_targetpath
) {
923 return NT_STATUS_NO_MEMORY
;
928 /* Special case where client asked for a path that does not exist */
930 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
932 *targetcli
= rootcli
;
933 *pp_targetpath
= talloc_strdup(ctx
, path
);
934 if (!*pp_targetpath
) {
935 return NT_STATUS_NO_MEMORY
;
940 /* We got an error, check for DFS referral. */
942 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
947 /* Check for the referral. */
949 status
= cli_cm_open(ctx
,
951 smbXcli_conn_remote_name(rootcli
->conn
),
955 smb1cli_conn_encryption_on(rootcli
->conn
),
956 smbXcli_conn_protocol(rootcli
->conn
),
960 if (!NT_STATUS_IS_OK(status
)) {
964 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
965 &num_refs
, &consumed
);
966 if (!NT_STATUS_IS_OK(status
)) {
970 if (!num_refs
|| !refs
[0].dfspath
) {
971 return NT_STATUS_NOT_FOUND
;
975 * Bug#10123 - DFS referal entries can be provided in a random order,
976 * so check the connection cache for each item to avoid unnecessary
979 dfs_refs
= talloc_array(ctx
, struct cli_dfs_path_split
, num_refs
);
980 if (dfs_refs
== NULL
) {
981 return NT_STATUS_NO_MEMORY
;
984 for (count
= 0; count
< num_refs
; count
++) {
985 if (!split_dfs_path(dfs_refs
, refs
[count
].dfspath
,
986 &dfs_refs
[count
].server
,
987 &dfs_refs
[count
].share
,
988 &dfs_refs
[count
].extrapath
)) {
989 TALLOC_FREE(dfs_refs
);
990 return NT_STATUS_NOT_FOUND
;
993 ccli
= cli_cm_find(rootcli
, dfs_refs
[count
].server
,
994 dfs_refs
[count
].share
);
996 extrapath
= dfs_refs
[count
].extrapath
;
1003 * If no cached connection was found, then connect to the first live
1004 * referral server in the list.
1006 for (count
= 0; (ccli
== NULL
) && (count
< num_refs
); count
++) {
1007 /* Connect to the target server & share */
1008 status
= cli_cm_connect(ctx
, rootcli
,
1009 dfs_refs
[count
].server
,
1010 dfs_refs
[count
].share
,
1013 smb1cli_conn_encryption_on(rootcli
->conn
),
1014 smbXcli_conn_protocol(rootcli
->conn
),
1018 if (!NT_STATUS_IS_OK(status
)) {
1019 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1020 dfs_refs
[count
].server
,
1021 dfs_refs
[count
].share
);
1024 extrapath
= dfs_refs
[count
].extrapath
;
1029 /* No available referral server for the connection */
1030 if (*targetcli
== NULL
) {
1031 TALLOC_FREE(dfs_refs
);
1035 /* Make sure to recreate the original string including any wildcards. */
1037 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
1039 TALLOC_FREE(dfs_refs
);
1040 return NT_STATUS_NO_MEMORY
;
1042 pathlen
= strlen(dfs_path
);
1043 consumed
= MIN(pathlen
, consumed
);
1044 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
1045 if (!*pp_targetpath
) {
1046 TALLOC_FREE(dfs_refs
);
1047 return NT_STATUS_NO_MEMORY
;
1049 dfs_path
[consumed
] = '\0';
1052 * *pp_targetpath is now the unconsumed part of the path.
1053 * dfs_path is now the consumed part of the path
1054 * (in \server\share\path format).
1057 if (extrapath
&& strlen(extrapath
) > 0) {
1058 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1059 /* put the trailing \ on the path, so to be save we put one in if needed */
1060 if (extrapath
[strlen(extrapath
)-1] != '\\' && **pp_targetpath
!= '\\') {
1061 *pp_targetpath
= talloc_asprintf(ctx
,
1066 *pp_targetpath
= talloc_asprintf(ctx
,
1071 if (!*pp_targetpath
) {
1072 TALLOC_FREE(dfs_refs
);
1073 return NT_STATUS_NO_MEMORY
;
1077 /* parse out the consumed mount path */
1078 /* trim off the \server\share\ */
1082 if (*ppath
!= '\\') {
1083 d_printf("cli_resolve_path: "
1084 "dfs_path (%s) not in correct format.\n",
1086 TALLOC_FREE(dfs_refs
);
1087 return NT_STATUS_NOT_FOUND
;
1090 ppath
++; /* Now pointing at start of server name. */
1092 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
1093 TALLOC_FREE(dfs_refs
);
1094 return NT_STATUS_NOT_FOUND
;
1097 ppath
++; /* Now pointing at start of share name. */
1099 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
1100 TALLOC_FREE(dfs_refs
);
1101 return NT_STATUS_NOT_FOUND
;
1104 ppath
++; /* Now pointing at path component. */
1106 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
1108 TALLOC_FREE(dfs_refs
);
1109 return NT_STATUS_NOT_FOUND
;
1112 cli_set_mntpoint(*targetcli
, newmount
);
1114 /* Check for another dfs referral, note that we are not
1115 checking for loops here. */
1117 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1118 status
= cli_resolve_path(ctx
,
1125 if (NT_STATUS_IS_OK(status
)) {
1127 * When cli_resolve_path returns true here it's always
1128 * returning the complete path in newpath, so we're done
1131 *targetcli
= newcli
;
1132 *pp_targetpath
= newpath
;
1133 TALLOC_FREE(dfs_refs
);
1140 if (smbXcli_conn_protocol((*targetcli
)->conn
) >= PROTOCOL_SMB2_02
) {
1141 target_tcon
= (*targetcli
)->smb2
.tcon
;
1143 target_tcon
= (*targetcli
)->smb1
.tcon
;
1146 /* If returning true ensure we return a dfs root full path. */
1147 if (smbXcli_tcon_is_dfs_share(target_tcon
)) {
1148 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1150 TALLOC_FREE(dfs_refs
);
1151 return NT_STATUS_NO_MEMORY
;
1153 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1154 if (*pp_targetpath
== NULL
) {
1155 TALLOC_FREE(dfs_refs
);
1156 return NT_STATUS_NO_MEMORY
;
1160 TALLOC_FREE(dfs_refs
);
1161 return NT_STATUS_OK
;
1164 /********************************************************************
1165 ********************************************************************/
1167 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1168 struct cli_state
*cli
,
1169 const char *sharename
,
1170 char **pp_newserver
,
1173 const char *username
,
1174 const char *password
,
1177 struct client_dfs_referral
*refs
= NULL
;
1178 size_t num_refs
= 0;
1179 size_t consumed
= 0;
1180 char *fullpath
= NULL
;
1183 char *newextrapath
= NULL
;
1185 const char *remote_name
;
1187 if (!cli
|| !sharename
) {
1191 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
1192 cnum
= cli_state_get_tid(cli
);
1194 /* special case. never check for a referral on the IPC$ share */
1196 if (strequal(sharename
, "IPC$")) {
1200 /* send a trans2_query_path_info to check for a referral */
1202 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", remote_name
, sharename
);
1207 /* check for the referral */
1209 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, "IPC$", "IPC", NULL
, 0))) {
1213 if (force_encrypt
) {
1214 status
= cli_cm_force_encryption(cli
,
1219 if (!NT_STATUS_IS_OK(status
)) {
1224 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1225 &num_refs
, &consumed
);
1226 res
= NT_STATUS_IS_OK(status
);
1228 status
= cli_tdis(cli
);
1229 if (!NT_STATUS_IS_OK(status
)) {
1233 cli_state_set_tid(cli
, cnum
);
1235 if (!res
|| !num_refs
) {
1239 if (!refs
[0].dfspath
) {
1243 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1244 pp_newshare
, &newextrapath
)) {
1248 /* check that this is not a self-referral */
1250 if (strequal(remote_name
, *pp_newserver
) &&
1251 strequal(sharename
, *pp_newshare
)) {