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/>.
28 static char *capencode(TALLOC_CTX *ctx, const char *from);
29 static char *capdecode(TALLOC_CTX *ctx, const char *from);
31 static uint64_t cap_disk_free(vfs_handle_struct *handle, const char *path,
32 bool small_query, uint64_t *bsize,
33 uint64_t *dfree, uint64_t *dsize)
35 char *cappath = capencode(talloc_tos(), path);
41 return SMB_VFS_NEXT_DISK_FREE(handle, cappath, small_query, bsize,
45 static SMB_STRUCT_DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
47 char *capname = capencode(talloc_tos(), fname);
53 return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
56 static SMB_STRUCT_DIRENT *cap_readdir(vfs_handle_struct *handle,
58 SMB_STRUCT_STAT *sbuf)
60 SMB_STRUCT_DIRENT *result;
61 SMB_STRUCT_DIRENT *newdirent;
64 DEBUG(3,("cap: cap_readdir\n"));
66 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
71 newname = capdecode(talloc_tos(), result->d_name);
75 DEBUG(3,("cap: cap_readdir: %s\n", newname));
76 newnamelen = strlen(newname)+1;
77 newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
79 sizeof(SMB_STRUCT_DIRENT)+
84 memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
85 memcpy(&newdirent->d_name, newname, newnamelen);
89 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
91 char *cappath = capencode(talloc_tos(), path);
97 return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
100 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
102 char *cappath = capencode(talloc_tos(), path);
108 return SMB_VFS_NEXT_RMDIR(handle, cappath);
111 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
112 files_struct *fsp, int flags, mode_t mode)
115 char *tmp_base_name = NULL;
118 cappath = capencode(talloc_tos(), smb_fname->base_name);
125 tmp_base_name = smb_fname->base_name;
126 smb_fname->base_name = cappath;
128 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
129 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
131 smb_fname->base_name = tmp_base_name;
132 TALLOC_FREE(cappath);
137 static int cap_rename(vfs_handle_struct *handle,
138 const struct smb_filename *smb_fname_src,
139 const struct smb_filename *smb_fname_dst)
143 struct smb_filename *smb_fname_src_tmp = NULL;
144 struct smb_filename *smb_fname_dst_tmp = NULL;
148 capold = capencode(talloc_tos(), smb_fname_src->base_name);
149 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
150 if (!capold || !capnew) {
155 /* Setup temporary smb_filename structs. */
156 status = copy_smb_filename(talloc_tos(), smb_fname_src,
158 if (!NT_STATUS_IS_OK(status)) {
159 errno = map_errno_from_nt_status(status);
162 status = copy_smb_filename(talloc_tos(), smb_fname_dst,
164 if (!NT_STATUS_IS_OK(status)) {
165 errno = map_errno_from_nt_status(status);
169 smb_fname_src_tmp->base_name = capold;
170 smb_fname_dst_tmp->base_name = capnew;
172 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
177 TALLOC_FREE(smb_fname_src_tmp);
178 TALLOC_FREE(smb_fname_dst_tmp);
183 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
186 char *tmp_base_name = NULL;
189 cappath = capencode(talloc_tos(), smb_fname->base_name);
196 tmp_base_name = smb_fname->base_name;
197 smb_fname->base_name = cappath;
199 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
201 smb_fname->base_name = tmp_base_name;
202 TALLOC_FREE(cappath);
207 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
210 char *tmp_base_name = NULL;
213 cappath = capencode(talloc_tos(), smb_fname->base_name);
220 tmp_base_name = smb_fname->base_name;
221 smb_fname->base_name = cappath;
223 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
225 smb_fname->base_name = tmp_base_name;
226 TALLOC_FREE(cappath);
231 static int cap_unlink(vfs_handle_struct *handle,
232 const struct smb_filename *smb_fname)
234 struct smb_filename *smb_fname_tmp = NULL;
235 char *cappath = NULL;
239 cappath = capencode(talloc_tos(), smb_fname->base_name);
245 /* Setup temporary smb_filename structs. */
246 status = copy_smb_filename(talloc_tos(), smb_fname,
248 if (!NT_STATUS_IS_OK(status)) {
249 errno = map_errno_from_nt_status(status);
253 smb_fname_tmp->base_name = cappath;
255 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
257 TALLOC_FREE(smb_fname_tmp);
261 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
263 char *cappath = capencode(talloc_tos(), path);
269 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
272 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
274 char *cappath = capencode(talloc_tos(), path);
280 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
283 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
285 char *cappath = capencode(talloc_tos(), path);
291 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
294 static int cap_chdir(vfs_handle_struct *handle, const char *path)
296 char *cappath = capencode(talloc_tos(), path);
302 DEBUG(3,("cap: cap_chdir for %s\n", path));
303 return SMB_VFS_NEXT_CHDIR(handle, cappath);
306 static int cap_ntimes(vfs_handle_struct *handle,
307 const struct smb_filename *smb_fname,
308 struct smb_file_time *ft)
310 struct smb_filename *smb_fname_tmp = NULL;
311 char *cappath = NULL;
315 cappath = capencode(talloc_tos(), smb_fname->base_name);
322 /* Setup temporary smb_filename structs. */
323 status = copy_smb_filename(talloc_tos(), smb_fname,
325 if (!NT_STATUS_IS_OK(status)) {
326 errno = map_errno_from_nt_status(status);
330 smb_fname_tmp->base_name = cappath;
332 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
334 TALLOC_FREE(smb_fname_tmp);
339 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
342 char *capold = capencode(talloc_tos(), oldpath);
343 char *capnew = capencode(talloc_tos(), newpath);
345 if (!capold || !capnew) {
349 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
352 static int cap_readlink(vfs_handle_struct *handle, const char *path,
353 char *buf, size_t bufsiz)
355 char *cappath = capencode(talloc_tos(), path);
361 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
364 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
366 char *capold = capencode(talloc_tos(), oldpath);
367 char *capnew = capencode(talloc_tos(), newpath);
369 if (!capold || !capnew) {
373 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
376 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
378 char *cappath = capencode(talloc_tos(), path);
384 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
387 static char *cap_realpath(vfs_handle_struct *handle, const char *path, char *resolved_path)
389 /* monyo need capencode'ed and capdecode'ed? */
390 char *cappath = capencode(talloc_tos(), path);
396 return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
399 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
401 char *cappath = capencode(talloc_tos(), path);
403 /* If the underlying VFS doesn't have ACL support... */
404 if (!handle->vfs_next.ops.chmod_acl) {
412 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
415 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T type)
417 char *cappath = capencode(talloc_tos(), path);
421 return (SMB_ACL_T)NULL;
423 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type);
426 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
428 char *cappath = capencode(talloc_tos(), path);
434 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
437 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
439 char *cappath = capencode(talloc_tos(), path);
445 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
448 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
450 char *cappath = capencode(talloc_tos(), path);
451 char *capname = capencode(talloc_tos(), name);
453 if (!cappath || !capname) {
457 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
460 static ssize_t cap_lgetxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t
463 char *cappath = capencode(talloc_tos(), path);
464 char *capname = capencode(talloc_tos(), name);
466 if (!cappath || !capname) {
470 return SMB_VFS_NEXT_LGETXATTR(handle, cappath, capname, value, size);
473 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
475 char *cappath = capencode(talloc_tos(), path);
481 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
484 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
486 char *cappath = capencode(talloc_tos(), path);
492 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
495 static ssize_t cap_llistxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
497 char *cappath = capencode(talloc_tos(), path);
503 return SMB_VFS_NEXT_LLISTXATTR(handle, cappath, list, size);
506 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
508 char *cappath = capencode(talloc_tos(), path);
509 char *capname = capencode(talloc_tos(), name);
511 if (!cappath || !capname) {
515 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
518 static int cap_lremovexattr(vfs_handle_struct *handle, const char *path, const char *name)
520 char *cappath = capencode(talloc_tos(), path);
521 char *capname = capencode(talloc_tos(), name);
523 if (!cappath || !capname) {
527 return SMB_VFS_NEXT_LREMOVEXATTR(handle, cappath, capname);
530 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
532 char *cappath = capencode(talloc_tos(), path);
538 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
541 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
543 char *cappath = capencode(talloc_tos(), path);
544 char *capname = capencode(talloc_tos(), name);
546 if (!cappath || !capname) {
550 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
553 static int cap_lsetxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
555 char *cappath = capencode(talloc_tos(), path);
556 char *capname = capencode(talloc_tos(), name);
558 if (!cappath || !capname) {
562 return SMB_VFS_NEXT_LSETXATTR(handle, cappath, capname, value, size, flags);
565 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
567 char *cappath = capencode(talloc_tos(), path);
573 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
576 /* VFS operations structure */
578 static vfs_op_tuple cap_op_tuples[] = {
580 /* Disk operations */
582 {SMB_VFS_OP(cap_disk_free), SMB_VFS_OP_DISK_FREE, SMB_VFS_LAYER_TRANSPARENT},
584 /* Directory operations */
586 {SMB_VFS_OP(cap_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_TRANSPARENT},
587 {SMB_VFS_OP(cap_readdir), SMB_VFS_OP_READDIR, SMB_VFS_LAYER_TRANSPARENT},
588 {SMB_VFS_OP(cap_mkdir), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
589 {SMB_VFS_OP(cap_rmdir), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
591 /* File operations */
593 {SMB_VFS_OP(cap_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
594 {SMB_VFS_OP(cap_rename), SMB_VFS_OP_RENAME, SMB_VFS_LAYER_TRANSPARENT},
595 {SMB_VFS_OP(cap_stat), SMB_VFS_OP_STAT, SMB_VFS_LAYER_TRANSPARENT},
596 {SMB_VFS_OP(cap_lstat), SMB_VFS_OP_LSTAT, SMB_VFS_LAYER_TRANSPARENT},
597 {SMB_VFS_OP(cap_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
598 {SMB_VFS_OP(cap_chmod), SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_TRANSPARENT},
599 {SMB_VFS_OP(cap_chown), SMB_VFS_OP_CHOWN, SMB_VFS_LAYER_TRANSPARENT},
600 {SMB_VFS_OP(cap_lchown), SMB_VFS_OP_LCHOWN, SMB_VFS_LAYER_TRANSPARENT},
601 {SMB_VFS_OP(cap_chdir), SMB_VFS_OP_CHDIR, SMB_VFS_LAYER_TRANSPARENT},
602 {SMB_VFS_OP(cap_ntimes), SMB_VFS_OP_NTIMES, SMB_VFS_LAYER_TRANSPARENT},
603 {SMB_VFS_OP(cap_symlink), SMB_VFS_OP_SYMLINK, SMB_VFS_LAYER_TRANSPARENT},
604 {SMB_VFS_OP(cap_readlink), SMB_VFS_OP_READLINK, SMB_VFS_LAYER_TRANSPARENT},
605 {SMB_VFS_OP(cap_link), SMB_VFS_OP_LINK, SMB_VFS_LAYER_TRANSPARENT},
606 {SMB_VFS_OP(cap_mknod), SMB_VFS_OP_MKNOD, SMB_VFS_LAYER_TRANSPARENT},
607 {SMB_VFS_OP(cap_realpath), SMB_VFS_OP_REALPATH, SMB_VFS_LAYER_TRANSPARENT},
609 /* POSIX ACL operations */
611 {SMB_VFS_OP(cap_chmod_acl), SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_TRANSPARENT},
613 {SMB_VFS_OP(cap_sys_acl_get_file), SMB_VFS_OP_SYS_ACL_GET_FILE, SMB_VFS_LAYER_TRANSPARENT},
614 {SMB_VFS_OP(cap_sys_acl_set_file), SMB_VFS_OP_SYS_ACL_SET_FILE, SMB_VFS_LAYER_TRANSPARENT},
615 {SMB_VFS_OP(cap_sys_acl_delete_def_file), SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE, SMB_VFS_LAYER_TRANSPARENT},
618 {SMB_VFS_OP(cap_getxattr), SMB_VFS_OP_GETXATTR, SMB_VFS_LAYER_TRANSPARENT},
619 {SMB_VFS_OP(cap_lgetxattr), SMB_VFS_OP_LGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
620 {SMB_VFS_OP(cap_fgetxattr), SMB_VFS_OP_FGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
621 {SMB_VFS_OP(cap_listxattr), SMB_VFS_OP_LISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
622 {SMB_VFS_OP(cap_llistxattr), SMB_VFS_OP_LLISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
623 {SMB_VFS_OP(cap_removexattr), SMB_VFS_OP_REMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
624 {SMB_VFS_OP(cap_lremovexattr), SMB_VFS_OP_LREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
625 {SMB_VFS_OP(cap_fremovexattr), SMB_VFS_OP_FREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
626 {SMB_VFS_OP(cap_setxattr), SMB_VFS_OP_SETXATTR, SMB_VFS_LAYER_TRANSPARENT},
627 {SMB_VFS_OP(cap_lsetxattr), SMB_VFS_OP_LSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
628 {SMB_VFS_OP(cap_fsetxattr), SMB_VFS_OP_FSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
630 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
633 NTSTATUS vfs_cap_init(void);
634 NTSTATUS vfs_cap_init(void)
636 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
639 /* For CAP functions */
641 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
642 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
643 #define is_hex(s) ((s)[0] == hex_tag)
645 static unsigned char hex2bin_table[256] = {
646 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
647 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
648 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
649 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
650 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
651 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
652 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
653 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
654 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
655 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
656 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
657 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
658 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
659 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
660 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
661 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
662 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
663 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
665 static unsigned char bin2hex_table[256] = "0123456789abcdef";
667 /*******************************************************************
668 original code -> ":xx" - CAP format
669 ********************************************************************/
671 static char *capencode(TALLOC_CTX *ctx, const char *from)
678 for (p1 = from; *p1; p1++) {
679 if ((unsigned char)*p1 >= 0x80) {
687 to = TALLOC_ARRAY(ctx, char, len);
692 for (out = to; *from;) {
693 /* buffer husoku error */
694 if ((unsigned char)*from >= 0x80) {
696 *out++ = bin2hex (((*from)>>4)&0x0f);
697 *out++ = bin2hex ((*from)&0x0f);
707 /*******************************************************************
709 ********************************************************************/
710 /* ":xx" -> a byte */
712 static char *capdecode(TALLOC_CTX *ctx, const char *from)
719 for (p1 = from; *p1; len++) {
727 to = TALLOC_ARRAY(ctx, char, len);
732 for (out = to; *from;) {
734 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));