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