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