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_creds(struct cli_state
*c
,
46 struct cli_credentials
*creds
,
47 const char *sharename
)
49 uint16_t major
, minor
;
50 uint32_t caplow
, caphigh
;
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 if (!SERVER_HAS_UNIX_CIFS(c
)) {
68 d_printf("Encryption required and "
69 "server that doesn't support "
70 "UNIX extensions - failing connect\n");
71 return NT_STATUS_NOT_SUPPORTED
;
74 status
= cli_unix_extensions_version(c
, &major
, &minor
, &caplow
,
76 if (!NT_STATUS_IS_OK(status
)) {
77 d_printf("Encryption required and "
78 "can't get UNIX CIFS extensions "
79 "version from server.\n");
80 return NT_STATUS_UNKNOWN_REVISION
;
83 if (!(caplow
& CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP
)) {
84 d_printf("Encryption required and "
85 "share %s doesn't support "
86 "encryption.\n", sharename
);
87 return NT_STATUS_UNSUPPORTED_COMPRESSION
;
90 status
= cli_smb1_setup_encryption(c
, creds
);
91 if (!NT_STATUS_IS_OK(status
)) {
92 d_printf("Encryption required and "
93 "setup failed with error %s.\n",
101 NTSTATUS
cli_cm_force_encryption(struct cli_state
*c
,
102 const char *username
,
103 const char *password
,
105 const char *sharename
)
107 struct cli_credentials
*creds
= NULL
;
110 creds
= cli_session_creds_init(c
,
113 NULL
, /* default realm */
116 c
->fallback_after_kerberos
,
120 return NT_STATUS_NO_MEMORY
;
123 status
= cli_cm_force_encryption_creds(c
, creds
, sharename
);
124 /* gensec currently references the creds so we can't free them here */
125 talloc_unlink(c
, creds
);
129 /********************************************************************
130 Return a connection to a server.
131 ********************************************************************/
133 static NTSTATUS
do_connect(TALLOC_CTX
*ctx
,
136 const struct user_auth_info
*auth_info
,
141 struct cli_state
**pcli
)
143 struct cli_state
*c
= NULL
;
146 char *newserver
, *newshare
;
147 const char *username
;
148 const char *password
;
152 int signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
153 struct cli_credentials
*creds
= NULL
;
156 signing_state
= SMB_SIGNING_REQUIRED
;
159 /* make a copy so we don't modify the global string 'service' */
160 servicename
= talloc_strdup(ctx
,share
);
162 return NT_STATUS_NO_MEMORY
;
164 sharename
= servicename
;
165 if (*sharename
== '\\') {
167 if (server
== NULL
) {
170 sharename
= strchr_m(sharename
,'\\');
172 return NT_STATUS_NO_MEMORY
;
177 if (server
== NULL
) {
178 return NT_STATUS_INVALID_PARAMETER
;
181 if (get_cmdline_auth_info_use_kerberos(auth_info
)) {
182 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
;
184 if (get_cmdline_auth_info_fallback_after_kerberos(auth_info
)) {
185 flags
|= CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
187 if (get_cmdline_auth_info_use_ccache(auth_info
)) {
188 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
190 if (get_cmdline_auth_info_use_pw_nt_hash(auth_info
)) {
191 flags
|= CLI_FULL_CONNECTION_USE_NT_HASH
;
194 status
= cli_connect_nb(
195 server
, NULL
, port
, name_type
, NULL
,
199 if (!NT_STATUS_IS_OK(status
)) {
200 d_printf("Connection to %s failed (Error %s)\n",
206 if (max_protocol
== 0) {
207 max_protocol
= PROTOCOL_NT1
;
209 DEBUG(4,(" session request ok\n"));
211 status
= smbXcli_negprot(c
->conn
, c
->timeout
,
212 lp_client_min_protocol(),
215 if (!NT_STATUS_IS_OK(status
)) {
216 d_printf("protocol negotiation failed: %s\n",
222 if (smbXcli_conn_protocol(c
->conn
) >= PROTOCOL_SMB2_02
) {
223 /* Ensure we ask for some initial credits. */
224 smb2cli_conn_set_max_credits(c
->conn
, DEFAULT_SMB2_MAX_CREDITS
);
227 username
= get_cmdline_auth_info_username(auth_info
);
228 password
= get_cmdline_auth_info_password(auth_info
);
229 domain
= get_cmdline_auth_info_domain(auth_info
);
230 if ((domain
== NULL
) || (domain
[0] == '\0')) {
231 domain
= lp_workgroup();
234 creds
= get_cmdline_auth_info_creds(auth_info
);
236 status
= cli_session_setup_creds(c
, creds
);
237 if (!NT_STATUS_IS_OK(status
)) {
238 /* If a password was not supplied then
239 * try again with a null username. */
240 if (password
[0] || !username
[0] ||
241 get_cmdline_auth_info_use_kerberos(auth_info
) ||
242 !NT_STATUS_IS_OK(status
= cli_session_setup_anon(c
)))
244 d_printf("session setup failed: %s\n",
246 if (NT_STATUS_EQUAL(status
,
247 NT_STATUS_MORE_PROCESSING_REQUIRED
))
248 d_printf("did you forget to run kinit?\n");
252 d_printf("Anonymous login successful\n");
255 if (!NT_STATUS_IS_OK(status
)) {
256 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
261 DEBUG(4,(" session setup ok\n"));
263 /* here's the fun part....to support 'msdfs proxy' shares
264 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
265 here before trying to connect to the original share.
266 cli_check_msdfs_proxy() will fail if it is a normal share. */
268 if (smbXcli_conn_dfs_supported(c
->conn
) &&
269 cli_check_msdfs_proxy(ctx
, c
, sharename
,
270 &newserver
, &newshare
,
271 force_encrypt
, creds
)) {
273 return do_connect(ctx
, newserver
,
275 force_encrypt
, max_protocol
,
276 port
, name_type
, pcli
);
279 /* must be a normal share */
281 status
= cli_tree_connect_creds(c
, sharename
, "?????", creds
);
282 if (!NT_STATUS_IS_OK(status
)) {
283 d_printf("tree connect failed: %s\n", nt_errstr(status
));
289 status
= cli_cm_force_encryption_creds(c
,
292 if (!NT_STATUS_IS_OK(status
)) {
298 DEBUG(4,(" tconx ok\n"));
303 /****************************************************************************
304 ****************************************************************************/
306 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
308 TALLOC_CTX
*frame
= talloc_stackframe();
309 char *name
= clean_name(frame
, mnt
);
314 TALLOC_FREE(cli
->dfs_mountpoint
);
315 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
319 /********************************************************************
320 Add a new connection to the list.
321 referring_cli == NULL means a new initial connection.
322 ********************************************************************/
324 static NTSTATUS
cli_cm_connect(TALLOC_CTX
*ctx
,
325 struct cli_state
*referring_cli
,
328 const struct user_auth_info
*auth_info
,
333 struct cli_state
**pcli
)
335 struct cli_state
*cli
;
338 status
= do_connect(ctx
, server
, share
,
340 force_encrypt
, max_protocol
,
341 port
, name_type
, &cli
);
343 if (!NT_STATUS_IS_OK(status
)) {
347 /* Enter into the list. */
349 DLIST_ADD_END(referring_cli
, cli
);
352 if (referring_cli
&& referring_cli
->requested_posix_capabilities
) {
353 uint16_t major
, minor
;
354 uint32_t caplow
, caphigh
;
355 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
357 if (NT_STATUS_IS_OK(status
)) {
358 cli_set_unix_extensions_capabilities(cli
,
368 /********************************************************************
369 Return a connection to a server on a particular share.
370 ********************************************************************/
372 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
382 /* Search to the start of the list. */
383 for (p
= cli
; p
; p
= DLIST_PREV(p
)) {
384 const char *remote_name
=
385 smbXcli_conn_remote_name(p
->conn
);
387 if (strequal(server
, remote_name
) &&
388 strequal(share
,p
->share
)) {
393 /* Search to the end of the list. */
394 for (p
= cli
->next
; p
; p
= p
->next
) {
395 const char *remote_name
=
396 smbXcli_conn_remote_name(p
->conn
);
398 if (strequal(server
, remote_name
) &&
399 strequal(share
,p
->share
)) {
407 /****************************************************************************
408 Open a client connection to a \\server\share.
409 ****************************************************************************/
411 NTSTATUS
cli_cm_open(TALLOC_CTX
*ctx
,
412 struct cli_state
*referring_cli
,
415 const struct user_auth_info
*auth_info
,
420 struct cli_state
**pcli
)
422 /* Try to reuse an existing connection in this list. */
423 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
431 if (auth_info
== NULL
) {
432 /* Can't do a new connection
433 * without auth info. */
434 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
435 "without auth info\n",
437 return NT_STATUS_INVALID_PARAMETER
;
440 status
= cli_cm_connect(ctx
,
450 if (!NT_STATUS_IS_OK(status
)) {
457 /****************************************************************************
458 ****************************************************************************/
460 void cli_cm_display(struct cli_state
*cli
)
464 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
465 d_printf("%d:\tserver=%s, share=%s\n",
466 i
, smbXcli_conn_remote_name(cli
->conn
), cli
->share
);
470 /****************************************************************************
471 ****************************************************************************/
473 /****************************************************************************
474 ****************************************************************************/
477 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
479 SAFE_FREE(cm_creds
.username
);
480 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
483 if (get_cmdline_auth_info_got_pass(auth_info
)) {
484 cm_set_password(get_cmdline_auth_info_password(auth_info
));
487 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
488 cm_creds
.fallback_after_kerberos
= false;
489 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
493 /**********************************************************************
494 split a dfs path into the server, share name, and extrapath components
495 **********************************************************************/
497 static bool split_dfs_path(TALLOC_CTX
*ctx
,
498 const char *nodepath
,
508 *pp_extrapath
= NULL
;
510 path
= talloc_strdup(ctx
, nodepath
);
515 if ( path
[0] != '\\' ) {
519 p
= strchr_m( path
+ 1, '\\' );
527 /* Look for any extra/deep path */
528 q
= strchr_m(p
, '\\');
532 *pp_extrapath
= talloc_strdup(ctx
, q
);
534 *pp_extrapath
= talloc_strdup(ctx
, "");
536 if (*pp_extrapath
== NULL
) {
540 *pp_share
= talloc_strdup(ctx
, p
);
541 if (*pp_share
== NULL
) {
545 *pp_server
= talloc_strdup(ctx
, &path
[1]);
546 if (*pp_server
== NULL
) {
554 TALLOC_FREE(*pp_share
);
555 TALLOC_FREE(*pp_extrapath
);
560 /****************************************************************************
561 Return the original path truncated at the directory component before
562 the first wildcard character. Trust the caller to provide a NULL
564 ****************************************************************************/
566 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
572 /* No absolute paths. */
573 while (IS_DIRECTORY_SEP(*path
)) {
577 path_out
= talloc_strdup(ctx
, path
);
582 p1
= strchr_m(path_out
, '*');
583 p2
= strchr_m(path_out
, '?');
595 /* Now go back to the start of this component. */
596 p1
= strrchr_m(path_out
, '/');
597 p2
= strrchr_m(path_out
, '\\');
604 /* Strip any trailing separator */
606 len
= strlen(path_out
);
607 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
608 path_out
[len
-1] = '\0';
614 /****************************************************************************
615 ****************************************************************************/
617 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
618 struct cli_state
*cli
,
621 char path_sep
= '\\';
623 /* Ensure the extrapath doesn't start with a separator. */
624 while (IS_DIRECTORY_SEP(*dir
)) {
628 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
631 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
633 smbXcli_conn_remote_name(cli
->conn
),
640 /********************************************************************
641 check for dfs referral
642 ********************************************************************/
644 static bool cli_dfs_check_error(struct cli_state
*cli
, NTSTATUS expected
,
647 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
649 if (!(smbXcli_conn_use_unicode(cli
->conn
))) {
652 if (!(smb1cli_conn_capabilities(cli
->conn
) & CAP_STATUS32
)) {
655 if (NT_STATUS_EQUAL(status
, expected
)) {
661 /********************************************************************
662 Get the dfs referral link.
663 ********************************************************************/
665 NTSTATUS
cli_dfs_get_referral_ex(TALLOC_CTX
*ctx
,
666 struct cli_state
*cli
,
668 uint16_t max_referral_level
,
669 struct client_dfs_referral
**refs
,
673 unsigned int param_len
= 0;
674 uint16_t recv_flags2
;
675 uint8_t *param
= NULL
;
676 uint8_t *rdata
= NULL
;
679 smb_ucs2_t
*path_ucs
;
680 char *consumed_path
= NULL
;
681 uint16_t consumed_ucs
;
682 uint16_t num_referrals
;
683 struct client_dfs_referral
*referrals
= NULL
;
685 TALLOC_CTX
*frame
= talloc_stackframe();
690 param
= talloc_array(talloc_tos(), uint8_t, 2);
692 status
= NT_STATUS_NO_MEMORY
;
695 SSVAL(param
, 0, max_referral_level
);
697 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
698 path
, strlen(path
)+1,
701 status
= NT_STATUS_NO_MEMORY
;
704 param_len
= talloc_get_size(param
);
705 path_ucs
= (smb_ucs2_t
*)¶m
[2];
707 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
708 DATA_BLOB in_input_buffer
;
709 DATA_BLOB in_output_buffer
= data_blob_null
;
710 DATA_BLOB out_input_buffer
= data_blob_null
;
711 DATA_BLOB out_output_buffer
= data_blob_null
;
713 in_input_buffer
.data
= param
;
714 in_input_buffer
.length
= param_len
;
716 status
= smb2cli_ioctl(cli
->conn
,
720 UINT64_MAX
, /* in_fid_persistent */
721 UINT64_MAX
, /* in_fid_volatile */
722 FSCTL_DFS_GET_REFERRALS
,
723 0, /* in_max_input_length */
725 CLI_BUFFER_SIZE
, /* in_max_output_length */
727 SMB2_IOCTL_FLAG_IS_FSCTL
,
731 if (!NT_STATUS_IS_OK(status
)) {
735 if (out_output_buffer
.length
< 4) {
736 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
740 recv_flags2
= FLAGS2_UNICODE_STRINGS
;
741 rdata
= out_output_buffer
.data
;
742 endp
= (char *)rdata
+ out_output_buffer
.length
;
744 unsigned int data_len
= 0;
747 SSVAL(setup
, 0, TRANSACT2_GET_DFS_REFERRAL
);
749 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
753 NULL
, 0, CLI_BUFFER_SIZE
,
755 NULL
, 0, NULL
, /* rsetup */
757 &rdata
, 4, &data_len
);
758 if (!NT_STATUS_IS_OK(status
)) {
762 endp
= (char *)rdata
+ data_len
;
765 consumed_ucs
= SVAL(rdata
, 0);
766 num_referrals
= SVAL(rdata
, 2);
768 /* consumed_ucs is the number of bytes
769 * of the UCS2 path consumed not counting any
770 * terminating null. We need to convert
771 * back to unix charset and count again
772 * to get the number of bytes consumed from
773 * the incoming path. */
776 if (pull_string_talloc(talloc_tos(),
784 status
= map_nt_error_from_unix(errno
);
786 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
790 if (consumed_path
== NULL
) {
791 status
= map_nt_error_from_unix(errno
);
794 *consumed
= strlen(consumed_path
);
796 if (num_referrals
!= 0) {
797 uint16_t ref_version
;
800 uint16_t node_offset
;
802 referrals
= talloc_array(ctx
, struct client_dfs_referral
,
806 status
= NT_STATUS_NO_MEMORY
;
809 /* start at the referrals array */
812 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
816 ref_version
= SVAL(p
, 0);
817 ref_size
= SVAL(p
, 2);
818 node_offset
= SVAL(p
, 16);
820 if (ref_version
!= 3) {
825 referrals
[i
].proximity
= SVAL(p
, 8);
826 referrals
[i
].ttl
= SVAL(p
, 10);
828 if (p
+ node_offset
> endp
) {
829 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
832 clistr_pull_talloc(referrals
,
835 &referrals
[i
].dfspath
,
837 PTR_DIFF(endp
, p
+node_offset
),
838 STR_TERMINATE
|STR_UNICODE
);
840 if (!referrals
[i
].dfspath
) {
841 status
= map_nt_error_from_unix(errno
);
846 if (i
< num_referrals
) {
847 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
852 *num_refs
= num_referrals
;
861 NTSTATUS
cli_dfs_get_referral(TALLOC_CTX
*ctx
,
862 struct cli_state
*cli
,
864 struct client_dfs_referral
**refs
,
868 return cli_dfs_get_referral_ex(ctx
,
872 refs
, /* Max referral level we want */
877 /********************************************************************
878 ********************************************************************/
879 struct cli_dfs_path_split
{
885 NTSTATUS
cli_resolve_path(TALLOC_CTX
*ctx
,
887 const struct user_auth_info
*dfs_auth_info
,
888 struct cli_state
*rootcli
,
890 struct cli_state
**targetcli
,
891 char **pp_targetpath
)
893 struct client_dfs_referral
*refs
= NULL
;
896 struct cli_state
*cli_ipc
= NULL
;
897 char *dfs_path
= NULL
;
898 char *cleanpath
= NULL
;
899 char *extrapath
= NULL
;
901 struct cli_state
*newcli
= NULL
;
902 struct cli_state
*ccli
= NULL
;
904 char *newpath
= NULL
;
905 char *newmount
= NULL
;
907 SMB_STRUCT_STAT sbuf
;
910 struct smbXcli_tcon
*root_tcon
= NULL
;
911 struct smbXcli_tcon
*target_tcon
= NULL
;
912 struct cli_dfs_path_split
*dfs_refs
= NULL
;
914 if ( !rootcli
|| !path
|| !targetcli
) {
915 return NT_STATUS_INVALID_PARAMETER
;
918 /* Don't do anything if this is not a DFS root. */
920 if (smbXcli_conn_protocol(rootcli
->conn
) >= PROTOCOL_SMB2_02
) {
921 root_tcon
= rootcli
->smb2
.tcon
;
923 root_tcon
= rootcli
->smb1
.tcon
;
927 * Avoid more than one leading directory separator
929 while (IS_DIRECTORY_SEP(path
[0]) && IS_DIRECTORY_SEP(path
[1])) {
933 if (!smbXcli_tcon_is_dfs_share(root_tcon
)) {
934 *targetcli
= rootcli
;
935 *pp_targetpath
= talloc_strdup(ctx
, path
);
936 if (!*pp_targetpath
) {
937 return NT_STATUS_NO_MEMORY
;
944 /* Send a trans2_query_path_info to check for a referral. */
946 cleanpath
= clean_path(ctx
, path
);
948 return NT_STATUS_NO_MEMORY
;
951 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
953 return NT_STATUS_NO_MEMORY
;
956 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
957 if (NT_STATUS_IS_OK(status
)) {
958 /* This is an ordinary path, just return it. */
959 *targetcli
= rootcli
;
960 *pp_targetpath
= talloc_strdup(ctx
, path
);
961 if (!*pp_targetpath
) {
962 return NT_STATUS_NO_MEMORY
;
967 /* Special case where client asked for a path that does not exist */
969 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
971 *targetcli
= rootcli
;
972 *pp_targetpath
= talloc_strdup(ctx
, path
);
973 if (!*pp_targetpath
) {
974 return NT_STATUS_NO_MEMORY
;
979 /* We got an error, check for DFS referral. */
981 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
986 /* Check for the referral. */
988 status
= cli_cm_open(ctx
,
990 smbXcli_conn_remote_name(rootcli
->conn
),
993 smb1cli_conn_encryption_on(rootcli
->conn
),
994 smbXcli_conn_protocol(rootcli
->conn
),
998 if (!NT_STATUS_IS_OK(status
)) {
1002 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
1003 &num_refs
, &consumed
);
1004 if (!NT_STATUS_IS_OK(status
)) {
1008 if (!num_refs
|| !refs
[0].dfspath
) {
1009 return NT_STATUS_NOT_FOUND
;
1013 * Bug#10123 - DFS referal entries can be provided in a random order,
1014 * so check the connection cache for each item to avoid unnecessary
1017 dfs_refs
= talloc_array(ctx
, struct cli_dfs_path_split
, num_refs
);
1018 if (dfs_refs
== NULL
) {
1019 return NT_STATUS_NO_MEMORY
;
1022 for (count
= 0; count
< num_refs
; count
++) {
1023 if (!split_dfs_path(dfs_refs
, refs
[count
].dfspath
,
1024 &dfs_refs
[count
].server
,
1025 &dfs_refs
[count
].share
,
1026 &dfs_refs
[count
].extrapath
)) {
1027 TALLOC_FREE(dfs_refs
);
1028 return NT_STATUS_NOT_FOUND
;
1031 ccli
= cli_cm_find(rootcli
, dfs_refs
[count
].server
,
1032 dfs_refs
[count
].share
);
1034 extrapath
= dfs_refs
[count
].extrapath
;
1041 * If no cached connection was found, then connect to the first live
1042 * referral server in the list.
1044 for (count
= 0; (ccli
== NULL
) && (count
< num_refs
); count
++) {
1045 /* Connect to the target server & share */
1046 status
= cli_cm_connect(ctx
, rootcli
,
1047 dfs_refs
[count
].server
,
1048 dfs_refs
[count
].share
,
1050 smb1cli_conn_encryption_on(rootcli
->conn
),
1051 smbXcli_conn_protocol(rootcli
->conn
),
1055 if (!NT_STATUS_IS_OK(status
)) {
1056 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1057 dfs_refs
[count
].server
,
1058 dfs_refs
[count
].share
);
1061 extrapath
= dfs_refs
[count
].extrapath
;
1066 /* No available referral server for the connection */
1067 if (*targetcli
== NULL
) {
1068 TALLOC_FREE(dfs_refs
);
1072 /* Make sure to recreate the original string including any wildcards. */
1074 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
1076 TALLOC_FREE(dfs_refs
);
1077 return NT_STATUS_NO_MEMORY
;
1079 pathlen
= strlen(dfs_path
);
1080 consumed
= MIN(pathlen
, consumed
);
1081 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
1082 if (!*pp_targetpath
) {
1083 TALLOC_FREE(dfs_refs
);
1084 return NT_STATUS_NO_MEMORY
;
1086 dfs_path
[consumed
] = '\0';
1089 * *pp_targetpath is now the unconsumed part of the path.
1090 * dfs_path is now the consumed part of the path
1091 * (in \server\share\path format).
1094 if (extrapath
&& strlen(extrapath
) > 0) {
1095 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1096 /* put the trailing \ on the path, so to be save we put one in if needed */
1097 if (extrapath
[strlen(extrapath
)-1] != '\\' && **pp_targetpath
!= '\\') {
1098 *pp_targetpath
= talloc_asprintf(ctx
,
1103 *pp_targetpath
= talloc_asprintf(ctx
,
1108 if (!*pp_targetpath
) {
1109 TALLOC_FREE(dfs_refs
);
1110 return NT_STATUS_NO_MEMORY
;
1114 /* parse out the consumed mount path */
1115 /* trim off the \server\share\ */
1119 if (*ppath
!= '\\') {
1120 d_printf("cli_resolve_path: "
1121 "dfs_path (%s) not in correct format.\n",
1123 TALLOC_FREE(dfs_refs
);
1124 return NT_STATUS_NOT_FOUND
;
1127 ppath
++; /* Now pointing at start of server name. */
1129 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
1130 TALLOC_FREE(dfs_refs
);
1131 return NT_STATUS_NOT_FOUND
;
1134 ppath
++; /* Now pointing at start of share name. */
1136 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
1137 TALLOC_FREE(dfs_refs
);
1138 return NT_STATUS_NOT_FOUND
;
1141 ppath
++; /* Now pointing at path component. */
1143 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
1145 TALLOC_FREE(dfs_refs
);
1146 return NT_STATUS_NOT_FOUND
;
1149 cli_set_mntpoint(*targetcli
, newmount
);
1151 /* Check for another dfs referral, note that we are not
1152 checking for loops here. */
1154 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1155 status
= cli_resolve_path(ctx
,
1162 if (NT_STATUS_IS_OK(status
)) {
1164 * When cli_resolve_path returns true here it's always
1165 * returning the complete path in newpath, so we're done
1168 *targetcli
= newcli
;
1169 *pp_targetpath
= newpath
;
1170 TALLOC_FREE(dfs_refs
);
1177 if (smbXcli_conn_protocol((*targetcli
)->conn
) >= PROTOCOL_SMB2_02
) {
1178 target_tcon
= (*targetcli
)->smb2
.tcon
;
1180 target_tcon
= (*targetcli
)->smb1
.tcon
;
1183 /* If returning true ensure we return a dfs root full path. */
1184 if (smbXcli_tcon_is_dfs_share(target_tcon
)) {
1185 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1187 TALLOC_FREE(dfs_refs
);
1188 return NT_STATUS_NO_MEMORY
;
1190 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1191 if (*pp_targetpath
== NULL
) {
1192 TALLOC_FREE(dfs_refs
);
1193 return NT_STATUS_NO_MEMORY
;
1197 TALLOC_FREE(dfs_refs
);
1198 return NT_STATUS_OK
;
1201 /********************************************************************
1202 ********************************************************************/
1204 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1205 struct cli_state
*cli
,
1206 const char *sharename
,
1207 char **pp_newserver
,
1210 struct cli_credentials
*creds
)
1212 struct client_dfs_referral
*refs
= NULL
;
1213 size_t num_refs
= 0;
1214 size_t consumed
= 0;
1215 char *fullpath
= NULL
;
1217 struct smbXcli_tcon
*orig_tcon
= NULL
;
1218 char *newextrapath
= NULL
;
1220 const char *remote_name
;
1222 if (!cli
|| !sharename
) {
1226 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
1228 /* special case. never check for a referral on the IPC$ share */
1230 if (strequal(sharename
, "IPC$")) {
1234 /* send a trans2_query_path_info to check for a referral */
1236 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", remote_name
, sharename
);
1241 /* Store tcon state. */
1242 if (cli_state_has_tcon(cli
)) {
1243 orig_tcon
= cli_state_save_tcon(cli
);
1244 if (orig_tcon
== NULL
) {
1249 /* check for the referral */
1251 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, "IPC$", "IPC", NULL
))) {
1252 cli_state_restore_tcon(cli
, orig_tcon
);
1256 if (force_encrypt
) {
1257 status
= cli_cm_force_encryption_creds(cli
, creds
, "IPC$");
1258 if (!NT_STATUS_IS_OK(status
)) {
1259 cli_state_restore_tcon(cli
, orig_tcon
);
1264 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1265 &num_refs
, &consumed
);
1266 res
= NT_STATUS_IS_OK(status
);
1268 status
= cli_tdis(cli
);
1270 cli_state_restore_tcon(cli
, orig_tcon
);
1272 if (!NT_STATUS_IS_OK(status
)) {
1276 if (!res
|| !num_refs
) {
1280 if (!refs
[0].dfspath
) {
1284 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1285 pp_newshare
, &newextrapath
)) {
1289 /* check that this is not a self-referral */
1291 if (strequal(remote_name
, *pp_newserver
) &&
1292 strequal(sharename
, *pp_newshare
)) {