s4:librpc/rpc: remember some smbXcli_* pointers within struct dcerpc_pipe_connect
[samba.git] / source4 / libcli / clifile.c
index cdb29beb2dd872f380958e6179dc37c24ca8a93e..19288247c4467351f01e3a729b86460a87c17d55 100644 (file)
@@ -7,7 +7,7 @@
    
    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/>.
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "libcli/raw/libcliraw.h"
+#include "libcli/libcli.h"
 
 /****************************************************************************
  Hard/Symlink a file (UNIX extensions).
 
 static NTSTATUS smbcli_link_internal(struct smbcli_tree *tree, 
                                  const char *fname_src, 
-                                 const char *fname_dst, BOOL hard_link)
+                                 const char *fname_dst, bool hard_link)
 {
        union smb_setfileinfo parms;
        NTSTATUS status;
 
        if (hard_link) {
                parms.generic.level = RAW_SFILEINFO_UNIX_HLINK;
-               parms.unix_hlink.file.fname = fname_src;
+               parms.unix_hlink.in.file.path = fname_src;
                parms.unix_hlink.in.link_dest = fname_dst;
        } else {
                parms.generic.level = RAW_SFILEINFO_UNIX_LINK;
-               parms.unix_link.file.fname = fname_src;
+               parms.unix_link.in.file.path = fname_src;
                parms.unix_link.in.link_dest = fname_dst;
        }
        
@@ -49,41 +50,13 @@ static NTSTATUS smbcli_link_internal(struct smbcli_tree *tree,
        return status;
 }
 
-/****************************************************************************
- Map standard UNIX permissions onto wire representations.
-****************************************************************************/
-uint32_t unix_perms_to_wire(mode_t perms)
-{
-        uint_t ret = 0;
-
-        ret |= ((perms & S_IXOTH) ?  UNIX_X_OTH : 0);
-        ret |= ((perms & S_IWOTH) ?  UNIX_W_OTH : 0);
-        ret |= ((perms & S_IROTH) ?  UNIX_R_OTH : 0);
-        ret |= ((perms & S_IXGRP) ?  UNIX_X_GRP : 0);
-        ret |= ((perms & S_IWGRP) ?  UNIX_W_GRP : 0);
-        ret |= ((perms & S_IRGRP) ?  UNIX_R_GRP : 0);
-        ret |= ((perms & S_IXUSR) ?  UNIX_X_USR : 0);
-        ret |= ((perms & S_IWUSR) ?  UNIX_W_USR : 0);
-        ret |= ((perms & S_IRUSR) ?  UNIX_R_USR : 0);
-#ifdef S_ISVTX
-        ret |= ((perms & S_ISVTX) ?  UNIX_STICKY : 0);
-#endif
-#ifdef S_ISGID
-        ret |= ((perms & S_ISGID) ?  UNIX_SET_GID : 0);
-#endif
-#ifdef S_ISUID
-        ret |= ((perms & S_ISUID) ?  UNIX_SET_UID : 0);
-#endif
-        return ret;
-}
-
 /****************************************************************************
  Symlink a file (UNIX extensions).
 ****************************************************************************/
 NTSTATUS smbcli_unix_symlink(struct smbcli_tree *tree, const char *fname_src, 
                          const char *fname_dst)
 {
-       return smbcli_link_internal(tree, fname_src, fname_dst, False);
+       return smbcli_link_internal(tree, fname_src, fname_dst, false);
 }
 
 /****************************************************************************
@@ -92,7 +65,7 @@ NTSTATUS smbcli_unix_symlink(struct smbcli_tree *tree, const char *fname_src,
 NTSTATUS smbcli_unix_hardlink(struct smbcli_tree *tree, const char *fname_src, 
                           const char *fname_dst)
 {
-       return smbcli_link_internal(tree, fname_src, fname_dst, True);
+       return smbcli_link_internal(tree, fname_src, fname_dst, true);
 }
 
 
@@ -108,7 +81,7 @@ static NTSTATUS smbcli_unix_chmod_chown_internal(struct smbcli_tree *tree,
        NTSTATUS status;
 
        parms.generic.level = SMB_SFILEINFO_UNIX_BASIC;
-       parms.unix_basic.file.fname = fname;
+       parms.unix_basic.in.file.path = fname;
        parms.unix_basic.in.uid = uid;
        parms.unix_basic.in.gid = gid;
        parms.unix_basic.in.mode = mode;
@@ -163,13 +136,13 @@ NTSTATUS smbcli_rename(struct smbcli_tree *tree, const char *fname_src,
 ****************************************************************************/
 NTSTATUS smbcli_unlink(struct smbcli_tree *tree, const char *fname)
 {
-       struct smb_unlink parms;
+       union smb_unlink parms;
 
-       parms.in.pattern = fname;
+       parms.unlink.in.pattern = fname;
        if (strchr(fname, '*')) {
-               parms.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
+               parms.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
        } else {
-               parms.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
+               parms.unlink.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
        }
 
        return smb_raw_unlink(tree, &parms);
@@ -205,13 +178,14 @@ NTSTATUS smbcli_rmdir(struct smbcli_tree *tree, const char *dname)
 /****************************************************************************
  Set or clear the delete on close flag.
 ****************************************************************************/
-NTSTATUS smbcli_nt_delete_on_close(struct smbcli_tree *tree, int fnum, BOOL flag)
+NTSTATUS smbcli_nt_delete_on_close(struct smbcli_tree *tree, int fnum, 
+                                  bool flag)
 {
        union smb_setfileinfo parms;
        NTSTATUS status;
 
        parms.disposition_info.level = RAW_SFILEINFO_DISPOSITION_INFO;
-       parms.disposition_info.file.fnum = fnum;
+       parms.disposition_info.in.file.fnum = fnum;
        parms.disposition_info.in.delete_on_close = flag;
        
        status = smb_raw_setfileinfo(tree, &parms);
@@ -239,7 +213,7 @@ int smbcli_nt_create_full(struct smbcli_tree *tree, const char *fname,
 
        open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
        open_parms.ntcreatex.in.flags = CreatFlags;
-       open_parms.ntcreatex.in.root_fid = 0;
+       open_parms.ntcreatex.in.root_fid.fnum = 0;
        open_parms.ntcreatex.in.access_mask = DesiredAccess;
        open_parms.ntcreatex.in.file_attr = FileAttributes;
        open_parms.ntcreatex.in.alloc_size = 0;
@@ -251,10 +225,10 @@ int smbcli_nt_create_full(struct smbcli_tree *tree, const char *fname,
        open_parms.ntcreatex.in.fname = fname;
 
        status = smb_raw_open(tree, mem_ctx, &open_parms);
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
        if (NT_STATUS_IS_OK(status)) {
-               return open_parms.ntcreatex.out.fnum;
+               return open_parms.ntcreatex.out.file.fnum;
        }
        
        return -1;
@@ -269,8 +243,8 @@ int smbcli_open(struct smbcli_tree *tree, const char *fname, int flags,
             int share_mode)
 {
        union smb_open open_parms;
-       uint_t openfn=0;
-       uint_t accessmode=0;
+       unsigned int openfn=0;
+       unsigned int accessmode=0;
        TALLOC_CTX *mem_ctx;
        NTSTATUS status;
 
@@ -318,10 +292,10 @@ int smbcli_open(struct smbcli_tree *tree, const char *fname, int flags,
        open_parms.openx.in.fname = fname;
 
        status = smb_raw_open(tree, mem_ctx, &open_parms);
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
 
        if (NT_STATUS_IS_OK(status)) {
-               return open_parms.openx.out.fnum;
+               return open_parms.openx.out.file.fnum;
        }
 
        return -1;
@@ -337,7 +311,7 @@ NTSTATUS smbcli_close(struct smbcli_tree *tree, int fnum)
        NTSTATUS status;
 
        close_parms.close.level = RAW_CLOSE_CLOSE;
-       close_parms.close.in.fnum = fnum;
+       close_parms.close.in.file.fnum = fnum;
        close_parms.close.in.write_time = 0;
        status = smb_raw_close(tree, &close_parms);
        return status;
@@ -356,7 +330,7 @@ NTSTATUS smbcli_locktype(struct smbcli_tree *tree, int fnum,
        NTSTATUS status;
 
        parms.lockx.level = RAW_LOCK_LOCKX;
-       parms.lockx.in.fnum = fnum;
+       parms.lockx.in.file.fnum = fnum;
        parms.lockx.in.mode = locktype;
        parms.lockx.in.timeout = timeout;
        parms.lockx.in.ulock_cnt = 0;
@@ -384,7 +358,7 @@ NTSTATUS smbcli_lock(struct smbcli_tree *tree, int fnum,
        NTSTATUS status;
 
        parms.lockx.level = RAW_LOCK_LOCKX;
-       parms.lockx.in.fnum = fnum;
+       parms.lockx.in.file.fnum = fnum;
        parms.lockx.in.mode = (lock_type == READ_LOCK? 1 : 0);
        parms.lockx.in.timeout = timeout;
        parms.lockx.in.ulock_cnt = 0;
@@ -410,7 +384,7 @@ NTSTATUS smbcli_unlock(struct smbcli_tree *tree, int fnum, uint32_t offset, uint
        NTSTATUS status;
 
        parms.lockx.level = RAW_LOCK_LOCKX;
-       parms.lockx.in.fnum = fnum;
+       parms.lockx.in.file.fnum = fnum;
        parms.lockx.in.mode = 0;
        parms.lockx.in.timeout = 0;
        parms.lockx.in.ulock_cnt = 1;
@@ -442,7 +416,7 @@ NTSTATUS smbcli_lock64(struct smbcli_tree *tree, int fnum,
        }
 
        parms.lockx.level = RAW_LOCK_LOCKX;
-       parms.lockx.in.fnum = fnum;
+       parms.lockx.in.file.fnum = fnum;
        
        ltype = (lock_type == READ_LOCK? 1 : 0);
        ltype |= LOCKING_ANDX_LARGE_FILES;
@@ -476,7 +450,7 @@ NTSTATUS smbcli_unlock64(struct smbcli_tree *tree, int fnum, off_t offset,
        }
 
        parms.lockx.level = RAW_LOCK_LOCKX;
-       parms.lockx.in.fnum = fnum;
+       parms.lockx.in.file.fnum = fnum;
        parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
        parms.lockx.in.timeout = 0;
        parms.lockx.in.ulock_cnt = 1;
@@ -503,7 +477,7 @@ NTSTATUS smbcli_getattrE(struct smbcli_tree *tree, int fnum,
        NTSTATUS status;
 
        parms.getattre.level = RAW_FILEINFO_GETATTRE;
-       parms.getattre.in.fnum = fnum;
+       parms.getattre.in.file.fnum = fnum;
 
        status = smb_raw_fileinfo(tree, NULL, &parms);
 
@@ -543,7 +517,7 @@ NTSTATUS smbcli_getatr(struct smbcli_tree *tree, const char *fname,
        NTSTATUS status;
 
        parms.getattr.level = RAW_FILEINFO_GETATTR;
-       parms.getattr.in.fname = fname;
+       parms.getattr.in.file.path = fname;
 
        status = smb_raw_pathinfo(tree, NULL, &parms);
        
@@ -574,16 +548,48 @@ NTSTATUS smbcli_setatr(struct smbcli_tree *tree, const char *fname, uint16_t mod
                    time_t t)
 {
        union smb_setfileinfo parms;
-       NTSTATUS status;
 
        parms.setattr.level = RAW_SFILEINFO_SETATTR;
+       parms.setattr.in.file.path = fname;
        parms.setattr.in.attrib = mode;
        parms.setattr.in.write_time = t;
-       parms.setattr.file.fname = fname;
        
-       status = smb_raw_setpathinfo(tree, &parms);
+       return smb_raw_setpathinfo(tree, &parms);
+}
 
-       return status;
+/****************************************************************************
+ Do a setfileinfo basic_info call.
+****************************************************************************/
+NTSTATUS smbcli_fsetatr(struct smbcli_tree *tree, int fnum, uint16_t mode, 
+                       NTTIME create_time, NTTIME access_time, 
+                       NTTIME write_time, NTTIME change_time)
+{
+       union smb_setfileinfo parms;
+
+       parms.basic_info.level = RAW_SFILEINFO_BASIC_INFO;
+       parms.basic_info.in.file.fnum = fnum;
+       parms.basic_info.in.attrib = mode;
+       parms.basic_info.in.create_time = create_time;
+       parms.basic_info.in.access_time = access_time;
+       parms.basic_info.in.write_time = write_time;
+       parms.basic_info.in.change_time = change_time;
+       
+       return smb_raw_setfileinfo(tree, &parms);
+}
+
+
+/****************************************************************************
+ truncate a file to a given size
+****************************************************************************/
+NTSTATUS smbcli_ftruncate(struct smbcli_tree *tree, int fnum, uint64_t size)
+{
+       union smb_setfileinfo parms;
+
+       parms.end_of_file_info.level = RAW_SFILEINFO_END_OF_FILE_INFO;
+       parms.end_of_file_info.in.file.fnum = fnum;
+       parms.end_of_file_info.in.size = size;
+       
+       return smb_raw_setfileinfo(tree, &parms);
 }
 
 
@@ -592,7 +598,7 @@ NTSTATUS smbcli_setatr(struct smbcli_tree *tree, const char *fname, uint16_t mod
 ****************************************************************************/
 NTSTATUS smbcli_chkpath(struct smbcli_tree *tree, const char *path)
 {
-       struct smb_chkpath parms;
+       union smb_chkpath parms;
        char *path2;
        NTSTATUS status;
 
@@ -603,8 +609,8 @@ NTSTATUS smbcli_chkpath(struct smbcli_tree *tree, const char *path)
                path2 = strdup("\\");
        }
 
-       parms.in.path = path2;
-       
+       parms.chkpath.in.path = path2;
+
        status = smb_raw_chkpath(tree, &parms);
 
        free(path2);
@@ -616,7 +622,8 @@ NTSTATUS smbcli_chkpath(struct smbcli_tree *tree, const char *path)
 /****************************************************************************
  Query disk space.
 ****************************************************************************/
-NTSTATUS smbcli_dskattr(struct smbcli_tree *tree, int *bsize, int *total, int *avail)
+NTSTATUS smbcli_dskattr(struct smbcli_tree *tree, uint32_t *bsize, 
+                       uint64_t *total, uint64_t *avail)
 {
        union smb_fsinfo fsinfo_parms;
        TALLOC_CTX *mem_ctx;
@@ -624,15 +631,15 @@ NTSTATUS smbcli_dskattr(struct smbcli_tree *tree, int *bsize, int *total, int *a
 
        mem_ctx = talloc_init("smbcli_dskattr");
 
-       fsinfo_parms.dskattr.level = RAW_QFS_DSKATTR;
+       fsinfo_parms.dskattr.level = RAW_QFS_SIZE_INFO;
        status = smb_raw_fsinfo(tree, mem_ctx, &fsinfo_parms);
        if (NT_STATUS_IS_OK(status)) {
-               *bsize = fsinfo_parms.dskattr.out.block_size;
-               *total = fsinfo_parms.dskattr.out.units_total;
-               *avail = fsinfo_parms.dskattr.out.units_free;
+               *bsize = fsinfo_parms.size_info.out.bytes_per_sector * fsinfo_parms.size_info.out.sectors_per_unit;
+               *total = fsinfo_parms.size_info.out.total_alloc_units;
+               *avail = fsinfo_parms.size_info.out.avail_alloc_units;
        }
 
-       talloc_destroy(mem_ctx);
+       talloc_free(mem_ctx);
        
        return status;
 }
@@ -646,9 +653,10 @@ int smbcli_ctemp(struct smbcli_tree *tree, const char *path, char **tmp_path)
        union smb_open open_parms;
        TALLOC_CTX *mem_ctx;
        NTSTATUS status;
+       int ret = -1;
 
        mem_ctx = talloc_init("raw_open");
-       if (!mem_ctx) return -1;
+       if (!mem_ctx) return ret;
 
        open_parms.openx.level = RAW_OPEN_CTEMP;
        open_parms.ctemp.in.attrib = 0;
@@ -656,12 +664,12 @@ int smbcli_ctemp(struct smbcli_tree *tree, const char *path, char **tmp_path)
        open_parms.ctemp.in.write_time = 0;
 
        status = smb_raw_open(tree, mem_ctx, &open_parms);
-       if (tmp_path) {
-               *tmp_path = strdup(open_parms.ctemp.out.name);
-       }
-       talloc_destroy(mem_ctx);
        if (NT_STATUS_IS_OK(status)) {
-               return open_parms.ctemp.out.fnum;
+               if (tmp_path) {
+                       *tmp_path = strdup(open_parms.ctemp.out.name);
+               }
+               ret = open_parms.ctemp.out.file.fnum;
        }
-       return -1;
+       talloc_free(mem_ctx);
+       return ret;
 }