s3:streams_xattr: add support for renaming streams
[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
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
28
29 struct stream_io {
30         char *base;
31         char *xattr_name;
32 };
33
34 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
35 {
36         struct MD5Context ctx;
37         unsigned char hash[16];
38         SMB_INO_T result;
39         char *upper_sname;
40
41         DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42                    (unsigned long)sbuf->st_dev,
43                    (unsigned long)sbuf->st_ino, sname));
44
45         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
46         SMB_ASSERT(upper_sname != NULL);
47
48         MD5Init(&ctx);
49         MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
50                   sizeof(sbuf->st_dev));
51         MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
52                   sizeof(sbuf->st_ino));
53         MD5Update(&ctx, (unsigned char *)upper_sname,
54                   talloc_get_size(upper_sname)-1);
55         MD5Final(hash, &ctx);
56
57         TALLOC_FREE(upper_sname);
58
59         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60         memcpy(&result, hash, sizeof(result));
61
62         DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
63
64         return result;
65 }
66
67 static ssize_t get_xattr_size(connection_struct *conn,
68                                 files_struct *fsp,
69                                 const char *fname,
70                                 const char *xattr_name)
71 {
72         NTSTATUS status;
73         struct ea_struct ea;
74         ssize_t result;
75
76         status = get_ea_value(talloc_tos(), conn, fsp, fname,
77                               xattr_name, &ea);
78
79         if (!NT_STATUS_IS_OK(status)) {
80                 return -1;
81         }
82
83         result = ea.value.length-1;
84         TALLOC_FREE(ea.value.data);
85         return result;
86 }
87
88
89 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
90                                SMB_STRUCT_STAT *sbuf)
91 {
92         struct stream_io *io = (struct stream_io *)
93                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
94
95         DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
96
97         if (io == NULL || fsp->base_fsp == NULL) {
98                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
99         }
100
101         if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
102                 return -1;
103         }
104
105         sbuf->st_size = get_xattr_size(handle->conn, fsp->base_fsp,
106                                         io->base, io->xattr_name);
107         if (sbuf->st_size == -1) {
108                 return -1;
109         }
110
111         DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
112
113         sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
114         sbuf->st_mode &= ~S_IFMT;
115         sbuf->st_mode |= S_IFREG;
116         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
117
118         return 0;
119 }
120
121 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
122                               SMB_STRUCT_STAT *sbuf)
123 {
124         NTSTATUS status;
125         char *base = NULL, *sname = NULL;
126         int result = -1;
127         char *xattr_name;
128
129         if (!is_ntfs_stream_name(fname)) {
130                 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
131         }
132
133         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
134         if (!NT_STATUS_IS_OK(status)) {
135                 errno = EINVAL;
136                 return -1;
137         }
138
139         if (sname == NULL){
140                 return SMB_VFS_NEXT_STAT(handle, base, sbuf);
141         }
142
143         if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
144                 goto fail;
145         }
146
147         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
148                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
149         if (xattr_name == NULL) {
150                 errno = ENOMEM;
151                 goto fail;
152         }
153
154         sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
155         if (sbuf->st_size == -1) {
156                 errno = ENOENT;
157                 goto fail;
158         }
159
160         sbuf->st_ino = stream_inode(sbuf, xattr_name);
161         sbuf->st_mode &= ~S_IFMT;
162         sbuf->st_mode |= S_IFREG;
163         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
164
165         result = 0;
166  fail:
167         TALLOC_FREE(base);
168         TALLOC_FREE(sname);
169         return result;
170 }
171
172 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
173                                SMB_STRUCT_STAT *sbuf)
174 {
175         NTSTATUS status;
176         char *base, *sname;
177         int result = -1;
178         char *xattr_name;
179
180         if (!is_ntfs_stream_name(fname)) {
181                 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
182         }
183
184         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
185         if (!NT_STATUS_IS_OK(status)) {
186                 errno = EINVAL;
187                 goto fail;
188         }
189
190         if (sname == NULL){
191                 return SMB_VFS_NEXT_LSTAT(handle, base, sbuf);
192         }
193
194         if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
195                 goto fail;
196         }
197
198         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
199                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
200         if (xattr_name == NULL) {
201                 errno = ENOMEM;
202                 goto fail;
203         }
204
205         sbuf->st_size = get_xattr_size(handle->conn, NULL, base, xattr_name);
206         if (sbuf->st_size == -1) {
207                 errno = ENOENT;
208                 goto fail;
209         }
210
211         sbuf->st_ino = stream_inode(sbuf, xattr_name);
212         sbuf->st_mode &= ~S_IFMT;
213         sbuf->st_mode |= S_IFREG;
214         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
215
216         result = 0;
217  fail:
218         TALLOC_FREE(base);
219         TALLOC_FREE(sname);
220         return result;
221 }
222
223 static int streams_xattr_open(vfs_handle_struct *handle,  const char *fname,
224                               files_struct *fsp, int flags, mode_t mode)
225 {
226         TALLOC_CTX *frame;
227         NTSTATUS status;
228         struct stream_io *sio;
229         char *base, *sname;
230         struct ea_struct ea;
231         char *xattr_name;
232         int baseflags;
233         int hostfd = -1;
234
235         DEBUG(10, ("streams_xattr_open called for %s\n", fname));
236
237         if (!is_ntfs_stream_name(fname)) {
238                 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
239         }
240
241         frame = talloc_stackframe();
242
243         status = split_ntfs_stream_name(talloc_tos(), fname,
244                                         &base, &sname);
245         if (!NT_STATUS_IS_OK(status)) {
246                 errno = EINVAL;
247                 goto fail;
248         }
249
250         if (sname == NULL) {
251                 hostfd = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
252                 talloc_free(frame);
253                 return hostfd;
254         }
255
256         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
257                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
258         if (xattr_name == NULL) {
259                 errno = ENOMEM;
260                 goto fail;
261         }
262
263         /*
264          * We use baseflags to turn off nasty side-effects when opening the
265          * underlying file.
266          */
267         baseflags = flags;
268         baseflags &= ~O_TRUNC;
269         baseflags &= ~O_EXCL;
270         baseflags &= ~O_CREAT;
271
272         hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
273
274         /* It is legit to open a stream on a directory, but the base
275          * fd has to be read-only.
276          */
277         if ((hostfd == -1) && (errno == EISDIR)) {
278                 baseflags &= ~O_ACCMODE;
279                 baseflags |= O_RDONLY;
280                 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
281                                       mode);
282         }
283
284         if (hostfd == -1) {
285                 goto fail;
286         }
287
288         status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
289                               xattr_name, &ea);
290
291         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
292
293         if (!NT_STATUS_IS_OK(status)
294             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
295                 /*
296                  * The base file is not there. This is an error even if we got
297                  * O_CREAT, the higher levels should have created the base
298                  * file for us.
299                  */
300                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
301                            "returning ENOENT\n", base));
302                 errno = ENOENT;
303                 goto fail;
304         }
305
306         if (!NT_STATUS_IS_OK(status)) {
307                 /*
308                  * The attribute does not exist
309                  */
310
311                 if (flags & O_CREAT) {
312                         /*
313                          * Darn, xattrs need at least 1 byte
314                          */
315                         char null = '\0';
316
317                         DEBUG(10, ("creating attribute %s on file %s\n",
318                                    xattr_name, base));
319
320                         if (fsp->base_fsp->fh->fd != -1) {
321                                 if (SMB_VFS_FSETXATTR(
322                                         fsp->base_fsp, xattr_name,
323                                         &null, sizeof(null),
324                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
325                                         goto fail;
326                                 }
327                         } else {
328                                 if (SMB_VFS_SETXATTR(
329                                         handle->conn, base, xattr_name,
330                                         &null, sizeof(null),
331                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
332                                         goto fail;
333                                 }
334                         }
335                 }
336         }
337
338         if (flags & O_TRUNC) {
339                 char null = '\0';
340                 if (fsp->base_fsp->fh->fd != -1) {
341                         if (SMB_VFS_FSETXATTR(
342                                         fsp->base_fsp, xattr_name,
343                                         &null, sizeof(null),
344                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
345                                 goto fail;
346                         }
347                 } else {
348                         if (SMB_VFS_SETXATTR(
349                                         handle->conn, base, xattr_name,
350                                         &null, sizeof(null),
351                                         flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
352                                 goto fail;
353                         }
354                 }
355         }
356
357         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
358                                                         struct stream_io);
359         if (sio == NULL) {
360                 errno = ENOMEM;
361                 goto fail;
362         }
363
364         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
365                                         xattr_name);
366         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
367                                   base);
368
369         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
370                 errno = ENOMEM;
371                 goto fail;
372         }
373
374         TALLOC_FREE(frame);
375         return hostfd;
376
377  fail:
378         if (hostfd >= 0) {
379                 /*
380                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
381                  * we don't have a full fsp yet
382                  */
383                 SMB_VFS_CLOSE(fsp);
384         }
385
386         TALLOC_FREE(frame);
387         return -1;
388 }
389
390 static int streams_xattr_unlink(vfs_handle_struct *handle,  const char *fname)
391 {
392         NTSTATUS status;
393         char *base = NULL;
394         char *sname = NULL;
395         int ret = -1;
396         char *xattr_name;
397
398         if (!is_ntfs_stream_name(fname)) {
399                 return SMB_VFS_NEXT_UNLINK(handle, fname);
400         }
401
402         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
403         if (!NT_STATUS_IS_OK(status)) {
404                 errno = EINVAL;
405                 goto fail;
406         }
407
408         if (sname == NULL){
409                 return SMB_VFS_NEXT_UNLINK(handle, base);
410         }
411
412         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
413                                      SAMBA_XATTR_DOSSTREAM_PREFIX, sname);
414         if (xattr_name == NULL) {
415                 errno = ENOMEM;
416                 goto fail;
417         }
418
419         ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
420
421         if ((ret == -1) && (errno == ENOATTR)) {
422                 errno = ENOENT;
423                 goto fail;
424         }
425
426         ret = 0;
427
428  fail:
429         TALLOC_FREE(base);
430         TALLOC_FREE(sname);
431         return ret;
432 }
433
434 static int streams_xattr_rename(vfs_handle_struct *handle,
435                                 const char *oldname,
436                                 const char *newname)
437 {
438         NTSTATUS status;
439         TALLOC_CTX *frame = NULL;
440         char *obase;
441         char *ostream;
442         char *nbase;
443         char *nstream;
444         const char *base;
445         int ret = -1;
446         char *oxattr_name;
447         char *nxattr_name;
448         bool o_is_stream;
449         bool n_is_stream;
450         ssize_t oret;
451         ssize_t nret;
452         struct ea_struct ea;
453
454         o_is_stream = is_ntfs_stream_name(oldname);
455         n_is_stream = is_ntfs_stream_name(newname);
456
457         if (!o_is_stream && !n_is_stream) {
458                 return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
459         }
460
461         if (!(o_is_stream && n_is_stream)) {
462                 errno = ENOSYS;
463                 goto fail;
464         }
465
466         frame = talloc_stackframe();
467         if (!frame) {
468                 goto fail;
469         }
470
471         status = split_ntfs_stream_name(talloc_tos(), oldname, &obase, &ostream);
472         if (!NT_STATUS_IS_OK(status)) {
473                 errno = EINVAL;
474                 goto fail;
475         }
476
477         status = split_ntfs_stream_name(talloc_tos(), newname, &nbase, &nstream);
478         if (!NT_STATUS_IS_OK(status)) {
479                 errno = EINVAL;
480                 goto fail;
481         }
482
483         /*TODO: maybe call SMB_VFS_NEXT_RENAME() both streams are NULL (::$DATA) */
484         if (ostream == NULL) {
485                 errno = ENOSYS;
486                 goto fail;
487         }
488
489         if (nstream == NULL) {
490                 errno = ENOSYS;
491                 goto fail;
492         }
493
494         /* the new base should be empty */
495         if (StrCaseCmp(obase, nbase) != 0) {
496                 errno = ENOSYS;
497                 goto fail;
498         }
499
500         if (StrCaseCmp(ostream, nstream) == 0) {
501                 goto done;
502         }
503
504         base = obase;
505
506         oxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
507                                       SAMBA_XATTR_DOSSTREAM_PREFIX, ostream);
508         if (oxattr_name == NULL) {
509                 errno = ENOMEM;
510                 goto fail;
511         }
512
513         nxattr_name = talloc_asprintf(talloc_tos(), "%s%s",
514                                       SAMBA_XATTR_DOSSTREAM_PREFIX, nstream);
515         if (nxattr_name == NULL) {
516                 errno = ENOMEM;
517                 goto fail;
518         }
519
520         /* read the old stream */
521         status = get_ea_value(talloc_tos(), handle->conn, NULL,
522                               base, oxattr_name, &ea);
523         if (!NT_STATUS_IS_OK(status)) {
524                 errno = ENOENT;
525                 goto fail;
526         }
527
528         /* (over)write the new stream */
529         nret = SMB_VFS_SETXATTR(handle->conn, base, nxattr_name,
530                                 ea.value.data, ea.value.length, 0);
531         if (nret < 0) {
532                 if (errno == ENOATTR) {
533                         errno = ENOENT;
534                 }
535                 goto fail;
536         }
537
538         /* remove the old stream */
539         oret = SMB_VFS_REMOVEXATTR(handle->conn, base, oxattr_name);
540         if (oret < 0) {
541                 if (errno == ENOATTR) {
542                         errno = ENOENT;
543                 }
544                 goto fail;
545         }
546
547  done:
548         errno = 0;
549         ret = 0;
550  fail:
551         TALLOC_FREE(frame);
552         return ret;
553 }
554
555 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
556                                    const char *fname,
557                                    bool (*fn)(struct ea_struct *ea,
558                                               void *private_data),
559                                    void *private_data)
560 {
561         NTSTATUS status;
562         char **names;
563         size_t i, num_names;
564         size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
565
566         status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
567                                         &names, &num_names);
568         if (!NT_STATUS_IS_OK(status)) {
569                 return status;
570         }
571
572         for (i=0; i<num_names; i++) {
573                 struct ea_struct ea;
574
575                 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
576                             prefix_len) != 0) {
577                         continue;
578                 }
579
580                 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
581                 if (!NT_STATUS_IS_OK(status)) {
582                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
583                                    names[i], fname, nt_errstr(status)));
584                         continue;
585                 }
586
587                 ea.name = talloc_asprintf(ea.value.data, ":%s",
588                                           names[i] + prefix_len);
589                 if (ea.name == NULL) {
590                         DEBUG(0, ("talloc failed\n"));
591                         continue;
592                 }
593
594                 if (!fn(&ea, private_data)) {
595                         TALLOC_FREE(ea.value.data);
596                         return NT_STATUS_OK;
597                 }
598
599                 TALLOC_FREE(ea.value.data);
600         }
601
602         TALLOC_FREE(names);
603         return NT_STATUS_OK;
604 }
605
606 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
607                            struct stream_struct **streams,
608                            const char *name, SMB_OFF_T size,
609                            SMB_OFF_T alloc_size)
610 {
611         struct stream_struct *tmp;
612
613         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
614                                    (*num_streams)+1);
615         if (tmp == NULL) {
616                 return false;
617         }
618
619         tmp[*num_streams].name = talloc_strdup(tmp, name);
620         if (tmp[*num_streams].name == NULL) {
621                 return false;
622         }
623
624         tmp[*num_streams].size = size;
625         tmp[*num_streams].alloc_size = alloc_size;
626
627         *streams = tmp;
628         *num_streams += 1;
629         return true;
630 }
631
632 struct streaminfo_state {
633         TALLOC_CTX *mem_ctx;
634         vfs_handle_struct *handle;
635         unsigned int num_streams;
636         struct stream_struct *streams;
637         NTSTATUS status;
638 };
639
640 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
641 {
642         struct streaminfo_state *state =
643                 (struct streaminfo_state *)private_data;
644
645         if (!add_one_stream(state->mem_ctx,
646                             &state->num_streams, &state->streams,
647                             ea->name, ea->value.length-1,
648                             smb_roundup(state->handle->conn,
649                                         ea->value.length-1))) {
650                 state->status = NT_STATUS_NO_MEMORY;
651                 return false;
652         }
653
654         return true;
655 }
656
657 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
658                                          struct files_struct *fsp,
659                                          const char *fname,
660                                          TALLOC_CTX *mem_ctx,
661                                          unsigned int *pnum_streams,
662                                          struct stream_struct **pstreams)
663 {
664         SMB_STRUCT_STAT sbuf;
665         int ret;
666         NTSTATUS status;
667         struct streaminfo_state state;
668
669         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
670                 if (is_ntfs_stream_name(fsp->fsp_name)) {
671                         return NT_STATUS_INVALID_PARAMETER;
672                 }
673                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
674         }
675         else {
676                 if (is_ntfs_stream_name(fname)) {
677                         return NT_STATUS_INVALID_PARAMETER;
678                 }
679                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
680         }
681
682         if (ret == -1) {
683                 return map_nt_error_from_unix(errno);
684         }
685
686         state.streams = NULL;
687         state.num_streams = 0;
688
689         if (!S_ISDIR(sbuf.st_mode)) {
690                 if (!add_one_stream(mem_ctx,
691                                     &state.num_streams, &state.streams,
692                                     "::$DATA", sbuf.st_size,
693                                     get_allocation_size(handle->conn, fsp,
694                                                         &sbuf))) {
695                         return NT_STATUS_NO_MEMORY;
696                 }
697         }
698
699         state.mem_ctx = mem_ctx;
700         state.handle = handle;
701         state.status = NT_STATUS_OK;
702
703         status = walk_xattr_streams(handle->conn, fsp, fname,
704                                     collect_one_stream, &state);
705
706         if (!NT_STATUS_IS_OK(status)) {
707                 TALLOC_FREE(state.streams);
708                 return status;
709         }
710
711         if (!NT_STATUS_IS_OK(state.status)) {
712                 TALLOC_FREE(state.streams);
713                 return state.status;
714         }
715
716         *pnum_streams = state.num_streams;
717         *pstreams = state.streams;
718         return NT_STATUS_OK;
719 }
720
721 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
722 {
723         return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
724 }
725
726 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
727                                     files_struct *fsp, const void *data,
728                                     size_t n, SMB_OFF_T offset)
729 {
730         struct stream_io *sio =
731                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
732         struct ea_struct ea;
733         NTSTATUS status;
734         int ret;
735
736         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
737
738         if (sio == NULL) {
739                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
740         }
741
742         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
743                               sio->base, sio->xattr_name, &ea);
744         if (!NT_STATUS_IS_OK(status)) {
745                 return -1;
746         }
747
748         if ((offset + n) > ea.value.length-1) {
749                 uint8 *tmp;
750
751                 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
752                                            offset + n + 1);
753
754                 if (tmp == NULL) {
755                         TALLOC_FREE(ea.value.data);
756                         errno = ENOMEM;
757                         return -1;
758                 }
759                 ea.value.data = tmp;
760                 ea.value.length = offset + n + 1;
761                 ea.value.data[offset+n] = 0;
762         }
763
764         memcpy(ea.value.data + offset, data, n);
765
766         if (fsp->base_fsp->fh->fd != -1) {
767                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
768                                 sio->xattr_name,
769                                 ea.value.data, ea.value.length, 0);
770         } else {
771                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
772                                 sio->xattr_name,
773                                 ea.value.data, ea.value.length, 0);
774         }
775         TALLOC_FREE(ea.value.data);
776
777         if (ret == -1) {
778                 return -1;
779         }
780
781         return n;
782 }
783
784 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
785                                    files_struct *fsp, void *data,
786                                    size_t n, SMB_OFF_T offset)
787 {
788         struct stream_io *sio =
789                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
790         struct ea_struct ea;
791         NTSTATUS status;
792         size_t length, overlap;
793
794         if (sio == NULL) {
795                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
796         }
797
798         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
799                               sio->base, sio->xattr_name, &ea);
800         if (!NT_STATUS_IS_OK(status)) {
801                 return -1;
802         }
803
804         length = ea.value.length-1;
805
806         /* Attempt to read past EOF. */
807         if (length <= offset) {
808                 errno = EINVAL;
809                 return -1;
810         }
811
812         overlap = (offset + n) > length ? (length - offset) : n;
813         memcpy(data, ea.value.data + offset, overlap);
814
815         TALLOC_FREE(ea.value.data);
816         return overlap;
817 }
818
819 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
820                                         struct files_struct *fsp,
821                                         SMB_OFF_T offset)
822 {
823         int ret;
824         uint8 *tmp;
825         struct ea_struct ea;
826         NTSTATUS status;
827         struct stream_io *sio =
828                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
829
830         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
831                 fsp->fsp_name,
832                 (double)offset ));
833
834         if (sio == NULL) {
835                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
836         }
837
838         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
839                               sio->base, sio->xattr_name, &ea);
840         if (!NT_STATUS_IS_OK(status)) {
841                 return -1;
842         }
843
844         tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
845                                    offset + 1);
846
847         if (tmp == NULL) {
848                 TALLOC_FREE(ea.value.data);
849                 errno = ENOMEM;
850                 return -1;
851         }
852
853         /* Did we expand ? */
854         if (ea.value.length < offset + 1) {
855                 memset(&tmp[ea.value.length], '\0',
856                         offset + 1 - ea.value.length);
857         }
858
859         ea.value.data = tmp;
860         ea.value.length = offset + 1;
861         ea.value.data[offset] = 0;
862
863         if (fsp->base_fsp->fh->fd != -1) {
864                 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
865                                 sio->xattr_name,
866                                 ea.value.data, ea.value.length, 0);
867         } else {
868                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
869                                 sio->xattr_name,
870                                 ea.value.data, ea.value.length, 0);
871         }
872
873         TALLOC_FREE(ea.value.data);
874
875         if (ret == -1) {
876                 return -1;
877         }
878
879         return 0;
880 }
881
882 /* VFS operations structure */
883
884 static vfs_op_tuple streams_xattr_ops[] = {
885         {SMB_VFS_OP(streams_xattr_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
886          SMB_VFS_LAYER_TRANSPARENT},
887         {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
888          SMB_VFS_LAYER_TRANSPARENT},
889         {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
890          SMB_VFS_LAYER_TRANSPARENT},
891         {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
892          SMB_VFS_LAYER_TRANSPARENT},
893         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
894          SMB_VFS_LAYER_TRANSPARENT},
895         {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
896          SMB_VFS_LAYER_TRANSPARENT},
897         {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
898          SMB_VFS_LAYER_TRANSPARENT},
899         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
900          SMB_VFS_LAYER_TRANSPARENT},
901         {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
902          SMB_VFS_LAYER_TRANSPARENT},
903         {SMB_VFS_OP(streams_xattr_rename), SMB_VFS_OP_RENAME,
904          SMB_VFS_LAYER_TRANSPARENT},
905         {SMB_VFS_OP(streams_xattr_ftruncate),  SMB_VFS_OP_FTRUNCATE,
906          SMB_VFS_LAYER_TRANSPARENT},
907         {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
908          SMB_VFS_LAYER_OPAQUE},
909         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
910 };
911
912 NTSTATUS vfs_streams_xattr_init(void);
913 NTSTATUS vfs_streams_xattr_init(void)
914 {
915         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
916                                 streams_xattr_ops);
917 }