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(),
73 if (cap_smb_fname == NULL) {
78 return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
81 static struct dirent *cap_readdir(vfs_handle_struct *handle,
83 SMB_STRUCT_STAT *sbuf)
85 struct dirent *result;
86 struct dirent *newdirent;
89 DEBUG(3,("cap: cap_readdir\n"));
91 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
96 newname = capdecode(talloc_tos(), result->d_name);
100 DEBUG(3,("cap: cap_readdir: %s\n", newname));
101 newnamelen = strlen(newname)+1;
102 newdirent = talloc_size(
103 talloc_tos(), sizeof(struct dirent) + newnamelen);
107 talloc_set_name_const(newdirent, "struct dirent");
108 memcpy(newdirent, result, sizeof(struct dirent));
109 memcpy(&newdirent->d_name, newname, newnamelen);
113 static int cap_mkdir(vfs_handle_struct *handle,
114 const struct smb_filename *smb_fname,
117 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
118 struct smb_filename *cap_smb_fname = NULL;
125 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
129 if (cap_smb_fname == NULL) {
130 TALLOC_FREE(cappath);
135 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
138 static int cap_rmdir(vfs_handle_struct *handle,
139 const struct smb_filename *smb_fname)
141 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
142 struct smb_filename *cap_smb_fname = NULL;
149 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
153 if (cap_smb_fname == NULL) {
154 TALLOC_FREE(cappath);
159 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
162 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
163 files_struct *fsp, int flags, mode_t mode)
166 char *tmp_base_name = NULL;
169 cappath = capencode(talloc_tos(), smb_fname->base_name);
176 tmp_base_name = smb_fname->base_name;
177 smb_fname->base_name = cappath;
179 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
180 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
182 smb_fname->base_name = tmp_base_name;
183 TALLOC_FREE(cappath);
188 static int cap_rename(vfs_handle_struct *handle,
189 const struct smb_filename *smb_fname_src,
190 const struct smb_filename *smb_fname_dst)
194 struct smb_filename *smb_fname_src_tmp = NULL;
195 struct smb_filename *smb_fname_dst_tmp = NULL;
198 capold = capencode(talloc_tos(), smb_fname_src->base_name);
199 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
200 if (!capold || !capnew) {
205 /* Setup temporary smb_filename structs. */
206 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
207 if (smb_fname_src_tmp == NULL) {
211 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
212 if (smb_fname_dst_tmp == NULL) {
217 smb_fname_src_tmp->base_name = capold;
218 smb_fname_dst_tmp->base_name = capnew;
220 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
225 TALLOC_FREE(smb_fname_src_tmp);
226 TALLOC_FREE(smb_fname_dst_tmp);
231 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
234 char *tmp_base_name = NULL;
237 cappath = capencode(talloc_tos(), smb_fname->base_name);
244 tmp_base_name = smb_fname->base_name;
245 smb_fname->base_name = cappath;
247 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
249 smb_fname->base_name = tmp_base_name;
250 TALLOC_FREE(cappath);
255 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
258 char *tmp_base_name = NULL;
261 cappath = capencode(talloc_tos(), smb_fname->base_name);
268 tmp_base_name = smb_fname->base_name;
269 smb_fname->base_name = cappath;
271 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
273 smb_fname->base_name = tmp_base_name;
274 TALLOC_FREE(cappath);
279 static int cap_unlink(vfs_handle_struct *handle,
280 const struct smb_filename *smb_fname)
282 struct smb_filename *smb_fname_tmp = NULL;
283 char *cappath = NULL;
286 cappath = capencode(talloc_tos(), smb_fname->base_name);
292 /* Setup temporary smb_filename structs. */
293 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
294 if (smb_fname_tmp == NULL) {
299 smb_fname_tmp->base_name = cappath;
301 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
303 TALLOC_FREE(smb_fname_tmp);
307 static int cap_chmod(vfs_handle_struct *handle,
308 const struct smb_filename *smb_fname,
311 struct smb_filename *cap_smb_fname = NULL;
312 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
321 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
325 if (cap_smb_fname == NULL) {
326 TALLOC_FREE(cappath);
331 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
333 TALLOC_FREE(cappath);
334 TALLOC_FREE(cap_smb_fname);
339 static int cap_chown(vfs_handle_struct *handle,
340 const struct smb_filename *smb_fname,
344 struct smb_filename *cap_smb_fname = NULL;
345 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
354 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
358 if (cap_smb_fname == NULL) {
359 TALLOC_FREE(cappath);
364 ret = SMB_VFS_NEXT_CHOWN(handle, cap_smb_fname, uid, gid);
366 TALLOC_FREE(cappath);
367 TALLOC_FREE(cap_smb_fname);
372 static int cap_lchown(vfs_handle_struct *handle,
373 const struct smb_filename *smb_fname,
377 struct smb_filename *cap_smb_fname = NULL;
378 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
387 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
391 if (cap_smb_fname == NULL) {
392 TALLOC_FREE(cappath);
397 ret = SMB_VFS_NEXT_LCHOWN(handle, cap_smb_fname, uid, gid);
399 TALLOC_FREE(cappath);
400 TALLOC_FREE(cap_smb_fname);
405 static int cap_chdir(vfs_handle_struct *handle, const char *path)
407 char *cappath = capencode(talloc_tos(), path);
413 DEBUG(3,("cap: cap_chdir for %s\n", path));
414 return SMB_VFS_NEXT_CHDIR(handle, cappath);
417 static int cap_ntimes(vfs_handle_struct *handle,
418 const struct smb_filename *smb_fname,
419 struct smb_file_time *ft)
421 struct smb_filename *smb_fname_tmp = NULL;
422 char *cappath = NULL;
425 cappath = capencode(talloc_tos(), smb_fname->base_name);
432 /* Setup temporary smb_filename structs. */
433 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
434 if (smb_fname_tmp == NULL) {
439 smb_fname_tmp->base_name = cappath;
441 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
443 TALLOC_FREE(smb_fname_tmp);
448 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
451 char *capold = capencode(talloc_tos(), oldpath);
452 char *capnew = capencode(talloc_tos(), newpath);
454 if (!capold || !capnew) {
458 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
461 static int cap_readlink(vfs_handle_struct *handle, const char *path,
462 char *buf, size_t bufsiz)
464 char *cappath = capencode(talloc_tos(), path);
470 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
473 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
475 char *capold = capencode(talloc_tos(), oldpath);
476 char *capnew = capencode(talloc_tos(), newpath);
478 if (!capold || !capnew) {
482 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
485 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
487 char *cappath = capencode(talloc_tos(), path);
493 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
496 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
498 /* monyo need capencode'ed and capdecode'ed? */
499 char *cappath = capencode(talloc_tos(), path);
505 return SMB_VFS_NEXT_REALPATH(handle, cappath);
508 static int cap_chmod_acl(vfs_handle_struct *handle,
509 const struct smb_filename *smb_fname,
512 struct smb_filename *cap_smb_fname = NULL;
513 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
517 /* If the underlying VFS doesn't have ACL support... */
522 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
526 if (cap_smb_fname == NULL) {
527 TALLOC_FREE(cappath);
532 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
534 TALLOC_FREE(cappath);
535 TALLOC_FREE(cap_smb_fname);
540 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
541 const char *path, SMB_ACL_TYPE_T type,
544 char *cappath = capencode(talloc_tos(), path);
548 return (SMB_ACL_T)NULL;
550 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
553 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
555 char *cappath = capencode(talloc_tos(), path);
561 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
564 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
566 char *cappath = capencode(talloc_tos(), path);
572 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
575 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
577 char *cappath = capencode(talloc_tos(), path);
578 char *capname = capencode(talloc_tos(), name);
580 if (!cappath || !capname) {
584 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
587 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
589 char *cappath = capencode(talloc_tos(), path);
595 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
598 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
600 char *cappath = capencode(talloc_tos(), path);
606 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
609 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
611 char *cappath = capencode(talloc_tos(), path);
612 char *capname = capencode(talloc_tos(), name);
614 if (!cappath || !capname) {
618 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
621 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
623 char *cappath = capencode(talloc_tos(), path);
629 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
632 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
634 char *cappath = capencode(talloc_tos(), path);
635 char *capname = capencode(talloc_tos(), name);
637 if (!cappath || !capname) {
641 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
644 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
646 char *cappath = capencode(talloc_tos(), path);
652 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
655 static struct vfs_fn_pointers vfs_cap_fns = {
656 .disk_free_fn = cap_disk_free,
657 .get_quota_fn = cap_get_quota,
658 .opendir_fn = cap_opendir,
659 .readdir_fn = cap_readdir,
660 .mkdir_fn = cap_mkdir,
661 .rmdir_fn = cap_rmdir,
663 .rename_fn = cap_rename,
665 .lstat_fn = cap_lstat,
666 .unlink_fn = cap_unlink,
667 .chmod_fn = cap_chmod,
668 .chown_fn = cap_chown,
669 .lchown_fn = cap_lchown,
670 .chdir_fn = cap_chdir,
671 .ntimes_fn = cap_ntimes,
672 .symlink_fn = cap_symlink,
673 .readlink_fn = cap_readlink,
675 .mknod_fn = cap_mknod,
676 .realpath_fn = cap_realpath,
677 .chmod_acl_fn = cap_chmod_acl,
678 .sys_acl_get_file_fn = cap_sys_acl_get_file,
679 .sys_acl_set_file_fn = cap_sys_acl_set_file,
680 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
681 .getxattr_fn = cap_getxattr,
682 .fgetxattr_fn = cap_fgetxattr,
683 .listxattr_fn = cap_listxattr,
684 .removexattr_fn = cap_removexattr,
685 .fremovexattr_fn = cap_fremovexattr,
686 .setxattr_fn = cap_setxattr,
687 .fsetxattr_fn = cap_fsetxattr
690 NTSTATUS vfs_cap_init(void);
691 NTSTATUS vfs_cap_init(void)
693 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
697 /* For CAP functions */
699 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
700 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
701 #define is_hex(s) ((s)[0] == hex_tag)
703 static unsigned char hex2bin_table[256] = {
704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
705 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
706 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
707 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
708 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
709 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
710 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
711 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
712 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
713 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
714 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
715 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
716 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
717 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
718 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
719 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
720 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
721 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
723 static unsigned char bin2hex_table[256] = "0123456789abcdef";
725 /*******************************************************************
726 original code -> ":xx" - CAP format
727 ********************************************************************/
729 static char *capencode(TALLOC_CTX *ctx, const char *from)
736 for (p1 = from; *p1; p1++) {
737 if ((unsigned char)*p1 >= 0x80) {
745 to = talloc_array(ctx, char, len);
750 for (out = to; *from;) {
751 /* buffer husoku error */
752 if ((unsigned char)*from >= 0x80) {
754 *out++ = bin2hex (((*from)>>4)&0x0f);
755 *out++ = bin2hex ((*from)&0x0f);
765 /*******************************************************************
767 ********************************************************************/
768 /* ":xx" -> a byte */
770 static char *capdecode(TALLOC_CTX *ctx, const char *from)
777 for (p1 = from; *p1; len++) {
786 to = talloc_array(ctx, char, len);
791 for (out = to; *from;) {
793 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));