1 | /*
|
---|
2 | * Store streams in a separate subdirectory
|
---|
3 | *
|
---|
4 | * Copyright (C) Volker Lendecke, 2007
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 3 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "smbd/smbd.h"
|
---|
22 | #include "system/filesys.h"
|
---|
23 |
|
---|
24 | #undef DBGC_CLASS
|
---|
25 | #define DBGC_CLASS DBGC_VFS
|
---|
26 |
|
---|
27 | /*
|
---|
28 | * Excerpt from a mail from tridge:
|
---|
29 | *
|
---|
30 | * Volker, what I'm thinking of is this:
|
---|
31 | * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
|
---|
32 | * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
|
---|
33 | *
|
---|
34 | * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
|
---|
35 | * is the fsid/inode. "namedstreamX" is a file named after the stream
|
---|
36 | * name.
|
---|
37 | */
|
---|
38 |
|
---|
39 | static uint32_t hash_fn(DATA_BLOB key)
|
---|
40 | {
|
---|
41 | uint32_t value; /* Used to compute the hash value. */
|
---|
42 | uint32_t i; /* Used to cycle through random values. */
|
---|
43 |
|
---|
44 | /* Set the initial value from the key size. */
|
---|
45 | for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
|
---|
46 | value = (value + (key.data[i] << (i*5 % 24)));
|
---|
47 |
|
---|
48 | return (1103515243 * value + 12345);
|
---|
49 | }
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * With the hashing scheme based on the inode we need to protect against
|
---|
53 | * streams showing up on files with re-used inodes. This can happen if we
|
---|
54 | * create a stream directory from within Samba, and a local process or NFS
|
---|
55 | * client deletes the file without deleting the streams directory. When the
|
---|
56 | * inode is re-used and the stream directory is still around, the streams in
|
---|
57 | * there would be show up as belonging to the new file.
|
---|
58 | *
|
---|
59 | * There are several workarounds for this, probably the easiest one is on
|
---|
60 | * systems which have a true birthtime stat element: When the file has a later
|
---|
61 | * birthtime than the streams directory, then we have to recreate the
|
---|
62 | * directory.
|
---|
63 | *
|
---|
64 | * The other workaround is to somehow mark the file as generated by Samba with
|
---|
65 | * something that a NFS client would not do. The closest one is a special
|
---|
66 | * xattr value being set. On systems which do not support xattrs, it might be
|
---|
67 | * an option to put in a special ACL entry for a non-existing group.
|
---|
68 | */
|
---|
69 |
|
---|
70 | static bool file_is_valid(vfs_handle_struct *handle, const char *path,
|
---|
71 | bool check_valid)
|
---|
72 | {
|
---|
73 | char buf;
|
---|
74 |
|
---|
75 | if (!check_valid) {
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | DEBUG(10, ("file_is_valid (%s) called\n", path));
|
---|
80 |
|
---|
81 | if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
|
---|
82 | &buf, sizeof(buf)) != sizeof(buf)) {
|
---|
83 | DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (buf != '1') {
|
---|
88 | DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
|
---|
96 | bool check_valid)
|
---|
97 | {
|
---|
98 | char buf = '1';
|
---|
99 | int ret;
|
---|
100 |
|
---|
101 | if (!check_valid) {
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | DEBUG(10, ("marking file %s as valid\n", path));
|
---|
106 |
|
---|
107 | ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
|
---|
108 | &buf, sizeof(buf), 0);
|
---|
109 |
|
---|
110 | if (ret == -1) {
|
---|
111 | DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Given an smb_filename, determine the stream directory using the file's
|
---|
120 | * base_name.
|
---|
121 | */
|
---|
122 | static char *stream_dir(vfs_handle_struct *handle,
|
---|
123 | const struct smb_filename *smb_fname,
|
---|
124 | const SMB_STRUCT_STAT *base_sbuf, bool create_it)
|
---|
125 | {
|
---|
126 | uint32_t hash;
|
---|
127 | struct smb_filename *smb_fname_hash = NULL;
|
---|
128 | char *result = NULL;
|
---|
129 | SMB_STRUCT_STAT base_sbuf_tmp;
|
---|
130 | uint8_t first, second;
|
---|
131 | char *tmp;
|
---|
132 | char *id_hex;
|
---|
133 | struct file_id id;
|
---|
134 | uint8 id_buf[16];
|
---|
135 | bool check_valid;
|
---|
136 | const char *rootdir;
|
---|
137 | NTSTATUS status;
|
---|
138 |
|
---|
139 | check_valid = lp_parm_bool(SNUM(handle->conn),
|
---|
140 | "streams_depot", "check_valid", true);
|
---|
141 |
|
---|
142 | tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
|
---|
143 |
|
---|
144 | if (tmp == NULL) {
|
---|
145 | errno = ENOMEM;
|
---|
146 | goto fail;
|
---|
147 | }
|
---|
148 |
|
---|
149 | rootdir = lp_parm_const_string(
|
---|
150 | SNUM(handle->conn), "streams_depot", "directory",
|
---|
151 | tmp);
|
---|
152 |
|
---|
153 | /* Stat the base file if it hasn't already been done. */
|
---|
154 | if (base_sbuf == NULL) {
|
---|
155 | struct smb_filename *smb_fname_base = NULL;
|
---|
156 |
|
---|
157 | status = create_synthetic_smb_fname(talloc_tos(),
|
---|
158 | smb_fname->base_name,
|
---|
159 | NULL, NULL,
|
---|
160 | &smb_fname_base);
|
---|
161 | if (!NT_STATUS_IS_OK(status)) {
|
---|
162 | errno = map_errno_from_nt_status(status);
|
---|
163 | goto fail;
|
---|
164 | }
|
---|
165 | if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
|
---|
166 | TALLOC_FREE(smb_fname_base);
|
---|
167 | goto fail;
|
---|
168 | }
|
---|
169 | base_sbuf_tmp = smb_fname_base->st;
|
---|
170 | TALLOC_FREE(smb_fname_base);
|
---|
171 | } else {
|
---|
172 | base_sbuf_tmp = *base_sbuf;
|
---|
173 | }
|
---|
174 |
|
---|
175 | id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
|
---|
176 |
|
---|
177 | push_file_id_16((char *)id_buf, &id);
|
---|
178 |
|
---|
179 | hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
|
---|
180 |
|
---|
181 | first = hash & 0xff;
|
---|
182 | second = (hash >> 8) & 0xff;
|
---|
183 |
|
---|
184 | id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
|
---|
185 |
|
---|
186 | if (id_hex == NULL) {
|
---|
187 | errno = ENOMEM;
|
---|
188 | goto fail;
|
---|
189 | }
|
---|
190 |
|
---|
191 | result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
|
---|
192 | first, second, id_hex);
|
---|
193 |
|
---|
194 | TALLOC_FREE(id_hex);
|
---|
195 |
|
---|
196 | if (result == NULL) {
|
---|
197 | errno = ENOMEM;
|
---|
198 | return NULL;
|
---|
199 | }
|
---|
200 |
|
---|
201 | status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
|
---|
202 | &smb_fname_hash);
|
---|
203 | if (!NT_STATUS_IS_OK(status)) {
|
---|
204 | errno = map_errno_from_nt_status(status);
|
---|
205 | goto fail;
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
|
---|
209 | struct smb_filename *smb_fname_new = NULL;
|
---|
210 | char *newname;
|
---|
211 |
|
---|
212 | if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
|
---|
213 | errno = EINVAL;
|
---|
214 | goto fail;
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
|
---|
218 | return result;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Someone has recreated a file under an existing inode
|
---|
223 | * without deleting the streams directory. For now, just move
|
---|
224 | * it away.
|
---|
225 | */
|
---|
226 |
|
---|
227 | again:
|
---|
228 | newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
|
---|
229 | if (newname == NULL) {
|
---|
230 | errno = ENOMEM;
|
---|
231 | goto fail;
|
---|
232 | }
|
---|
233 |
|
---|
234 | status = create_synthetic_smb_fname(talloc_tos(), newname,
|
---|
235 | NULL, NULL,
|
---|
236 | &smb_fname_new);
|
---|
237 | TALLOC_FREE(newname);
|
---|
238 | if (!NT_STATUS_IS_OK(status)) {
|
---|
239 | errno = map_errno_from_nt_status(status);
|
---|
240 | goto fail;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
|
---|
244 | smb_fname_new) == -1) {
|
---|
245 | TALLOC_FREE(smb_fname_new);
|
---|
246 | if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
|
---|
247 | goto again;
|
---|
248 | }
|
---|
249 | goto fail;
|
---|
250 | }
|
---|
251 |
|
---|
252 | TALLOC_FREE(smb_fname_new);
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (!create_it) {
|
---|
256 | errno = ENOENT;
|
---|
257 | goto fail;
|
---|
258 | }
|
---|
259 |
|
---|
260 | if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
|
---|
261 | && (errno != EEXIST)) {
|
---|
262 | goto fail;
|
---|
263 | }
|
---|
264 |
|
---|
265 | tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
|
---|
266 | if (tmp == NULL) {
|
---|
267 | errno = ENOMEM;
|
---|
268 | goto fail;
|
---|
269 | }
|
---|
270 |
|
---|
271 | if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
|
---|
272 | && (errno != EEXIST)) {
|
---|
273 | goto fail;
|
---|
274 | }
|
---|
275 |
|
---|
276 | TALLOC_FREE(tmp);
|
---|
277 |
|
---|
278 | tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
|
---|
279 | second);
|
---|
280 | if (tmp == NULL) {
|
---|
281 | errno = ENOMEM;
|
---|
282 | goto fail;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
|
---|
286 | && (errno != EEXIST)) {
|
---|
287 | goto fail;
|
---|
288 | }
|
---|
289 |
|
---|
290 | TALLOC_FREE(tmp);
|
---|
291 |
|
---|
292 | if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
|
---|
293 | && (errno != EEXIST)) {
|
---|
294 | goto fail;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
|
---|
298 | goto fail;
|
---|
299 | }
|
---|
300 |
|
---|
301 | TALLOC_FREE(smb_fname_hash);
|
---|
302 | return result;
|
---|
303 |
|
---|
304 | fail:
|
---|
305 | TALLOC_FREE(smb_fname_hash);
|
---|
306 | TALLOC_FREE(result);
|
---|
307 | return NULL;
|
---|
308 | }
|
---|
309 | /**
|
---|
310 | * Given a stream name, populate smb_fname_out with the actual location of the
|
---|
311 | * stream.
|
---|
312 | */
|
---|
313 | static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
|
---|
314 | const struct smb_filename *smb_fname,
|
---|
315 | struct smb_filename **smb_fname_out,
|
---|
316 | bool create_dir)
|
---|
317 | {
|
---|
318 | char *dirname, *stream_fname;
|
---|
319 | const char *stype;
|
---|
320 | NTSTATUS status;
|
---|
321 |
|
---|
322 | *smb_fname_out = NULL;
|
---|
323 |
|
---|
324 | stype = strchr_m(smb_fname->stream_name + 1, ':');
|
---|
325 |
|
---|
326 | if (stype) {
|
---|
327 | if (StrCaseCmp(stype, ":$DATA") != 0) {
|
---|
328 | return NT_STATUS_INVALID_PARAMETER;
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | dirname = stream_dir(handle, smb_fname, NULL, create_dir);
|
---|
333 |
|
---|
334 | if (dirname == NULL) {
|
---|
335 | status = map_nt_error_from_unix(errno);
|
---|
336 | goto fail;
|
---|
337 | }
|
---|
338 |
|
---|
339 | stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
|
---|
340 | smb_fname->stream_name);
|
---|
341 |
|
---|
342 | if (stream_fname == NULL) {
|
---|
343 | status = NT_STATUS_NO_MEMORY;
|
---|
344 | goto fail;
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (stype == NULL) {
|
---|
348 | /* Append an explicit stream type if one wasn't specified. */
|
---|
349 | stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
|
---|
350 | stream_fname);
|
---|
351 | if (stream_fname == NULL) {
|
---|
352 | status = NT_STATUS_NO_MEMORY;
|
---|
353 | goto fail;
|
---|
354 | }
|
---|
355 | } else {
|
---|
356 | /* Normalize the stream type to upercase. */
|
---|
357 | strupper_m(strrchr_m(stream_fname, ':') + 1);
|
---|
358 | }
|
---|
359 |
|
---|
360 | DEBUG(10, ("stream filename = %s\n", stream_fname));
|
---|
361 |
|
---|
362 | /* Create an smb_filename with stream_name == NULL. */
|
---|
363 | status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
|
---|
364 | NULL, smb_fname_out);
|
---|
365 | if (!NT_STATUS_IS_OK(status)) {
|
---|
366 | return status;
|
---|
367 | }
|
---|
368 |
|
---|
369 | return NT_STATUS_OK;
|
---|
370 |
|
---|
371 | fail:
|
---|
372 | DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
|
---|
373 | TALLOC_FREE(*smb_fname_out);
|
---|
374 | return status;
|
---|
375 | }
|
---|
376 |
|
---|
377 | static NTSTATUS walk_streams(vfs_handle_struct *handle,
|
---|
378 | struct smb_filename *smb_fname_base,
|
---|
379 | char **pdirname,
|
---|
380 | bool (*fn)(const char *dirname,
|
---|
381 | const char *dirent,
|
---|
382 | void *private_data),
|
---|
383 | void *private_data)
|
---|
384 | {
|
---|
385 | char *dirname;
|
---|
386 | SMB_STRUCT_DIR *dirhandle = NULL;
|
---|
387 | const char *dirent = NULL;
|
---|
388 | char *talloced = NULL;
|
---|
389 |
|
---|
390 | dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
|
---|
391 | false);
|
---|
392 |
|
---|
393 | if (dirname == NULL) {
|
---|
394 | if (errno == ENOENT) {
|
---|
395 | /*
|
---|
396 | * no stream around
|
---|
397 | */
|
---|
398 | return NT_STATUS_OK;
|
---|
399 | }
|
---|
400 | return map_nt_error_from_unix(errno);
|
---|
401 | }
|
---|
402 |
|
---|
403 | DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
|
---|
404 |
|
---|
405 | dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
|
---|
406 |
|
---|
407 | if (dirhandle == NULL) {
|
---|
408 | TALLOC_FREE(dirname);
|
---|
409 | return map_nt_error_from_unix(errno);
|
---|
410 | }
|
---|
411 |
|
---|
412 | while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
|
---|
413 | &talloced)) != NULL) {
|
---|
414 |
|
---|
415 | if (ISDOT(dirent) || ISDOTDOT(dirent)) {
|
---|
416 | TALLOC_FREE(talloced);
|
---|
417 | continue;
|
---|
418 | }
|
---|
419 |
|
---|
420 | DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
|
---|
421 |
|
---|
422 | if (!fn(dirname, dirent, private_data)) {
|
---|
423 | TALLOC_FREE(talloced);
|
---|
424 | break;
|
---|
425 | }
|
---|
426 | TALLOC_FREE(talloced);
|
---|
427 | }
|
---|
428 |
|
---|
429 | SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
|
---|
430 |
|
---|
431 | if (pdirname != NULL) {
|
---|
432 | *pdirname = dirname;
|
---|
433 | }
|
---|
434 | else {
|
---|
435 | TALLOC_FREE(dirname);
|
---|
436 | }
|
---|
437 |
|
---|
438 | return NT_STATUS_OK;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * Helper to stat/lstat the base file of an smb_fname. This will actually
|
---|
443 | * fills in the stat struct in smb_filename.
|
---|
444 | */
|
---|
445 | static int streams_depot_stat_base(vfs_handle_struct *handle,
|
---|
446 | struct smb_filename *smb_fname,
|
---|
447 | bool follow_links)
|
---|
448 | {
|
---|
449 | char *tmp_stream_name;
|
---|
450 | int result;
|
---|
451 |
|
---|
452 | tmp_stream_name = smb_fname->stream_name;
|
---|
453 | smb_fname->stream_name = NULL;
|
---|
454 | if (follow_links) {
|
---|
455 | result = SMB_VFS_NEXT_STAT(handle, smb_fname);
|
---|
456 | } else {
|
---|
457 | result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
|
---|
458 | }
|
---|
459 | smb_fname->stream_name = tmp_stream_name;
|
---|
460 | return result;
|
---|
461 | }
|
---|
462 |
|
---|
463 | static int streams_depot_stat(vfs_handle_struct *handle,
|
---|
464 | struct smb_filename *smb_fname)
|
---|
465 | {
|
---|
466 | struct smb_filename *smb_fname_stream = NULL;
|
---|
467 | NTSTATUS status;
|
---|
468 | int ret = -1;
|
---|
469 |
|
---|
470 | DEBUG(10, ("streams_depot_stat called for [%s]\n",
|
---|
471 | smb_fname_str_dbg(smb_fname)));
|
---|
472 |
|
---|
473 | if (!is_ntfs_stream_smb_fname(smb_fname)) {
|
---|
474 | return SMB_VFS_NEXT_STAT(handle, smb_fname);
|
---|
475 | }
|
---|
476 |
|
---|
477 | /* If the default stream is requested, just stat the base file. */
|
---|
478 | if (is_ntfs_default_stream_smb_fname(smb_fname)) {
|
---|
479 | return streams_depot_stat_base(handle, smb_fname, true);
|
---|
480 | }
|
---|
481 |
|
---|
482 | /* Stat the actual stream now. */
|
---|
483 | status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
|
---|
484 | false);
|
---|
485 | if (!NT_STATUS_IS_OK(status)) {
|
---|
486 | ret = -1;
|
---|
487 | errno = map_errno_from_nt_status(status);
|
---|
488 | goto done;
|
---|
489 | }
|
---|
490 |
|
---|
491 | ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
|
---|
492 |
|
---|
493 | /* Update the original smb_fname with the stat info. */
|
---|
494 | smb_fname->st = smb_fname_stream->st;
|
---|
495 | done:
|
---|
496 | TALLOC_FREE(smb_fname_stream);
|
---|
497 | return ret;
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 |
|
---|
502 | static int streams_depot_lstat(vfs_handle_struct *handle,
|
---|
503 | struct smb_filename *smb_fname)
|
---|
504 | {
|
---|
505 | struct smb_filename *smb_fname_stream = NULL;
|
---|
506 | NTSTATUS status;
|
---|
507 | int ret = -1;
|
---|
508 |
|
---|
509 | DEBUG(10, ("streams_depot_lstat called for [%s]\n",
|
---|
510 | smb_fname_str_dbg(smb_fname)));
|
---|
511 |
|
---|
512 | if (!is_ntfs_stream_smb_fname(smb_fname)) {
|
---|
513 | return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
|
---|
514 | }
|
---|
515 |
|
---|
516 | /* If the default stream is requested, just stat the base file. */
|
---|
517 | if (is_ntfs_default_stream_smb_fname(smb_fname)) {
|
---|
518 | return streams_depot_stat_base(handle, smb_fname, false);
|
---|
519 | }
|
---|
520 |
|
---|
521 | /* Stat the actual stream now. */
|
---|
522 | status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
|
---|
523 | false);
|
---|
524 | if (!NT_STATUS_IS_OK(status)) {
|
---|
525 | ret = -1;
|
---|
526 | errno = map_errno_from_nt_status(status);
|
---|
527 | goto done;
|
---|
528 | }
|
---|
529 |
|
---|
530 | ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
|
---|
531 |
|
---|
532 | done:
|
---|
533 | TALLOC_FREE(smb_fname_stream);
|
---|
534 | return ret;
|
---|
535 | }
|
---|
536 |
|
---|
537 | static int streams_depot_open(vfs_handle_struct *handle,
|
---|
538 | struct smb_filename *smb_fname,
|
---|
539 | files_struct *fsp, int flags, mode_t mode)
|
---|
540 | {
|
---|
541 | struct smb_filename *smb_fname_stream = NULL;
|
---|
542 | struct smb_filename *smb_fname_base = NULL;
|
---|
543 | NTSTATUS status;
|
---|
544 | int ret = -1;
|
---|
545 |
|
---|
546 | if (!is_ntfs_stream_smb_fname(smb_fname)) {
|
---|
547 | return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
|
---|
548 | }
|
---|
549 |
|
---|
550 | /* If the default stream is requested, just open the base file. */
|
---|
551 | if (is_ntfs_default_stream_smb_fname(smb_fname)) {
|
---|
552 | char *tmp_stream_name;
|
---|
553 |
|
---|
554 | tmp_stream_name = smb_fname->stream_name;
|
---|
555 | smb_fname->stream_name = NULL;
|
---|
556 | ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
|
---|
557 | smb_fname->stream_name = tmp_stream_name;
|
---|
558 |
|
---|
559 | return ret;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /* Ensure the base file still exists. */
|
---|
563 | status = create_synthetic_smb_fname(talloc_tos(),
|
---|
564 | smb_fname->base_name,
|
---|
565 | NULL, NULL,
|
---|
566 | &smb_fname_base);
|
---|
567 | if (!NT_STATUS_IS_OK(status)) {
|
---|
568 | ret = -1;
|
---|
569 | errno = map_errno_from_nt_status(status);
|
---|
570 | goto done;
|
---|
571 | }
|
---|
572 |
|
---|
573 | ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
|
---|
574 | if (ret == -1) {
|
---|
575 | goto done;
|
---|
576 | }
|
---|
577 |
|
---|
578 | /* Determine the stream name, and then open it. */
|
---|
579 | status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
|
---|
580 | if (!NT_STATUS_IS_OK(status)) {
|
---|
581 | ret = -1;
|
---|
582 | errno = map_errno_from_nt_status(status);
|
---|
583 | goto done;
|
---|
584 | }
|
---|
585 |
|
---|
586 | ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
|
---|
587 |
|
---|
588 | done:
|
---|
589 | TALLOC_FREE(smb_fname_stream);
|
---|
590 | TALLOC_FREE(smb_fname_base);
|
---|
591 | return ret;
|
---|
592 | }
|
---|
593 |
|
---|
594 | static int streams_depot_unlink(vfs_handle_struct *handle,
|
---|
595 | const struct smb_filename *smb_fname)
|
---|
596 | {
|
---|
597 | struct smb_filename *smb_fname_base = NULL;
|
---|
598 | NTSTATUS status;
|
---|
599 | int ret = -1;
|
---|
600 |
|
---|
601 | DEBUG(10, ("streams_depot_unlink called for %s\n",
|
---|
602 | smb_fname_str_dbg(smb_fname)));
|
---|
603 |
|
---|
604 | /* If there is a valid stream, just unlink the stream and return. */
|
---|
605 | if (is_ntfs_stream_smb_fname(smb_fname) &&
|
---|
606 | !is_ntfs_default_stream_smb_fname(smb_fname)) {
|
---|
607 | struct smb_filename *smb_fname_stream = NULL;
|
---|
608 |
|
---|
609 | status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
|
---|
610 | false);
|
---|
611 | if (!NT_STATUS_IS_OK(status)) {
|
---|
612 | errno = map_errno_from_nt_status(status);
|
---|
613 | return -1;
|
---|
614 | }
|
---|
615 |
|
---|
616 | ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
|
---|
617 |
|
---|
618 | TALLOC_FREE(smb_fname_stream);
|
---|
619 | return ret;
|
---|
620 | }
|
---|
621 |
|
---|
622 | /*
|
---|
623 | * We potentially need to delete the per-inode streams directory
|
---|
624 | */
|
---|
625 |
|
---|
626 | status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
|
---|
627 | NULL, NULL, &smb_fname_base);
|
---|
628 | if (!NT_STATUS_IS_OK(status)) {
|
---|
629 | errno = map_errno_from_nt_status(status);
|
---|
630 | return -1;
|
---|
631 | }
|
---|
632 |
|
---|
633 | if (lp_posix_pathnames()) {
|
---|
634 | ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
|
---|
635 | } else {
|
---|
636 | ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
|
---|
637 | }
|
---|
638 |
|
---|
639 | if (ret == -1) {
|
---|
640 | TALLOC_FREE(smb_fname_base);
|
---|
641 | return -1;
|
---|
642 | }
|
---|
643 |
|
---|
644 | if (smb_fname_base->st.st_ex_nlink == 1) {
|
---|
645 | char *dirname = stream_dir(handle, smb_fname_base,
|
---|
646 | &smb_fname_base->st, false);
|
---|
647 |
|
---|
648 | if (dirname != NULL) {
|
---|
649 | SMB_VFS_NEXT_RMDIR(handle, dirname);
|
---|
650 | }
|
---|
651 | TALLOC_FREE(dirname);
|
---|
652 | }
|
---|
653 |
|
---|
654 | ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
|
---|
655 |
|
---|
656 | TALLOC_FREE(smb_fname_base);
|
---|
657 | return ret;
|
---|
658 | }
|
---|
659 |
|
---|
660 | static int streams_depot_rmdir(vfs_handle_struct *handle, const char *path)
|
---|
661 | {
|
---|
662 | struct smb_filename *smb_fname_base = NULL;
|
---|
663 | NTSTATUS status;
|
---|
664 | int ret = -1;
|
---|
665 |
|
---|
666 | DEBUG(10, ("streams_depot_rmdir called for %s\n", path));
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * We potentially need to delete the per-inode streams directory
|
---|
670 | */
|
---|
671 |
|
---|
672 | status = create_synthetic_smb_fname(talloc_tos(), path,
|
---|
673 | NULL, NULL, &smb_fname_base);
|
---|
674 | if (!NT_STATUS_IS_OK(status)) {
|
---|
675 | errno = map_errno_from_nt_status(status);
|
---|
676 | return -1;
|
---|
677 | }
|
---|
678 |
|
---|
679 | if (lp_posix_pathnames()) {
|
---|
680 | ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
|
---|
681 | } else {
|
---|
682 | ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
|
---|
683 | }
|
---|
684 |
|
---|
685 | if (ret == -1) {
|
---|
686 | TALLOC_FREE(smb_fname_base);
|
---|
687 | return -1;
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (smb_fname_base->st.st_ex_nlink == 2) {
|
---|
691 | char *dirname = stream_dir(handle, smb_fname_base,
|
---|
692 | &smb_fname_base->st, false);
|
---|
693 |
|
---|
694 | if (dirname != NULL) {
|
---|
695 | SMB_VFS_NEXT_RMDIR(handle, dirname);
|
---|
696 | }
|
---|
697 | TALLOC_FREE(dirname);
|
---|
698 | }
|
---|
699 |
|
---|
700 | ret = SMB_VFS_NEXT_RMDIR(handle, path);
|
---|
701 |
|
---|
702 | TALLOC_FREE(smb_fname_base);
|
---|
703 | return ret;
|
---|
704 | }
|
---|
705 |
|
---|
706 | static int streams_depot_rename(vfs_handle_struct *handle,
|
---|
707 | const struct smb_filename *smb_fname_src,
|
---|
708 | const struct smb_filename *smb_fname_dst)
|
---|
709 | {
|
---|
710 | struct smb_filename *smb_fname_src_stream = NULL;
|
---|
711 | struct smb_filename *smb_fname_dst_stream = NULL;
|
---|
712 | bool src_is_stream, dst_is_stream;
|
---|
713 | NTSTATUS status;
|
---|
714 | int ret = -1;
|
---|
715 |
|
---|
716 | DEBUG(10, ("streams_depot_rename called for %s => %s\n",
|
---|
717 | smb_fname_str_dbg(smb_fname_src),
|
---|
718 | smb_fname_str_dbg(smb_fname_dst)));
|
---|
719 |
|
---|
720 | src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
|
---|
721 | dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
|
---|
722 |
|
---|
723 | if (!src_is_stream && !dst_is_stream) {
|
---|
724 | return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
|
---|
725 | smb_fname_dst);
|
---|
726 | }
|
---|
727 |
|
---|
728 | /* for now don't allow renames from or to the default stream */
|
---|
729 | if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
|
---|
730 | is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
|
---|
731 | errno = ENOSYS;
|
---|
732 | goto done;
|
---|
733 | }
|
---|
734 |
|
---|
735 | status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
|
---|
736 | false);
|
---|
737 | if (!NT_STATUS_IS_OK(status)) {
|
---|
738 | errno = map_errno_from_nt_status(status);
|
---|
739 | goto done;
|
---|
740 | }
|
---|
741 |
|
---|
742 | status = stream_smb_fname(handle, smb_fname_dst,
|
---|
743 | &smb_fname_dst_stream, false);
|
---|
744 | if (!NT_STATUS_IS_OK(status)) {
|
---|
745 | errno = map_errno_from_nt_status(status);
|
---|
746 | goto done;
|
---|
747 | }
|
---|
748 |
|
---|
749 | ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
|
---|
750 | smb_fname_dst_stream);
|
---|
751 |
|
---|
752 | done:
|
---|
753 | TALLOC_FREE(smb_fname_src_stream);
|
---|
754 | TALLOC_FREE(smb_fname_dst_stream);
|
---|
755 | return ret;
|
---|
756 | }
|
---|
757 |
|
---|
758 | static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
|
---|
759 | struct stream_struct **streams,
|
---|
760 | const char *name, SMB_OFF_T size,
|
---|
761 | SMB_OFF_T alloc_size)
|
---|
762 | {
|
---|
763 | struct stream_struct *tmp;
|
---|
764 |
|
---|
765 | tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
|
---|
766 | (*num_streams)+1);
|
---|
767 | if (tmp == NULL) {
|
---|
768 | return false;
|
---|
769 | }
|
---|
770 |
|
---|
771 | tmp[*num_streams].name = talloc_strdup(tmp, name);
|
---|
772 | if (tmp[*num_streams].name == NULL) {
|
---|
773 | return false;
|
---|
774 | }
|
---|
775 |
|
---|
776 | tmp[*num_streams].size = size;
|
---|
777 | tmp[*num_streams].alloc_size = alloc_size;
|
---|
778 |
|
---|
779 | *streams = tmp;
|
---|
780 | *num_streams += 1;
|
---|
781 | return true;
|
---|
782 | }
|
---|
783 |
|
---|
784 | struct streaminfo_state {
|
---|
785 | TALLOC_CTX *mem_ctx;
|
---|
786 | vfs_handle_struct *handle;
|
---|
787 | unsigned int num_streams;
|
---|
788 | struct stream_struct *streams;
|
---|
789 | NTSTATUS status;
|
---|
790 | };
|
---|
791 |
|
---|
792 | static bool collect_one_stream(const char *dirname,
|
---|
793 | const char *dirent,
|
---|
794 | void *private_data)
|
---|
795 | {
|
---|
796 | struct streaminfo_state *state =
|
---|
797 | (struct streaminfo_state *)private_data;
|
---|
798 | struct smb_filename *smb_fname = NULL;
|
---|
799 | char *sname = NULL;
|
---|
800 | NTSTATUS status;
|
---|
801 | bool ret;
|
---|
802 |
|
---|
803 | sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
|
---|
804 | if (sname == NULL) {
|
---|
805 | state->status = NT_STATUS_NO_MEMORY;
|
---|
806 | ret = false;
|
---|
807 | goto out;
|
---|
808 | }
|
---|
809 |
|
---|
810 | status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
|
---|
811 | NULL, &smb_fname);
|
---|
812 | if (!NT_STATUS_IS_OK(status)) {
|
---|
813 | state->status = status;
|
---|
814 | ret = false;
|
---|
815 | goto out;
|
---|
816 | }
|
---|
817 |
|
---|
818 | if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
|
---|
819 | DEBUG(10, ("Could not stat %s: %s\n", sname,
|
---|
820 | strerror(errno)));
|
---|
821 | ret = true;
|
---|
822 | goto out;
|
---|
823 | }
|
---|
824 |
|
---|
825 | if (!add_one_stream(state->mem_ctx,
|
---|
826 | &state->num_streams, &state->streams,
|
---|
827 | dirent, smb_fname->st.st_ex_size,
|
---|
828 | SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
|
---|
829 | &smb_fname->st))) {
|
---|
830 | state->status = NT_STATUS_NO_MEMORY;
|
---|
831 | ret = false;
|
---|
832 | goto out;
|
---|
833 | }
|
---|
834 |
|
---|
835 | ret = true;
|
---|
836 | out:
|
---|
837 | TALLOC_FREE(sname);
|
---|
838 | TALLOC_FREE(smb_fname);
|
---|
839 | return ret;
|
---|
840 | }
|
---|
841 |
|
---|
842 | static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
|
---|
843 | struct files_struct *fsp,
|
---|
844 | const char *fname,
|
---|
845 | TALLOC_CTX *mem_ctx,
|
---|
846 | unsigned int *pnum_streams,
|
---|
847 | struct stream_struct **pstreams)
|
---|
848 | {
|
---|
849 | struct smb_filename *smb_fname_base = NULL;
|
---|
850 | int ret;
|
---|
851 | NTSTATUS status;
|
---|
852 | struct streaminfo_state state;
|
---|
853 |
|
---|
854 | status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
|
---|
855 | &smb_fname_base);
|
---|
856 | if (!NT_STATUS_IS_OK(status)) {
|
---|
857 | return status;
|
---|
858 | }
|
---|
859 |
|
---|
860 | if ((fsp != NULL) && (fsp->fh->fd != -1)) {
|
---|
861 | ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
|
---|
862 | }
|
---|
863 | else {
|
---|
864 | if (lp_posix_pathnames()) {
|
---|
865 | ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
|
---|
866 | } else {
|
---|
867 | ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
|
---|
868 | }
|
---|
869 | }
|
---|
870 |
|
---|
871 | if (ret == -1) {
|
---|
872 | status = map_nt_error_from_unix(errno);
|
---|
873 | goto out;
|
---|
874 | }
|
---|
875 |
|
---|
876 | state.streams = *pstreams;
|
---|
877 | state.num_streams = *pnum_streams;
|
---|
878 | state.mem_ctx = mem_ctx;
|
---|
879 | state.handle = handle;
|
---|
880 | state.status = NT_STATUS_OK;
|
---|
881 |
|
---|
882 | status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
|
---|
883 | &state);
|
---|
884 |
|
---|
885 | if (!NT_STATUS_IS_OK(status)) {
|
---|
886 | TALLOC_FREE(state.streams);
|
---|
887 | goto out;
|
---|
888 | }
|
---|
889 |
|
---|
890 | if (!NT_STATUS_IS_OK(state.status)) {
|
---|
891 | TALLOC_FREE(state.streams);
|
---|
892 | status = state.status;
|
---|
893 | goto out;
|
---|
894 | }
|
---|
895 |
|
---|
896 | *pnum_streams = state.num_streams;
|
---|
897 | *pstreams = state.streams;
|
---|
898 | status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
|
---|
899 |
|
---|
900 | out:
|
---|
901 | TALLOC_FREE(smb_fname_base);
|
---|
902 | return status;
|
---|
903 | }
|
---|
904 |
|
---|
905 | static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
|
---|
906 | enum timestamp_set_resolution *p_ts_res)
|
---|
907 | {
|
---|
908 | return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
|
---|
909 | }
|
---|
910 |
|
---|
911 | static struct vfs_fn_pointers vfs_streams_depot_fns = {
|
---|
912 | .fs_capabilities = streams_depot_fs_capabilities,
|
---|
913 | .open_fn = streams_depot_open,
|
---|
914 | .stat = streams_depot_stat,
|
---|
915 | .lstat = streams_depot_lstat,
|
---|
916 | .unlink = streams_depot_unlink,
|
---|
917 | .rmdir = streams_depot_rmdir,
|
---|
918 | .rename = streams_depot_rename,
|
---|
919 | .streaminfo = streams_depot_streaminfo,
|
---|
920 | };
|
---|
921 |
|
---|
922 | NTSTATUS vfs_streams_depot_init(void);
|
---|
923 | NTSTATUS vfs_streams_depot_init(void)
|
---|
924 | {
|
---|
925 | return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
|
---|
926 | &vfs_streams_depot_fns);
|
---|
927 | }
|
---|