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! */
43 static pstring username;
44 static pstring password;
45 static BOOL use_kerberos;
47 static int signing_state;
48 int max_protocol = PROTOCOL_NT1;
51 static int name_type = 0x20;
53 static struct in_addr dest_ip;
55 static struct client_connection *connections;
57 /********************************************************************
58 Return a connection to a server.
59 ********************************************************************/
61 static struct cli_state *do_connect( const char *server, const char *share,
64 struct cli_state *c = NULL;
65 struct nmb_name called, calling;
70 fstring newserver, newshare;
73 /* make a copy so we don't modify the global string 'service' */
74 pstrcpy(servicename, share);
75 sharename = servicename;
76 if (*sharename == '\\') {
78 sharename = strchr_m(server,'\\');
79 if (!sharename) return NULL;
88 make_nmb_name(&calling, global_myname(), 0x0);
89 make_nmb_name(&called , server, name_type);
96 /* have to open a new connection */
97 if (!(c=cli_initialise()) || (cli_set_port(c, port) != port)) {
98 d_printf("Connection to %s failed\n", server_n);
101 status = cli_connect(c, server_n, &ip);
102 if (!NT_STATUS_IS_OK(status)) {
103 d_printf("Connection to %s failed (Error %s)\n", server_n, nt_errstr(status));
107 c->protocol = max_protocol;
108 c->use_kerberos = use_kerberos;
109 cli_setup_signing_state(c, signing_state);
112 if (!cli_session_request(c, &calling, &called)) {
114 d_printf("session request to %s failed (%s)\n",
115 called.name, cli_errstr(c));
118 if ((p=strchr_m(called.name, '.'))) {
122 if (strcmp(called.name, "*SMBSERVER")) {
123 make_nmb_name(&called , "*SMBSERVER", 0x20);
129 DEBUG(4,(" session request ok\n"));
131 if (!cli_negprot(c)) {
132 d_printf("protocol negotiation failed\n");
138 char *pass = getpass("Password: ");
140 pstrcpy(password, pass);
145 if (!NT_STATUS_IS_OK(cli_session_setup(c, username,
146 password, strlen(password),
147 password, strlen(password),
149 /* if a password was not supplied then try again with a null username */
150 if (password[0] || !username[0] || use_kerberos ||
151 !NT_STATUS_IS_OK(cli_session_setup(c, "", "", 0, "", 0,
153 d_printf("session setup failed: %s\n", cli_errstr(c));
154 if (NT_STATUS_V(cli_nt_error(c)) ==
155 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
156 d_printf("did you forget to run kinit?\n");
160 d_printf("Anonymous login successful\n");
163 if ( show_sessetup ) {
164 if (*c->server_domain) {
165 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
166 c->server_domain,c->server_os,c->server_type));
167 } else if (*c->server_os || *c->server_type){
168 DEBUG(0,("OS=[%s] Server=[%s]\n",
169 c->server_os,c->server_type));
172 DEBUG(4,(" session setup ok\n"));
174 /* here's the fun part....to support 'msdfs proxy' shares
175 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
176 here before trying to connect to the original share.
177 check_dfs_proxy() will fail if it is a normal share. */
179 if ( (c->capabilities & CAP_DFS) && cli_check_msdfs_proxy( c, sharename, newserver, newshare ) ) {
181 return do_connect( newserver, newshare, False );
184 /* must be a normal share */
186 if (!cli_send_tconX(c, sharename, "?????", password, strlen(password)+1)) {
187 d_printf("tree connect failed: %s\n", cli_errstr(c));
192 DEBUG(4,(" tconx ok\n"));
197 /****************************************************************************
198 ****************************************************************************/
200 static void cli_cm_set_mntpoint( struct cli_state *c, const char *mnt )
202 struct client_connection *p;
205 for ( p=connections,i=0; p; p=p->next,i++ ) {
206 if ( strequal(p->cli->desthost, c->desthost) && strequal(p->cli->share, c->share) )
211 pstrcpy( p->mount, mnt );
212 clean_name(p->mount);
216 /****************************************************************************
217 ****************************************************************************/
219 const char *cli_cm_get_mntpoint( struct cli_state *c )
221 struct client_connection *p;
224 for ( p=connections,i=0; p; p=p->next,i++ ) {
225 if ( strequal(p->cli->desthost, c->desthost) && strequal(p->cli->share, c->share) )
235 /********************************************************************
236 Add a new connection to the list
237 ********************************************************************/
239 static struct cli_state *cli_cm_connect( const char *server,
243 struct client_connection *node;
245 node = SMB_XMALLOC_P( struct client_connection );
247 node->cli = do_connect( server, share, show_hdr );
254 DLIST_ADD( connections, node );
256 cli_cm_set_mntpoint( node->cli, "" );
262 /********************************************************************
263 Return a connection to a server.
264 ********************************************************************/
266 static struct cli_state *cli_cm_find( const char *server, const char *share )
268 struct client_connection *p;
270 for ( p=connections; p; p=p->next ) {
271 if ( strequal(server, p->cli->desthost) && strequal(share,p->cli->share) )
278 /****************************************************************************
279 open a client connection to a \\server\share. Set's the current *cli
280 global variable as a side-effect (but only if the connection is successful).
281 ****************************************************************************/
283 struct cli_state *cli_cm_open(const char *server,
289 /* try to reuse an existing connection */
291 c = cli_cm_find( server, share );
294 c = cli_cm_connect(server, share, show_hdr);
300 /****************************************************************************
301 ****************************************************************************/
303 void cli_cm_shutdown( void )
306 struct client_connection *p, *x;
308 for ( p=connections; p; ) {
309 cli_shutdown( p->cli );
320 /****************************************************************************
321 ****************************************************************************/
323 void cli_cm_display(void)
325 struct client_connection *p;
328 for ( p=connections,i=0; p; p=p->next,i++ ) {
329 d_printf("%d:\tserver=%s, share=%s\n",
330 i, p->cli->desthost, p->cli->share );
334 /****************************************************************************
335 ****************************************************************************/
337 void cli_cm_set_credentials( struct user_auth_info *user )
339 pstrcpy( username, user->username );
341 if ( user->got_pass ) {
342 pstrcpy( password, user->password );
346 use_kerberos = user->use_kerberos;
347 signing_state = user->signing_state;
350 /****************************************************************************
351 ****************************************************************************/
353 void cli_cm_set_port( int port_number )
358 /****************************************************************************
359 ****************************************************************************/
361 void cli_cm_set_dest_name_type( int type )
366 /****************************************************************************
367 ****************************************************************************/
369 void cli_cm_set_dest_ip(struct in_addr ip )
375 /**********************************************************************
376 split a dfs path into the server, share name, and extrapath components
377 **********************************************************************/
379 static void split_dfs_path( const char *nodepath, fstring server, fstring share, pstring extrapath )
384 pstrcpy( path, nodepath );
386 if ( path[0] != '\\' ) {
390 p = strchr_m( path + 1, '\\' );
398 /* Look for any extra/deep path */
399 q = strchr_m(p, '\\');
403 pstrcpy( extrapath, q );
405 pstrcpy( extrapath, '\0' );
409 fstrcpy( server, &path[1] );
412 /****************************************************************************
413 Return the original path truncated at the directory component before
414 the first wildcard character. Trust the caller to provide a NULL
416 ****************************************************************************/
418 static void clean_path(const char *path, pstring path_out)
423 /* No absolute paths. */
424 while (IS_DIRECTORY_SEP(*path)) {
428 pstrcpy(path_out, path);
430 p1 = strchr_m(path_out, '*');
431 p2 = strchr_m(path_out, '?');
443 /* Now go back to the start of this component. */
444 p1 = strrchr_m(path_out, '/');
445 p2 = strrchr_m(path_out, '\\');
452 /* Strip any trailing separator */
454 len = strlen(path_out);
455 if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
456 path_out[len-1] = '\0';
460 /****************************************************************************
461 ****************************************************************************/
463 static void cli_dfs_make_full_path( struct cli_state *cli,
467 /* Ensure the extrapath doesn't start with a separator. */
468 while (IS_DIRECTORY_SEP(*dir)) {
472 pstr_sprintf( path_out, "\\%s\\%s\\%s", cli->desthost, cli->share, dir);
475 /********************************************************************
476 check for dfs referral
477 ********************************************************************/
479 static BOOL cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
481 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
483 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
485 if ( !( (flgs2&FLAGS2_32_BIT_ERROR_CODES) && (flgs2&FLAGS2_UNICODE_STRINGS) ) )
488 if ( NT_STATUS_EQUAL( status, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
494 /********************************************************************
495 get the dfs referral link
496 ********************************************************************/
498 BOOL cli_dfs_get_referral( struct cli_state *cli,
500 CLIENT_DFS_REFERRAL**refs,
504 unsigned int data_len = 0;
505 unsigned int param_len = 0;
506 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
507 char param[sizeof(pstring)+2];
509 char *rparam=NULL, *rdata=NULL;
511 size_t pathlen = 2*(strlen(path)+1);
512 uint16 num_referrals;
513 CLIENT_DFS_REFERRAL *referrals = NULL;
515 memset(param, 0, sizeof(param));
516 SSVAL(param, 0, 0x03); /* max referral level */
519 p += clistr_push(cli, p, path, MIN(pathlen, sizeof(param)-2), STR_TERMINATE);
520 param_len = PTR_DIFF(p, param);
522 if (!cli_send_trans(cli, SMBtrans2,
524 -1, 0, /* fid, flags */
525 &setup, 1, 0, /* setup, length, max */
526 param, param_len, 2, /* param, length, max */
527 (char *)&data, data_len, cli->max_xmit /* data, length, max */
532 if (!cli_receive_trans(cli, SMBtrans2,
534 &rdata, &data_len)) {
538 *consumed = SVAL( rdata, 0 );
539 num_referrals = SVAL( rdata, 2 );
541 if ( num_referrals != 0 ) {
547 referrals = SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL, num_referrals );
549 /* start at the referrals array */
552 for ( i=0; i<num_referrals; i++ ) {
553 ref_version = SVAL( p, 0 );
554 ref_size = SVAL( p, 2 );
555 node_offset = SVAL( p, 16 );
557 if ( ref_version != 3 ) {
562 referrals[i].proximity = SVAL( p, 8 );
563 referrals[i].ttl = SVAL( p, 10 );
565 clistr_pull( cli, referrals[i].dfspath, p+node_offset,
566 sizeof(referrals[i].dfspath), -1, STR_TERMINATE|STR_UNICODE );
572 *num_refs = num_referrals;
582 /********************************************************************
583 ********************************************************************/
585 BOOL cli_resolve_path( const char *mountpt,
586 struct cli_state *rootcli,
588 struct cli_state **targetcli,
591 CLIENT_DFS_REFERRAL *refs = NULL;
594 struct cli_state *cli_ipc;
595 pstring dfs_path, cleanpath, extrapath;
597 fstring server, share;
598 struct cli_state *newcli;
601 char *ppath, *temppath = NULL;
603 SMB_STRUCT_STAT sbuf;
606 if ( !rootcli || !path || !targetcli ) {
610 /* Don't do anything if this is not a DFS root. */
612 if ( !rootcli->dfsroot) {
613 *targetcli = rootcli;
614 pstrcpy( targetpath, path );
620 /* Send a trans2_query_path_info to check for a referral. */
622 clean_path(path, cleanpath);
623 cli_dfs_make_full_path(rootcli, cleanpath, dfs_path );
625 if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes ) ) {
626 /* This is an ordinary path, just return it. */
627 *targetcli = rootcli;
628 pstrcpy( targetpath, path );
632 /* Special case where client asked for a path that does not exist */
634 if ( cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND) ) {
635 *targetcli = rootcli;
636 pstrcpy( targetpath, path );
640 /* We got an error, check for DFS referral. */
642 if ( !cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED)) {
646 /* Check for the referral. */
648 if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) ) {
652 if ( !cli_dfs_get_referral(cli_ipc, dfs_path, &refs, &num_refs, &consumed)
657 /* Just store the first referral for now. */
659 split_dfs_path( refs[0].dfspath, server, share, extrapath );
662 /* Make sure to recreate the original string including any wildcards. */
664 cli_dfs_make_full_path( rootcli, path, dfs_path);
665 pathlen = strlen( dfs_path )*2;
666 consumed = MIN(pathlen, consumed );
667 pstrcpy( targetpath, &dfs_path[consumed/2] );
668 dfs_path[consumed/2] = '\0';
671 * targetpath is now the unconsumed part of the path.
672 * dfs_path is now the consumed part of the path (in \server\share\path format).
675 /* Open the connection to the target server & share */
677 if ( (*targetcli = cli_cm_open(server, share, False)) == NULL ) {
678 d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
683 if (strlen(extrapath) > 0) {
684 string_append(&temppath, extrapath);
685 string_append(&temppath, targetpath);
686 pstrcpy( targetpath, temppath );
689 /* parse out the consumed mount path */
690 /* trim off the \server\share\ */
694 if (*ppath != '\\') {
695 d_printf("cli_resolve_path: dfs_path (%s) not in correct format.\n",
700 ppath++; /* Now pointing at start of server name. */
702 if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
706 ppath++; /* Now pointing at start of share name. */
708 if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
712 ppath++; /* Now pointing at path component. */
714 pstr_sprintf( newmount, "%s\\%s", mountpt, ppath );
716 cli_cm_set_mntpoint( *targetcli, newmount );
718 /* Check for another dfs referral, note that we are not
719 checking for loops here. */
721 if ( !strequal( targetpath, "\\" ) && !strequal( targetpath, "/")) {
722 if ( cli_resolve_path( newmount, *targetcli, targetpath, &newcli, newpath ) ) {
724 * When cli_resolve_path returns true here it's always
725 * returning the complete path in newpath, so we're done
729 pstrcpy( targetpath, newpath );
736 /* If returning True ensure we return a dfs root full path. */
737 if ( (*targetcli)->dfsroot ) {
738 pstrcpy(dfs_path, targetpath );
739 cli_dfs_make_full_path( *targetcli, dfs_path, targetpath);
745 /********************************************************************
746 ********************************************************************/
748 BOOL cli_check_msdfs_proxy( struct cli_state *cli, const char *sharename,
749 fstring newserver, fstring newshare )
751 CLIENT_DFS_REFERRAL *refs = NULL;
757 pstring newextrapath;
759 if ( !cli || !sharename )
764 /* special case. never check for a referral on the IPC$ share */
766 if ( strequal( sharename, "IPC$" ) ) {
770 /* send a trans2_query_path_info to check for a referral */
772 pstr_sprintf( fullpath, "\\%s\\%s", cli->desthost, sharename );
774 /* check for the referral */
776 if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
780 res = cli_dfs_get_referral(cli, fullpath, &refs, &num_refs, &consumed);
782 if (!cli_tdis(cli)) {
789 if (!res || !num_refs ) {
794 split_dfs_path( refs[0].dfspath, newserver, newshare, newextrapath );
796 /* check that this is not a self-referral */
798 if ( strequal( cli->desthost, newserver ) && strequal( sharename, newshare ) ) {