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