r13099: allow shares that point to /
[metze/samba/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   this implements most of the POSIX NTVFS backend
24   This is the default backend
25 */
26
27 #include "includes.h"
28 #include "vfs_posix.h"
29 #include "librpc/gen_ndr/ndr_security.h"
30 #include "smbd/service_stream.h"
31 #include "lib/tdb/include/tdb.h"
32
33
34 /*
35   setup config options for a posix share
36 */
37 static void pvfs_setup_options(struct pvfs_state *pvfs)
38 {
39         int snum = pvfs->tcon->service;
40         const char *eadb;
41
42         if (lp_map_hidden(snum))     pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
43         if (lp_map_archive(snum))    pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
44         if (lp_map_system(snum))     pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
45         if (lp_readonly(snum))       pvfs->flags |= PVFS_FLAG_READONLY;
46         if (lp_strict_sync(snum))    pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
47         if (lp_strict_locking(snum)) pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
48         if (lp_ci_filesystem(snum))  pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
49
50         if (lp_parm_bool(snum, "posix", "fakeoplocks", False)) {
51                 pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
52         }
53
54         /* this must be a power of 2 */
55         pvfs->alloc_size_rounding = lp_parm_int(snum, 
56                                                 "posix", "allocationrounding", 512);
57
58         pvfs->search_inactivity_time = lp_parm_int(snum, 
59                                                    "posix", "searchinactivity", 300);
60
61 #if HAVE_XATTR_SUPPORT
62         if (lp_parm_bool(snum, "posix", "xattr", True)) pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
63 #endif
64
65         pvfs->sharing_violation_delay = lp_parm_int(snum, "posix", "sharedelay", 1000000);
66
67         pvfs->share_name = talloc_strdup(pvfs, lp_servicename(snum));
68
69         pvfs->fs_attribs = 
70                 FS_ATTR_CASE_SENSITIVE_SEARCH | 
71                 FS_ATTR_CASE_PRESERVED_NAMES |
72                 FS_ATTR_UNICODE_ON_DISK |
73                 FS_ATTR_SPARSE_FILES;
74
75         /* allow xattrs to be stored in a external tdb */
76         eadb = lp_parm_string(snum, "posix", "eadb");
77         if (eadb != NULL) {
78                 pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,  
79                                             TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
80                 if (pvfs->ea_db != NULL) {
81                         pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
82                 } else {
83                         DEBUG(0,("Failed to open eadb '%s' - %s\n",
84                                  eadb, strerror(errno)));
85                         pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
86                 }
87         }
88
89         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
90                 pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
91         }
92         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
93                 pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
94         }
95
96         pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
97         pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
98
99         /* check if the system really supports xattrs */
100         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
101                 pvfs_xattr_probe(pvfs);
102         }
103 }
104
105
106 /*
107   connect to a share - used when a tree_connect operation comes
108   in. For a disk based backend we needs to ensure that the base
109   directory exists (tho it doesn't need to be accessible by the user,
110   that comes later)
111 */
112 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
113                              struct smbsrv_request *req, const char *sharename)
114 {
115         struct smbsrv_tcon *tcon = req->tcon;
116         struct pvfs_state *pvfs;
117         struct stat st;
118         char *base_directory;
119         NTSTATUS status;
120
121         pvfs = talloc_zero(tcon, struct pvfs_state);
122         NT_STATUS_HAVE_NO_MEMORY(pvfs);
123
124         /* for simplicity of path construction, remove any trailing slash now */
125         base_directory = talloc_strdup(pvfs, lp_pathname(tcon->service));
126         NT_STATUS_HAVE_NO_MEMORY(base_directory);
127         if (strcmp(base_directory, "/") != 0) {
128                 trim_string(base_directory, NULL, "/");
129         }
130
131         pvfs->tcon = tcon;
132         pvfs->base_directory = base_directory;
133
134         /* the directory must exist. Note that we deliberately don't
135            check that it is readable */
136         if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
137                 DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n", 
138                          pvfs->base_directory, sharename));
139                 return NT_STATUS_BAD_NETWORK_NAME;
140         }
141
142         tcon->fs_type = talloc_strdup(tcon, "NTFS");
143         NT_STATUS_HAVE_NO_MEMORY(tcon->fs_type);
144
145         tcon->dev_type = talloc_strdup(tcon, "A:");
146         NT_STATUS_HAVE_NO_MEMORY(tcon->dev_type);
147
148         ntvfs->private_data = pvfs;
149
150         pvfs->brl_context = brl_init(pvfs, 
151                                      pvfs->tcon->smb_conn->connection->server_id,  
152                                      pvfs->tcon->service,
153                                      pvfs->tcon->smb_conn->connection->msg_ctx);
154         if (pvfs->brl_context == NULL) {
155                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
156         }
157
158         pvfs->odb_context = odb_init(pvfs, 
159                                      pvfs->tcon->smb_conn->connection->server_id,  
160                                      pvfs->tcon->smb_conn->connection->msg_ctx);
161         if (pvfs->odb_context == NULL) {
162                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
163         }
164
165         pvfs->sidmap = sidmap_open(pvfs);
166         if (pvfs->sidmap == NULL) {
167                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
168         }
169
170         /* allocate the fnum id -> ptr tree */
171         pvfs->idtree_fnum = idr_init(pvfs);
172         NT_STATUS_HAVE_NO_MEMORY(pvfs->idtree_fnum);
173
174         /* allocate the search handle -> ptr tree */
175         pvfs->idtree_search = idr_init(pvfs);
176         NT_STATUS_HAVE_NO_MEMORY(pvfs->idtree_search);
177
178         status = pvfs_mangle_init(pvfs);
179         NT_STATUS_NOT_OK_RETURN(status);
180
181         pvfs_setup_options(pvfs);
182
183 #ifdef SIGXFSZ
184         /* who had the stupid idea to generate a signal on a large
185            file write instead of just failing it!? */
186         BlockSignals(True, SIGXFSZ);
187 #endif
188
189         return NT_STATUS_OK;
190 }
191
192 /*
193   disconnect from a share
194 */
195 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs,
196                                 struct smbsrv_tcon *tcon)
197 {
198         return NT_STATUS_OK;
199 }
200
201 /*
202   check if a directory exists
203 */
204 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
205                              struct smbsrv_request *req, struct smb_chkpath *cp)
206 {
207         struct pvfs_state *pvfs = ntvfs->private_data;
208         struct pvfs_filename *name;
209         NTSTATUS status;
210
211         /* resolve the cifs name to a posix name */
212         status = pvfs_resolve_name(pvfs, req, cp->in.path, 0, &name);
213         NT_STATUS_NOT_OK_RETURN(status);
214
215         if (!name->exists) {
216                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
217         }
218
219         if (!S_ISDIR(name->st.st_mode)) {
220                 return NT_STATUS_NOT_A_DIRECTORY;
221         }
222
223         return NT_STATUS_OK;
224 }
225
226 /*
227   copy a set of files
228 */
229 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
230                           struct smbsrv_request *req, struct smb_copy *cp)
231 {
232         DEBUG(0,("pvfs_copy not implemented\n"));
233         return NT_STATUS_NOT_SUPPORTED;
234 }
235
236 /*
237   return print queue info
238 */
239 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
240                          struct smbsrv_request *req, union smb_lpq *lpq)
241 {
242         return NT_STATUS_NOT_SUPPORTED;
243 }
244
245 /* SMBtrans - not used on file shares */
246 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
247                            struct smbsrv_request *req, struct smb_trans2 *trans2)
248 {
249         return NT_STATUS_ACCESS_DENIED;
250 }
251
252 /*
253   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
254  */
255 NTSTATUS ntvfs_posix_init(void)
256 {
257         NTSTATUS ret;
258         struct ntvfs_ops ops;
259
260         ZERO_STRUCT(ops);
261
262         ops.type = NTVFS_DISK;
263         
264         /* fill in all the operations */
265         ops.connect = pvfs_connect;
266         ops.disconnect = pvfs_disconnect;
267         ops.unlink = pvfs_unlink;
268         ops.chkpath = pvfs_chkpath;
269         ops.qpathinfo = pvfs_qpathinfo;
270         ops.setpathinfo = pvfs_setpathinfo;
271         ops.openfile = pvfs_open;
272         ops.mkdir = pvfs_mkdir;
273         ops.rmdir = pvfs_rmdir;
274         ops.rename = pvfs_rename;
275         ops.copy = pvfs_copy;
276         ops.ioctl = pvfs_ioctl;
277         ops.read = pvfs_read;
278         ops.write = pvfs_write;
279         ops.seek = pvfs_seek;
280         ops.flush = pvfs_flush; 
281         ops.close = pvfs_close;
282         ops.exit = pvfs_exit;
283         ops.lock = pvfs_lock;
284         ops.setfileinfo = pvfs_setfileinfo;
285         ops.qfileinfo = pvfs_qfileinfo;
286         ops.fsinfo = pvfs_fsinfo;
287         ops.lpq = pvfs_lpq;
288         ops.search_first = pvfs_search_first;
289         ops.search_next = pvfs_search_next;
290         ops.search_close = pvfs_search_close;
291         ops.trans = pvfs_trans;
292         ops.logoff = pvfs_logoff;
293         ops.async_setup = pvfs_async_setup;
294         ops.cancel = pvfs_cancel;
295
296         /* register ourselves with the NTVFS subsystem. We register
297            under the name 'default' as we wish to be the default
298            backend, and also register as 'posix' */
299         ops.name = "default";
300         ret = ntvfs_register(&ops);
301
302         ops.name = "posix";
303         ret = ntvfs_register(&ops);
304
305         if (!NT_STATUS_IS_OK(ret)) {
306                 DEBUG(0,("Failed to register POSIX backend!\n"));
307         }
308
309         return ret;
310 }