This is a large patch (sorry). Migrate from struct in_addr
[nivanova/samba-autobuild/.git] / source3 / libsmb / clidfs.c
index 867e8e9ba0a8ccaf604289800da5ed599501fe73..e1ca924b0925ab8045ea52a523ddc87d83aa2b97 100644 (file)
@@ -3,10 +3,11 @@
    client connect/disconnect routines
    Copyright (C) Andrew Tridgell                  1994-1998
    Copyright (C) Gerald (Jerry) Carter            2004
+   Copyright (C) Jeremy Allison                   2007
       
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define NO_SYSLOG
-
 #include "includes.h"
 
+/********************************************************************
+ Important point.
+
+ DFS paths are *always* of the form \server\share\<pathname> (the \ characters
+ are not C escaped here).
+
+ - but if we're using POSIX paths then <pathname> may contain
+   '/' separators, not '\\' separators. So cope with '\\' or '/'
+   as a separator when looking at the pathname part.... JRA.
+********************************************************************/
 
 struct client_connection {
        struct client_connection *prev, *next;
@@ -34,15 +42,15 @@ struct client_connection {
 
 static pstring username;
 static pstring password;
-static BOOL use_kerberos;
-static BOOL got_pass;
+static bool use_kerberos;
+static bool got_pass;
 static int signing_state;
+int max_protocol = PROTOCOL_NT1;
 
 static int port;
 static int name_type = 0x20;
-static int max_protocol = PROTOCOL_NT1;
-static BOOL have_ip;
-static struct in_addr dest_ip;
+static bool have_ip;
+static struct sockaddr_storage dest_ss;
 
 static struct client_connection *connections;
 
@@ -51,14 +59,16 @@ static struct client_connection *connections;
 ********************************************************************/
 
 static struct cli_state *do_connect( const char *server, const char *share,
-                                     BOOL show_sessetup )
+                                     bool show_sessetup )
 {
-       struct cli_state *c;
+       struct cli_state *c = NULL;
        struct nmb_name called, calling;
        const char *server_n;
-       struct in_addr ip;
+       struct sockaddr_storage ss;
        pstring servicename;
        char *sharename;
+       fstring newserver, newshare;
+       NTSTATUS status;
        
        /* make a copy so we don't modify the global string 'service' */
        pstrcpy(servicename, share);
@@ -73,22 +83,26 @@ static struct cli_state *do_connect( const char *server, const char *share,
 
        server_n = server;
        
-       zero_ip(&ip);
+       zero_addr(&ss, AF_INET);
 
        make_nmb_name(&calling, global_myname(), 0x0);
        make_nmb_name(&called , server, name_type);
 
  again:
-       zero_ip(&ip);
-       if (have_ip) 
-               ip = dest_ip;
+       zero_addr(&ss, AF_INET);
+       if (have_ip)
+               ss = dest_ss;
 
        /* have to open a new connection */
-       if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) != port) ||
-           !cli_connect(c, server_n, &ip)) {
+       if (!(c=cli_initialise()) || (cli_set_port(c, port) != port)) {
                d_printf("Connection to %s failed\n", server_n);
                return NULL;
        }
+       status = cli_connect(c, server_n, &ss);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_printf("Connection to %s failed (Error %s)\n", server_n, nt_errstr(status));
+               return NULL;
+       }
 
        c->protocol = max_protocol;
        c->use_kerberos = use_kerberos;
