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,
33 const struct smb_filename *smb_fname,
38 char *capname = capencode(talloc_tos(), smb_fname->base_name);
39 struct smb_filename *cap_smb_fname = NULL;
45 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
50 if (cap_smb_fname == NULL) {
55 return SMB_VFS_NEXT_DISK_FREE(handle, cap_smb_fname,
59 static int cap_get_quota(vfs_handle_struct *handle, const char *path,
60 enum SMB_QUOTA_TYPE qtype, unid_t id,
63 char *cappath = capencode(talloc_tos(), path);
69 return SMB_VFS_NEXT_GET_QUOTA(handle, cappath, qtype, id, dq);
72 static DIR *cap_opendir(vfs_handle_struct *handle,
73 const struct smb_filename *smb_fname,
77 char *capname = capencode(talloc_tos(), smb_fname->base_name);
78 struct smb_filename *cap_smb_fname = NULL;
84 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
89 if (cap_smb_fname == NULL) {
94 return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
97 static struct dirent *cap_readdir(vfs_handle_struct *handle,
99 SMB_STRUCT_STAT *sbuf)
101 struct dirent *result;
102 struct dirent *newdirent;
105 DEBUG(3,("cap: cap_readdir\n"));
107 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
112 newname = capdecode(talloc_tos(), result->d_name);
116 DEBUG(3,("cap: cap_readdir: %s\n", newname));
117 newnamelen = strlen(newname)+1;
118 newdirent = talloc_size(
119 talloc_tos(), sizeof(struct dirent) + newnamelen);
123 talloc_set_name_const(newdirent, "struct dirent");
124 memcpy(newdirent, result, sizeof(struct dirent));
125 memcpy(&newdirent->d_name, newname, newnamelen);
129 static int cap_mkdir(vfs_handle_struct *handle,
130 const struct smb_filename *smb_fname,
133 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
134 struct smb_filename *cap_smb_fname = NULL;
141 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
146 if (cap_smb_fname == NULL) {
147 TALLOC_FREE(cappath);
152 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
155 static int cap_rmdir(vfs_handle_struct *handle,
156 const struct smb_filename *smb_fname)
158 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
159 struct smb_filename *cap_smb_fname = NULL;
166 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
171 if (cap_smb_fname == NULL) {
172 TALLOC_FREE(cappath);
177 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
180 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
181 files_struct *fsp, int flags, mode_t mode)
184 char *tmp_base_name = NULL;
187 cappath = capencode(talloc_tos(), smb_fname->base_name);
194 tmp_base_name = smb_fname->base_name;
195 smb_fname->base_name = cappath;
197 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
198 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
200 smb_fname->base_name = tmp_base_name;
201 TALLOC_FREE(cappath);
206 static int cap_rename(vfs_handle_struct *handle,
207 const struct smb_filename *smb_fname_src,
208 const struct smb_filename *smb_fname_dst)
212 struct smb_filename *smb_fname_src_tmp = NULL;
213 struct smb_filename *smb_fname_dst_tmp = NULL;
216 capold = capencode(talloc_tos(), smb_fname_src->base_name);
217 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
218 if (!capold || !capnew) {
223 /* Setup temporary smb_filename structs. */
224 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
225 if (smb_fname_src_tmp == NULL) {
229 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
230 if (smb_fname_dst_tmp == NULL) {
235 smb_fname_src_tmp->base_name = capold;
236 smb_fname_dst_tmp->base_name = capnew;
238 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
243 TALLOC_FREE(smb_fname_src_tmp);
244 TALLOC_FREE(smb_fname_dst_tmp);
249 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
252 char *tmp_base_name = NULL;
255 cappath = capencode(talloc_tos(), smb_fname->base_name);
262 tmp_base_name = smb_fname->base_name;
263 smb_fname->base_name = cappath;
265 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
267 smb_fname->base_name = tmp_base_name;
268 TALLOC_FREE(cappath);
273 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
276 char *tmp_base_name = NULL;
279 cappath = capencode(talloc_tos(), smb_fname->base_name);
286 tmp_base_name = smb_fname->base_name;
287 smb_fname->base_name = cappath;
289 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
291 smb_fname->base_name = tmp_base_name;
292 TALLOC_FREE(cappath);
297 static int cap_unlink(vfs_handle_struct *handle,
298 const struct smb_filename *smb_fname)
300 struct smb_filename *smb_fname_tmp = NULL;
301 char *cappath = NULL;
304 cappath = capencode(talloc_tos(), smb_fname->base_name);
310 /* Setup temporary smb_filename structs. */
311 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
312 if (smb_fname_tmp == NULL) {
317 smb_fname_tmp->base_name = cappath;
319 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
321 TALLOC_FREE(smb_fname_tmp);
325 static int cap_chmod(vfs_handle_struct *handle,
326 const struct smb_filename *smb_fname,
329 struct smb_filename *cap_smb_fname = NULL;
330 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
339 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
344 if (cap_smb_fname == NULL) {
345 TALLOC_FREE(cappath);
350 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
352 TALLOC_FREE(cappath);
353 TALLOC_FREE(cap_smb_fname);
358 static int cap_chown(vfs_handle_struct *handle,
359 const struct smb_filename *smb_fname,
363 struct smb_filename *cap_smb_fname = NULL;
364 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
373 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
378 if (cap_smb_fname == NULL) {
379 TALLOC_FREE(cappath);
384 ret = SMB_VFS_NEXT_CHOWN(handle, cap_smb_fname, uid, gid);
386 TALLOC_FREE(cappath);
387 TALLOC_FREE(cap_smb_fname);
392 static int cap_lchown(vfs_handle_struct *handle,
393 const struct smb_filename *smb_fname,
397 struct smb_filename *cap_smb_fname = NULL;
398 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
407 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
412 if (cap_smb_fname == NULL) {
413 TALLOC_FREE(cappath);
418 ret = SMB_VFS_NEXT_LCHOWN(handle, cap_smb_fname, uid, gid);
420 TALLOC_FREE(cappath);
421 TALLOC_FREE(cap_smb_fname);
426 static int cap_chdir(vfs_handle_struct *handle, const char *path)
428 char *cappath = capencode(talloc_tos(), path);
434 DEBUG(3,("cap: cap_chdir for %s\n", path));
435 return SMB_VFS_NEXT_CHDIR(handle, cappath);
438 static int cap_ntimes(vfs_handle_struct *handle,
439 const struct smb_filename *smb_fname,
440 struct smb_file_time *ft)
442 struct smb_filename *smb_fname_tmp = NULL;
443 char *cappath = NULL;
446 cappath = capencode(talloc_tos(), smb_fname->base_name);
453 /* Setup temporary smb_filename structs. */
454 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
455 if (smb_fname_tmp == NULL) {
460 smb_fname_tmp->base_name = cappath;
462 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
464 TALLOC_FREE(smb_fname_tmp);
469 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
472 char *capold = capencode(talloc_tos(), oldpath);
473 char *capnew = capencode(talloc_tos(), newpath);
475 if (!capold || !capnew) {
479 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
482 static int cap_readlink(vfs_handle_struct *handle, const char *path,
483 char *buf, size_t bufsiz)
485 char *cappath = capencode(talloc_tos(), path);
491 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
494 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
496 char *capold = capencode(talloc_tos(), oldpath);
497 char *capnew = capencode(talloc_tos(), newpath);
499 if (!capold || !capnew) {
503 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
506 static int cap_mknod(vfs_handle_struct *handle,
507 const struct smb_filename *smb_fname,
511 struct smb_filename *cap_smb_fname = NULL;
512 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
520 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
525 if (cap_smb_fname == NULL) {
526 TALLOC_FREE(cappath);
530 ret = SMB_VFS_NEXT_MKNOD(handle, cap_smb_fname, mode, dev);
534 TALLOC_FREE(cappath);
535 TALLOC_FREE(cap_smb_fname);
536 if (saved_errno != 0) {
542 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
544 /* monyo need capencode'ed and capdecode'ed? */
545 char *cappath = capencode(talloc_tos(), path);
551 return SMB_VFS_NEXT_REALPATH(handle, cappath);
554 static int cap_chmod_acl(vfs_handle_struct *handle,
555 const struct smb_filename *smb_fname,
558 struct smb_filename *cap_smb_fname = NULL;
559 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
563 /* If the underlying VFS doesn't have ACL support... */
568 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
573 if (cap_smb_fname == NULL) {
574 TALLOC_FREE(cappath);
579 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
581 TALLOC_FREE(cappath);
582 TALLOC_FREE(cap_smb_fname);
587 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
588 const struct smb_filename *smb_fname,
592 struct smb_filename *cap_smb_fname = NULL;
593 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
599 return (SMB_ACL_T)NULL;
601 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
606 if (cap_smb_fname == NULL) {
607 TALLOC_FREE(cappath);
609 return (SMB_ACL_T)NULL;
611 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
616 TALLOC_FREE(cappath);
617 TALLOC_FREE(cap_smb_fname);
618 if (saved_errno != 0) {
624 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
625 const struct smb_filename *smb_fname,
626 SMB_ACL_TYPE_T acltype,
629 struct smb_filename *cap_smb_fname = NULL;
630 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
638 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
643 if (cap_smb_fname == NULL) {
644 TALLOC_FREE(cappath);
648 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
653 TALLOC_FREE(cappath);
654 TALLOC_FREE(cap_smb_fname);
655 if (saved_errno != 0) {
661 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
662 const struct smb_filename *smb_fname)
664 struct smb_filename *cap_smb_fname = NULL;
665 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
673 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
678 if (cap_smb_fname == NULL) {
679 TALLOC_FREE(cappath);
683 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
687 TALLOC_FREE(cappath);
688 TALLOC_FREE(cap_smb_fname);
695 static ssize_t cap_getxattr(vfs_handle_struct *handle,
696 const struct smb_filename *smb_fname,
701 struct smb_filename *cap_smb_fname = NULL;
702 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
703 char *capname = capencode(talloc_tos(), name);
707 if (!cappath || !capname) {
711 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
716 if (cap_smb_fname == NULL) {
717 TALLOC_FREE(cappath);
718 TALLOC_FREE(capname);
722 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
723 capname, value, size);
727 TALLOC_FREE(cappath);
728 TALLOC_FREE(capname);
729 TALLOC_FREE(cap_smb_fname);
736 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
738 char *cappath = capencode(talloc_tos(), path);
744 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
747 static ssize_t cap_listxattr(vfs_handle_struct *handle,
748 const struct smb_filename *smb_fname,
752 struct smb_filename *cap_smb_fname = NULL;
753 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
761 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
766 if (cap_smb_fname == NULL) {
767 TALLOC_FREE(cappath);
771 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
775 TALLOC_FREE(cappath);
776 TALLOC_FREE(cap_smb_fname);
783 static int cap_removexattr(vfs_handle_struct *handle,
784 const struct smb_filename *smb_fname,
787 struct smb_filename *cap_smb_fname = NULL;
788 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
789 char *capname = capencode(talloc_tos(), name);
793 if (!cappath || !capname) {
797 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
802 if (cap_smb_fname == NULL) {
803 TALLOC_FREE(cappath);
804 TALLOC_FREE(capname);
808 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
812 TALLOC_FREE(cappath);
813 TALLOC_FREE(capname);
814 TALLOC_FREE(cap_smb_fname);
821 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
823 char *cappath = capencode(talloc_tos(), path);
829 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
832 static int cap_setxattr(vfs_handle_struct *handle,
833 const struct smb_filename *smb_fname,
839 struct smb_filename *cap_smb_fname = NULL;
840 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
841 char *capname = capencode(talloc_tos(), name);
845 if (!cappath || !capname) {
849 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
854 if (cap_smb_fname == NULL) {
855 TALLOC_FREE(cappath);
856 TALLOC_FREE(capname);
860 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
861 capname, value, size, flags);
865 TALLOC_FREE(cappath);
866 TALLOC_FREE(capname);
867 TALLOC_FREE(cap_smb_fname);
874 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
876 char *cappath = capencode(talloc_tos(), path);
882 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
885 static struct vfs_fn_pointers vfs_cap_fns = {
886 .disk_free_fn = cap_disk_free,
887 .get_quota_fn = cap_get_quota,
888 .opendir_fn = cap_opendir,
889 .readdir_fn = cap_readdir,
890 .mkdir_fn = cap_mkdir,
891 .rmdir_fn = cap_rmdir,
893 .rename_fn = cap_rename,
895 .lstat_fn = cap_lstat,
896 .unlink_fn = cap_unlink,
897 .chmod_fn = cap_chmod,
898 .chown_fn = cap_chown,
899 .lchown_fn = cap_lchown,
900 .chdir_fn = cap_chdir,
901 .ntimes_fn = cap_ntimes,
902 .symlink_fn = cap_symlink,
903 .readlink_fn = cap_readlink,
905 .mknod_fn = cap_mknod,
906 .realpath_fn = cap_realpath,
907 .chmod_acl_fn = cap_chmod_acl,
908 .sys_acl_get_file_fn = cap_sys_acl_get_file,
909 .sys_acl_set_file_fn = cap_sys_acl_set_file,
910 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
911 .getxattr_fn = cap_getxattr,
912 .fgetxattr_fn = cap_fgetxattr,
913 .listxattr_fn = cap_listxattr,
914 .removexattr_fn = cap_removexattr,
915 .fremovexattr_fn = cap_fremovexattr,
916 .setxattr_fn = cap_setxattr,
917 .fsetxattr_fn = cap_fsetxattr
920 NTSTATUS vfs_cap_init(TALLOC_CTX *);
921 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
923 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
927 /* For CAP functions */
929 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
930 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
931 #define is_hex(s) ((s)[0] == hex_tag)
933 static unsigned char hex2bin_table[256] = {
934 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
935 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
936 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
937 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
938 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
939 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
940 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
941 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
942 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
943 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
944 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
945 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
946 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
947 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
948 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
949 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
950 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
951 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
953 static unsigned char bin2hex_table[256] = "0123456789abcdef";
955 /*******************************************************************
956 original code -> ":xx" - CAP format
957 ********************************************************************/
959 static char *capencode(TALLOC_CTX *ctx, const char *from)
966 for (p1 = from; *p1; p1++) {
967 if ((unsigned char)*p1 >= 0x80) {
975 to = talloc_array(ctx, char, len);
980 for (out = to; *from;) {
981 /* buffer husoku error */
982 if ((unsigned char)*from >= 0x80) {
984 *out++ = bin2hex (((*from)>>4)&0x0f);
985 *out++ = bin2hex ((*from)&0x0f);
995 /*******************************************************************
997 ********************************************************************/
998 /* ":xx" -> a byte */
1000 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1007 for (p1 = from; *p1; len++) {
1016 to = talloc_array(ctx, char, len);
1021 for (out = to; *from;) {
1023 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));