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, const char *path, mode_t mode)
309 char *cappath = capencode(talloc_tos(), path);
315 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
318 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
320 char *cappath = capencode(talloc_tos(), path);
326 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
329 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
331 char *cappath = capencode(talloc_tos(), path);
337 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
340 static int cap_chdir(vfs_handle_struct *handle, const char *path)
342 char *cappath = capencode(talloc_tos(), path);
348 DEBUG(3,("cap: cap_chdir for %s\n", path));
349 return SMB_VFS_NEXT_CHDIR(handle, cappath);
352 static int cap_ntimes(vfs_handle_struct *handle,
353 const struct smb_filename *smb_fname,
354 struct smb_file_time *ft)
356 struct smb_filename *smb_fname_tmp = NULL;
357 char *cappath = NULL;
360 cappath = capencode(talloc_tos(), smb_fname->base_name);
367 /* Setup temporary smb_filename structs. */
368 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
369 if (smb_fname_tmp == NULL) {
374 smb_fname_tmp->base_name = cappath;
376 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
378 TALLOC_FREE(smb_fname_tmp);
383 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
386 char *capold = capencode(talloc_tos(), oldpath);
387 char *capnew = capencode(talloc_tos(), newpath);
389 if (!capold || !capnew) {
393 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
396 static int cap_readlink(vfs_handle_struct *handle, const char *path,
397 char *buf, size_t bufsiz)
399 char *cappath = capencode(talloc_tos(), path);
405 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
408 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
410 char *capold = capencode(talloc_tos(), oldpath);
411 char *capnew = capencode(talloc_tos(), newpath);
413 if (!capold || !capnew) {
417 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
420 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
422 char *cappath = capencode(talloc_tos(), path);
428 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
431 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
433 /* monyo need capencode'ed and capdecode'ed? */
434 char *cappath = capencode(talloc_tos(), path);
440 return SMB_VFS_NEXT_REALPATH(handle, cappath);
443 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
445 char *cappath = capencode(talloc_tos(), path);
447 /* If the underlying VFS doesn't have ACL support... */
452 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
455 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
456 const char *path, SMB_ACL_TYPE_T type,
459 char *cappath = capencode(talloc_tos(), path);
463 return (SMB_ACL_T)NULL;
465 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
468 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
470 char *cappath = capencode(talloc_tos(), path);
476 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
479 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
481 char *cappath = capencode(talloc_tos(), path);
487 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
490 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
492 char *cappath = capencode(talloc_tos(), path);
493 char *capname = capencode(talloc_tos(), name);
495 if (!cappath || !capname) {
499 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
502 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
504 char *cappath = capencode(talloc_tos(), path);
510 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
513 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
515 char *cappath = capencode(talloc_tos(), path);
521 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
524 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
526 char *cappath = capencode(talloc_tos(), path);
527 char *capname = capencode(talloc_tos(), name);
529 if (!cappath || !capname) {
533 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
536 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
538 char *cappath = capencode(talloc_tos(), path);
544 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
547 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
549 char *cappath = capencode(talloc_tos(), path);
550 char *capname = capencode(talloc_tos(), name);
552 if (!cappath || !capname) {
556 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
559 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
561 char *cappath = capencode(talloc_tos(), path);
567 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
570 static struct vfs_fn_pointers vfs_cap_fns = {
571 .disk_free_fn = cap_disk_free,
572 .get_quota_fn = cap_get_quota,
573 .opendir_fn = cap_opendir,
574 .readdir_fn = cap_readdir,
575 .mkdir_fn = cap_mkdir,
576 .rmdir_fn = cap_rmdir,
578 .rename_fn = cap_rename,
580 .lstat_fn = cap_lstat,
581 .unlink_fn = cap_unlink,
582 .chmod_fn = cap_chmod,
583 .chown_fn = cap_chown,
584 .lchown_fn = cap_lchown,
585 .chdir_fn = cap_chdir,
586 .ntimes_fn = cap_ntimes,
587 .symlink_fn = cap_symlink,
588 .readlink_fn = cap_readlink,
590 .mknod_fn = cap_mknod,
591 .realpath_fn = cap_realpath,
592 .chmod_acl_fn = cap_chmod_acl,
593 .sys_acl_get_file_fn = cap_sys_acl_get_file,
594 .sys_acl_set_file_fn = cap_sys_acl_set_file,
595 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
596 .getxattr_fn = cap_getxattr,
597 .fgetxattr_fn = cap_fgetxattr,
598 .listxattr_fn = cap_listxattr,
599 .removexattr_fn = cap_removexattr,
600 .fremovexattr_fn = cap_fremovexattr,
601 .setxattr_fn = cap_setxattr,
602 .fsetxattr_fn = cap_fsetxattr
605 NTSTATUS vfs_cap_init(void);
606 NTSTATUS vfs_cap_init(void)
608 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
612 /* For CAP functions */
614 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
615 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
616 #define is_hex(s) ((s)[0] == hex_tag)
618 static unsigned char hex2bin_table[256] = {
619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
620 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
621 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
622 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
623 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
624 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
625 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
626 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
627 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
629 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
634 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
635 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
636 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
638 static unsigned char bin2hex_table[256] = "0123456789abcdef";
640 /*******************************************************************
641 original code -> ":xx" - CAP format
642 ********************************************************************/
644 static char *capencode(TALLOC_CTX *ctx, const char *from)
651 for (p1 = from; *p1; p1++) {
652 if ((unsigned char)*p1 >= 0x80) {
660 to = talloc_array(ctx, char, len);
665 for (out = to; *from;) {
666 /* buffer husoku error */
667 if ((unsigned char)*from >= 0x80) {
669 *out++ = bin2hex (((*from)>>4)&0x0f);
670 *out++ = bin2hex ((*from)&0x0f);
680 /*******************************************************************
682 ********************************************************************/
683 /* ":xx" -> a byte */
685 static char *capdecode(TALLOC_CTX *ctx, const char *from)
692 for (p1 = from; *p1; len++) {
701 to = talloc_array(ctx, char, len);
706 for (out = to; *from;) {
708 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));