r7339: only check for dfs proxy referrals when the server supports dfs
[Samba/gbeck.git] / source / libsmb / clidfs.c
blob7ce4871b4d7cea7acebec6d70a3448cedbf2314b
1 /*
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.
22 #define NO_SYSLOG
24 #include "includes.h"
27 struct client_connection {
28 struct client_connection *prev, *next;
29 struct cli_state *cli;
30 pstring mount;
33 /* global state....globals reek! */
35 static pstring username;
36 static pstring password;
37 static BOOL use_kerberos;
38 static BOOL got_pass;
39 static int signing_state;
40 int max_protocol = PROTOCOL_NT1;
42 static int port;
43 static int name_type = 0x20;
44 static BOOL have_ip;
45 static struct in_addr dest_ip;
47 static struct client_connection *connections;
49 /********************************************************************
50 Return a connection to a server.
51 ********************************************************************/
53 static struct cli_state *do_connect( const char *server, const char *share,
54 BOOL show_sessetup )
56 struct cli_state *c;
57 struct nmb_name called, calling;
58 const char *server_n;
59 struct in_addr ip;
60 pstring servicename;
61 char *sharename;
62 fstring newserver, newshare;
64 /* make a copy so we don't modify the global string 'service' */
65 pstrcpy(servicename, share);
66 sharename = servicename;
67 if (*sharename == '\\') {
68 server = sharename+2;
69 sharename = strchr_m(server,'\\');
70 if (!sharename) return NULL;
71 *sharename = 0;
72 sharename++;
75 server_n = server;
77 zero_ip(&ip);
79 make_nmb_name(&calling, global_myname(), 0x0);
80 make_nmb_name(&called , server, name_type);
82 again:
83 zero_ip(&ip);
84 if (have_ip)
85 ip = dest_ip;
87 /* have to open a new connection */
88 if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) != port) ||
89 !cli_connect(c, server_n, &ip)) {
90 d_printf("Connection to %s failed\n", server_n);
91 return NULL;
94 c->protocol = max_protocol;
95 c->use_kerberos = use_kerberos;
96 cli_setup_signing_state(c, signing_state);
99 if (!cli_session_request(c, &calling, &called)) {
100 char *p;
101 d_printf("session request to %s failed (%s)\n",
102 called.name, cli_errstr(c));
103 cli_shutdown(c);
104 if ((p=strchr_m(called.name, '.'))) {
105 *p = 0;
106 goto again;
108 if (strcmp(called.name, "*SMBSERVER")) {
109 make_nmb_name(&called , "*SMBSERVER", 0x20);
110 goto again;
112 return NULL;
115 DEBUG(4,(" session request ok\n"));
117 if (!cli_negprot(c)) {
118 d_printf("protocol negotiation failed\n");
119 cli_shutdown(c);
120 return NULL;
123 if (!got_pass) {
124 char *pass = getpass("Password: ");
125 if (pass) {
126 pstrcpy(password, pass);
127 got_pass = 1;
131 if (!cli_session_setup(c, username,
132 password, strlen(password),
133 password, strlen(password),
134 lp_workgroup())) {
135 /* if a password was not supplied then try again with a null username */
136 if (password[0] || !username[0] || use_kerberos ||
137 !cli_session_setup(c, "", "", 0, "", 0, lp_workgroup())) {
138 d_printf("session setup failed: %s\n", cli_errstr(c));
139 if (NT_STATUS_V(cli_nt_error(c)) ==
140 NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
141 d_printf("did you forget to run kinit?\n");
142 cli_shutdown(c);
143 return NULL;
145 d_printf("Anonymous login successful\n");
148 if ( show_sessetup ) {
149 if (*c->server_domain) {
150 DEBUG(0,("Domain=[%s] OS=[%s] Server=[%s]\n",
151 c->server_domain,c->server_os,c->server_type));
152 } else if (*c->server_os || *c->server_type){
153 DEBUG(0,("OS=[%s] Server=[%s]\n",
154 c->server_os,c->server_type));
157 DEBUG(4,(" session setup ok\n"));
159 /* here's the fun part....to support 'msdfs proxy' shares
160 (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL
161 here before trying to connect to the original share.
162 check_dfs_proxy() will fail if it is a normal share. */
164 if ( (c->capabilities & CAP_DFS) && cli_check_msdfs_proxy( c, sharename, newserver, newshare ) ) {
165 cli_shutdown(c);
166 return do_connect( newserver, newshare, False );
169 /* must be a normal share */
171 if (!cli_send_tconX(c, sharename, "?????", password, strlen(password)+1)) {
172 d_printf("tree connect failed: %s\n", cli_errstr(c));
173 cli_shutdown(c);
174 return NULL;
177 DEBUG(4,(" tconx ok\n"));
179 return c;
182 /****************************************************************************
183 ****************************************************************************/
185 static void cli_cm_set_mntpoint( struct cli_state *c, const char *mnt )
187 struct client_connection *p;
188 int i;
190 for ( p=connections,i=0; p; p=p->next,i++ ) {
191 if ( strequal(p->cli->desthost, c->desthost) && strequal(p->cli->share, c->share) )
192 break;
195 if ( p ) {
196 pstrcpy( p->mount, mnt );
197 dos_clean_name( p->mount );
201 /****************************************************************************
202 ****************************************************************************/
204 const char * cli_cm_get_mntpoint( struct cli_state *c )
206 struct client_connection *p;
207 int i;
209 for ( p=connections,i=0; p; p=p->next,i++ ) {
210 if ( strequal(p->cli->desthost, c->desthost) && strequal(p->cli->share, c->share) )
211 break;
214 if ( p )
215 return p->mount;
217 return NULL;
220 /********************************************************************
221 Add a new connection to the list
222 ********************************************************************/
224 static struct cli_state* cli_cm_connect( const char *server, const char *share,
225 BOOL show_hdr )
227 struct client_connection *node;
229 node = SMB_XMALLOC_P( struct client_connection );
231 node->cli = do_connect( server, share, show_hdr );
233 if ( !node->cli ) {
234 SAFE_FREE( node );
235 return NULL;
238 DLIST_ADD( connections, node );
240 cli_cm_set_mntpoint( node->cli, "" );
242 return node->cli;
246 /********************************************************************
247 Return a connection to a server.
248 ********************************************************************/
250 static struct cli_state* cli_cm_find( const char *server, const char *share )
252 struct client_connection *p;
254 for ( p=connections; p; p=p->next ) {
255 if ( strequal(server, p->cli->desthost) && strequal(share,p->cli->share) )
256 return p->cli;
259 return NULL;
262 /****************************************************************************
263 open a client connection to a \\server\share. Set's the current *cli
264 global variable as a side-effect (but only if the connection is successful).
265 ****************************************************************************/
267 struct cli_state* cli_cm_open( const char *server, const char *share, BOOL show_hdr )
269 struct cli_state *c;
271 /* try to reuse an existing connection */
273 c = cli_cm_find( server, share );
275 if ( !c )
276 c = cli_cm_connect( server, share, show_hdr );
278 return c;
281 /****************************************************************************
282 ****************************************************************************/
284 void cli_cm_shutdown( void )
287 struct client_connection *p, *x;
289 for ( p=connections; p; ) {
290 cli_shutdown( p->cli );
291 x = p;
292 p = p->next;
294 SAFE_FREE( x );
297 connections = NULL;
299 return;
302 /****************************************************************************
303 ****************************************************************************/
305 void cli_cm_display(void)
307 struct client_connection *p;
308 int i;
310 for ( p=connections,i=0; p; p=p->next,i++ ) {
311 d_printf("%d:\tserver=%s, share=%s\n",
312 i, p->cli->desthost, p->cli->share );
316 /****************************************************************************
317 ****************************************************************************/
319 void cli_cm_set_credentials( struct user_auth_info *user )
321 pstrcpy( username, user->username );
323 if ( user->got_pass ) {
324 pstrcpy( password, user->password );
325 got_pass = True;
328 use_kerberos = user->use_kerberos;
329 signing_state = user->signing_state;
332 /****************************************************************************
333 ****************************************************************************/
335 void cli_cm_set_port( int port_number )
337 port = port_number;
340 /****************************************************************************
341 ****************************************************************************/
343 void cli_cm_set_dest_name_type( int type )
345 name_type = type;
348 /****************************************************************************
349 ****************************************************************************/
351 void cli_cm_set_dest_ip(struct in_addr ip )
353 dest_ip = ip;
354 have_ip = True;
357 /********************************************************************
358 split a dfs path into the server and share name components
359 ********************************************************************/
361 static void split_dfs_path( const char *nodepath, fstring server, fstring share )
363 char *p;
364 pstring path;
366 pstrcpy( path, nodepath );
368 if ( path[0] != '\\' )
369 return;
371 p = strrchr_m( path, '\\' );
373 if ( !p )
374 return;
376 *p = '\0';
377 p++;
379 fstrcpy( share, p );
380 fstrcpy( server, &path[1] );
383 /****************************************************************************
384 return the original path truncated at the first wildcard character
385 (also strips trailing \'s). Trust the caller to provide a NULL
386 terminated string
387 ****************************************************************************/
389 static void clean_path( pstring clean, const char *path )
391 int len;
392 char *p;
393 pstring newpath;
395 pstrcpy( newpath, path );
396 p = newpath;
398 while ( p ) {
399 /* first check for '*' */
401 p = strrchr_m( newpath, '*' );
402 if ( p ) {
403 *p = '\0';
404 p = newpath;
405 continue;
408 /* first check for '?' */
410 p = strrchr_m( newpath, '?' );
411 if ( p ) {
412 *p = '\0';
413 p = newpath;
417 /* strip a trailing backslash */
419 len = strlen( newpath );
420 if ( newpath[len-1] == '\\' )
421 newpath[len-1] = '\0';
423 pstrcpy( clean, newpath );
426 /****************************************************************************
427 ****************************************************************************/
429 BOOL cli_dfs_make_full_path( pstring path, const char *server, const char *share,
430 const char *dir )
432 pstring servicename;
433 char *sharename;
434 const char *directory;
437 /* make a copy so we don't modify the global string 'service' */
439 pstrcpy(servicename, share);
440 sharename = servicename;
442 if (*sharename == '\\') {
444 server = sharename+2;
445 sharename = strchr_m(server,'\\');
447 if (!sharename)
448 return False;
450 *sharename = 0;
451 sharename++;
454 directory = dir;
455 if ( *directory == '\\' )
456 directory++;
458 pstr_sprintf( path, "\\%s\\%s\\%s", server, sharename, directory );
460 return True;
463 /********************************************************************
464 check for dfs referral
465 ********************************************************************/
467 static BOOL cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
469 uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
471 /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
473 if ( !( (flgs2&FLAGS2_32_BIT_ERROR_CODES) && (flgs2&FLAGS2_UNICODE_STRINGS) ) )
474 return False;
476 if ( NT_STATUS_EQUAL( status, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
477 return True;
479 return False;
482 /********************************************************************
483 get the dfs referral link
484 ********************************************************************/
486 BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path,
487 CLIENT_DFS_REFERRAL**refs, size_t *num_refs,
488 uint16 *consumed)
490 unsigned int data_len = 0;
491 unsigned int param_len = 0;
492 uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
493 char param[sizeof(pstring)+2];
494 pstring data;
495 char *rparam=NULL, *rdata=NULL;
496 char *p;
497 size_t pathlen = 2*(strlen(path)+1);
498 uint16 num_referrals;
499 CLIENT_DFS_REFERRAL *referrals = NULL;
501 memset(param, 0, sizeof(param));
502 SSVAL(param, 0, 0x03); /* max referral level */
503 p = &param[2];
505 p += clistr_push(cli, p, path, MIN(pathlen, sizeof(param)-2), STR_TERMINATE);
506 param_len = PTR_DIFF(p, param);
508 if (!cli_send_trans(cli, SMBtrans2,
509 NULL, /* name */
510 -1, 0, /* fid, flags */
511 &setup, 1, 0, /* setup, length, max */
512 param, param_len, 2, /* param, length, max */
513 (char *)&data, data_len, cli->max_xmit /* data, length, max */
514 )) {
515 return False;
518 if (!cli_receive_trans(cli, SMBtrans2,
519 &rparam, &param_len,
520 &rdata, &data_len)) {
521 return False;
524 *consumed = SVAL( rdata, 0 );
525 num_referrals = SVAL( rdata, 2 );
527 if ( num_referrals != 0 ) {
528 uint16 ref_version;
529 uint16 ref_size;
530 int i;
531 uint16 node_offset;
534 referrals = SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL, num_referrals );
536 /* start at the referrals array */
538 p = rdata+8;
539 for ( i=0; i<num_referrals; i++ ) {
540 ref_version = SVAL( p, 0 );
541 ref_size = SVAL( p, 2 );
542 node_offset = SVAL( p, 16 );
544 if ( ref_version != 3 ) {
545 p += ref_size;
546 continue;
549 referrals[i].proximity = SVAL( p, 8 );
550 referrals[i].ttl = SVAL( p, 10 );
552 clistr_pull( cli, referrals[i].dfspath, p+node_offset,
553 sizeof(referrals[i].dfspath), -1, STR_TERMINATE|STR_UNICODE );
555 p += ref_size;
560 *num_refs = num_referrals;
561 *refs = referrals;
563 SAFE_FREE(rdata);
564 SAFE_FREE(rparam);
566 return True;
569 /********************************************************************
570 ********************************************************************/
572 BOOL cli_resolve_path( const char *mountpt, struct cli_state *rootcli, const char *path,
573 struct cli_state **targetcli, pstring targetpath )
575 CLIENT_DFS_REFERRAL *refs = NULL;
576 size_t num_refs;
577 uint16 consumed;
578 struct cli_state *cli_ipc;
579 pstring fullpath, cleanpath;
580 int pathlen;
581 fstring server, share;
582 struct cli_state *newcli;
583 pstring newpath;
584 pstring newmount;
585 char *ppath;
587 SMB_STRUCT_STAT sbuf;
588 uint32 attributes;
590 *targetcli = NULL;
592 if ( !rootcli || !path || !targetcli )
593 return False;
595 /* send a trans2_query_path_info to check for a referral */
597 clean_path( cleanpath, path );
598 cli_dfs_make_full_path( fullpath, rootcli->desthost, rootcli->share, cleanpath );
600 /* don't bother continuing if this is not a dfs root */
602 if ( !rootcli->dfsroot || cli_qpathinfo_basic( rootcli, cleanpath, &sbuf, &attributes ) ) {
603 *targetcli = rootcli;
604 pstrcpy( targetpath, path );
605 return True;
608 /* special case where client asked for a path that does not exist */
610 if ( cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND) ) {
611 *targetcli = rootcli;
612 pstrcpy( targetpath, path );
613 return True;
616 /* we got an error, check for DFS referral */
618 if ( !cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED) )
619 return False;
621 /* check for the referral */
623 if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) )
624 return False;
626 if ( !cli_dfs_get_referral(cli_ipc, fullpath, &refs, &num_refs, &consumed)
627 || !num_refs )
629 return False;
632 /* just store the first referral for now
633 Make sure to recreate the original string including any wildcards */
635 cli_dfs_make_full_path( fullpath, rootcli->desthost, rootcli->share, path );
636 pathlen = strlen( fullpath )*2;
637 consumed = MIN(pathlen, consumed );
638 pstrcpy( targetpath, &fullpath[consumed/2] );
640 split_dfs_path( refs[0].dfspath, server, share );
641 SAFE_FREE( refs );
643 /* open the connection to the target path */
645 if ( (*targetcli = cli_cm_open(server, share, False)) == NULL ) {
646 d_printf("Unable to follow dfs referral [//%s/%s]\n",
647 server, share );
649 return False;
652 /* parse out the consumed mount path */
653 /* trim off the \server\share\ */
655 fullpath[consumed/2] = '\0';
656 dos_clean_name( fullpath );
657 ppath = strchr_m( fullpath, '\\' );
658 ppath = strchr_m( ppath+1, '\\' );
659 ppath = strchr_m( ppath+1, '\\' );
660 ppath++;
662 pstr_sprintf( newmount, "%s\\%s", mountpt, ppath );
663 cli_cm_set_mntpoint( *targetcli, newmount );
665 /* check for another dfs referral, note that we are not
666 checking for loops here */
668 if ( !strequal( targetpath, "\\" ) ) {
669 if ( cli_resolve_path( newmount, *targetcli, targetpath, &newcli, newpath ) ) {
670 *targetcli = newcli;
671 pstrcpy( targetpath, newpath );
675 return True;
678 /********************************************************************
679 ********************************************************************/
681 BOOL cli_check_msdfs_proxy( struct cli_state *cli, const char *sharename,
682 fstring newserver, fstring newshare )
684 CLIENT_DFS_REFERRAL *refs = NULL;
685 size_t num_refs;
686 uint16 consumed;
687 struct cli_state *cli_ipc;
688 pstring fullpath;
690 if ( !cli || !sharename )
691 return False;
693 /* special case. never check for a referral on the IPC$ share */
695 if ( strequal( sharename, "IPC$" ) )
696 return False;
698 /* send a trans2_query_path_info to check for a referral */
700 pstr_sprintf( fullpath, "\\%s\\%s", cli->desthost, sharename );
702 /* check for the referral */
704 if ( !(cli_ipc = cli_cm_open( cli->desthost, "IPC$", False )) )
705 return False;
707 if ( !cli_dfs_get_referral(cli_ipc, fullpath, &refs, &num_refs, &consumed)
708 || !num_refs )
710 return False;
713 split_dfs_path( refs[0].dfspath, newserver, newshare );
715 /* check that this is not a self-referral */
717 if ( strequal( cli->desthost, newserver ) && strequal( sharename, newshare ) )
718 return False;
720 SAFE_FREE( refs );
722 return True;