lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[samba.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.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
119         /* allow xattrs to be stored in a external tdb */
120         eadb = share_string_option(pvfs, scfg, PVFS_EADB, NULL);
121         if (eadb != NULL) {
122                 pvfs->ea_db = tdb_wrap_open(
123                         pvfs, eadb, 50000,
124                         lpcfg_tdb_flags(pvfs->ntvfs->ctx->lp_ctx, TDB_DEFAULT),
125                         O_RDWR|O_CREAT, 0600);
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         /* allocate the search handle -> ptr tree */
279         pvfs->search.idtree = idr_init(pvfs);
280         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
281
282         status = pvfs_mangle_init(pvfs);
283         NT_STATUS_NOT_OK_RETURN(status);
284
285         pvfs_setup_options(pvfs);
286
287         talloc_set_destructor(pvfs, pvfs_state_destructor);
288
289 #ifdef SIGXFSZ
290         /* who had the stupid idea to generate a signal on a large
291            file write instead of just failing it!? */
292         BlockSignals(true, SIGXFSZ);
293 #endif
294
295         return NT_STATUS_OK;
296 }
297
298 /*
299   disconnect from a share
300 */
301 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
302 {
303         return NT_STATUS_OK;
304 }
305
306 /*
307   check if a directory exists
308 */
309 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
310                              struct ntvfs_request *req,
311                              union smb_chkpath *cp)
312 {
313         struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
314                                   struct pvfs_state);
315         struct pvfs_filename *name;
316         NTSTATUS status;
317
318         /* resolve the cifs name to a posix name */
319         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
320         NT_STATUS_NOT_OK_RETURN(status);
321
322         if (!name->exists) {
323                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
324         }
325
326         if (!S_ISDIR(name->st.st_mode)) {
327                 return NT_STATUS_NOT_A_DIRECTORY;
328         }
329
330         return NT_STATUS_OK;
331 }
332
333 /*
334   copy a set of files
335 */
336 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
337                           struct ntvfs_request *req, struct smb_copy *cp)
338 {
339         DEBUG(0,("pvfs_copy not implemented\n"));
340         return NT_STATUS_NOT_SUPPORTED;
341 }
342
343 /*
344   return print queue info
345 */
346 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
347                          struct ntvfs_request *req, union smb_lpq *lpq)
348 {
349         return NT_STATUS_NOT_SUPPORTED;
350 }
351
352 /* SMBtrans - not used on file shares */
353 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
354                            struct ntvfs_request *req, struct smb_trans2 *trans2)
355 {
356         return NT_STATUS_ACCESS_DENIED;
357 }
358
359 /*
360   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
361  */
362 NTSTATUS ntvfs_posix_init(TALLOC_CTX *ctx)
363 {
364         NTSTATUS ret;
365         struct ntvfs_ops ops;
366         NTVFS_CURRENT_CRITICAL_SIZES(vers);
367
368         ZERO_STRUCT(ops);
369
370         ops.type = NTVFS_DISK;
371         
372         /* fill in all the operations */
373         ops.connect_fn = pvfs_connect;
374         ops.disconnect_fn = pvfs_disconnect;
375         ops.unlink_fn = pvfs_unlink;
376         ops.chkpath_fn = pvfs_chkpath;
377         ops.qpathinfo_fn = pvfs_qpathinfo;
378         ops.setpathinfo_fn = pvfs_setpathinfo;
379         ops.open_fn = pvfs_open;
380         ops.mkdir_fn = pvfs_mkdir;
381         ops.rmdir_fn = pvfs_rmdir;
382         ops.rename_fn = pvfs_rename;
383         ops.copy_fn = pvfs_copy;
384         ops.ioctl_fn = pvfs_ioctl;
385         ops.read_fn = pvfs_read;
386         ops.write_fn = pvfs_write;
387         ops.seek_fn = pvfs_seek;
388         ops.flush_fn = pvfs_flush;
389         ops.close_fn = pvfs_close;
390         ops.exit_fn = pvfs_exit;
391         ops.lock_fn = pvfs_lock;
392         ops.setfileinfo_fn = pvfs_setfileinfo;
393         ops.qfileinfo_fn = pvfs_qfileinfo;
394         ops.fsinfo_fn = pvfs_fsinfo;
395         ops.lpq_fn = pvfs_lpq;
396         ops.search_first_fn = pvfs_search_first;
397         ops.search_next_fn = pvfs_search_next;
398         ops.search_close_fn = pvfs_search_close;
399         ops.trans_fn = pvfs_trans;
400         ops.logoff_fn = pvfs_logoff;
401         ops.async_setup_fn = pvfs_async_setup;
402         ops.cancel_fn = pvfs_cancel;
403         ops.notify_fn = pvfs_notify;
404
405         /* register ourselves with the NTVFS subsystem. We register
406            under the name 'default' as we wish to be the default
407            backend, and also register as 'posix' */
408         ops.name = "default";
409         ret = ntvfs_register(&ops, &vers);
410
411         if (!NT_STATUS_IS_OK(ret)) {
412                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
413         }
414
415         ops.name = "posix";
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         if (NT_STATUS_IS_OK(ret)) {
423                 ret = ntvfs_common_init();
424         }
425
426         return ret;
427 }