2 Unix SMB/CIFS implementation.
3 client connect/disconnect routines
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 struct client_connection
{
26 struct client_connection
*prev
, *next
;
27 struct cli_state
*cli
;
31 /* global state....globals reek! */
33 static pstring username
;
34 static pstring password
;
35 static BOOL use_kerberos
;
37 static int signing_state
;
38 int max_protocol
= PROTOCOL_NT1
;
41 static int name_type
= 0x20;
43 static struct in_addr dest_ip
;
45 static struct client_connection
*connections
;
47 /********************************************************************
48 Return a connection to a server.
49 ********************************************************************/
51 static struct cli_state
*do_connect( const char *server
, const char *share
,
55 struct nmb_name called
, calling
;
60 fstring newserver
, newshare
;
62 /* make a copy so we don't modify the global string 'service' */
63 pstrcpy(servicename
, share
);
64 sharename
= servicename
;
65 if (*sharename
== '\\') {
67 sharename
= strchr_m(server
,'\\');
68 if (!sharename
) return NULL
;
77 make_nmb_name(&calling
, global_myname(), 0x0);
78 make_nmb_name(&called
, server
, name_type
);
85 /* have to open a new connection */
86 if (!(c
=cli_initialise(NULL
)) || (cli_set_port(c
, port
) != port
) ||
87 !cli_connect(c
, server_n
, &ip
)) {
88 d_printf("Connection to %s failed\n", server_n
);
92 c
->protocol
= max_protocol
;
93 c
->use_kerberos
= use_kerberos
;
94 cli_setup_signing_state(c
, signing_state
);
97 if (!cli_session_request(c
, &calling
, &called
)) {
99 d_printf("session request to %s failed (%s)\n",
100 called
.name
, cli_errstr(c
));
102 if ((p
=strchr_m(called
.name
, '.'))) {
106 if (strcmp(called
.name
, "*SMBSERVER")) {
107 make_nmb_name(&called
, "*SMBSERVER", 0x20);
113 DEBUG(4,(" session request ok\n"));
115 if (!cli_negprot(c
)) {
116 d_printf("protocol negotiation failed\n");
122 char *pass
= getpass("Password: ");
124 pstrcpy(password
, pass
);
129 if (!cli_session_setup(c
, username
,
130 password
, strlen(password
),
131 password
, strlen(password
),
133 /* if a password was not supplied then try again with a null username */
134 if (password
[0] || !username
[0] || use_kerberos
||
135 !cli_session_setup(c
, "", "", 0, "", 0, lp_workgroup())) {
136 d_printf("session setup failed: %s\n", cli_errstr(c
));
137 if (NT_STATUS_V(cli_nt_error(c
)) ==
138 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED
))
139 d_printf("did you forget to run kinit?\n");
143 d_printf("Anonymous login successful\n");
146 if ( show_sessetup
) {
147 if (*c
->server_domain
) {
148 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
149 c
->server_domain
,c
->server_os
,c
->server_type
));
150 } else if (*c
->server_os
|| *c
->server_type
){
151 DEBUG(0,("OS=[%s] Server=[%s]\n",
152 c
->server_os
,c
->server_type
));
155 DEBUG(4,(" session setup ok\n"));
157 /* here's the fun part....to support 'msdfs proxy' shares
158 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
159 here before trying to connect to the original share.
160 check_dfs_proxy() will fail if it is a normal share. */
162 if ( (c
->capabilities
& CAP_DFS
) && cli_check_msdfs_proxy( c
, sharename
, newserver
, newshare
) ) {
164 return do_connect( newserver
, newshare
, False
);
167 /* must be a normal share */
169 if (!cli_send_tconX(c
, sharename
, "?????", password
, strlen(password
)+1)) {
170 d_printf("tree connect failed: %s\n", cli_errstr(c
));
175 DEBUG(4,(" tconx ok\n"));
180 /****************************************************************************
181 ****************************************************************************/
183 static void cli_cm_set_mntpoint( struct cli_state
*c
, const char *mnt
)
185 struct client_connection
*p
;
188 for ( p
=connections
,i
=0; p
; p
=p
->next
,i
++ ) {
189 if ( strequal(p
->cli
->desthost
, c
->desthost
) && strequal(p
->cli
->share
, c
->share
) )
194 pstrcpy( p
->mount
, mnt
);
195 dos_clean_name( p
->mount
);
199 /****************************************************************************
200 ****************************************************************************/
202 const char * cli_cm_get_mntpoint( struct cli_state
*c
)
204 struct client_connection
*p
;
207 for ( p
=connections
,i
=0; p
; p
=p
->next
,i
++ ) {
208 if ( strequal(p
->cli
->desthost
, c
->desthost
) && strequal(p
->cli
->share
, c
->share
) )
218 /********************************************************************
219 Add a new connection to the list
220 ********************************************************************/
222 static struct cli_state
* cli_cm_connect( const char *server
, const char *share
,
225 struct client_connection
*node
;
227 node
= SMB_XMALLOC_P( struct client_connection
);
229 node
->cli
= do_connect( server
, share
, show_hdr
);
236 DLIST_ADD( connections
, node
);
238 cli_cm_set_mntpoint( node
->cli
, "" );
244 /********************************************************************
245 Return a connection to a server.
246 ********************************************************************/
248 static struct cli_state
* cli_cm_find( const char *server
, const char *share
)
250 struct client_connection
*p
;
252 for ( p
=connections
; p
; p
=p
->next
) {
253 if ( strequal(server
, p
->cli
->desthost
) && strequal(share
,p
->cli
->share
) )
260 /****************************************************************************
261 open a client connection to a \\server\share. Set's the current *cli
262 global variable as a side-effect (but only if the connection is successful).
263 ****************************************************************************/
265 struct cli_state
* cli_cm_open( const char *server
, const char *share
, BOOL show_hdr
)
269 /* try to reuse an existing connection */
271 c
= cli_cm_find( server
, share
);
274 c
= cli_cm_connect( server
, share
, show_hdr
);
279 /****************************************************************************
280 ****************************************************************************/
282 void cli_cm_shutdown( void )
285 struct client_connection
*p
, *x
;
287 for ( p
=connections
; p
; ) {
288 cli_shutdown( p
->cli
);
300 /****************************************************************************
301 ****************************************************************************/
303 void cli_cm_display(void)
305 struct client_connection
*p
;
308 for ( p
=connections
,i
=0; p
; p
=p
->next
,i
++ ) {
309 d_printf("%d:\tserver=%s, share=%s\n",
310 i
, p
->cli
->desthost
, p
->cli
->share
);
314 /****************************************************************************
315 ****************************************************************************/
317 void cli_cm_set_credentials( struct user_auth_info
*user
)
319 pstrcpy( username
, user
->username
);
321 if ( user
->got_pass
) {
322 pstrcpy( password
, user
->password
);
326 use_kerberos
= user
->use_kerberos
;
327 signing_state
= user
->signing_state
;
330 /****************************************************************************
331 ****************************************************************************/
333 void cli_cm_set_port( int port_number
)
338 /****************************************************************************
339 ****************************************************************************/
341 void cli_cm_set_dest_name_type( int type
)
346 /****************************************************************************
347 ****************************************************************************/
349 void cli_cm_set_dest_ip(struct in_addr ip
)
355 /********************************************************************
356 split a dfs path into the server and share name components
357 ********************************************************************/
359 static void split_dfs_path( const char *nodepath
, fstring server
, fstring share
)
364 pstrcpy( path
, nodepath
);
366 if ( path
[0] != '\\' )
369 p
= strrchr_m( path
, '\\' );
378 fstrcpy( server
, &path
[1] );
381 /****************************************************************************
382 return the original path truncated at the first wildcard character
383 (also strips trailing \'s). Trust the caller to provide a NULL
385 ****************************************************************************/
387 static void clean_path( pstring clean
, const char *path
)
393 pstrcpy( newpath
, path
);
397 /* first check for '*' */
399 p
= strrchr_m( newpath
, '*' );
406 /* first check for '?' */
408 p
= strrchr_m( newpath
, '?' );
415 /* strip a trailing backslash */
417 len
= strlen( newpath
);
418 if ( newpath
[len
-1] == '\\' )
419 newpath
[len
-1] = '\0';
421 pstrcpy( clean
, newpath
);
424 /****************************************************************************
425 ****************************************************************************/
427 BOOL
cli_dfs_make_full_path( pstring path
, const char *server
, const char *share
,
432 const char *directory
;
435 /* make a copy so we don't modify the global string 'service' */
437 pstrcpy(servicename
, share
);
438 sharename
= servicename
;
440 if (*sharename
== '\\') {
442 server
= sharename
+2;
443 sharename
= strchr_m(server
,'\\');
453 if ( *directory
== '\\' )
456 pstr_sprintf( path
, "\\%s\\%s\\%s", server
, sharename
, directory
);
461 /********************************************************************
462 check for dfs referral
463 ********************************************************************/
465 static BOOL
cli_dfs_check_error( struct cli_state
*cli
, NTSTATUS status
)
467 uint32 flgs2
= SVAL(cli
->inbuf
,smb_flg2
);
469 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
471 if ( !( (flgs2
&FLAGS2_32_BIT_ERROR_CODES
) && (flgs2
&FLAGS2_UNICODE_STRINGS
) ) )
474 if ( NT_STATUS_EQUAL( status
, NT_STATUS(IVAL(cli
->inbuf
,smb_rcls
)) ) )
480 /********************************************************************
481 get the dfs referral link
482 ********************************************************************/
484 BOOL
cli_dfs_get_referral( struct cli_state
*cli
, const char *path
,
485 CLIENT_DFS_REFERRAL
**refs
, size_t *num_refs
,
488 unsigned int data_len
= 0;
489 unsigned int param_len
= 0;
490 uint16 setup
= TRANSACT2_GET_DFS_REFERRAL
;
491 char param
[sizeof(pstring
)+2];
493 char *rparam
=NULL
, *rdata
=NULL
;
495 size_t pathlen
= 2*(strlen(path
)+1);
496 uint16 num_referrals
;
497 CLIENT_DFS_REFERRAL
*referrals
= NULL
;
499 memset(param
, 0, sizeof(param
));
500 SSVAL(param
, 0, 0x03); /* max referral level */
503 p
+= clistr_push(cli
, p
, path
, MIN(pathlen
, sizeof(param
)-2), STR_TERMINATE
);
504 param_len
= PTR_DIFF(p
, param
);
506 if (!cli_send_trans(cli
, SMBtrans2
,
508 -1, 0, /* fid, flags */
509 &setup
, 1, 0, /* setup, length, max */
510 param
, param_len
, 2, /* param, length, max */
511 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
516 if (!cli_receive_trans(cli
, SMBtrans2
,
518 &rdata
, &data_len
)) {
522 *consumed
= SVAL( rdata
, 0 );
523 num_referrals
= SVAL( rdata
, 2 );
525 if ( num_referrals
!= 0 ) {
532 referrals
= SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL
, num_referrals
);
534 /* start at the referrals array */
537 for ( i
=0; i
<num_referrals
; i
++ ) {
538 ref_version
= SVAL( p
, 0 );
539 ref_size
= SVAL( p
, 2 );
540 node_offset
= SVAL( p
, 16 );
542 if ( ref_version
!= 3 ) {
547 referrals
[i
].proximity
= SVAL( p
, 8 );
548 referrals
[i
].ttl
= SVAL( p
, 10 );
550 clistr_pull( cli
, referrals
[i
].dfspath
, p
+node_offset
,
551 sizeof(referrals
[i
].dfspath
), -1, STR_TERMINATE
|STR_UNICODE
);
558 *num_refs
= num_referrals
;
567 /********************************************************************
568 ********************************************************************/
570 BOOL
cli_resolve_path( const char *mountpt
, struct cli_state
*rootcli
, const char *path
,
571 struct cli_state
**targetcli
, pstring targetpath
)
573 CLIENT_DFS_REFERRAL
*refs
= NULL
;
576 struct cli_state
*cli_ipc
;
577 pstring fullpath
, cleanpath
;
579 fstring server
, share
;
580 struct cli_state
*newcli
;
585 SMB_STRUCT_STAT sbuf
;
588 if ( !rootcli
|| !path
|| !targetcli
)
593 /* send a trans2_query_path_info to check for a referral */
595 clean_path( cleanpath
, path
);
596 cli_dfs_make_full_path( fullpath
, rootcli
->desthost
, rootcli
->share
, cleanpath
);
598 /* don't bother continuing if this is not a dfs root */
600 if ( !rootcli
->dfsroot
|| cli_qpathinfo_basic( rootcli
, cleanpath
, &sbuf
, &attributes
) ) {
601 *targetcli
= rootcli
;
602 pstrcpy( targetpath
, path
);
606 /* special case where client asked for a path that does not exist */
608 if ( cli_dfs_check_error(rootcli
, NT_STATUS_OBJECT_NAME_NOT_FOUND
) ) {
609 *targetcli
= rootcli
;
610 pstrcpy( targetpath
, path
);
614 /* we got an error, check for DFS referral */
616 if ( !cli_dfs_check_error(rootcli
, NT_STATUS_PATH_NOT_COVERED
) )
619 /* check for the referral */
621 if ( !(cli_ipc
= cli_cm_open( rootcli
->desthost
, "IPC$", False
)) )
624 if ( !cli_dfs_get_referral(cli_ipc
, fullpath
, &refs
, &num_refs
, &consumed
)
630 /* just store the first referral for now
631 Make sure to recreate the original string including any wildcards */
633 cli_dfs_make_full_path( fullpath
, rootcli
->desthost
, rootcli
->share
, path
);
634 pathlen
= strlen( fullpath
)*2;
635 consumed
= MIN(pathlen
, consumed
);
636 pstrcpy( targetpath
, &fullpath
[consumed
/2] );
638 split_dfs_path( refs
[0].dfspath
, server
, share
);
641 /* open the connection to the target path */
643 if ( (*targetcli
= cli_cm_open(server
, share
, False
)) == NULL
) {
644 d_printf("Unable to follow dfs referral [//%s/%s]\n",
650 /* parse out the consumed mount path */
651 /* trim off the \server\share\ */
653 fullpath
[consumed
/2] = '\0';
654 dos_clean_name( fullpath
);
655 ppath
= strchr_m( fullpath
, '\\' );
656 ppath
= strchr_m( ppath
+1, '\\' );
657 ppath
= strchr_m( ppath
+1, '\\' );
660 pstr_sprintf( newmount
, "%s\\%s", mountpt
, ppath
);
661 cli_cm_set_mntpoint( *targetcli
, newmount
);
663 /* check for another dfs referral, note that we are not
664 checking for loops here */
666 if ( !strequal( targetpath
, "\\" ) ) {
667 if ( cli_resolve_path( newmount
, *targetcli
, targetpath
, &newcli
, newpath
) ) {
669 pstrcpy( targetpath
, newpath
);
676 /********************************************************************
677 ********************************************************************/
679 BOOL
cli_check_msdfs_proxy( struct cli_state
*cli
, const char *sharename
,
680 fstring newserver
, fstring newshare
)
682 CLIENT_DFS_REFERRAL
*refs
= NULL
;
689 if ( !cli
|| !sharename
)
694 /* special case. never check for a referral on the IPC$ share */
696 if ( strequal( sharename
, "IPC$" ) )
699 /* send a trans2_query_path_info to check for a referral */
701 pstr_sprintf( fullpath
, "\\%s\\%s", cli
->desthost
, sharename
);
703 /* check for the referral */
705 if (!cli_send_tconX(cli
, "IPC$", "IPC", NULL
, 0)) {
709 res
= cli_dfs_get_referral(cli
, fullpath
, &refs
, &num_refs
, &consumed
);
711 if (!cli_tdis(cli
)) {
717 if (!res
|| !num_refs
) {
721 split_dfs_path( refs
[0].dfspath
, newserver
, newshare
);
723 /* check that this is not a self-referral */
725 if ( strequal( cli
->desthost
, newserver
) && strequal( sharename
, newshare
) )