For pread/pwrite we need to do the setxattr on base_fsp
[metze/samba/wip.git] / source / 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 #define XATTR_DOSSTREAM_PREFIX "user.DosStream."
30
31 struct stream_io {
32         char *base;
33         char *xattr_name;
34 };
35
36 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
37 {
38         struct MD5Context ctx;
39         unsigned char hash[16];
40         SMB_INO_T result;
41         char *upper_sname;
42
43         DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
44                    (unsigned long)sbuf->st_dev,
45                    (unsigned long)sbuf->st_ino, sname));
46
47         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
48         SMB_ASSERT(upper_sname != NULL);
49
50         MD5Init(&ctx);
51         MD5Update(&ctx, (unsigned char *)&(sbuf->st_dev),
52                   sizeof(sbuf->st_dev));
53         MD5Update(&ctx, (unsigned char *)&(sbuf->st_ino),
54                   sizeof(sbuf->st_ino));
55         MD5Update(&ctx, (unsigned char *)upper_sname,
56                   talloc_get_size(upper_sname)-1);
57         MD5Final(hash, &ctx);
58
59         TALLOC_FREE(upper_sname);
60
61         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
62         memcpy(&result, hash, sizeof(result));
63
64         DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
65
66         return result;
67 }
68
69 static ssize_t get_xattr_size(connection_struct *conn, 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, NULL, 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         if (io == NULL) {
96                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
97         }
98
99         if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
100                 return -1;
101         }
102
103         sbuf->st_size = get_xattr_size(handle->conn, io->base, io->xattr_name);
104         if (sbuf->st_size == -1) {
105                 return -1;
106         }
107
108         sbuf->st_ino = stream_inode(sbuf, io->xattr_name);
109         sbuf->st_mode &= ~S_IFMT;
110         sbuf->st_mode |= S_IFREG;
111         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
112
113         return 0;
114 }
115
116 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
117                               SMB_STRUCT_STAT *sbuf)
118 {
119         NTSTATUS status;
120         char *base, *sname;
121         int result = -1;
122         char *xattr_name;
123
124         if (!is_ntfs_stream_name(fname)) {
125                 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
126         }
127
128         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
129         if (!NT_STATUS_IS_OK(status)) {
130                 errno = EINVAL;
131                 goto fail;
132         }
133
134         if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
135                 goto fail;
136         }
137
138         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
139                                      XATTR_DOSSTREAM_PREFIX, sname);
140         if (xattr_name == NULL) {
141                 errno = ENOMEM;
142                 goto fail;
143         }
144
145         sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
146         if (sbuf->st_size == -1) {
147                 errno = ENOENT;
148                 goto fail;
149         }
150
151         sbuf->st_ino = stream_inode(sbuf, xattr_name);
152         sbuf->st_mode &= ~S_IFMT;
153         sbuf->st_mode |= S_IFREG;
154         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
155
156         result = 0;
157  fail:
158         TALLOC_FREE(base);
159         TALLOC_FREE(sname);
160         return result;
161 }
162
163 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
164                                SMB_STRUCT_STAT *sbuf)
165 {
166         NTSTATUS status;
167         char *base, *sname;
168         int result = -1;
169         char *xattr_name;
170
171         if (!is_ntfs_stream_name(fname)) {
172                 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
173         }
174
175         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
176         if (!NT_STATUS_IS_OK(status)) {
177                 errno = EINVAL;
178                 goto fail;
179         }
180
181         if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
182                 goto fail;
183         }
184
185         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
186                                      XATTR_DOSSTREAM_PREFIX, sname);
187         if (xattr_name == NULL) {
188                 errno = ENOMEM;
189                 goto fail;
190         }
191
192         sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
193         if (sbuf->st_size == -1) {
194                 errno = ENOENT;
195                 goto fail;
196         }
197
198         sbuf->st_ino = stream_inode(sbuf, xattr_name);
199         sbuf->st_mode &= ~S_IFMT;
200         sbuf->st_mode |= S_IFREG;
201         sbuf->st_blocks = sbuf->st_size % STAT_ST_BLOCKSIZE + 1;
202
203         result = 0;
204  fail:
205         TALLOC_FREE(base);
206         TALLOC_FREE(sname);
207         return result;
208 }
209
210 static int streams_xattr_open(vfs_handle_struct *handle,  const char *fname,
211                               files_struct *fsp, int flags, mode_t mode)
212 {
213         TALLOC_CTX *frame;
214         NTSTATUS status;
215         struct stream_io *sio;
216         char *base, *sname;
217         struct ea_struct ea;
218         char *xattr_name;
219         int baseflags;
220         int hostfd = -1;
221
222         if (!is_ntfs_stream_name(fname)) {
223                 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
224         }
225
226         frame = talloc_stackframe();
227
228         status = split_ntfs_stream_name(talloc_tos(), fname,
229                                         &base, &sname);
230         if (!NT_STATUS_IS_OK(status)) {
231                 errno = EINVAL;
232                 goto fail;
233         }
234
235         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
236                                      XATTR_DOSSTREAM_PREFIX, sname);
237         if (xattr_name == NULL) {
238                 errno = ENOMEM;
239                 goto fail;
240         }
241
242         /*
243          * We use baseflags to turn off nasty side-effects when opening the
244          * underlying file.
245          */
246         baseflags = flags;
247         baseflags &= ~O_TRUNC;
248         baseflags &= ~O_EXCL;
249         baseflags &= ~O_CREAT;
250
251         hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
252
253         /* It is legit to open a stream on a directory, but the base
254          * fd has to be read-only.
255          */
256         if ((hostfd == -1) && (errno == EISDIR)) {
257                 baseflags &= ~O_ACCMODE;
258                 baseflags |= O_RDONLY;
259                 hostfd = SMB_VFS_OPEN(handle->conn, fname, fsp, baseflags,
260                                       mode);
261         }
262
263         if (hostfd == -1) {
264                 goto fail;
265         }
266
267         status = get_ea_value(talloc_tos(), handle->conn, fsp, base,
268                               xattr_name, &ea);
269
270         if (!NT_STATUS_IS_OK(status)
271             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
272                 /*
273                  * The base file is not there. This is an error even if we got
274                  * O_CREAT, the higher levels should have created the base
275                  * file for us.
276                  */
277                 errno = ENOENT;
278                 goto fail;
279         }
280
281         if (!NT_STATUS_IS_OK(status)) {
282                 /*
283                  * The attribute does not exist
284                  */
285
286                 if (flags & O_CREAT) {
287                         /*
288                          * Darn, xattrs need at least 1 byte
289                          */
290                         char null = '\0';
291
292                         DEBUG(10, ("creating attribute %s on file %s\n",
293                                    xattr_name, base));
294
295                         if (SMB_VFS_SETXATTR(
296                                 handle->conn, base, xattr_name,
297                                 &null, sizeof(null),
298                                 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
299                                 goto fail;
300                         }
301                 }
302         }
303
304         if (flags & O_TRUNC) {
305                 char null = '\0';
306                 if (SMB_VFS_SETXATTR(
307                             handle->conn, base, xattr_name,
308                             &null, sizeof(null),
309                             flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
310                         goto fail;
311                 }
312         }
313
314         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
315                                                         struct stream_io);
316         if (sio == NULL) {
317                 errno = ENOMEM;
318                 goto fail;
319         }
320
321         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
322                                         xattr_name);
323         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
324                                   base);
325
326         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
327                 errno = ENOMEM;
328                 goto fail;
329         }
330
331         TALLOC_FREE(frame);
332         return hostfd;
333
334  fail:
335         if (hostfd >= 0) {
336                 /*
337                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
338                  * we don't have a full fsp yet
339                  */
340                 SMB_VFS_CLOSE(fsp, hostfd);
341         }
342
343         TALLOC_FREE(frame);
344         return -1;
345 }
346
347 static int streams_xattr_unlink(vfs_handle_struct *handle,  const char *fname)
348 {
349         NTSTATUS status;
350         char *base = NULL;
351         char *sname = NULL;
352         int ret = -1;
353         char *xattr_name;
354
355         if (!is_ntfs_stream_name(fname)) {
356                 return SMB_VFS_NEXT_UNLINK(handle, fname);
357         }
358
359         status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
360         if (!NT_STATUS_IS_OK(status)) {
361                 errno = EINVAL;
362                 goto fail;
363         }
364
365         xattr_name = talloc_asprintf(talloc_tos(), "%s%s",
366                                      XATTR_DOSSTREAM_PREFIX, sname);
367         if (xattr_name == NULL) {
368                 errno = ENOMEM;
369                 goto fail;
370         }
371
372         ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
373
374         if ((ret == -1) && (errno == ENOATTR)) {
375                 errno = ENOENT;
376                 goto fail;
377         }
378
379         ret = 0;
380
381  fail:
382         TALLOC_FREE(base);
383         TALLOC_FREE(sname);
384         return ret;
385 }
386
387 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
388                                    const char *fname,
389                                    bool (*fn)(struct ea_struct *ea,
390                                               void *private_data),
391                                    void *private_data)
392 {
393         NTSTATUS status;
394         char **names;
395         size_t i, num_names;
396         size_t prefix_len = strlen(XATTR_DOSSTREAM_PREFIX);
397
398         status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
399                                         &names, &num_names);
400         if (!NT_STATUS_IS_OK(status)) {
401                 return status;
402         }
403
404         for (i=0; i<num_names; i++) {
405                 struct ea_struct ea;
406
407                 if (strncmp(names[i], XATTR_DOSSTREAM_PREFIX,
408                             prefix_len) != 0) {
409                         continue;
410                 }
411
412                 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
413                 if (!NT_STATUS_IS_OK(status)) {
414                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
415                                    names[i], fname, nt_errstr(status)));
416                         continue;
417                 }
418
419                 ea.name = talloc_asprintf(ea.value.data, ":%s",
420                                           names[i] + prefix_len);
421                 if (ea.name == NULL) {
422                         DEBUG(0, ("talloc failed\n"));
423                         continue;
424                 }
425
426                 if (!fn(&ea, private_data)) {
427                         TALLOC_FREE(ea.value.data);
428                         return NT_STATUS_OK;
429                 }
430
431                 TALLOC_FREE(ea.value.data);
432         }
433
434         TALLOC_FREE(names);
435         return NT_STATUS_OK;
436 }
437
438 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
439                            struct stream_struct **streams,
440                            const char *name, SMB_OFF_T size,
441                            SMB_OFF_T alloc_size)
442 {
443         struct stream_struct *tmp;
444
445         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
446                                    (*num_streams)+1);
447         if (tmp == NULL) {
448                 return false;
449         }
450
451         tmp[*num_streams].name = talloc_strdup(tmp, name);
452         if (tmp[*num_streams].name == NULL) {
453                 return false;
454         }
455
456         tmp[*num_streams].size = size;
457         tmp[*num_streams].alloc_size = alloc_size;
458
459         *streams = tmp;
460         *num_streams += 1;
461         return true;
462 }
463
464 struct streaminfo_state {
465         TALLOC_CTX *mem_ctx;
466         vfs_handle_struct *handle;
467         unsigned int num_streams;
468         struct stream_struct *streams;
469         NTSTATUS status;
470 };
471
472 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
473 {
474         struct streaminfo_state *state =
475                 (struct streaminfo_state *)private_data;
476
477         if (!add_one_stream(state->mem_ctx,
478                             &state->num_streams, &state->streams,
479                             ea->name, ea->value.length-1,
480                             smb_roundup(state->handle->conn,
481                                         ea->value.length-1))) {
482                 state->status = NT_STATUS_NO_MEMORY;
483                 return false;
484         }
485
486         return true;
487 }
488
489 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
490                                          struct files_struct *fsp,
491                                          const char *fname,
492                                          TALLOC_CTX *mem_ctx,
493                                          unsigned int *pnum_streams,
494                                          struct stream_struct **pstreams)
495 {
496         SMB_STRUCT_STAT sbuf;
497         int ret;
498         NTSTATUS status;
499         struct streaminfo_state state;
500
501         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
502                 if (is_ntfs_stream_name(fsp->fsp_name)) {
503                         return NT_STATUS_INVALID_PARAMETER;
504                 }
505                 ret = SMB_VFS_FSTAT(fsp, &sbuf);
506         }
507         else {
508                 if (is_ntfs_stream_name(fname)) {
509                         return NT_STATUS_INVALID_PARAMETER;
510                 }
511                 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
512         }
513
514         if (ret == -1) {
515                 return map_nt_error_from_unix(errno);
516         }
517
518         state.streams = NULL;
519         state.num_streams = 0;
520
521         if (!S_ISDIR(sbuf.st_mode)) {
522                 if (!add_one_stream(mem_ctx,
523                                     &state.num_streams, &state.streams,
524                                     "::$DATA", sbuf.st_size,
525                                     get_allocation_size(handle->conn, fsp,
526                                                         &sbuf))) {
527                         return NT_STATUS_NO_MEMORY;
528                 }
529         }
530
531         state.mem_ctx = mem_ctx;
532         state.handle = handle;
533         state.status = NT_STATUS_OK;
534
535         status = walk_xattr_streams(handle->conn, fsp, fname,
536                                     collect_one_stream, &state);
537
538         if (!NT_STATUS_IS_OK(status)) {
539                 TALLOC_FREE(state.streams);
540                 return status;
541         }
542
543         if (!NT_STATUS_IS_OK(state.status)) {
544                 TALLOC_FREE(state.streams);
545                 return state.status;
546         }
547
548         *pnum_streams = state.num_streams;
549         *pstreams = state.streams;
550         return NT_STATUS_OK;
551 }
552
553 static int streams_xattr_statvfs(struct vfs_handle_struct *handle,
554                                  const char *path,
555                                  struct vfs_statvfs_struct *statbuf)
556 {
557         int ret;
558
559         ret = SMB_VFS_NEXT_STATVFS(handle, path, statbuf);
560         statbuf->FsCapabilities |= FILE_NAMED_STREAMS;
561         return ret;
562
563 }
564
565 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
566                                     files_struct *fsp, const void *data,
567                                     size_t n, SMB_OFF_T offset)
568 {
569         struct stream_io *sio =
570                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
571         struct ea_struct ea;
572         NTSTATUS status;
573         int ret;
574
575         if (sio == NULL) {
576                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
577         }
578
579         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
580                               sio->base, sio->xattr_name, &ea);
581         if (!NT_STATUS_IS_OK(status)) {
582                 return -1;
583         }
584
585         if ((offset + n) > ea.value.length-1) {
586                 uint8 *tmp;
587
588                 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
589                                            offset + n + 1);
590
591                 if (tmp == NULL) {
592                         TALLOC_FREE(ea.value.data);
593                         errno = ENOMEM;
594                         return -1;
595                 }
596                 ea.value.data = tmp;
597                 ea.value.length = offset + n + 1;
598                 ea.value.data[offset+n] = 0;
599         }
600
601         memcpy(ea.value.data + offset, data, n);
602
603         ret = SMB_VFS_FSETXATTR(fsp->base_fsp, sio->xattr_name,
604                                 ea.value.data, ea.value.length, 0);
605
606         TALLOC_FREE(ea.value.data);
607
608         return n;
609 }
610
611 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
612                                    files_struct *fsp, void *data,
613                                    size_t n, SMB_OFF_T offset)
614 {
615         struct stream_io *sio =
616                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
617         struct ea_struct ea;
618         NTSTATUS status;
619         size_t length, overlap;
620
621         if (sio == NULL) {
622                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
623         }
624
625         status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
626                               sio->base, sio->xattr_name, &ea);
627         if (!NT_STATUS_IS_OK(status)) {
628                 return -1;
629         }
630
631         length = ea.value.length-1;
632
633         /* Attempt to read past EOF. */
634         if (length <= offset) {
635                 errno = EINVAL;
636                 return -1;
637         }
638
639         overlap = (offset + n) > length ? (length - offset) : n;
640         memcpy(data, ea.value.data + offset, overlap);
641
642         TALLOC_FREE(ea.value.data);
643         return overlap;
644 }
645
646 /* VFS operations structure */
647
648 static vfs_op_tuple streams_xattr_ops[] = {
649         {SMB_VFS_OP(streams_xattr_statvfs), SMB_VFS_OP_STATVFS,
650          SMB_VFS_LAYER_TRANSPARENT},
651         {SMB_VFS_OP(streams_xattr_open), SMB_VFS_OP_OPEN,
652          SMB_VFS_LAYER_TRANSPARENT},
653         {SMB_VFS_OP(streams_xattr_stat), SMB_VFS_OP_STAT,
654          SMB_VFS_LAYER_TRANSPARENT},
655         {SMB_VFS_OP(streams_xattr_fstat), SMB_VFS_OP_FSTAT,
656          SMB_VFS_LAYER_TRANSPARENT},
657         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
658          SMB_VFS_LAYER_TRANSPARENT},
659         {SMB_VFS_OP(streams_xattr_pread), SMB_VFS_OP_PREAD,
660          SMB_VFS_LAYER_TRANSPARENT},
661         {SMB_VFS_OP(streams_xattr_pwrite), SMB_VFS_OP_PWRITE,
662          SMB_VFS_LAYER_TRANSPARENT},
663         {SMB_VFS_OP(streams_xattr_lstat), SMB_VFS_OP_LSTAT,
664          SMB_VFS_LAYER_TRANSPARENT},
665         {SMB_VFS_OP(streams_xattr_unlink), SMB_VFS_OP_UNLINK,
666          SMB_VFS_LAYER_TRANSPARENT},
667         {SMB_VFS_OP(streams_xattr_streaminfo), SMB_VFS_OP_STREAMINFO,
668          SMB_VFS_LAYER_OPAQUE},
669         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
670 };
671
672 NTSTATUS vfs_streams_xattr_init(void);
673 NTSTATUS vfs_streams_xattr_init(void)
674 {
675         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
676                                 streams_xattr_ops);
677 }