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