@@ -100,6 +114,7 @@ static struct cli_state *do_connect( const char *server, const char *share,
                d_printf("session request to %s failed (%s)\n", 
                         called.name, cli_errstr(c));
                cli_shutdown(c);
+               c = NULL;
                if ((p=strchr_m(called.name, '.'))) {
                        *p = 0;
                        goto again;
@@ -127,13 +142,14 @@ static struct cli_state *do_connect( const char *server, const char *share,
                }
        }
 
-       if (!cli_session_setup(c, username, 
-                              password, strlen(password),
-                              password, strlen(password),
-                              lp_workgroup())) {
+       if (!NT_STATUS_IS_OK(cli_session_setup(c, username, 
+                                              password, strlen(password),
+                                              password, strlen(password),
+                                              lp_workgroup()))) {
                /* if a password was not supplied then try again with a null username */
                if (password[0] || !username[0] || use_kerberos ||
-                   !cli_session_setup(c, "", "", 0, "", 0, lp_workgroup())) { 
+                   !NT_STATUS_IS_OK(cli_session_setup(c, "", "", 0, "", 0,
+                                                      lp_workgroup()))) { 
                        d_printf("session setup failed: %s\n", cli_errstr(c));
                        if (NT_STATUS_V(cli_nt_error(c)) == 
                            NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED))
@@ -155,8 +171,19 @@ static struct cli_state *do_connect( const char *server, const char *share,
        }
        DEBUG(4,(" session setup ok\n"));
 
-       if (!cli_send_tconX(c, sharename, "?????",
-                           password, strlen(password)+1)) {
+       /* here's the fun part....to support 'msdfs proxy' shares
+          (on Samba or windows) we have to issues a TRANS_GET_DFS_REFERRAL 
+          here before trying to connect to the original share.
+          check_dfs_proxy() will fail if it is a normal share. */
+
+       if ( (c->capabilities & CAP_DFS) && cli_check_msdfs_proxy( c, sharename, newserver, newshare ) ) {
+               cli_shutdown(c);
+               return do_connect( newserver, newshare, False );
+       }
+
+       /* must be a normal share */
+
+       if (!cli_send_tconX(c, sharename, "?????", password, strlen(password)+1)) {
                d_printf("tree connect failed: %s\n", cli_errstr(c));
                cli_shutdown(c);
                return NULL;
@@ -182,14 +209,14 @@ static void cli_cm_set_mntpoint( struct cli_state *c, const char *mnt )
        
        if ( p ) {
                pstrcpy( p->mount, mnt );
-               dos_clean_name( p->mount );
+               clean_name(p->mount);
        }
 }
 
 /****************************************************************************
 ****************************************************************************/
 
-const char * cli_cm_get_mntpoint( struct cli_state *c )
+const char *cli_cm_get_mntpoint( struct cli_state *c )
 {
        struct client_connection *p;
        int i;
@@ -209,8 +236,9 @@ const char * cli_cm_get_mntpoint( struct cli_state *c )
  Add a new connection to the list
 ********************************************************************/
 
-static struct cli_state* cli_cm_connect( const char *server, const char *share,
-                                         BOOL show_hdr )
+static struct cli_state *cli_cm_connect( const char *server,
+                                       const char *share,
+                                       bool show_hdr)
 {
        struct client_connection *node;
        
@@ -235,7 +263,7 @@ static struct cli_state* cli_cm_connect( const char *server, const char *share,
  Return a connection to a server.
 ********************************************************************/
 
-static struct cli_statecli_cm_find( const char *server, const char *share )
+static struct cli_state *cli_cm_find( const char *server, const char *share )
 {
        struct client_connection *p;
 
@@ -252,7 +280,9 @@ static struct cli_state* cli_cm_find( const char *server, const char *share )
  global variable as a side-effect (but only if the connection is successful).
 ****************************************************************************/
 
-struct cli_state* cli_cm_open( const char *server, const char *share, BOOL show_hdr )
+struct cli_state *cli_cm_open(const char *server,
+                               const char *share,
+                               bool show_hdr)
 {
        struct cli_state *c;
        
@@ -260,8 +290,9 @@ struct cli_state* cli_cm_open( const char *server, const char *share, BOOL show_
 
        c = cli_cm_find( server, share );
        
-       if ( !c )
-               c = cli_cm_connect( server, share, show_hdr );
+       if ( !c ) {
+               c = cli_cm_connect(server, share, show_hdr);
+       }
 
        return c;
 }
@@ -283,7 +314,6 @@ void cli_cm_shutdown( void )
        }
 
        connections = NULL;
-
        return;
 }
 
@@ -336,123 +366,117 @@ void cli_cm_set_dest_name_type( int type )
 /****************************************************************************
 ****************************************************************************/
 
-void cli_cm_set_dest_ip(struct in_addr ip )
+void cli_cm_set_dest_ss(struct sockaddr_storage *pss)
 {
-       dest_ip = ip;
-       have_ip = True;
+       dest_ss = *pss;
+       have_ip = true;
 }
 
-/********************************************************************
- split a dfs path into the server and share name components
-********************************************************************/
+/**********************************************************************
+ split a dfs path into the server, share name, and extrapath components
+**********************************************************************/
 
-static void split_dfs_path( const char *nodepath, fstring server, fstring share )
+static void split_dfs_path( const char *nodepath, fstring server, fstring share, pstring extrapath )
 {
-       char *p;
+       char *p, *q;
        pstring path;
 
        pstrcpy( path, nodepath );
 
-       if ( path[0] != '\\' )
+       if ( path[0] != '\\' ) {
                return;
+       }
 
-       p = strrchr_m( path, '\\' );
-
-       if ( !p )
+       p = strchr_m( path + 1, '\\' );
+       if ( !p ) {
                return;
+       }
 
        *p = '\0';
        p++;
 
+       /* Look for any extra/deep path */
+       q = strchr_m(p, '\\');
+       if (q != NULL) {
+               *q = '\0';
+               q++;
+               pstrcpy( extrapath, q );
+       } else {
+               pstrcpy( extrapath, '\0' );
+       }
+       
        fstrcpy( share, p );
        fstrcpy( server, &path[1] );
 }
 
 /****************************************************************************
- return the original path truncated at the first wildcard character
(also strips trailing \'s).  Trust the caller to provide a NULL 
+ Return the original path truncated at the directory component before
the first wildcard character. Trust the caller to provide a NULL 
  terminated string
 ****************************************************************************/
 
-static void clean_path( pstring clean, const char *path )
+static void clean_path(const char *path, pstring path_out)
 {
-       int len;
-       char *p;
-       pstring newpath;
+       size_t len;
+       char *p1, *p2, *p;
                
-       pstrcpy( newpath, path );
-       p = newpath;
-       
-       while ( p ) {
-               /* first check for '*' */
-               
-               p = strrchr_m( newpath, '*' );
-               if ( p ) {
-                       *p = '\0';
-                       p = newpath;
-                       continue;
+       /* No absolute paths. */
+       while (IS_DIRECTORY_SEP(*path)) {
+               path++;
+       }
+
+       pstrcpy(path_out, path);
+
+       p1 = strchr_m(path_out, '*');
+       p2 = strchr_m(path_out, '?');
+
+       if (p1 || p2) {
+               if (p1 && p2) {
+                       p = MIN(p1,p2);
+               } else if (!p1) {
+                       p = p2;
+               } else {
+                       p = p1;
                }
-       
-               /* first check for '?' */
-               
-               p = strrchr_m( newpath, '?' );
-               if ( p ) {
+               *p = '\0';
+
+               /* Now go back to the start of this component. */
+               p1 = strrchr_m(path_out, '/');
+               p2 = strrchr_m(path_out, '\\');
+               p = MAX(p1,p2);
+               if (p) {
                        *p = '\0';
-                       p = newpath;
                }
        }
-       
-       /* strip a trailing backslash */
-       
-       len = strlen( newpath );
-       if ( newpath[len-1] == '\\' )
-               newpath[len-1] = '\0';
-               
-       pstrcpy( clean, newpath );
+
+       /* Strip any trailing separator */
+
+       len = strlen(path_out);
+       if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) {
+               path_out[len-1] = '\0';
+       }
 }
 
 /****************************************************************************
 ****************************************************************************/
 
-static BOOL make_full_path( pstring path, const char *server, const char *share,
-                            const char *dir )
+static void cli_dfs_make_full_path( struct cli_state *cli,
+                                       const char *dir,
+                                       pstring path_out)
 {
-       pstring servicename;
-       char *sharename;
-       const char *directory;
-
-       
-       /* make a copy so we don't modify the global string 'service' */
-       
-       pstrcpy(servicename, share);
-       sharename = servicename;
-       
-       if (*sharename == '\\') {
-       
-               server = sharename+2;
-               sharename = strchr_m(server,'\\');
-               
-               if (!sharename) 
-                       return False;
-                       
-               *sharename = 0;
-               sharename++;
+       /* Ensure the extrapath doesn't start with a separator. */
+       while (IS_DIRECTORY_SEP(*dir)) {
+               dir++;
        }
 
-       directory = dir;
-       if ( *directory == '\\' )
-               directory++;
-       
-       pstr_sprintf( path, "\\%s\\%s\\%s", server, sharename, directory );
-
-       return True;
+       pstr_sprintf( path_out, "\\%s\\%s\\%s", cli->desthost, cli->share, dir);
 }
 
 /********************************************************************
  check for dfs referral
 ********************************************************************/
 
-static BOOL cli_dfs_check_error( struct cli_state *cli )
+static bool cli_dfs_check_error( struct cli_state *cli, NTSTATUS status )
 {
        uint32 flgs2 = SVAL(cli->inbuf,smb_flg2);
 
@@ -461,7 +485,7 @@ static BOOL cli_dfs_check_error( struct cli_state *cli )
        if ( !( (flgs2&FLAGS2_32_BIT_ERROR_CODES) && (flgs2&FLAGS2_UNICODE_STRINGS) ) )
                return False;
 
-       if ( NT_STATUS_EQUAL( NT_STATUS_PATH_NOT_COVERED, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
+       if ( NT_STATUS_EQUAL( status, NT_STATUS(IVAL(cli->inbuf,smb_rcls)) ) )
                return True;
 
        return False;
@@ -471,9 +495,11 @@ static BOOL cli_dfs_check_error( struct cli_state *cli )
  get the dfs referral link
 ********************************************************************/
 
-BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path, 
-                           CLIENT_DFS_REFERRAL**refs, size_t *num_refs,
-                          uint16 *consumed)
+bool cli_dfs_get_referral( struct cli_state *cli,
+                       const char *path, 
+                       CLIENT_DFS_REFERRAL**refs,
+                       size_t *num_refs,
+                       uint16 *consumed)
 {
        unsigned int data_len = 0;
        unsigned int param_len = 0;
@@ -484,7 +510,7 @@ BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path,
        char *p;
        size_t pathlen = 2*(strlen(path)+1);
        uint16 num_referrals;
-       CLIENT_DFS_REFERRAL *referrals;
+       CLIENT_DFS_REFERRAL *referrals = NULL;
        
        memset(param, 0, sizeof(param));
        SSVAL(param, 0, 0x03);  /* max referral level */
@@ -517,10 +543,9 @@ BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path,
                uint16 ref_size;
                int i;
                uint16 node_offset;
-               
-               
+
                referrals = SMB_XMALLOC_ARRAY( CLIENT_DFS_REFERRAL, num_referrals );
-       
+
                /* start at the referrals array */
        
                p = rdata+8;
@@ -542,7 +567,6 @@ BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path,
 
                        p += ref_size;
                }
-       
        }
        
        *num_refs = num_referrals;
@@ -554,103 +578,229 @@ BOOL cli_dfs_get_referral( struct cli_state *cli, const char *path,
        return True;
 }
 
+
 /********************************************************************
 ********************************************************************/
 
-BOOL cli_resolve_path( const char *mountpt, struct cli_state *rootcli, const char *path,
-                       struct cli_state **targetcli, pstring targetpath )
+bool cli_resolve_path( const char *mountpt,
+                       struct cli_state *rootcli,
+                       const char *path,
+                       struct cli_state **targetcli,
+                       pstring targetpath)
 {
        CLIENT_DFS_REFERRAL *refs = NULL;
        size_t num_refs;
        uint16 consumed;
        struct cli_state *cli_ipc;
-       pstring fullpath, cleanpath;
+       pstring dfs_path, cleanpath, extrapath;
        int pathlen;
        fstring server, share;
        struct cli_state *newcli;
        pstring newpath;
        pstring newmount;
-       char *ppath;
+       char *ppath, *temppath = NULL;
        
        SMB_STRUCT_STAT sbuf;
        uint32 attributes;
        
-       *targetcli = NULL;
-       
-       if ( !rootcli || !path || !targetcli )
+       if ( !rootcli || !path || !targetcli ) {
                return False;
+       }
                
-       /* send a trans2_query_path_info to check for a referral */
-       
-       clean_path( cleanpath,  path );
-       make_full_path( fullpath, rootcli->desthost, rootcli->share, cleanpath );
+       /* Don't do anything if this is not a DFS root. */
 
-       /* don't bother continuing if this is not a dfs root */
-       
-       if ( !rootcli->dfsroot || cli_qpathinfo_basic( rootcli, cleanpath, &sbuf, &attributes ) ) {
+       if ( !rootcli->dfsroot) {
                *targetcli = rootcli;
                pstrcpy( targetpath, path );
                return True;
        }
 
-       /* we got an error, check for DFS referral */
-                       
-       if ( !cli_dfs_check_error(rootcli) )
+       *targetcli = NULL;
+
+       /* Send a trans2_query_path_info to check for a referral. */
+
+       clean_path(path, cleanpath);
+       cli_dfs_make_full_path(rootcli, cleanpath, dfs_path );
+
+       if (cli_qpathinfo_basic( rootcli, dfs_path, &sbuf, &attributes ) ) {
+               /* This is an ordinary path, just return it. */
+               *targetcli = rootcli;
+               pstrcpy( targetpath, path );
+               goto done;
+       }
+
+       /* Special case where client asked for a path that does not exist */
+
+       if ( cli_dfs_check_error(rootcli, NT_STATUS_OBJECT_NAME_NOT_FOUND) ) {
+               *targetcli = rootcli;
+               pstrcpy( targetpath, path );
+               goto done;
+       }
+
+       /* We got an error, check for DFS referral. */
+
+       if ( !cli_dfs_check_error(rootcli, NT_STATUS_PATH_NOT_COVERED))  {
                return False;
+       }
 
-       /* check for the referral */
+       /* Check for the referral. */
 
-       if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) )
+       if ( !(cli_ipc = cli_cm_open( rootcli->desthost, "IPC$", False )) ) {
                return False;
+       }
        
-       if ( !cli_dfs_get_referral(cli_ipc, fullpath, &refs, &num_refs, &consumed) 
-               || !num_refs )
-       {
+       if ( !cli_dfs_get_referral(cli_ipc, dfs_path, &refs, &num_refs, &consumed) 
+                       || !num_refs ) {
                return False;
        }
        
-       /* just store the first referral for now
-          Make sure to recreate the original string including any wildcards */
+       /* Just store the first referral for now. */
+
+       split_dfs_path( refs[0].dfspath, server, share, extrapath );
+       SAFE_FREE(refs);
+
+       /* Make sure to recreate the original string including any wildcards. */
        
-       make_full_path( fullpath, rootcli->desthost, rootcli->share, path );
-       pathlen = strlen( fullpath )*2;
+       cli_dfs_make_full_path( rootcli, path, dfs_path);
+       pathlen = strlen( dfs_path )*2;
        consumed = MIN(pathlen, consumed );
-       pstrcpy( targetpath, &fullpath[consumed/2] );
+       pstrcpy( targetpath, &dfs_path[consumed/2] );
+       dfs_path[consumed/2] = '\0';
 
-       split_dfs_path( refs[0].dfspath, server, share );
-       SAFE_FREE( refs );
-       
-       /* open the connection to the target path */
+       /*
+        * targetpath is now the unconsumed part of the path.
+        * dfs_path is now the consumed part of the path (in \server\share\path format).
+        */
+
+       /* Open the connection to the target server & share */
        
        if ( (*targetcli = cli_cm_open(server, share, False)) == NULL ) {
-               d_printf("Unable to follow dfs referral [//%s/%s]\n",
+               d_printf("Unable to follow dfs referral [\\%s\\%s]\n",
                        server, share );
-                       
                return False;
        }
        
+       if (strlen(extrapath) > 0) {
+               string_append(&temppath, extrapath);
+               string_append(&temppath, targetpath);
+               pstrcpy( targetpath, temppath );
+       }
+       
        /* parse out the consumed mount path */
        /* trim off the \server\share\ */
 
-       fullpath[consumed/2] = '\0';
-       dos_clean_name( fullpath );
-       ppath = strchr_m( fullpath, '\\' );
-       ppath = strchr_m( ppath+1, '\\' );
-       ppath = strchr_m( ppath+1, '\\' );
-       ppath++;
+       ppath = dfs_path;
+
+       if (*ppath != '\\') {
+               d_printf("cli_resolve_path: dfs_path (%s) not in correct format.\n",
+                       dfs_path );
+               return False;
+       }
+
+       ppath++; /* Now pointing at start of server name. */
        
+       if ((ppath = strchr_m( dfs_path, '\\' )) == NULL) {
+               return False;
+       }
+
+       ppath++; /* Now pointing at start of share name. */
+
+       if ((ppath = strchr_m( ppath+1, '\\' )) == NULL) {
+               return False;
+       }
+
+       ppath++; /* Now pointing at path component. */
+
        pstr_sprintf( newmount, "%s\\%s", mountpt, ppath );
+
        cli_cm_set_mntpoint( *targetcli, newmount );
 
-       /* check for another dfs referral, note that we are not 
-          checking for loops here */
+       /* Check for another dfs referral, note that we are not 
+          checking for loops here. */
 
-       if ( !strequal( targetpath, "\\" ) ) {
+       if ( !strequal( targetpath, "\\" ) &&  !strequal( targetpath, "/")) {
                if ( cli_resolve_path( newmount, *targetcli, targetpath, &newcli, newpath ) ) {
+                       /*
+                        * When cli_resolve_path returns true here it's always
+                        * returning the complete path in newpath, so we're done
+                        * here.
+                        */
                        *targetcli = newcli;
                        pstrcpy( targetpath, newpath );
+                       return True;
                }
        }
 
+  done:
+
+       /* If returning True ensure we return a dfs root full path. */
+       if ( (*targetcli)->dfsroot ) {
+               pstrcpy(dfs_path, targetpath );
+               cli_dfs_make_full_path( *targetcli, dfs_path, targetpath); 
+       }
+
+       return True;
+}
+
+/********************************************************************
+********************************************************************/
+
+bool cli_check_msdfs_proxy( struct cli_state *cli, const char *sharename,
+                            fstring newserver, fstring newshare )
+{
+       CLIENT_DFS_REFERRAL *refs = NULL;
+       size_t num_refs;
+       uint16 consumed;
+       pstring fullpath;
+       bool res;
+       uint16 cnum;
+       pstring newextrapath;
+       
+       if ( !cli || !sharename )
+               return False;
+
+       cnum = cli->cnum;
+
+       /* special case.  never check for a referral on the IPC$ share */
+
+       if ( strequal( sharename, "IPC$" ) ) {
+               return False;
+       }
+               
+       /* send a trans2_query_path_info to check for a referral */
+       
+       pstr_sprintf( fullpath, "\\%s\\%s", cli->desthost, sharename );
+
+       /* check for the referral */
+
+       if (!cli_send_tconX(cli, "IPC$", "IPC", NULL, 0)) {
+               return False;
+       }
+
+       res = cli_dfs_get_referral(cli, fullpath, &refs, &num_refs, &consumed);
+
+       if (!cli_tdis(cli)) {
+               SAFE_FREE( refs );
+               return False;
+       }
+
+       cli->cnum = cnum;
+
+       if (!res || !num_refs ) {
+               SAFE_FREE( refs );
+               return False;
+       }
+       
+       split_dfs_path( refs[0].dfspath, newserver, newshare, newextrapath );
+
+       /* check that this is not a self-referral */
+
+       if ( strequal( cli->desthost, newserver ) && strequal( sharename, newshare ) ) {
+               SAFE_FREE( refs );
+               return False;
+       }
+       
+       SAFE_FREE( refs );
+       
        return True;
 }