2 * CAP VFS module for Samba 3.x Version 0.3
4 * Copyright (C) Tim Potter, 1999-2000
5 * Copyright (C) Alexander Bokovoy, 2002-2003
6 * Copyright (C) Stefan (metze) Metzmacher, 2003
7 * Copyright (C) TAKAHASHI Motonobu (monyo), 2003
8 * Copyright (C) Jeremy Allison, 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/>.
26 #include "smbd/smbd.h"
29 static char *capencode(TALLOC_CTX *ctx, const char *from);
30 static char *capdecode(TALLOC_CTX *ctx, const char *from);
32 static uint64_t cap_disk_free(vfs_handle_struct *handle, const char *path,
33 uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
35 char *cappath = capencode(talloc_tos(), path);
41 return SMB_VFS_NEXT_DISK_FREE(handle, cappath, bsize, dfree, dsize);
44 static int cap_get_quota(vfs_handle_struct *handle, const char *path,
45 enum SMB_QUOTA_TYPE qtype, unid_t id,
48 char *cappath = capencode(talloc_tos(), path);
54 return SMB_VFS_NEXT_GET_QUOTA(handle, cappath, qtype, id, dq);
57 static DIR *cap_opendir(vfs_handle_struct *handle,
58 const struct smb_filename *smb_fname,
62 char *capname = capencode(talloc_tos(), smb_fname->base_name);
63 struct smb_filename *cap_smb_fname = NULL;
69 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
74 if (cap_smb_fname == NULL) {
79 return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
82 static struct dirent *cap_readdir(vfs_handle_struct *handle,
84 SMB_STRUCT_STAT *sbuf)
86 struct dirent *result;
87 struct dirent *newdirent;
90 DEBUG(3,("cap: cap_readdir\n"));
92 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
97 newname = capdecode(talloc_tos(), result->d_name);
101 DEBUG(3,("cap: cap_readdir: %s\n", newname));
102 newnamelen = strlen(newname)+1;
103 newdirent = talloc_size(
104 talloc_tos(), sizeof(struct dirent) + newnamelen);
108 talloc_set_name_const(newdirent, "struct dirent");
109 memcpy(newdirent, result, sizeof(struct dirent));
110 memcpy(&newdirent->d_name, newname, newnamelen);
114 static int cap_mkdir(vfs_handle_struct *handle,
115 const struct smb_filename *smb_fname,
118 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
119 struct smb_filename *cap_smb_fname = NULL;
126 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
131 if (cap_smb_fname == NULL) {
132 TALLOC_FREE(cappath);
137 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
140 static int cap_rmdir(vfs_handle_struct *handle,
141 const struct smb_filename *smb_fname)
143 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
144 struct smb_filename *cap_smb_fname = NULL;
151 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
156 if (cap_smb_fname == NULL) {
157 TALLOC_FREE(cappath);
162 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
165 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
166 files_struct *fsp, int flags, mode_t mode)
169 char *tmp_base_name = NULL;
172 cappath = capencode(talloc_tos(), smb_fname->base_name);
179 tmp_base_name = smb_fname->base_name;
180 smb_fname->base_name = cappath;
182 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
183 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
185 smb_fname->base_name = tmp_base_name;
186 TALLOC_FREE(cappath);
191 static int cap_rename(vfs_handle_struct *handle,
192 const struct smb_filename *smb_fname_src,
193 const struct smb_filename *smb_fname_dst)
197 struct smb_filename *smb_fname_src_tmp = NULL;
198 struct smb_filename *smb_fname_dst_tmp = NULL;
201 capold = capencode(talloc_tos(), smb_fname_src->base_name);
202 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
203 if (!capold || !capnew) {
208 /* Setup temporary smb_filename structs. */
209 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
210 if (smb_fname_src_tmp == NULL) {
214 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
215 if (smb_fname_dst_tmp == NULL) {
220 smb_fname_src_tmp->base_name = capold;
221 smb_fname_dst_tmp->base_name = capnew;
223 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
228 TALLOC_FREE(smb_fname_src_tmp);
229 TALLOC_FREE(smb_fname_dst_tmp);
234 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
237 char *tmp_base_name = NULL;
240 cappath = capencode(talloc_tos(), smb_fname->base_name);
247 tmp_base_name = smb_fname->base_name;
248 smb_fname->base_name = cappath;
250 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
252 smb_fname->base_name = tmp_base_name;
253 TALLOC_FREE(cappath);
258 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
261 char *tmp_base_name = NULL;
264 cappath = capencode(talloc_tos(), smb_fname->base_name);
271 tmp_base_name = smb_fname->base_name;
272 smb_fname->base_name = cappath;
274 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
276 smb_fname->base_name = tmp_base_name;
277 TALLOC_FREE(cappath);
282 static int cap_unlink(vfs_handle_struct *handle,
283 const struct smb_filename *smb_fname)
285 struct smb_filename *smb_fname_tmp = NULL;
286 char *cappath = NULL;
289 cappath = capencode(talloc_tos(), smb_fname->base_name);
295 /* Setup temporary smb_filename structs. */
296 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
297 if (smb_fname_tmp == NULL) {
302 smb_fname_tmp->base_name = cappath;
304 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
306 TALLOC_FREE(smb_fname_tmp);
310 static int cap_chmod(vfs_handle_struct *handle,
311 const struct smb_filename *smb_fname,
314 struct smb_filename *cap_smb_fname = NULL;
315 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
324 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
329 if (cap_smb_fname == NULL) {
330 TALLOC_FREE(cappath);
335 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
337 TALLOC_FREE(cappath);
338 TALLOC_FREE(cap_smb_fname);
343 static int cap_chown(vfs_handle_struct *handle,
344 const struct smb_filename *smb_fname,
348 struct smb_filename *cap_smb_fname = NULL;
349 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
358 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
363 if (cap_smb_fname == NULL) {
364 TALLOC_FREE(cappath);
369 ret = SMB_VFS_NEXT_CHOWN(handle, cap_smb_fname, uid, gid);
371 TALLOC_FREE(cappath);
372 TALLOC_FREE(cap_smb_fname);
377 static int cap_lchown(vfs_handle_struct *handle,
378 const struct smb_filename *smb_fname,
382 struct smb_filename *cap_smb_fname = NULL;
383 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
392 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
397 if (cap_smb_fname == NULL) {
398 TALLOC_FREE(cappath);
403 ret = SMB_VFS_NEXT_LCHOWN(handle, cap_smb_fname, uid, gid);
405 TALLOC_FREE(cappath);
406 TALLOC_FREE(cap_smb_fname);
411 static int cap_chdir(vfs_handle_struct *handle, const char *path)
413 char *cappath = capencode(talloc_tos(), path);
419 DEBUG(3,("cap: cap_chdir for %s\n", path));
420 return SMB_VFS_NEXT_CHDIR(handle, cappath);
423 static int cap_ntimes(vfs_handle_struct *handle,
424 const struct smb_filename *smb_fname,
425 struct smb_file_time *ft)
427 struct smb_filename *smb_fname_tmp = NULL;
428 char *cappath = NULL;
431 cappath = capencode(talloc_tos(), smb_fname->base_name);
438 /* Setup temporary smb_filename structs. */
439 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
440 if (smb_fname_tmp == NULL) {
445 smb_fname_tmp->base_name = cappath;
447 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
449 TALLOC_FREE(smb_fname_tmp);
454 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
457 char *capold = capencode(talloc_tos(), oldpath);
458 char *capnew = capencode(talloc_tos(), newpath);
460 if (!capold || !capnew) {
464 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
467 static int cap_readlink(vfs_handle_struct *handle, const char *path,
468 char *buf, size_t bufsiz)
470 char *cappath = capencode(talloc_tos(), path);
476 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
479 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
481 char *capold = capencode(talloc_tos(), oldpath);
482 char *capnew = capencode(talloc_tos(), newpath);
484 if (!capold || !capnew) {
488 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
491 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
493 char *cappath = capencode(talloc_tos(), path);
499 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
502 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
504 /* monyo need capencode'ed and capdecode'ed? */
505 char *cappath = capencode(talloc_tos(), path);
511 return SMB_VFS_NEXT_REALPATH(handle, cappath);
514 static int cap_chmod_acl(vfs_handle_struct *handle,
515 const struct smb_filename *smb_fname,
518 struct smb_filename *cap_smb_fname = NULL;
519 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
523 /* If the underlying VFS doesn't have ACL support... */
528 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
533 if (cap_smb_fname == NULL) {
534 TALLOC_FREE(cappath);
539 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
541 TALLOC_FREE(cappath);
542 TALLOC_FREE(cap_smb_fname);
547 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
548 const struct smb_filename *smb_fname,
552 struct smb_filename *cap_smb_fname = NULL;
553 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
559 return (SMB_ACL_T)NULL;
561 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
566 if (cap_smb_fname == NULL) {
567 TALLOC_FREE(cappath);
569 return (SMB_ACL_T)NULL;
571 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
576 TALLOC_FREE(cappath);
577 TALLOC_FREE(cap_smb_fname);
578 if (saved_errno != 0) {
584 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
585 const struct smb_filename *smb_fname,
586 SMB_ACL_TYPE_T acltype,
589 struct smb_filename *cap_smb_fname = NULL;
590 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
598 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
603 if (cap_smb_fname == NULL) {
604 TALLOC_FREE(cappath);
608 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
613 TALLOC_FREE(cappath);
614 TALLOC_FREE(cap_smb_fname);
615 if (saved_errno != 0) {
621 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
622 const struct smb_filename *smb_fname)
624 struct smb_filename *cap_smb_fname = NULL;
625 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
633 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
638 if (cap_smb_fname == NULL) {
639 TALLOC_FREE(cappath);
643 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
647 TALLOC_FREE(cappath);
648 TALLOC_FREE(cap_smb_fname);
655 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
657 char *cappath = capencode(talloc_tos(), path);
658 char *capname = capencode(talloc_tos(), name);
660 if (!cappath || !capname) {
664 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
667 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
669 char *cappath = capencode(talloc_tos(), path);
675 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
678 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
680 char *cappath = capencode(talloc_tos(), path);
686 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
689 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
691 char *cappath = capencode(talloc_tos(), path);
692 char *capname = capencode(talloc_tos(), name);
694 if (!cappath || !capname) {
698 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
701 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
703 char *cappath = capencode(talloc_tos(), path);
709 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
712 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
714 char *cappath = capencode(talloc_tos(), path);
715 char *capname = capencode(talloc_tos(), name);
717 if (!cappath || !capname) {
721 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
724 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
726 char *cappath = capencode(talloc_tos(), path);
732 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
735 static struct vfs_fn_pointers vfs_cap_fns = {
736 .disk_free_fn = cap_disk_free,
737 .get_quota_fn = cap_get_quota,
738 .opendir_fn = cap_opendir,
739 .readdir_fn = cap_readdir,
740 .mkdir_fn = cap_mkdir,
741 .rmdir_fn = cap_rmdir,
743 .rename_fn = cap_rename,
745 .lstat_fn = cap_lstat,
746 .unlink_fn = cap_unlink,
747 .chmod_fn = cap_chmod,
748 .chown_fn = cap_chown,
749 .lchown_fn = cap_lchown,
750 .chdir_fn = cap_chdir,
751 .ntimes_fn = cap_ntimes,
752 .symlink_fn = cap_symlink,
753 .readlink_fn = cap_readlink,
755 .mknod_fn = cap_mknod,
756 .realpath_fn = cap_realpath,
757 .chmod_acl_fn = cap_chmod_acl,
758 .sys_acl_get_file_fn = cap_sys_acl_get_file,
759 .sys_acl_set_file_fn = cap_sys_acl_set_file,
760 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
761 .getxattr_fn = cap_getxattr,
762 .fgetxattr_fn = cap_fgetxattr,
763 .listxattr_fn = cap_listxattr,
764 .removexattr_fn = cap_removexattr,
765 .fremovexattr_fn = cap_fremovexattr,
766 .setxattr_fn = cap_setxattr,
767 .fsetxattr_fn = cap_fsetxattr
770 NTSTATUS vfs_cap_init(TALLOC_CTX *);
771 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
773 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
777 /* For CAP functions */
779 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
780 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
781 #define is_hex(s) ((s)[0] == hex_tag)
783 static unsigned char hex2bin_table[256] = {
784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
787 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
788 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
789 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
790 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
791 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
792 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
793 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
794 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
795 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
796 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
797 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
798 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
800 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
801 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
803 static unsigned char bin2hex_table[256] = "0123456789abcdef";
805 /*******************************************************************
806 original code -> ":xx" - CAP format
807 ********************************************************************/
809 static char *capencode(TALLOC_CTX *ctx, const char *from)
816 for (p1 = from; *p1; p1++) {
817 if ((unsigned char)*p1 >= 0x80) {
825 to = talloc_array(ctx, char, len);
830 for (out = to; *from;) {
831 /* buffer husoku error */
832 if ((unsigned char)*from >= 0x80) {
834 *out++ = bin2hex (((*from)>>4)&0x0f);
835 *out++ = bin2hex ((*from)&0x0f);
845 /*******************************************************************
847 ********************************************************************/
848 /* ":xx" -> a byte */
850 static char *capdecode(TALLOC_CTX *ctx, const char *from)
857 for (p1 = from; *p1; len++) {
866 to = talloc_array(ctx, char, len);
871 for (out = to; *from;) {
873 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));