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, const char *fname, const char *mask, uint32_t attr)
59 char *capname = capencode(talloc_tos(), fname);
65 return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
68 static struct dirent *cap_readdir(vfs_handle_struct *handle,
70 SMB_STRUCT_STAT *sbuf)
72 struct dirent *result;
73 struct dirent *newdirent;
76 DEBUG(3,("cap: cap_readdir\n"));
78 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
83 newname = capdecode(talloc_tos(), result->d_name);
87 DEBUG(3,("cap: cap_readdir: %s\n", newname));
88 newnamelen = strlen(newname)+1;
89 newdirent = talloc_size(
90 talloc_tos(), sizeof(struct dirent) + newnamelen);
94 talloc_set_name_const(newdirent, "struct dirent");
95 memcpy(newdirent, result, sizeof(struct dirent));
96 memcpy(&newdirent->d_name, newname, newnamelen);
100 static int cap_mkdir(vfs_handle_struct *handle,
101 const struct smb_filename *smb_fname,
104 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
105 struct smb_filename *cap_smb_fname = NULL;
112 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
116 if (cap_smb_fname == NULL) {
117 TALLOC_FREE(cappath);
122 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
125 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
127 char *cappath = capencode(talloc_tos(), path);
133 return SMB_VFS_NEXT_RMDIR(handle, cappath);
136 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
137 files_struct *fsp, int flags, mode_t mode)
140 char *tmp_base_name = NULL;
143 cappath = capencode(talloc_tos(), smb_fname->base_name);
150 tmp_base_name = smb_fname->base_name;
151 smb_fname->base_name = cappath;
153 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
154 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
156 smb_fname->base_name = tmp_base_name;
157 TALLOC_FREE(cappath);
162 static int cap_rename(vfs_handle_struct *handle,
163 const struct smb_filename *smb_fname_src,
164 const struct smb_filename *smb_fname_dst)
168 struct smb_filename *smb_fname_src_tmp = NULL;
169 struct smb_filename *smb_fname_dst_tmp = NULL;
172 capold = capencode(talloc_tos(), smb_fname_src->base_name);
173 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
174 if (!capold || !capnew) {
179 /* Setup temporary smb_filename structs. */
180 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
181 if (smb_fname_src_tmp == NULL) {
185 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
186 if (smb_fname_dst_tmp == NULL) {
191 smb_fname_src_tmp->base_name = capold;
192 smb_fname_dst_tmp->base_name = capnew;
194 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
199 TALLOC_FREE(smb_fname_src_tmp);
200 TALLOC_FREE(smb_fname_dst_tmp);
205 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
208 char *tmp_base_name = NULL;
211 cappath = capencode(talloc_tos(), smb_fname->base_name);
218 tmp_base_name = smb_fname->base_name;
219 smb_fname->base_name = cappath;
221 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
223 smb_fname->base_name = tmp_base_name;
224 TALLOC_FREE(cappath);
229 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
232 char *tmp_base_name = NULL;
235 cappath = capencode(talloc_tos(), smb_fname->base_name);
242 tmp_base_name = smb_fname->base_name;
243 smb_fname->base_name = cappath;
245 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
247 smb_fname->base_name = tmp_base_name;
248 TALLOC_FREE(cappath);
253 static int cap_unlink(vfs_handle_struct *handle,
254 const struct smb_filename *smb_fname)
256 struct smb_filename *smb_fname_tmp = NULL;
257 char *cappath = NULL;
260 cappath = capencode(talloc_tos(), smb_fname->base_name);
266 /* Setup temporary smb_filename structs. */
267 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
268 if (smb_fname_tmp == NULL) {
273 smb_fname_tmp->base_name = cappath;
275 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
277 TALLOC_FREE(smb_fname_tmp);
281 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
283 char *cappath = capencode(talloc_tos(), path);
289 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
292 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
294 char *cappath = capencode(talloc_tos(), path);
300 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
303 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
305 char *cappath = capencode(talloc_tos(), path);
311 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
314 static int cap_chdir(vfs_handle_struct *handle, const char *path)
316 char *cappath = capencode(talloc_tos(), path);
322 DEBUG(3,("cap: cap_chdir for %s\n", path));
323 return SMB_VFS_NEXT_CHDIR(handle, cappath);
326 static int cap_ntimes(vfs_handle_struct *handle,
327 const struct smb_filename *smb_fname,
328 struct smb_file_time *ft)
330 struct smb_filename *smb_fname_tmp = NULL;
331 char *cappath = NULL;
334 cappath = capencode(talloc_tos(), smb_fname->base_name);
341 /* Setup temporary smb_filename structs. */
342 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
343 if (smb_fname_tmp == NULL) {
348 smb_fname_tmp->base_name = cappath;
350 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
352 TALLOC_FREE(smb_fname_tmp);
357 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
360 char *capold = capencode(talloc_tos(), oldpath);
361 char *capnew = capencode(talloc_tos(), newpath);
363 if (!capold || !capnew) {
367 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
370 static int cap_readlink(vfs_handle_struct *handle, const char *path,
371 char *buf, size_t bufsiz)
373 char *cappath = capencode(talloc_tos(), path);
379 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
382 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
384 char *capold = capencode(talloc_tos(), oldpath);
385 char *capnew = capencode(talloc_tos(), newpath);
387 if (!capold || !capnew) {
391 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
394 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
396 char *cappath = capencode(talloc_tos(), path);
402 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
405 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
407 /* monyo need capencode'ed and capdecode'ed? */
408 char *cappath = capencode(talloc_tos(), path);
414 return SMB_VFS_NEXT_REALPATH(handle, cappath);
417 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
419 char *cappath = capencode(talloc_tos(), path);
421 /* If the underlying VFS doesn't have ACL support... */
426 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
429 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
430 const char *path, SMB_ACL_TYPE_T type,
433 char *cappath = capencode(talloc_tos(), path);
437 return (SMB_ACL_T)NULL;
439 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
442 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
444 char *cappath = capencode(talloc_tos(), path);
450 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
453 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
455 char *cappath = capencode(talloc_tos(), path);
461 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
464 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
466 char *cappath = capencode(talloc_tos(), path);
467 char *capname = capencode(talloc_tos(), name);
469 if (!cappath || !capname) {
473 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
476 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
478 char *cappath = capencode(talloc_tos(), path);
484 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
487 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
489 char *cappath = capencode(talloc_tos(), path);
495 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
498 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
500 char *cappath = capencode(talloc_tos(), path);
501 char *capname = capencode(talloc_tos(), name);
503 if (!cappath || !capname) {
507 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
510 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
512 char *cappath = capencode(talloc_tos(), path);
518 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
521 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
523 char *cappath = capencode(talloc_tos(), path);
524 char *capname = capencode(talloc_tos(), name);
526 if (!cappath || !capname) {
530 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
533 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
535 char *cappath = capencode(talloc_tos(), path);
541 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
544 static struct vfs_fn_pointers vfs_cap_fns = {
545 .disk_free_fn = cap_disk_free,
546 .get_quota_fn = cap_get_quota,
547 .opendir_fn = cap_opendir,
548 .readdir_fn = cap_readdir,
549 .mkdir_fn = cap_mkdir,
550 .rmdir_fn = cap_rmdir,
552 .rename_fn = cap_rename,
554 .lstat_fn = cap_lstat,
555 .unlink_fn = cap_unlink,
556 .chmod_fn = cap_chmod,
557 .chown_fn = cap_chown,
558 .lchown_fn = cap_lchown,
559 .chdir_fn = cap_chdir,
560 .ntimes_fn = cap_ntimes,
561 .symlink_fn = cap_symlink,
562 .readlink_fn = cap_readlink,
564 .mknod_fn = cap_mknod,
565 .realpath_fn = cap_realpath,
566 .chmod_acl_fn = cap_chmod_acl,
567 .sys_acl_get_file_fn = cap_sys_acl_get_file,
568 .sys_acl_set_file_fn = cap_sys_acl_set_file,
569 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
570 .getxattr_fn = cap_getxattr,
571 .fgetxattr_fn = cap_fgetxattr,
572 .listxattr_fn = cap_listxattr,
573 .removexattr_fn = cap_removexattr,
574 .fremovexattr_fn = cap_fremovexattr,
575 .setxattr_fn = cap_setxattr,
576 .fsetxattr_fn = cap_fsetxattr
579 NTSTATUS vfs_cap_init(void);
580 NTSTATUS vfs_cap_init(void)
582 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
586 /* For CAP functions */
588 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
589 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
590 #define is_hex(s) ((s)[0] == hex_tag)
592 static unsigned char hex2bin_table[256] = {
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
596 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
597 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
598 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
599 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
600 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
601 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
602 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
603 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
605 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
609 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
610 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
612 static unsigned char bin2hex_table[256] = "0123456789abcdef";
614 /*******************************************************************
615 original code -> ":xx" - CAP format
616 ********************************************************************/
618 static char *capencode(TALLOC_CTX *ctx, const char *from)
625 for (p1 = from; *p1; p1++) {
626 if ((unsigned char)*p1 >= 0x80) {
634 to = talloc_array(ctx, char, len);
639 for (out = to; *from;) {
640 /* buffer husoku error */
641 if ((unsigned char)*from >= 0x80) {
643 *out++ = bin2hex (((*from)>>4)&0x0f);
644 *out++ = bin2hex ((*from)&0x0f);
654 /*******************************************************************
656 ********************************************************************/
657 /* ":xx" -> a byte */
659 static char *capdecode(TALLOC_CTX *ctx, const char *from)
666 for (p1 = from; *p1; len++) {
675 to = talloc_array(ctx, char, len);
680 for (out = to; *from;) {
682 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));