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 DIR *cap_opendir(vfs_handle_struct
*handle
, const char *fname
, const char *mask
, uint32_t attr
)
46 char *capname
= capencode(talloc_tos(), fname
);
52 return SMB_VFS_NEXT_OPENDIR(handle
, capname
, mask
, attr
);
55 static struct dirent
*cap_readdir(vfs_handle_struct
*handle
,
57 SMB_STRUCT_STAT
*sbuf
)
59 struct dirent
*result
;
60 struct dirent
*newdirent
;
63 DEBUG(3,("cap: cap_readdir\n"));
65 result
= SMB_VFS_NEXT_READDIR(handle
, dirp
, NULL
);
70 newname
= capdecode(talloc_tos(), result
->d_name
);
74 DEBUG(3,("cap: cap_readdir: %s\n", newname
));
75 newnamelen
= strlen(newname
)+1;
76 newdirent
= (struct dirent
*)talloc_array(talloc_tos(),
78 sizeof(struct dirent
)+
83 memcpy(newdirent
, result
, sizeof(struct dirent
));
84 memcpy(&newdirent
->d_name
, newname
, newnamelen
);
88 static int cap_mkdir(vfs_handle_struct
*handle
, const char *path
, mode_t mode
)
90 char *cappath
= capencode(talloc_tos(), path
);
96 return SMB_VFS_NEXT_MKDIR(handle
, cappath
, mode
);
99 static int cap_rmdir(vfs_handle_struct
*handle
, const char *path
)
101 char *cappath
= capencode(talloc_tos(), path
);
107 return SMB_VFS_NEXT_RMDIR(handle
, cappath
);
110 static int cap_open(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
,
111 files_struct
*fsp
, int flags
, mode_t mode
)
114 char *tmp_base_name
= NULL
;
117 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
124 tmp_base_name
= smb_fname
->base_name
;
125 smb_fname
->base_name
= cappath
;
127 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname
)));
128 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
130 smb_fname
->base_name
= tmp_base_name
;
131 TALLOC_FREE(cappath
);
136 static int cap_rename(vfs_handle_struct
*handle
,
137 const struct smb_filename
*smb_fname_src
,
138 const struct smb_filename
*smb_fname_dst
)
142 struct smb_filename
*smb_fname_src_tmp
= NULL
;
143 struct smb_filename
*smb_fname_dst_tmp
= NULL
;
146 capold
= capencode(talloc_tos(), smb_fname_src
->base_name
);
147 capnew
= capencode(talloc_tos(), smb_fname_dst
->base_name
);
148 if (!capold
|| !capnew
) {
153 /* Setup temporary smb_filename structs. */
154 smb_fname_src_tmp
= cp_smb_filename(talloc_tos(), smb_fname_src
);
155 if (smb_fname_src_tmp
== NULL
) {
159 smb_fname_dst_tmp
= cp_smb_filename(talloc_tos(), smb_fname_dst
);
160 if (smb_fname_dst_tmp
== NULL
) {
165 smb_fname_src_tmp
->base_name
= capold
;
166 smb_fname_dst_tmp
->base_name
= capnew
;
168 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src_tmp
,
173 TALLOC_FREE(smb_fname_src_tmp
);
174 TALLOC_FREE(smb_fname_dst_tmp
);
179 static int cap_stat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
182 char *tmp_base_name
= NULL
;
185 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
192 tmp_base_name
= smb_fname
->base_name
;
193 smb_fname
->base_name
= cappath
;
195 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
197 smb_fname
->base_name
= tmp_base_name
;
198 TALLOC_FREE(cappath
);
203 static int cap_lstat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
206 char *tmp_base_name
= NULL
;
209 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
216 tmp_base_name
= smb_fname
->base_name
;
217 smb_fname
->base_name
= cappath
;
219 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
221 smb_fname
->base_name
= tmp_base_name
;
222 TALLOC_FREE(cappath
);
227 static int cap_unlink(vfs_handle_struct
*handle
,
228 const struct smb_filename
*smb_fname
)
230 struct smb_filename
*smb_fname_tmp
= NULL
;
231 char *cappath
= NULL
;
234 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
240 /* Setup temporary smb_filename structs. */
241 smb_fname_tmp
= cp_smb_filename(talloc_tos(), smb_fname
);
242 if (smb_fname_tmp
== NULL
) {
247 smb_fname_tmp
->base_name
= cappath
;
249 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
251 TALLOC_FREE(smb_fname_tmp
);
255 static int cap_chmod(vfs_handle_struct
*handle
, const char *path
, mode_t mode
)
257 char *cappath
= capencode(talloc_tos(), path
);
263 return SMB_VFS_NEXT_CHMOD(handle
, cappath
, mode
);
266 static int cap_chown(vfs_handle_struct
*handle
, const char *path
, uid_t uid
, gid_t gid
)
268 char *cappath
= capencode(talloc_tos(), path
);
274 return SMB_VFS_NEXT_CHOWN(handle
, cappath
, uid
, gid
);
277 static int cap_lchown(vfs_handle_struct
*handle
, const char *path
, uid_t uid
, gid_t gid
)
279 char *cappath
= capencode(talloc_tos(), path
);
285 return SMB_VFS_NEXT_LCHOWN(handle
, cappath
, uid
, gid
);
288 static int cap_chdir(vfs_handle_struct
*handle
, const char *path
)
290 char *cappath
= capencode(talloc_tos(), path
);
296 DEBUG(3,("cap: cap_chdir for %s\n", path
));
297 return SMB_VFS_NEXT_CHDIR(handle
, cappath
);
300 static int cap_ntimes(vfs_handle_struct
*handle
,
301 const struct smb_filename
*smb_fname
,
302 struct smb_file_time
*ft
)
304 struct smb_filename
*smb_fname_tmp
= NULL
;
305 char *cappath
= NULL
;
308 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
315 /* Setup temporary smb_filename structs. */
316 smb_fname_tmp
= cp_smb_filename(talloc_tos(), smb_fname
);
317 if (smb_fname_tmp
== NULL
) {
322 smb_fname_tmp
->base_name
= cappath
;
324 ret
= SMB_VFS_NEXT_NTIMES(handle
, smb_fname_tmp
, ft
);
326 TALLOC_FREE(smb_fname_tmp
);
331 static int cap_symlink(vfs_handle_struct
*handle
, const char *oldpath
,
334 char *capold
= capencode(talloc_tos(), oldpath
);
335 char *capnew
= capencode(talloc_tos(), newpath
);
337 if (!capold
|| !capnew
) {
341 return SMB_VFS_NEXT_SYMLINK(handle
, capold
, capnew
);
344 static int cap_readlink(vfs_handle_struct
*handle
, const char *path
,
345 char *buf
, size_t bufsiz
)
347 char *cappath
= capencode(talloc_tos(), path
);
353 return SMB_VFS_NEXT_READLINK(handle
, cappath
, buf
, bufsiz
);
356 static int cap_link(vfs_handle_struct
*handle
, const char *oldpath
, const char *newpath
)
358 char *capold
= capencode(talloc_tos(), oldpath
);
359 char *capnew
= capencode(talloc_tos(), newpath
);
361 if (!capold
|| !capnew
) {
365 return SMB_VFS_NEXT_LINK(handle
, capold
, capnew
);
368 static int cap_mknod(vfs_handle_struct
*handle
, const char *path
, mode_t mode
, SMB_DEV_T dev
)
370 char *cappath
= capencode(talloc_tos(), path
);
376 return SMB_VFS_NEXT_MKNOD(handle
, cappath
, mode
, dev
);
379 static char *cap_realpath(vfs_handle_struct
*handle
, const char *path
)
381 /* monyo need capencode'ed and capdecode'ed? */
382 char *cappath
= capencode(talloc_tos(), path
);
388 return SMB_VFS_NEXT_REALPATH(handle
, cappath
);
391 static int cap_chmod_acl(vfs_handle_struct
*handle
, const char *path
, mode_t mode
)
393 char *cappath
= capencode(talloc_tos(), path
);
395 /* If the underlying VFS doesn't have ACL support... */
400 return SMB_VFS_NEXT_CHMOD_ACL(handle
, cappath
, mode
);
403 static SMB_ACL_T
cap_sys_acl_get_file(vfs_handle_struct
*handle
,
404 const char *path
, SMB_ACL_TYPE_T type
,
407 char *cappath
= capencode(talloc_tos(), path
);
411 return (SMB_ACL_T
)NULL
;
413 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle
, cappath
, type
, mem_ctx
);
416 static int cap_sys_acl_set_file(vfs_handle_struct
*handle
, const char *path
, SMB_ACL_TYPE_T acltype
, SMB_ACL_T theacl
)
418 char *cappath
= capencode(talloc_tos(), path
);
424 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle
, cappath
, acltype
, theacl
);
427 static int cap_sys_acl_delete_def_file(vfs_handle_struct
*handle
, const char *path
)
429 char *cappath
= capencode(talloc_tos(), path
);
435 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle
, cappath
);
438 static ssize_t
cap_getxattr(vfs_handle_struct
*handle
, const char *path
, const char *name
, void *value
, size_t size
)
440 char *cappath
= capencode(talloc_tos(), path
);
441 char *capname
= capencode(talloc_tos(), name
);
443 if (!cappath
|| !capname
) {
447 return SMB_VFS_NEXT_GETXATTR(handle
, cappath
, capname
, value
, size
);
450 static ssize_t
cap_fgetxattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
, void *value
, size_t size
)
452 char *cappath
= capencode(talloc_tos(), path
);
458 return SMB_VFS_NEXT_FGETXATTR(handle
, fsp
, cappath
, value
, size
);
461 static ssize_t
cap_listxattr(vfs_handle_struct
*handle
, const char *path
, char *list
, size_t size
)
463 char *cappath
= capencode(talloc_tos(), path
);
469 return SMB_VFS_NEXT_LISTXATTR(handle
, cappath
, list
, size
);
472 static int cap_removexattr(vfs_handle_struct
*handle
, const char *path
, const char *name
)
474 char *cappath
= capencode(talloc_tos(), path
);
475 char *capname
= capencode(talloc_tos(), name
);
477 if (!cappath
|| !capname
) {
481 return SMB_VFS_NEXT_REMOVEXATTR(handle
, cappath
, capname
);
484 static int cap_fremovexattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
)
486 char *cappath
= capencode(talloc_tos(), path
);
492 return SMB_VFS_NEXT_FREMOVEXATTR(handle
, fsp
, cappath
);
495 static int cap_setxattr(vfs_handle_struct
*handle
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
497 char *cappath
= capencode(talloc_tos(), path
);
498 char *capname
= capencode(talloc_tos(), name
);
500 if (!cappath
|| !capname
) {
504 return SMB_VFS_NEXT_SETXATTR(handle
, cappath
, capname
, value
, size
, flags
);
507 static int cap_fsetxattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
, const void *value
, size_t size
, int flags
)
509 char *cappath
= capencode(talloc_tos(), path
);
515 return SMB_VFS_NEXT_FSETXATTR(handle
, fsp
, cappath
, value
, size
, flags
);
518 static struct vfs_fn_pointers vfs_cap_fns
= {
519 .disk_free_fn
= cap_disk_free
,
520 .opendir_fn
= cap_opendir
,
521 .readdir_fn
= cap_readdir
,
522 .mkdir_fn
= cap_mkdir
,
523 .rmdir_fn
= cap_rmdir
,
525 .rename_fn
= cap_rename
,
527 .lstat_fn
= cap_lstat
,
528 .unlink_fn
= cap_unlink
,
529 .chmod_fn
= cap_chmod
,
530 .chown_fn
= cap_chown
,
531 .lchown_fn
= cap_lchown
,
532 .chdir_fn
= cap_chdir
,
533 .ntimes_fn
= cap_ntimes
,
534 .symlink_fn
= cap_symlink
,
535 .readlink_fn
= cap_readlink
,
537 .mknod_fn
= cap_mknod
,
538 .realpath_fn
= cap_realpath
,
539 .chmod_acl_fn
= cap_chmod_acl
,
540 .sys_acl_get_file_fn
= cap_sys_acl_get_file
,
541 .sys_acl_set_file_fn
= cap_sys_acl_set_file
,
542 .sys_acl_delete_def_file_fn
= cap_sys_acl_delete_def_file
,
543 .getxattr_fn
= cap_getxattr
,
544 .fgetxattr_fn
= cap_fgetxattr
,
545 .listxattr_fn
= cap_listxattr
,
546 .removexattr_fn
= cap_removexattr
,
547 .fremovexattr_fn
= cap_fremovexattr
,
548 .setxattr_fn
= cap_setxattr
,
549 .fsetxattr_fn
= cap_fsetxattr
552 NTSTATUS
vfs_cap_init(void);
553 NTSTATUS
vfs_cap_init(void)
555 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "cap",
559 /* For CAP functions */
561 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
562 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
563 #define is_hex(s) ((s)[0] == hex_tag)
565 static unsigned char hex2bin_table
[256] = {
566 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
567 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
569 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
570 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
571 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
573 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
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, /* 0x70 */
576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
585 static unsigned char bin2hex_table
[256] = "0123456789abcdef";
587 /*******************************************************************
588 original code -> ":xx" - CAP format
589 ********************************************************************/
591 static char *capencode(TALLOC_CTX
*ctx
, const char *from
)
598 for (p1
= from
; *p1
; p1
++) {
599 if ((unsigned char)*p1
>= 0x80) {
607 to
= talloc_array(ctx
, char, len
);
612 for (out
= to
; *from
;) {
613 /* buffer husoku error */
614 if ((unsigned char)*from
>= 0x80) {
616 *out
++ = bin2hex (((*from
)>>4)&0x0f);
617 *out
++ = bin2hex ((*from
)&0x0f);
627 /*******************************************************************
629 ********************************************************************/
630 /* ":xx" -> a byte */
632 static char *capdecode(TALLOC_CTX
*ctx
, const char *from
)
639 for (p1
= from
; *p1
; len
++) {
648 to
= talloc_array(ctx
, char, len
);
653 for (out
= to
; *from
;) {
655 *out
++ = (hex2bin(from
[1])<<4) | (hex2bin(from
[2]));