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
,
201 if (!NT_STATUS_IS_OK(status
)) {
202 /* If a password was not supplied then
203 * try again with a null username. */
204 if (password
[0] || !username
[0] ||
205 get_cmdline_auth_info_use_kerberos(auth_info
) ||
206 !NT_STATUS_IS_OK(status
= cli_session_setup_anon(c
)))
208 d_printf("session setup failed: %s\n",
210 if (NT_STATUS_EQUAL(status
,
211 NT_STATUS_MORE_PROCESSING_REQUIRED
))
212 d_printf("did you forget to run kinit?\n");
216 d_printf("Anonymous login successful\n");
219 if (!NT_STATUS_IS_OK(status
)) {
220 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
225 if ( show_sessetup
) {
226 if (*c
->server_domain
) {
227 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
228 c
->server_domain
,c
->server_os
,c
->server_type
));
229 } else if (*c
->server_os
|| *c
->server_type
) {
230 DEBUG(0,("OS=[%s] Server=[%s]\n",
231 c
->server_os
,c
->server_type
));
234 DEBUG(4,(" session setup ok\n"));
236 /* here's the fun part....to support 'msdfs proxy' shares
237 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
238 here before trying to connect to the original share.
239 cli_check_msdfs_proxy() will fail if it is a normal share. */
241 if (smbXcli_conn_dfs_supported(c
->conn
) &&
242 cli_check_msdfs_proxy(ctx
, c
, sharename
,
243 &newserver
, &newshare
,
249 return do_connect(ctx
, newserver
,
250 newshare
, auth_info
, false,
251 force_encrypt
, max_protocol
,
252 port
, name_type
, pcli
);
255 /* must be a normal share */
257 status
= cli_tree_connect(c
, sharename
, "?????",
258 password
, strlen(password
)+1);
259 if (!NT_STATUS_IS_OK(status
)) {
260 d_printf("tree connect failed: %s\n", nt_errstr(status
));
266 status
= cli_cm_force_encryption(c
,
271 if (!NT_STATUS_IS_OK(status
)) {
277 DEBUG(4,(" tconx ok\n"));
282 /****************************************************************************
283 ****************************************************************************/
285 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
287 TALLOC_CTX
*frame
= talloc_stackframe();
288 char *name
= clean_name(frame
, mnt
);
293 TALLOC_FREE(cli
->dfs_mountpoint
);
294 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
298 /********************************************************************
299 Add a new connection to the list.
300 referring_cli == NULL means a new initial connection.
301 ********************************************************************/
303 static NTSTATUS
cli_cm_connect(TALLOC_CTX
*ctx
,
304 struct cli_state
*referring_cli
,
307 const struct user_auth_info
*auth_info
,
313 struct cli_state
**pcli
)
315 struct cli_state
*cli
;
318 status
= do_connect(ctx
, server
, share
,
320 show_hdr
, force_encrypt
, max_protocol
,
321 port
, name_type
, &cli
);
323 if (!NT_STATUS_IS_OK(status
)) {
327 /* Enter into the list. */
329 DLIST_ADD_END(referring_cli
, cli
);
332 if (referring_cli
&& referring_cli
->requested_posix_capabilities
) {
333 uint16_t major
, minor
;
334 uint32_t caplow
, caphigh
;
335 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
337 if (NT_STATUS_IS_OK(status
)) {
338 cli_set_unix_extensions_capabilities(cli
,
348 /********************************************************************
349 Return a connection to a server on a particular share.
350 ********************************************************************/
352 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
362 /* Search to the start of the list. */
363 for (p
= cli
; p
; p
= DLIST_PREV(p
)) {
364 const char *remote_name
=
365 smbXcli_conn_remote_name(p
->conn
);
367 if (strequal(server
, remote_name
) &&
368 strequal(share
,p
->share
)) {
373 /* Search to the end of the list. */
374 for (p
= cli
->next
; p
; p
= p
->next
) {
375 const char *remote_name
=
376 smbXcli_conn_remote_name(p
->conn
);
378 if (strequal(server
, remote_name
) &&
379 strequal(share
,p
->share
)) {
387 /****************************************************************************
388 Open a client connection to a \\server\share.
389 ****************************************************************************/
391 NTSTATUS
cli_cm_open(TALLOC_CTX
*ctx
,
392 struct cli_state
*referring_cli
,
395 const struct user_auth_info
*auth_info
,
401 struct cli_state
**pcli
)
403 /* Try to reuse an existing connection in this list. */
404 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
412 if (auth_info
== NULL
) {
413 /* Can't do a new connection
414 * without auth info. */
415 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
416 "without auth info\n",
418 return NT_STATUS_INVALID_PARAMETER
;
421 status
= cli_cm_connect(ctx
,
432 if (!NT_STATUS_IS_OK(status
)) {
439 /****************************************************************************
440 ****************************************************************************/
442 void cli_cm_display(struct cli_state
*cli
)
446 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
447 d_printf("%d:\tserver=%s, share=%s\n",
448 i
, smbXcli_conn_remote_name(cli
->conn
), cli
->share
);
452 /****************************************************************************
453 ****************************************************************************/
455 /****************************************************************************
456 ****************************************************************************/
459 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
461 SAFE_FREE(cm_creds
.username
);
462 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
465 if (get_cmdline_auth_info_got_pass(auth_info
)) {
466 cm_set_password(get_cmdline_auth_info_password(auth_info
));
469 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
470 cm_creds
.fallback_after_kerberos
= false;
471 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
475 /**********************************************************************
476 split a dfs path into the server, share name, and extrapath components
477 **********************************************************************/
479 static bool split_dfs_path(TALLOC_CTX
*ctx
,
480 const char *nodepath
,
490 *pp_extrapath
= NULL
;
492 path
= talloc_strdup(ctx
, nodepath
);
497 if ( path
[0] != '\\' ) {
501 p
= strchr_m( path
+ 1, '\\' );
509 /* Look for any extra/deep path */
510 q
= strchr_m(p
, '\\');
514 *pp_extrapath
= talloc_strdup(ctx
, q
);
516 *pp_extrapath
= talloc_strdup(ctx
, "");
518 if (*pp_extrapath
== NULL
) {
522 *pp_share
= talloc_strdup(ctx
, p
);
523 if (*pp_share
== NULL
) {
527 *pp_server
= talloc_strdup(ctx
, &path
[1]);
528 if (*pp_server
== NULL
) {
536 TALLOC_FREE(*pp_share
);
537 TALLOC_FREE(*pp_extrapath
);
542 /****************************************************************************
543 Return the original path truncated at the directory component before
544 the first wildcard character. Trust the caller to provide a NULL
546 ****************************************************************************/
548 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
554 /* No absolute paths. */
555 while (IS_DIRECTORY_SEP(*path
)) {
559 path_out
= talloc_strdup(ctx
, path
);
564 p1
= strchr_m(path_out
, '*');
565 p2
= strchr_m(path_out
, '?');
577 /* Now go back to the start of this component. */
578 p1
= strrchr_m(path_out
, '/');
579 p2
= strrchr_m(path_out
, '\\');
586 /* Strip any trailing separator */
588 len
= strlen(path_out
);
589 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
590 path_out
[len
-1] = '\0';
596 /****************************************************************************
597 ****************************************************************************/
599 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
600 struct cli_state
*cli
,
603 char path_sep
= '\\';
605 /* Ensure the extrapath doesn't start with a separator. */
606 while (IS_DIRECTORY_SEP(*dir
)) {
610 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
613 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
615 smbXcli_conn_remote_name(cli
->conn
),
622 /********************************************************************
623 check for dfs referral
624 ********************************************************************/
626 static bool cli_dfs_check_error(struct cli_state
*cli
, NTSTATUS expected
,
629 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
631 if (!(smbXcli_conn_use_unicode(cli
->conn
))) {
634 if (!(smb1cli_conn_capabilities(cli
->conn
) & CAP_STATUS32
)) {
637 if (NT_STATUS_EQUAL(status
, expected
)) {
643 /********************************************************************
644 Get the dfs referral link.
645 ********************************************************************/
647 NTSTATUS
cli_dfs_get_referral(TALLOC_CTX
*ctx
,
648 struct cli_state
*cli
,
650 struct client_dfs_referral
**refs
,
654 unsigned int param_len
= 0;
655 uint16_t recv_flags2
;
656 uint8_t *param
= NULL
;
657 uint8_t *rdata
= NULL
;
660 smb_ucs2_t
*path_ucs
;
661 char *consumed_path
= NULL
;
662 uint16_t consumed_ucs
;
663 uint16_t num_referrals
;
664 struct client_dfs_referral
*referrals
= NULL
;
666 TALLOC_CTX
*frame
= talloc_stackframe();
671 param
= talloc_array(talloc_tos(), uint8_t, 2);
673 status
= NT_STATUS_NO_MEMORY
;
676 SSVAL(param
, 0, 0x03); /* max referral level */
678 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
679 path
, strlen(path
)+1,
682 status
= NT_STATUS_NO_MEMORY
;
685 param_len
= talloc_get_size(param
);
686 path_ucs
= (smb_ucs2_t
*)¶m
[2];
688 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
689 DATA_BLOB in_input_buffer
;
690 DATA_BLOB in_output_buffer
= data_blob_null
;
691 DATA_BLOB out_input_buffer
= data_blob_null
;
692 DATA_BLOB out_output_buffer
= data_blob_null
;
694 in_input_buffer
.data
= param
;
695 in_input_buffer
.length
= param_len
;
697 status
= smb2cli_ioctl(cli
->conn
,
701 UINT64_MAX
, /* in_fid_persistent */
702 UINT64_MAX
, /* in_fid_volatile */
703 FSCTL_DFS_GET_REFERRALS
,
704 0, /* in_max_input_length */
706 CLI_BUFFER_SIZE
, /* in_max_output_length */
708 SMB2_IOCTL_FLAG_IS_FSCTL
,
712 if (!NT_STATUS_IS_OK(status
)) {
716 if (out_output_buffer
.length
< 4) {
717 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
721 recv_flags2
= FLAGS2_UNICODE_STRINGS
;
722 rdata
= out_output_buffer
.data
;
723 endp
= (char *)rdata
+ out_output_buffer
.length
;
725 unsigned int data_len
= 0;
728 SSVAL(setup
, 0, TRANSACT2_GET_DFS_REFERRAL
);
730 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
734 NULL
, 0, CLI_BUFFER_SIZE
,
736 NULL
, 0, NULL
, /* rsetup */
738 &rdata
, 4, &data_len
);
739 if (!NT_STATUS_IS_OK(status
)) {
743 endp
= (char *)rdata
+ data_len
;
746 consumed_ucs
= SVAL(rdata
, 0);
747 num_referrals
= SVAL(rdata
, 2);
749 /* consumed_ucs is the number of bytes
750 * of the UCS2 path consumed not counting any
751 * terminating null. We need to convert
752 * back to unix charset and count again
753 * to get the number of bytes consumed from
754 * the incoming path. */
757 if (pull_string_talloc(talloc_tos(),
765 status
= map_nt_error_from_unix(errno
);
767 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
771 if (consumed_path
== NULL
) {
772 status
= map_nt_error_from_unix(errno
);
775 *consumed
= strlen(consumed_path
);
777 if (num_referrals
!= 0) {
778 uint16_t ref_version
;
781 uint16_t node_offset
;
783 referrals
= talloc_array(ctx
, struct client_dfs_referral
,
787 status
= NT_STATUS_NO_MEMORY
;
790 /* start at the referrals array */
793 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
797 ref_version
= SVAL(p
, 0);
798 ref_size
= SVAL(p
, 2);
799 node_offset
= SVAL(p
, 16);
801 if (ref_version
!= 3) {
806 referrals
[i
].proximity
= SVAL(p
, 8);
807 referrals
[i
].ttl
= SVAL(p
, 10);
809 if (p
+ node_offset
> endp
) {
810 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
813 clistr_pull_talloc(referrals
,
816 &referrals
[i
].dfspath
,
818 PTR_DIFF(endp
, p
+node_offset
),
819 STR_TERMINATE
|STR_UNICODE
);
821 if (!referrals
[i
].dfspath
) {
822 status
= map_nt_error_from_unix(errno
);
827 if (i
< num_referrals
) {
828 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
833 *num_refs
= num_referrals
;
842 /********************************************************************
843 ********************************************************************/
844 struct cli_dfs_path_split
{
850 NTSTATUS
cli_resolve_path(TALLOC_CTX
*ctx
,
852 const struct user_auth_info
*dfs_auth_info
,
853 struct cli_state
*rootcli
,
855 struct cli_state
**targetcli
,
856 char **pp_targetpath
)
858 struct client_dfs_referral
*refs
= NULL
;
861 struct cli_state
*cli_ipc
= NULL
;
862 char *dfs_path
= NULL
;
863 char *cleanpath
= NULL
;
864 char *extrapath
= NULL
;
866 struct cli_state
*newcli
= NULL
;
867 struct cli_state
*ccli
= NULL
;
869 char *newpath
= NULL
;
870 char *newmount
= NULL
;
872 SMB_STRUCT_STAT sbuf
;
875 struct smbXcli_tcon
*root_tcon
= NULL
;
876 struct smbXcli_tcon
*target_tcon
= NULL
;
877 struct cli_dfs_path_split
*dfs_refs
= NULL
;
879 if ( !rootcli
|| !path
|| !targetcli
) {
880 return NT_STATUS_INVALID_PARAMETER
;
883 /* Don't do anything if this is not a DFS root. */
885 if (smbXcli_conn_protocol(rootcli
->conn
) >= PROTOCOL_SMB2_02
) {
886 root_tcon
= rootcli
->smb2
.tcon
;
888 root_tcon
= rootcli
->smb1
.tcon
;
891 if (!smbXcli_tcon_is_dfs_share(root_tcon
)) {
892 *targetcli
= rootcli
;
893 *pp_targetpath
= talloc_strdup(ctx
, path
);
894 if (!*pp_targetpath
) {
895 return NT_STATUS_NO_MEMORY
;
902 /* Send a trans2_query_path_info to check for a referral. */
904 cleanpath
= clean_path(ctx
, path
);
906 return NT_STATUS_NO_MEMORY
;
909 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
911 return NT_STATUS_NO_MEMORY
;
914 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
915 if (NT_STATUS_IS_OK(status
)) {
916 /* This is an ordinary path, just return it. */
917 *targetcli
= rootcli
;
918 *pp_targetpath
= talloc_strdup(ctx
, path
);
919 if (!*pp_targetpath
) {
920 return NT_STATUS_NO_MEMORY
;
925 /* Special case where client asked for a path that does not exist */
927 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
929 *targetcli
= rootcli
;
930 *pp_targetpath
= talloc_strdup(ctx
, path
);
931 if (!*pp_targetpath
) {
932 return NT_STATUS_NO_MEMORY
;
937 /* We got an error, check for DFS referral. */
939 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
944 /* Check for the referral. */
946 status
= cli_cm_open(ctx
,
948 smbXcli_conn_remote_name(rootcli
->conn
),
952 smb1cli_conn_encryption_on(rootcli
->conn
),
953 smbXcli_conn_protocol(rootcli
->conn
),
957 if (!NT_STATUS_IS_OK(status
)) {
961 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
962 &num_refs
, &consumed
);
963 if (!NT_STATUS_IS_OK(status
)) {
967 if (!num_refs
|| !refs
[0].dfspath
) {
968 return NT_STATUS_NOT_FOUND
;
972 * Bug#10123 - DFS referal entries can be provided in a random order,
973 * so check the connection cache for each item to avoid unnecessary
976 dfs_refs
= talloc_array(ctx
, struct cli_dfs_path_split
, num_refs
);
977 if (dfs_refs
== NULL
) {
978 return NT_STATUS_NO_MEMORY
;
981 for (count
= 0; count
< num_refs
; count
++) {
982 if (!split_dfs_path(dfs_refs
, refs
[count
].dfspath
,
983 &dfs_refs
[count
].server
,
984 &dfs_refs
[count
].share
,
985 &dfs_refs
[count
].extrapath
)) {
986 TALLOC_FREE(dfs_refs
);
987 return NT_STATUS_NOT_FOUND
;
990 ccli
= cli_cm_find(rootcli
, dfs_refs
[count
].server
,
991 dfs_refs
[count
].share
);
993 extrapath
= dfs_refs
[count
].extrapath
;
1000 * If no cached connection was found, then connect to the first live
1001 * referral server in the list.
1003 for (count
= 0; (ccli
== NULL
) && (count
< num_refs
); count
++) {
1004 /* Connect to the target server & share */
1005 status
= cli_cm_connect(ctx
, rootcli
,
1006 dfs_refs
[count
].server
,
1007 dfs_refs
[count
].share
,
1010 smb1cli_conn_encryption_on(rootcli
->conn
),
1011 smbXcli_conn_protocol(rootcli
->conn
),
1015 if (!NT_STATUS_IS_OK(status
)) {
1016 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1017 dfs_refs
[count
].server
,
1018 dfs_refs
[count
].share
);
1021 extrapath
= dfs_refs
[count
].extrapath
;
1026 /* No available referral server for the connection */
1027 if (*targetcli
== NULL
) {
1028 TALLOC_FREE(dfs_refs
);
1032 /* Make sure to recreate the original string including any wildcards. */
1034 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
1036 TALLOC_FREE(dfs_refs
);
1037 return NT_STATUS_NO_MEMORY
;
1039 pathlen
= strlen(dfs_path
);
1040 consumed
= MIN(pathlen
, consumed
);
1041 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
1042 if (!*pp_targetpath
) {
1043 TALLOC_FREE(dfs_refs
);
1044 return NT_STATUS_NO_MEMORY
;
1046 dfs_path
[consumed
] = '\0';
1049 * *pp_targetpath is now the unconsumed part of the path.
1050 * dfs_path is now the consumed part of the path
1051 * (in \server\share\path format).
1054 if (extrapath
&& strlen(extrapath
) > 0) {
1055 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1056 /* put the trailing \ on the path, so to be save we put one in if needed */
1057 if (extrapath
[strlen(extrapath
)-1] != '\\' && **pp_targetpath
!= '\\') {
1058 *pp_targetpath
= talloc_asprintf(ctx
,
1063 *pp_targetpath
= talloc_asprintf(ctx
,
1068 if (!*pp_targetpath
) {
1069 TALLOC_FREE(dfs_refs
);
1070 return NT_STATUS_NO_MEMORY
;
1074 /* parse out the consumed mount path */
1075 /* trim off the \server\share\ */
1079 if (*ppath
!= '\\') {
1080 d_printf("cli_resolve_path: "
1081 "dfs_path (%s) not in correct format.\n",
1083 TALLOC_FREE(dfs_refs
);
1084 return NT_STATUS_NOT_FOUND
;
1087 ppath
++; /* Now pointing at start of server name. */
1089 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
1090 TALLOC_FREE(dfs_refs
);
1091 return NT_STATUS_NOT_FOUND
;
1094 ppath
++; /* Now pointing at start of share name. */
1096 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
1097 TALLOC_FREE(dfs_refs
);
1098 return NT_STATUS_NOT_FOUND
;
1101 ppath
++; /* Now pointing at path component. */
1103 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
1105 TALLOC_FREE(dfs_refs
);
1106 return NT_STATUS_NOT_FOUND
;
1109 cli_set_mntpoint(*targetcli
, newmount
);
1111 /* Check for another dfs referral, note that we are not
1112 checking for loops here. */
1114 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1115 status
= cli_resolve_path(ctx
,
1122 if (NT_STATUS_IS_OK(status
)) {
1124 * When cli_resolve_path returns true here it's always
1125 * returning the complete path in newpath, so we're done
1128 *targetcli
= newcli
;
1129 *pp_targetpath
= newpath
;
1130 TALLOC_FREE(dfs_refs
);
1137 if (smbXcli_conn_protocol((*targetcli
)->conn
) >= PROTOCOL_SMB2_02
) {
1138 target_tcon
= (*targetcli
)->smb2
.tcon
;
1140 target_tcon
= (*targetcli
)->smb1
.tcon
;
1143 /* If returning true ensure we return a dfs root full path. */
1144 if (smbXcli_tcon_is_dfs_share(target_tcon
)) {
1145 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1147 TALLOC_FREE(dfs_refs
);
1148 return NT_STATUS_NO_MEMORY
;
1150 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1151 if (*pp_targetpath
== NULL
) {
1152 TALLOC_FREE(dfs_refs
);
1153 return NT_STATUS_NO_MEMORY
;
1157 TALLOC_FREE(dfs_refs
);
1158 return NT_STATUS_OK
;
1161 /********************************************************************
1162 ********************************************************************/
1164 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1165 struct cli_state
*cli
,
1166 const char *sharename
,
1167 char **pp_newserver
,
1170 const char *username
,
1171 const char *password
,
1174 struct client_dfs_referral
*refs
= NULL
;
1175 size_t num_refs
= 0;
1176 size_t consumed
= 0;
1177 char *fullpath
= NULL
;
1180 char *newextrapath
= NULL
;
1182 const char *remote_name
;
1184 if (!cli
|| !sharename
) {
1188 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
1189 cnum
= cli_state_get_tid(cli
);
1191 /* special case. never check for a referral on the IPC$ share */
1193 if (strequal(sharename
, "IPC$")) {
1197 /* send a trans2_query_path_info to check for a referral */
1199 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", remote_name
, sharename
);
1204 /* check for the referral */
1206 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, "IPC$", "IPC", NULL
, 0))) {
1210 if (force_encrypt
) {
1211 status
= cli_cm_force_encryption(cli
,
1216 if (!NT_STATUS_IS_OK(status
)) {
1221 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1222 &num_refs
, &consumed
);
1223 res
= NT_STATUS_IS_OK(status
);
1225 status
= cli_tdis(cli
);
1226 if (!NT_STATUS_IS_OK(status
)) {
1230 cli_state_set_tid(cli
, cnum
);
1232 if (!res
|| !num_refs
) {
1236 if (!refs
[0].dfspath
) {
1240 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1241 pp_newshare
, &newextrapath
)) {
1245 /* check that this is not a self-referral */
1247 if (strequal(remote_name
, *pp_newserver
) &&
1248 strequal(sharename
, *pp_newshare
)) {