r14683: Get rid of hardcoded output file. With no arg, print to stdout,
[Samba.git] / source / libsmb / clidfs.c
blob03cfd4dd400808f623e14a88f22c76f9cbb6f118
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 #include "includes.h"
25 struct client_connection {
26 struct client_connection *prev, *next;
27 struct cli_state *cli;
28 pstring mount;
31 /* global state....globals reek! */
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;
40 static int port;
41 static int name_type = 0x20;
42 static BOOL have_ip;
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,
52 BOOL show_sessetup )
54 struct cli_state *c;
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;
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++;
73 server_n = server;
75 zero_ip(&ip);
77 make_nmb_name(&calling, global_myname(), 0x0);
78 make_nmb_name(&called , server, name_type);
80 again:
81 zero_ip(&ip);
82 if (have_ip)
83 ip = dest_ip;
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);
89 return NULL;
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)) {
98 char *p;
99 d_printf("session request to %s failed (%s)\n",
100 called.name, cli_errstr(c));
101 cli_shutdown(c);
102 if ((p=strchr_m(called.name, '.'))) {
103 *p = 0;
104 goto again;
106 if (strcmp(called.name, "*SMBSERVER")) {
107 make_nmb_name(&called , "*SMBSERVER", 0x20);
108 goto again;
110 return NULL;
113 DEBUG(4,(" session request ok\n"));
115 if (!cli_negprot(c)) {
116 d_printf("protocol negotiation failed\n");
117 cli_shutdown(c);
118 return NULL;
121 if (!got_pass) {
122 char *pass = getpass("Password: ");
123 if (pass) {
124 pstrcpy(password, pass);
125 got_pass = 1;
129 if (!cli_session_setup(c, username,
130 password, strlen(password),
131 password, strlen(password),
132 lp_workgroup())) {
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");
140 cli_shutdown(c);
141 return NULL;
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 ) ) {
163 cli_shutdown(c);
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));
171 cli_shutdown(c);
172 return NULL;
175 DEBUG(4,(" tconx ok\n"));
177 return c;
180 /****************************************************************************
181 ****************************************************************************/
183 static void cli_cm_set_mntpoint( struct cli_state *c, const char *mnt )
185 struct client_connection *p;
186 int i;
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) )
190 break;
193 if ( p ) {
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;
205 int i;
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) )
209 break;
212 if ( p )
213 return p->mount;
215 return NULL;
218 /********************************************************************
219 Add a new connection to the list
220 ********************************************************************/
222 static struct cli_state* cli_cm_connect( const char *server, const char *share,
223 BOOL show_hdr )
225 struct client_connection *node;
227 node = SMB_XMALLOC_P( struct client_connection );
229 node->cli = do_connect( server, share, show_hdr );
231 if ( !node->cli ) {
232 SAFE_FREE( node );
233 return NULL;
236 DLIST_ADD( connections, node );
238 cli_cm_set_mntpoint( node->cli, "" );
240 return 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) )
254 return p->cli;
257 return NULL;
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 )
267 struct cli_state *c;
269 /* try to reuse an existing connection */
271 c = cli_cm_find( server, share );
273 if ( !c )
274 c = cli_cm_connect( server, share, show_hdr );
276 return c;
279 /****************************************************************************
280 ****************************************************************************/
282 void cli_cm_shutdown( void )
285 struct client_connection *p, *x;
287 for ( p=connections; p; ) {
288 cli_shutdown( p->cli );
289 x = p;
290 p = p->next;
292 SAFE_FREE( x );
295 connections = NULL;
297 return;
300 /****************************************************************************
301 ****************************************************************************/
303 void cli_cm_display(void)
305 struct client_connection *p;
306 int i;
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 );
323 got_pass = True;
326 use_kerberos = user->use_kerberos;
327 signing_state = user->signing_state;
330 /****************************************************************************
331 ****************************************************************************/
333 void cli_cm_set_port( int port_number )
335 port = port_number;
338 /****************************************************************************
339 ****************************************************************************/
341 void cli_cm_set_dest_name_type( int type )
343 name_type = type;
346 /****************************************************************************
347 ****************************************************************************/
349 void cli_cm_set_dest_ip(struct in_addr ip )
351 dest_ip = ip;
352 have_ip = True;
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 )
361 char *p;
362 pstring path;
364 pstrcpy( path, nodepath );
366 if ( path[0] != '\\' )
367 return;
369 p = strrchr_m( path, '\\' );
371 if ( !p )
372 return;
374 *p = '\0';
375 p++;
377 fstrcpy( share, p );
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
384 terminated string
385 ****************************************************************************/
387 static void clean_path( pstring clean, const char *path )
389 int len;
390 char *p;
391 pstring newpath;
393 pstrcpy( newpath, path );
394 p = newpath;
396 while ( p ) {
397 /* first check for '*' */
399 p = strrchr_m( newpath, '*' );
400 if ( p ) {
401 *p = '\0';
402 p = newpath;
403 continue;
406 /* first check for '?' */
408 p = strrchr_m( newpath, '?' );
409 if ( p ) {
410 *p = '\0';
411 p = 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,
428 const char *dir )
430 pstring servicename;
431 char *sharename;
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,'\\');
445 if (!sharename)
446 return False;
448 *sharename = 0;
449 sharename++;
452 directory = dir;
453 if ( *directory == '\\' )
454 directory++;
456 pstr_sprintf( path, "\\%s\\%s\\%s", server, sharename, directory );
458 return True;
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) ) )
472 return False;
474 if ( NT_STATUS_EQUAL( status, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
475 return True;
477 return False;
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,
486 uint16 *consumed)
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];
492 pstring data;
493 char *rparam=NULL, *rdata=NULL;
494 char *p;
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 */
501 p = &param[2];
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,
507 NULL, /* name */
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 */
512 )) {
513 return False;
516 if (!cli_receive_trans(cli, SMBtrans2,
517 &rparam, &param_len,
518 &rdata, &data_len)) {
519 return False;
522 *consumed = SVAL( rdata, 0 );
523 num_referrals = SVAL( rdata, 2 );
525 if ( num_referrals != 0 ) {
526 uint16 ref_version;
527 uint16 ref_size;
528 int i;
529 uint16 node_offset;
532 referrals = SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL, num_referrals );
534 /* start at the referrals array */
536 p = rdata+8;
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 ) {
543 p += ref_size;
544 continue;
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 );
553 p += ref_size;
558 *num_refs = num_referrals;
559 *refs = referrals;
561 SAFE_FREE(rdata);
562 SAFE_FREE(rparam);
564 return True;
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;
574 size_t num_refs;
575 uint16 consumed;
576 struct cli_state *cli_ipc;
577 pstring fullpath, cleanpath;
578 int pathlen;
579 fstring server, share;
580 struct cli_state *newcli;
581 pstring newpath;
582 pstring newmount;
583 char *ppath;
585 SMB_STRUCT_STAT sbuf;
586 uint32 attributes;
588 if ( !rootcli || !path || !targetcli )
589 return False;
591 *targetcli = NULL;
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 );
603 return True;
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 );
611 return True;
614 /* we got an error, check for DFS referral */
616 if ( !cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED) )
617 return False;
619 /* check for the referral */
621 if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) )
622 return False;
624 if ( !cli_dfs_get_referral(cli_ipc, fullpath, &refs, &num_refs, &consumed)
625 || !num_refs )
627 return False;
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 );
639 SAFE_FREE( refs );
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",
645 server, share );
647 return False;
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, '\\' );
658 ppath++;
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 ) ) {
668 *targetcli = newcli;
669 pstrcpy( targetpath, newpath );
673 return True;
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;
683 size_t num_refs;
684 uint16 consumed;
685 pstring fullpath;
686 BOOL res;
687 uint16 cnum;
689 if ( !cli || !sharename )
690 return False;
692 cnum = cli->cnum;
694 /* special case. never check for a referral on the IPC$ share */
696 if ( strequal( sharename, "IPC$" ) )
697 return False;
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)) {
706 return False;
709 res = cli_dfs_get_referral(cli, fullpath, &refs, &num_refs, &consumed);
711 if (!cli_tdis(cli)) {
712 return False;
715 cli->cnum = cnum;
717 if (!res || !num_refs ) {
718 return False;
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 ) )
726 return False;
728 SAFE_FREE( refs );
730 return True;