Merge commit 'release-4-0-0alpha1' into v4-0-test
[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 "lib/tdb/include/tdb.h"
30 #include "db_wrap.h"
31 #include "libcli/security/security.h"
32 #include "lib/events/events.h"
33
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
62         /* file perm options */
63         pvfs->options.create_mask       = share_int_option(scfg,
64                                                            SHARE_CREATE_MASK,
65                                                            SHARE_CREATE_MASK_DEFAULT);
66         pvfs->options.dir_mask          = share_int_option(scfg,
67                                                            SHARE_DIR_MASK,
68                                                            SHARE_DIR_MASK_DEFAULT);
69         pvfs->options.force_dir_mode    = share_int_option(scfg,
70                                                            SHARE_FORCE_DIR_MODE,
71                                                            SHARE_FORCE_DIR_MODE_DEFAULT);
72         pvfs->options.force_create_mode = share_int_option(scfg,
73                                                            SHARE_FORCE_CREATE_MODE,
74                                                            SHARE_FORCE_CREATE_MODE_DEFAULT);
75         /* this must be a power of 2 */
76         pvfs->alloc_size_rounding = share_int_option(scfg,
77                                                         PVFS_ALLOCATION_ROUNDING,
78                                                         PVFS_ALLOCATION_ROUNDING_DEFAULT);
79
80         pvfs->search.inactivity_time = share_int_option(scfg,
81                                                         PVFS_SEARCH_INACTIVITY,
82                                                         PVFS_SEARCH_INACTIVITY_DEFAULT);
83
84 #if HAVE_XATTR_SUPPORT
85         if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
86                 pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
87 #endif
88
89         pvfs->sharing_violation_delay = share_int_option(scfg,
90                                                         PVFS_SHARE_DELAY,
91                                                         PVFS_SHARE_DELAY_DEFAULT);
92
93         pvfs->share_name = talloc_strdup(pvfs, scfg->name);
94
95         pvfs->fs_attribs = 
96                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
97                 FS_ATTR_CASE_PRESERVED_NAMES |
98                 FS_ATTR_UNICODE_ON_DISK |
99                 FS_ATTR_SPARSE_FILES;
100
101         /* allow xattrs to be stored in a external tdb */
102         eadb = share_string_option(scfg, PVFS_EADB, NULL);
103         if (eadb != NULL) {
104                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
105                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
106                 if (pvfs->ea_db != NULL) {
107                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
108                 } else {
109                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
110                                  eadb, strerror(errno)));
111                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
112                 }
113         }
114
115         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
116                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
117         }
118         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
119                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
120         }
121
122         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
123         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
124
125         /* check if the system really supports xattrs */
126         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
127                 pvfs_xattr_probe(pvfs);
128         }
129
130         /* enable an ACL backend */
131         pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
132 }
133
134 static int pvfs_state_destructor(struct pvfs_state *pvfs)
135 {
136         struct pvfs_file *f, *fn;
137         struct pvfs_search_state *s, *sn;
138
139         /* 
140          * make sure we cleanup files and searches before anything else
141          * because there destructors need to acess the pvfs_state struct
142          */
143         for (f=pvfs->files.list; f; f=fn) {
144                 fn = f->next;
145                 talloc_free(f);
146         }
147
148         for (s=pvfs->search.list; s; s=sn) {
149                 sn = s->next;
150                 talloc_free(s);
151         }
152
153         return 0;
154 }
155
156 /*
157   connect to a share - used when a tree_connect operation comes
158   in. For a disk based backend we needs to ensure that the base
159   directory exists (tho it doesn't need to be accessible by the user,
160   that comes later)
161 */
162 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
163                              struct ntvfs_request *req, const char *sharename)
164 {
165         struct pvfs_state *pvfs;
166         struct stat st;
167         char *base_directory;
168         NTSTATUS status;
169
170         pvfs = talloc_zero(ntvfs, struct pvfs_state);
171         NT_STATUS_HAVE_NO_MEMORY(pvfs);
172
173         /* for simplicity of path construction, remove any trailing slash now */
174         base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
175         NT_STATUS_HAVE_NO_MEMORY(base_directory);
176         if (strcmp(base_directory, "/") != 0) {
177                 trim_string(base_directory, NULL, "/");
178         }
179
180         pvfs->ntvfs = ntvfs;
181         pvfs->base_directory = base_directory;
182
183         /* the directory must exist. Note that we deliberately don't
184            check that it is readable */
185         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
186                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
187                          pvfs->base_directory, sharename));
188                 return NT_STATUS_BAD_NETWORK_NAME;
189         }
190
191         ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
192         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
193
194         ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
195         NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
196
197         ntvfs->private_data = pvfs;
198
199         pvfs->brl_context = brl_init(pvfs, 
200                                      pvfs->ntvfs->ctx->server_id,
201                                      pvfs->ntvfs->ctx->msg_ctx);
202         if (pvfs->brl_context == NULL) {
203                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
204         }
205
206         pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
207         if (pvfs->odb_context == NULL) {
208                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
209         }
210
211         /* allow this to be NULL - we just disable change notify */
212         pvfs->notify_context = notify_init(pvfs, 
213                                            pvfs->ntvfs->ctx->server_id,  
214                                            pvfs->ntvfs->ctx->msg_ctx, 
215                                            event_context_find(pvfs),
216                                            pvfs->ntvfs->ctx->config);
217
218         pvfs->sidmap = sidmap_open(pvfs);
219         if (pvfs->sidmap == NULL) {
220                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
221         }
222
223         /* allocate the search handle -> ptr tree */
224         pvfs->search.idtree = idr_init(pvfs);
225         NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
226
227         status = pvfs_mangle_init(pvfs);
228         NT_STATUS_NOT_OK_RETURN(status);
229
230         pvfs_setup_options(pvfs);
231
232         talloc_set_destructor(pvfs, pvfs_state_destructor);
233
234 #ifdef SIGXFSZ
235         /* who had the stupid idea to generate a signal on a large
236            file write instead of just failing it!? */
237         BlockSignals(true, SIGXFSZ);
238 #endif
239
240         return NT_STATUS_OK;
241 }
242
243 /*
244   disconnect from a share
245 */
246 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
247 {
248         return NT_STATUS_OK;
249 }
250
251 /*
252   check if a directory exists
253 */
254 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
255                              struct ntvfs_request *req,
256                              union smb_chkpath *cp)
257 {
258         struct pvfs_state *pvfs = ntvfs->private_data;
259         struct pvfs_filename *name;
260         NTSTATUS status;
261
262         /* resolve the cifs name to a posix name */
263         status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
264         NT_STATUS_NOT_OK_RETURN(status);
265
266         if (!name->exists) {
267                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
268         }
269
270         if (!S_ISDIR(name->st.st_mode)) {
271                 return NT_STATUS_NOT_A_DIRECTORY;
272         }
273
274         return NT_STATUS_OK;
275 }
276
277 /*
278   copy a set of files
279 */
280 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
281                           struct ntvfs_request *req, struct smb_copy *cp)
282 {
283         DEBUG(0,("pvfs_copy not implemented\n"));
284         return NT_STATUS_NOT_SUPPORTED;
285 }
286
287 /*
288   return print queue info
289 */
290 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
291                          struct ntvfs_request *req, union smb_lpq *lpq)
292 {
293         return NT_STATUS_NOT_SUPPORTED;
294 }
295
296 /* SMBtrans - not used on file shares */
297 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
298                            struct ntvfs_request *req, struct smb_trans2 *trans2)
299 {
300         return NT_STATUS_ACCESS_DENIED;
301 }
302
303 /*
304   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
305  */
306 NTSTATUS ntvfs_posix_init(void)
307 {
308         NTSTATUS ret;
309         struct ntvfs_ops ops;
310         NTVFS_CURRENT_CRITICAL_SIZES(vers);
311
312         ZERO_STRUCT(ops);
313
314         ops.type = NTVFS_DISK;
315         
316         /* fill in all the operations */
317         ops.connect = pvfs_connect;
318         ops.disconnect = pvfs_disconnect;
319         ops.unlink = pvfs_unlink;
320         ops.chkpath = pvfs_chkpath;
321         ops.qpathinfo = pvfs_qpathinfo;
322         ops.setpathinfo = pvfs_setpathinfo;
323         ops.open = pvfs_open;
324         ops.mkdir = pvfs_mkdir;
325         ops.rmdir = pvfs_rmdir;
326         ops.rename = pvfs_rename;
327         ops.copy = pvfs_copy;
328         ops.ioctl = pvfs_ioctl;
329         ops.read = pvfs_read;
330         ops.write = pvfs_write;
331         ops.seek = pvfs_seek;
332         ops.flush = pvfs_flush; 
333         ops.close = pvfs_close;
334         ops.exit = pvfs_exit;
335         ops.lock = pvfs_lock;
336         ops.setfileinfo = pvfs_setfileinfo;
337         ops.qfileinfo = pvfs_qfileinfo;
338         ops.fsinfo = pvfs_fsinfo;
339         ops.lpq = pvfs_lpq;
340         ops.search_first = pvfs_search_first;
341         ops.search_next = pvfs_search_next;
342         ops.search_close = pvfs_search_close;
343         ops.trans = pvfs_trans;
344         ops.logoff = pvfs_logoff;
345         ops.async_setup = pvfs_async_setup;
346         ops.cancel = pvfs_cancel;
347         ops.notify = pvfs_notify;
348
349         /* register ourselves with the NTVFS subsystem. We register
350            under the name 'default' as we wish to be the default
351            backend, and also register as 'posix' */
352         ops.name = "default";
353         ret = ntvfs_register(&ops, &vers);
354
355         if (!NT_STATUS_IS_OK(ret)) {
356                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
357         }
358
359         ops.name = "posix";
360         ret = ntvfs_register(&ops, &vers);
361
362         if (!NT_STATUS_IS_OK(ret)) {
363                 DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
364         }
365
366         if (NT_STATUS_IS_OK(ret)) {
367                 ret = ntvfs_common_init();
368         }
369
370         return ret;
371 }