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