s3/packaging: pam_winbind has been moved to section 8.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_streams_depot.c
blob72affe402ab3d0b4f9b0f17b9af6be2c37d35b9e
1 /*
2 * Store streams in a separate subdirectory
4 * Copyright (C) Volker Lendecke, 2007
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.
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.
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/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
26 * Excerpt from a mail from tridge:
28 * Volker, what I'm thinking of is this:
29 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
30 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
32 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
33 * is the fsid/inode. "namedstreamX" is a file named after the stream
34 * name.
37 static uint32_t hash_fn(DATA_BLOB key)
39 uint32_t value; /* Used to compute the hash value. */
40 uint32_t i; /* Used to cycle through random values. */
42 /* Set the initial value from the key size. */
43 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
44 value = (value + (key.data[i] << (i*5 % 24)));
46 return (1103515243 * value + 12345);
50 * With the hashing scheme based on the inode we need to protect against
51 * streams showing up on files with re-used inodes. This can happen if we
52 * create a stream directory from within Samba, and a local process or NFS
53 * client deletes the file without deleting the streams directory. When the
54 * inode is re-used and the stream directory is still around, the streams in
55 * there would be show up as belonging to the new file.
57 * There are several workarounds for this, probably the easiest one is on
58 * systems which have a true birthtime stat element: When the file has a later
59 * birthtime than the streams directory, then we have to recreate the
60 * directory.
62 * The other workaround is to somehow mark the file as generated by Samba with
63 * something that a NFS client would not do. The closest one is a special
64 * xattr value being set. On systems which do not support xattrs, it might be
65 * an option to put in a special ACL entry for a non-existing group.
68 #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
70 static bool file_is_valid(vfs_handle_struct *handle, const char *path,
71 bool check_valid)
73 char buf;
75 if (!check_valid) {
76 return true;
79 DEBUG(10, ("file_is_valid (%s) called\n", path));
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;
87 if (buf != '1') {
88 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
89 return false;
92 return true;
95 static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
96 bool check_valid)
98 char buf = '1';
99 int ret;
101 if (!check_valid) {
102 return true;
105 DEBUG(10, ("marking file %s as valid\n", path));
107 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
108 &buf, sizeof(buf), 0);
110 if (ret == -1) {
111 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
112 return false;
115 return true;
118 static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
119 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
121 uint32_t hash;
122 char *result = NULL;
123 SMB_STRUCT_STAT sbuf;
124 uint8_t first, second;
125 char *tmp;
126 char *id_hex;
127 struct file_id id;
128 uint8 id_buf[16];
129 bool check_valid;
130 const char *rootdir;
132 check_valid = lp_parm_bool(SNUM(handle->conn),
133 "streams_depot", "check_valid", true);
135 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
137 if (tmp == NULL) {
138 errno = ENOMEM;
139 goto fail;
142 rootdir = lp_parm_const_string(
143 SNUM(handle->conn), "streams_depot", "directory",
144 tmp);
146 if (base_sbuf == NULL) {
147 if (SMB_VFS_NEXT_STAT(handle, base_path, &sbuf) == -1) {
149 * base file is not there
151 goto fail;
153 base_sbuf = &sbuf;
156 id = SMB_VFS_FILE_ID_CREATE(handle->conn, base_sbuf);
158 push_file_id_16((char *)id_buf, &id);
160 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
162 first = hash & 0xff;
163 second = (hash >> 8) & 0xff;
165 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
167 if (id_hex == NULL) {
168 errno = ENOMEM;
169 goto fail;
172 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
173 first, second, id_hex);
175 TALLOC_FREE(id_hex);
177 if (result == NULL) {
178 errno = ENOMEM;
179 return NULL;
182 if (SMB_VFS_NEXT_STAT(handle, result, &sbuf) == 0) {
183 char *newname;
185 if (!S_ISDIR(sbuf.st_ex_mode)) {
186 errno = EINVAL;
187 goto fail;
190 if (file_is_valid(handle, base_path, check_valid)) {
191 return result;
195 * Someone has recreated a file under an existing inode
196 * without deleting the streams directory. For now, just move
197 * it away.
200 again:
201 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
202 if (newname == NULL) {
203 errno = ENOMEM;
204 goto fail;
207 if (SMB_VFS_NEXT_RENAME(handle, result, newname) == -1) {
208 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
209 TALLOC_FREE(newname);
210 goto again;
212 goto fail;
215 TALLOC_FREE(newname);
218 if (!create_it) {
219 errno = ENOENT;
220 goto fail;
223 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
224 && (errno != EEXIST)) {
225 goto fail;
228 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
229 if (tmp == NULL) {
230 errno = ENOMEM;
231 goto fail;
234 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
235 && (errno != EEXIST)) {
236 goto fail;
239 TALLOC_FREE(tmp);
241 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
242 second);
243 if (tmp == NULL) {
244 errno = ENOMEM;
245 goto fail;
248 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
249 && (errno != EEXIST)) {
250 goto fail;
253 TALLOC_FREE(tmp);
255 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
256 && (errno != EEXIST)) {
257 goto fail;
260 if (!mark_file_valid(handle, base_path, check_valid)) {
261 goto fail;
264 return result;
266 fail:
267 TALLOC_FREE(result);
268 return NULL;
271 static char *stream_name(vfs_handle_struct *handle, const char *fname,
272 bool create_dir)
274 char *base = NULL;
275 char *sname = NULL;
276 char *id_hex = NULL;
277 char *dirname, *stream_fname;
279 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
280 &base, &sname))) {
281 DEBUG(10, ("split_ntfs_stream_name failed\n"));
282 errno = ENOMEM;
283 goto fail;
286 /* if it's the ::$DATA stream just return the base file name */
287 if (!sname) {
288 return base;
291 dirname = stream_dir(handle, base, NULL, create_dir);
293 if (dirname == NULL) {
294 goto fail;
297 stream_fname = talloc_asprintf(talloc_tos(), "%s/:%s", dirname, sname);
299 if (stream_fname == NULL) {
300 errno = ENOMEM;
301 goto fail;
304 DEBUG(10, ("stream filename = %s\n", stream_fname));
306 TALLOC_FREE(base);
307 TALLOC_FREE(sname);
308 TALLOC_FREE(id_hex);
310 return stream_fname;
312 fail:
313 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
314 TALLOC_FREE(base);
315 TALLOC_FREE(sname);
316 TALLOC_FREE(id_hex);
317 return NULL;
320 static NTSTATUS walk_streams(vfs_handle_struct *handle,
321 const char *fname,
322 const SMB_STRUCT_STAT *sbuf,
323 char **pdirname,
324 bool (*fn)(const char *dirname,
325 const char *dirent,
326 void *private_data),
327 void *private_data)
329 char *dirname;
330 SMB_STRUCT_DIR *dirhandle = NULL;
331 char *dirent;
333 dirname = stream_dir(handle, fname, sbuf, false);
335 if (dirname == NULL) {
336 if (errno == ENOENT) {
338 * no stream around
340 return NT_STATUS_OK;
342 return map_nt_error_from_unix(errno);
345 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
347 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
349 if (dirhandle == NULL) {
350 TALLOC_FREE(dirname);
351 return map_nt_error_from_unix(errno);
354 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL)) != NULL) {
356 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
357 continue;
360 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
362 if (!fn(dirname, dirent, private_data)) {
363 break;
367 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
369 if (pdirname != NULL) {
370 *pdirname = dirname;
372 else {
373 TALLOC_FREE(dirname);
376 return NT_STATUS_OK;
379 static int streams_depot_stat(vfs_handle_struct *handle, const char *fname,
380 SMB_STRUCT_STAT *sbuf)
382 char *stream_fname;
383 int ret = -1;
385 DEBUG(10, ("streams_depot_stat called for [%s]\n", fname));
387 if (!is_ntfs_stream_name(fname)) {
388 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
391 stream_fname = stream_name(handle, fname, false);
392 if (stream_fname == NULL) {
393 goto done;
396 ret = SMB_VFS_NEXT_STAT(handle, stream_fname, sbuf);
398 done:
399 TALLOC_FREE(stream_fname);
400 return ret;
403 static int streams_depot_lstat(vfs_handle_struct *handle, const char *fname,
404 SMB_STRUCT_STAT *sbuf)
406 char *stream_fname;
407 int ret = -1;
409 if (!is_ntfs_stream_name(fname)) {
410 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
413 stream_fname = stream_name(handle, fname, false);
414 if (stream_fname == NULL) {
415 goto done;
418 ret = SMB_VFS_NEXT_LSTAT(handle, stream_fname, sbuf);
420 done:
421 TALLOC_FREE(stream_fname);
422 return ret;
425 static int streams_depot_open(vfs_handle_struct *handle, const char *fname,
426 files_struct *fsp, int flags, mode_t mode)
428 TALLOC_CTX *frame;
429 char *base = NULL;
430 char *sname = NULL;
431 SMB_STRUCT_STAT base_sbuf;
432 char *stream_fname;
433 int ret = -1;
435 if (!is_ntfs_stream_name(fname)) {
436 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
439 frame = talloc_stackframe();
441 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
442 &base, &sname))) {
443 errno = ENOMEM;
444 goto done;
447 if (!sname) {
448 ret = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
449 goto done;
452 ret = SMB_VFS_NEXT_STAT(handle, base, &base_sbuf);
454 if (ret == -1) {
455 goto done;
458 TALLOC_FREE(base);
460 stream_fname = stream_name(handle, fname, true);
461 if (stream_fname == NULL) {
462 goto done;
465 ret = SMB_VFS_NEXT_OPEN(handle, stream_fname, fsp, flags, mode);
467 done:
468 TALLOC_FREE(frame);
469 return ret;
472 static int streams_depot_unlink(vfs_handle_struct *handle, const char *fname)
474 int ret = -1;
475 SMB_STRUCT_STAT sbuf;
477 DEBUG(10, ("streams_depot_unlink called for %s\n", fname));
479 if (is_ntfs_stream_name(fname)) {
480 char *stream_fname;
482 stream_fname = stream_name(handle, fname, false);
483 if (stream_fname == NULL) {
484 return -1;
487 ret = SMB_VFS_NEXT_UNLINK(handle, stream_fname);
489 TALLOC_FREE(stream_fname);
490 return ret;
494 * We potentially need to delete the per-inode streams directory
497 if (lp_posix_pathnames()) {
498 ret = SMB_VFS_NEXT_LSTAT(handle, fname, &sbuf);
499 } else {
500 ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
503 if (ret == -1) {
504 return -1;
507 if (sbuf.st_ex_nlink == 1) {
508 char *dirname = stream_dir(handle, fname, &sbuf, false);
510 if (dirname != NULL) {
511 SMB_VFS_NEXT_RMDIR(handle, dirname);
513 TALLOC_FREE(dirname);
516 return SMB_VFS_NEXT_UNLINK(handle, fname);
519 static int streams_depot_rename(vfs_handle_struct *handle,
520 const char *oldname,
521 const char *newname)
523 TALLOC_CTX *frame = NULL;
524 int ret = -1;
525 bool old_is_stream;
526 bool new_is_stream;
527 char *obase = NULL;
528 char *osname = NULL;
529 char *nbase = NULL;
530 char *nsname = NULL;
531 char *ostream_fname = NULL;
532 char *nstream_fname = NULL;
533 char *newname_full = NULL;
535 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
536 oldname, newname));
538 old_is_stream = is_ntfs_stream_name(oldname);
539 new_is_stream = is_ntfs_stream_name(newname);
541 if (!old_is_stream && !new_is_stream) {
542 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
545 frame = talloc_stackframe();
547 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), oldname,
548 &obase, &osname))) {
549 errno = ENOMEM;
550 goto done;
553 if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), newname,
554 &nbase, &nsname))) {
555 errno = ENOMEM;
556 goto done;
559 /* for now don't allow renames from or to the default stream */
560 if (!osname || !nsname) {
561 errno = ENOSYS;
562 goto done;
565 ostream_fname = stream_name(handle, oldname, false);
566 if (ostream_fname == NULL) {
567 return -1;
571 * Handle passing in a stream name without the base file. This is
572 * exercised by the NTRENAME streams rename path.
574 if (StrCaseCmp(nbase, "./") == 0) {
575 newname_full = talloc_asprintf(talloc_tos(), "%s:%s", obase,
576 nsname);
577 if (newname_full == NULL) {
578 errno = ENOMEM;
579 goto done;
583 nstream_fname = stream_name(handle,
584 newname_full ? newname_full : newname,
585 false);
586 if (nstream_fname == NULL) {
587 return -1;
590 ret = SMB_VFS_NEXT_RENAME(handle, ostream_fname, nstream_fname);
592 done:
593 TALLOC_FREE(frame);
594 return ret;
597 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
598 struct stream_struct **streams,
599 const char *name, SMB_OFF_T size,
600 SMB_OFF_T alloc_size)
602 struct stream_struct *tmp;
604 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
605 (*num_streams)+1);
606 if (tmp == NULL) {
607 return false;
610 tmp[*num_streams].name = talloc_strdup(tmp, name);
611 if (tmp[*num_streams].name == NULL) {
612 return false;
615 tmp[*num_streams].size = size;
616 tmp[*num_streams].alloc_size = alloc_size;
618 *streams = tmp;
619 *num_streams += 1;
620 return true;
623 struct streaminfo_state {
624 TALLOC_CTX *mem_ctx;
625 vfs_handle_struct *handle;
626 unsigned int num_streams;
627 struct stream_struct *streams;
628 NTSTATUS status;
631 static bool collect_one_stream(const char *dirname,
632 const char *dirent,
633 void *private_data)
635 struct streaminfo_state *state =
636 (struct streaminfo_state *)private_data;
637 char *full_sname;
638 SMB_STRUCT_STAT sbuf;
640 if (asprintf(&full_sname, "%s/%s", dirname, dirent) == -1) {
641 state->status = NT_STATUS_NO_MEMORY;
642 return false;
644 if (SMB_VFS_NEXT_STAT(state->handle, full_sname, &sbuf) == -1) {
645 DEBUG(10, ("Could not stat %s: %s\n", full_sname,
646 strerror(errno)));
647 SAFE_FREE(full_sname);
648 return true;
651 SAFE_FREE(full_sname);
653 if (!add_one_stream(state->mem_ctx,
654 &state->num_streams, &state->streams,
655 dirent, sbuf.st_ex_size,
656 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
657 &sbuf))) {
658 state->status = NT_STATUS_NO_MEMORY;
659 return false;
662 return true;
665 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
666 struct files_struct *fsp,
667 const char *fname,
668 TALLOC_CTX *mem_ctx,
669 unsigned int *pnum_streams,
670 struct stream_struct **pstreams)
672 SMB_STRUCT_STAT sbuf;
673 int ret;
674 NTSTATUS status;
675 struct streaminfo_state state;
677 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
678 if (is_ntfs_stream_name(fsp->fsp_name)) {
679 return NT_STATUS_INVALID_PARAMETER;
681 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf);
683 else {
684 if (is_ntfs_stream_name(fname)) {
685 return NT_STATUS_INVALID_PARAMETER;
687 if (lp_posix_pathnames()) {
688 ret = SMB_VFS_NEXT_LSTAT(handle, fname, &sbuf);
689 } else {
690 ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
694 if (ret == -1) {
695 return map_nt_error_from_unix(errno);
698 state.streams = NULL;
699 state.num_streams = 0;
701 if (!S_ISDIR(sbuf.st_ex_mode)) {
702 if (!add_one_stream(mem_ctx,
703 &state.num_streams, &state.streams,
704 "::$DATA", sbuf.st_ex_size,
705 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
706 &sbuf))) {
707 return NT_STATUS_NO_MEMORY;
711 state.mem_ctx = mem_ctx;
712 state.handle = handle;
713 state.status = NT_STATUS_OK;
715 status = walk_streams(handle, fname, &sbuf, NULL, collect_one_stream,
716 &state);
718 if (!NT_STATUS_IS_OK(status)) {
719 TALLOC_FREE(state.streams);
720 return status;
723 if (!NT_STATUS_IS_OK(state.status)) {
724 TALLOC_FREE(state.streams);
725 return state.status;
728 *pnum_streams = state.num_streams;
729 *pstreams = state.streams;
730 return NT_STATUS_OK;
733 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
735 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
738 /* VFS operations structure */
740 static vfs_op_tuple streams_depot_ops[] = {
741 {SMB_VFS_OP(streams_depot_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
742 SMB_VFS_LAYER_TRANSPARENT},
743 {SMB_VFS_OP(streams_depot_open), SMB_VFS_OP_OPEN,
744 SMB_VFS_LAYER_TRANSPARENT},
745 {SMB_VFS_OP(streams_depot_stat), SMB_VFS_OP_STAT,
746 SMB_VFS_LAYER_TRANSPARENT},
747 {SMB_VFS_OP(streams_depot_lstat), SMB_VFS_OP_LSTAT,
748 SMB_VFS_LAYER_TRANSPARENT},
749 {SMB_VFS_OP(streams_depot_unlink), SMB_VFS_OP_UNLINK,
750 SMB_VFS_LAYER_TRANSPARENT},
751 {SMB_VFS_OP(streams_depot_rename), SMB_VFS_OP_RENAME,
752 SMB_VFS_LAYER_TRANSPARENT},
753 {SMB_VFS_OP(streams_depot_streaminfo), SMB_VFS_OP_STREAMINFO,
754 SMB_VFS_LAYER_OPAQUE},
755 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
758 NTSTATUS vfs_streams_depot_init(void);
759 NTSTATUS vfs_streams_depot_init(void)
761 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
762 streams_depot_ops);