s3:streams_depot: map 'file::$DATA' to just 'file'
[kai/samba.git] / source3 / modules / vfs_streams_depot.c
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
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
24
25 /*
26  * Excerpt from a mail from tridge:
27  *
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
31  *
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.
35  */
36
37 static uint32_t hash_fn(DATA_BLOB key)
38 {
39         uint32_t value; /* Used to compute the hash value.  */
40         uint32_t i;     /* Used to cycle through random values. */
41
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)));
45
46         return (1103515243 * value + 12345);
47 }
48
49 /*
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.
56  *
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.
61  *
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.
66  */
67
68 #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
69
70 static bool file_is_valid(vfs_handle_struct *handle, const char *path)
71 {
72         char buf;
73
74         DEBUG(10, ("file_is_valid (%s) called\n", path));
75
76         if (SMB_VFS_NEXT_GETXATTR(handle, path, SAMBA_XATTR_MARKER,
77                                   &buf, sizeof(buf)) != sizeof(buf)) {
78                 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
79                 return false;
80         }
81
82         if (buf != '1') {
83                 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
84                 return false;
85         }
86
87         return true;
88 }
89
90 static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
91 {
92         char buf = '1';
93         int ret;
94
95         DEBUG(10, ("marking file %s as valid\n", path));
96
97         ret = SMB_VFS_NEXT_SETXATTR(handle, path, SAMBA_XATTR_MARKER,
98                                     &buf, sizeof(buf), 0);
99
100         if (ret == -1) {
101                 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
102                 return false;
103         }
104
105         return true;
106 }
107
108 static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
109                         const SMB_STRUCT_STAT *base_sbuf, bool create_it)
110 {
111         uint32_t hash;
112         char *result = NULL;
113         SMB_STRUCT_STAT sbuf;
114         uint8_t first, second;
115         char *tmp;
116         char *id_hex;
117         struct file_id id;
118         uint8 id_buf[16];
119         const char *rootdir;
120
121         tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
122
123         if (tmp == NULL) {
124                 errno = ENOMEM;
125                 goto fail;
126         }
127
128         rootdir = lp_parm_const_string(
129                 SNUM(handle->conn), "streams_depot", "directory",
130                 tmp);
131         TALLOC_FREE(tmp);
132
133         if (base_sbuf == NULL) {
134                 if (SMB_VFS_NEXT_STAT(handle, base_path, &sbuf) == -1) {
135                         /*
136                          * base file is not there
137                          */
138                         goto fail;
139                 }
140                 base_sbuf = &sbuf;
141         }
142
143         id = SMB_VFS_FILE_ID_CREATE(handle->conn, base_sbuf->st_dev,
144                                     base_sbuf->st_ino);
145
146         push_file_id_16((char *)id_buf, &id);
147
148         hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
149
150         first = hash & 0xff;
151         second = (hash >> 8) & 0xff;
152
153         id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
154
155         if (id_hex == NULL) {
156                 errno = ENOMEM;
157                 goto fail;
158         }
159
160         result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
161                                  first, second, id_hex);
162
163         TALLOC_FREE(id_hex);
164
165         if (result == NULL) {
166                 errno = ENOMEM;
167                 return NULL;
168         }
169
170         if (SMB_VFS_NEXT_STAT(handle, result, &sbuf) == 0) {
171                 char *newname;
172
173                 if (!S_ISDIR(sbuf.st_mode)) {
174                         errno = EINVAL;
175                         goto fail;
176                 }
177
178                 if (file_is_valid(handle, base_path)) {
179                         return result;
180                 }
181
182                 /*
183                  * Someone has recreated a file under an existing inode
184                  * without deleting the streams directory. For now, just move
185                  * it away.
186                  */
187
188         again:
189                 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
190                 if (newname == NULL) {
191                         errno = ENOMEM;
192                         goto fail;
193                 }
194
195                 if (SMB_VFS_NEXT_RENAME(handle, result, newname) == -1) {
196                         if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
197                                 TALLOC_FREE(newname);
198                                 goto again;
199                         }
200                         goto fail;
201                 }
202
203                 TALLOC_FREE(newname);
204         }
205
206         if (!create_it) {
207                 errno = ENOENT;
208                 goto fail;
209         }
210
211         if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
212             && (errno != EEXIST)) {
213                 goto fail;
214         }
215
216         tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
217         if (tmp == NULL) {
218                 errno = ENOMEM;
219                 goto fail;
220         }
221
222         if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
223             && (errno != EEXIST)) {
224                 goto fail;
225         }
226
227         TALLOC_FREE(tmp);
228
229         tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
230                               second);
231         if (tmp == NULL) {
232                 errno = ENOMEM;
233                 goto fail;
234         }
235
236         if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
237             && (errno != EEXIST)) {
238                 goto fail;
239         }
240
241         TALLOC_FREE(tmp);
242
243         if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
244             && (errno != EEXIST)) {
245                 goto fail;
246         }
247
248         if (!mark_file_valid(handle, base_path)) {
249                 goto fail;
250         }
251
252         return result;
253
254  fail:
255         TALLOC_FREE(result);
256         return NULL;
257 }
258
259 static char *stream_name(vfs_handle_struct *handle, const char *fname,
260                          bool create_dir)
261 {
262         char *base = NULL;
263         char *sname = NULL;
264         char *id_hex = NULL;
265         char *dirname, *stream_fname;
266
267         if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
268                                                     &base, &sname))) {
269                 DEBUG(10, ("split_ntfs_stream_name failed\n"));
270                 errno = ENOMEM;
271                 goto fail;
272         }
273
274         /* if it's the ::$DATA stream just return the base file name */
275         if (!sname) {
276                 return base;
277         }
278
279         dirname = stream_dir(handle, base, NULL, create_dir);
280
281         if (dirname == NULL) {
282                 goto fail;
283         }
284
285         stream_fname = talloc_asprintf(talloc_tos(), "%s/:%s", dirname, sname);
286
287         if (stream_fname == NULL) {
288                 errno = ENOMEM;
289                 goto fail;
290         }
291
292         DEBUG(10, ("stream filename = %s\n", stream_fname));
293
294         TALLOC_FREE(base);
295         TALLOC_FREE(sname);
296         TALLOC_FREE(id_hex);
297
298         return stream_fname;
299
300  fail:
301         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
302         TALLOC_FREE(base);
303         TALLOC_FREE(sname);
304         TALLOC_FREE(id_hex);
305         return NULL;
306 }
307
308 static NTSTATUS walk_streams(vfs_handle_struct *handle,
309                              const char *fname,
310                              const SMB_STRUCT_STAT *sbuf,
311                              char **pdirname,
312                              bool (*fn)(const char *dirname,
313                                         const char *dirent,
314                                         void *private_data),
315                              void *private_data)
316 {
317         char *dirname;
318         SMB_STRUCT_DIR *dirhandle = NULL;
319         char *dirent;
320
321         dirname = stream_dir(handle, fname, sbuf, false);
322
323         if (dirname == NULL) {
324                 if (errno == ENOENT) {
325                         /*
326                          * no stream around
327                          */
328                         return NT_STATUS_OK;
329                 }
330                 return map_nt_error_from_unix(errno);
331         }
332
333         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
334
335         dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
336
337         if (dirhandle == NULL) {
338                 TALLOC_FREE(dirname);
339                 return map_nt_error_from_unix(errno);
340         }
341
342         while ((dirent = vfs_readdirname(handle->conn, dirhandle)) != NULL) {
343
344                 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
345                         continue;
346                 }
347
348                 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
349
350                 if (!fn(dirname, dirent, private_data)) {
351                         break;
352                 }
353         }
354
355         SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
356
357         if (pdirname != NULL) {
358                 *pdirname = dirname;
359         }
360         else {
361                 TALLOC_FREE(dirname);
362         }
363
364         return NT_STATUS_OK;
365 }
366
367 static int streams_depot_stat(vfs_handle_struct *handle, const char *fname,
368                               SMB_STRUCT_STAT *sbuf)
369 {
370         char *stream_fname;
371         int ret = -1;
372
373         DEBUG(10, ("streams_depot_stat called for [%s]\n", fname));
374
375         if (!is_ntfs_stream_name(fname)) {
376                 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
377         }
378
379         stream_fname = stream_name(handle, fname, false);
380         if (stream_fname == NULL) {
381                 goto done;
382         }
383
384         ret = SMB_VFS_NEXT_STAT(handle, stream_fname, sbuf);
385
386  done:
387         TALLOC_FREE(stream_fname);
388         return ret;
389 }
390
391 static int streams_depot_lstat(vfs_handle_struct *handle, const char *fname,
392                                SMB_STRUCT_STAT *sbuf)
393 {
394         char *stream_fname;
395         int ret = -1;
396
397         if (!is_ntfs_stream_name(fname)) {
398                 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
399         }
400
401         stream_fname = stream_name(handle, fname, false);
402         if (stream_fname == NULL) {
403                 goto done;
404         }
405
406         ret = SMB_VFS_NEXT_LSTAT(handle, stream_fname, sbuf);
407
408  done:
409         TALLOC_FREE(stream_fname);
410         return ret;
411 }
412
413 static int streams_depot_open(vfs_handle_struct *handle,  const char *fname,
414                               files_struct *fsp, int flags, mode_t mode)
415 {
416         TALLOC_CTX *frame;
417         char *base = NULL;
418         char *sname = NULL;
419         SMB_STRUCT_STAT base_sbuf;
420         char *stream_fname;
421         int ret = -1;
422
423         if (!is_ntfs_stream_name(fname)) {
424                 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
425         }
426
427         frame = talloc_stackframe();
428
429         if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
430                                                     &base, &sname))) {
431                 errno = ENOMEM;
432                 goto done;
433         }
434
435         if (!sname) {
436                 ret = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
437                 goto done;
438         }
439
440         ret = SMB_VFS_NEXT_STAT(handle, base, &base_sbuf);
441
442         if (ret == -1) {
443                 goto done;
444         }
445
446         TALLOC_FREE(base);
447
448         stream_fname = stream_name(handle, fname, true);
449         if (stream_fname == NULL) {
450                 goto done;
451         }
452
453         ret = SMB_VFS_NEXT_OPEN(handle, stream_fname, fsp, flags, mode);
454
455  done:
456         TALLOC_FREE(frame);
457         return ret;
458 }
459
460 static int streams_depot_unlink(vfs_handle_struct *handle,  const char *fname)
461 {
462         int ret = -1;
463         SMB_STRUCT_STAT sbuf;
464
465         DEBUG(10, ("streams_depot_unlink called for %s\n", fname));
466
467         if (is_ntfs_stream_name(fname)) {
468                 char *stream_fname;
469
470                 stream_fname = stream_name(handle, fname, false);
471                 if (stream_fname == NULL) {
472                         return -1;
473                 }
474
475                 ret = SMB_VFS_NEXT_UNLINK(handle, stream_fname);
476
477                 TALLOC_FREE(stream_fname);
478                 return ret;
479         }
480
481         /*
482          * We potentially need to delete the per-inode streams directory
483          */
484
485         if (SMB_VFS_NEXT_STAT(handle, fname, &sbuf) == -1) {
486                 return -1;
487         }
488
489         if (sbuf.st_nlink == 1) {
490                 char *dirname = stream_dir(handle, fname, &sbuf, false);
491
492                 if (dirname != NULL) {
493                         SMB_VFS_NEXT_RMDIR(handle, dirname);
494                 }
495                 TALLOC_FREE(dirname);
496         }
497
498         return SMB_VFS_NEXT_UNLINK(handle, fname);
499 }
500
501 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
502                            struct stream_struct **streams,
503                            const char *name, SMB_OFF_T size,
504                            SMB_OFF_T alloc_size)
505 {
506         struct stream_struct *tmp;
507
508         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
509                                    (*num_streams)+1);
510         if (tmp == NULL) {
511                 return false;
512         }
513
514         tmp[*num_streams].name = talloc_strdup(tmp, name);
515         if (tmp[*num_streams].name == NULL) {
516                 return false;
517         }
518
519         tmp[*num_streams].size = size;
520         tmp[*num_streams].alloc_size = alloc_size;
521
522         *streams = tmp;
523         *num_streams += 1;
524         return true;
525 }
526
527 struct streaminfo_state {
528         TALLOC_CTX *mem_ctx;
529         vfs_handle_struct *handle;
530         unsigned int num_streams;
531         struct stream_struct *streams;
532         NTSTATUS status;
533 };
534
535 static bool collect_one_stream(const char *dirname,
536                                const char *dirent,
537                                void *private_data)
538 {
539         struct streaminfo_state *state =
540                 (struct streaminfo_state *)private_data;
541         char *full_sname;
542         SMB_STRUCT_STAT sbuf;
543
544         if (asprintf(&full_sname, "%s/%s", dirname, dirent) == -1) {
545                 state->status = NT_STATUS_NO_MEMORY;
546                 return false;
547         }
548         if (SMB_VFS_NEXT_STAT(state->handle, full_sname, &sbuf) == -1) {
549                 DEBUG(10, ("Could not stat %s: %s\n", full_sname,
550                            strerror(errno)));
551                 SAFE_FREE(full_sname);
552                 return true;
553         }
554
555         SAFE_FREE(full_sname);
556
557         if (!add_one_stream(state->mem_ctx,
558                             &state->num_streams, &state->streams,
559                             dirent, sbuf.st_size,
560                             get_allocation_size(
561                                     state->handle->conn, NULL, &sbuf))) {
562                 state->status = NT_STATUS_NO_MEMORY;
563                 return false;
564         }
565
566         return true;
567 }
568
569 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
570                                          struct files_struct *fsp,
571                                          const char *fname,
572                                          TALLOC_CTX *mem_ctx,
573                                          unsigned int *pnum_streams,
574                                          struct stream_struct **pstreams)
575 {
576         SMB_STRUCT_STAT sbuf;
577         int ret;
578         NTSTATUS status;
579         struct streaminfo_state state;
580
581         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
582                 if (is_ntfs_stream_name(fsp->fsp_name)) {
583                         return NT_STATUS_INVALID_PARAMETER;
584                 }
585                 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf);
586         }
587         else {
588                 if (is_ntfs_stream_name(fname)) {
589                         return NT_STATUS_INVALID_PARAMETER;
590                 }
591                 ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
592         }
593
594         if (ret == -1) {
595                 return map_nt_error_from_unix(errno);
596         }
597
598         state.streams = NULL;
599         state.num_streams = 0;
600
601         if (!S_ISDIR(sbuf.st_mode)) {
602                 if (!add_one_stream(mem_ctx,
603                                     &state.num_streams, &state.streams,
604                                     "::$DATA", sbuf.st_size,
605                                     get_allocation_size(handle->conn, fsp,
606                                                         &sbuf))) {
607                         return NT_STATUS_NO_MEMORY;
608                 }
609         }
610
611         state.mem_ctx = mem_ctx;
612         state.handle = handle;
613         state.status = NT_STATUS_OK;
614
615         status = walk_streams(handle, fname, &sbuf, NULL, collect_one_stream,
616                               &state);
617
618         if (!NT_STATUS_IS_OK(status)) {
619                 TALLOC_FREE(state.streams);
620                 return status;
621         }
622
623         if (!NT_STATUS_IS_OK(state.status)) {
624                 TALLOC_FREE(state.streams);
625                 return state.status;
626         }
627
628         *pnum_streams = state.num_streams;
629         *pstreams = state.streams;
630         return NT_STATUS_OK;
631 }
632
633 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
634 {
635         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
636 }
637
638 /* VFS operations structure */
639
640 static vfs_op_tuple streams_depot_ops[] = {
641         {SMB_VFS_OP(streams_depot_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
642          SMB_VFS_LAYER_TRANSPARENT},
643         {SMB_VFS_OP(streams_depot_open), SMB_VFS_OP_OPEN,
644          SMB_VFS_LAYER_TRANSPARENT},
645         {SMB_VFS_OP(streams_depot_stat), SMB_VFS_OP_STAT,
646          SMB_VFS_LAYER_TRANSPARENT},
647         {SMB_VFS_OP(streams_depot_lstat), SMB_VFS_OP_LSTAT,
648          SMB_VFS_LAYER_TRANSPARENT},
649         {SMB_VFS_OP(streams_depot_unlink), SMB_VFS_OP_UNLINK,
650          SMB_VFS_LAYER_TRANSPARENT},
651         {SMB_VFS_OP(streams_depot_streaminfo), SMB_VFS_OP_STREAMINFO,
652          SMB_VFS_LAYER_OPAQUE},
653         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
654 };
655
656 NTSTATUS vfs_streams_depot_init(void);
657 NTSTATUS vfs_streams_depot_init(void)
658 {
659         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
660                                 streams_depot_ops);
661 }