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, const char *path, mode_t mode)
102 char *cappath = capencode(talloc_tos(), path);
108 return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
111 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
113 char *cappath = capencode(talloc_tos(), path);
119 return SMB_VFS_NEXT_RMDIR(handle, cappath);
122 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
123 files_struct *fsp, int flags, mode_t mode)
126 char *tmp_base_name = NULL;
129 cappath = capencode(talloc_tos(), smb_fname->base_name);
136 tmp_base_name = smb_fname->base_name;
137 smb_fname->base_name = cappath;
139 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
140 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
142 smb_fname->base_name = tmp_base_name;
143 TALLOC_FREE(cappath);
148 static int cap_rename(vfs_handle_struct *handle,
149 const struct smb_filename *smb_fname_src,
150 const struct smb_filename *smb_fname_dst)
154 struct smb_filename *smb_fname_src_tmp = NULL;
155 struct smb_filename *smb_fname_dst_tmp = NULL;
158 capold = capencode(talloc_tos(), smb_fname_src->base_name);
159 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
160 if (!capold || !capnew) {
165 /* Setup temporary smb_filename structs. */
166 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
167 if (smb_fname_src_tmp == NULL) {
171 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
172 if (smb_fname_dst_tmp == NULL) {
177 smb_fname_src_tmp->base_name = capold;
178 smb_fname_dst_tmp->base_name = capnew;
180 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
185 TALLOC_FREE(smb_fname_src_tmp);
186 TALLOC_FREE(smb_fname_dst_tmp);
191 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
194 char *tmp_base_name = NULL;
197 cappath = capencode(talloc_tos(), smb_fname->base_name);
204 tmp_base_name = smb_fname->base_name;
205 smb_fname->base_name = cappath;
207 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
209 smb_fname->base_name = tmp_base_name;
210 TALLOC_FREE(cappath);
215 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
218 char *tmp_base_name = NULL;
221 cappath = capencode(talloc_tos(), smb_fname->base_name);
228 tmp_base_name = smb_fname->base_name;
229 smb_fname->base_name = cappath;
231 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
233 smb_fname->base_name = tmp_base_name;
234 TALLOC_FREE(cappath);
239 static int cap_unlink(vfs_handle_struct *handle,
240 const struct smb_filename *smb_fname)
242 struct smb_filename *smb_fname_tmp = NULL;
243 char *cappath = NULL;
246 cappath = capencode(talloc_tos(), smb_fname->base_name);
252 /* Setup temporary smb_filename structs. */
253 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
254 if (smb_fname_tmp == NULL) {
259 smb_fname_tmp->base_name = cappath;
261 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
263 TALLOC_FREE(smb_fname_tmp);
267 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
269 char *cappath = capencode(talloc_tos(), path);
275 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
278 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
280 char *cappath = capencode(talloc_tos(), path);
286 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
289 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
291 char *cappath = capencode(talloc_tos(), path);
297 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
300 static int cap_chdir(vfs_handle_struct *handle, const char *path)
302 char *cappath = capencode(talloc_tos(), path);
308 DEBUG(3,("cap: cap_chdir for %s\n", path));
309 return SMB_VFS_NEXT_CHDIR(handle, cappath);
312 static int cap_ntimes(vfs_handle_struct *handle,
313 const struct smb_filename *smb_fname,
314 struct smb_file_time *ft)
316 struct smb_filename *smb_fname_tmp = NULL;
317 char *cappath = NULL;
320 cappath = capencode(talloc_tos(), smb_fname->base_name);
327 /* Setup temporary smb_filename structs. */
328 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
329 if (smb_fname_tmp == NULL) {
334 smb_fname_tmp->base_name = cappath;
336 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
338 TALLOC_FREE(smb_fname_tmp);
343 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
346 char *capold = capencode(talloc_tos(), oldpath);
347 char *capnew = capencode(talloc_tos(), newpath);
349 if (!capold || !capnew) {
353 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
356 static int cap_readlink(vfs_handle_struct *handle, const char *path,
357 char *buf, size_t bufsiz)
359 char *cappath = capencode(talloc_tos(), path);
365 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
368 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
370 char *capold = capencode(talloc_tos(), oldpath);
371 char *capnew = capencode(talloc_tos(), newpath);
373 if (!capold || !capnew) {
377 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
380 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
382 char *cappath = capencode(talloc_tos(), path);
388 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
391 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
393 /* monyo need capencode'ed and capdecode'ed? */
394 char *cappath = capencode(talloc_tos(), path);
400 return SMB_VFS_NEXT_REALPATH(handle, cappath);
403 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
405 char *cappath = capencode(talloc_tos(), path);
407 /* If the underlying VFS doesn't have ACL support... */
412 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
415 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
416 const char *path, SMB_ACL_TYPE_T type,
419 char *cappath = capencode(talloc_tos(), path);
423 return (SMB_ACL_T)NULL;
425 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
428 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
430 char *cappath = capencode(talloc_tos(), path);
436 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
439 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
441 char *cappath = capencode(talloc_tos(), path);
447 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
450 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
452 char *cappath = capencode(talloc_tos(), path);
453 char *capname = capencode(talloc_tos(), name);
455 if (!cappath || !capname) {
459 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
462 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
464 char *cappath = capencode(talloc_tos(), path);
470 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
473 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
475 char *cappath = capencode(talloc_tos(), path);
481 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
484 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
486 char *cappath = capencode(talloc_tos(), path);
487 char *capname = capencode(talloc_tos(), name);
489 if (!cappath || !capname) {
493 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
496 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
498 char *cappath = capencode(talloc_tos(), path);
504 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
507 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
509 char *cappath = capencode(talloc_tos(), path);
510 char *capname = capencode(talloc_tos(), name);
512 if (!cappath || !capname) {
516 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
519 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
521 char *cappath = capencode(talloc_tos(), path);
527 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
530 static struct vfs_fn_pointers vfs_cap_fns = {
531 .disk_free_fn = cap_disk_free,
532 .get_quota_fn = cap_get_quota,
533 .opendir_fn = cap_opendir,
534 .readdir_fn = cap_readdir,
535 .mkdir_fn = cap_mkdir,
536 .rmdir_fn = cap_rmdir,
538 .rename_fn = cap_rename,
540 .lstat_fn = cap_lstat,
541 .unlink_fn = cap_unlink,
542 .chmod_fn = cap_chmod,
543 .chown_fn = cap_chown,
544 .lchown_fn = cap_lchown,
545 .chdir_fn = cap_chdir,
546 .ntimes_fn = cap_ntimes,
547 .symlink_fn = cap_symlink,
548 .readlink_fn = cap_readlink,
550 .mknod_fn = cap_mknod,
551 .realpath_fn = cap_realpath,
552 .chmod_acl_fn = cap_chmod_acl,
553 .sys_acl_get_file_fn = cap_sys_acl_get_file,
554 .sys_acl_set_file_fn = cap_sys_acl_set_file,
555 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
556 .getxattr_fn = cap_getxattr,
557 .fgetxattr_fn = cap_fgetxattr,
558 .listxattr_fn = cap_listxattr,
559 .removexattr_fn = cap_removexattr,
560 .fremovexattr_fn = cap_fremovexattr,
561 .setxattr_fn = cap_setxattr,
562 .fsetxattr_fn = cap_fsetxattr
565 NTSTATUS vfs_cap_init(void);
566 NTSTATUS vfs_cap_init(void)
568 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
572 /* For CAP functions */
574 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
575 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
576 #define is_hex(s) ((s)[0] == hex_tag)
578 static unsigned char hex2bin_table[256] = {
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
582 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
583 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
584 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
586 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
587 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
594 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
595 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
596 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
598 static unsigned char bin2hex_table[256] = "0123456789abcdef";
600 /*******************************************************************
601 original code -> ":xx" - CAP format
602 ********************************************************************/
604 static char *capencode(TALLOC_CTX *ctx, const char *from)
611 for (p1 = from; *p1; p1++) {
612 if ((unsigned char)*p1 >= 0x80) {
620 to = talloc_array(ctx, char, len);
625 for (out = to; *from;) {
626 /* buffer husoku error */
627 if ((unsigned char)*from >= 0x80) {
629 *out++ = bin2hex (((*from)>>4)&0x0f);
630 *out++ = bin2hex ((*from)&0x0f);
640 /*******************************************************************
642 ********************************************************************/
643 /* ":xx" -> a byte */
645 static char *capdecode(TALLOC_CTX *ctx, const char *from)
652 for (p1 = from; *p1; len++) {
661 to = talloc_array(ctx, char, len);
666 for (out = to; *from;) {
668 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));