]> git.samba.org - metze/samba/wip.git/blob - source3/libsmb/clidfs.c
r21133: - Apply patch from ages ago, which should allow following \\server\share...
[metze/samba/wip.git] / source3 / libsmb / clidfs.c
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
6       
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24
25 struct client_connection {
26         struct client_connection *prev, *next;
27         struct cli_state *cli;
28         pstring mount;
29 };
30
31 /* global state....globals reek! */
32
33 static pstring username;
34 static pstring password;
35 static BOOL use_kerberos;
36 static BOOL got_pass;
37 static int signing_state;
38 int max_protocol = PROTOCOL_NT1;
39
40 static int port;
41 static int name_type = 0x20;
42 static BOOL have_ip;
43 static struct in_addr dest_ip;
44
45 static struct client_connection *connections;
46
47 /********************************************************************
48  Return a connection to a server.
49 ********************************************************************/
50
51 static struct cli_state *do_connect( const char *server, const char *share,
52                                      BOOL show_sessetup )
53 {
54         struct cli_state *c = NULL;
55         struct nmb_name called, calling;
56         const char *server_n;
57         struct in_addr ip;
58         pstring servicename;
59         char *sharename;
60         fstring newserver, newshare;
61         
62         /* make a copy so we don't modify the global string 'service' */
63         pstrcpy(servicename, share);
64         sharename = servicename;
65         if (*sharename == '\\') {
66                 server = sharename+2;
67                 sharename = strchr_m(server,'\\');
68                 if (!sharename) return NULL;
69                 *sharename = 0;
70                 sharename++;
71         }
72
73         server_n = server;
74         
75         zero_ip(&ip);
76
77         make_nmb_name(&calling, global_myname(), 0x0);
78         make_nmb_name(&called , server, name_type);
79
80  again:
81         zero_ip(&ip);
82         if (have_ip) 
83                 ip = dest_ip;
84
85         /* have to open a new connection */
86         if (!(c=cli_initialise()) || (cli_set_port(c, port) != port) ||
87             !cli_connect(c, server_n, &ip)) {
88                 d_printf("Connection to %s failed\n", server_n);
89                 return NULL;
90         }
91
92         c->protocol = max_protocol;
93         c->use_kerberos = use_kerberos;
94         cli_setup_signing_state(c, signing_state);
95                 
96
97         if (!cli_session_request(c, &calling, &called)) {
98                 char *p;
99                 d_printf("session request to %s failed (%s)\n", 
100                          called.name, cli_errstr(c));
101                 cli_shutdown(c);
102                 c = NULL;
103                 if ((p=strchr_m(called.name, '.'))) {
104                         *p = 0;
105                         goto again;
106                 }
107                 if (strcmp(called.name, "*SMBSERVER")) {
108                         make_nmb_name(&called , "*SMBSERVER", 0x20);
109                         goto again;
110                 }
111                 return NULL;
112         }
113
114         DEBUG(4,(" session request ok\n"));
115
116         if (!cli_negprot(c)) {
117                 d_printf("protocol negotiation failed\n");
118                 cli_shutdown(c);
119                 return NULL;
120         }
121
122         if (!got_pass) {
123                 char *pass = getpass("Password: ");
124                 if (pass) {
125                         pstrcpy(password, pass);
126                         got_pass = 1;
127                 }
128         }
129
130         if (!NT_STATUS_IS_OK(cli_session_setup(c, username, 
131                                                password, strlen(password),
132                                                password, strlen(password),
133                                                lp_workgroup()))) {
134                 /* if a password was not supplied then try again with a null username */
135                 if (password[0] || !username[0] || use_kerberos ||
136                     !NT_STATUS_IS_OK(cli_session_setup(c, "", "", 0, "", 0,
137                                                        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;
144                 }
145                 d_printf("Anonymous login successful\n");
146         }
147
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));
155                 }               
156         }
157         DEBUG(4,(" session setup ok\n"));
158
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. */
163
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 );
167         }
168
169         /* must be a normal share */
170
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;
175         }
176
177         DEBUG(4,(" tconx ok\n"));
178
179         return c;
180 }
181
182 /****************************************************************************
183 ****************************************************************************/
184
185 static void cli_cm_set_mntpoint( struct cli_state *c, const char *mnt )
186 {
187         struct client_connection *p;
188         int i;
189
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;
193         }
194         
195         if ( p ) {
196                 pstrcpy( p->mount, mnt );
197                 dos_clean_name( p->mount );
198         }
199 }
200
201 /****************************************************************************
202 ****************************************************************************/
203
204 const char * cli_cm_get_mntpoint( struct cli_state *c )
205 {
206         struct client_connection *p;
207         int i;
208
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;
212         }
213         
214         if ( p )
215                 return p->mount;
216                 
217         return NULL;
218 }
219
220 /********************************************************************
221  Add a new connection to the list
222 ********************************************************************/
223
224 static struct cli_state* cli_cm_connect( const char *server, const char *share,
225                                          BOOL show_hdr )
226 {
227         struct client_connection *node;
228         
229         node = SMB_XMALLOC_P( struct client_connection );
230         
231         node->cli = do_connect( server, share, show_hdr );
232
233         if ( !node->cli ) {
234                 SAFE_FREE( node );
235                 return NULL;
236         }
237
238         DLIST_ADD( connections, node );
239
240         cli_cm_set_mntpoint( node->cli, "" );
241
242         return node->cli;
243
244 }
245
246 /********************************************************************
247  Return a connection to a server.
248 ********************************************************************/
249
250 static struct cli_state* cli_cm_find( const char *server, const char *share )
251 {
252         struct client_connection *p;
253
254         for ( p=connections; p; p=p->next ) {
255                 if ( strequal(server, p->cli->desthost) && strequal(share,p->cli->share) )
256                         return p->cli;
257         }
258
259         return NULL;
260 }
261
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 ****************************************************************************/
266
267 struct cli_state* cli_cm_open( const char *server, const char *share, BOOL show_hdr )
268 {
269         struct cli_state *c;
270         
271         /* try to reuse an existing connection */
272
273         c = cli_cm_find( server, share );
274         
275         if ( !c )
276                 c = cli_cm_connect( server, share, show_hdr );
277
278         return c;
279 }
280
281 /****************************************************************************
282 ****************************************************************************/
283
284 void cli_cm_shutdown( void )
285 {
286
287         struct client_connection *p, *x;
288
289         for ( p=connections; p; ) {
290                 cli_shutdown( p->cli );
291                 x = p;
292                 p = p->next;
293
294                 SAFE_FREE( x );
295         }
296
297         connections = NULL;
298
299         return;
300 }
301
302 /****************************************************************************
303 ****************************************************************************/
304
305 void cli_cm_display(void)
306 {
307         struct client_connection *p;
308         int i;
309
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 );
313         }
314 }
315
316 /****************************************************************************
317 ****************************************************************************/
318
319 void cli_cm_set_credentials( struct user_auth_info *user )
320 {
321         pstrcpy( username, user->username );
322         
323         if ( user->got_pass ) {
324                 pstrcpy( password, user->password );
325                 got_pass = True;
326         }
327         
328         use_kerberos = user->use_kerberos;      
329         signing_state = user->signing_state;
330 }
331
332 /****************************************************************************
333 ****************************************************************************/
334
335 void cli_cm_set_port( int port_number )
336 {
337         port = port_number;
338 }
339
340 /****************************************************************************
341 ****************************************************************************/
342
343 void cli_cm_set_dest_name_type( int type )
344 {
345         name_type = type;
346 }
347
348 /****************************************************************************
349 ****************************************************************************/
350
351 void cli_cm_set_dest_ip(struct in_addr ip )
352 {
353         dest_ip = ip;
354         have_ip = True;
355 }
356
357 /**********************************************************************
358  split a dfs path into the server, share name, and extrapath components
359 **********************************************************************/
360
361 static void split_dfs_path( const char *nodepath, fstring server, fstring share, fstring extrapath )
362 {
363         char *p, *q;
364         pstring path;
365
366         pstrcpy( path, nodepath );
367
368         if ( path[0] != '\\' )
369                 return;
370
371         p = strchr_m( path + 1, '\\' );
372
373         if ( !p )
374                 return;
375
376         *p = '\0';
377         p++;
378
379         /* Look for any extra/deep path */
380         q = strchr_m(p, '\\');
381         if (q != NULL) {
382                 *q = '\0';
383                 q++;
384                 fstrcpy( extrapath, q );
385         } else {
386                 fstrcpy( extrapath, '\0' );
387         }
388         
389         fstrcpy( share, p );
390         fstrcpy( server, &path[1] );
391 }
392
393 /****************************************************************************
394  return the original path truncated at the first wildcard character
395  (also strips trailing \'s).  Trust the caller to provide a NULL 
396  terminated string
397 ****************************************************************************/
398
399 static void clean_path( pstring clean, const char *path )
400 {
401         int len;
402         char *p;
403         pstring newpath;
404                 
405         pstrcpy( newpath, path );
406         p = newpath;
407         
408         while ( p ) {
409                 /* first check for '*' */
410                 
411                 p = strrchr_m( newpath, '*' );
412                 if ( p ) {
413                         *p = '\0';
414                         p = newpath;
415                         continue;
416                 }
417         
418                 /* first check for '?' */
419                 
420                 p = strrchr_m( newpath, '?' );
421                 if ( p ) {
422                         *p = '\0';
423                         p = newpath;
424                 }
425         }
426         
427         /* strip a trailing backslash */
428         
429         len = strlen( newpath );
430         if ( (len > 0) && (newpath[len-1] == '\\') )
431                 newpath[len-1] = '\0';
432                 
433         pstrcpy( clean, newpath );
434 }
435
436 /****************************************************************************
437 ****************************************************************************/
438
439 BOOL cli_dfs_make_full_path( pstring path, const char *server, const char *share,
440                             const char *dir )
441 {
442         pstring servicename;
443         char *sharename;
444         const char *directory;
445
446         
447         /* make a copy so we don't modify the global string 'service' */
448         
449         pstrcpy(servicename, share);
450         sharename = servicename;
451         
452         if (*sharename == '\\') {
453         
454                 server = sharename+2;
455                 sharename = strchr_m(server,'\\');
456                 
457                 if (!sharename) 
458                         return False;
459                         
460                 *sharename = 0;
461                 sharename++;
462         }
463
464         directory = dir;
465         if ( *directory == '\\' )
466                 directory++;
467         
468         pstr_sprintf( path, "\\%s\\%s\\%s", server, sharename, directory );
469
470         return True;
471 }
472
473 /********************************************************************
474  check for dfs referral
475 ********************************************************************/
476
477 static BOOL cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
478 {
479         uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
480
481         /* only deal with DS when we negotiated NT_STATUS codes and UNICODE */
482
483         if ( !( (flgs2&FLAGS2_32_BIT_ERROR_CODES) && (flgs2&FLAGS2_UNICODE_STRINGS) ) )
484                 return False;
485
486         if ( NT_STATUS_EQUAL( status, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
487                 return True;
488
489         return False;
490 }
491
492 /********************************************************************
493  get the dfs referral link
494 ********************************************************************/
495
496 BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path, 
497                            CLIENT_DFS_REFERRAL**refs, size_t *num_refs,
498                            uint16 *consumed)
499 {
500         unsigned int data_len = 0;
501         unsigned int param_len = 0;
502         uint16 setup = TRANSACT2_GET_DFS_REFERRAL;
503         char param[sizeof(pstring)+2];
504         pstring data;
505         char *rparam=NULL, *rdata=NULL;
506         char *p;
507         size_t pathlen = 2*(strlen(path)+1);
508         uint16 num_referrals;
509         CLIENT_DFS_REFERRAL *referrals = NULL;
510         
511         memset(param, 0, sizeof(param));
512         SSVAL(param, 0, 0x03);  /* max referral level */
513         p = &param[2];
514
515         p += clistr_push(cli, p, path, MIN(pathlen, sizeof(param)-2), STR_TERMINATE);
516         param_len = PTR_DIFF(p, param);
517
518         if (!cli_send_trans(cli, SMBtrans2,
519                 NULL,                        /* name */
520                 -1, 0,                          /* fid, flags */
521                 &setup, 1, 0,                   /* setup, length, max */
522                 param, param_len, 2,            /* param, length, max */
523                 (char *)&data,  data_len, cli->max_xmit /* data, length, max */
524                 )) {
525                         return False;
526         }
527
528         if (!cli_receive_trans(cli, SMBtrans2,
529                 &rparam, &param_len,
530                 &rdata, &data_len)) {
531                         return False;
532         }
533         
534         *consumed     = SVAL( rdata, 0 );
535         num_referrals = SVAL( rdata, 2 );
536         
537         if ( num_referrals != 0 ) {
538                 uint16 ref_version;
539                 uint16 ref_size;
540                 int i;
541                 uint16 node_offset;
542                 
543                 
544                 referrals = SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL, num_referrals );
545         
546                 /* start at the referrals array */
547         
548                 p = rdata+8;
549                 for ( i=0; i<num_referrals; i++ ) {
550                         ref_version = SVAL( p, 0 );
551                         ref_size    = SVAL( p, 2 );
552                         node_offset = SVAL( p, 16 );
553                         
554                         if ( ref_version != 3 ) {
555                                 p += ref_size;
556                                 continue;
557                         }
558                         
559                         referrals[i].proximity = SVAL( p, 8 );
560                         referrals[i].ttl       = SVAL( p, 10 );
561
562                         clistr_pull( cli, referrals[i].dfspath, p+node_offset, 
563                                 sizeof(referrals[i].dfspath), -1, STR_TERMINATE|STR_UNICODE );
564
565                         p += ref_size;
566                 }
567         
568         }
569         
570         *num_refs = num_referrals;
571         *refs = referrals;
572
573         SAFE_FREE(rdata);
574         SAFE_FREE(rparam);
575
576         return True;
577 }
578
579 /********************************************************************
580 ********************************************************************/
581
582 BOOL cli_resolve_path( const char *mountpt, struct cli_state *rootcli, const char *path,
583                        struct cli_state **targetcli, pstring targetpath )
584 {
585         CLIENT_DFS_REFERRAL *refs = NULL;
586         size_t num_refs;
587         uint16 consumed;
588         struct cli_state *cli_ipc;
589         pstring fullpath, cleanpath, extrapath;
590         int pathlen;
591         fstring server, share;
592         struct cli_state *newcli;
593         pstring newpath;
594         pstring newmount;
595         char *ppath, *temppath = NULL;
596         
597         SMB_STRUCT_STAT sbuf;
598         uint32 attributes;
599         
600         if ( !rootcli || !path || !targetcli )
601                 return False;
602                 
603         *targetcli = NULL;
604         
605         /* send a trans2_query_path_info to check for a referral */
606         
607         clean_path( cleanpath,  path );
608         cli_dfs_make_full_path( fullpath, rootcli->desthost, rootcli->share, cleanpath );
609
610         /* don't bother continuing if this is not a dfs root */
611         
612         if ( !rootcli->dfsroot || cli_qpathinfo_basic( rootcli, cleanpath, &sbuf, &attributes ) ) {
613                 *targetcli = rootcli;
614                 pstrcpy( targetpath, path );
615                 return True;
616         }
617
618         /* special case where client asked for a path that does not exist */
619
620         if ( cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND) ) {
621                 *targetcli = rootcli;
622                 pstrcpy( targetpath, path );
623                 return True;
624         }
625
626         /* we got an error, check for DFS referral */
627                         
628         if ( !cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED) ) 
629                 return False;
630
631         /* check for the referral */
632
633         if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) )
634                 return False;
635         
636         if ( !cli_dfs_get_referral(cli_ipc, fullpath, &refs, &num_refs, &consumed) 
637                 || !num_refs )
638         {
639                 return False;
640         }
641         
642         /* just store the first referral for now
643            Make sure to recreate the original string including any wildcards */
644         
645         cli_dfs_make_full_path( fullpath, rootcli->desthost, rootcli->share, path );
646         pathlen = strlen( fullpath )*2;
647         consumed = MIN(pathlen, consumed );
648         pstrcpy( targetpath, &fullpath[consumed/2] );
649
650         split_dfs_path( refs[0].dfspath, server, share, extrapath );
651         SAFE_FREE( refs );
652
653         if (strlen(extrapath) > 0) {
654                 string_append(&temppath, extrapath);
655                 string_append(&temppath, targetpath);
656                 pstrcpy( targetpath, temppath );
657         }
658         
659         /* open the connection to the target path */
660         
661         if ( (*targetcli = cli_cm_open(server, share, False)) == NULL ) {
662                 d_printf("Unable to follow dfs referral [//%s/%s]\n",
663                         server, share );
664                         
665                 return False;
666         }
667         
668         /* parse out the consumed mount path */
669         /* trim off the \server\share\ */
670
671         fullpath[consumed/2] = '\0';
672         dos_clean_name( fullpath );
673         if ((ppath = strchr_m( fullpath, '\\' )) == NULL)
674                 return False;
675         if ((ppath = strchr_m( ppath+1, '\\' )) == NULL)
676                 return False;
677         if ((ppath = strchr_m( ppath+1, '\\' )) == NULL)
678                 return False;
679         ppath++;
680         
681         pstr_sprintf( newmount, "%s\\%s", mountpt, ppath );
682         cli_cm_set_mntpoint( *targetcli, newmount );
683
684         /* check for another dfs referral, note that we are not 
685            checking for loops here */
686
687         if ( !strequal( targetpath, "\\" ) ) {
688                 if ( cli_resolve_path( newmount, *targetcli, targetpath, &newcli, newpath ) ) {
689                         *targetcli = newcli;
690                         pstrcpy( targetpath, newpath );
691                 }
692         }
693
694         return True;
695 }
696
697 /********************************************************************
698 ********************************************************************/
699
700 BOOL cli_check_msdfs_proxy( struct cli_state *cli, const char *sharename,
701                             fstring newserver, fstring newshare )
702 {
703         CLIENT_DFS_REFERRAL *refs = NULL;
704         size_t num_refs;
705         uint16 consumed;
706         pstring fullpath;
707         BOOL res;
708         uint16 cnum;
709         fstring newextrapath;
710         
711         if ( !cli || !sharename )
712                 return False;
713
714         cnum = cli->cnum;
715
716         /* special case.  never check for a referral on the IPC$ share */
717
718         if ( strequal( sharename, "IPC$" ) )
719                 return False;
720                 
721         /* send a trans2_query_path_info to check for a referral */
722         
723         pstr_sprintf( fullpath, "\\%s\\%s", cli->desthost, sharename );
724
725         /* check for the referral */
726
727         if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
728                 return False;
729         }
730
731         res = cli_dfs_get_referral(cli, fullpath, &refs, &num_refs, &consumed);
732
733         if (!cli_tdis(cli)) {
734                 SAFE_FREE( refs );
735                 return False;
736         }
737
738         cli->cnum = cnum;
739                 
740         if (!res || !num_refs ) {
741                 SAFE_FREE( refs );
742                 return False;
743         }
744         
745         split_dfs_path( refs[0].dfspath, newserver, newshare, newextrapath );
746
747         /* check that this is not a self-referral */
748
749         if ( strequal( cli->desthost, newserver ) && strequal( sharename, newshare ) ) {
750                 SAFE_FREE( refs );
751                 return False;
752         }
753         
754         SAFE_FREE( refs );
755         
756         return True;
757 }