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