s3: smbd: Change open_streams_for_delete() to take a struct smb_filename *.
[kai/samba-autobuild/.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 #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 {
72         char buf;
73
74         DEBUG(10, ("file_is_valid (%s) called\n", path));
75
76         if (SMB_VFS_GETXATTR(handle->conn, 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_SETXATTR(handle->conn, 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 /**
109  * Given an smb_filename, determine the stream directory using the file's
110  * base_name.
111  */
112 static char *stream_dir(vfs_handle_struct *handle,
113                         const struct smb_filename *smb_fname,
114                         const SMB_STRUCT_STAT *base_sbuf, bool create_it)
115 {
116         uint32_t hash;
117         struct smb_filename *smb_fname_hash = NULL;
118         char *result = NULL;
119         SMB_STRUCT_STAT base_sbuf_tmp;
120         uint8_t first, second;
121         char *tmp;
122         char *id_hex;
123         struct file_id id;
124         uint8_t id_buf[16];
125         bool check_valid;
126         const char *rootdir;
127         struct smb_filename *rootdir_fname = NULL;
128         struct smb_filename *tmp_fname = NULL;
129
130         check_valid = lp_parm_bool(SNUM(handle->conn),
131                       "streams_depot", "check_valid", true);
132
133         tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->cwd);
134
135         if (tmp == NULL) {
136                 errno = ENOMEM;
137                 goto fail;
138         }
139
140         rootdir = lp_parm_const_string(
141                 SNUM(handle->conn), "streams_depot", "directory",
142                 tmp);
143
144         rootdir_fname = synthetic_smb_fname(talloc_tos(),
145                                         rootdir,
146                                         NULL,
147                                         NULL);
148         if (rootdir_fname == NULL) {
149                 errno = ENOMEM;
150                 goto fail;
151         }
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;
156
157                 smb_fname_base = synthetic_smb_fname(
158                         talloc_tos(), smb_fname->base_name, NULL, NULL);
159                 if (smb_fname_base == NULL) {
160                         errno = ENOMEM;
161                         goto fail;
162                 }
163                 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
164                         TALLOC_FREE(smb_fname_base);
165                         goto fail;
166                 }
167                 base_sbuf_tmp = smb_fname_base->st;
168                 TALLOC_FREE(smb_fname_base);
169         } else {
170                 base_sbuf_tmp = *base_sbuf;
171         }
172
173         id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
174
175         push_file_id_16((char *)id_buf, &id);
176
177         hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
178
179         first = hash & 0xff;
180         second = (hash >> 8) & 0xff;
181
182         id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
183
184         if (id_hex == NULL) {
185                 errno = ENOMEM;
186                 goto fail;
187         }
188
189         result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
190                                  first, second, id_hex);
191
192         TALLOC_FREE(id_hex);
193
194         if (result == NULL) {
195                 errno = ENOMEM;
196                 return NULL;
197         }
198
199         smb_fname_hash = synthetic_smb_fname(talloc_tos(), result, NULL, NULL);
200         if (smb_fname_hash == NULL) {
201                 errno = ENOMEM;
202                 goto fail;
203         }
204
205         if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
206                 struct smb_filename *smb_fname_new = NULL;
207                 char *newname;
208                 bool delete_lost;
209
210                 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
211                         errno = EINVAL;
212                         goto fail;
213                 }
214
215                 if (!check_valid ||
216                     file_is_valid(handle, smb_fname->base_name)) {
217                         return result;
218                 }
219
220                 /*
221                  * Someone has recreated a file under an existing inode
222                  * without deleting the streams directory.
223                  * Move it away or remove if streams_depot:delete_lost is set.
224                  */
225
226         again:
227                 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
228                                            "delete_lost", false);
229
230                 if (delete_lost) {
231                         DEBUG(3, ("Someone has recreated a file under an "
232                               "existing inode. Removing: %s\n",
233                               smb_fname_hash->base_name));
234                         recursive_rmdir(talloc_tos(), handle->conn,
235                                         smb_fname_hash);
236                         SMB_VFS_NEXT_RMDIR(handle, smb_fname_hash);
237                 } else {
238                         newname = talloc_asprintf(talloc_tos(), "lost-%lu",
239                                                   random());
240                         DEBUG(3, ("Someone has recreated a file under an "
241                               "existing inode. Renaming: %s to: %s\n",
242                               smb_fname_hash->base_name,
243                               newname));
244                         if (newname == NULL) {
245                                 errno = ENOMEM;
246                                 goto fail;
247                         }
248
249                         smb_fname_new = synthetic_smb_fname(
250                                 talloc_tos(), newname, NULL, NULL);
251                         TALLOC_FREE(newname);
252                         if (smb_fname_new == NULL) {
253                                 errno = ENOMEM;
254                                 goto fail;
255                         }
256
257                         if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
258                                                 smb_fname_new) == -1) {
259                                 TALLOC_FREE(smb_fname_new);
260                                 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
261                                         goto again;
262                                 }
263                                 goto fail;
264                         }
265
266                         TALLOC_FREE(smb_fname_new);
267                 }
268         }
269
270         if (!create_it) {
271                 errno = ENOENT;
272                 goto fail;
273         }
274
275         if ((SMB_VFS_NEXT_MKDIR(handle, rootdir_fname, 0755) != 0)
276             && (errno != EEXIST)) {
277                 goto fail;
278         }
279
280         tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
281         if (tmp == NULL) {
282                 errno = ENOMEM;
283                 goto fail;
284         }
285
286         tmp_fname = synthetic_smb_fname(talloc_tos(), tmp, NULL, NULL);
287         if (tmp_fname == NULL) {
288                 errno = ENOMEM;
289                 goto fail;
290         }
291
292         if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
293             && (errno != EEXIST)) {
294                 goto fail;
295         }
296
297         TALLOC_FREE(tmp);
298         TALLOC_FREE(tmp_fname);
299
300         tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
301                               second);
302         if (tmp == NULL) {
303                 errno = ENOMEM;
304                 goto fail;
305         }
306
307         tmp_fname = synthetic_smb_fname(talloc_tos(), tmp, NULL, NULL);
308         if (tmp_fname == NULL) {
309                 errno = ENOMEM;
310                 goto fail;
311         }
312
313         if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
314             && (errno != EEXIST)) {
315                 goto fail;
316         }
317
318         TALLOC_FREE(tmp);
319         TALLOC_FREE(tmp_fname);
320
321         /* smb_fname_hash is the struct smb_filename version of 'result' */
322         if ((SMB_VFS_NEXT_MKDIR(handle, smb_fname_hash, 0755) != 0)
323             && (errno != EEXIST)) {
324                 goto fail;
325         }
326
327         if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
328                 goto fail;
329         }
330
331         TALLOC_FREE(rootdir_fname);
332         TALLOC_FREE(tmp_fname);
333         TALLOC_FREE(smb_fname_hash);
334         return result;
335
336  fail:
337         TALLOC_FREE(rootdir_fname);
338         TALLOC_FREE(tmp_fname);
339         TALLOC_FREE(smb_fname_hash);
340         TALLOC_FREE(result);
341         return NULL;
342 }
343 /**
344  * Given a stream name, populate smb_fname_out with the actual location of the
345  * stream.
346  */
347 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
348                                  const struct smb_filename *smb_fname,
349                                  struct smb_filename **smb_fname_out,
350                                  bool create_dir)
351 {
352         char *dirname, *stream_fname;
353         const char *stype;
354         NTSTATUS status;
355
356         *smb_fname_out = NULL;
357
358         stype = strchr_m(smb_fname->stream_name + 1, ':');
359
360         if (stype) {
361                 if (strcasecmp_m(stype, ":$DATA") != 0) {
362                         return NT_STATUS_INVALID_PARAMETER;
363                 }
364         }
365
366         dirname = stream_dir(handle, smb_fname, NULL, create_dir);
367
368         if (dirname == NULL) {
369                 status = map_nt_error_from_unix(errno);
370                 goto fail;
371         }
372
373         stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
374                                        smb_fname->stream_name);
375
376         if (stream_fname == NULL) {
377                 status = NT_STATUS_NO_MEMORY;
378                 goto fail;
379         }
380
381         if (stype == NULL) {
382                 /* Append an explicit stream type if one wasn't specified. */
383                 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
384                                                stream_fname);
385                 if (stream_fname == NULL) {
386                         status = NT_STATUS_NO_MEMORY;
387                         goto fail;
388                 }
389         } else {
390                 /* Normalize the stream type to upercase. */
391                 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
392                         status = NT_STATUS_INVALID_PARAMETER;
393                         goto fail;
394                 }
395         }
396
397         DEBUG(10, ("stream filename = %s\n", stream_fname));
398
399         /* Create an smb_filename with stream_name == NULL. */
400         *smb_fname_out = synthetic_smb_fname(
401                 talloc_tos(), stream_fname, NULL, NULL);
402         if (*smb_fname_out == NULL) {
403                 return NT_STATUS_NO_MEMORY;
404         }
405
406         return NT_STATUS_OK;
407
408  fail:
409         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
410         TALLOC_FREE(*smb_fname_out);
411         return status;
412 }
413
414 static NTSTATUS walk_streams(vfs_handle_struct *handle,
415                              struct smb_filename *smb_fname_base,
416                              char **pdirname,
417                              bool (*fn)(const char *dirname,
418                                         const char *dirent,
419                                         void *private_data),
420                              void *private_data)
421 {
422         char *dirname;
423         struct smb_filename *dir_smb_fname = NULL;
424         DIR *dirhandle = NULL;
425         const char *dirent = NULL;
426         char *talloced = NULL;
427
428         dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
429                              false);
430
431         if (dirname == NULL) {
432                 if (errno == ENOENT) {
433                         /*
434                          * no stream around
435                          */
436                         return NT_STATUS_OK;
437                 }
438                 return map_nt_error_from_unix(errno);
439         }
440
441         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
442
443         dir_smb_fname = synthetic_smb_fname(talloc_tos(),
444                                         dirname,
445                                         NULL,
446                                         NULL);
447         if (dir_smb_fname == NULL) {
448                 TALLOC_FREE(dirname);
449                 return NT_STATUS_NO_MEMORY;
450         }
451
452         dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dir_smb_fname, NULL, 0);
453
454         TALLOC_FREE(dir_smb_fname);
455
456         if (dirhandle == NULL) {
457                 TALLOC_FREE(dirname);
458                 return map_nt_error_from_unix(errno);
459         }
460
461         while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
462                                          &talloced)) != NULL) {
463
464                 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
465                         TALLOC_FREE(talloced);
466                         continue;
467                 }
468
469                 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
470
471                 if (!fn(dirname, dirent, private_data)) {
472                         TALLOC_FREE(talloced);
473                         break;
474                 }
475                 TALLOC_FREE(talloced);
476         }
477
478         SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
479
480         if (pdirname != NULL) {
481                 *pdirname = dirname;
482         }
483         else {
484                 TALLOC_FREE(dirname);
485         }
486
487         return NT_STATUS_OK;
488 }
489
490 /**
491  * Helper to stat/lstat the base file of an smb_fname. This will actually
492  * fills in the stat struct in smb_filename.
493  */
494 static int streams_depot_stat_base(vfs_handle_struct *handle,
495                                    struct smb_filename *smb_fname,
496                                    bool follow_links)
497 {
498         char *tmp_stream_name;
499         int result;
500
501         tmp_stream_name = smb_fname->stream_name;
502         smb_fname->stream_name = NULL;
503         if (follow_links) {
504                 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
505         } else {
506                 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
507         }
508         smb_fname->stream_name = tmp_stream_name;
509         return result;
510 }
511
512 static int streams_depot_stat(vfs_handle_struct *handle,
513                               struct smb_filename *smb_fname)
514 {
515         struct smb_filename *smb_fname_stream = NULL;
516         NTSTATUS status;
517         int ret = -1;
518
519         DEBUG(10, ("streams_depot_stat called for [%s]\n",
520                    smb_fname_str_dbg(smb_fname)));
521
522         if (!is_ntfs_stream_smb_fname(smb_fname)) {
523                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
524         }
525
526         /* If the default stream is requested, just stat the base file. */
527         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
528                 return streams_depot_stat_base(handle, smb_fname, true);
529         }
530
531         /* Stat the actual stream now. */
532         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
533                                   false);
534         if (!NT_STATUS_IS_OK(status)) {
535                 ret = -1;
536                 errno = map_errno_from_nt_status(status);
537                 goto done;
538         }
539
540         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
541
542         /* Update the original smb_fname with the stat info. */
543         smb_fname->st = smb_fname_stream->st;
544  done:
545         TALLOC_FREE(smb_fname_stream);
546         return ret;
547 }
548
549
550
551 static int streams_depot_lstat(vfs_handle_struct *handle,
552                                struct smb_filename *smb_fname)
553 {
554         struct smb_filename *smb_fname_stream = NULL;
555         NTSTATUS status;
556         int ret = -1;
557
558         DEBUG(10, ("streams_depot_lstat called for [%s]\n",
559                    smb_fname_str_dbg(smb_fname)));
560
561         if (!is_ntfs_stream_smb_fname(smb_fname)) {
562                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
563         }
564
565         /* If the default stream is requested, just stat the base file. */
566         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
567                 return streams_depot_stat_base(handle, smb_fname, false);
568         }
569
570         /* Stat the actual stream now. */
571         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
572                                   false);
573         if (!NT_STATUS_IS_OK(status)) {
574                 ret = -1;
575                 errno = map_errno_from_nt_status(status);
576                 goto done;
577         }
578
579         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
580
581  done:
582         TALLOC_FREE(smb_fname_stream);
583         return ret;
584 }
585
586 static int streams_depot_open(vfs_handle_struct *handle,
587                               struct smb_filename *smb_fname,
588                               files_struct *fsp, int flags, mode_t mode)
589 {
590         struct smb_filename *smb_fname_stream = NULL;
591         struct smb_filename *smb_fname_base = NULL;
592         NTSTATUS status;
593         int ret = -1;
594
595         if (!is_ntfs_stream_smb_fname(smb_fname)) {
596                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
597         }
598
599         /* If the default stream is requested, just open the base file. */
600         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
601                 char *tmp_stream_name;
602
603                 tmp_stream_name = smb_fname->stream_name;
604                 smb_fname->stream_name = NULL;
605                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
606                 smb_fname->stream_name = tmp_stream_name;
607
608                 return ret;
609         }
610
611         /* Ensure the base file still exists. */
612         smb_fname_base = synthetic_smb_fname(
613                 talloc_tos(), smb_fname->base_name, NULL, NULL);
614         if (smb_fname_base == NULL) {
615                 ret = -1;
616                 errno = ENOMEM;
617                 goto done;
618         }
619
620         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
621         if (ret == -1) {
622                 goto done;
623         }
624
625         /* Determine the stream name, and then open it. */
626         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
627         if (!NT_STATUS_IS_OK(status)) {
628                 ret = -1;
629                 errno = map_errno_from_nt_status(status);
630                 goto done;
631         }
632
633         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
634
635  done:
636         TALLOC_FREE(smb_fname_stream);
637         TALLOC_FREE(smb_fname_base);
638         return ret;
639 }
640
641 static int streams_depot_unlink(vfs_handle_struct *handle,
642                                 const struct smb_filename *smb_fname)
643 {
644         struct smb_filename *smb_fname_base = NULL;
645         int ret = -1;
646
647         DEBUG(10, ("streams_depot_unlink called for %s\n",
648                    smb_fname_str_dbg(smb_fname)));
649
650         /* If there is a valid stream, just unlink the stream and return. */
651         if (is_ntfs_stream_smb_fname(smb_fname) &&
652             !is_ntfs_default_stream_smb_fname(smb_fname)) {
653                 struct smb_filename *smb_fname_stream = NULL;
654                 NTSTATUS status;
655
656                 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
657                                           false);
658                 if (!NT_STATUS_IS_OK(status)) {
659                         errno = map_errno_from_nt_status(status);
660                         return -1;
661                 }
662
663                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
664
665                 TALLOC_FREE(smb_fname_stream);
666                 return ret;
667         }
668
669         /*
670          * We potentially need to delete the per-inode streams directory
671          */
672
673         smb_fname_base = synthetic_smb_fname(
674                 talloc_tos(), smb_fname->base_name, NULL, NULL);
675         if (smb_fname_base == NULL) {
676                 errno = ENOMEM;
677                 return -1;
678         }
679
680         if (lp_posix_pathnames()) {
681                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
682         } else {
683                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
684         }
685
686         if (ret == -1) {
687                 TALLOC_FREE(smb_fname_base);
688                 return -1;
689         }
690
691         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
692         if (ret == 0) {
693                 char *dirname = stream_dir(handle, smb_fname_base,
694                                            &smb_fname_base->st, false);
695
696                 if (dirname != NULL) {
697                         struct smb_filename *smb_fname_dir =
698                                 synthetic_smb_fname(talloc_tos(),
699                                                 dirname,
700                                                 NULL,
701                                                 NULL);
702                         if (smb_fname_dir == NULL) {
703                                 TALLOC_FREE(smb_fname_base);
704                                 TALLOC_FREE(dirname);
705                                 errno = ENOMEM;
706                                 return -1;
707                         }
708                         SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
709                         TALLOC_FREE(smb_fname_dir);
710                 }
711                 TALLOC_FREE(dirname);
712         }
713
714         TALLOC_FREE(smb_fname_base);
715         return ret;
716 }
717
718 static int streams_depot_rmdir(vfs_handle_struct *handle,
719                                 const struct smb_filename *smb_fname)
720 {
721         struct smb_filename *smb_fname_base = NULL;
722         int ret = -1;
723
724         DEBUG(10, ("streams_depot_rmdir called for %s\n",
725                 smb_fname->base_name));
726
727         /*
728          * We potentially need to delete the per-inode streams directory
729          */
730
731         smb_fname_base = synthetic_smb_fname(talloc_tos(),
732                                 smb_fname->base_name,
733                                 NULL,
734                                 NULL);
735         if (smb_fname_base == NULL) {
736                 errno = ENOMEM;
737                 return -1;
738         }
739
740         if (lp_posix_pathnames()) {
741                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
742         } else {
743                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
744         }
745
746         if (ret == -1) {
747                 TALLOC_FREE(smb_fname_base);
748                 return -1;
749         }
750
751         ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname_base);
752         if (ret == 0) {
753                 char *dirname = stream_dir(handle, smb_fname_base,
754                                            &smb_fname_base->st, false);
755
756                 if (dirname != NULL) {
757                         struct smb_filename *smb_fname_dir =
758                                 synthetic_smb_fname(talloc_tos(),
759                                                 dirname,
760                                                 NULL,
761                                                 NULL);
762                         if (smb_fname_dir == NULL) {
763                                 TALLOC_FREE(smb_fname_base);
764                                 TALLOC_FREE(dirname);
765                                 errno = ENOMEM;
766                                 return -1;
767                         }
768                         SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
769                         TALLOC_FREE(smb_fname_dir);
770                 }
771                 TALLOC_FREE(dirname);
772         }
773
774         TALLOC_FREE(smb_fname_base);
775         return ret;
776 }
777
778 static int streams_depot_rename(vfs_handle_struct *handle,
779                                 const struct smb_filename *smb_fname_src,
780                                 const struct smb_filename *smb_fname_dst)
781 {
782         struct smb_filename *smb_fname_src_stream = NULL;
783         struct smb_filename *smb_fname_dst_stream = NULL;
784         bool src_is_stream, dst_is_stream;
785         NTSTATUS status;
786         int ret = -1;
787
788         DEBUG(10, ("streams_depot_rename called for %s => %s\n",
789                    smb_fname_str_dbg(smb_fname_src),
790                    smb_fname_str_dbg(smb_fname_dst)));
791
792         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
793         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
794
795         if (!src_is_stream && !dst_is_stream) {
796                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
797                                            smb_fname_dst);
798         }
799
800         /* for now don't allow renames from or to the default stream */
801         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
802             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
803                 errno = ENOSYS;
804                 goto done;
805         }
806
807         status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
808                                   false);
809         if (!NT_STATUS_IS_OK(status)) {
810                 errno = map_errno_from_nt_status(status);
811                 goto done;
812         }
813
814         status = stream_smb_fname(handle, smb_fname_dst,
815                                   &smb_fname_dst_stream, false);
816         if (!NT_STATUS_IS_OK(status)) {
817                 errno = map_errno_from_nt_status(status);
818                 goto done;
819         }
820
821         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
822                                   smb_fname_dst_stream);
823
824 done:
825         TALLOC_FREE(smb_fname_src_stream);
826         TALLOC_FREE(smb_fname_dst_stream);
827         return ret;
828 }
829
830 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
831                            struct stream_struct **streams,
832                            const char *name, off_t size,
833                            off_t alloc_size)
834 {
835         struct stream_struct *tmp;
836
837         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
838                                    (*num_streams)+1);
839         if (tmp == NULL) {
840                 return false;
841         }
842
843         tmp[*num_streams].name = talloc_strdup(tmp, name);
844         if (tmp[*num_streams].name == NULL) {
845                 return false;
846         }
847
848         tmp[*num_streams].size = size;
849         tmp[*num_streams].alloc_size = alloc_size;
850
851         *streams = tmp;
852         *num_streams += 1;
853         return true;
854 }
855
856 struct streaminfo_state {
857         TALLOC_CTX *mem_ctx;
858         vfs_handle_struct *handle;
859         unsigned int num_streams;
860         struct stream_struct *streams;
861         NTSTATUS status;
862 };
863
864 static bool collect_one_stream(const char *dirname,
865                                const char *dirent,
866                                void *private_data)
867 {
868         struct streaminfo_state *state =
869                 (struct streaminfo_state *)private_data;
870         struct smb_filename *smb_fname = NULL;
871         char *sname = NULL;
872         bool ret;
873
874         sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
875         if (sname == NULL) {
876                 state->status = NT_STATUS_NO_MEMORY;
877                 ret = false;
878                 goto out;
879         }
880
881         smb_fname = synthetic_smb_fname(talloc_tos(), sname, NULL, NULL);
882         if (smb_fname == NULL) {
883                 state->status = NT_STATUS_NO_MEMORY;
884                 ret = false;
885                 goto out;
886         }
887
888         if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
889                 DEBUG(10, ("Could not stat %s: %s\n", sname,
890                            strerror(errno)));
891                 ret = true;
892                 goto out;
893         }
894
895         if (!add_one_stream(state->mem_ctx,
896                             &state->num_streams, &state->streams,
897                             dirent, smb_fname->st.st_ex_size,
898                             SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
899                                                    &smb_fname->st))) {
900                 state->status = NT_STATUS_NO_MEMORY;
901                 ret = false;
902                 goto out;
903         }
904
905         ret = true;
906  out:
907         TALLOC_FREE(sname);
908         TALLOC_FREE(smb_fname);
909         return ret;
910 }
911
912 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
913                                          struct files_struct *fsp,
914                                          const struct smb_filename *smb_fname,
915                                          TALLOC_CTX *mem_ctx,
916                                          unsigned int *pnum_streams,
917                                          struct stream_struct **pstreams)
918 {
919         struct smb_filename *smb_fname_base = NULL;
920         int ret;
921         NTSTATUS status;
922         struct streaminfo_state state;
923
924         smb_fname_base = synthetic_smb_fname(talloc_tos(),
925                                         smb_fname->base_name,
926                                         NULL,
927                                         NULL);
928         if (smb_fname_base == NULL) {
929                 return NT_STATUS_NO_MEMORY;
930         }
931
932         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
933                 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
934         }
935         else {
936                 if (lp_posix_pathnames()) {
937                         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
938                 } else {
939                         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
940                 }
941         }
942
943         if (ret == -1) {
944                 status = map_nt_error_from_unix(errno);
945                 goto out;
946         }
947
948         state.streams = *pstreams;
949         state.num_streams = *pnum_streams;
950         state.mem_ctx = mem_ctx;
951         state.handle = handle;
952         state.status = NT_STATUS_OK;
953
954         if (S_ISLNK(smb_fname_base->st.st_ex_mode)) {
955                 /*
956                  * Currently we do't have SMB_VFS_LLISTXATTR
957                  * inside the VFS which means there's no way
958                  * to cope with a symlink when lp_posix_pathnames().
959                  * returns true. For now ignore links.
960                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
961                  */
962                 status = NT_STATUS_OK;
963         } else {
964                 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
965                               &state);
966         }
967
968         if (!NT_STATUS_IS_OK(status)) {
969                 TALLOC_FREE(state.streams);
970                 goto out;
971         }
972
973         if (!NT_STATUS_IS_OK(state.status)) {
974                 TALLOC_FREE(state.streams);
975                 status = state.status;
976                 goto out;
977         }
978
979         *pnum_streams = state.num_streams;
980         *pstreams = state.streams;
981         status = SMB_VFS_NEXT_STREAMINFO(handle,
982                                 fsp,
983                                 smb_fname_base,
984                                 mem_ctx,
985                                 pnum_streams,
986                                 pstreams);
987
988  out:
989         TALLOC_FREE(smb_fname_base);
990         return status;
991 }
992
993 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
994                         enum timestamp_set_resolution *p_ts_res)
995 {
996         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
997 }
998
999 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1000         .fs_capabilities_fn = streams_depot_fs_capabilities,
1001         .open_fn = streams_depot_open,
1002         .stat_fn = streams_depot_stat,
1003         .lstat_fn = streams_depot_lstat,
1004         .unlink_fn = streams_depot_unlink,
1005         .rmdir_fn = streams_depot_rmdir,
1006         .rename_fn = streams_depot_rename,
1007         .streaminfo_fn = streams_depot_streaminfo,
1008 };
1009
1010 NTSTATUS vfs_streams_depot_init(void);
1011 NTSTATUS vfs_streams_depot_init(void)
1012 {
1013         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1014                                 &vfs_streams_depot_fns);
1015 }