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/>.
24 /********************************************************************
27 DFS paths are *always* of the form \server\share\<pathname> (the \ characters
28 are not C escaped here).
30 - but if we're using POSIX paths then <pathname> may contain
31 '/' separators, not '\\' separators. So cope with '\\' or '/'
32 as a separator when looking at the pathname part.... JRA.
33 ********************************************************************/
35 /********************************************************************
36 Ensure a connection is encrypted.
37 ********************************************************************/
39 NTSTATUS
cli_cm_force_encryption(struct cli_state
*c
,
43 const char *sharename
)
45 NTSTATUS status
= cli_force_encryption(c
,
50 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
)) {
51 d_printf("Encryption required and "
52 "server that doesn't support "
53 "UNIX extensions - failing connect\n");
54 } else if (NT_STATUS_EQUAL(status
,NT_STATUS_UNKNOWN_REVISION
)) {
55 d_printf("Encryption required and "
56 "can't get UNIX CIFS extensions "
57 "version from server.\n");
58 } else if (NT_STATUS_EQUAL(status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
59 d_printf("Encryption required and "
60 "share %s doesn't support "
61 "encryption.\n", sharename
);
62 } else if (!NT_STATUS_IS_OK(status
)) {
63 d_printf("Encryption required and "
64 "setup failed with error %s.\n",
71 /********************************************************************
72 Return a connection to a server.
73 ********************************************************************/
75 static struct cli_state
*do_connect(TALLOC_CTX
*ctx
,
78 const struct user_auth_info
*auth_info
,
85 struct cli_state
*c
= NULL
;
86 struct nmb_name called
, calling
;
87 const char *called_str
;
89 struct sockaddr_storage ss
;
92 char *newserver
, *newshare
;
97 /* make a copy so we don't modify the global string 'service' */
98 servicename
= talloc_strdup(ctx
,share
);
102 sharename
= servicename
;
103 if (*sharename
== '\\') {
105 called_str
= sharename
;
106 if (server
== NULL
) {
109 sharename
= strchr_m(sharename
,'\\');
123 make_nmb_name(&calling
, global_myname(), 0x0);
124 make_nmb_name(&called
, called_str
, name_type
);
129 /* have to open a new connection */
130 if (!(c
=cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info
)))) {
131 d_printf("Connection to %s failed\n", server_n
);
138 cli_set_port(c
, port
);
141 status
= cli_connect(c
, server_n
, &ss
);
142 if (!NT_STATUS_IS_OK(status
)) {
143 d_printf("Connection to %s failed (Error %s)\n",
150 if (max_protocol
== 0) {
151 max_protocol
= PROTOCOL_NT1
;
153 c
->protocol
= max_protocol
;
154 c
->use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
155 c
->fallback_after_kerberos
=
156 get_cmdline_auth_info_fallback_after_kerberos(auth_info
);
158 if (!cli_session_request(c
, &calling
, &called
)) {
160 d_printf("session request to %s failed (%s)\n",
161 called
.name
, cli_errstr(c
));
164 if ((p
=strchr_m(called
.name
, '.'))) {
168 if (strcmp(called
.name
, "*SMBSERVER")) {
169 make_nmb_name(&called
, "*SMBSERVER", 0x20);
175 DEBUG(4,(" session request ok\n"));
177 status
= cli_negprot(c
);
179 if (!NT_STATUS_IS_OK(status
)) {
180 d_printf("protocol negotiation failed: %s\n",
186 username
= get_cmdline_auth_info_username(auth_info
);
187 password
= get_cmdline_auth_info_password(auth_info
);
189 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username
,
190 password
, strlen(password
),
191 password
, strlen(password
),
193 /* If a password was not supplied then
194 * try again with a null username. */
195 if (password
[0] || !username
[0] ||
196 get_cmdline_auth_info_use_kerberos(auth_info
) ||
197 !NT_STATUS_IS_OK(cli_session_setup(c
, "",
201 d_printf("session setup failed: %s\n", cli_errstr(c
));
202 if (NT_STATUS_V(cli_nt_error(c
)) ==
203 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED
))
204 d_printf("did you forget to run kinit?\n");
208 d_printf("Anonymous login successful\n");
209 status
= cli_init_creds(c
, "", lp_workgroup(), "");
211 status
= cli_init_creds(c
, username
, lp_workgroup(), password
);
214 if (!NT_STATUS_IS_OK(status
)) {
215 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
220 if ( show_sessetup
) {
221 if (*c
->server_domain
) {
222 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
223 c
->server_domain
,c
->server_os
,c
->server_type
));
224 } else if (*c
->server_os
|| *c
->server_type
) {
225 DEBUG(0,("OS=[%s] Server=[%s]\n",
226 c
->server_os
,c
->server_type
));
229 DEBUG(4,(" session setup ok\n"));
231 /* here's the fun part....to support 'msdfs proxy' shares
232 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
233 here before trying to connect to the original share.
234 cli_check_msdfs_proxy() will fail if it is a normal share. */
236 if ((c
->capabilities
& CAP_DFS
) &&
237 cli_check_msdfs_proxy(ctx
, c
, sharename
,
238 &newserver
, &newshare
,
244 return do_connect(ctx
, newserver
,
245 newshare
, auth_info
, false,
246 force_encrypt
, max_protocol
,
250 /* must be a normal share */
252 status
= cli_tcon_andx(c
, sharename
, "?????",
253 password
, strlen(password
)+1);
254 if (!NT_STATUS_IS_OK(status
)) {
255 d_printf("tree connect failed: %s\n", nt_errstr(status
));
261 status
= cli_cm_force_encryption(c
,
266 if (!NT_STATUS_IS_OK(status
)) {
272 DEBUG(4,(" tconx ok\n"));
276 /****************************************************************************
277 ****************************************************************************/
279 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
281 char *name
= clean_name(NULL
, mnt
);
285 TALLOC_FREE(cli
->dfs_mountpoint
);
286 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
290 /********************************************************************
291 Add a new connection to the list.
292 referring_cli == NULL means a new initial connection.
293 ********************************************************************/
295 static struct cli_state
*cli_cm_connect(TALLOC_CTX
*ctx
,
296 struct cli_state
*referring_cli
,
299 const struct user_auth_info
*auth_info
,
306 struct cli_state
*cli
;
308 cli
= do_connect(ctx
, server
, share
,
310 show_hdr
, force_encrypt
, max_protocol
,
317 /* Enter into the list. */
319 DLIST_ADD_END(referring_cli
, cli
, struct cli_state
*);
322 if (referring_cli
&& referring_cli
->posix_capabilities
) {
324 uint32 caplow
, caphigh
;
326 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
328 if (NT_STATUS_IS_OK(status
)) {
329 cli_set_unix_extensions_capabilities(cli
,
338 /********************************************************************
339 Return a connection to a server on a particular share.
340 ********************************************************************/
342 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
352 /* Search to the start of the list. */
353 for (p
= cli
; p
; p
= p
->prev
) {
354 if (strequal(server
, p
->desthost
) &&
355 strequal(share
,p
->share
)) {
360 /* Search to the end of the list. */
361 for (p
= cli
->next
; p
; p
= p
->next
) {
362 if (strequal(server
, p
->desthost
) &&
363 strequal(share
,p
->share
)) {
371 /****************************************************************************
372 Open a client connection to a \\server\share.
373 ****************************************************************************/
375 struct cli_state
*cli_cm_open(TALLOC_CTX
*ctx
,
376 struct cli_state
*referring_cli
,
379 const struct user_auth_info
*auth_info
,
386 /* Try to reuse an existing connection in this list. */
387 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
393 if (auth_info
== NULL
) {
394 /* Can't do a new connection
395 * without auth info. */
396 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
397 "without auth info\n",
402 return cli_cm_connect(ctx
,
414 /****************************************************************************
415 ****************************************************************************/
417 void cli_cm_display(const struct cli_state
*cli
)
421 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
422 d_printf("%d:\tserver=%s, share=%s\n",
423 i
, cli
->desthost
, cli
->share
);
427 /****************************************************************************
428 ****************************************************************************/
430 /****************************************************************************
431 ****************************************************************************/
434 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
436 SAFE_FREE(cm_creds
.username
);
437 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
440 if (get_cmdline_auth_info_got_pass(auth_info
)) {
441 cm_set_password(get_cmdline_auth_info_password(auth_info
));
444 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
445 cm_creds
.fallback_after_kerberos
= false;
446 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
450 /**********************************************************************
451 split a dfs path into the server, share name, and extrapath components
452 **********************************************************************/
454 static void split_dfs_path(TALLOC_CTX
*ctx
,
455 const char *nodepath
,
465 *pp_extrapath
= NULL
;
467 path
= talloc_strdup(ctx
, nodepath
);
472 if ( path
[0] != '\\' ) {
476 p
= strchr_m( path
+ 1, '\\' );
484 /* Look for any extra/deep path */
485 q
= strchr_m(p
, '\\');
489 *pp_extrapath
= talloc_strdup(ctx
, q
);
491 *pp_extrapath
= talloc_strdup(ctx
, "");
494 *pp_share
= talloc_strdup(ctx
, p
);
495 *pp_server
= talloc_strdup(ctx
, &path
[1]);
498 /****************************************************************************
499 Return the original path truncated at the directory component before
500 the first wildcard character. Trust the caller to provide a NULL
502 ****************************************************************************/
504 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
510 /* No absolute paths. */
511 while (IS_DIRECTORY_SEP(*path
)) {
515 path_out
= talloc_strdup(ctx
, path
);
520 p1
= strchr_m(path_out
, '*');
521 p2
= strchr_m(path_out
, '?');
533 /* Now go back to the start of this component. */
534 p1
= strrchr_m(path_out
, '/');
535 p2
= strrchr_m(path_out
, '\\');
542 /* Strip any trailing separator */
544 len
= strlen(path_out
);
545 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
546 path_out
[len
-1] = '\0';
552 /****************************************************************************
553 ****************************************************************************/
555 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
556 struct cli_state
*cli
,
559 char path_sep
= '\\';
561 /* Ensure the extrapath doesn't start with a separator. */
562 while (IS_DIRECTORY_SEP(*dir
)) {
566 if (cli
->posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
569 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
578 /********************************************************************
579 check for dfs referral
580 ********************************************************************/
582 static bool cli_dfs_check_error( struct cli_state
*cli
, NTSTATUS status
)
584 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
586 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
588 if (!((flgs2
&FLAGS2_32_BIT_ERROR_CODES
) &&
589 (flgs2
&FLAGS2_UNICODE_STRINGS
)))
592 if (NT_STATUS_EQUAL(status
, NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
))))
598 /********************************************************************
599 Get the dfs referral link.
600 ********************************************************************/
602 bool cli_dfs_get_referral(TALLOC_CTX
*ctx
,
603 struct cli_state
*cli
,
605 CLIENT_DFS_REFERRAL
**refs
,
609 unsigned int data_len
= 0;
610 unsigned int param_len
= 0;
611 uint16 setup
= TRANSACT2_GET_DFS_REFERRAL
;
613 char *rparam
=NULL
, *rdata
=NULL
;
616 size_t pathlen
= 2*(strlen(path
)+1);
617 smb_ucs2_t
*path_ucs
;
618 char *consumed_path
= NULL
;
619 uint16_t consumed_ucs
;
620 uint16 num_referrals
;
621 CLIENT_DFS_REFERRAL
*referrals
= NULL
;
627 param
= SMB_MALLOC_ARRAY(char, 2+pathlen
+2);
631 SSVAL(param
, 0, 0x03); /* max referral level */
634 path_ucs
= (smb_ucs2_t
*)p
;
635 p
+= clistr_push(cli
, p
, path
, pathlen
, STR_TERMINATE
);
636 param_len
= PTR_DIFF(p
, param
);
638 if (!cli_send_trans(cli
, SMBtrans2
,
640 -1, 0, /* fid, flags */
641 &setup
, 1, 0, /* setup, length, max */
642 param
, param_len
, 2, /* param, length, max */
643 NULL
, 0, cli
->max_xmit
/* data, length, max */
648 if (!cli_receive_trans(cli
, SMBtrans2
,
650 &rdata
, &data_len
)) {
658 endp
= rdata
+ data_len
;
660 consumed_ucs
= SVAL(rdata
, 0);
661 num_referrals
= SVAL(rdata
, 2);
663 /* consumed_ucs is the number of bytes
664 * of the UCS2 path consumed not counting any
665 * terminating null. We need to convert
666 * back to unix charset and count again
667 * to get the number of bytes consumed from
668 * the incoming path. */
670 if (pull_string_talloc(talloc_tos(),
679 if (consumed_path
== NULL
) {
682 *consumed
= strlen(consumed_path
);
684 if (num_referrals
!= 0) {
690 referrals
= TALLOC_ARRAY(ctx
, CLIENT_DFS_REFERRAL
,
696 /* start at the referrals array */
699 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
703 ref_version
= SVAL(p
, 0);
704 ref_size
= SVAL(p
, 2);
705 node_offset
= SVAL(p
, 16);
707 if (ref_version
!= 3) {
712 referrals
[i
].proximity
= SVAL(p
, 8);
713 referrals
[i
].ttl
= SVAL(p
, 10);
715 if (p
+ node_offset
> endp
) {
718 clistr_pull_talloc(ctx
, cli
->inbuf
,
719 &referrals
[i
].dfspath
,
721 STR_TERMINATE
|STR_UNICODE
);
723 if (!referrals
[i
].dfspath
) {
728 if (i
< num_referrals
) {
735 *num_refs
= num_referrals
;
740 TALLOC_FREE(consumed_path
);
747 /********************************************************************
748 ********************************************************************/
750 bool cli_resolve_path(TALLOC_CTX
*ctx
,
752 const struct user_auth_info
*dfs_auth_info
,
753 struct cli_state
*rootcli
,
755 struct cli_state
**targetcli
,
756 char **pp_targetpath
)
758 CLIENT_DFS_REFERRAL
*refs
= NULL
;
761 struct cli_state
*cli_ipc
= NULL
;
762 char *dfs_path
= NULL
;
763 char *cleanpath
= NULL
;
764 char *extrapath
= NULL
;
768 struct cli_state
*newcli
= NULL
;
769 char *newpath
= NULL
;
770 char *newmount
= NULL
;
772 SMB_STRUCT_STAT sbuf
;
775 if ( !rootcli
|| !path
|| !targetcli
) {
779 /* Don't do anything if this is not a DFS root. */
781 if ( !rootcli
->dfsroot
) {
782 *targetcli
= rootcli
;
783 *pp_targetpath
= talloc_strdup(ctx
, path
);
784 if (!*pp_targetpath
) {
792 /* Send a trans2_query_path_info to check for a referral. */
794 cleanpath
= clean_path(ctx
, path
);
799 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
804 if (cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
)) {
805 /* This is an ordinary path, just return it. */
806 *targetcli
= rootcli
;
807 *pp_targetpath
= talloc_strdup(ctx
, path
);
808 if (!*pp_targetpath
) {
814 /* Special case where client asked for a path that does not exist */
816 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
817 *targetcli
= rootcli
;
818 *pp_targetpath
= talloc_strdup(ctx
, path
);
819 if (!*pp_targetpath
) {
825 /* We got an error, check for DFS referral. */
827 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
)) {
831 /* Check for the referral. */
833 if (!(cli_ipc
= cli_cm_open(ctx
,
839 (rootcli
->trans_enc_state
!= NULL
),
846 if (!cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
847 &num_refs
, &consumed
) || !num_refs
) {
851 /* Just store the first referral for now. */
853 if (!refs
[0].dfspath
) {
856 split_dfs_path(ctx
, refs
[0].dfspath
, &server
, &share
, &extrapath
);
858 if (!server
|| !share
) {
862 /* Make sure to recreate the original string including any wildcards. */
864 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
868 pathlen
= strlen(dfs_path
);
869 consumed
= MIN(pathlen
, consumed
);
870 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
871 if (!*pp_targetpath
) {
874 dfs_path
[consumed
] = '\0';
877 * *pp_targetpath is now the unconsumed part of the path.
878 * dfs_path is now the consumed part of the path
879 * (in \server\share\path format).
882 /* Open the connection to the target server & share */
883 if ((*targetcli
= cli_cm_open(ctx
, rootcli
,
888 (rootcli
->trans_enc_state
!= NULL
),
892 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
897 if (extrapath
&& strlen(extrapath
) > 0) {
898 *pp_targetpath
= talloc_asprintf(ctx
,
902 if (!*pp_targetpath
) {
907 /* parse out the consumed mount path */
908 /* trim off the \server\share\ */
912 if (*ppath
!= '\\') {
913 d_printf("cli_resolve_path: "
914 "dfs_path (%s) not in correct format.\n",
919 ppath
++; /* Now pointing at start of server name. */
921 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
925 ppath
++; /* Now pointing at start of share name. */
927 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
931 ppath
++; /* Now pointing at path component. */
933 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
938 cli_set_mntpoint(*targetcli
, newmount
);
940 /* Check for another dfs referral, note that we are not
941 checking for loops here. */
943 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
944 if (cli_resolve_path(ctx
,
952 * When cli_resolve_path returns true here it's always
953 * returning the complete path in newpath, so we're done
957 *pp_targetpath
= newpath
;
964 /* If returning true ensure we return a dfs root full path. */
965 if ((*targetcli
)->dfsroot
) {
966 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
970 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
976 /********************************************************************
977 ********************************************************************/
979 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
980 struct cli_state
*cli
,
981 const char *sharename
,
985 const char *username
,
986 const char *password
,
989 CLIENT_DFS_REFERRAL
*refs
= NULL
;
992 char *fullpath
= NULL
;
995 char *newextrapath
= NULL
;
997 if (!cli
|| !sharename
) {
1003 /* special case. never check for a referral on the IPC$ share */
1005 if (strequal(sharename
, "IPC$")) {
1009 /* send a trans2_query_path_info to check for a referral */
1011 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", cli
->desthost
, sharename
);
1016 /* check for the referral */
1018 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, "IPC$", "IPC", NULL
, 0))) {
1022 if (force_encrypt
) {
1023 NTSTATUS status
= cli_cm_force_encryption(cli
,
1028 if (!NT_STATUS_IS_OK(status
)) {
1033 res
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
, &num_refs
, &consumed
);
1035 if (!cli_tdis(cli
)) {
1041 if (!res
|| !num_refs
) {
1045 if (!refs
[0].dfspath
) {
1049 split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1050 pp_newshare
, &newextrapath
);
1052 if ((*pp_newserver
== NULL
) || (*pp_newshare
== NULL
)) {
1056 /* check that this is not a self-referral */
1058 if (strequal(cli
->desthost
, *pp_newserver
) &&
1059 strequal(sharename
, *pp_newshare
)) {