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