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
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 struct client_connection
{
36 struct client_connection
*prev
, *next
;
37 struct cli_state
*cli
;
41 /* global state....globals reek! */
42 int max_protocol
= PROTOCOL_NT1
;
44 static struct cm_cred_struct
{
49 bool fallback_after_kerberos
;
53 static void cm_set_password(const char *newpass
);
56 static int name_type
= 0x20;
58 static struct sockaddr_storage dest_ss
;
60 static struct client_connection
*connections
;
62 static bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
63 struct cli_state
*cli
,
64 const char *sharename
,
72 /********************************************************************
73 Ensure a connection is encrypted.
74 ********************************************************************/
76 NTSTATUS
cli_cm_force_encryption(struct cli_state
*c
,
80 const char *sharename
)
82 NTSTATUS status
= cli_force_encryption(c
,
87 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_SUPPORTED
)) {
88 d_printf("Encryption required and "
89 "server that doesn't support "
90 "UNIX extensions - failing connect\n");
91 } else if (NT_STATUS_EQUAL(status
,NT_STATUS_UNKNOWN_REVISION
)) {
92 d_printf("Encryption required and "
93 "can't get UNIX CIFS extensions "
94 "version from server.\n");
95 } else if (NT_STATUS_EQUAL(status
,NT_STATUS_UNSUPPORTED_COMPRESSION
)) {
96 d_printf("Encryption required and "
97 "share %s doesn't support "
98 "encryption.\n", sharename
);
99 } else if (!NT_STATUS_IS_OK(status
)) {
100 d_printf("Encryption required and "
101 "setup failed with error %s.\n",
108 /********************************************************************
109 Return a connection to a server.
110 ********************************************************************/
112 static struct cli_state
*do_connect(TALLOC_CTX
*ctx
,
118 struct cli_state
*c
= NULL
;
119 struct nmb_name called
, calling
;
120 const char *server_n
;
121 struct sockaddr_storage ss
;
124 char *newserver
, *newshare
;
125 const char *username
;
126 const char *password
;
129 /* make a copy so we don't modify the global string 'service' */
130 servicename
= talloc_strdup(ctx
,share
);
134 sharename
= servicename
;
135 if (*sharename
== '\\') {
136 server
= sharename
+2;
137 sharename
= strchr_m(server
,'\\');
149 make_nmb_name(&calling
, global_myname(), 0x0);
150 make_nmb_name(&called
, server
, name_type
);
157 /* have to open a new connection */
158 if (!(c
=cli_initialise()) || (cli_set_port(c
, port
) != port
)) {
159 d_printf("Connection to %s failed\n", server_n
);
165 status
= cli_connect(c
, server_n
, &ss
);
166 if (!NT_STATUS_IS_OK(status
)) {
167 d_printf("Connection to %s failed (Error %s)\n",
174 c
->protocol
= max_protocol
;
175 c
->use_kerberos
= cm_creds
.use_kerberos
;
176 c
->fallback_after_kerberos
= cm_creds
.fallback_after_kerberos
;
177 cli_setup_signing_state(c
, cm_creds
.signing_state
);
179 if (!cli_session_request(c
, &calling
, &called
)) {
181 d_printf("session request to %s failed (%s)\n",
182 called
.name
, cli_errstr(c
));
185 if ((p
=strchr_m(called
.name
, '.'))) {
189 if (strcmp(called
.name
, "*SMBSERVER")) {
190 make_nmb_name(&called
, "*SMBSERVER", 0x20);
196 DEBUG(4,(" session request ok\n"));
198 if (!cli_negprot(c
)) {
199 d_printf("protocol negotiation failed\n");
204 if (!cm_creds
.got_pass
&& !cm_creds
.use_kerberos
) {
205 char *pass
= getpass("Password: ");
207 cm_set_password(pass
);
211 username
= cm_creds
.username
? cm_creds
.username
: "";
212 password
= cm_creds
.password
? cm_creds
.password
: "";
214 if (!NT_STATUS_IS_OK(cli_session_setup(c
, username
,
215 password
, strlen(password
),
216 password
, strlen(password
),
218 /* If a password was not supplied then
219 * try again with a null username. */
220 if (password
[0] || !username
[0] || cm_creds
.use_kerberos
||
221 !NT_STATUS_IS_OK(cli_session_setup(c
, "",
225 d_printf("session setup failed: %s\n", cli_errstr(c
));
226 if (NT_STATUS_V(cli_nt_error(c
)) ==
227 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED
))
228 d_printf("did you forget to run kinit?\n");
232 d_printf("Anonymous login successful\n");
235 if ( show_sessetup
) {
236 if (*c
->server_domain
) {
237 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
238 c
->server_domain
,c
->server_os
,c
->server_type
));
239 } else if (*c
->server_os
|| *c
->server_type
) {
240 DEBUG(0,("OS=[%s] Server=[%s]\n",
241 c
->server_os
,c
->server_type
));
244 DEBUG(4,(" session setup ok\n"));
246 /* here's the fun part....to support 'msdfs proxy' shares
247 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
248 here before trying to connect to the original share.
249 check_dfs_proxy() will fail if it is a normal share. */
251 if ((c
->capabilities
& CAP_DFS
) &&
252 cli_check_msdfs_proxy(ctx
, c
, sharename
,
253 &newserver
, &newshare
,
259 return do_connect(ctx
, newserver
,
260 newshare
, false, force_encrypt
);
263 /* must be a normal share */
265 if (!cli_send_tconX(c
, sharename
, "?????",
266 password
, strlen(password
)+1)) {
267 d_printf("tree connect failed: %s\n", cli_errstr(c
));
273 status
= cli_cm_force_encryption(c
,
278 if (!NT_STATUS_IS_OK(status
)) {
284 DEBUG(4,(" tconx ok\n"));
288 /****************************************************************************
289 ****************************************************************************/
291 static void cli_cm_set_mntpoint(struct cli_state
*c
, const char *mnt
)
293 struct client_connection
*p
;
296 for (p
=connections
,i
=0; p
; p
=p
->next
,i
++) {
297 if (strequal(p
->cli
->desthost
, c
->desthost
) &&
298 strequal(p
->cli
->share
, c
->share
)) {
304 char *name
= clean_name(NULL
, p
->mount
);
308 p
->mount
= talloc_strdup(p
, name
);
313 /****************************************************************************
314 ****************************************************************************/
316 const char *cli_cm_get_mntpoint(struct cli_state
*c
)
318 struct client_connection
*p
;
321 for (p
=connections
,i
=0; p
; p
=p
->next
,i
++) {
322 if (strequal(p
->cli
->desthost
, c
->desthost
) &&
323 strequal(p
->cli
->share
, c
->share
)) {
334 /********************************************************************
335 Add a new connection to the list
336 ********************************************************************/
338 static struct cli_state
*cli_cm_connect(TALLOC_CTX
*ctx
,
339 struct cli_state
*referring_cli
,
345 struct client_connection
*node
;
347 /* NB This must be the null context here... JRA. */
348 node
= TALLOC_ZERO_ARRAY(NULL
, struct client_connection
, 1);
353 node
->cli
= do_connect(ctx
, server
, share
, show_hdr
, force_encrypt
);
360 DLIST_ADD( connections
, node
);
362 cli_cm_set_mntpoint(node
->cli
, "");
364 if (referring_cli
&& referring_cli
->posix_capabilities
) {
366 uint32 caplow
, caphigh
;
367 if (cli_unix_extensions_version(node
->cli
, &major
,
368 &minor
, &caplow
, &caphigh
)) {
369 cli_set_unix_extensions_capabilities(node
->cli
,
378 /********************************************************************
379 Return a connection to a server.
380 ********************************************************************/
382 static struct cli_state
*cli_cm_find(const char *server
, const char *share
)
384 struct client_connection
*p
;
386 for (p
=connections
; p
; p
=p
->next
) {
387 if ( strequal(server
, p
->cli
->desthost
) &&
388 strequal(share
,p
->cli
->share
)) {
396 /****************************************************************************
397 Open a client connection to a \\server\share. Set's the current *cli
398 global variable as a side-effect (but only if the connection is successful).
399 ****************************************************************************/
401 struct cli_state
*cli_cm_open(TALLOC_CTX
*ctx
,
402 struct cli_state
*referring_cli
,
410 /* try to reuse an existing connection */
412 c
= cli_cm_find(server
, share
);
414 c
= cli_cm_connect(ctx
, referring_cli
,
415 server
, share
, show_hdr
, force_encrypt
);
421 /****************************************************************************
422 ****************************************************************************/
424 void cli_cm_shutdown(void)
426 struct client_connection
*p
, *x
;
428 for (p
=connections
; p
;) {
429 cli_shutdown(p
->cli
);
440 /****************************************************************************
441 ****************************************************************************/
443 void cli_cm_display(void)
445 struct client_connection
*p
;
448 for ( p
=connections
,i
=0; p
; p
=p
->next
,i
++ ) {
449 d_printf("%d:\tserver=%s, share=%s\n",
450 i
, p
->cli
->desthost
, p
->cli
->share
);
454 /****************************************************************************
455 ****************************************************************************/
457 static void cm_set_password(const char *newpass
)
459 SAFE_FREE(cm_creds
.password
);
460 cm_creds
.password
= SMB_STRDUP(newpass
);
461 if (cm_creds
.password
) {
462 cm_creds
.got_pass
= true;
466 /****************************************************************************
467 ****************************************************************************/
469 void cli_cm_set_credentials(void)
471 SAFE_FREE(cm_creds
.username
);
472 cm_creds
.username
= SMB_STRDUP(get_cmdline_auth_info_username());
474 if (get_cmdline_auth_info_got_pass()) {
475 cm_set_password(get_cmdline_auth_info_password());
478 cm_creds
.use_kerberos
= get_cmdline_auth_info_use_kerberos();
479 cm_creds
.fallback_after_kerberos
= false;
480 cm_creds
.signing_state
= get_cmdline_auth_info_signing_state();
483 /****************************************************************************
484 ****************************************************************************/
486 void cli_cm_set_port(int port_number
)
491 /****************************************************************************
492 ****************************************************************************/
494 void cli_cm_set_dest_name_type(int type
)
499 /****************************************************************************
500 ****************************************************************************/
502 void cli_cm_set_signing_state(int state
)
504 cm_creds
.signing_state
= state
;
507 /****************************************************************************
508 ****************************************************************************/
510 void cli_cm_set_username(const char *username
)
512 SAFE_FREE(cm_creds
.username
);
513 cm_creds
.username
= SMB_STRDUP(username
);
516 /****************************************************************************
517 ****************************************************************************/
519 void cli_cm_set_password(const char *newpass
)
521 SAFE_FREE(cm_creds
.password
);
522 cm_creds
.password
= SMB_STRDUP(newpass
);
523 if (cm_creds
.password
) {
524 cm_creds
.got_pass
= true;
528 /****************************************************************************
529 ****************************************************************************/
531 void cli_cm_set_use_kerberos(void)
533 cm_creds
.use_kerberos
= true;
536 /****************************************************************************
537 ****************************************************************************/
539 void cli_cm_set_fallback_after_kerberos(void)
541 cm_creds
.fallback_after_kerberos
= true;
544 /****************************************************************************
545 ****************************************************************************/
547 void cli_cm_set_dest_ss(struct sockaddr_storage
*pss
)
553 /**********************************************************************
554 split a dfs path into the server, share name, and extrapath components
555 **********************************************************************/
557 static void split_dfs_path(TALLOC_CTX
*ctx
,
558 const char *nodepath
,
568 *pp_extrapath
= NULL
;
570 path
= talloc_strdup(ctx
, nodepath
);
575 if ( path
[0] != '\\' ) {
579 p
= strchr_m( path
+ 1, '\\' );
587 /* Look for any extra/deep path */
588 q
= strchr_m(p
, '\\');
592 *pp_extrapath
= talloc_strdup(ctx
, q
);
594 *pp_extrapath
= talloc_strdup(ctx
, "");
597 *pp_share
= talloc_strdup(ctx
, p
);
598 *pp_server
= talloc_strdup(ctx
, &path
[1]);
601 /****************************************************************************
602 Return the original path truncated at the directory component before
603 the first wildcard character. Trust the caller to provide a NULL
605 ****************************************************************************/
607 static char *clean_path(TALLOC_CTX
*ctx
, const char *path
)
613 /* No absolute paths. */
614 while (IS_DIRECTORY_SEP(*path
)) {
618 path_out
= talloc_strdup(ctx
, path
);
623 p1
= strchr_m(path_out
, '*');
624 p2
= strchr_m(path_out
, '?');
636 /* Now go back to the start of this component. */
637 p1
= strrchr_m(path_out
, '/');
638 p2
= strrchr_m(path_out
, '\\');
645 /* Strip any trailing separator */
647 len
= strlen(path_out
);
648 if ( (len
> 0) && IS_DIRECTORY_SEP(path_out
[len
-1])) {
649 path_out
[len
-1] = '\0';
655 /****************************************************************************
656 ****************************************************************************/
658 static char *cli_dfs_make_full_path(TALLOC_CTX
*ctx
,
659 struct cli_state
*cli
,
662 /* Ensure the extrapath doesn't start with a separator. */
663 while (IS_DIRECTORY_SEP(*dir
)) {
667 return talloc_asprintf(ctx
, "\\%s\\%s\\%s",
668 cli
->desthost
, cli
->share
, dir
);
671 /********************************************************************
672 check for dfs referral
673 ********************************************************************/
675 static bool cli_dfs_check_error( struct cli_state
*cli
, NTSTATUS status
)
677 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
679 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
681 if (!((flgs2
&FLAGS2_32_BIT_ERROR_CODES
) &&
682 (flgs2
&FLAGS2_UNICODE_STRINGS
)))
685 if (NT_STATUS_EQUAL(status
, NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
))))
691 /********************************************************************
692 Get the dfs referral link.
693 ********************************************************************/
695 bool cli_dfs_get_referral(TALLOC_CTX
*ctx
,
696 struct cli_state
*cli
,
698 CLIENT_DFS_REFERRAL
**refs
,
702 unsigned int data_len
= 0;
703 unsigned int param_len
= 0;
704 uint16 setup
= TRANSACT2_GET_DFS_REFERRAL
;
706 char *rparam
=NULL
, *rdata
=NULL
;
709 size_t pathlen
= 2*(strlen(path
)+1);
710 uint16 num_referrals
;
711 CLIENT_DFS_REFERRAL
*referrals
= NULL
;
717 param
= SMB_MALLOC_ARRAY(char, 2+pathlen
+2);
721 SSVAL(param
, 0, 0x03); /* max referral level */
724 p
+= clistr_push(cli
, p
, path
, pathlen
, STR_TERMINATE
);
725 param_len
= PTR_DIFF(p
, param
);
727 if (!cli_send_trans(cli
, SMBtrans2
,
729 -1, 0, /* fid, flags */
730 &setup
, 1, 0, /* setup, length, max */
731 param
, param_len
, 2, /* param, length, max */
732 NULL
, 0, cli
->max_xmit
/* data, length, max */
740 if (!cli_receive_trans(cli
, SMBtrans2
,
742 &rdata
, &data_len
)) {
750 endp
= rdata
+ data_len
;
752 *consumed
= SVAL(rdata
, 0);
753 num_referrals
= SVAL(rdata
, 2);
755 if (num_referrals
!= 0) {
761 referrals
= TALLOC_ARRAY(ctx
, CLIENT_DFS_REFERRAL
,
767 /* start at the referrals array */
770 for (i
=0; i
<num_referrals
&& p
< endp
; i
++) {
774 ref_version
= SVAL(p
, 0);
775 ref_size
= SVAL(p
, 2);
776 node_offset
= SVAL(p
, 16);
778 if (ref_version
!= 3) {
783 referrals
[i
].proximity
= SVAL(p
, 8);
784 referrals
[i
].ttl
= SVAL(p
, 10);
786 if (p
+ node_offset
> endp
) {
789 clistr_pull_talloc(ctx
, cli
, &referrals
[i
].dfspath
,
791 STR_TERMINATE
|STR_UNICODE
);
793 if (!referrals
[i
].dfspath
) {
798 if (i
< num_referrals
) {
805 *num_refs
= num_referrals
;
815 /********************************************************************
816 ********************************************************************/
818 bool cli_resolve_path(TALLOC_CTX
*ctx
,
820 struct cli_state
*rootcli
,
822 struct cli_state
**targetcli
,
823 char **pp_targetpath
)
825 CLIENT_DFS_REFERRAL
*refs
= NULL
;
828 struct cli_state
*cli_ipc
= NULL
;
829 char *dfs_path
= NULL
;
830 char *cleanpath
= NULL
;
831 char *extrapath
= NULL
;
835 struct cli_state
*newcli
= NULL
;
836 char *newpath
= NULL
;
837 char *newmount
= NULL
;
839 SMB_STRUCT_STAT sbuf
;
842 if ( !rootcli
|| !path
|| !targetcli
) {
846 /* Don't do anything if this is not a DFS root. */
848 if ( !rootcli
->dfsroot
) {
849 *targetcli
= rootcli
;
850 *pp_targetpath
= talloc_strdup(ctx
, path
);
851 if (!*pp_targetpath
) {
859 /* Send a trans2_query_path_info to check for a referral. */
861 cleanpath
= clean_path(ctx
, path
);
866 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, cleanpath
);
871 if (cli_qpathinfo_basic( rootcli
, dfs_path
, &sbuf
, &attributes
)) {
872 /* This is an ordinary path, just return it. */
873 *targetcli
= rootcli
;
874 *pp_targetpath
= talloc_strdup(ctx
, path
);
875 if (!*pp_targetpath
) {
881 /* Special case where client asked for a path that does not exist */
883 if (cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
884 *targetcli
= rootcli
;
885 *pp_targetpath
= talloc_strdup(ctx
, path
);
886 if (!*pp_targetpath
) {
892 /* We got an error, check for DFS referral. */
894 if (!cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
)) {
898 /* Check for the referral. */
900 if (!(cli_ipc
= cli_cm_open(ctx
, rootcli
,
903 (rootcli
->trans_enc_state
!= NULL
)))) {
907 if (!cli_dfs_get_referral(ctx
, cli_ipc
, dfs_path
, &refs
,
908 &num_refs
, &consumed
) || !num_refs
) {
912 /* Just store the first referral for now. */
914 if (!refs
[0].dfspath
) {
917 split_dfs_path(ctx
, refs
[0].dfspath
, &server
, &share
, &extrapath
);
919 if (!server
|| !share
) {
923 /* Make sure to recreate the original string including any wildcards. */
925 dfs_path
= cli_dfs_make_full_path(ctx
, rootcli
, path
);
929 pathlen
= strlen(dfs_path
)*2;
930 consumed
= MIN(pathlen
, consumed
);
931 *pp_targetpath
= talloc_strdup(ctx
, &dfs_path
[consumed
/2]);
932 if (!*pp_targetpath
) {
935 dfs_path
[consumed
/2] = '\0';
938 * *pp_targetpath is now the unconsumed part of the path.
939 * dfs_path is now the consumed part of the path
940 * (in \server\share\path format).
943 /* Open the connection to the target server & share */
944 if ((*targetcli
= cli_cm_open(ctx
, rootcli
,
948 (rootcli
->trans_enc_state
!= NULL
))) == NULL
) {
949 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
954 if (extrapath
&& strlen(extrapath
) > 0) {
955 *pp_targetpath
= talloc_asprintf(ctx
,
959 if (!*pp_targetpath
) {
964 /* parse out the consumed mount path */
965 /* trim off the \server\share\ */
969 if (*ppath
!= '\\') {
970 d_printf("cli_resolve_path: "
971 "dfs_path (%s) not in correct format.\n",
976 ppath
++; /* Now pointing at start of server name. */
978 if ((ppath
= strchr_m( dfs_path
, '\\' )) == NULL
) {
982 ppath
++; /* Now pointing at start of share name. */
984 if ((ppath
= strchr_m( ppath
+1, '\\' )) == NULL
) {
988 ppath
++; /* Now pointing at path component. */
990 newmount
= talloc_asprintf(ctx
, "%s\\%s", mountpt
, ppath
);
995 cli_cm_set_mntpoint(*targetcli
, newmount
);
997 /* Check for another dfs referral, note that we are not
998 checking for loops here. */
1000 if (!strequal(*pp_targetpath
, "\\") && !strequal(*pp_targetpath
, "/")) {
1001 if (cli_resolve_path(ctx
,
1008 * When cli_resolve_path returns true here it's always
1009 * returning the complete path in newpath, so we're done
1012 *targetcli
= newcli
;
1013 *pp_targetpath
= newpath
;
1020 /* If returning true ensure we return a dfs root full path. */
1021 if ((*targetcli
)->dfsroot
) {
1022 dfs_path
= talloc_strdup(ctx
, *pp_targetpath
);
1026 *pp_targetpath
= cli_dfs_make_full_path(ctx
, *targetcli
, dfs_path
);
1032 /********************************************************************
1033 ********************************************************************/
1035 static bool cli_check_msdfs_proxy(TALLOC_CTX
*ctx
,
1036 struct cli_state
*cli
,
1037 const char *sharename
,
1038 char **pp_newserver
,
1041 const char *username
,
1042 const char *password
,
1045 CLIENT_DFS_REFERRAL
*refs
= NULL
;
1046 size_t num_refs
= 0;
1048 char *fullpath
= NULL
;
1051 char *newextrapath
= NULL
;
1053 if (!cli
|| !sharename
) {
1059 /* special case. never check for a referral on the IPC$ share */
1061 if (strequal(sharename
, "IPC$")) {
1065 /* send a trans2_query_path_info to check for a referral */
1067 fullpath
= talloc_asprintf(ctx
, "\\%s\\%s", cli
->desthost
, sharename
);
1072 /* check for the referral */
1074 if (!cli_send_tconX(cli
, "IPC$", "IPC", NULL
, 0)) {
1078 if (force_encrypt
) {
1079 NTSTATUS status
= cli_cm_force_encryption(cli
,
1084 if (!NT_STATUS_IS_OK(status
)) {
1089 res
= cli_dfs_get_referral(ctx
, cli
, fullpath
, &refs
, &num_refs
, &consumed
);
1091 if (!cli_tdis(cli
)) {
1097 if (!res
|| !num_refs
) {
1101 if (!refs
[0].dfspath
) {
1105 split_dfs_path(ctx
, refs
[0].dfspath
, pp_newserver
,
1106 pp_newshare
, &newextrapath
);
1108 if ((*pp_newserver
== NULL
) || (*pp_newshare
== NULL
)) {
1112 /* check that this is not a self-referral */
1114 if (strequal(cli
->desthost
, *pp_newserver
) &&
1115 strequal(sharename
, *pp_newshare
)) {