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
;
118 /* make a copy so we don't modify the global string 'service' */
119 servicename
= talloc_strdup(ctx
,share
);
121 return NT_STATUS_NO_MEMORY
;
123 sharename
= servicename
;
124 if (*sharename
== '\\') {
126 if (server
== NULL
) {
129 sharename
= strchr_m(sharename
,'\\');
131 return NT_STATUS_NO_MEMORY
;
136 if (server
== NULL
) {
137 return NT_STATUS_INVALID_PARAMETER
;
140 if (get_cmdline_auth_info_use_kerberos(auth_info
)) {
141 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
143 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info
)) {
144 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
146 if (get_cmdline_auth_info_use_ccache(auth_info
)) {
147 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
149 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info
)) {
150 flags
|= CLI_FULL_CONNECTION_USE_NT_HASH
;
153 status
= cli_connect_nb(
154 server
, NULL
, port
, name_type
, NULL
,
155 get_cmdline_auth_info_signing_state(auth_info
),
158 if (!NT_STATUS_IS_OK(status
)) {
159 d_printf("Connection to %s failed (Error %s)\n",
165 if (max_protocol
== 0) {
166 max_protocol
= PROTOCOL_NT1
;
168 DEBUG(4,(" session request ok\n"));
170 status
= smbXcli_negprot(c
->conn
, c
->timeout
,
171 lp_client_min_protocol(),
174 if (!NT_STATUS_IS_OK(status
)) {
175 d_printf("protocol negotiation failed: %s\n",
181 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
182 /* Ensure we ask for some initial credits. */
183 smb2cli_conn_set_max_credits(c
->conn
, DEFAULT_SMB2_MAX_CREDITS
);
186 username
= get_cmdline_auth_info_username(auth_info
);
187 password
= get_cmdline_auth_info_password(auth_info
);
188 domain
= get_cmdline_auth_info_domain(auth_info
);
189 if ((domain
== NULL
) || (domain
[0] == '\0')) {
190 domain
= lp_workgroup();
193 status
= cli_session_setup(c
, username
,
194 password
, strlen(password
),
195 password
, strlen(password
),
197 if (!NT_STATUS_IS_OK(status
)) {
198 /* If a password was not supplied then
199 * try again with a null username. */
200 if (password
[0] || !username
[0] ||
201 get_cmdline_auth_info_use_kerberos(auth_info
) ||
202 !NT_STATUS_IS_OK(status
= cli_session_setup(c
, "",
206 d_printf("session setup failed: %s\n",
208 if (NT_STATUS_EQUAL(status
,
209 NT_STATUS_MORE_PROCESSING_REQUIRED
))
210 d_printf("did you forget to run kinit?\n");
214 d_printf("Anonymous login successful\n");
217 if (!NT_STATUS_IS_OK(status
)) {
218 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
223 if ( show_sessetup
) {
224 if (*c
->server_domain
) {
225 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
226 c
->server_domain
,c
->server_os
,c
->server_type
));
227 } else if (*c
->server_os
|| *c
->server_type
) {
228 DEBUG(0,("OS=[%s] Server=[%s]\n",
229 c
->server_os
,c
->server_type
));
232 DEBUG(4,(" session setup ok\n"));
234 /* here's the fun part....to support 'msdfs proxy' shares
235 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
236 here before trying to connect to the original share.
237 cli_check_msdfs_proxy() will fail if it is a normal share. */
239 if (smbXcli_conn_dfs_supported(c
->conn
) &&
240 cli_check_msdfs_proxy(ctx
, c
, sharename
,
241 &newserver
, &newshare
,
247 return do_connect(ctx
, newserver
,
248 newshare
, auth_info
, false,
249 force_encrypt
, max_protocol
,
250 port
, name_type
, pcli
);
253 /* must be a normal share */
255 status
= cli_tree_connect(c
, sharename
, "?????",
256 password
, strlen(password
)+1);
257 if (!NT_STATUS_IS_OK(status
)) {
258 d_printf("tree connect failed: %s\n", nt_errstr(status
));
264 status
= cli_cm_force_encryption(c
,
269 if (!NT_STATUS_IS_OK(status
)) {
275 DEBUG(4,(" tconx ok\n"));
280 /****************************************************************************
281 ****************************************************************************/
283 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
285 TALLOC_CTX
*frame
= talloc_stackframe();
286 char *name
= clean_name(frame
, mnt
);
291 TALLOC_FREE(cli
->dfs_mountpoint
);
292 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
296 /********************************************************************
297 Add a new connection to the list.
298 referring_cli == NULL means a new initial connection.
299 ********************************************************************/
301 static NTSTATUS
cli_cm_connect(TALLOC_CTX
*ctx
,
302 struct cli_state
*referring_cli
,
305 const struct user_auth_info
*auth_info
,
311 struct cli_state
**pcli
)
313 struct cli_state
*cli
;
316 status
= do_connect(ctx
, server
, share
,
318 show_hdr
, force_encrypt
, max_protocol
,
319 port
, name_type
, &cli
);
321 if (!NT_STATUS_IS_OK(status
)) {
325 /* Enter into the list. */
327 DLIST_ADD_END(referring_cli
, cli
, struct cli_state
*);
330 if (referring_cli
&& referring_cli
->requested_posix_capabilities
) {
331 uint16_t major
, minor
;
332 uint32_t caplow
, caphigh
;
333 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
335 if (NT_STATUS_IS_OK(status
)) {
336 cli_set_unix_extensions_capabilities(cli
,
346 /********************************************************************
347 Return a connection to a server on a particular share.
348 ********************************************************************/
350 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
360 /* Search to the start of the list. */
361 for (p
= cli
; p
; p
= DLIST_PREV(p
)) {
362 const char *remote_name
=
363 smbXcli_conn_remote_name(p
->conn
);
365 if (strequal(server
, remote_name
) &&
366 strequal(share
,p
->share
)) {
371 /* Search to the end of the list. */
372 for (p
= cli
->next
; p
; p
= p
->next
) {
373 const char *remote_name
=
374 smbXcli_conn_remote_name(p
->conn
);
376 if (strequal(server
, remote_name
) &&
377 strequal(share
,p
->share
)) {
385 /****************************************************************************
386 Open a client connection to a \\server\share.
387 ****************************************************************************/
389 NTSTATUS
cli_cm_open(TALLOC_CTX
*ctx
,
390 struct cli_state
*referring_cli
,
393 const struct user_auth_info
*auth_info
,
399 struct cli_state
**pcli
)
401 /* Try to reuse an existing connection in this list. */
402 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
410 if (auth_info
== NULL
) {
411 /* Can't do a new connection
412 * without auth info. */
413 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
414 "without auth info\n",
416 return NT_STATUS_INVALID_PARAMETER
;
419 status
= cli_cm_connect(ctx
,
430 if (!NT_STATUS_IS_OK(status
)) {
437 /****************************************************************************
438 ****************************************************************************/
440 void cli_cm_display(struct cli_state
*cli
)
444 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
445 d_printf("%d:\tserver=%s, share=%s\n",
446 i
, smbXcli_conn_remote_name(cli
->conn
), cli
->share
);
450 /****************************************************************************
451 ****************************************************************************/
453 /****************************************************************************
454 ****************************************************************************/
457 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
459 SAFE_FREE(cm_creds
.username
);
460 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
463 if (get_cmdline_auth_info_got_pass(auth_info
)) {
464 cm_set_password(get_cmdline_auth_info_password(auth_info
));
467 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
468 cm_creds
.fallback_after_kerberos
= false;
469 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
473 /**********************************************************************
474 split a dfs path into the server, share name, and extrapath components
475 **********************************************************************/
477 static bool split_dfs_path(TALLOC_CTX
*ctx
,
478 const char *nodepath
,
488 *pp_extrapath
= NULL
;
490 path
= talloc_strdup(ctx
, nodepath
);
495 if ( path
[0] != '\\' ) {
499 p
= strchr_m( path
+ 1, '\\' );
507 /* Look for any extra/deep path */
508 q
= strchr_m(p
, '\\');
512 *pp_extrapath
= talloc_strdup(ctx
, q
);
514 *pp_extrapath
= talloc_strdup(ctx
, "");
516 if (*pp_extrapath
== NULL
) {
520 *pp_share
= talloc_strdup(ctx
, p
);
521 if (*pp_share
== NULL
) {
525 *pp_server
= talloc_strdup(ctx
, &path
[1]);
526 if (*pp_server
== NULL
) {
534 TALLOC_FREE(*pp_share
);
535 TALLOC_FREE(*pp_extrapath
);
540 /****************************************************************************
541 Return the original path truncated at the directory component before
542 the first wildcard character. Trust the caller to provide a NULL
544 ****************************************************************************/
546 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
552 /* No absolute paths. */
553 while (IS_DIRECTORY_SEP(*path
)) {
557 path_out
= talloc_strdup(ctx
, path
);
562 p1
= strchr_m(path_out
, '*');
563 p2
= strchr_m(path_out
, '?');
575 /* Now go back to the start of this component. */
576 p1
= strrchr_m(path_out
, '/');
577 p2
= strrchr_m(path_out
, '\\');
584 /* Strip any trailing separator */
586 len
= strlen(path_out
);
587 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
588 path_out
[len
-1] = '\0';
594 /****************************************************************************
595 ****************************************************************************/
597 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
598 struct cli_state
*cli
,
601 char path_sep
= '\\';
603 /* Ensure the extrapath doesn't start with a separator. */
604 while (IS_DIRECTORY_SEP(*dir
)) {
608 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
611 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
613 smbXcli_conn_remote_name(cli
->conn
),
620 /********************************************************************
621 check for dfs referral
622 ********************************************************************/
624 static bool cli_dfs_check_error(struct cli_state
*cli
, NTSTATUS expected
,
627 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
629 if (!(smbXcli_conn_use_unicode(cli
->conn
))) {
632 if (!(smb1cli_conn_capabilities(cli
->conn
) & CAP_STATUS32
)) {
635 if (NT_STATUS_EQUAL(status
, expected
)) {
641 /********************************************************************
642 Get the dfs referral link.
643 ********************************************************************/
645 NTSTATUS
cli_dfs_get_referral(TALLOC_CTX
*ctx
,
646 struct cli_state
*cli
,
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, 0x03); /* 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 clistr_pull_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 /********************************************************************
841 ********************************************************************/
842 struct cli_dfs_path_split
{
848 NTSTATUS
cli_resolve_path(TALLOC_CTX
*ctx
,
850 const struct user_auth_info
*dfs_auth_info
,
851 struct cli_state
*rootcli
,
853 struct cli_state
**targetcli
,
854 char **pp_targetpath
)
856 struct client_dfs_referral
*refs
= NULL
;
859 struct cli_state
*cli_ipc
= NULL
;
860 char *dfs_path
= NULL
;
861 char *cleanpath
= NULL
;
862 char *extrapath
= NULL
;
864 struct cli_state
*newcli
= NULL
;
865 struct cli_state
*ccli
= NULL
;
867 char *newpath
= NULL
;
868 char *newmount
= NULL
;
870 SMB_STRUCT_STAT sbuf
;
873 struct smbXcli_tcon
*root_tcon
= NULL
;
874 struct smbXcli_tcon
*target_tcon
= NULL
;
875 struct cli_dfs_path_split
*dfs_refs
= NULL
;
877 if ( !rootcli
|| !path
|| !targetcli
) {
878 return NT_STATUS_INVALID_PARAMETER
;
881 /* Don't do anything if this is not a DFS root. */
883 if (smbXcli_conn_protocol(rootcli
->conn
) >= PROTOCOL_SMB2_02
) {
884 root_tcon
= rootcli
->smb2
.tcon
;
886 root_tcon
= rootcli
->smb1
.tcon
;
889 if (!smbXcli_tcon_is_dfs_share(root_tcon
)) {
890 *targetcli
= rootcli
;
891 *pp_targetpath
= talloc_strdup(ctx
, path
);
892 if (!*pp_targetpath
) {
893 return NT_STATUS_NO_MEMORY
;
900 /* Send a trans2_query_path_info to check for a referral. */
902 cleanpath
= clean_path(ctx
, path
);
904 return NT_STATUS_NO_MEMORY
;
907 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
909 return NT_STATUS_NO_MEMORY
;
912 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
913 if (NT_STATUS_IS_OK(status
)) {
914 /* This is an ordinary path, just return it. */
915 *targetcli
= rootcli
;
916 *pp_targetpath
= talloc_strdup(ctx
, path
);
917 if (!*pp_targetpath
) {
918 return NT_STATUS_NO_MEMORY
;
923 /* Special case where client asked for a path that does not exist */
925 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
927 *targetcli
= rootcli
;
928 *pp_targetpath
= talloc_strdup(ctx
, path
);
929 if (!*pp_targetpath
) {
930 return NT_STATUS_NO_MEMORY
;
935 /* We got an error, check for DFS referral. */
937 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
942 /* Check for the referral. */
944 status
= cli_cm_open(ctx
,
946 smbXcli_conn_remote_name(rootcli
->conn
),
950 smb1cli_conn_encryption_on(rootcli
->conn
),
951 smbXcli_conn_protocol(rootcli
->conn
),
955 if (!NT_STATUS_IS_OK(status
)) {
959 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
960 &num_refs
, &consumed
);
961 if (!NT_STATUS_IS_OK(status
) || !num_refs
) {
965 if (!refs
[0].dfspath
) {
966 return NT_STATUS_NOT_FOUND
;
970 * Bug#10123 - DFS referal entries can be provided in a random order,
971 * so check the connection cache for each item to avoid unnecessary
974 dfs_refs
= talloc_array(ctx
, struct cli_dfs_path_split
, num_refs
);
975 if (dfs_refs
== NULL
) {
976 return NT_STATUS_NO_MEMORY
;
979 for (count
= 0; count
< num_refs
; count
++) {
980 if (!split_dfs_path(dfs_refs
, refs
[count
].dfspath
,
981 &dfs_refs
[count
].server
,
982 &dfs_refs
[count
].share
,
983 &dfs_refs
[count
].extrapath
)) {
984 TALLOC_FREE(dfs_refs
);
985 return NT_STATUS_NOT_FOUND
;
988 ccli
= cli_cm_find(rootcli
, dfs_refs
[count
].server
,
989 dfs_refs
[count
].share
);
991 extrapath
= dfs_refs
[count
].extrapath
;
998 * If no cached connection was found, then connect to the first live
999 * referral server in the list.
1001 for (count
= 0; (ccli
== NULL
) && (count
< num_refs
); count
++) {
1002 /* Connect to the target server & share */
1003 status
= cli_cm_connect(ctx
, rootcli
,
1004 dfs_refs
[count
].server
,
1005 dfs_refs
[count
].share
,
1008 smb1cli_conn_encryption_on(rootcli
->conn
),
1009 smbXcli_conn_protocol(rootcli
->conn
),
1013 if (!NT_STATUS_IS_OK(status
)) {
1014 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1015 dfs_refs
[count
].server
,
1016 dfs_refs
[count
].share
);
1019 extrapath
= dfs_refs
[count
].extrapath
;
1024 /* No available referral server for the connection */
1025 if (*targetcli
== NULL
) {
1026 TALLOC_FREE(dfs_refs
);
1030 /* Make sure to recreate the original string including any wildcards. */
1032 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
1034 TALLOC_FREE(dfs_refs
);
1035 return NT_STATUS_NO_MEMORY
;
1037 pathlen
= strlen(dfs_path
);
1038 consumed
= MIN(pathlen
, consumed
);
1039 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
1040 if (!*pp_targetpath
) {
1041 TALLOC_FREE(dfs_refs
);
1042 return NT_STATUS_NO_MEMORY
;
1044 dfs_path
[consumed
] = '\0';
1047 * *pp_targetpath is now the unconsumed part of the path.
1048 * dfs_path is now the consumed part of the path
1049 * (in \server\share\path format).
1052 if (extrapath
&& strlen(extrapath
) > 0) {
1053 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1054 /* put the trailing \ on the path, so to be save we put one in if needed */
1055 if (extrapath
[strlen(extrapath
)-1] != '\\' && **pp_targetpath
!= '\\') {
1056 *pp_targetpath
= talloc_asprintf(ctx
,
1061 *pp_targetpath
= talloc_asprintf(ctx
,
1066 if (!*pp_targetpath
) {
1067 TALLOC_FREE(dfs_refs
);
1068 return NT_STATUS_NO_MEMORY
;
1072 /* parse out the consumed mount path */
1073 /* trim off the \server\share\ */
1077 if (*ppath
!= '\\') {
1078 d_printf("cli_resolve_path: "
1079 "dfs_path (%s) not in correct format.\n",
1081 TALLOC_FREE(dfs_refs
);
1082 return NT_STATUS_NOT_FOUND
;
1085 ppath
++; /* Now pointing at start of server name. */
1087 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
1088 TALLOC_FREE(dfs_refs
);
1089 return NT_STATUS_NOT_FOUND
;
1092 ppath
++; /* Now pointing at start of share name. */
1094 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
1095 TALLOC_FREE(dfs_refs
);
1096 return NT_STATUS_NOT_FOUND
;
1099 ppath
++; /* Now pointing at path component. */
1101 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
1103 TALLOC_FREE(dfs_refs
);
1104 return NT_STATUS_NOT_FOUND
;
1107 cli_set_mntpoint(*targetcli
, newmount
);
1109 /* Check for another dfs referral, note that we are not
1110 checking for loops here. */
1112 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1113 status
= cli_resolve_path(ctx
,
1120 if (NT_STATUS_IS_OK(status
)) {
1122 * When cli_resolve_path returns true here it's always
1123 * returning the complete path in newpath, so we're done
1126 *targetcli
= newcli
;
1127 *pp_targetpath
= newpath
;
1128 TALLOC_FREE(dfs_refs
);
1135 if (smbXcli_conn_protocol((*targetcli
)->conn
) >= PROTOCOL_SMB2_02
) {
1136 target_tcon
= (*targetcli
)->smb2
.tcon
;
1138 target_tcon
= (*targetcli
)->smb1
.tcon
;
1141 /* If returning true ensure we return a dfs root full path. */
1142 if (smbXcli_tcon_is_dfs_share(target_tcon
)) {
1143 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1145 TALLOC_FREE(dfs_refs
);
1146 return NT_STATUS_NO_MEMORY
;
1148 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1149 if (*pp_targetpath
== NULL
) {
1150 TALLOC_FREE(dfs_refs
);
1151 return NT_STATUS_NO_MEMORY
;
1155 TALLOC_FREE(dfs_refs
);
1156 return NT_STATUS_OK
;
1159 /********************************************************************
1160 ********************************************************************/
1162 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1163 struct cli_state
*cli
,
1164 const char *sharename
,
1165 char **pp_newserver
,
1168 const char *username
,
1169 const char *password
,
1172 struct client_dfs_referral
*refs
= NULL
;
1173 size_t num_refs
= 0;
1174 size_t consumed
= 0;
1175 char *fullpath
= NULL
;
1178 char *newextrapath
= NULL
;
1180 const char *remote_name
;
1182 if (!cli
|| !sharename
) {
1186 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
1187 cnum
= cli_state_get_tid(cli
);
1189 /* special case. never check for a referral on the IPC$ share */
1191 if (strequal(sharename
, "IPC$")) {
1195 /* send a trans2_query_path_info to check for a referral */
1197 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", remote_name
, sharename
);
1202 /* check for the referral */
1204 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, "IPC$", "IPC", NULL
, 0))) {
1208 if (force_encrypt
) {
1209 status
= cli_cm_force_encryption(cli
,
1214 if (!NT_STATUS_IS_OK(status
)) {
1219 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1220 &num_refs
, &consumed
);
1221 res
= NT_STATUS_IS_OK(status
);
1223 status
= cli_tdis(cli
);
1224 if (!NT_STATUS_IS_OK(status
)) {
1228 cli_state_set_tid(cli
, cnum
);
1230 if (!res
|| !num_refs
) {
1234 if (!refs
[0].dfspath
) {
1238 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1239 pp_newshare
, &newextrapath
)) {
1243 /* check that this is not a self-referral */
1245 if (strequal(remote_name
, *pp_newserver
) &&
1246 strequal(sharename
, *pp_newshare
)) {