Simplify samba_version_string.
[Samba/gebeck_regimport.git] / source3 / modules / vfs_streams_xattr.c
blobb74c4f7902e75382e5f61b022e2b5d8e1a4c6a5c
1 /*
2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
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.
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.
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/>.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 struct stream_io {
30 char *base;
31 char *xattr_name;
34 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
36 struct MD5Context ctx;
37 unsigned char hash[16];
38 SMB_INO_T result;
39 char *upper_sname;
41 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
42 (unsigned long)sbuf->st_dev,
43 (unsigned long)sbuf->st_ino, sname));
45 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
46 SMB_ASSERT(upper_sname != NULL);
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);
57 TALLOC_FREE(upper_sname);
59 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
60 memcpy(&result, hash, sizeof(result));
62 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
64 return result;
67 static ssize_t get_xattr_size(connection_struct *conn, const char *fname,
68 const char *xattr_name)
70 NTSTATUS status;
71 struct ea_struct ea;
72 ssize_t result;
74 status = get_ea_value(talloc_tos(), conn, NULL, fname,
75 xattr_name, &ea);
77 if (!NT_STATUS_IS_OK(status)) {
78 return -1;
81 result = ea.value.length-1;
82 TALLOC_FREE(ea.value.data);
83 return result;
87 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
88 SMB_STRUCT_STAT *sbuf)
90 struct stream_io *io = (struct stream_io *)
91 VFS_FETCH_FSP_EXTENSION(handle, fsp);
93 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
95 if (io == NULL) {
96 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
99 if (SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf) == -1) {
100 return -1;
103 sbuf->st_size = get_xattr_size(handle->conn, io->base, io->xattr_name);
104 if (sbuf->st_size == -1) {
105 return -1;
108 DEBUG(10, ("sbuf->st_size = %d\n", (int)sbuf->st_size));
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;
115 return 0;
118 static int streams_xattr_stat(vfs_handle_struct *handle, const char *fname,
119 SMB_STRUCT_STAT *sbuf)
121 NTSTATUS status;
122 char *base = NULL, *sname = NULL;
123 int result = -1;
124 char *xattr_name;
126 if (!is_ntfs_stream_name(fname)) {
127 return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
130 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
131 if (!NT_STATUS_IS_OK(status)) {
132 errno = EINVAL;
133 return -1;
136 if (SMB_VFS_STAT(handle->conn, base, sbuf) == -1) {
137 goto fail;
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;
147 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
148 if (sbuf->st_size == -1) {
149 errno = ENOENT;
150 goto fail;
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;
158 result = 0;
159 fail:
160 TALLOC_FREE(base);
161 TALLOC_FREE(sname);
162 return result;
165 static int streams_xattr_lstat(vfs_handle_struct *handle, const char *fname,
166 SMB_STRUCT_STAT *sbuf)
168 NTSTATUS status;
169 char *base, *sname;
170 int result = -1;
171 char *xattr_name;
173 if (!is_ntfs_stream_name(fname)) {
174 return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
177 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
178 if (!NT_STATUS_IS_OK(status)) {
179 errno = EINVAL;
180 goto fail;
183 if (SMB_VFS_LSTAT(handle->conn, base, sbuf) == -1) {
184 goto fail;
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;
194 sbuf->st_size = get_xattr_size(handle->conn, base, xattr_name);
195 if (sbuf->st_size == -1) {
196 errno = ENOENT;
197 goto fail;
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;
205 result = 0;
206 fail:
207 TALLOC_FREE(base);
208 TALLOC_FREE(sname);
209 return result;
212 static int streams_xattr_open(vfs_handle_struct *handle, const char *fname,
213 files_struct *fsp, int flags, mode_t mode)
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;
224 DEBUG(10, ("streams_xattr_open called for %s\n", fname));
226 if (!is_ntfs_stream_name(fname)) {
227 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
230 frame = talloc_stackframe();
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;
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;
247 * We use baseflags to turn off nasty side-effects when opening the
248 * underlying file.
250 baseflags = flags;
251 baseflags &= ~O_TRUNC;
252 baseflags &= ~O_EXCL;
253 baseflags &= ~O_CREAT;
255 hostfd = SMB_VFS_OPEN(handle->conn, base, fsp, baseflags, mode);
257 /* It is legit to open a stream on a directory, but the base
258 * fd has to be read-only.
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);
267 if (hostfd == -1) {
268 goto fail;
271 status = get_ea_value(talloc_tos(), handle->conn, NULL, base,
272 xattr_name, &ea);
274 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
276 if (!NT_STATUS_IS_OK(status)
277 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
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.
283 DEBUG(10, ("streams_xattr_open: base file %s not around, "
284 "returning ENOENT\n", base));
285 errno = ENOENT;
286 goto fail;
289 if (!NT_STATUS_IS_OK(status)) {
291 * The attribute does not exist
294 if (flags & O_CREAT) {
296 * Darn, xattrs need at least 1 byte
298 char null = '\0';
300 DEBUG(10, ("creating attribute %s on file %s\n",
301 xattr_name, base));
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;
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;
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;
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);
334 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
335 errno = ENOMEM;
336 goto fail;
339 TALLOC_FREE(frame);
340 return hostfd;
342 fail:
343 if (hostfd >= 0) {
345 * BUGBUGBUG -- we would need to call fd_close_posix here, but
346 * we don't have a full fsp yet
348 SMB_VFS_CLOSE(fsp);
351 TALLOC_FREE(frame);
352 return -1;
355 static int streams_xattr_unlink(vfs_handle_struct *handle, const char *fname)
357 NTSTATUS status;
358 char *base = NULL;
359 char *sname = NULL;
360 int ret = -1;
361 char *xattr_name;
363 if (!is_ntfs_stream_name(fname)) {
364 return SMB_VFS_NEXT_UNLINK(handle, fname);
367 status = split_ntfs_stream_name(talloc_tos(), fname, &base, &sname);
368 if (!NT_STATUS_IS_OK(status)) {
369 errno = EINVAL;
370 goto fail;
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;
380 ret = SMB_VFS_REMOVEXATTR(handle->conn, base, xattr_name);
382 if ((ret == -1) && (errno == ENOATTR)) {
383 errno = ENOENT;
384 goto fail;
387 ret = 0;
389 fail:
390 TALLOC_FREE(base);
391 TALLOC_FREE(sname);
392 return ret;
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)
401 NTSTATUS status;
402 char **names;
403 size_t i, num_names;
404 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
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;
412 for (i=0; i<num_names; i++) {
413 struct ea_struct ea;
415 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
416 prefix_len) != 0) {
417 continue;
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;
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;
434 if (!fn(&ea, private_data)) {
435 TALLOC_FREE(ea.value.data);
436 return NT_STATUS_OK;
439 TALLOC_FREE(ea.value.data);
442 TALLOC_FREE(names);
443 return NT_STATUS_OK;
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)
451 struct stream_struct *tmp;
453 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
454 (*num_streams)+1);
455 if (tmp == NULL) {
456 return false;
459 tmp[*num_streams].name = talloc_strdup(tmp, name);
460 if (tmp[*num_streams].name == NULL) {
461 return false;
464 tmp[*num_streams].size = size;
465 tmp[*num_streams].alloc_size = alloc_size;
467 *streams = tmp;
468 *num_streams += 1;
469 return true;
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;
480 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
482 struct streaminfo_state *state =
483 (struct streaminfo_state *)private_data;
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;
494 return true;
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)
504 SMB_STRUCT_STAT sbuf;
505 int ret;
506 NTSTATUS status;
507 struct streaminfo_state state;
509 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
510 if (is_ntfs_stream_name(fsp->fsp_name)) {
511 return NT_STATUS_INVALID_PARAMETER;
513 ret = SMB_VFS_FSTAT(fsp, &sbuf);
515 else {
516 if (is_ntfs_stream_name(fname)) {
517 return NT_STATUS_INVALID_PARAMETER;
519 ret = SMB_VFS_STAT(handle->conn, fname, &sbuf);
522 if (ret == -1) {
523 return map_nt_error_from_unix(errno);
526 state.streams = NULL;
527 state.num_streams = 0;
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;
539 state.mem_ctx = mem_ctx;
540 state.handle = handle;
541 state.status = NT_STATUS_OK;
543 status = walk_xattr_streams(handle->conn, fsp, fname,
544 collect_one_stream, &state);
546 if (!NT_STATUS_IS_OK(status)) {
547 TALLOC_FREE(state.streams);
548 return status;
551 if (!NT_STATUS_IS_OK(state.status)) {
552 TALLOC_FREE(state.streams);
553 return state.status;
556 *pnum_streams = state.num_streams;
557 *pstreams = state.streams;
558 return NT_STATUS_OK;
561 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle)
563 return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
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)
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;
576 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
578 if (sio == NULL) {
579 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
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;
588 if ((offset + n) > ea.value.length-1) {
589 uint8 *tmp;
591 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
592 offset + n + 1);
594 if (tmp == NULL) {
595 TALLOC_FREE(ea.value.data);
596 errno = ENOMEM;
597 return -1;
599 ea.value.data = tmp;
600 ea.value.length = offset + n + 1;
601 ea.value.data[offset+n] = 0;
604 memcpy(ea.value.data + offset, data, n);
606 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->base_fsp->fsp_name,
607 sio->xattr_name,
608 ea.value.data, ea.value.length, 0);
610 TALLOC_FREE(ea.value.data);
612 if (ret == -1) {
613 return -1;
616 return n;
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)
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;
629 if (sio == NULL) {
630 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
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;
639 length = ea.value.length-1;
641 /* Attempt to read past EOF. */
642 if (length <= offset) {
643 errno = EINVAL;
644 return -1;
647 overlap = (offset + n) > length ? (length - offset) : n;
648 memcpy(data, ea.value.data + offset, overlap);
650 TALLOC_FREE(ea.value.data);
651 return overlap;
654 /* VFS operations structure */
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}
680 NTSTATUS vfs_streams_xattr_init(void);
681 NTSTATUS vfs_streams_xattr_init(void)
683 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
684 streams_xattr_ops);