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_cli_minprotocol(),
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");
215 status
= cli_init_creds(c
, "", lp_workgroup(), "");
217 status
= cli_init_creds(c
, username
, domain
, password
);
220 if (!NT_STATUS_IS_OK(status
)) {
221 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
226 if ( show_sessetup
) {
227 if (*c
->server_domain
) {
228 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
229 c
->server_domain
,c
->server_os
,c
->server_type
));
230 } else if (*c
->server_os
|| *c
->server_type
) {
231 DEBUG(0,("OS=[%s] Server=[%s]\n",
232 c
->server_os
,c
->server_type
));
235 DEBUG(4,(" session setup ok\n"));
237 /* here's the fun part....to support 'msdfs proxy' shares
238 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
239 here before trying to connect to the original share.
240 cli_check_msdfs_proxy() will fail if it is a normal share. */
242 if (smbXcli_conn_dfs_supported(c
->conn
) &&
243 cli_check_msdfs_proxy(ctx
, c
, sharename
,
244 &newserver
, &newshare
,
250 return do_connect(ctx
, newserver
,
251 newshare
, auth_info
, false,
252 force_encrypt
, max_protocol
,
253 port
, name_type
, pcli
);
256 /* must be a normal share */
258 status
= cli_tree_connect(c
, sharename
, "?????",
259 password
, strlen(password
)+1);
260 if (!NT_STATUS_IS_OK(status
)) {
261 d_printf("tree connect failed: %s\n", nt_errstr(status
));
267 status
= cli_cm_force_encryption(c
,
272 if (!NT_STATUS_IS_OK(status
)) {
278 DEBUG(4,(" tconx ok\n"));
283 /****************************************************************************
284 ****************************************************************************/
286 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
288 TALLOC_CTX
*frame
= talloc_stackframe();
289 char *name
= clean_name(frame
, mnt
);
294 TALLOC_FREE(cli
->dfs_mountpoint
);
295 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
299 /********************************************************************
300 Add a new connection to the list.
301 referring_cli == NULL means a new initial connection.
302 ********************************************************************/
304 static NTSTATUS
cli_cm_connect(TALLOC_CTX
*ctx
,
305 struct cli_state
*referring_cli
,
308 const struct user_auth_info
*auth_info
,
314 struct cli_state
**pcli
)
316 struct cli_state
*cli
;
319 status
= do_connect(ctx
, server
, share
,
321 show_hdr
, force_encrypt
, max_protocol
,
322 port
, name_type
, &cli
);
324 if (!NT_STATUS_IS_OK(status
)) {
328 /* Enter into the list. */
330 DLIST_ADD_END(referring_cli
, cli
, struct cli_state
*);
333 if (referring_cli
&& referring_cli
->requested_posix_capabilities
) {
335 uint32 caplow
, caphigh
;
336 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
338 if (NT_STATUS_IS_OK(status
)) {
339 cli_set_unix_extensions_capabilities(cli
,
349 /********************************************************************
350 Return a connection to a server on a particular share.
351 ********************************************************************/
353 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
363 /* Search to the start of the list. */
364 for (p
= cli
; p
; p
= DLIST_PREV(p
)) {
365 const char *remote_name
=
366 smbXcli_conn_remote_name(p
->conn
);
368 if (strequal(server
, remote_name
) &&
369 strequal(share
,p
->share
)) {
374 /* Search to the end of the list. */
375 for (p
= cli
->next
; p
; p
= p
->next
) {
376 const char *remote_name
=
377 smbXcli_conn_remote_name(p
->conn
);
379 if (strequal(server
, remote_name
) &&
380 strequal(share
,p
->share
)) {
388 /****************************************************************************
389 Open a client connection to a \\server\share.
390 ****************************************************************************/
392 NTSTATUS
cli_cm_open(TALLOC_CTX
*ctx
,
393 struct cli_state
*referring_cli
,
396 const struct user_auth_info
*auth_info
,
402 struct cli_state
**pcli
)
404 /* Try to reuse an existing connection in this list. */
405 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
413 if (auth_info
== NULL
) {
414 /* Can't do a new connection
415 * without auth info. */
416 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
417 "without auth info\n",
419 return NT_STATUS_INVALID_PARAMETER
;
422 status
= cli_cm_connect(ctx
,
433 if (!NT_STATUS_IS_OK(status
)) {
440 /****************************************************************************
441 ****************************************************************************/
443 void cli_cm_display(struct cli_state
*cli
)
447 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
448 d_printf("%d:\tserver=%s, share=%s\n",
449 i
, smbXcli_conn_remote_name(cli
->conn
), cli
->share
);
453 /****************************************************************************
454 ****************************************************************************/
456 /****************************************************************************
457 ****************************************************************************/
460 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
462 SAFE_FREE(cm_creds
.username
);
463 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
466 if (get_cmdline_auth_info_got_pass(auth_info
)) {
467 cm_set_password(get_cmdline_auth_info_password(auth_info
));
470 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
471 cm_creds
.fallback_after_kerberos
= false;
472 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
476 /**********************************************************************
477 split a dfs path into the server, share name, and extrapath components
478 **********************************************************************/
480 static bool split_dfs_path(TALLOC_CTX
*ctx
,
481 const char *nodepath
,
491 *pp_extrapath
= NULL
;
493 path
= talloc_strdup(ctx
, nodepath
);
498 if ( path
[0] != '\\' ) {
502 p
= strchr_m( path
+ 1, '\\' );
510 /* Look for any extra/deep path */
511 q
= strchr_m(p
, '\\');
515 *pp_extrapath
= talloc_strdup(ctx
, q
);
517 *pp_extrapath
= talloc_strdup(ctx
, "");
519 if (*pp_extrapath
== NULL
) {
523 *pp_share
= talloc_strdup(ctx
, p
);
524 if (*pp_share
== NULL
) {
528 *pp_server
= talloc_strdup(ctx
, &path
[1]);
529 if (*pp_server
== NULL
) {
537 TALLOC_FREE(*pp_share
);
538 TALLOC_FREE(*pp_extrapath
);
543 /****************************************************************************
544 Return the original path truncated at the directory component before
545 the first wildcard character. Trust the caller to provide a NULL
547 ****************************************************************************/
549 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
555 /* No absolute paths. */
556 while (IS_DIRECTORY_SEP(*path
)) {
560 path_out
= talloc_strdup(ctx
, path
);
565 p1
= strchr_m(path_out
, '*');
566 p2
= strchr_m(path_out
, '?');
578 /* Now go back to the start of this component. */
579 p1
= strrchr_m(path_out
, '/');
580 p2
= strrchr_m(path_out
, '\\');
587 /* Strip any trailing separator */
589 len
= strlen(path_out
);
590 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
591 path_out
[len
-1] = '\0';
597 /****************************************************************************
598 ****************************************************************************/
600 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
601 struct cli_state
*cli
,
604 char path_sep
= '\\';
606 /* Ensure the extrapath doesn't start with a separator. */
607 while (IS_DIRECTORY_SEP(*dir
)) {
611 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
614 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
616 smbXcli_conn_remote_name(cli
->conn
),
623 /********************************************************************
624 check for dfs referral
625 ********************************************************************/
627 static bool cli_dfs_check_error(struct cli_state
*cli
, NTSTATUS expected
,
630 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
632 if (!(smbXcli_conn_use_unicode(cli
->conn
))) {
635 if (!(smb1cli_conn_capabilities(cli
->conn
) & CAP_STATUS32
)) {
638 if (NT_STATUS_EQUAL(status
, expected
)) {
644 /********************************************************************
645 Get the dfs referral link.
646 ********************************************************************/
648 NTSTATUS
cli_dfs_get_referral(TALLOC_CTX
*ctx
,
649 struct cli_state
*cli
,
651 struct client_dfs_referral
**refs
,
655 unsigned int param_len
= 0;
656 uint16_t recv_flags2
;
657 uint8_t *param
= NULL
;
658 uint8_t *rdata
= NULL
;
661 smb_ucs2_t
*path_ucs
;
662 char *consumed_path
= NULL
;
663 uint16_t consumed_ucs
;
664 uint16 num_referrals
;
665 struct client_dfs_referral
*referrals
= NULL
;
667 TALLOC_CTX
*frame
= talloc_stackframe();
672 param
= talloc_array(talloc_tos(), uint8_t, 2);
674 status
= NT_STATUS_NO_MEMORY
;
677 SSVAL(param
, 0, 0x03); /* max referral level */
679 param
= trans2_bytes_push_str(param
, smbXcli_conn_use_unicode(cli
->conn
),
680 path
, strlen(path
)+1,
683 status
= NT_STATUS_NO_MEMORY
;
686 param_len
= talloc_get_size(param
);
687 path_ucs
= (smb_ucs2_t
*)¶m
[2];
689 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
690 DATA_BLOB in_input_buffer
;
691 DATA_BLOB in_output_buffer
= data_blob_null
;
692 DATA_BLOB out_input_buffer
= data_blob_null
;
693 DATA_BLOB out_output_buffer
= data_blob_null
;
695 in_input_buffer
.data
= param
;
696 in_input_buffer
.length
= param_len
;
698 status
= smb2cli_ioctl(cli
->conn
,
702 UINT64_MAX
, /* in_fid_persistent */
703 UINT64_MAX
, /* in_fid_volatile */
704 FSCTL_DFS_GET_REFERRALS
,
705 0, /* in_max_input_length */
707 CLI_BUFFER_SIZE
, /* in_max_output_length */
709 SMB2_IOCTL_FLAG_IS_FSCTL
,
713 if (!NT_STATUS_IS_OK(status
)) {
717 if (out_output_buffer
.length
< 4) {
718 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
722 recv_flags2
= FLAGS2_UNICODE_STRINGS
;
723 rdata
= out_output_buffer
.data
;
724 endp
= (char *)rdata
+ out_output_buffer
.length
;
726 unsigned int data_len
= 0;
729 SSVAL(setup
, 0, TRANSACT2_GET_DFS_REFERRAL
);
731 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
735 NULL
, 0, CLI_BUFFER_SIZE
,
737 NULL
, 0, NULL
, /* rsetup */
739 &rdata
, 4, &data_len
);
740 if (!NT_STATUS_IS_OK(status
)) {
744 endp
= (char *)rdata
+ data_len
;
747 consumed_ucs
= SVAL(rdata
, 0);
748 num_referrals
= SVAL(rdata
, 2);
750 /* consumed_ucs is the number of bytes
751 * of the UCS2 path consumed not counting any
752 * terminating null. We need to convert
753 * back to unix charset and count again
754 * to get the number of bytes consumed from
755 * the incoming path. */
758 if (pull_string_talloc(talloc_tos(),
766 status
= map_nt_error_from_unix(errno
);
768 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
772 if (consumed_path
== NULL
) {
773 status
= map_nt_error_from_unix(errno
);
776 *consumed
= strlen(consumed_path
);
778 if (num_referrals
!= 0) {
784 referrals
= talloc_array(ctx
, struct client_dfs_referral
,
788 status
= NT_STATUS_NO_MEMORY
;
791 /* start at the referrals array */
794 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
798 ref_version
= SVAL(p
, 0);
799 ref_size
= SVAL(p
, 2);
800 node_offset
= SVAL(p
, 16);
802 if (ref_version
!= 3) {
807 referrals
[i
].proximity
= SVAL(p
, 8);
808 referrals
[i
].ttl
= SVAL(p
, 10);
810 if (p
+ node_offset
> endp
) {
811 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
814 clistr_pull_talloc(referrals
,
817 &referrals
[i
].dfspath
,
819 PTR_DIFF(endp
, p
+node_offset
),
820 STR_TERMINATE
|STR_UNICODE
);
822 if (!referrals
[i
].dfspath
) {
823 status
= map_nt_error_from_unix(errno
);
828 if (i
< num_referrals
) {
829 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
834 *num_refs
= num_referrals
;
843 /********************************************************************
844 ********************************************************************/
845 struct cli_dfs_path_split
{
851 NTSTATUS
cli_resolve_path(TALLOC_CTX
*ctx
,
853 const struct user_auth_info
*dfs_auth_info
,
854 struct cli_state
*rootcli
,
856 struct cli_state
**targetcli
,
857 char **pp_targetpath
)
859 struct client_dfs_referral
*refs
= NULL
;
862 struct cli_state
*cli_ipc
= NULL
;
863 char *dfs_path
= NULL
;
864 char *cleanpath
= NULL
;
865 char *extrapath
= NULL
;
867 struct cli_state
*newcli
= NULL
;
868 struct cli_state
*ccli
= NULL
;
870 char *newpath
= NULL
;
871 char *newmount
= NULL
;
873 SMB_STRUCT_STAT sbuf
;
876 struct smbXcli_tcon
*root_tcon
= NULL
;
877 struct smbXcli_tcon
*target_tcon
= NULL
;
878 struct cli_dfs_path_split
*dfs_refs
= NULL
;
880 if ( !rootcli
|| !path
|| !targetcli
) {
881 return NT_STATUS_INVALID_PARAMETER
;
884 /* Don't do anything if this is not a DFS root. */
886 if (smbXcli_conn_protocol(rootcli
->conn
) >= PROTOCOL_SMB2_02
) {
887 root_tcon
= rootcli
->smb2
.tcon
;
889 root_tcon
= rootcli
->smb1
.tcon
;
892 if (!smbXcli_tcon_is_dfs_share(root_tcon
)) {
893 *targetcli
= rootcli
;
894 *pp_targetpath
= talloc_strdup(ctx
, path
);
895 if (!*pp_targetpath
) {
896 return NT_STATUS_NO_MEMORY
;
903 /* Send a trans2_query_path_info to check for a referral. */
905 cleanpath
= clean_path(ctx
, path
);
907 return NT_STATUS_NO_MEMORY
;
910 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
912 return NT_STATUS_NO_MEMORY
;
915 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
916 if (NT_STATUS_IS_OK(status
)) {
917 /* This is an ordinary path, just return it. */
918 *targetcli
= rootcli
;
919 *pp_targetpath
= talloc_strdup(ctx
, path
);
920 if (!*pp_targetpath
) {
921 return NT_STATUS_NO_MEMORY
;
926 /* Special case where client asked for a path that does not exist */
928 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
930 *targetcli
= rootcli
;
931 *pp_targetpath
= talloc_strdup(ctx
, path
);
932 if (!*pp_targetpath
) {
933 return NT_STATUS_NO_MEMORY
;
938 /* We got an error, check for DFS referral. */
940 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
945 /* Check for the referral. */
947 status
= cli_cm_open(ctx
,
949 smbXcli_conn_remote_name(rootcli
->conn
),
953 smb1cli_conn_encryption_on(rootcli
->conn
),
954 smbXcli_conn_protocol(rootcli
->conn
),
958 if (!NT_STATUS_IS_OK(status
)) {
962 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
963 &num_refs
, &consumed
);
964 if (!NT_STATUS_IS_OK(status
) || !num_refs
) {
968 if (!refs
[0].dfspath
) {
969 return NT_STATUS_NOT_FOUND
;
973 * Bug#10123 - DFS referal entries can be provided in a random order,
974 * so check the connection cache for each item to avoid unnecessary
977 dfs_refs
= talloc_array(ctx
, struct cli_dfs_path_split
, num_refs
);
978 if (dfs_refs
== NULL
) {
979 return NT_STATUS_NO_MEMORY
;
982 for (count
= 0; count
< num_refs
; count
++) {
983 if (!split_dfs_path(dfs_refs
, refs
[count
].dfspath
,
984 &dfs_refs
[count
].server
,
985 &dfs_refs
[count
].share
,
986 &dfs_refs
[count
].extrapath
)) {
987 TALLOC_FREE(dfs_refs
);
988 return NT_STATUS_NOT_FOUND
;
991 ccli
= cli_cm_find(rootcli
, dfs_refs
[count
].server
,
992 dfs_refs
[count
].share
);
994 extrapath
= dfs_refs
[count
].extrapath
;
1001 * If no cached connection was found, then connect to the first live
1002 * referral server in the list.
1004 for (count
= 0; (ccli
== NULL
) && (count
< num_refs
); count
++) {
1005 /* Connect to the target server & share */
1006 status
= cli_cm_connect(ctx
, rootcli
,
1007 dfs_refs
[count
].server
,
1008 dfs_refs
[count
].share
,
1011 smb1cli_conn_encryption_on(rootcli
->conn
),
1012 smbXcli_conn_protocol(rootcli
->conn
),
1016 if (!NT_STATUS_IS_OK(status
)) {
1017 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
1018 dfs_refs
[count
].server
,
1019 dfs_refs
[count
].share
);
1022 extrapath
= dfs_refs
[count
].extrapath
;
1027 /* No available referral server for the connection */
1028 if (*targetcli
== NULL
) {
1029 TALLOC_FREE(dfs_refs
);
1033 /* Make sure to recreate the original string including any wildcards. */
1035 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
1037 TALLOC_FREE(dfs_refs
);
1038 return NT_STATUS_NO_MEMORY
;
1040 pathlen
= strlen(dfs_path
);
1041 consumed
= MIN(pathlen
, consumed
);
1042 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
1043 if (!*pp_targetpath
) {
1044 TALLOC_FREE(dfs_refs
);
1045 return NT_STATUS_NO_MEMORY
;
1047 dfs_path
[consumed
] = '\0';
1050 * *pp_targetpath is now the unconsumed part of the path.
1051 * dfs_path is now the consumed part of the path
1052 * (in \server\share\path format).
1055 if (extrapath
&& strlen(extrapath
) > 0) {
1056 /* EMC Celerra NAS version 5.6.50 (at least) doesn't appear to */
1057 /* put the trailing \ on the path, so to be save we put one in if needed */
1058 if (extrapath
[strlen(extrapath
)-1] != '\\' && **pp_targetpath
!= '\\') {
1059 *pp_targetpath
= talloc_asprintf(ctx
,
1064 *pp_targetpath
= talloc_asprintf(ctx
,
1069 if (!*pp_targetpath
) {
1070 TALLOC_FREE(dfs_refs
);
1071 return NT_STATUS_NO_MEMORY
;
1075 /* parse out the consumed mount path */
1076 /* trim off the \server\share\ */
1080 if (*ppath
!= '\\') {
1081 d_printf("cli_resolve_path: "
1082 "dfs_path (%s) not in correct format.\n",
1084 TALLOC_FREE(dfs_refs
);
1085 return NT_STATUS_NOT_FOUND
;
1088 ppath
++; /* Now pointing at start of server name. */
1090 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
1091 TALLOC_FREE(dfs_refs
);
1092 return NT_STATUS_NOT_FOUND
;
1095 ppath
++; /* Now pointing at start of share name. */
1097 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
1098 TALLOC_FREE(dfs_refs
);
1099 return NT_STATUS_NOT_FOUND
;
1102 ppath
++; /* Now pointing at path component. */
1104 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
1106 TALLOC_FREE(dfs_refs
);
1107 return NT_STATUS_NOT_FOUND
;
1110 cli_set_mntpoint(*targetcli
, newmount
);
1112 /* Check for another dfs referral, note that we are not
1113 checking for loops here. */
1115 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1116 status
= cli_resolve_path(ctx
,
1123 if (NT_STATUS_IS_OK(status
)) {
1125 * When cli_resolve_path returns true here it's always
1126 * returning the complete path in newpath, so we're done
1129 *targetcli
= newcli
;
1130 *pp_targetpath
= newpath
;
1131 TALLOC_FREE(dfs_refs
);
1138 if (smbXcli_conn_protocol((*targetcli
)->conn
) >= PROTOCOL_SMB2_02
) {
1139 target_tcon
= (*targetcli
)->smb2
.tcon
;
1141 target_tcon
= (*targetcli
)->smb1
.tcon
;
1144 /* If returning true ensure we return a dfs root full path. */
1145 if (smbXcli_tcon_is_dfs_share(target_tcon
)) {
1146 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1148 TALLOC_FREE(dfs_refs
);
1149 return NT_STATUS_NO_MEMORY
;
1151 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1152 if (*pp_targetpath
== NULL
) {
1153 TALLOC_FREE(dfs_refs
);
1154 return NT_STATUS_NO_MEMORY
;
1158 TALLOC_FREE(dfs_refs
);
1159 return NT_STATUS_OK
;
1162 /********************************************************************
1163 ********************************************************************/
1165 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1166 struct cli_state
*cli
,
1167 const char *sharename
,
1168 char **pp_newserver
,
1171 const char *username
,
1172 const char *password
,
1175 struct client_dfs_referral
*refs
= NULL
;
1176 size_t num_refs
= 0;
1177 size_t consumed
= 0;
1178 char *fullpath
= NULL
;
1181 char *newextrapath
= NULL
;
1183 const char *remote_name
;
1185 if (!cli
|| !sharename
) {
1189 remote_name
= smbXcli_conn_remote_name(cli
->conn
);
1190 cnum
= cli_state_get_tid(cli
);
1192 /* special case. never check for a referral on the IPC$ share */
1194 if (strequal(sharename
, "IPC$")) {
1198 /* send a trans2_query_path_info to check for a referral */
1200 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", remote_name
, sharename
);
1205 /* check for the referral */
1207 if (!NT_STATUS_IS_OK(cli_tree_connect(cli
, "IPC$", "IPC", NULL
, 0))) {
1211 if (force_encrypt
) {
1212 status
= cli_cm_force_encryption(cli
,
1217 if (!NT_STATUS_IS_OK(status
)) {
1222 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1223 &num_refs
, &consumed
);
1224 res
= NT_STATUS_IS_OK(status
);
1226 status
= cli_tdis(cli
);
1227 if (!NT_STATUS_IS_OK(status
)) {
1231 cli_state_set_tid(cli
, cnum
);
1233 if (!res
|| !num_refs
) {
1237 if (!refs
[0].dfspath
) {
1241 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1242 pp_newshare
, &newextrapath
)) {
1246 /* check that this is not a self-referral */
1248 if (strequal(remote_name
, *pp_newserver
) &&
1249 strequal(sharename
, *pp_newshare
)) {