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/security.h"
30 #include "lib/tdb/include/tdb.h"
31 #include "db_wrap.h"
32 #include "libcli/security/security.h"
33 #include "lib/events/events.h"
34 
35 
36 /*
37   setup config options for a posix share
38 */
pvfs_setup_options(struct pvfs_state * pvfs)39 static void pvfs_setup_options(struct pvfs_state *pvfs)
40 {
41 	struct share_config *scfg = pvfs->ntvfs->ctx->config;
42 	const char *eadb;
43 
44 	if (share_bool_option(scfg, SHARE_MAP_HIDDEN, SHARE_MAP_HIDDEN_DEFAULT))
45 		pvfs->flags |= PVFS_FLAG_MAP_HIDDEN;
46 	if (share_bool_option(scfg, SHARE_MAP_ARCHIVE, SHARE_MAP_ARCHIVE_DEFAULT))
47 		pvfs->flags |= PVFS_FLAG_MAP_ARCHIVE;
48 	if (share_bool_option(scfg, SHARE_MAP_SYSTEM, SHARE_MAP_SYSTEM_DEFAULT))
49 		pvfs->flags |= PVFS_FLAG_MAP_SYSTEM;
50 	if (share_bool_option(scfg, SHARE_READONLY, SHARE_READONLY_DEFAULT))
51 		pvfs->flags |= PVFS_FLAG_READONLY;
52 	if (share_bool_option(scfg, SHARE_STRICT_SYNC, SHARE_STRICT_SYNC_DEFAULT))
53 		pvfs->flags |= PVFS_FLAG_STRICT_SYNC;
54 	if (share_bool_option(scfg, SHARE_STRICT_LOCKING, SHARE_STRICT_LOCKING_DEFAULT))
55 		pvfs->flags |= PVFS_FLAG_STRICT_LOCKING;
56 	if (share_bool_option(scfg, SHARE_CI_FILESYSTEM, SHARE_CI_FILESYSTEM_DEFAULT))
57 		pvfs->flags |= PVFS_FLAG_CI_FILESYSTEM;
58 	if (share_bool_option(scfg, PVFS_FAKE_OPLOCKS, PVFS_FAKE_OPLOCKS_DEFAULT)) {
59 		pvfs->flags |= PVFS_FLAG_FAKE_OPLOCKS;
60 	}
61 
62 	/* this must be a power of 2 */
63 	pvfs->alloc_size_rounding = share_int_option(scfg,
64 							PVFS_ALLOCATION_ROUNDING,
65 							PVFS_ALLOCATION_ROUNDING_DEFAULT);
66 
67 	pvfs->search.inactivity_time = share_int_option(scfg,
68 							PVFS_SEARCH_INACTIVITY,
69 							PVFS_SEARCH_INACTIVITY_DEFAULT);
70 
71 #if HAVE_XATTR_SUPPORT
72 	if (share_bool_option(scfg, PVFS_XATTR, PVFS_XATTR_DEFAULT))
73 		pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
74 #endif
75 
76 	pvfs->sharing_violation_delay = share_int_option(scfg,
77 							PVFS_SHARE_DELAY,
78 							PVFS_SHARE_DELAY_DEFAULT);
79 
80 	pvfs->share_name = talloc_strdup(pvfs, scfg->name);
81 
82 	pvfs->fs_attribs =
83 		FS_ATTR_CASE_SENSITIVE_SEARCH |
84 		FS_ATTR_CASE_PRESERVED_NAMES |
85 		FS_ATTR_UNICODE_ON_DISK |
86 		FS_ATTR_SPARSE_FILES;
87 
88 	/* allow xattrs to be stored in a external tdb */
89 	eadb = share_string_option(scfg, PVFS_EADB, NULL);
90 	if (eadb != NULL) {
91 		pvfs->ea_db = tdb_wrap_open(pvfs, eadb, 50000,
92 					    TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
93 		if (pvfs->ea_db != NULL) {
94 			pvfs->flags |= PVFS_FLAG_XATTR_ENABLE;
95 		} else {
96 			DEBUG(0,("Failed to open eadb '%s' - %s\n",
97 				 eadb, strerror(errno)));
98 			pvfs->flags &= ~PVFS_FLAG_XATTR_ENABLE;
99 		}
100 	}
101 
102 	if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
103 		pvfs->fs_attribs |= FS_ATTR_NAMED_STREAMS;
104 	}
105 	if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
106 		pvfs->fs_attribs |= FS_ATTR_PERSISTANT_ACLS;
107 	}
108 
109 	pvfs->sid_cache.creator_owner = dom_sid_parse_talloc(pvfs, SID_CREATOR_OWNER);
110 	pvfs->sid_cache.creator_group = dom_sid_parse_talloc(pvfs, SID_CREATOR_GROUP);
111 
112 	/* check if the system really supports xattrs */
113 	if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
114 		pvfs_xattr_probe(pvfs);
115 	}
116 
117 	/* enable an ACL backend */
118 	pvfs->acl_ops = pvfs_acl_backend_byname(share_string_option(scfg, PVFS_ACL, "xattr"));
119 }
120 
pvfs_state_destructor(struct pvfs_state * pvfs)121 static int pvfs_state_destructor(struct pvfs_state *pvfs)
122 {
123 	struct pvfs_file *f, *fn;
124 	struct pvfs_search_state *s, *sn;
125 
126 	/*
127 	 * make sure we cleanup files and searches before anything else
128 	 * because there destructors need to acess the pvfs_state struct
129 	 */
130 	for (f=pvfs->files.list; f; f=fn) {
131 		fn = f->next;
132 		talloc_free(f);
133 	}
134 
135 	for (s=pvfs->search.list; s; s=sn) {
136 		sn = s->next;
137 		talloc_free(s);
138 	}
139 
140 	return 0;
141 }
142 
143 /*
144   connect to a share - used when a tree_connect operation comes
145   in. For a disk based backend we needs to ensure that the base
146   directory exists (tho it doesn't need to be accessible by the user,
147   that comes later)
148 */
pvfs_connect(struct ntvfs_module_context * ntvfs,struct ntvfs_request * req,const char * sharename)149 static NTSTATUS pvfs_connect(struct ntvfs_module_context *ntvfs,
150 			     struct ntvfs_request *req, const char *sharename)
151 {
152 	struct pvfs_state *pvfs;
153 	struct stat st;
154 	char *base_directory;
155 	NTSTATUS status;
156 
157 	pvfs = talloc_zero(ntvfs, struct pvfs_state);
158 	NT_STATUS_HAVE_NO_MEMORY(pvfs);
159 
160 	/* for simplicity of path construction, remove any trailing slash now */
161 	base_directory = talloc_strdup(pvfs, share_string_option(ntvfs->ctx->config, SHARE_PATH, ""));
162 	NT_STATUS_HAVE_NO_MEMORY(base_directory);
163 	if (strcmp(base_directory, "/") != 0) {
164 		trim_string(base_directory, NULL, "/");
165 	}
166 
167 	pvfs->ntvfs = ntvfs;
168 	pvfs->base_directory = base_directory;
169 
170 	/* the directory must exist. Note that we deliberately don't
171 	   check that it is readable */
172 	if (stat(pvfs->base_directory, &st) != 0 || !S_ISDIR(st.st_mode)) {
173 		DEBUG(0,("pvfs_connect: '%s' is not a directory, when connecting to [%s]\n",
174 			 pvfs->base_directory, sharename));
175 		return NT_STATUS_BAD_NETWORK_NAME;
176 	}
177 
178 	ntvfs->ctx->fs_type = talloc_strdup(ntvfs->ctx, "NTFS");
179 	NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->fs_type);
180 
181 	ntvfs->ctx->dev_type = talloc_strdup(ntvfs->ctx, "A:");
182 	NT_STATUS_HAVE_NO_MEMORY(ntvfs->ctx->dev_type);
183 
184 	ntvfs->private_data = pvfs;
185 
186 	pvfs->brl_context = brl_init(pvfs,
187 				     pvfs->ntvfs->ctx->server_id,
188 				     pvfs->ntvfs->ctx->msg_ctx);
189 	if (pvfs->brl_context == NULL) {
190 		return NT_STATUS_INTERNAL_DB_CORRUPTION;
191 	}
192 
193 	pvfs->odb_context = odb_init(pvfs, pvfs->ntvfs->ctx);
194 	if (pvfs->odb_context == NULL) {
195 		return NT_STATUS_INTERNAL_DB_CORRUPTION;
196 	}
197 
198 	/* allow this to be NULL - we just disable change notify */
199 	pvfs->notify_context = notify_init(pvfs,
200 					   pvfs->ntvfs->ctx->server_id,
201 					   pvfs->ntvfs->ctx->msg_ctx,
202 					   event_context_find(pvfs),
203 					   pvfs->ntvfs->ctx->config);
204 
205 	pvfs->sidmap = sidmap_open(pvfs);
206 	if (pvfs->sidmap == NULL) {
207 		return NT_STATUS_INTERNAL_DB_CORRUPTION;
208 	}
209 
210 	/* allocate the search handle -> ptr tree */
211 	pvfs->search.idtree = idr_init(pvfs);
212 	NT_STATUS_HAVE_NO_MEMORY(pvfs->search.idtree);
213 
214 	status = pvfs_mangle_init(pvfs);
215 	NT_STATUS_NOT_OK_RETURN(status);
216 
217 	pvfs_setup_options(pvfs);
218 
219 	talloc_set_destructor(pvfs, pvfs_state_destructor);
220 
221 #ifdef SIGXFSZ
222 	/* who had the stupid idea to generate a signal on a large
223 	   file write instead of just failing it!? */
224 	BlockSignals(True, SIGXFSZ);
225 #endif
226 
227 	return NT_STATUS_OK;
228 }
229 
230 /*
231   disconnect from a share
232 */
pvfs_disconnect(struct ntvfs_module_context * ntvfs)233 static NTSTATUS pvfs_disconnect(struct ntvfs_module_context *ntvfs)
234 {
235 	return NT_STATUS_OK;
236 }
237 
238 /*
239   check if a directory exists
240 */
pvfs_chkpath(struct ntvfs_module_context * ntvfs,struct ntvfs_request * req,union smb_chkpath * cp)241 static NTSTATUS pvfs_chkpath(struct ntvfs_module_context *ntvfs,
242 			     struct ntvfs_request *req,
243 			     union smb_chkpath *cp)
244 {
245 	struct pvfs_state *pvfs = ntvfs->private_data;
246 	struct pvfs_filename *name;
247 	NTSTATUS status;
248 
249 	/* resolve the cifs name to a posix name */
250 	status = pvfs_resolve_name(pvfs, req, cp->chkpath.in.path, 0, &name);
251 	NT_STATUS_NOT_OK_RETURN(status);
252 
253 	if (!name->exists) {
254 		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
255 	}
256 
257 	if (!S_ISDIR(name->st.st_mode)) {
258 		return NT_STATUS_NOT_A_DIRECTORY;
259 	}
260 
261 	return NT_STATUS_OK;
262 }
263 
264 /*
265   copy a set of files
266 */
pvfs_copy(struct ntvfs_module_context * ntvfs,struct ntvfs_request * req,struct smb_copy * cp)267 static NTSTATUS pvfs_copy(struct ntvfs_module_context *ntvfs,
268 			  struct ntvfs_request *req, struct smb_copy *cp)
269 {
270 	DEBUG(0,("pvfs_copy not implemented\n"));
271 	return NT_STATUS_NOT_SUPPORTED;
272 }
273 
274 /*
275   return print queue info
276 */
pvfs_lpq(struct ntvfs_module_context * ntvfs,struct ntvfs_request * req,union smb_lpq * lpq)277 static NTSTATUS pvfs_lpq(struct ntvfs_module_context *ntvfs,
278 			 struct ntvfs_request *req, union smb_lpq *lpq)
279 {
280 	return NT_STATUS_NOT_SUPPORTED;
281 }
282 
283 /* SMBtrans - not used on file shares */
pvfs_trans(struct ntvfs_module_context * ntvfs,struct ntvfs_request * req,struct smb_trans2 * trans2)284 static NTSTATUS pvfs_trans(struct ntvfs_module_context *ntvfs,
285 			   struct ntvfs_request *req, struct smb_trans2 *trans2)
286 {
287 	return NT_STATUS_ACCESS_DENIED;
288 }
289 
290 /*
291   initialialise the POSIX disk backend, registering ourselves with the ntvfs subsystem
292  */
ntvfs_posix_init(void)293 NTSTATUS ntvfs_posix_init(void)
294 {
295 	NTSTATUS ret;
296 	struct ntvfs_ops ops;
297 	NTVFS_CURRENT_CRITICAL_SIZES(vers);
298 
299 	ZERO_STRUCT(ops);
300 
301 	ops.type = NTVFS_DISK;
302 
303 	/* fill in all the operations */
304 	ops.connect = pvfs_connect;
305 	ops.disconnect = pvfs_disconnect;
306 	ops.unlink = pvfs_unlink;
307 	ops.chkpath = pvfs_chkpath;
308 	ops.qpathinfo = pvfs_qpathinfo;
309 	ops.setpathinfo = pvfs_setpathinfo;
310 	ops.open = pvfs_open;
311 	ops.mkdir = pvfs_mkdir;
312 	ops.rmdir = pvfs_rmdir;
313 	ops.rename = pvfs_rename;
314 	ops.copy = pvfs_copy;
315 	ops.ioctl = pvfs_ioctl;
316 	ops.read = pvfs_read;
317 	ops.write = pvfs_write;
318 	ops.seek = pvfs_seek;
319 	ops.flush = pvfs_flush;
320 	ops.close = pvfs_close;
321 	ops.exit = pvfs_exit;
322 	ops.lock = pvfs_lock;
323 	ops.setfileinfo = pvfs_setfileinfo;
324 	ops.qfileinfo = pvfs_qfileinfo;
325 	ops.fsinfo = pvfs_fsinfo;
326 	ops.lpq = pvfs_lpq;
327 	ops.search_first = pvfs_search_first;
328 	ops.search_next = pvfs_search_next;
329 	ops.search_close = pvfs_search_close;
330 	ops.trans = pvfs_trans;
331 	ops.logoff = pvfs_logoff;
332 	ops.async_setup = pvfs_async_setup;
333 	ops.cancel = pvfs_cancel;
334 	ops.notify = pvfs_notify;
335 
336 	/* register ourselves with the NTVFS subsystem. We register
337 	   under the name 'default' as we wish to be the default
338 	   backend, and also register as 'posix' */
339 	ops.name = "default";
340 	ret = ntvfs_register(&ops, &vers);
341 
342 	if (!NT_STATUS_IS_OK(ret)) {
343 		DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
344 	}
345 
346 	ops.name = "posix";
347 	ret = ntvfs_register(&ops, &vers);
348 
349 	if (!NT_STATUS_IS_OK(ret)) {
350 		DEBUG(0,("Failed to register POSIX backend as '%s'!\n", ops.name));
351 	}
352 
353 	if (NT_STATUS_IS_OK(ret)) {
354 		ret = ntvfs_common_init();
355 	}
356 
357 	return ret;
358 }
359