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, SMB_STRUCT_DIR *dirp)
58 SMB_STRUCT_DIRENT *result;
59 SMB_STRUCT_DIRENT *newdirent;
62 DEBUG(3,("cap: cap_readdir\n"));
64 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
69 newname = capdecode(talloc_tos(), result->d_name);
73 DEBUG(3,("cap: cap_readdir: %s\n", newname));
74 newnamelen = strlen(newname)+1;
75 newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
77 sizeof(SMB_STRUCT_DIRENT)+
82 memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
83 memcpy(&newdirent->d_name, newname, newnamelen);
87 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
89 char *cappath = capencode(talloc_tos(), path);
95 return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
98 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
100 char *cappath = capencode(talloc_tos(), path);
106 return SMB_VFS_NEXT_RMDIR(handle, cappath);
109 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
110 files_struct *fsp, int flags, mode_t mode)
113 char *tmp_base_name = NULL;
116 cappath = capencode(talloc_tos(), smb_fname->base_name);
123 tmp_base_name = smb_fname->base_name;
124 smb_fname->base_name = cappath;
126 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
127 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
129 smb_fname->base_name = tmp_base_name;
130 TALLOC_FREE(cappath);
135 static int cap_rename(vfs_handle_struct *handle, const char *oldname, const char *newname)
137 char *capold = capencode(talloc_tos(), oldname);
138 char *capnew = capencode(talloc_tos(), newname);
140 if (!capold || !capnew) {
144 return SMB_VFS_NEXT_RENAME(handle, capold, capnew);
147 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
150 char *tmp_base_name = NULL;
153 cappath = capencode(talloc_tos(), smb_fname->base_name);
160 tmp_base_name = smb_fname->base_name;
161 smb_fname->base_name = cappath;
163 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
165 smb_fname->base_name = tmp_base_name;
166 TALLOC_FREE(cappath);
171 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
174 char *tmp_base_name = NULL;
177 cappath = capencode(talloc_tos(), smb_fname->base_name);
184 tmp_base_name = smb_fname->base_name;
185 smb_fname->base_name = cappath;
187 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
189 smb_fname->base_name = tmp_base_name;
190 TALLOC_FREE(cappath);
195 static int cap_unlink(vfs_handle_struct *handle, const char *path)
197 char *cappath = capencode(talloc_tos(), path);
203 return SMB_VFS_NEXT_UNLINK(handle, cappath);
206 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
208 char *cappath = capencode(talloc_tos(), path);
214 return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
217 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
219 char *cappath = capencode(talloc_tos(), path);
225 return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
228 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
230 char *cappath = capencode(talloc_tos(), path);
236 return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
239 static int cap_chdir(vfs_handle_struct *handle, const char *path)
241 char *cappath = capencode(talloc_tos(), path);
247 DEBUG(3,("cap: cap_chdir for %s\n", path));
248 return SMB_VFS_NEXT_CHDIR(handle, cappath);
251 static int cap_ntimes(vfs_handle_struct *handle, const char *path,
252 struct smb_file_time *ft)
254 char *cappath = capencode(talloc_tos(), path);
260 return SMB_VFS_NEXT_NTIMES(handle, cappath, ft);
264 static bool cap_symlink(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
266 char *capold = capencode(talloc_tos(), oldpath);
267 char *capnew = capencode(talloc_tos(), newpath);
269 if (!capold || !capnew) {
273 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
276 static bool cap_readlink(vfs_handle_struct *handle, const char *path, char *buf, size_t bufsiz)
278 char *cappath = capencode(talloc_tos(), path);
284 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
287 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
289 char *capold = capencode(talloc_tos(), oldpath);
290 char *capnew = capencode(talloc_tos(), newpath);
292 if (!capold || !capnew) {
296 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
299 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
301 char *cappath = capencode(talloc_tos(), path);
307 return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
310 static char *cap_realpath(vfs_handle_struct *handle, const char *path, char *resolved_path)
312 /* monyo need capencode'ed and capdecode'ed? */
313 char *cappath = capencode(talloc_tos(), path);
319 return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
322 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
324 char *cappath = capencode(talloc_tos(), path);
326 /* If the underlying VFS doesn't have ACL support... */
327 if (!handle->vfs_next.ops.chmod_acl) {
335 return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
338 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T type)
340 char *cappath = capencode(talloc_tos(), path);
344 return (SMB_ACL_T)NULL;
346 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type);
349 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
351 char *cappath = capencode(talloc_tos(), path);
357 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
360 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
362 char *cappath = capencode(talloc_tos(), path);
368 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
371 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
373 char *cappath = capencode(talloc_tos(), path);
374 char *capname = capencode(talloc_tos(), name);
376 if (!cappath || !capname) {
380 return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
383 static ssize_t cap_lgetxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t
386 char *cappath = capencode(talloc_tos(), path);
387 char *capname = capencode(talloc_tos(), name);
389 if (!cappath || !capname) {
393 return SMB_VFS_NEXT_LGETXATTR(handle, cappath, capname, value, size);
396 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
398 char *cappath = capencode(talloc_tos(), path);
404 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
407 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
409 char *cappath = capencode(talloc_tos(), path);
415 return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
418 static ssize_t cap_llistxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
420 char *cappath = capencode(talloc_tos(), path);
426 return SMB_VFS_NEXT_LLISTXATTR(handle, cappath, list, size);
429 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
431 char *cappath = capencode(talloc_tos(), path);
432 char *capname = capencode(talloc_tos(), name);
434 if (!cappath || !capname) {
438 return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
441 static int cap_lremovexattr(vfs_handle_struct *handle, const char *path, const char *name)
443 char *cappath = capencode(talloc_tos(), path);
444 char *capname = capencode(talloc_tos(), name);
446 if (!cappath || !capname) {
450 return SMB_VFS_NEXT_LREMOVEXATTR(handle, cappath, capname);
453 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
455 char *cappath = capencode(talloc_tos(), path);
461 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
464 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
466 char *cappath = capencode(talloc_tos(), path);
467 char *capname = capencode(talloc_tos(), name);
469 if (!cappath || !capname) {
473 return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
476 static int cap_lsetxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
478 char *cappath = capencode(talloc_tos(), path);
479 char *capname = capencode(talloc_tos(), name);
481 if (!cappath || !capname) {
485 return SMB_VFS_NEXT_LSETXATTR(handle, cappath, capname, value, size, flags);
488 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
490 char *cappath = capencode(talloc_tos(), path);
496 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
499 /* VFS operations structure */
501 static vfs_op_tuple cap_op_tuples[] = {
503 /* Disk operations */
505 {SMB_VFS_OP(cap_disk_free), SMB_VFS_OP_DISK_FREE, SMB_VFS_LAYER_TRANSPARENT},
507 /* Directory operations */
509 {SMB_VFS_OP(cap_opendir), SMB_VFS_OP_OPENDIR, SMB_VFS_LAYER_TRANSPARENT},
510 {SMB_VFS_OP(cap_readdir), SMB_VFS_OP_READDIR, SMB_VFS_LAYER_TRANSPARENT},
511 {SMB_VFS_OP(cap_mkdir), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
512 {SMB_VFS_OP(cap_rmdir), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
514 /* File operations */
516 {SMB_VFS_OP(cap_open), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
517 {SMB_VFS_OP(cap_rename), SMB_VFS_OP_RENAME, SMB_VFS_LAYER_TRANSPARENT},
518 {SMB_VFS_OP(cap_stat), SMB_VFS_OP_STAT, SMB_VFS_LAYER_TRANSPARENT},
519 {SMB_VFS_OP(cap_lstat), SMB_VFS_OP_LSTAT, SMB_VFS_LAYER_TRANSPARENT},
520 {SMB_VFS_OP(cap_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
521 {SMB_VFS_OP(cap_chmod), SMB_VFS_OP_CHMOD, SMB_VFS_LAYER_TRANSPARENT},
522 {SMB_VFS_OP(cap_chown), SMB_VFS_OP_CHOWN, SMB_VFS_LAYER_TRANSPARENT},
523 {SMB_VFS_OP(cap_lchown), SMB_VFS_OP_LCHOWN, SMB_VFS_LAYER_TRANSPARENT},
524 {SMB_VFS_OP(cap_chdir), SMB_VFS_OP_CHDIR, SMB_VFS_LAYER_TRANSPARENT},
525 {SMB_VFS_OP(cap_ntimes), SMB_VFS_OP_NTIMES, SMB_VFS_LAYER_TRANSPARENT},
526 {SMB_VFS_OP(cap_symlink), SMB_VFS_OP_SYMLINK, SMB_VFS_LAYER_TRANSPARENT},
527 {SMB_VFS_OP(cap_readlink), SMB_VFS_OP_READLINK, SMB_VFS_LAYER_TRANSPARENT},
528 {SMB_VFS_OP(cap_link), SMB_VFS_OP_LINK, SMB_VFS_LAYER_TRANSPARENT},
529 {SMB_VFS_OP(cap_mknod), SMB_VFS_OP_MKNOD, SMB_VFS_LAYER_TRANSPARENT},
530 {SMB_VFS_OP(cap_realpath), SMB_VFS_OP_REALPATH, SMB_VFS_LAYER_TRANSPARENT},
532 /* POSIX ACL operations */
534 {SMB_VFS_OP(cap_chmod_acl), SMB_VFS_OP_CHMOD_ACL, SMB_VFS_LAYER_TRANSPARENT},
536 {SMB_VFS_OP(cap_sys_acl_get_file), SMB_VFS_OP_SYS_ACL_GET_FILE, SMB_VFS_LAYER_TRANSPARENT},
537 {SMB_VFS_OP(cap_sys_acl_set_file), SMB_VFS_OP_SYS_ACL_SET_FILE, SMB_VFS_LAYER_TRANSPARENT},
538 {SMB_VFS_OP(cap_sys_acl_delete_def_file), SMB_VFS_OP_SYS_ACL_DELETE_DEF_FILE, SMB_VFS_LAYER_TRANSPARENT},
541 {SMB_VFS_OP(cap_getxattr), SMB_VFS_OP_GETXATTR, SMB_VFS_LAYER_TRANSPARENT},
542 {SMB_VFS_OP(cap_lgetxattr), SMB_VFS_OP_LGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
543 {SMB_VFS_OP(cap_fgetxattr), SMB_VFS_OP_FGETXATTR, SMB_VFS_LAYER_TRANSPARENT},
544 {SMB_VFS_OP(cap_listxattr), SMB_VFS_OP_LISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
545 {SMB_VFS_OP(cap_llistxattr), SMB_VFS_OP_LLISTXATTR, SMB_VFS_LAYER_TRANSPARENT},
546 {SMB_VFS_OP(cap_removexattr), SMB_VFS_OP_REMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
547 {SMB_VFS_OP(cap_lremovexattr), SMB_VFS_OP_LREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
548 {SMB_VFS_OP(cap_fremovexattr), SMB_VFS_OP_FREMOVEXATTR, SMB_VFS_LAYER_TRANSPARENT},
549 {SMB_VFS_OP(cap_setxattr), SMB_VFS_OP_SETXATTR, SMB_VFS_LAYER_TRANSPARENT},
550 {SMB_VFS_OP(cap_lsetxattr), SMB_VFS_OP_LSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
551 {SMB_VFS_OP(cap_fsetxattr), SMB_VFS_OP_FSETXATTR, SMB_VFS_LAYER_TRANSPARENT},
553 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
556 NTSTATUS vfs_cap_init(void);
557 NTSTATUS vfs_cap_init(void)
559 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap", cap_op_tuples);
562 /* For CAP functions */
564 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
565 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
566 #define is_hex(s) ((s)[0] == hex_tag)
568 static unsigned char hex2bin_table[256] = {
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
572 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
573 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
574 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
576 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
577 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
588 static unsigned char bin2hex_table[256] = "0123456789abcdef";
590 /*******************************************************************
591 original code -> ":xx" - CAP format
592 ********************************************************************/
594 static char *capencode(TALLOC_CTX *ctx, const char *from)
601 for (p1 = from; *p1; p1++) {
602 if ((unsigned char)*p1 >= 0x80) {
610 to = TALLOC_ARRAY(ctx, char, len);
615 for (out = to; *from;) {
616 /* buffer husoku error */
617 if ((unsigned char)*from >= 0x80) {
619 *out++ = bin2hex (((*from)>>4)&0x0f);
620 *out++ = bin2hex ((*from)&0x0f);
630 /*******************************************************************
632 ********************************************************************/
633 /* ":xx" -> a byte */
635 static char *capdecode(TALLOC_CTX *ctx, const char *from)
642 for (p1 = from; *p1; len++) {
650 to = TALLOC_ARRAY(ctx, char, len);
655 for (out = to; *from;) {
657 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));