s3: smbd: Change open_streams_for_delete() to take a struct smb_filename *.
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_streams_xattr.c
1 /*
2  * Store streams in xattrs
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  *
6  * Partly based on James Peach's Darwin module, which is
7  *
8  * Copyright (C) James Peach 2006-2007
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
31
32 struct streams_xattr_config {
33         const char *prefix;
34         size_t prefix_len;
35         bool store_stream_type;
36 };
37
38 struct stream_io {
39         char *base;
40         char *xattr_name;
41         void *fsp_name_ptr;
42         files_struct *fsp;
43         vfs_handle_struct *handle;
44 };
45
46 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
47 {
48         MD5_CTX ctx;
49         unsigned char hash[16];
50         SMB_INO_T result;
51         char *upper_sname;
52
53         DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
54                    (unsigned long)sbuf->st_ex_dev,
55                    (unsigned long)sbuf->st_ex_ino, sname));
56
57         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
58         SMB_ASSERT(upper_sname != NULL);
59
60         MD5Init(&ctx);
61         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
62                   sizeof(sbuf->st_ex_dev));
63         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
64                   sizeof(sbuf->st_ex_ino));
65         MD5Update(&ctx, (unsigned char *)upper_sname,
66                   talloc_get_size(upper_sname)-1);
67         MD5Final(hash, &ctx);
68
69         TALLOC_FREE(upper_sname);
70
71         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
72         memcpy(&result, hash, sizeof(result));
73
74         DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
75
76         return result;
77 }
78
79 static ssize_t get_xattr_size(connection_struct *conn,
80                                 files_struct *fsp,
81                                 const char *fname,
82                                 const char *xattr_name)
83 {
84         NTSTATUS status;
85         struct ea_struct ea;
86         ssize_t result;
87
88         status = get_ea_value(talloc_tos(), conn, fsp, fname,
89                               xattr_name, &ea);
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 return -1;
93         }
94
95         result = ea.value.length-1;
96         TALLOC_FREE(ea.value.data);
97         return result;
98 }
99
100 /**
101  * Given a stream name, populate xattr_name with the xattr name to use for
102  * accessing the stream.
103  */
104 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
105                                        TALLOC_CTX *ctx,
106                                        const char *stream_name,
107                                        char **xattr_name)
108 {
109         char *sname;
110         char *stype;
111         struct streams_xattr_config *config;
112
113         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
114                                 return NT_STATUS_UNSUCCESSFUL);
115
116         sname = talloc_strdup(ctx, stream_name + 1);
117         if (sname == NULL) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         /*
122          * With vfs_fruit option "fruit:encoding = native" we're
123          * already converting stream names that contain illegal NTFS
124          * characters from their on-the-wire Unicode Private Range
125          * encoding to their native ASCII representation.
126          *
127          * As as result the name of xattrs storing the streams (via
128          * vfs_streams_xattr) may contain a colon, so we have to use
129          * strrchr_m() instead of strchr_m() for matching the stream
130          * type suffix.
131          *
132          * In check_path_syntax() we've already ensured the streamname
133          * we got from the client is valid.
134          */
135         stype = strrchr_m(sname, ':');
136
137         if (stype) {
138                 /*
139                  * We only support one stream type: "$DATA"
140                  */
141                 if (strcasecmp_m(stype, ":$DATA") != 0) {
142                         talloc_free(sname);
143                         return NT_STATUS_INVALID_PARAMETER;
144                 }
145
146                 /* Split name and type */
147                 stype[0] = '\0';
148         }
149
150         *xattr_name = talloc_asprintf(ctx, "%s%s%s",
151                                       config->prefix,
152                                       sname,
153                                       config->store_stream_type ? ":$DATA" : "");
154         if (*xattr_name == NULL) {
155                 talloc_free(sname);
156                 return NT_STATUS_NO_MEMORY;
157         }
158
159         DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
160                    stream_name));
161
162         talloc_free(sname);
163         return NT_STATUS_OK;
164 }
165
166 static bool streams_xattr_recheck(struct stream_io *sio)
167 {
168         NTSTATUS status;
169         char *xattr_name = NULL;
170
171         if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
172                 return true;
173         }
174
175         if (sio->fsp->fsp_name->stream_name == NULL) {
176                 /* how can this happen */
177                 errno = EINVAL;
178                 return false;
179         }
180
181         status = streams_xattr_get_name(sio->handle, talloc_tos(),
182                                         sio->fsp->fsp_name->stream_name,
183                                         &xattr_name);
184         if (!NT_STATUS_IS_OK(status)) {
185                 return false;
186         }
187
188         TALLOC_FREE(sio->xattr_name);
189         TALLOC_FREE(sio->base);
190         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
191                                         xattr_name);
192         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
193                                   sio->fsp->fsp_name->base_name);
194         sio->fsp_name_ptr = sio->fsp->fsp_name;
195
196         TALLOC_FREE(xattr_name);
197
198         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
199                 return false;
200         }
201
202         return true;
203 }
204
205 /**
206  * Helper to stat/lstat the base file of an smb_fname.
207  */
208 static int streams_xattr_stat_base(vfs_handle_struct *handle,
209                                    struct smb_filename *smb_fname,
210                                    bool follow_links)
211 {
212         char *tmp_stream_name;
213         int result;
214
215         tmp_stream_name = smb_fname->stream_name;
216         smb_fname->stream_name = NULL;
217         if (follow_links) {
218                 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
219         } else {
220                 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
221         }
222         smb_fname->stream_name = tmp_stream_name;
223         return result;
224 }
225
226 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
227                                SMB_STRUCT_STAT *sbuf)
228 {
229         struct smb_filename *smb_fname_base = NULL;
230         int ret = -1;
231         struct stream_io *io = (struct stream_io *)
232                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
233
234         DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
235
236         if (io == NULL || fsp->base_fsp == NULL) {
237                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
238         }
239
240         if (!streams_xattr_recheck(io)) {
241                 return -1;
242         }
243
244         /* Create an smb_filename with stream_name == NULL. */
245         smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
246                                              NULL, NULL);
247         if (smb_fname_base == NULL) {
248                 errno = ENOMEM;
249                 return -1;
250         }
251
252         if (lp_posix_pathnames()) {
253                 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
254         } else {
255                 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
256         }
257         *sbuf = smb_fname_base->st;
258         TALLOC_FREE(smb_fname_base);
259
260         if (ret == -1) {
261                 return -1;
262         }
263
264         sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
265                                         io->base, io->xattr_name);
266         if (sbuf->st_ex_size == -1) {
267                 return -1;
268         }
269
270         DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
271
272         sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
273         sbuf->st_ex_mode &= ~S_IFMT;
274         sbuf->st_ex_mode |= S_IFREG;
275         sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
276
277         return 0;
278 }
279
280 static int streams_xattr_stat(vfs_handle_struct *handle,
281                               struct smb_filename *smb_fname)
282 {
283         NTSTATUS status;
284         int result = -1;
285         char *xattr_name = NULL;
286
287         if (!is_ntfs_stream_smb_fname(smb_fname)) {
288                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
289         }
290
291         /* Note if lp_posix_paths() is true, we can never
292          * get here as is_ntfs_stream_smb_fname() is
293          * always false. So we never need worry about
294          * not following links here. */
295
296         /* If the default stream is requested, just stat the base file. */
297         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
298                 return streams_xattr_stat_base(handle, smb_fname, true);
299         }
300
301         /* Populate the stat struct with info from the base file. */
302         if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
303                 return -1;
304         }
305
306         /* Derive the xattr name to lookup. */
307         status = streams_xattr_get_name(handle, talloc_tos(),
308                                         smb_fname->stream_name, &xattr_name);
309         if (!NT_STATUS_IS_OK(status)) {
310                 errno = map_errno_from_nt_status(status);
311                 return -1;
312         }
313
314         /* Augment the base file's stat information before returning. */
315         smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
316                                                   smb_fname->base_name,
317                                                   xattr_name);
318         if (smb_fname->st.st_ex_size == -1) {
319                 errno = ENOENT;
320                 result = -1;
321                 goto fail;
322         }
323
324         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
325         smb_fname->st.st_ex_mode &= ~S_IFMT;
326         smb_fname->st.st_ex_mode |= S_IFREG;
327         smb_fname->st.st_ex_blocks =
328             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
329
330         result = 0;
331  fail:
332         TALLOC_FREE(xattr_name);
333         return result;
334 }
335
336 static int streams_xattr_lstat(vfs_handle_struct *handle,
337                                struct smb_filename *smb_fname)
338 {
339         NTSTATUS status;
340         int result = -1;
341         char *xattr_name = NULL;
342
343         if (!is_ntfs_stream_smb_fname(smb_fname)) {
344                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
345         }
346
347         /* If the default stream is requested, just stat the base file. */
348         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
349                 return streams_xattr_stat_base(handle, smb_fname, false);
350         }
351
352         /* Populate the stat struct with info from the base file. */
353         if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
354                 return -1;
355         }
356
357         /* Derive the xattr name to lookup. */
358         status = streams_xattr_get_name(handle, talloc_tos(),
359                                         smb_fname->stream_name, &xattr_name);
360         if (!NT_STATUS_IS_OK(status)) {
361                 errno = map_errno_from_nt_status(status);
362                 return -1;
363         }
364
365         /* Augment the base file's stat information before returning. */
366         smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
367                                                   smb_fname->base_name,
368                                                   xattr_name);
369         if (smb_fname->st.st_ex_size == -1) {
370                 errno = ENOENT;
371                 result = -1;
372                 goto fail;
373         }
374
375         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
376         smb_fname->st.st_ex_mode &= ~S_IFMT;
377         smb_fname->st.st_ex_mode |= S_IFREG;
378         smb_fname->st.st_ex_blocks =
379             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
380
381         result = 0;
382
383  fail:
384         TALLOC_FREE(xattr_name);
385         return result;
386 }
387
388 static int streams_xattr_open(vfs_handle_struct *handle,
389                               struct smb_filename *smb_fname,
390                               files_struct *fsp, int flags, mode_t mode)
391 {
392         NTSTATUS status;
393         struct smb_filename *smb_fname_base = NULL;
394         struct stream_io *sio;
395         struct ea_struct ea;
396         char *xattr_name = NULL;
397         int baseflags;
398         int hostfd = -1;
399
400         DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
401                    smb_fname_str_dbg(smb_fname), flags));
402
403         if (!is_ntfs_stream_smb_fname(smb_fname)) {
404                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
405         }
406
407         /* If the default stream is requested, just open the base file. */
408         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
409                 char *tmp_stream_name;
410                 int ret;
411
412                 tmp_stream_name = smb_fname->stream_name;
413                 smb_fname->stream_name = NULL;
414
415                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
416
417                 smb_fname->stream_name = tmp_stream_name;
418
419                 return ret;
420         }
421
422         status = streams_xattr_get_name(handle, talloc_tos(),
423                                         smb_fname->stream_name, &xattr_name);
424         if (!NT_STATUS_IS_OK(status)) {
425                 errno = map_errno_from_nt_status(status);
426                 goto fail;
427         }
428
429         /* Create an smb_filename with stream_name == NULL. */
430         smb_fname_base = synthetic_smb_fname(
431                 talloc_tos(), smb_fname->base_name, NULL, NULL);
432         if (smb_fname_base == NULL) {
433                 errno = ENOMEM;
434                 goto fail;
435         }
436
437         /*
438          * We use baseflags to turn off nasty side-effects when opening the
439          * underlying file.
440          */
441         baseflags = flags;
442         baseflags &= ~O_TRUNC;
443         baseflags &= ~O_EXCL;
444         baseflags &= ~O_CREAT;
445
446         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
447                               baseflags, mode);
448
449         TALLOC_FREE(smb_fname_base);
450
451         /* It is legit to open a stream on a directory, but the base
452          * fd has to be read-only.
453          */
454         if ((hostfd == -1) && (errno == EISDIR)) {
455                 baseflags &= ~O_ACCMODE;
456                 baseflags |= O_RDONLY;
457                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
458                                       mode);
459         }
460
461         if (hostfd == -1) {
462                 goto fail;
463         }
464
465         status = get_ea_value(talloc_tos(), handle->conn, NULL,
466                               smb_fname->base_name, xattr_name, &ea);
467
468         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
469
470         if (!NT_STATUS_IS_OK(status)
471             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
472                 /*
473                  * The base file is not there. This is an error even if we got
474                  * O_CREAT, the higher levels should have created the base
475                  * file for us.
476                  */
477                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
478                            "returning ENOENT\n", smb_fname->base_name));
479                 errno = ENOENT;
480                 goto fail;
481         }
482
483         if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
484             (flags & O_TRUNC)) {
485                 /*
486                  * The attribute does not exist or needs to be truncated
487                  */
488
489                 /*
490                  * Darn, xattrs need at least 1 byte
491                  */
492                 char null = '\0';
493
494                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
495                            xattr_name, smb_fname->base_name));
496
497                 if (fsp->base_fsp->fh->fd != -1) {
498                         if (SMB_VFS_FSETXATTR(
499                                         fsp->base_fsp, xattr_name,
500                                         &null, sizeof(null),
501                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
502                                 goto fail;
503                         }
504                 } else {
505                         if (SMB_VFS_SETXATTR(
506                                         handle->conn, smb_fname->base_name,
507                                         xattr_name, &null, sizeof(null),
508                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
509                                 goto fail;
510                         }
511                 }
512         }
513
514         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
515                                                         struct stream_io,
516                                                         NULL);
517         if (sio == NULL) {
518                 errno = ENOMEM;
519                 goto fail;
520         }
521
522         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
523                                         xattr_name);
524         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
525                                   smb_fname->base_name);
526         sio->fsp_name_ptr = fsp->fsp_name;
527         sio->handle = handle;
528         sio->fsp = fsp;
529
530         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
531                 errno = ENOMEM;
532                 goto fail;
533         }
534
535         return hostfd;
536
537  fail:
538         if (hostfd >= 0) {
539                 /*
540                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
541                  * we don't have a full fsp yet
542                  */
543                 fsp->fh->fd = hostfd;
544                 SMB_VFS_CLOSE(fsp);
545         }
546
547         return -1;
548 }
549
550 static int streams_xattr_unlink(vfs_handle_struct *handle,
551                                 const struct smb_filename *smb_fname)
552 {
553         NTSTATUS status;
554         int ret = -1;
555         char *xattr_name = NULL;
556
557         if (!is_ntfs_stream_smb_fname(smb_fname)) {
558                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
559         }
560
561         /* If the default stream is requested, just open the base file. */
562         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
563                 struct smb_filename *smb_fname_base = NULL;
564
565                 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
566                 if (smb_fname_base == NULL) {
567                         errno = ENOMEM;
568                         return -1;
569                 }
570
571                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
572
573                 TALLOC_FREE(smb_fname_base);
574                 return ret;
575         }
576
577         status = streams_xattr_get_name(handle, talloc_tos(),
578                                         smb_fname->stream_name, &xattr_name);
579         if (!NT_STATUS_IS_OK(status)) {
580                 errno = map_errno_from_nt_status(status);
581                 goto fail;
582         }
583
584         ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
585
586         if ((ret == -1) && (errno == ENOATTR)) {
587                 errno = ENOENT;
588                 goto fail;
589         }
590
591         ret = 0;
592
593  fail:
594         TALLOC_FREE(xattr_name);
595         return ret;
596 }
597
598 static int streams_xattr_rename(vfs_handle_struct *handle,
599                                 const struct smb_filename *smb_fname_src,
600                                 const struct smb_filename *smb_fname_dst)
601 {
602         NTSTATUS status;
603         int ret = -1;
604         char *src_xattr_name = NULL;
605         char *dst_xattr_name = NULL;
606         bool src_is_stream, dst_is_stream;
607         ssize_t oret;
608         ssize_t nret;
609         struct ea_struct ea;
610
611         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
612         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
613
614         if (!src_is_stream && !dst_is_stream) {
615                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
616                                            smb_fname_dst);
617         }
618
619         /* For now don't allow renames from or to the default stream. */
620         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
621             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
622                 errno = ENOSYS;
623                 goto done;
624         }
625
626         /* Don't rename if the streams are identical. */
627         if (strcasecmp_m(smb_fname_src->stream_name,
628                        smb_fname_dst->stream_name) == 0) {
629                 goto done;
630         }
631
632         /* Get the xattr names. */
633         status = streams_xattr_get_name(handle, talloc_tos(),
634                                         smb_fname_src->stream_name,
635                                         &src_xattr_name);
636         if (!NT_STATUS_IS_OK(status)) {
637                 errno = map_errno_from_nt_status(status);
638                 goto fail;
639         }
640         status = streams_xattr_get_name(handle, talloc_tos(),
641                                         smb_fname_dst->stream_name,
642                                         &dst_xattr_name);
643         if (!NT_STATUS_IS_OK(status)) {
644                 errno = map_errno_from_nt_status(status);
645                 goto fail;
646         }
647
648         /* read the old stream */
649         status = get_ea_value(talloc_tos(), handle->conn, NULL,
650                               smb_fname_src->base_name, src_xattr_name, &ea);
651         if (!NT_STATUS_IS_OK(status)) {
652                 errno = ENOENT;
653                 goto fail;
654         }
655
656         /* (over)write the new stream */
657         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
658                                 dst_xattr_name, ea.value.data, ea.value.length,
659                                 0);
660         if (nret < 0) {
661                 if (errno == ENOATTR) {
662                         errno = ENOENT;
663                 }
664                 goto fail;
665         }
666
667         /* remove the old stream */
668         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
669                                    src_xattr_name);
670         if (oret < 0) {
671                 if (errno == ENOATTR) {
672                         errno = ENOENT;
673                 }
674                 goto fail;
675         }
676
677  done:
678         errno = 0;
679         ret = 0;
680  fail:
681         TALLOC_FREE(src_xattr_name);
682         TALLOC_FREE(dst_xattr_name);
683         return ret;
684 }
685
686 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle, files_struct *fsp,
687                                    const char *fname,
688                                    bool (*fn)(struct ea_struct *ea,
689                                               void *private_data),
690                                    void *private_data)
691 {
692         NTSTATUS status;
693         char **names;
694         size_t i, num_names;
695         struct streams_xattr_config *config;
696
697         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
698                                 return NT_STATUS_UNSUCCESSFUL);
699
700         status = get_ea_names_from_file(talloc_tos(), handle->conn, fsp, fname,
701                                         &names, &num_names);
702         if (!NT_STATUS_IS_OK(status)) {
703                 return status;
704         }
705
706         for (i=0; i<num_names; i++) {
707                 struct ea_struct ea;
708
709                 /*
710                  * We want to check with samba_private_attr_name()
711                  * whether the xattr name is a private one,
712                  * unfortunately it flags xattrs that begin with the
713                  * default streams prefix as private.
714                  *
715                  * By only calling samba_private_attr_name() in case
716                  * the xattr does NOT begin with the default prefix,
717                  * we know that if it returns 'true' it definitely one
718                  * of our internal xattr like "user.DOSATTRIB".
719                  */
720                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
721                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
722                         if (samba_private_attr_name(names[i])) {
723                                 continue;
724                         }
725                 }
726
727                 if (strncmp(names[i], config->prefix,
728                             config->prefix_len) != 0) {
729                         continue;
730                 }
731
732                 status = get_ea_value(names, handle->conn, fsp, fname,
733                                       names[i], &ea);
734                 if (!NT_STATUS_IS_OK(status)) {
735                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
736                                    names[i], fname, nt_errstr(status)));
737                         continue;
738                 }
739
740                 ea.name = talloc_asprintf(
741                         ea.value.data, ":%s%s",
742                         names[i] + config->prefix_len,
743                         config->store_stream_type ? "" : ":$DATA");
744                 if (ea.name == NULL) {
745                         DEBUG(0, ("talloc failed\n"));
746                         continue;
747                 }
748
749                 if (!fn(&ea, private_data)) {
750                         TALLOC_FREE(ea.value.data);
751                         return NT_STATUS_OK;
752                 }
753
754                 TALLOC_FREE(ea.value.data);
755         }
756
757         TALLOC_FREE(names);
758         return NT_STATUS_OK;
759 }
760
761 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
762                            struct stream_struct **streams,
763                            const char *name, off_t size,
764                            off_t alloc_size)
765 {
766         struct stream_struct *tmp;
767
768         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
769                                    (*num_streams)+1);
770         if (tmp == NULL) {
771                 return false;
772         }
773
774         tmp[*num_streams].name = talloc_strdup(tmp, name);
775         if (tmp[*num_streams].name == NULL) {
776                 return false;
777         }
778
779         tmp[*num_streams].size = size;
780         tmp[*num_streams].alloc_size = alloc_size;
781
782         *streams = tmp;
783         *num_streams += 1;
784         return true;
785 }
786
787 struct streaminfo_state {
788         TALLOC_CTX *mem_ctx;
789         vfs_handle_struct *handle;
790         unsigned int num_streams;
791         struct stream_struct *streams;
792         NTSTATUS status;
793 };
794
795 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
796 {
797         struct streaminfo_state *state =
798                 (struct streaminfo_state *)private_data;
799
800         if (!add_one_stream(state->mem_ctx,
801                             &state->num_streams, &state->streams,
802                             ea->name, ea->value.length-1,
803                             smb_roundup(state->handle->conn,
804                                         ea->value.length-1))) {
805                 state->status = NT_STATUS_NO_MEMORY;
806                 return false;
807         }
808
809         return true;
810 }
811
812 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
813                                          struct files_struct *fsp,
814                                          const struct smb_filename *smb_fname,
815                                          TALLOC_CTX *mem_ctx,
816                                          unsigned int *pnum_streams,
817                                          struct stream_struct **pstreams)
818 {
819         SMB_STRUCT_STAT sbuf;
820         int ret;
821         NTSTATUS status;
822         struct streaminfo_state state;
823
824         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
825                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
826         }
827         else {
828                 struct smb_filename *smb_fname_base = NULL;
829                 smb_fname_base = synthetic_smb_fname(talloc_tos(),
830                                         smb_fname->base_name,
831                                         NULL,
832                                         NULL);
833                 if (smb_fname_base == NULL) {
834                         return NT_STATUS_NO_MEMORY;
835                 }
836                 if (lp_posix_pathnames()) {
837                         ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
838                 } else {
839                         ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
840                 }
841                 sbuf = smb_fname_base->st;
842                 TALLOC_FREE(smb_fname_base);
843         }
844
845         if (ret == -1) {
846                 return map_nt_error_from_unix(errno);
847         }
848
849         state.streams = *pstreams;
850         state.num_streams = *pnum_streams;
851         state.mem_ctx = mem_ctx;
852         state.handle = handle;
853         state.status = NT_STATUS_OK;
854
855         if (S_ISLNK(sbuf.st_ex_mode)) {
856                 /*
857                  * Currently we do't have SMB_VFS_LLISTXATTR
858                  * inside the VFS which means there's no way
859                  * to cope with a symlink when lp_posix_pathnames().
860                  * returns true. For now ignore links.
861                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
862                  */
863                 status = NT_STATUS_OK;
864         } else {
865                 status = walk_xattr_streams(handle, fsp, smb_fname->base_name,
866                                     collect_one_stream, &state);
867         }
868
869         if (!NT_STATUS_IS_OK(status)) {
870                 TALLOC_FREE(state.streams);
871                 return status;
872         }
873
874         if (!NT_STATUS_IS_OK(state.status)) {
875                 TALLOC_FREE(state.streams);
876                 return state.status;
877         }
878
879         *pnum_streams = state.num_streams;
880         *pstreams = state.streams;
881
882         return SMB_VFS_NEXT_STREAMINFO(handle,
883                         fsp,
884                         smb_fname,
885                         mem_ctx,
886                         pnum_streams,
887                         pstreams);
888 }
889
890 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
891                         enum timestamp_set_resolution *p_ts_res)
892 {
893         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
894 }
895
896 static int streams_xattr_connect(vfs_handle_struct *handle,
897                                  const char *service, const char *user)
898 {
899         struct streams_xattr_config *config;
900         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
901         const char *prefix;
902         int rc;
903
904         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
905         if (rc != 0) {
906                 return rc;
907         }
908
909         config = talloc_zero(handle->conn, struct streams_xattr_config);
910         if (config == NULL) {
911                 DEBUG(1, ("talloc_zero() failed\n"));
912                 errno = ENOMEM;
913                 return -1;
914         }
915
916         prefix = lp_parm_const_string(SNUM(handle->conn),
917                                       "streams_xattr", "prefix",
918                                       default_prefix);
919         config->prefix = talloc_strdup(config, prefix);
920         if (config->prefix == NULL) {
921                 DEBUG(1, ("talloc_strdup() failed\n"));
922                 errno = ENOMEM;
923                 return -1;
924         }
925         config->prefix_len = strlen(config->prefix);
926         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
927
928         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
929                                                  "streams_xattr",
930                                                  "store_stream_type",
931                                                  true);
932
933         SMB_VFS_HANDLE_SET_DATA(handle, config,
934                                 NULL, struct stream_xattr_config,
935                                 return -1);
936
937         return 0;
938 }
939
940 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
941                                     files_struct *fsp, const void *data,
942                                     size_t n, off_t offset)
943 {
944         struct stream_io *sio =
945                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
946         struct ea_struct ea;
947         NTSTATUS status;
948         int ret;
949
950         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
951
952         if (sio == NULL) {
953                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
954         }
955
956         if (!streams_xattr_recheck(sio)) {
957                 return -1;
958         }
959
960         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
961                               sio->base, sio->xattr_name, &ea);
962         if (!NT_STATUS_IS_OK(status)) {
963                 return -1;
964         }
965
966         if ((offset + n) > ea.value.length-1) {
967                 uint8_t *tmp;
968
969                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
970                                            offset + n + 1);
971
972                 if (tmp == NULL) {
973                         TALLOC_FREE(ea.value.data);
974                         errno = ENOMEM;
975                         return -1;
976                 }
977                 ea.value.data = tmp;
978                 ea.value.length = offset + n + 1;
979                 ea.value.data[offset+n] = 0;
980         }
981
982         memcpy(ea.value.data + offset, data, n);
983
984         if (fsp->base_fsp->fh->fd != -1) {
985                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
986                                 sio->xattr_name,
987                                 ea.value.data, ea.value.length, 0);
988         } else {
989                 ret = SMB_VFS_SETXATTR(fsp->conn,
990                                        fsp->base_fsp->fsp_name->base_name,
991                                 sio->xattr_name,
992                                 ea.value.data, ea.value.length, 0);
993         }
994         TALLOC_FREE(ea.value.data);
995
996         if (ret == -1) {
997                 return -1;
998         }
999
1000         return n;
1001 }
1002
1003 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1004                                    files_struct *fsp, void *data,
1005                                    size_t n, off_t offset)
1006 {
1007         struct stream_io *sio =
1008                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1009         struct ea_struct ea;
1010         NTSTATUS status;
1011         size_t length, overlap;
1012
1013         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1014                    (int)offset, (int)n));
1015
1016         if (sio == NULL) {
1017                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1018         }
1019
1020         if (!streams_xattr_recheck(sio)) {
1021                 return -1;
1022         }
1023
1024         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1025                               sio->base, sio->xattr_name, &ea);
1026         if (!NT_STATUS_IS_OK(status)) {
1027                 return -1;
1028         }
1029
1030         length = ea.value.length-1;
1031
1032         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1033                    (int)length));
1034
1035         /* Attempt to read past EOF. */
1036         if (length <= offset) {
1037                 return 0;
1038         }
1039
1040         overlap = (offset + n) > length ? (length - offset) : n;
1041         memcpy(data, ea.value.data + offset, overlap);
1042
1043         TALLOC_FREE(ea.value.data);
1044         return overlap;
1045 }
1046
1047 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1048                                         struct files_struct *fsp,
1049                                         off_t offset)
1050 {
1051         int ret;
1052         uint8_t *tmp;
1053         struct ea_struct ea;
1054         NTSTATUS status;
1055         struct stream_io *sio =
1056                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1057
1058         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1059                    fsp_str_dbg(fsp), (double)offset));
1060
1061         if (sio == NULL) {
1062                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1063         }
1064
1065         if (!streams_xattr_recheck(sio)) {
1066                 return -1;
1067         }
1068
1069         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1070                               sio->base, sio->xattr_name, &ea);
1071         if (!NT_STATUS_IS_OK(status)) {
1072                 return -1;
1073         }
1074
1075         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1076                                    offset + 1);
1077
1078         if (tmp == NULL) {
1079                 TALLOC_FREE(ea.value.data);
1080                 errno = ENOMEM;
1081                 return -1;
1082         }
1083
1084         /* Did we expand ? */
1085         if (ea.value.length < offset + 1) {
1086                 memset(&tmp[ea.value.length], '\0',
1087                         offset + 1 - ea.value.length);
1088         }
1089
1090         ea.value.data = tmp;
1091         ea.value.length = offset + 1;
1092         ea.value.data[offset] = 0;
1093
1094         if (fsp->base_fsp->fh->fd != -1) {
1095                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1096                                 sio->xattr_name,
1097                                 ea.value.data, ea.value.length, 0);
1098         } else {
1099                 ret = SMB_VFS_SETXATTR(fsp->conn,
1100                                        fsp->base_fsp->fsp_name->base_name,
1101                                 sio->xattr_name,
1102                                 ea.value.data, ea.value.length, 0);
1103         }
1104
1105         TALLOC_FREE(ea.value.data);
1106
1107         if (ret == -1) {
1108                 return -1;
1109         }
1110
1111         return 0;
1112 }
1113
1114 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1115                                         struct files_struct *fsp,
1116                                         uint32_t mode,
1117                                         off_t offset,
1118                                         off_t len)
1119 {
1120         struct stream_io *sio =
1121                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1122
1123         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1124                 "len = %.0f\n",
1125                 fsp_str_dbg(fsp), (double)offset, (double)len));
1126
1127         if (sio == NULL) {
1128                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1129         }
1130
1131         if (!streams_xattr_recheck(sio)) {
1132                 return -1;
1133         }
1134
1135         /* Let the pwrite code path handle it. */
1136         errno = ENOSYS;
1137         return -1;
1138 }
1139
1140
1141 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1142         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1143         .connect_fn = streams_xattr_connect,
1144         .open_fn = streams_xattr_open,
1145         .stat_fn = streams_xattr_stat,
1146         .fstat_fn = streams_xattr_fstat,
1147         .lstat_fn = streams_xattr_lstat,
1148         .pread_fn = streams_xattr_pread,
1149         .pwrite_fn = streams_xattr_pwrite,
1150         .unlink_fn = streams_xattr_unlink,
1151         .rename_fn = streams_xattr_rename,
1152         .ftruncate_fn = streams_xattr_ftruncate,
1153         .fallocate_fn = streams_xattr_fallocate,
1154         .streaminfo_fn = streams_xattr_streaminfo,
1155 };
1156
1157 NTSTATUS vfs_streams_xattr_init(void);
1158 NTSTATUS vfs_streams_xattr_init(void)
1159 {
1160         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1161                                 &vfs_streams_xattr_fns);
1162 }