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