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 c
= cli_initialise_ex(get_cmdline_auth_info_signing_state(auth_info
));
132 d_printf("Connection to %s failed\n", server_n
);
136 cli_set_port(c
, port
);
139 status
= cli_connect(c
, server_n
, &ss
);
140 if (!NT_STATUS_IS_OK(status
)) {
141 d_printf("Connection to %s failed (Error %s)\n",
148 if (max_protocol
== 0) {
149 max_protocol
= PROTOCOL_NT1
;
151 c
->protocol
= max_protocol
;
152 c
->use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
153 c
->fallback_after_kerberos
=
154 get_cmdline_auth_info_fallback_after_kerberos(auth_info
);
155 c
->use_ccache
= get_cmdline_auth_info_use_ccache(auth_info
);
157 if (!cli_session_request(c
, &calling
, &called
)) {
159 d_printf("session request to %s failed (%s)\n",
160 called
.name
, cli_errstr(c
));
163 if ((p
=strchr_m(called
.name
, '.'))) {
167 if (strcmp(called
.name
, "*SMBSERVER")) {
168 make_nmb_name(&called
, "*SMBSERVER", 0x20);
174 DEBUG(4,(" session request ok\n"));
176 status
= cli_negprot(c
);
178 if (!NT_STATUS_IS_OK(status
)) {
179 d_printf("protocol negotiation failed: %s\n",
185 username
= get_cmdline_auth_info_username(auth_info
);
186 password
= get_cmdline_auth_info_password(auth_info
);
188 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username
,
189 password
, strlen(password
),
190 password
, strlen(password
),
192 /* If a password was not supplied then
193 * try again with a null username. */
194 if (password
[0] || !username
[0] ||
195 get_cmdline_auth_info_use_kerberos(auth_info
) ||
196 !NT_STATUS_IS_OK(cli_session_setup(c
, "",
200 d_printf("session setup failed: %s\n", cli_errstr(c
));
201 if (NT_STATUS_V(cli_nt_error(c
)) ==
202 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED
))
203 d_printf("did you forget to run kinit?\n");
207 d_printf("Anonymous login successful\n");
208 status
= cli_init_creds(c
, "", lp_workgroup(), "");
210 status
= cli_init_creds(c
, username
, lp_workgroup(), password
);
213 if (!NT_STATUS_IS_OK(status
)) {
214 DEBUG(10,("cli_init_creds() failed: %s\n", nt_errstr(status
)));
219 if ( show_sessetup
) {
220 if (*c
->server_domain
) {
221 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
222 c
->server_domain
,c
->server_os
,c
->server_type
));
223 } else if (*c
->server_os
|| *c
->server_type
) {
224 DEBUG(0,("OS=[%s] Server=[%s]\n",
225 c
->server_os
,c
->server_type
));
228 DEBUG(4,(" session setup ok\n"));
230 /* here's the fun part....to support 'msdfs proxy' shares
231 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
232 here before trying to connect to the original share.
233 cli_check_msdfs_proxy() will fail if it is a normal share. */
235 if ((c
->capabilities
& CAP_DFS
) &&
236 cli_check_msdfs_proxy(ctx
, c
, sharename
,
237 &newserver
, &newshare
,
243 return do_connect(ctx
, newserver
,
244 newshare
, auth_info
, false,
245 force_encrypt
, max_protocol
,
249 /* must be a normal share */
251 status
= cli_tcon_andx(c
, sharename
, "?????",
252 password
, strlen(password
)+1);
253 if (!NT_STATUS_IS_OK(status
)) {
254 d_printf("tree connect failed: %s\n", nt_errstr(status
));
260 status
= cli_cm_force_encryption(c
,
265 if (!NT_STATUS_IS_OK(status
)) {
271 DEBUG(4,(" tconx ok\n"));
275 /****************************************************************************
276 ****************************************************************************/
278 static void cli_set_mntpoint(struct cli_state
*cli
, const char *mnt
)
280 char *name
= clean_name(NULL
, mnt
);
284 TALLOC_FREE(cli
->dfs_mountpoint
);
285 cli
->dfs_mountpoint
= talloc_strdup(cli
, name
);
289 /********************************************************************
290 Add a new connection to the list.
291 referring_cli == NULL means a new initial connection.
292 ********************************************************************/
294 static struct cli_state
*cli_cm_connect(TALLOC_CTX
*ctx
,
295 struct cli_state
*referring_cli
,
298 const struct user_auth_info
*auth_info
,
305 struct cli_state
*cli
;
307 cli
= do_connect(ctx
, server
, share
,
309 show_hdr
, force_encrypt
, max_protocol
,
316 /* Enter into the list. */
318 DLIST_ADD_END(referring_cli
, cli
, struct cli_state
*);
321 if (referring_cli
&& referring_cli
->requested_posix_capabilities
) {
323 uint32 caplow
, caphigh
;
325 status
= cli_unix_extensions_version(cli
, &major
, &minor
,
327 if (NT_STATUS_IS_OK(status
)) {
328 cli_set_unix_extensions_capabilities(cli
,
337 /********************************************************************
338 Return a connection to a server on a particular share.
339 ********************************************************************/
341 static struct cli_state
*cli_cm_find(struct cli_state
*cli
,
351 /* Search to the start of the list. */
352 for (p
= cli
; p
; p
= DLIST_PREV(p
)) {
353 if (strequal(server
, p
->desthost
) &&
354 strequal(share
,p
->share
)) {
359 /* Search to the end of the list. */
360 for (p
= cli
->next
; p
; p
= p
->next
) {
361 if (strequal(server
, p
->desthost
) &&
362 strequal(share
,p
->share
)) {
370 /****************************************************************************
371 Open a client connection to a \\server\share.
372 ****************************************************************************/
374 struct cli_state
*cli_cm_open(TALLOC_CTX
*ctx
,
375 struct cli_state
*referring_cli
,
378 const struct user_auth_info
*auth_info
,
385 /* Try to reuse an existing connection in this list. */
386 struct cli_state
*c
= cli_cm_find(referring_cli
, server
, share
);
392 if (auth_info
== NULL
) {
393 /* Can't do a new connection
394 * without auth info. */
395 d_printf("cli_cm_open() Unable to open connection [\\%s\\%s] "
396 "without auth info\n",
401 return cli_cm_connect(ctx
,
413 /****************************************************************************
414 ****************************************************************************/
416 void cli_cm_display(const struct cli_state
*cli
)
420 for (i
=0; cli
; cli
= cli
->next
,i
++ ) {
421 d_printf("%d:\tserver=%s, share=%s\n",
422 i
, cli
->desthost
, cli
->share
);
426 /****************************************************************************
427 ****************************************************************************/
429 /****************************************************************************
430 ****************************************************************************/
433 void cli_cm_set_credentials(struct user_auth_info
*auth_info
)
435 SAFE_FREE(cm_creds
.username
);
436 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username(
439 if (get_cmdline_auth_info_got_pass(auth_info
)) {
440 cm_set_password(get_cmdline_auth_info_password(auth_info
));
443 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos(auth_info
);
444 cm_creds
.fallback_after_kerberos
= false;
445 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state(auth_info
);
449 /**********************************************************************
450 split a dfs path into the server, share name, and extrapath components
451 **********************************************************************/
453 static bool split_dfs_path(TALLOC_CTX
*ctx
,
454 const char *nodepath
,
464 *pp_extrapath
= NULL
;
466 path
= talloc_strdup(ctx
, nodepath
);
471 if ( path
[0] != '\\' ) {
475 p
= strchr_m( path
+ 1, '\\' );
483 /* Look for any extra/deep path */
484 q
= strchr_m(p
, '\\');
488 *pp_extrapath
= talloc_strdup(ctx
, q
);
490 *pp_extrapath
= talloc_strdup(ctx
, "");
492 if (*pp_extrapath
== NULL
) {
496 *pp_share
= talloc_strdup(ctx
, p
);
497 if (*pp_share
== NULL
) {
501 *pp_server
= talloc_strdup(ctx
, &path
[1]);
502 if (*pp_server
== NULL
) {
510 TALLOC_FREE(*pp_share
);
511 TALLOC_FREE(*pp_extrapath
);
516 /****************************************************************************
517 Return the original path truncated at the directory component before
518 the first wildcard character. Trust the caller to provide a NULL
520 ****************************************************************************/
522 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
528 /* No absolute paths. */
529 while (IS_DIRECTORY_SEP(*path
)) {
533 path_out
= talloc_strdup(ctx
, path
);
538 p1
= strchr_m(path_out
, '*');
539 p2
= strchr_m(path_out
, '?');
551 /* Now go back to the start of this component. */
552 p1
= strrchr_m(path_out
, '/');
553 p2
= strrchr_m(path_out
, '\\');
560 /* Strip any trailing separator */
562 len
= strlen(path_out
);
563 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
564 path_out
[len
-1] = '\0';
570 /****************************************************************************
571 ****************************************************************************/
573 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
574 struct cli_state
*cli
,
577 char path_sep
= '\\';
579 /* Ensure the extrapath doesn't start with a separator. */
580 while (IS_DIRECTORY_SEP(*dir
)) {
584 if (cli
->requested_posix_capabilities
& CIFS_UNIX_POSIX_PATHNAMES_CAP
) {
587 return talloc_asprintf(ctx
, "%c%s%c%s%c%s",
596 /********************************************************************
597 check for dfs referral
598 ********************************************************************/
600 static bool cli_dfs_check_error(struct cli_state
*cli
, NTSTATUS expected
,
603 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
605 if (!(cli
->capabilities
& CAP_UNICODE
)) {
608 if (!(cli
->capabilities
& CAP_STATUS32
)) {
611 if (NT_STATUS_EQUAL(status
, expected
)) {
617 /********************************************************************
618 Get the dfs referral link.
619 ********************************************************************/
621 NTSTATUS
cli_dfs_get_referral(TALLOC_CTX
*ctx
,
622 struct cli_state
*cli
,
624 struct client_dfs_referral
**refs
,
628 unsigned int data_len
= 0;
629 unsigned int param_len
= 0;
631 uint8_t *param
= NULL
;
632 uint8_t *rdata
= NULL
;
635 size_t pathlen
= 2*(strlen(path
)+1);
636 smb_ucs2_t
*path_ucs
;
637 char *consumed_path
= NULL
;
638 uint16_t consumed_ucs
;
639 uint16 num_referrals
;
640 struct client_dfs_referral
*referrals
= NULL
;
646 SSVAL(setup
, 0, TRANSACT2_GET_DFS_REFERRAL
);
648 param
= SMB_MALLOC_ARRAY(uint8_t, 2+pathlen
+2);
650 status
= NT_STATUS_NO_MEMORY
;
653 SSVAL(param
, 0, 0x03); /* max referral level */
654 p
= (char *)(¶m
[2]);
656 path_ucs
= (smb_ucs2_t
*)p
;
657 p
+= clistr_push(cli
, p
, path
, pathlen
, STR_TERMINATE
);
658 param_len
= PTR_DIFF(p
, param
);
660 status
= cli_trans(talloc_tos(), cli
, SMBtrans2
,
664 NULL
, 0, cli
->max_xmit
,
666 NULL
, 0, NULL
, /* rsetup */
668 &rdata
, 4, &data_len
);
669 if (!NT_STATUS_IS_OK(status
)) {
676 endp
= (char *)rdata
+ data_len
;
678 consumed_ucs
= SVAL(rdata
, 0);
679 num_referrals
= SVAL(rdata
, 2);
681 /* consumed_ucs is the number of bytes
682 * of the UCS2 path consumed not counting any
683 * terminating null. We need to convert
684 * back to unix charset and count again
685 * to get the number of bytes consumed from
686 * the incoming path. */
688 if (pull_string_talloc(talloc_tos(),
697 if (consumed_path
== NULL
) {
700 *consumed
= strlen(consumed_path
);
702 if (num_referrals
!= 0) {
708 referrals
= talloc_array(ctx
, struct client_dfs_referral
,
714 /* start at the referrals array */
717 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
721 ref_version
= SVAL(p
, 0);
722 ref_size
= SVAL(p
, 2);
723 node_offset
= SVAL(p
, 16);
725 if (ref_version
!= 3) {
730 referrals
[i
].proximity
= SVAL(p
, 8);
731 referrals
[i
].ttl
= SVAL(p
, 10);
733 if (p
+ node_offset
> endp
) {
736 clistr_pull_talloc(ctx
, cli
->inbuf
,
737 SVAL(cli
->inbuf
, smb_flg2
),
738 &referrals
[i
].dfspath
,
740 STR_TERMINATE
|STR_UNICODE
);
742 if (!referrals
[i
].dfspath
) {
747 if (i
< num_referrals
) {
752 *num_refs
= num_referrals
;
757 TALLOC_FREE(consumed_path
);
763 /********************************************************************
764 ********************************************************************/
766 bool cli_resolve_path(TALLOC_CTX
*ctx
,
768 const struct user_auth_info
*dfs_auth_info
,
769 struct cli_state
*rootcli
,
771 struct cli_state
**targetcli
,
772 char **pp_targetpath
)
774 struct client_dfs_referral
*refs
= NULL
;
777 struct cli_state
*cli_ipc
= NULL
;
778 char *dfs_path
= NULL
;
779 char *cleanpath
= NULL
;
780 char *extrapath
= NULL
;
784 struct cli_state
*newcli
= NULL
;
785 char *newpath
= NULL
;
786 char *newmount
= NULL
;
788 SMB_STRUCT_STAT sbuf
;
792 if ( !rootcli
|| !path
|| !targetcli
) {
796 /* Don't do anything if this is not a DFS root. */
798 if ( !rootcli
->dfsroot
) {
799 *targetcli
= rootcli
;
800 *pp_targetpath
= talloc_strdup(ctx
, path
);
801 if (!*pp_targetpath
) {
809 /* Send a trans2_query_path_info to check for a referral. */
811 cleanpath
= clean_path(ctx
, path
);
816 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
821 status
= cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
);
822 if (NT_STATUS_IS_OK(status
)) {
823 /* This is an ordinary path, just return it. */
824 *targetcli
= rootcli
;
825 *pp_targetpath
= talloc_strdup(ctx
, path
);
826 if (!*pp_targetpath
) {
832 /* Special case where client asked for a path that does not exist */
834 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
836 *targetcli
= rootcli
;
837 *pp_targetpath
= talloc_strdup(ctx
, path
);
838 if (!*pp_targetpath
) {
844 /* We got an error, check for DFS referral. */
846 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
,
851 /* Check for the referral. */
853 if (!(cli_ipc
= cli_cm_open(ctx
,
859 (rootcli
->trans_enc_state
!= NULL
),
866 status
= cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
867 &num_refs
, &consumed
);
868 if (!NT_STATUS_IS_OK(status
) || !num_refs
) {
872 /* Just store the first referral for now. */
874 if (!refs
[0].dfspath
) {
877 if (!split_dfs_path(ctx
, refs
[0].dfspath
, &server
, &share
,
882 /* Make sure to recreate the original string including any wildcards. */
884 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
888 pathlen
= strlen(dfs_path
);
889 consumed
= MIN(pathlen
, consumed
);
890 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
]);
891 if (!*pp_targetpath
) {
894 dfs_path
[consumed
] = '\0';
897 * *pp_targetpath is now the unconsumed part of the path.
898 * dfs_path is now the consumed part of the path
899 * (in \server\share\path format).
902 /* Open the connection to the target server & share */
903 if ((*targetcli
= cli_cm_open(ctx
, rootcli
,
908 (rootcli
->trans_enc_state
!= NULL
),
912 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
917 if (extrapath
&& strlen(extrapath
) > 0) {
918 *pp_targetpath
= talloc_asprintf(ctx
,
922 if (!*pp_targetpath
) {
927 /* parse out the consumed mount path */
928 /* trim off the \server\share\ */
932 if (*ppath
!= '\\') {
933 d_printf("cli_resolve_path: "
934 "dfs_path (%s) not in correct format.\n",
939 ppath
++; /* Now pointing at start of server name. */
941 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
945 ppath
++; /* Now pointing at start of share name. */
947 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
951 ppath
++; /* Now pointing at path component. */
953 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
958 cli_set_mntpoint(*targetcli
, newmount
);
960 /* Check for another dfs referral, note that we are not
961 checking for loops here. */
963 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
964 if (cli_resolve_path(ctx
,
972 * When cli_resolve_path returns true here it's always
973 * returning the complete path in newpath, so we're done
977 *pp_targetpath
= newpath
;
984 /* If returning true ensure we return a dfs root full path. */
985 if ((*targetcli
)->dfsroot
) {
986 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
990 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
996 /********************************************************************
997 ********************************************************************/
999 bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1000 struct cli_state
*cli
,
1001 const char *sharename
,
1002 char **pp_newserver
,
1005 const char *username
,
1006 const char *password
,
1009 struct client_dfs_referral
*refs
= NULL
;
1010 size_t num_refs
= 0;
1011 size_t consumed
= 0;
1012 char *fullpath
= NULL
;
1015 char *newextrapath
= NULL
;
1018 if (!cli
|| !sharename
) {
1024 /* special case. never check for a referral on the IPC$ share */
1026 if (strequal(sharename
, "IPC$")) {
1030 /* send a trans2_query_path_info to check for a referral */
1032 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", cli
->desthost
, sharename
);
1037 /* check for the referral */
1039 if (!NT_STATUS_IS_OK(cli_tcon_andx(cli
, "IPC$", "IPC", NULL
, 0))) {
1043 if (force_encrypt
) {
1044 status
= cli_cm_force_encryption(cli
,
1049 if (!NT_STATUS_IS_OK(status
)) {
1054 status
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
,
1055 &num_refs
, &consumed
);
1056 res
= NT_STATUS_IS_OK(status
);
1058 status
= cli_tdis(cli
);
1059 if (!NT_STATUS_IS_OK(status
)) {
1065 if (!res
|| !num_refs
) {
1069 if (!refs
[0].dfspath
) {
1073 if (!split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1074 pp_newshare
, &newextrapath
)) {
1078 /* check that this is not a self-referral */
1080 if (strequal(cli
->desthost
, *pp_newserver
) &&
1081 strequal(sharename
, *pp_newshare
)) {