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,
126 const struct smb_filename *smb_fname)
128 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
129 struct smb_filename *cap_smb_fname = NULL;
136 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
140 if (cap_smb_fname == NULL) {
141 TALLOC_FREE(cappath);
146 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
149 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
150 files_struct *fsp, int flags, mode_t mode)
153 char *tmp_base_name = NULL;
156 cappath = capencode(talloc_tos(), smb_fname->base_name);
163 tmp_base_name = smb_fname->base_name;
164 smb_fname->base_name = cappath;
166 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
167 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
169 smb_fname->base_name = tmp_base_name;
170 TALLOC_FREE(cappath);
175 static int cap_rename(vfs_handle_struct *handle,
176 const struct smb_filename *smb_fname_src,
177 const struct smb_filename *smb_fname_dst)
181 struct smb_filename *smb_fname_src_tmp = NULL;
182 struct smb_filename *smb_fname_dst_tmp = NULL;
185 capold = capencode(talloc_tos(), smb_fname_src->base_name);
186 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
187 if (!capold || !capnew) {
192 /* Setup temporary smb_filename structs. */
193 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
194 if (smb_fname_src_tmp == NULL) {
198 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
199 if (smb_fname_dst_tmp == NULL) {
204 smb_fname_src_tmp->base_name = capold;
205 smb_fname_dst_tmp->base_name = capnew;
207 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
212 TALLOC_FREE(smb_fname_src_tmp);
213 TALLOC_FREE(smb_fname_dst_tmp);
218 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
221 char *tmp_base_name = NULL;
224 cappath = capencode(talloc_tos(), smb_fname->base_name);
231 tmp_base_name = smb_fname->base_name;
232 smb_fname->base_name = cappath;
234 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
236 smb_fname->base_name = tmp_base_name;
237 TALLOC_FREE(cappath);
242 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
245 char *tmp_base_name = NULL;
248 cappath = capencode(talloc_tos(), smb_fname->base_name);
255 tmp_base_name = smb_fname->base_name;
256 smb_fname->base_name = cappath;
258 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
260 smb_fname->base_name = tmp_base_name;
261 TALLOC_FREE(cappath);
266 static int cap_unlink(vfs_handle_struct *handle,
267 const struct smb_filename *smb_fname)
269 struct smb_filename *smb_fname_tmp = NULL;
270 char *cappath = NULL;
273 cappath = capencode(talloc_tos(), smb_fname->base_name);
279 /* Setup temporary smb_filename structs. */
280 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
281 if (smb_fname_tmp == NULL) {
286 smb_fname_tmp->base_name = cappath;
288 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
290 TALLOC_FREE(smb_fname_tmp);
294 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
296 char *cappath = capencode(talloc_tos(), path);
302 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
305 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
307 char *cappath = capencode(talloc_tos(), path);
313 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
316 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
318 char *cappath = capencode(talloc_tos(), path);
324 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
327 static int cap_chdir(vfs_handle_struct *handle, const char *path)
329 char *cappath = capencode(talloc_tos(), path);
335 DEBUG(3,("cap: cap_chdir for %s\n", path));
336 return SMB_VFS_NEXT_CHDIR(handle, cappath);
339 static int cap_ntimes(vfs_handle_struct *handle,
340 const struct smb_filename *smb_fname,
341 struct smb_file_time *ft)
343 struct smb_filename *smb_fname_tmp = NULL;
344 char *cappath = NULL;
347 cappath = capencode(talloc_tos(), smb_fname->base_name);
354 /* Setup temporary smb_filename structs. */
355 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
356 if (smb_fname_tmp == NULL) {
361 smb_fname_tmp->base_name = cappath;
363 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
365 TALLOC_FREE(smb_fname_tmp);
370 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
373 char *capold = capencode(talloc_tos(), oldpath);
374 char *capnew = capencode(talloc_tos(), newpath);
376 if (!capold || !capnew) {
380 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
383 static int cap_readlink(vfs_handle_struct *handle, const char *path,
384 char *buf, size_t bufsiz)
386 char *cappath = capencode(talloc_tos(), path);
392 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
395 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
397 char *capold = capencode(talloc_tos(), oldpath);
398 char *capnew = capencode(talloc_tos(), newpath);
400 if (!capold || !capnew) {
404 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
407 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
409 char *cappath = capencode(talloc_tos(), path);
415 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
418 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
420 /* monyo need capencode'ed and capdecode'ed? */
421 char *cappath = capencode(talloc_tos(), path);
427 return SMB_VFS_NEXT_REALPATH(handle, cappath);
430 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
432 char *cappath = capencode(talloc_tos(), path);
434 /* If the underlying VFS doesn't have ACL support... */
439 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
442 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
443 const char *path, SMB_ACL_TYPE_T type,
446 char *cappath = capencode(talloc_tos(), path);
450 return (SMB_ACL_T)NULL;
452 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
455 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
457 char *cappath = capencode(talloc_tos(), path);
463 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
466 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
468 char *cappath = capencode(talloc_tos(), path);
474 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
477 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
479 char *cappath = capencode(talloc_tos(), path);
480 char *capname = capencode(talloc_tos(), name);
482 if (!cappath || !capname) {
486 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
489 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
491 char *cappath = capencode(talloc_tos(), path);
497 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
500 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
502 char *cappath = capencode(talloc_tos(), path);
508 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
511 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
513 char *cappath = capencode(talloc_tos(), path);
514 char *capname = capencode(talloc_tos(), name);
516 if (!cappath || !capname) {
520 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
523 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
525 char *cappath = capencode(talloc_tos(), path);
531 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
534 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
536 char *cappath = capencode(talloc_tos(), path);
537 char *capname = capencode(talloc_tos(), name);
539 if (!cappath || !capname) {
543 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
546 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
548 char *cappath = capencode(talloc_tos(), path);
554 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
557 static struct vfs_fn_pointers vfs_cap_fns = {
558 .disk_free_fn = cap_disk_free,
559 .get_quota_fn = cap_get_quota,
560 .opendir_fn = cap_opendir,
561 .readdir_fn = cap_readdir,
562 .mkdir_fn = cap_mkdir,
563 .rmdir_fn = cap_rmdir,
565 .rename_fn = cap_rename,
567 .lstat_fn = cap_lstat,
568 .unlink_fn = cap_unlink,
569 .chmod_fn = cap_chmod,
570 .chown_fn = cap_chown,
571 .lchown_fn = cap_lchown,
572 .chdir_fn = cap_chdir,
573 .ntimes_fn = cap_ntimes,
574 .symlink_fn = cap_symlink,
575 .readlink_fn = cap_readlink,
577 .mknod_fn = cap_mknod,
578 .realpath_fn = cap_realpath,
579 .chmod_acl_fn = cap_chmod_acl,
580 .sys_acl_get_file_fn = cap_sys_acl_get_file,
581 .sys_acl_set_file_fn = cap_sys_acl_set_file,
582 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
583 .getxattr_fn = cap_getxattr,
584 .fgetxattr_fn = cap_fgetxattr,
585 .listxattr_fn = cap_listxattr,
586 .removexattr_fn = cap_removexattr,
587 .fremovexattr_fn = cap_fremovexattr,
588 .setxattr_fn = cap_setxattr,
589 .fsetxattr_fn = cap_fsetxattr
592 NTSTATUS vfs_cap_init(void);
593 NTSTATUS vfs_cap_init(void)
595 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
599 /* For CAP functions */
601 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
602 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
603 #define is_hex(s) ((s)[0] == hex_tag)
605 static unsigned char hex2bin_table[256] = {
606 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
607 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
608 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
609 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
610 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
611 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
612 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
613 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
614 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
617 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
618 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
619 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
620 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
621 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
622 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
625 static unsigned char bin2hex_table[256] = "0123456789abcdef";
627 /*******************************************************************
628 original code -> ":xx" - CAP format
629 ********************************************************************/
631 static char *capencode(TALLOC_CTX *ctx, const char *from)
638 for (p1 = from; *p1; p1++) {
639 if ((unsigned char)*p1 >= 0x80) {
647 to = talloc_array(ctx, char, len);
652 for (out = to; *from;) {
653 /* buffer husoku error */
654 if ((unsigned char)*from >= 0x80) {
656 *out++ = bin2hex (((*from)>>4)&0x0f);
657 *out++ = bin2hex ((*from)&0x0f);
667 /*******************************************************************
669 ********************************************************************/
670 /* ":xx" -> a byte */
672 static char *capdecode(TALLOC_CTX *ctx, const char *from)
679 for (p1 = from; *p1; len++) {
688 to = talloc_array(ctx, char, len);
693 for (out = to; *from;) {
695 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));