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