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