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