r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.git] / source3 / smbd / fake_file.c
1 /* 
2    Unix SMB/CIFS implementation.
3    FAKE FILE suppport, for faking up special files windows want access to
4    Copyright (C) Stefan (metze) Metzmacher      2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 extern struct current_user current_user;
23
24 static FAKE_FILE fake_files[] = {
25 #ifdef WITH_QUOTAS
26         {FAKE_FILE_NAME_QUOTA_UNIX,     FAKE_FILE_TYPE_QUOTA,   init_quota_handle,      destroy_quota_handle},
27 #endif /* WITH_QUOTAS */
28         {NULL,                          FAKE_FILE_TYPE_NONE,    NULL,                   NULL }
29 };
30
31 /****************************************************************************
32  Create a fake file handle
33 ****************************************************************************/
34
35 static struct _FAKE_FILE_HANDLE *init_fake_file_handle(enum FAKE_FILE_TYPE type)
36 {
37         TALLOC_CTX *mem_ctx = NULL;
38         FAKE_FILE_HANDLE *fh = NULL;
39         int i;
40
41         for (i=0;fake_files[i].name!=NULL;i++) {
42                 if (fake_files[i].type==type) {
43                         DEBUG(5,("init_fake_file_handle: for [%s]\n",fake_files[i].name));
44
45                         if ((mem_ctx=talloc_init("fake_file_handle"))==NULL) {
46                                 DEBUG(0,("talloc_init(fake_file_handle) failed.\n"));
47                                 return NULL;    
48                         }
49
50                         if ((fh =TALLOC_ZERO_P(mem_ctx, FAKE_FILE_HANDLE))==NULL) {
51                                 DEBUG(0,("TALLOC_ZERO() failed.\n"));
52                                 talloc_destroy(mem_ctx);
53                                 return NULL;
54                         }
55
56                         fh->type = type;
57                         fh->mem_ctx = mem_ctx;
58
59                         if (fake_files[i].init_pd) {
60                                 fh->pd = fake_files[i].init_pd(fh->mem_ctx);
61                         }
62
63                         fh->free_pd = fake_files[i].free_pd;
64
65                         return fh;
66                 }
67         }
68
69         return NULL;    
70 }
71
72 /****************************************************************************
73  Does this name match a fake filename ?
74 ****************************************************************************/
75
76 enum FAKE_FILE_TYPE is_fake_file(const char *fname)
77 {
78 #ifdef HAVE_SYS_QUOTAS
79         int i;
80 #endif
81
82         if (!fname) {
83                 return FAKE_FILE_TYPE_NONE;
84         }
85
86 #ifdef HAVE_SYS_QUOTAS
87         for (i=0;fake_files[i].name!=NULL;i++) {
88                 if (strncmp(fname,fake_files[i].name,strlen(fake_files[i].name))==0) {
89                         DEBUG(5,("is_fake_file: [%s] is a fake file\n",fname));
90                         return fake_files[i].type;
91                 }
92         }
93 #endif
94
95         return FAKE_FILE_TYPE_NONE;
96 }
97
98
99 /****************************************************************************
100  Open a fake quota file with a share mode.
101 ****************************************************************************/
102
103 NTSTATUS open_fake_file(connection_struct *conn,
104                                 enum FAKE_FILE_TYPE fake_file_type,
105                                 const char *fname,
106                                 uint32 access_mask,
107                                 files_struct **result)
108 {
109         files_struct *fsp = NULL;
110         NTSTATUS status;
111
112         /* access check */
113         if (current_user.ut.uid != 0) {
114                 DEBUG(1,("open_fake_file_shared: access_denied to service[%s] file[%s] user[%s]\n",
115                         lp_servicename(SNUM(conn)),fname,conn->user));
116                 return NT_STATUS_ACCESS_DENIED;
117
118         }
119
120         status = file_new(conn, &fsp);
121         if(!NT_STATUS_IS_OK(status)) {
122                 return status;
123         }
124
125         DEBUG(5,("open_fake_file_shared: fname = %s, FID = %d, access_mask = 0x%x\n",
126                 fname, fsp->fnum, (unsigned int)access_mask));
127
128         fsp->conn = conn;
129         fsp->fh->fd = -1;
130         fsp->vuid = current_user.vuid;
131         fsp->fh->pos = -1;
132         fsp->can_lock = False; /* Should this be true ? - No, JRA */
133         fsp->access_mask = access_mask;
134         string_set(&fsp->fsp_name,fname);
135         
136         fsp->fake_file_handle = init_fake_file_handle(fake_file_type);
137         
138         if (fsp->fake_file_handle==NULL) {
139                 file_free(fsp);
140                 return NT_STATUS_NO_MEMORY;
141         }
142
143         conn->num_files_open++;
144         *result = fsp;
145         return NT_STATUS_OK;
146 }
147
148 void destroy_fake_file_handle(FAKE_FILE_HANDLE **fh)
149 {
150         if (!fh||!(*fh)) {
151                 return;
152         }
153
154         if ((*fh)->free_pd) {
155                 (*fh)->free_pd(&(*fh)->pd);             
156         }
157
158         talloc_destroy((*fh)->mem_ctx);
159         (*fh) = NULL;
160 }
161
162 NTSTATUS close_fake_file(files_struct *fsp)
163 {
164         file_free(fsp);
165         return NT_STATUS_OK;
166 }