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