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