f22f1d48172ea0022da2171e92ea5ac24abafef0
[kai/samba-autobuild/.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   this implements most of the POSIX NTVFS backend
23   This is the default backend
24 */
25
26 #include "includes.h"
27 #include "vfs_posix.h"
28 #include "librpc/gen_ndr/security.h"
29 #include "../tdb/include/tdb.h"
30 #include "tdb_wrap.h"
31 #include "libcli/security/security.h"
32 #include "lib/events/events.h"
33 #include "param/param.h"
34
35 /*
36   setup config options for a posix share
37 */
38 static void pvfs_setup_options(struct pvfs_state *pvfs)
39 {
40         struct share_config *scfg = pvfs->ntvfs->ctx->config;
41         const char *eadb;
42
43         if (share_bool_option(scfg, SHARE_MAP_HIDDEN, SHARE_MAP_HIDDEN_DEFAULT))
44                 pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
45         if (share_bool_option(scfg, SHARE_MAP_ARCHIVE, SHARE_MAP_ARCHIVE_DEFAULT))
46                 pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
47         if (share_bool_option(scfg, SHARE_MAP_SYSTEM, SHARE_MAP_SYSTEM_DEFAULT))
48                 pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
49         if (share_bool_option(scfg, SHARE_READONLY, SHARE_READONLY_DEFAULT))
50                 pvfs->flags |= PVFS_FLAG_READONLY;
51         if (share_bool_option(scfg, SHARE_STRICT_SYNC, SHARE_STRICT_SYNC_DEFAULT))
52                 pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
53         if (share_bool_option(scfg, SHARE_STRICT_LOCKING, SHARE_STRICT_LOCKING_DEFAULT))
54                 pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
55         if (share_bool_option(scfg, SHARE_CI_FILESYSTEM, SHARE_CI_FILESYSTEM_DEFAULT))
56                 pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
57         if (share_bool_option(scfg, PVFS_FAKE_OPLOCKS, PVFS_FAKE_OPLOCKS_DEFAULT))
58                 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
59         if (share_bool_option(scfg, PVFS_AIO, false))
60                 pvfs->flags |= PVFS_FLAG_LINUX_AIO;
61         if (share_bool_option(scfg, PVFS_PERM_OVERRIDE, true))
62                 pvfs->flags |= PVFS_FLAG_PERM_OVERRIDE;
63
64         /* file perm options */
65         pvfs->options.create_mask       = share_int_option(scfg,
66                                                            SHARE_CREATE_MASK,
67                                                            SHARE_CREATE_MASK_DEFAULT);
68         pvfs->options.dir_mask          = share_int_option(scfg,
69                                                            SHARE_DIR_MASK,
70                                                            SHARE_DIR_MASK_DEFAULT);
71         pvfs->options.force_dir_mode    = share_int_option(scfg,
72                                                            SHARE_FORCE_DIR_MODE,
73                                                            SHARE_FORCE_DIR_MODE_DEFAULT);
74         pvfs->options.force_create_mode = share_int_option(scfg,
75                                                            SHARE_FORCE_CREATE_MODE,
76                                                            SHARE_FORCE_CREATE_MODE_DEFAULT);
77         /* this must be a power of 2 */
78         pvfs->alloc_size_rounding = share_int_option(scfg,
79                                                         PVFS_ALLOCATION_ROUNDING,
80                                                         PVFS_ALLOCATION_ROUNDING_DEFAULT);
81
82         pvfs->search.inactivity_time = share_int_option(scfg,
83                                                         PVFS_SEARCH_INACTIVITY,
84                                                         PVFS_SEARCH_INACTIVITY_DEFAULT);
85
86 #if HAVE_XATTR_SUPPORT
87         if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
88                 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
89 #endif
90
91         pvfs->sharing_violation_delay = share_int_option(scfg,
92                                                         PVFS_SHARE_DELAY,
93                                                         PVFS_SHARE_DELAY_DEFAULT);
94
95         pvfs->oplock_break_timeout = share_int_option(scfg,
96                                                       PVFS_OPLOCK_TIMEOUT,
97                                                       PVFS_OPLOCK_TIMEOUT_DEFAULT);
98
99         pvfs->writetime_delay = share_int_option(scfg,
100                                                  PVFS_WRITETIME_DELAY,
101                                                  PVFS_WRITETIME_DELAY_DEFAULT);
102
103         pvfs->share_name = talloc_strdup(pvfs, scfg->name);
104
105         pvfs->fs_attribs = 
106                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
107                 FS_ATTR_CASE_PRESERVED_NAMES |
108                 FS_ATTR_UNICODE_ON_DISK |
109                 FS_ATTR_SPARSE_FILES;
110
111         /* allow xattrs to be stored in a external tdb */
112         eadb = share_string_option(scfg, PVFS_EADB, NULL);
113         if (eadb != NULL) {
114                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
115                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
116                 if (pvfs->ea_db != NULL) {
117                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
118                 } else {
119                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
120                                  eadb, strerror(errno)));
121                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
122                 }
123         }
124
125         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
126                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
127         }
128         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
129                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
130         }
131
132         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
133         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
134
135         /* check if the system really supports xattrs */
136         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
137                 pvfs_xattr_probe(pvfs);
138         }
139
140         /* enable an ACL backend */
141         pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
142 }
143
144 static int pvfs_state_destructor(struct pvfs_state *pvfs)
145 {
146         struct pvfs_file *f, *fn;
147         struct pvfs_search_state *s, *sn;
148
149         /* 
150          * make sure we cleanup files and searches before anything else
151          * because there destructors need to acess the pvfs_state struct
152          */
153         for (f=pvfs->files.list; f; f=fn) {
154                 fn = f->next;
155                 talloc_free(f);
156         }
157
158         for (s=pvfs->search.list; s; s=sn) {
159                 sn = s->next;
160                 talloc_free(s);
161         }
162
163         return 0;
164 }
165
166 /*
167   connect to a share - used when a tree_connect operation comes
168   in. For a disk based backend we needs to ensure that the base
169   directory exists (tho it doesn't need to be accessible by the user,
170   that comes later)
171 */
172 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
173                              struct ntvfs_request *req,
174                              union smb_tcon* tcon)
175 {
176         struct pvfs_state *pvfs;
177         struct stat st;
178         char *base_directory;
179         NTSTATUS status;
180         const char *sharename;
181
182         switch (tcon->generic.level) {
183         case RAW_TCON_TCON:
184                 sharename = tcon->tcon.in.service;
185                 break;
186         case RAW_TCON_TCONX:
187                 sharename = tcon->tconx.in.path;
188                 break;
189         case RAW_TCON_SMB2:
190                 sharename = tcon->smb2.in.path;
191                 break;
192         default:
193                 return NT_STATUS_INVALID_LEVEL;
194         }
195
196         if (strncmp(sharename, "\\\\", 2) == 0) {
197                 char *p = strchr(sharename+2, '\\');
198                 if (p) {
199                         sharename = p + 1;
200                 }
201         }
202
203         /*
204          * TODO: call this from ntvfs_posix_init()
205          *       but currently we don't have a lp_ctx there
206          */
207         status = pvfs_acl_init(ntvfs->ctx->lp_ctx);
208         NT_STATUS_NOT_OK_RETURN(status);
209
210         pvfs = talloc_zero(ntvfs, struct pvfs_state);
211         NT_STATUS_HAVE_NO_MEMORY(pvfs);
212
213         /* for simplicity of path construction, remove any trailing slash now */
214         base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
215         NT_STATUS_HAVE_NO_MEMORY(base_directory);
216         if (strcmp(base_directory, "/") != 0) {
217                 trim_string(base_directory, NULL, "/");
218         }
219
220         pvfs->ntvfs = ntvfs;
221         pvfs->base_directory = base_directory;
222
223         /* the directory must exist. Note that we deliberately don't
224            check that it is readable */
225         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
226                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
227                          pvfs->base_directory, sharename));
228                 return NT_STATUS_BAD_NETWORK_NAME;
229         }
230
231         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
232         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
233
234         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
235         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
236
237         if (tcon->generic.level == RAW_TCON_TCONX) {
238                 tcon->tconx.out.fs_type = ntvfs->ctx->fs_type;
239                 tcon->tconx.out.dev_type = ntvfs->ctx->dev_type;
240         }
241
242         ntvfs->private_data = pvfs;
243
244         pvfs->brl_context = brl_init(pvfs, 
245                                      pvfs->ntvfs->ctx->server_id,
246                                      pvfs->ntvfs->ctx->lp_ctx,
247                                      pvfs->ntvfs->ctx->msg_ctx);
248         if (pvfs->brl_context == NULL) {
249                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
250         }
251
252         pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
253         if (pvfs->odb_context == NULL) {
254                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
255         }
256
257         /* allow this to be NULL - we just disable change notify */
258         pvfs->notify_context = notify_init(pvfs, 
259                                            pvfs->ntvfs->ctx->server_id,  
260                                            pvfs->ntvfs->ctx->msg_ctx, 
261                                            pvfs->ntvfs->ctx->lp_ctx, 
262                                            pvfs->ntvfs->ctx->event_ctx,
263                                            pvfs->ntvfs->ctx->config);
264
265         pvfs->wbc_ctx = wbc_init(pvfs,
266                                  pvfs->ntvfs->ctx->msg_ctx,
267                                  pvfs->ntvfs->ctx->event_ctx);
268         if (pvfs->wbc_ctx == NULL) {
269                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
270         }
271
272         /* allocate the search handle -> ptr tree */
273         pvfs->search.idtree = idr_init(pvfs);
274         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
275
276         status = pvfs_mangle_init(pvfs);
277         NT_STATUS_NOT_OK_RETURN(status);
278
279         pvfs_setup_options(pvfs);
280
281         talloc_set_destructor(pvfs, pvfs_state_destructor);
282
283 #ifdef SIGXFSZ
284         /* who had the stupid idea to generate a signal on a large
285            file write instead of just failing it!? */
286         BlockSignals(true, SIGXFSZ);
287 #endif
288
289         return NT_STATUS_OK;
290 }
291
292 /*
293   disconnect from a share
294 */
295 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
296 {
297         return NT_STATUS_OK;
298 }
299
300 /*
301   check if a directory exists
302 */
303 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
304                              struct ntvfs_request *req,
305                              union smb_chkpath *cp)
306 {
307         struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
308                                   struct pvfs_state);
309         struct pvfs_filename *name;
310         NTSTATUS status;
311
312         /* resolve the cifs name to a posix name */
313         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
314         NT_STATUS_NOT_OK_RETURN(status);
315
316         if (!name->exists) {
317                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
318         }
319
320         if (!S_ISDIR(name->st.st_mode)) {
321                 return NT_STATUS_NOT_A_DIRECTORY;
322         }
323
324         return NT_STATUS_OK;
325 }
326
327 /*
328   copy a set of files
329 */
330 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
331                           struct ntvfs_request *req, struct smb_copy *cp)
332 {
333         DEBUG(0,("pvfs_copy not implemented\n"));
334         return NT_STATUS_NOT_SUPPORTED;
335 }
336
337 /*
338   return print queue info
339 */
340 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
341                          struct ntvfs_request *req, union smb_lpq *lpq)
342 {
343         return NT_STATUS_NOT_SUPPORTED;
344 }
345
346 /* SMBtrans - not used on file shares */
347 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
348                            struct ntvfs_request *req, struct smb_trans2 *trans2)
349 {
350         return NT_STATUS_ACCESS_DENIED;
351 }
352
353 /*
354   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
355  */
356 NTSTATUS ntvfs_posix_init(void)
357 {
358         NTSTATUS ret;
359         struct ntvfs_ops ops;
360         NTVFS_CURRENT_CRITICAL_SIZES(vers);
361
362         ZERO_STRUCT(ops);
363
364         ops.type = NTVFS_DISK;
365         
366         /* fill in all the operations */
367         ops.connect = pvfs_connect;
368         ops.disconnect = pvfs_disconnect;
369         ops.unlink = pvfs_unlink;
370         ops.chkpath = pvfs_chkpath;
371         ops.qpathinfo = pvfs_qpathinfo;
372         ops.setpathinfo = pvfs_setpathinfo;
373         ops.open = pvfs_open;
374         ops.mkdir = pvfs_mkdir;
375         ops.rmdir = pvfs_rmdir;
376         ops.rename = pvfs_rename;
377         ops.copy = pvfs_copy;
378         ops.ioctl = pvfs_ioctl;
379         ops.read = pvfs_read;
380         ops.write = pvfs_write;
381         ops.seek = pvfs_seek;
382         ops.flush = pvfs_flush; 
383         ops.close = pvfs_close;
384         ops.exit = pvfs_exit;
385         ops.lock = pvfs_lock;
386         ops.setfileinfo = pvfs_setfileinfo;
387         ops.qfileinfo = pvfs_qfileinfo;
388         ops.fsinfo = pvfs_fsinfo;
389         ops.lpq = pvfs_lpq;
390         ops.search_first = pvfs_search_first;
391         ops.search_next = pvfs_search_next;
392         ops.search_close = pvfs_search_close;
393         ops.trans = pvfs_trans;
394         ops.logoff = pvfs_logoff;
395         ops.async_setup = pvfs_async_setup;
396         ops.cancel = pvfs_cancel;
397         ops.notify = pvfs_notify;
398
399         /* register ourselves with the NTVFS subsystem. We register
400            under the name 'default' as we wish to be the default
401            backend, and also register as 'posix' */
402         ops.name = "default";
403         ret = ntvfs_register(&ops, &vers);
404
405         if (!NT_STATUS_IS_OK(ret)) {
406                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
407         }
408
409         ops.name = "posix";
410         ret = ntvfs_register(&ops, &vers);
411
412         if (!NT_STATUS_IS_OK(ret)) {
413                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
414         }
415
416         if (NT_STATUS_IS_OK(ret)) {
417                 ret = ntvfs_common_init();
418         }
419
420         return ret;
421 }