r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[samba.git] / source3 / smbd / fake_file.c
index d3660addf1153f0ccda044650f895338d052dd5a..e4f71193e1dac6d0dc09b356d0334f0b6e7e7012 100644 (file)
@@ -5,7 +5,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"
 
-/****************************************************************************
- Open a file with a share mode.
-****************************************************************************/
-files_struct *open_fake_file_shared1(enum FAKE_FILE_TYPE fake_file_type, connection_struct *conn,char *fname,
-                               SMB_STRUCT_STAT *psbuf, 
-                               uint32 desired_access, 
-                               int share_mode,int ofun, uint32 new_dos_attr, int oplock_request, 
-                               int *Access,int *action)
-{
-       extern struct current_user current_user;
-       int flags=0;
-       files_struct *fsp = NULL;
-
-       if (fake_file_type == 0) {
-               return open_file_shared1(conn,fname,psbuf,desired_access,
-                                       share_mode,ofun,new_dos_attr,
-                                       oplock_request,Access,action);  
-       }
-
-       /* access check */
-       if (current_user.uid != 0) {
-               DEBUG(1,("access_denied to service[%s] file[%s] user[%s]\n",
-                       lp_servicename(SNUM(conn)),fname,conn->user));
-               errno = EACCES;
-               return NULL;
-       }
-
-       fsp = file_new(conn);
-       if(!fsp)
-               return NULL;
-
-       DEBUG(5,("open_fake_file_shared1: fname = %s, FID = %d, share_mode = %x, ofun = %x, oplock request = %d\n",
-               fname, fsp->fnum, share_mode, ofun, oplock_request ));
-
-       if (!check_name(fname,conn)) {
-               file_free(fsp);
-               return NULL;
-       } 
-
-       fsp->fd = -1;
-       fsp->mode = psbuf->st_mode;
-       fsp->inode = psbuf->st_ino;
-       fsp->dev = psbuf->st_dev;
-       fsp->vuid = current_user.vuid;
-       fsp->size = psbuf->st_size;
-       fsp->pos = -1;
-       fsp->can_lock = True;
-       fsp->can_read = ((flags & O_WRONLY)==0);
-       fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
-       fsp->share_mode = 0;
-       fsp->desired_access = desired_access;
-       fsp->print_file = False;
-       fsp->modified = False;
-       fsp->oplock_type = NO_OPLOCK;
-       fsp->sent_oplock_break = NO_BREAK_SENT;
-       fsp->is_directory = False;
-       fsp->is_stat = False;
-       fsp->directory_delete_on_close = False;
-       fsp->conn = conn;
-       string_set(&fsp->fsp_name,fname);
-       fsp->wcp = NULL; /* Write cache pointer. */
-       
-       fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
-       
-       if (fsp->fake_file_handle==NULL) {
-               file_free(fsp);
-               return NULL;
-       }
-
-       conn->num_files_open++;
-       return fsp;
-}
+extern struct current_user current_user;
 
 static FAKE_FILE fake_files[] = {
 #ifdef WITH_QUOTAS
-       {FAKE_FILE_NAME_QUOTA,  FAKE_FILE_TYPE_QUOTA,   init_quota_handle,      destroy_quota_handle},
+       {FAKE_FILE_NAME_QUOTA_UNIX,     FAKE_FILE_TYPE_QUOTA,   init_quota_handle,      destroy_quota_handle},
 #endif /* WITH_QUOTAS */
-       {NULL,                  FAKE_FILE_TYPE_NONE,    NULL,                   NULL }
+       {NULL,                          FAKE_FILE_TYPE_NONE,    NULL,                   NULL }
 };
 
-int is_fake_file(char *fname)
-{
-       int i;
-
-       if (!fname)
-               return 0;
-
-       for (i=0;fake_files[i].name!=NULL;i++) {
-               if (strncmp(fname,fake_files[i].name,strlen(fake_files[i].name))==0) {
-                       DEBUG(5,("is_fake_file: [%s] is a fake file\n",fname));
-                       return fake_files[i].type;
-               }
-       }
-
-       return FAKE_FILE_TYPE_NONE;
-}
+/****************************************************************************
+ Create a fake file handle
+****************************************************************************/
 
-struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
+static struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
 {
        TALLOC_CTX *mem_ctx = NULL;
        FAKE_FILE_HANDLE *fh = NULL;
@@ -132,8 +47,8 @@ struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
                                return NULL;    
                        }
 
-                       if ((fh =(FAKE_FILE_HANDLE *)talloc_zero(mem_ctx, sizeof(FAKE_FILE_HANDLE)))==NULL) {
-                               DEBUG(0,("talloc_zero() failed.\n"));
+                       if ((fh =TALLOC_ZERO_P(mem_ctx, FAKE_FILE_HANDLE))==NULL) {
+                               DEBUG(0,("TALLOC_ZERO() failed.\n"));
                                talloc_destroy(mem_ctx);
                                return NULL;
                        }
@@ -141,8 +56,9 @@ struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
                        fh->type = type;
                        fh->mem_ctx = mem_ctx;
 
-                       if (fake_files[i].init_pd)
+                       if (fake_files[i].init_pd) {
                                fh->pd = fake_files[i].init_pd(fh->mem_ctx);
+                       }
 
                        fh->free_pd = fake_files[i].free_pd;
 
@@ -153,14 +69,98 @@ struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
        return NULL;    
 }
 
+/****************************************************************************
+ Does this name match a fake filename ?
+****************************************************************************/
+
+enum FAKE_FILE_TYPE is_fake_file(const char *fname)
+{
+#ifdef HAVE_SYS_QUOTAS
+       int i;
+#endif
+
+       if (!fname) {
+               return FAKE_FILE_TYPE_NONE;
+       }
+
+#ifdef HAVE_SYS_QUOTAS
+       for (i=0;fake_files[i].name!=NULL;i++) {
+               if (strncmp(fname,fake_files[i].name,strlen(fake_files[i].name))==0) {
+                       DEBUG(5,("is_fake_file: [%s] is a fake file\n",fname));
+                       return fake_files[i].type;
+               }
+       }
+#endif
+
+       return FAKE_FILE_TYPE_NONE;
+}
+
+
+/****************************************************************************
+ Open a fake quota file with a share mode.
+****************************************************************************/
+
+NTSTATUS open_fake_file(connection_struct *conn,
+                               enum FAKE_FILE_TYPE fake_file_type,
+                               const char *fname,
+                               uint32 access_mask,
+                               files_struct **result)
+{
+       files_struct *fsp = NULL;
+       NTSTATUS status;
+
+       /* access check */
+       if (current_user.ut.uid != 0) {
+               DEBUG(1,("open_fake_file_shared: access_denied to service[%s] file[%s] user[%s]\n",
+                       lp_servicename(SNUM(conn)),fname,conn->user));
+               return NT_STATUS_ACCESS_DENIED;
+
+       }
+
+       status = file_new(conn, &fsp);
+       if(!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
+               fname, fsp->fnum, (unsigned int)access_mask));
+
+       fsp->conn = conn;
+       fsp->fh->fd = -1;
+       fsp->vuid = current_user.vuid;
+       fsp->fh->pos = -1;
+       fsp->can_lock = False; /* Should this be true ? - No, JRA */
+       fsp->access_mask = access_mask;
+       string_set(&fsp->fsp_name,fname);
+       
+       fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
+       
+       if (fsp->fake_file_handle==NULL) {
+               file_free(fsp);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       conn->num_files_open++;
+       *result = fsp;
+       return NT_STATUS_OK;
+}
+
 void destroy_fake_file_handle(FAKE_FILE_HANDLE **fh)
 {
-       if (!fh||!(*fh))
-               return ;
+       if (!fh||!(*fh)) {
+               return;
+       }
 
-       if ((*fh)->free_pd)
+       if ((*fh)->free_pd) {
                (*fh)->free_pd(&(*fh)->pd);             
+       }
 
        talloc_destroy((*fh)->mem_ctx);
        (*fh) = NULL;
 }
+
+NTSTATUS close_fake_file(files_struct *fsp)
+{
+       file_free(fsp);
+       return NT_STATUS_OK;
+}