s3: VFS: vfs_streams_xattr.c: Make streams_xattr_open() store the same path as stream...
[amitay/samba.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(),
246                                         io->base,
247                                         NULL,
248                                         NULL,
249                                         fsp->fsp_name->flags);
250         if (smb_fname_base == NULL) {
251                 errno = ENOMEM;
252                 return -1;
253         }
254
255         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
256                 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
257         } else {
258                 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
259         }
260         *sbuf = smb_fname_base->st;
261         TALLOC_FREE(smb_fname_base);
262
263         if (ret == -1) {
264                 return -1;
265         }
266
267         sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
268                                         io->base, io->xattr_name);
269         if (sbuf->st_ex_size == -1) {
270                 return -1;
271         }
272
273         DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
274
275         sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
276         sbuf->st_ex_mode &= ~S_IFMT;
277         sbuf->st_ex_mode |= S_IFREG;
278         sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
279
280         return 0;
281 }
282
283 static int streams_xattr_stat(vfs_handle_struct *handle,
284                               struct smb_filename *smb_fname)
285 {
286         NTSTATUS status;
287         int result = -1;
288         char *xattr_name = NULL;
289
290         if (!is_ntfs_stream_smb_fname(smb_fname)) {
291                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
292         }
293
294         /* Note if lp_posix_paths() is true, we can never
295          * get here as is_ntfs_stream_smb_fname() is
296          * always false. So we never need worry about
297          * not following links here. */
298
299         /* If the default stream is requested, just stat the base file. */
300         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
301                 return streams_xattr_stat_base(handle, smb_fname, true);
302         }
303
304         /* Populate the stat struct with info from the base file. */
305         if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
306                 return -1;
307         }
308
309         /* Derive the xattr name to lookup. */
310         status = streams_xattr_get_name(handle, talloc_tos(),
311                                         smb_fname->stream_name, &xattr_name);
312         if (!NT_STATUS_IS_OK(status)) {
313                 errno = map_errno_from_nt_status(status);
314                 return -1;
315         }
316
317         /* Augment the base file's stat information before returning. */
318         smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
319                                                   smb_fname->base_name,
320                                                   xattr_name);
321         if (smb_fname->st.st_ex_size == -1) {
322                 errno = ENOENT;
323                 result = -1;
324                 goto fail;
325         }
326
327         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
328         smb_fname->st.st_ex_mode &= ~S_IFMT;
329         smb_fname->st.st_ex_mode |= S_IFREG;
330         smb_fname->st.st_ex_blocks =
331             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
332
333         result = 0;
334  fail:
335         TALLOC_FREE(xattr_name);
336         return result;
337 }
338
339 static int streams_xattr_lstat(vfs_handle_struct *handle,
340                                struct smb_filename *smb_fname)
341 {
342         NTSTATUS status;
343         int result = -1;
344         char *xattr_name = NULL;
345
346         if (!is_ntfs_stream_smb_fname(smb_fname)) {
347                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
348         }
349
350         /* If the default stream is requested, just stat the base file. */
351         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
352                 return streams_xattr_stat_base(handle, smb_fname, false);
353         }
354
355         /* Populate the stat struct with info from the base file. */
356         if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
357                 return -1;
358         }
359
360         /* Derive the xattr name to lookup. */
361         status = streams_xattr_get_name(handle, talloc_tos(),
362                                         smb_fname->stream_name, &xattr_name);
363         if (!NT_STATUS_IS_OK(status)) {
364                 errno = map_errno_from_nt_status(status);
365                 return -1;
366         }
367
368         /* Augment the base file's stat information before returning. */
369         smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
370                                                   smb_fname->base_name,
371                                                   xattr_name);
372         if (smb_fname->st.st_ex_size == -1) {
373                 errno = ENOENT;
374                 result = -1;
375                 goto fail;
376         }
377
378         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
379         smb_fname->st.st_ex_mode &= ~S_IFMT;
380         smb_fname->st.st_ex_mode |= S_IFREG;
381         smb_fname->st.st_ex_blocks =
382             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
383
384         result = 0;
385
386  fail:
387         TALLOC_FREE(xattr_name);
388         return result;
389 }
390
391 static int streams_xattr_open(vfs_handle_struct *handle,
392                               struct smb_filename *smb_fname,
393                               files_struct *fsp, int flags, mode_t mode)
394 {
395         NTSTATUS status;
396         struct smb_filename *smb_fname_base = NULL;
397         struct stream_io *sio;
398         struct ea_struct ea;
399         char *xattr_name = NULL;
400         int baseflags;
401         int hostfd = -1;
402
403         DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
404                    smb_fname_str_dbg(smb_fname), flags));
405
406         if (!is_ntfs_stream_smb_fname(smb_fname)) {
407                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
408         }
409
410         /* If the default stream is requested, just open the base file. */
411         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
412                 char *tmp_stream_name;
413                 int ret;
414
415                 tmp_stream_name = smb_fname->stream_name;
416                 smb_fname->stream_name = NULL;
417
418                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
419
420                 smb_fname->stream_name = tmp_stream_name;
421
422                 return ret;
423         }
424
425         status = streams_xattr_get_name(handle, talloc_tos(),
426                                         smb_fname->stream_name, &xattr_name);
427         if (!NT_STATUS_IS_OK(status)) {
428                 errno = map_errno_from_nt_status(status);
429                 goto fail;
430         }
431
432         /* Create an smb_filename with stream_name == NULL. */
433         smb_fname_base = synthetic_smb_fname(talloc_tos(),
434                                 smb_fname->base_name,
435                                 NULL,
436                                 NULL,
437                                 smb_fname->flags);
438         if (smb_fname_base == NULL) {
439                 errno = ENOMEM;
440                 goto fail;
441         }
442
443         /*
444          * We use baseflags to turn off nasty side-effects when opening the
445          * underlying file.
446          */
447         baseflags = flags;
448         baseflags &= ~O_TRUNC;
449         baseflags &= ~O_EXCL;
450         baseflags &= ~O_CREAT;
451
452         hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
453                               baseflags, mode);
454
455         TALLOC_FREE(smb_fname_base);
456
457         /* It is legit to open a stream on a directory, but the base
458          * fd has to be read-only.
459          */
460         if ((hostfd == -1) && (errno == EISDIR)) {
461                 baseflags &= ~O_ACCMODE;
462                 baseflags |= O_RDONLY;
463                 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
464                                       mode);
465         }
466
467         if (hostfd == -1) {
468                 goto fail;
469         }
470
471         status = get_ea_value(talloc_tos(), handle->conn, NULL,
472                               smb_fname->base_name, xattr_name, &ea);
473
474         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
475
476         if (!NT_STATUS_IS_OK(status)
477             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
478                 /*
479                  * The base file is not there. This is an error even if we got
480                  * O_CREAT, the higher levels should have created the base
481                  * file for us.
482                  */
483                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
484                            "returning ENOENT\n", smb_fname->base_name));
485                 errno = ENOENT;
486                 goto fail;
487         }
488
489         if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
490             (flags & O_TRUNC)) {
491                 /*
492                  * The attribute does not exist or needs to be truncated
493                  */
494
495                 /*
496                  * Darn, xattrs need at least 1 byte
497                  */
498                 char null = '\0';
499
500                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
501                            xattr_name, smb_fname->base_name));
502
503                 if (fsp->base_fsp->fh->fd != -1) {
504                         if (SMB_VFS_FSETXATTR(
505                                         fsp->base_fsp, xattr_name,
506                                         &null, sizeof(null),
507                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
508                                 goto fail;
509                         }
510                 } else {
511                         if (SMB_VFS_SETXATTR(
512                                         handle->conn, smb_fname->base_name,
513                                         xattr_name, &null, sizeof(null),
514                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
515                                 goto fail;
516                         }
517                 }
518         }
519
520         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
521                                                         struct stream_io,
522                                                         NULL);
523         if (sio == NULL) {
524                 errno = ENOMEM;
525                 goto fail;
526         }
527
528         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
529                                         xattr_name);
530         /*
531          * so->base needs to be a copy of fsp->fsp_name->base_name,
532          * making it identical to streams_xattr_recheck(). If the
533          * open is changing directories, fsp->fsp_name->base_name
534          * will be the full path from the share root, whilst
535          * smb_fname will be relative to the $cwd.
536          */
537         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
538                                   fsp->fsp_name->base_name);
539         sio->fsp_name_ptr = fsp->fsp_name;
540         sio->handle = handle;
541         sio->fsp = fsp;
542
543         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
544                 errno = ENOMEM;
545                 goto fail;
546         }
547
548         return hostfd;
549
550  fail:
551         if (hostfd >= 0) {
552                 /*
553                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
554                  * we don't have a full fsp yet
555                  */
556                 fsp->fh->fd = hostfd;
557                 SMB_VFS_CLOSE(fsp);
558         }
559
560         return -1;
561 }
562
563 static int streams_xattr_unlink(vfs_handle_struct *handle,
564                                 const struct smb_filename *smb_fname)
565 {
566         NTSTATUS status;
567         int ret = -1;
568         char *xattr_name = NULL;
569
570         if (!is_ntfs_stream_smb_fname(smb_fname)) {
571                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
572         }
573
574         /* If the default stream is requested, just open the base file. */
575         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
576                 struct smb_filename *smb_fname_base = NULL;
577
578                 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
579                 if (smb_fname_base == NULL) {
580                         errno = ENOMEM;
581                         return -1;
582                 }
583
584                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
585
586                 TALLOC_FREE(smb_fname_base);
587                 return ret;
588         }
589
590         status = streams_xattr_get_name(handle, talloc_tos(),
591                                         smb_fname->stream_name, &xattr_name);
592         if (!NT_STATUS_IS_OK(status)) {
593                 errno = map_errno_from_nt_status(status);
594                 goto fail;
595         }
596
597         ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
598
599         if ((ret == -1) && (errno == ENOATTR)) {
600                 errno = ENOENT;
601                 goto fail;
602         }
603
604         ret = 0;
605
606  fail:
607         TALLOC_FREE(xattr_name);
608         return ret;
609 }
610
611 static int streams_xattr_rename(vfs_handle_struct *handle,
612                                 const struct smb_filename *smb_fname_src,
613                                 const struct smb_filename *smb_fname_dst)
614 {
615         NTSTATUS status;
616         int ret = -1;
617         char *src_xattr_name = NULL;
618         char *dst_xattr_name = NULL;
619         bool src_is_stream, dst_is_stream;
620         ssize_t oret;
621         ssize_t nret;
622         struct ea_struct ea;
623
624         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
625         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
626
627         if (!src_is_stream && !dst_is_stream) {
628                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
629                                            smb_fname_dst);
630         }
631
632         /* For now don't allow renames from or to the default stream. */
633         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
634             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
635                 errno = ENOSYS;
636                 goto done;
637         }
638
639         /* Don't rename if the streams are identical. */
640         if (strcasecmp_m(smb_fname_src->stream_name,
641                        smb_fname_dst->stream_name) == 0) {
642                 goto done;
643         }
644
645         /* Get the xattr names. */
646         status = streams_xattr_get_name(handle, talloc_tos(),
647                                         smb_fname_src->stream_name,
648                                         &src_xattr_name);
649         if (!NT_STATUS_IS_OK(status)) {
650                 errno = map_errno_from_nt_status(status);
651                 goto fail;
652         }
653         status = streams_xattr_get_name(handle, talloc_tos(),
654                                         smb_fname_dst->stream_name,
655                                         &dst_xattr_name);
656         if (!NT_STATUS_IS_OK(status)) {
657                 errno = map_errno_from_nt_status(status);
658                 goto fail;
659         }
660
661         /* read the old stream */
662         status = get_ea_value(talloc_tos(), handle->conn, NULL,
663                               smb_fname_src->base_name, src_xattr_name, &ea);
664         if (!NT_STATUS_IS_OK(status)) {
665                 errno = ENOENT;
666                 goto fail;
667         }
668
669         /* (over)write the new stream */
670         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
671                                 dst_xattr_name, ea.value.data, ea.value.length,
672                                 0);
673         if (nret < 0) {
674                 if (errno == ENOATTR) {
675                         errno = ENOENT;
676                 }
677                 goto fail;
678         }
679
680         /* remove the old stream */
681         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
682                                    src_xattr_name);
683         if (oret < 0) {
684                 if (errno == ENOATTR) {
685                         errno = ENOENT;
686                 }
687                 goto fail;
688         }
689
690  done:
691         errno = 0;
692         ret = 0;
693  fail:
694         TALLOC_FREE(src_xattr_name);
695         TALLOC_FREE(dst_xattr_name);
696         return ret;
697 }
698
699 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
700                                 files_struct *fsp,
701                                 const struct smb_filename *smb_fname,
702                                 bool (*fn)(struct ea_struct *ea,
703                                         void *private_data),
704                                 void *private_data)
705 {
706         NTSTATUS status;
707         char **names;
708         size_t i, num_names;
709         struct streams_xattr_config *config;
710
711         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
712                                 return NT_STATUS_UNSUCCESSFUL);
713
714         status = get_ea_names_from_file(talloc_tos(),
715                                 handle->conn,
716                                 fsp,
717                                 smb_fname,
718                                 &names,
719                                 &num_names);
720         if (!NT_STATUS_IS_OK(status)) {
721                 return status;
722         }
723
724         for (i=0; i<num_names; i++) {
725                 struct ea_struct ea;
726
727                 /*
728                  * We want to check with samba_private_attr_name()
729                  * whether the xattr name is a private one,
730                  * unfortunately it flags xattrs that begin with the
731                  * default streams prefix as private.
732                  *
733                  * By only calling samba_private_attr_name() in case
734                  * the xattr does NOT begin with the default prefix,
735                  * we know that if it returns 'true' it definitely one
736                  * of our internal xattr like "user.DOSATTRIB".
737                  */
738                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
739                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
740                         if (samba_private_attr_name(names[i])) {
741                                 continue;
742                         }
743                 }
744
745                 if (strncmp(names[i], config->prefix,
746                             config->prefix_len) != 0) {
747                         continue;
748                 }
749
750                 status = get_ea_value(names,
751                                         handle->conn,
752                                         fsp,
753                                         smb_fname->base_name,
754                                         names[i],
755                                         &ea);
756                 if (!NT_STATUS_IS_OK(status)) {
757                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
758                                 names[i],
759                                 smb_fname->base_name,
760                                 nt_errstr(status)));
761                         continue;
762                 }
763
764                 ea.name = talloc_asprintf(
765                         ea.value.data, ":%s%s",
766                         names[i] + config->prefix_len,
767                         config->store_stream_type ? "" : ":$DATA");
768                 if (ea.name == NULL) {
769                         DEBUG(0, ("talloc failed\n"));
770                         continue;
771                 }
772
773                 if (!fn(&ea, private_data)) {
774                         TALLOC_FREE(ea.value.data);
775                         return NT_STATUS_OK;
776                 }
777
778                 TALLOC_FREE(ea.value.data);
779         }
780
781         TALLOC_FREE(names);
782         return NT_STATUS_OK;
783 }
784
785 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
786                            struct stream_struct **streams,
787                            const char *name, off_t size,
788                            off_t alloc_size)
789 {
790         struct stream_struct *tmp;
791
792         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
793                                    (*num_streams)+1);
794         if (tmp == NULL) {
795                 return false;
796         }
797
798         tmp[*num_streams].name = talloc_strdup(tmp, name);
799         if (tmp[*num_streams].name == NULL) {
800                 return false;
801         }
802
803         tmp[*num_streams].size = size;
804         tmp[*num_streams].alloc_size = alloc_size;
805
806         *streams = tmp;
807         *num_streams += 1;
808         return true;
809 }
810
811 struct streaminfo_state {
812         TALLOC_CTX *mem_ctx;
813         vfs_handle_struct *handle;
814         unsigned int num_streams;
815         struct stream_struct *streams;
816         NTSTATUS status;
817 };
818
819 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
820 {
821         struct streaminfo_state *state =
822                 (struct streaminfo_state *)private_data;
823
824         if (!add_one_stream(state->mem_ctx,
825                             &state->num_streams, &state->streams,
826                             ea->name, ea->value.length-1,
827                             smb_roundup(state->handle->conn,
828                                         ea->value.length-1))) {
829                 state->status = NT_STATUS_NO_MEMORY;
830                 return false;
831         }
832
833         return true;
834 }
835
836 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
837                                          struct files_struct *fsp,
838                                          const struct smb_filename *smb_fname,
839                                          TALLOC_CTX *mem_ctx,
840                                          unsigned int *pnum_streams,
841                                          struct stream_struct **pstreams)
842 {
843         SMB_STRUCT_STAT sbuf;
844         int ret;
845         NTSTATUS status;
846         struct streaminfo_state state;
847
848         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
849                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
850         } else {
851                 ret = vfs_stat_smb_basename(handle->conn,
852                                 smb_fname,
853                                 &sbuf);
854         }
855
856         if (ret == -1) {
857                 return map_nt_error_from_unix(errno);
858         }
859
860         state.streams = *pstreams;
861         state.num_streams = *pnum_streams;
862         state.mem_ctx = mem_ctx;
863         state.handle = handle;
864         state.status = NT_STATUS_OK;
865
866         if (S_ISLNK(sbuf.st_ex_mode)) {
867                 /*
868                  * Currently we do't have SMB_VFS_LLISTXATTR
869                  * inside the VFS which means there's no way
870                  * to cope with a symlink when lp_posix_pathnames().
871                  * returns true. For now ignore links.
872                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
873                  */
874                 status = NT_STATUS_OK;
875         } else {
876                 status = walk_xattr_streams(handle, fsp, smb_fname,
877                                     collect_one_stream, &state);
878         }
879
880         if (!NT_STATUS_IS_OK(status)) {
881                 TALLOC_FREE(state.streams);
882                 return status;
883         }
884
885         if (!NT_STATUS_IS_OK(state.status)) {
886                 TALLOC_FREE(state.streams);
887                 return state.status;
888         }
889
890         *pnum_streams = state.num_streams;
891         *pstreams = state.streams;
892
893         return SMB_VFS_NEXT_STREAMINFO(handle,
894                         fsp,
895                         smb_fname,
896                         mem_ctx,
897                         pnum_streams,
898                         pstreams);
899 }
900
901 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
902                         enum timestamp_set_resolution *p_ts_res)
903 {
904         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
905 }
906
907 static int streams_xattr_connect(vfs_handle_struct *handle,
908                                  const char *service, const char *user)
909 {
910         struct streams_xattr_config *config;
911         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
912         const char *prefix;
913         int rc;
914
915         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
916         if (rc != 0) {
917                 return rc;
918         }
919
920         config = talloc_zero(handle->conn, struct streams_xattr_config);
921         if (config == NULL) {
922                 DEBUG(1, ("talloc_zero() failed\n"));
923                 errno = ENOMEM;
924                 return -1;
925         }
926
927         prefix = lp_parm_const_string(SNUM(handle->conn),
928                                       "streams_xattr", "prefix",
929                                       default_prefix);
930         config->prefix = talloc_strdup(config, prefix);
931         if (config->prefix == NULL) {
932                 DEBUG(1, ("talloc_strdup() failed\n"));
933                 errno = ENOMEM;
934                 return -1;
935         }
936         config->prefix_len = strlen(config->prefix);
937         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
938
939         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
940                                                  "streams_xattr",
941                                                  "store_stream_type",
942                                                  true);
943
944         SMB_VFS_HANDLE_SET_DATA(handle, config,
945                                 NULL, struct stream_xattr_config,
946                                 return -1);
947
948         return 0;
949 }
950
951 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
952                                     files_struct *fsp, const void *data,
953                                     size_t n, off_t offset)
954 {
955         struct stream_io *sio =
956                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
957         struct ea_struct ea;
958         NTSTATUS status;
959         int ret;
960
961         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
962
963         if (sio == NULL) {
964                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
965         }
966
967         if (!streams_xattr_recheck(sio)) {
968                 return -1;
969         }
970
971         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
972                               sio->base, sio->xattr_name, &ea);
973         if (!NT_STATUS_IS_OK(status)) {
974                 return -1;
975         }
976
977         if ((offset + n) > ea.value.length-1) {
978                 uint8_t *tmp;
979
980                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
981                                            offset + n + 1);
982
983                 if (tmp == NULL) {
984                         TALLOC_FREE(ea.value.data);
985                         errno = ENOMEM;
986                         return -1;
987                 }
988                 ea.value.data = tmp;
989                 ea.value.length = offset + n + 1;
990                 ea.value.data[offset+n] = 0;
991         }
992
993         memcpy(ea.value.data + offset, data, n);
994
995         if (fsp->base_fsp->fh->fd != -1) {
996                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
997                                 sio->xattr_name,
998                                 ea.value.data, ea.value.length, 0);
999         } else {
1000                 ret = SMB_VFS_SETXATTR(fsp->conn,
1001                                        fsp->base_fsp->fsp_name->base_name,
1002                                 sio->xattr_name,
1003                                 ea.value.data, ea.value.length, 0);
1004         }
1005         TALLOC_FREE(ea.value.data);
1006
1007         if (ret == -1) {
1008                 return -1;
1009         }
1010
1011         return n;
1012 }
1013
1014 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1015                                    files_struct *fsp, void *data,
1016                                    size_t n, off_t offset)
1017 {
1018         struct stream_io *sio =
1019                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1020         struct ea_struct ea;
1021         NTSTATUS status;
1022         size_t length, overlap;
1023
1024         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1025                    (int)offset, (int)n));
1026
1027         if (sio == NULL) {
1028                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1029         }
1030
1031         if (!streams_xattr_recheck(sio)) {
1032                 return -1;
1033         }
1034
1035         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1036                               sio->base, sio->xattr_name, &ea);
1037         if (!NT_STATUS_IS_OK(status)) {
1038                 return -1;
1039         }
1040
1041         length = ea.value.length-1;
1042
1043         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1044                    (int)length));
1045
1046         /* Attempt to read past EOF. */
1047         if (length <= offset) {
1048                 return 0;
1049         }
1050
1051         overlap = (offset + n) > length ? (length - offset) : n;
1052         memcpy(data, ea.value.data + offset, overlap);
1053
1054         TALLOC_FREE(ea.value.data);
1055         return overlap;
1056 }
1057
1058 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1059                                         struct files_struct *fsp,
1060                                         off_t offset)
1061 {
1062         int ret;
1063         uint8_t *tmp;
1064         struct ea_struct ea;
1065         NTSTATUS status;
1066         struct stream_io *sio =
1067                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1068
1069         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1070                    fsp_str_dbg(fsp), (double)offset));
1071
1072         if (sio == NULL) {
1073                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1074         }
1075
1076         if (!streams_xattr_recheck(sio)) {
1077                 return -1;
1078         }
1079
1080         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1081                               sio->base, sio->xattr_name, &ea);
1082         if (!NT_STATUS_IS_OK(status)) {
1083                 return -1;
1084         }
1085
1086         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1087                                    offset + 1);
1088
1089         if (tmp == NULL) {
1090                 TALLOC_FREE(ea.value.data);
1091                 errno = ENOMEM;
1092                 return -1;
1093         }
1094
1095         /* Did we expand ? */
1096         if (ea.value.length < offset + 1) {
1097                 memset(&tmp[ea.value.length], '\0',
1098                         offset + 1 - ea.value.length);
1099         }
1100
1101         ea.value.data = tmp;
1102         ea.value.length = offset + 1;
1103         ea.value.data[offset] = 0;
1104
1105         if (fsp->base_fsp->fh->fd != -1) {
1106                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1107                                 sio->xattr_name,
1108                                 ea.value.data, ea.value.length, 0);
1109         } else {
1110                 ret = SMB_VFS_SETXATTR(fsp->conn,
1111                                        fsp->base_fsp->fsp_name->base_name,
1112                                 sio->xattr_name,
1113                                 ea.value.data, ea.value.length, 0);
1114         }
1115
1116         TALLOC_FREE(ea.value.data);
1117
1118         if (ret == -1) {
1119                 return -1;
1120         }
1121
1122         return 0;
1123 }
1124
1125 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1126                                         struct files_struct *fsp,
1127                                         uint32_t mode,
1128                                         off_t offset,
1129                                         off_t len)
1130 {
1131         struct stream_io *sio =
1132                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1133
1134         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1135                 "len = %.0f\n",
1136                 fsp_str_dbg(fsp), (double)offset, (double)len));
1137
1138         if (sio == NULL) {
1139                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1140         }
1141
1142         if (!streams_xattr_recheck(sio)) {
1143                 return -1;
1144         }
1145
1146         /* Let the pwrite code path handle it. */
1147         errno = ENOSYS;
1148         return -1;
1149 }
1150
1151
1152 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1153         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1154         .connect_fn = streams_xattr_connect,
1155         .open_fn = streams_xattr_open,
1156         .stat_fn = streams_xattr_stat,
1157         .fstat_fn = streams_xattr_fstat,
1158         .lstat_fn = streams_xattr_lstat,
1159         .pread_fn = streams_xattr_pread,
1160         .pwrite_fn = streams_xattr_pwrite,
1161         .unlink_fn = streams_xattr_unlink,
1162         .rename_fn = streams_xattr_rename,
1163         .ftruncate_fn = streams_xattr_ftruncate,
1164         .fallocate_fn = streams_xattr_fallocate,
1165         .streaminfo_fn = streams_xattr_streaminfo,
1166 };
1167
1168 NTSTATUS vfs_streams_xattr_init(void);
1169 NTSTATUS vfs_streams_xattr_init(void)
1170 {
1171         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1172                                 &vfs_streams_xattr_fns);
1173 }