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 bool small_query, uint64_t *bsize,
34 uint64_t *dfree, uint64_t *dsize)
36 char *cappath = capencode(talloc_tos(), path);
42 return SMB_VFS_NEXT_DISK_FREE(handle, cappath, small_query, bsize,
46 static DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
48 char *capname = capencode(talloc_tos(), fname);
54 return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
57 static struct dirent *cap_readdir(vfs_handle_struct *handle,
59 SMB_STRUCT_STAT *sbuf)
61 struct dirent *result;
62 struct dirent *newdirent;
65 DEBUG(3,("cap: cap_readdir\n"));
67 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
72 newname = capdecode(talloc_tos(), result->d_name);
76 DEBUG(3,("cap: cap_readdir: %s\n", newname));
77 newnamelen = strlen(newname)+1;
78 newdirent = (struct dirent *)talloc_array(talloc_tos(),
80 sizeof(struct dirent)+
85 memcpy(newdirent, result, sizeof(struct dirent));
86 memcpy(&newdirent->d_name, newname, newnamelen);
90 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
92 char *cappath = capencode(talloc_tos(), path);
98 return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
101 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
103 char *cappath = capencode(talloc_tos(), path);
109 return SMB_VFS_NEXT_RMDIR(handle, cappath);
112 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
113 files_struct *fsp, int flags, mode_t mode)
116 char *tmp_base_name = NULL;
119 cappath = capencode(talloc_tos(), smb_fname->base_name);
126 tmp_base_name = smb_fname->base_name;
127 smb_fname->base_name = cappath;
129 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
130 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
132 smb_fname->base_name = tmp_base_name;
133 TALLOC_FREE(cappath);
138 static int cap_rename(vfs_handle_struct *handle,
139 const struct smb_filename *smb_fname_src,
140 const struct smb_filename *smb_fname_dst)
144 struct smb_filename *smb_fname_src_tmp = NULL;
145 struct smb_filename *smb_fname_dst_tmp = NULL;
149 capold = capencode(talloc_tos(), smb_fname_src->base_name);
150 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
151 if (!capold || !capnew) {
156 /* Setup temporary smb_filename structs. */
157 status = copy_smb_filename(talloc_tos(), smb_fname_src,
159 if (!NT_STATUS_IS_OK(status)) {
160 errno = map_errno_from_nt_status(status);
163 status = copy_smb_filename(talloc_tos(), smb_fname_dst,
165 if (!NT_STATUS_IS_OK(status)) {
166 errno = map_errno_from_nt_status(status);
170 smb_fname_src_tmp->base_name = capold;
171 smb_fname_dst_tmp->base_name = capnew;
173 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
178 TALLOC_FREE(smb_fname_src_tmp);
179 TALLOC_FREE(smb_fname_dst_tmp);
184 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
187 char *tmp_base_name = NULL;
190 cappath = capencode(talloc_tos(), smb_fname->base_name);
197 tmp_base_name = smb_fname->base_name;
198 smb_fname->base_name = cappath;
200 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
202 smb_fname->base_name = tmp_base_name;
203 TALLOC_FREE(cappath);
208 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
211 char *tmp_base_name = NULL;
214 cappath = capencode(talloc_tos(), smb_fname->base_name);
221 tmp_base_name = smb_fname->base_name;
222 smb_fname->base_name = cappath;
224 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
226 smb_fname->base_name = tmp_base_name;
227 TALLOC_FREE(cappath);
232 static int cap_unlink(vfs_handle_struct *handle,
233 const struct smb_filename *smb_fname)
235 struct smb_filename *smb_fname_tmp = NULL;
236 char *cappath = NULL;
240 cappath = capencode(talloc_tos(), smb_fname->base_name);
246 /* Setup temporary smb_filename structs. */
247 status = copy_smb_filename(talloc_tos(), smb_fname,
249 if (!NT_STATUS_IS_OK(status)) {
250 errno = map_errno_from_nt_status(status);
254 smb_fname_tmp->base_name = cappath;
256 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
258 TALLOC_FREE(smb_fname_tmp);
262 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
264 char *cappath = capencode(talloc_tos(), path);
270 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
273 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
275 char *cappath = capencode(talloc_tos(), path);
281 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
284 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
286 char *cappath = capencode(talloc_tos(), path);
292 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
295 static int cap_chdir(vfs_handle_struct *handle, const char *path)
297 char *cappath = capencode(talloc_tos(), path);
303 DEBUG(3,("cap: cap_chdir for %s\n", path));
304 return SMB_VFS_NEXT_CHDIR(handle, cappath);
307 static int cap_ntimes(vfs_handle_struct *handle,
308 const struct smb_filename *smb_fname,
309 struct smb_file_time *ft)
311 struct smb_filename *smb_fname_tmp = NULL;
312 char *cappath = NULL;
316 cappath = capencode(talloc_tos(), smb_fname->base_name);
323 /* Setup temporary smb_filename structs. */
324 status = copy_smb_filename(talloc_tos(), smb_fname,
326 if (!NT_STATUS_IS_OK(status)) {
327 errno = map_errno_from_nt_status(status);
331 smb_fname_tmp->base_name = cappath;
333 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
335 TALLOC_FREE(smb_fname_tmp);
340 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
343 char *capold = capencode(talloc_tos(), oldpath);
344 char *capnew = capencode(talloc_tos(), newpath);
346 if (!capold || !capnew) {
350 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
353 static int cap_readlink(vfs_handle_struct *handle, const char *path,
354 char *buf, size_t bufsiz)
356 char *cappath = capencode(talloc_tos(), path);
362 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
365 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
367 char *capold = capencode(talloc_tos(), oldpath);
368 char *capnew = capencode(talloc_tos(), newpath);
370 if (!capold || !capnew) {
374 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
377 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
379 char *cappath = capencode(talloc_tos(), path);
385 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
388 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
390 /* monyo need capencode'ed and capdecode'ed? */
391 char *cappath = capencode(talloc_tos(), path);
397 return SMB_VFS_NEXT_REALPATH(handle, cappath);
400 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
402 char *cappath = capencode(talloc_tos(), path);
404 /* If the underlying VFS doesn't have ACL support... */
409 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
412 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
413 const char *path, SMB_ACL_TYPE_T type,
416 char *cappath = capencode(talloc_tos(), path);
420 return (SMB_ACL_T)NULL;
422 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
425 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
427 char *cappath = capencode(talloc_tos(), path);
433 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
436 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
438 char *cappath = capencode(talloc_tos(), path);
444 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
447 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
449 char *cappath = capencode(talloc_tos(), path);
450 char *capname = capencode(talloc_tos(), name);
452 if (!cappath || !capname) {
456 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
459 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
461 char *cappath = capencode(talloc_tos(), path);
467 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
470 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
472 char *cappath = capencode(talloc_tos(), path);
478 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
481 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
483 char *cappath = capencode(talloc_tos(), path);
484 char *capname = capencode(talloc_tos(), name);
486 if (!cappath || !capname) {
490 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
493 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
495 char *cappath = capencode(talloc_tos(), path);
501 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
504 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
506 char *cappath = capencode(talloc_tos(), path);
507 char *capname = capencode(talloc_tos(), name);
509 if (!cappath || !capname) {
513 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
516 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
518 char *cappath = capencode(talloc_tos(), path);
524 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
527 static struct vfs_fn_pointers vfs_cap_fns = {
528 .disk_free_fn = cap_disk_free,
529 .opendir_fn = cap_opendir,
530 .readdir_fn = cap_readdir,
531 .mkdir_fn = cap_mkdir,
532 .rmdir_fn = cap_rmdir,
534 .rename_fn = cap_rename,
536 .lstat_fn = cap_lstat,
537 .unlink_fn = cap_unlink,
538 .chmod_fn = cap_chmod,
539 .chown_fn = cap_chown,
540 .lchown_fn = cap_lchown,
541 .chdir_fn = cap_chdir,
542 .ntimes_fn = cap_ntimes,
543 .symlink_fn = cap_symlink,
544 .readlink_fn = cap_readlink,
546 .mknod_fn = cap_mknod,
547 .realpath_fn = cap_realpath,
548 .chmod_acl_fn = cap_chmod_acl,
549 .sys_acl_get_file_fn = cap_sys_acl_get_file,
550 .sys_acl_set_file_fn = cap_sys_acl_set_file,
551 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
552 .getxattr_fn = cap_getxattr,
553 .fgetxattr_fn = cap_fgetxattr,
554 .listxattr_fn = cap_listxattr,
555 .removexattr_fn = cap_removexattr,
556 .fremovexattr_fn = cap_fremovexattr,
557 .setxattr_fn = cap_setxattr,
558 .fsetxattr_fn = cap_fsetxattr
561 NTSTATUS vfs_cap_init(void);
562 NTSTATUS vfs_cap_init(void)
564 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
568 /* For CAP functions */
570 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
571 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
572 #define is_hex(s) ((s)[0] == hex_tag)
574 static unsigned char hex2bin_table[256] = {
575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
578 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
579 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
580 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
582 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
583 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
594 static unsigned char bin2hex_table[256] = "0123456789abcdef";
596 /*******************************************************************
597 original code -> ":xx" - CAP format
598 ********************************************************************/
600 static char *capencode(TALLOC_CTX *ctx, const char *from)
607 for (p1 = from; *p1; p1++) {
608 if ((unsigned char)*p1 >= 0x80) {
616 to = talloc_array(ctx, char, len);
621 for (out = to; *from;) {
622 /* buffer husoku error */
623 if ((unsigned char)*from >= 0x80) {
625 *out++ = bin2hex (((*from)>>4)&0x0f);
626 *out++ = bin2hex ((*from)&0x0f);
636 /*******************************************************************
638 ********************************************************************/
639 /* ":xx" -> a byte */
641 static char *capdecode(TALLOC_CTX *ctx, const char *from)
648 for (p1 = from; *p1; len++) {
657 to = talloc_array(ctx, char, len);
662 for (out = to; *from;) {
664 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));