r14860: create libcli/security/security.h
[metze/samba/wip.git] / source4 / ntvfs / posix / vfs_posix.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements most of the POSIX NTVFS backend
24   This is the default backend
25 */
26
27 #include "includes.h"
28 #include "vfs_posix.h"
29 #include "librpc/gen_ndr/security.h"
30 #include "lib/tdb/include/tdb.h"
31 #include "db_wrap.h"
32 #include "libcli/security/security.h"
33
34
35 /*
36   setup config options for a posix share
37 */
38 static void pvfs_setup_options(struct pvfs_state *pvfs)
39 {
40         int snum = pvfs->ntvfs->ctx->config.snum;
41         const char *eadb;
42
43         if (lp_map_hidden(snum))     pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
44         if (lp_map_archive(snum))    pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
45         if (lp_map_system(snum))     pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
46         if (lp_readonly(snum))       pvfs->flags |= PVFS_FLAG_READONLY;
47         if (lp_strict_sync(snum))    pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
48         if (lp_strict_locking(snum)) pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
49         if (lp_ci_filesystem(snum))  pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
50
51         if (lp_parm_bool(snum, "posix", "fakeoplocks", False)) {
52                 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
53         }
54
55         /* this must be a power of 2 */
56         pvfs->alloc_size_rounding = lp_parm_int(snum, 
57                                                 "posix", "allocationrounding", 512);
58
59         pvfs->search.inactivity_time = lp_parm_int(snum, 
60                                                    "posix", "searchinactivity", 300);
61
62 #if HAVE_XATTR_SUPPORT
63         if (lp_parm_bool(snum, "posix", "xattr", True)) pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
64 #endif
65
66         pvfs->sharing_violation_delay = lp_parm_int(snum, "posix", "sharedelay", 1000000);
67
68         pvfs->share_name = talloc_strdup(pvfs, lp_servicename(snum));
69
70         pvfs->fs_attribs = 
71                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
72                 FS_ATTR_CASE_PRESERVED_NAMES |
73                 FS_ATTR_UNICODE_ON_DISK |
74                 FS_ATTR_SPARSE_FILES;
75
76         /* allow xattrs to be stored in a external tdb */
77         eadb = lp_parm_string(snum, "posix", "eadb");
78         if (eadb != NULL) {
79                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
80                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
81                 if (pvfs->ea_db != NULL) {
82                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
83                 } else {
84                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
85                                  eadb, strerror(errno)));
86                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
87                 }
88         }
89
90         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
91                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
92         }
93         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
94                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
95         }
96
97         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
98         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
99
100         /* check if the system really supports xattrs */
101         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
102                 pvfs_xattr_probe(pvfs);
103         }
104 }
105
106 static int pvfs_state_destructor(void *ptr)
107 {
108         struct pvfs_state *pvfs = talloc_get_type(ptr, struct pvfs_state);
109         struct pvfs_file *f, *fn;
110         struct pvfs_search_state *s, *sn;
111
112         /* 
113          * make sure we cleanup files and searches before anythingelse
114          * because there destructors need to acess the pvfs_state struct
115          */
116         for (f=pvfs->files.list; f; f=fn) {
117                 fn = f->next;
118                 talloc_free(f);
119         }
120
121         for (s=pvfs->search.list; s; s=sn) {
122                 sn = s->next;
123                 talloc_free(s);
124         }
125
126         return 0;
127 }
128
129 /*
130   connect to a share - used when a tree_connect operation comes
131   in. For a disk based backend we needs to ensure that the base
132   directory exists (tho it doesn't need to be accessible by the user,
133   that comes later)
134 */
135 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
136                              struct ntvfs_request *req, const char *sharename)
137 {
138         struct pvfs_state *pvfs;
139         struct stat st;
140         char *base_directory;
141         NTSTATUS status;
142
143         pvfs = talloc_zero(ntvfs, struct pvfs_state);
144         NT_STATUS_HAVE_NO_MEMORY(pvfs);
145
146         /* for simplicity of path construction, remove any trailing slash now */
147         base_directory = talloc_strdup(pvfs, lp_pathname(ntvfs->ctx->config.snum));
148         NT_STATUS_HAVE_NO_MEMORY(base_directory);
149         if (strcmp(base_directory, "/") != 0) {
150                 trim_string(base_directory, NULL, "/");
151         }
152
153         pvfs->ntvfs = ntvfs;
154         pvfs->base_directory = base_directory;
155
156         /* the directory must exist. Note that we deliberately don't
157            check that it is readable */
158         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
159                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
160                          pvfs->base_directory, sharename));
161                 return NT_STATUS_BAD_NETWORK_NAME;
162         }
163
164         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
165         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
166
167         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
168         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
169
170         ntvfs->private_data = pvfs;
171
172         pvfs->brl_context = brl_init(pvfs, 
173                                      pvfs->ntvfs->ctx->server_id,  
174                                      pvfs->ntvfs->ctx->config.snum,
175                                      pvfs->ntvfs->ctx->msg_ctx);
176         if (pvfs->brl_context == NULL) {
177                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
178         }
179
180         pvfs->odb_context = odb_init(pvfs, 
181                                      pvfs->ntvfs->ctx->server_id,  
182                                      pvfs->ntvfs->ctx->msg_ctx);
183         if (pvfs->odb_context == NULL) {
184                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
185         }
186
187         pvfs->notify_context = notify_init(pvfs, 
188                                            pvfs->ntvfs->ctx->server_id,  
189                                            pvfs->ntvfs->ctx->msg_ctx);
190         if (pvfs->notify_context == NULL) {
191                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
192         }
193
194         pvfs->sidmap = sidmap_open(pvfs);
195         if (pvfs->sidmap == NULL) {
196                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
197         }
198
199         /* allocate the fnum id -> ptr tree */
200         pvfs->files.idtree = idr_init(pvfs);
201         NT_STATUS_HAVE_NO_MEMORY(pvfs->files.idtree);
202
203         /* allocate the search handle -> ptr tree */
204         pvfs->search.idtree = idr_init(pvfs);
205         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
206
207         status = pvfs_mangle_init(pvfs);
208         NT_STATUS_NOT_OK_RETURN(status);
209
210         pvfs_setup_options(pvfs);
211
212         talloc_set_destructor(pvfs, pvfs_state_destructor);
213
214 #ifdef SIGXFSZ
215         /* who had the stupid idea to generate a signal on a large
216            file write instead of just failing it!? */
217         BlockSignals(True, SIGXFSZ);
218 #endif
219
220         return NT_STATUS_OK;
221 }
222
223 /*
224   disconnect from a share
225 */
226 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
227 {
228         return NT_STATUS_OK;
229 }
230
231 /*
232   check if a directory exists
233 */
234 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
235                              struct ntvfs_request *req,
236                              union smb_chkpath *cp)
237 {
238         struct pvfs_state *pvfs = ntvfs->private_data;
239         struct pvfs_filename *name;
240         NTSTATUS status;
241
242         /* resolve the cifs name to a posix name */
243         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
244         NT_STATUS_NOT_OK_RETURN(status);
245
246         if (!name->exists) {
247                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
248         }
249
250         if (!S_ISDIR(name->st.st_mode)) {
251                 return NT_STATUS_NOT_A_DIRECTORY;
252         }
253
254         return NT_STATUS_OK;
255 }
256
257 /*
258   copy a set of files
259 */
260 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
261                           struct ntvfs_request *req, struct smb_copy *cp)
262 {
263         DEBUG(0,("pvfs_copy not implemented\n"));
264         return NT_STATUS_NOT_SUPPORTED;
265 }
266
267 /*
268   return print queue info
269 */
270 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
271                          struct ntvfs_request *req, union smb_lpq *lpq)
272 {
273         return NT_STATUS_NOT_SUPPORTED;
274 }
275
276 /* SMBtrans - not used on file shares */
277 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
278                            struct ntvfs_request *req, struct smb_trans2 *trans2)
279 {
280         return NT_STATUS_ACCESS_DENIED;
281 }
282
283 /*
284   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
285  */
286 NTSTATUS ntvfs_posix_init(void)
287 {
288         NTSTATUS ret;
289         struct ntvfs_ops ops;
290
291         ZERO_STRUCT(ops);
292
293         ops.type = NTVFS_DISK;
294         
295         /* fill in all the operations */
296         ops.connect = pvfs_connect;
297         ops.disconnect = pvfs_disconnect;
298         ops.unlink = pvfs_unlink;
299         ops.chkpath = pvfs_chkpath;
300         ops.qpathinfo = pvfs_qpathinfo;
301         ops.setpathinfo = pvfs_setpathinfo;
302         ops.open = pvfs_open;
303         ops.mkdir = pvfs_mkdir;
304         ops.rmdir = pvfs_rmdir;
305         ops.rename = pvfs_rename;
306         ops.copy = pvfs_copy;
307         ops.ioctl = pvfs_ioctl;
308         ops.read = pvfs_read;
309         ops.write = pvfs_write;
310         ops.seek = pvfs_seek;
311         ops.flush = pvfs_flush; 
312         ops.close = pvfs_close;
313         ops.exit = pvfs_exit;
314         ops.lock = pvfs_lock;
315         ops.setfileinfo = pvfs_setfileinfo;
316         ops.qfileinfo = pvfs_qfileinfo;
317         ops.fsinfo = pvfs_fsinfo;
318         ops.lpq = pvfs_lpq;
319         ops.search_first = pvfs_search_first;
320         ops.search_next = pvfs_search_next;
321         ops.search_close = pvfs_search_close;
322         ops.trans = pvfs_trans;
323         ops.logoff = pvfs_logoff;
324         ops.async_setup = pvfs_async_setup;
325         ops.cancel = pvfs_cancel;
326         ops.notify = pvfs_notify;
327
328         /* register ourselves with the NTVFS subsystem. We register
329            under the name 'default' as we wish to be the default
330            backend, and also register as 'posix' */
331         ops.name = "default";
332         ret = ntvfs_register(&ops);
333
334         ops.name = "posix";
335         ret = ntvfs_register(&ops);
336
337         if (!NT_STATUS_IS_OK(ret)) {
338                 DEBUG(0,("Failed to register POSIX backend!\n"));
339         }
340
341         return ret;
342 }