2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
22 this implements most of the POSIX NTVFS backend
23 This is the default backend
27 #include "vfs_posix.h"
28 #include "librpc/gen_ndr/security.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"
36 setup config options for a posix share
38 static void pvfs_setup_options(struct pvfs_state *pvfs)
40 struct share_config *scfg = pvfs->ntvfs->ctx->config;
43 bool def_perm_override = false;
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;
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
68 def_perm_override = true;
70 if (share_bool_option(scfg, PVFS_PERM_OVERRIDE, def_perm_override))
71 pvfs->flags |= PVFS_FLAG_PERM_OVERRIDE;
73 /* file perm options */
74 pvfs->options.create_mask = share_int_option(scfg,
76 SHARE_CREATE_MASK_DEFAULT);
77 pvfs->options.dir_mask = share_int_option(scfg,
79 SHARE_DIR_MASK_DEFAULT);
80 pvfs->options.force_dir_mode = share_int_option(scfg,
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);
91 pvfs->search.inactivity_time = share_int_option(scfg,
92 PVFS_SEARCH_INACTIVITY,
93 PVFS_SEARCH_INACTIVITY_DEFAULT);
95 #ifdef HAVE_XATTR_SUPPORT
96 if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
97 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
100 pvfs->sharing_violation_delay = share_int_option(scfg,
102 PVFS_SHARE_DELAY_DEFAULT);
104 pvfs->oplock_break_timeout = share_int_option(scfg,
106 PVFS_OPLOCK_TIMEOUT_DEFAULT);
108 pvfs->writetime_delay = share_int_option(scfg,
109 PVFS_WRITETIME_DELAY,
110 PVFS_WRITETIME_DELAY_DEFAULT);
112 pvfs->share_name = talloc_strdup(pvfs, scfg->name);
115 FS_ATTR_CASE_SENSITIVE_SEARCH |
116 FS_ATTR_CASE_PRESERVED_NAMES |
117 FS_ATTR_UNICODE_ON_DISK;
119 /* allow xattrs to be stored in a external tdb */
120 eadb = share_string_option(pvfs, scfg, PVFS_EADB, NULL);
122 pvfs->ea_db = tdb_wrap_open(
124 lpcfg_tdb_flags(pvfs->ntvfs->ctx->lp_ctx, TDB_DEFAULT),
125 O_RDWR|O_CREAT, 0600);
127 if (pvfs->ea_db != NULL) {
128 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
130 DEBUG(0,("Failed to open eadb '%s' - %s\n",
131 eadb, strerror(errno)));
132 pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
136 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
137 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
139 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
140 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
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);
146 /* check if the system really supports xattrs */
147 if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
148 pvfs_xattr_probe(pvfs);
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);
157 static int pvfs_state_destructor(struct pvfs_state *pvfs)
159 struct pvfs_file *f, *fn;
160 struct pvfs_search_state *s, *sn;
163 * make sure we cleanup files and searches before anything else
164 * because there destructors need to acess the pvfs_state struct
166 for (f=pvfs->files.list; f; f=fn) {
171 for (s=pvfs->search.list; s; s=sn) {
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,
185 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
186 struct ntvfs_request *req,
187 union smb_tcon* tcon)
189 struct pvfs_state *pvfs;
191 char *base_directory;
193 const char *sharename;
195 switch (tcon->generic.level) {
197 sharename = tcon->tcon.in.service;
200 sharename = tcon->tconx.in.path;
203 sharename = tcon->smb2.in.path;
206 return NT_STATUS_INVALID_LEVEL;
209 if (strncmp(sharename, "\\\\", 2) == 0) {
210 char *p = strchr(sharename+2, '\\');
217 * TODO: call this from ntvfs_posix_init()
218 * but currently we don't have a lp_ctx there
220 status = pvfs_acl_init();
221 NT_STATUS_NOT_OK_RETURN(status);
223 pvfs = talloc_zero(ntvfs, struct pvfs_state);
224 NT_STATUS_HAVE_NO_MEMORY(pvfs);
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, "/");
234 pvfs->base_directory = base_directory;
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;
244 ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
245 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
247 ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
248 NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
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;
255 ntvfs->private_data = pvfs;
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;
265 pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
266 if (pvfs->odb_context == NULL) {
267 return NT_STATUS_INTERNAL_DB_CORRUPTION;
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);
278 /* allocate the search handle -> ptr tree */
279 pvfs->search.idtree = idr_init(pvfs);
280 NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
282 status = pvfs_mangle_init(pvfs);
283 NT_STATUS_NOT_OK_RETURN(status);
285 pvfs_setup_options(pvfs);
287 talloc_set_destructor(pvfs, pvfs_state_destructor);
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);
299 disconnect from a share
301 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
307 check if a directory exists
309 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
310 struct ntvfs_request *req,
311 union smb_chkpath *cp)
313 struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
315 struct pvfs_filename *name;
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);
323 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
326 if (!S_ISDIR(name->st.st_mode)) {
327 return NT_STATUS_NOT_A_DIRECTORY;
336 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
337 struct ntvfs_request *req, struct smb_copy *cp)
339 DEBUG(0,("pvfs_copy not implemented\n"));
340 return NT_STATUS_NOT_SUPPORTED;
344 return print queue info
346 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
347 struct ntvfs_request *req, union smb_lpq *lpq)
349 return NT_STATUS_NOT_SUPPORTED;
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)
356 return NT_STATUS_ACCESS_DENIED;
360 initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
362 NTSTATUS ntvfs_posix_init(TALLOC_CTX *ctx)
365 struct ntvfs_ops ops;
366 NTVFS_CURRENT_CRITICAL_SIZES(vers);
370 ops.type = NTVFS_DISK;
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;
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);
411 if (!NT_STATUS_IS_OK(ret)) {
412 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
416 ret = ntvfs_register(&ops, &vers);
418 if (!NT_STATUS_IS_OK(ret)) {
419 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
422 if (NT_STATUS_IS_OK(ret)) {
423 ret = ntvfs_common_init();