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
;
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 smb_fname_src_tmp
= cp_smb_filename(talloc_tos(), smb_fname_src
);
157 if (smb_fname_src_tmp
== NULL
) {
161 smb_fname_dst_tmp
= cp_smb_filename(talloc_tos(), smb_fname_dst
);
162 if (smb_fname_dst_tmp
== NULL
) {
167 smb_fname_src_tmp
->base_name
= capold
;
168 smb_fname_dst_tmp
->base_name
= capnew
;
170 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src_tmp
,
175 TALLOC_FREE(smb_fname_src_tmp
);
176 TALLOC_FREE(smb_fname_dst_tmp
);
181 static int cap_stat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
184 char *tmp_base_name
= NULL
;
187 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
194 tmp_base_name
= smb_fname
->base_name
;
195 smb_fname
->base_name
= cappath
;
197 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
199 smb_fname
->base_name
= tmp_base_name
;
200 TALLOC_FREE(cappath
);
205 static int cap_lstat(vfs_handle_struct
*handle
, struct smb_filename
*smb_fname
)
208 char *tmp_base_name
= NULL
;
211 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
218 tmp_base_name
= smb_fname
->base_name
;
219 smb_fname
->base_name
= cappath
;
221 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
223 smb_fname
->base_name
= tmp_base_name
;
224 TALLOC_FREE(cappath
);
229 static int cap_unlink(vfs_handle_struct
*handle
,
230 const struct smb_filename
*smb_fname
)
232 struct smb_filename
*smb_fname_tmp
= NULL
;
233 char *cappath
= NULL
;
236 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
242 /* Setup temporary smb_filename structs. */
243 smb_fname_tmp
= cp_smb_filename(talloc_tos(), smb_fname
);
244 if (smb_fname_tmp
== NULL
) {
249 smb_fname_tmp
->base_name
= cappath
;
251 ret
= SMB_VFS_NEXT_UNLINK(handle
, smb_fname_tmp
);
253 TALLOC_FREE(smb_fname_tmp
);
257 static int cap_chmod(vfs_handle_struct
*handle
, const char *path
, mode_t mode
)
259 char *cappath
= capencode(talloc_tos(), path
);
265 return SMB_VFS_NEXT_CHMOD(handle
, cappath
, mode
);
268 static int cap_chown(vfs_handle_struct
*handle
, const char *path
, uid_t uid
, gid_t gid
)
270 char *cappath
= capencode(talloc_tos(), path
);
276 return SMB_VFS_NEXT_CHOWN(handle
, cappath
, uid
, gid
);
279 static int cap_lchown(vfs_handle_struct
*handle
, const char *path
, uid_t uid
, gid_t gid
)
281 char *cappath
= capencode(talloc_tos(), path
);
287 return SMB_VFS_NEXT_LCHOWN(handle
, cappath
, uid
, gid
);
290 static int cap_chdir(vfs_handle_struct
*handle
, const char *path
)
292 char *cappath
= capencode(talloc_tos(), path
);
298 DEBUG(3,("cap: cap_chdir for %s\n", path
));
299 return SMB_VFS_NEXT_CHDIR(handle
, cappath
);
302 static int cap_ntimes(vfs_handle_struct
*handle
,
303 const struct smb_filename
*smb_fname
,
304 struct smb_file_time
*ft
)
306 struct smb_filename
*smb_fname_tmp
= NULL
;
307 char *cappath
= NULL
;
310 cappath
= capencode(talloc_tos(), smb_fname
->base_name
);
317 /* Setup temporary smb_filename structs. */
318 smb_fname_tmp
= cp_smb_filename(talloc_tos(), smb_fname
);
319 if (smb_fname_tmp
== NULL
) {
324 smb_fname_tmp
->base_name
= cappath
;
326 ret
= SMB_VFS_NEXT_NTIMES(handle
, smb_fname_tmp
, ft
);
328 TALLOC_FREE(smb_fname_tmp
);
333 static int cap_symlink(vfs_handle_struct
*handle
, const char *oldpath
,
336 char *capold
= capencode(talloc_tos(), oldpath
);
337 char *capnew
= capencode(talloc_tos(), newpath
);
339 if (!capold
|| !capnew
) {
343 return SMB_VFS_NEXT_SYMLINK(handle
, capold
, capnew
);
346 static int cap_readlink(vfs_handle_struct
*handle
, const char *path
,
347 char *buf
, size_t bufsiz
)
349 char *cappath
= capencode(talloc_tos(), path
);
355 return SMB_VFS_NEXT_READLINK(handle
, cappath
, buf
, bufsiz
);
358 static int cap_link(vfs_handle_struct
*handle
, const char *oldpath
, const char *newpath
)
360 char *capold
= capencode(talloc_tos(), oldpath
);
361 char *capnew
= capencode(talloc_tos(), newpath
);
363 if (!capold
|| !capnew
) {
367 return SMB_VFS_NEXT_LINK(handle
, capold
, capnew
);
370 static int cap_mknod(vfs_handle_struct
*handle
, const char *path
, mode_t mode
, SMB_DEV_T dev
)
372 char *cappath
= capencode(talloc_tos(), path
);
378 return SMB_VFS_NEXT_MKNOD(handle
, cappath
, mode
, dev
);
381 static char *cap_realpath(vfs_handle_struct
*handle
, const char *path
)
383 /* monyo need capencode'ed and capdecode'ed? */
384 char *cappath
= capencode(talloc_tos(), path
);
390 return SMB_VFS_NEXT_REALPATH(handle
, cappath
);
393 static int cap_chmod_acl(vfs_handle_struct
*handle
, const char *path
, mode_t mode
)
395 char *cappath
= capencode(talloc_tos(), path
);
397 /* If the underlying VFS doesn't have ACL support... */
402 return SMB_VFS_NEXT_CHMOD_ACL(handle
, cappath
, mode
);
405 static SMB_ACL_T
cap_sys_acl_get_file(vfs_handle_struct
*handle
,
406 const char *path
, SMB_ACL_TYPE_T type
,
409 char *cappath
= capencode(talloc_tos(), path
);
413 return (SMB_ACL_T
)NULL
;
415 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle
, cappath
, type
, mem_ctx
);
418 static int cap_sys_acl_set_file(vfs_handle_struct
*handle
, const char *path
, SMB_ACL_TYPE_T acltype
, SMB_ACL_T theacl
)
420 char *cappath
= capencode(talloc_tos(), path
);
426 return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle
, cappath
, acltype
, theacl
);
429 static int cap_sys_acl_delete_def_file(vfs_handle_struct
*handle
, const char *path
)
431 char *cappath
= capencode(talloc_tos(), path
);
437 return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle
, cappath
);
440 static ssize_t
cap_getxattr(vfs_handle_struct
*handle
, const char *path
, const char *name
, void *value
, size_t size
)
442 char *cappath
= capencode(talloc_tos(), path
);
443 char *capname
= capencode(talloc_tos(), name
);
445 if (!cappath
|| !capname
) {
449 return SMB_VFS_NEXT_GETXATTR(handle
, cappath
, capname
, value
, size
);
452 static ssize_t
cap_fgetxattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
, void *value
, size_t size
)
454 char *cappath
= capencode(talloc_tos(), path
);
460 return SMB_VFS_NEXT_FGETXATTR(handle
, fsp
, cappath
, value
, size
);
463 static ssize_t
cap_listxattr(vfs_handle_struct
*handle
, const char *path
, char *list
, size_t size
)
465 char *cappath
= capencode(talloc_tos(), path
);
471 return SMB_VFS_NEXT_LISTXATTR(handle
, cappath
, list
, size
);
474 static int cap_removexattr(vfs_handle_struct
*handle
, const char *path
, const char *name
)
476 char *cappath
= capencode(talloc_tos(), path
);
477 char *capname
= capencode(talloc_tos(), name
);
479 if (!cappath
|| !capname
) {
483 return SMB_VFS_NEXT_REMOVEXATTR(handle
, cappath
, capname
);
486 static int cap_fremovexattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
)
488 char *cappath
= capencode(talloc_tos(), path
);
494 return SMB_VFS_NEXT_FREMOVEXATTR(handle
, fsp
, cappath
);
497 static int cap_setxattr(vfs_handle_struct
*handle
, const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
499 char *cappath
= capencode(talloc_tos(), path
);
500 char *capname
= capencode(talloc_tos(), name
);
502 if (!cappath
|| !capname
) {
506 return SMB_VFS_NEXT_SETXATTR(handle
, cappath
, capname
, value
, size
, flags
);
509 static int cap_fsetxattr(vfs_handle_struct
*handle
, struct files_struct
*fsp
, const char *path
, const void *value
, size_t size
, int flags
)
511 char *cappath
= capencode(talloc_tos(), path
);
517 return SMB_VFS_NEXT_FSETXATTR(handle
, fsp
, cappath
, value
, size
, flags
);
520 static struct vfs_fn_pointers vfs_cap_fns
= {
521 .disk_free_fn
= cap_disk_free
,
522 .opendir_fn
= cap_opendir
,
523 .readdir_fn
= cap_readdir
,
524 .mkdir_fn
= cap_mkdir
,
525 .rmdir_fn
= cap_rmdir
,
527 .rename_fn
= cap_rename
,
529 .lstat_fn
= cap_lstat
,
530 .unlink_fn
= cap_unlink
,
531 .chmod_fn
= cap_chmod
,
532 .chown_fn
= cap_chown
,
533 .lchown_fn
= cap_lchown
,
534 .chdir_fn
= cap_chdir
,
535 .ntimes_fn
= cap_ntimes
,
536 .symlink_fn
= cap_symlink
,
537 .readlink_fn
= cap_readlink
,
539 .mknod_fn
= cap_mknod
,
540 .realpath_fn
= cap_realpath
,
541 .chmod_acl_fn
= cap_chmod_acl
,
542 .sys_acl_get_file_fn
= cap_sys_acl_get_file
,
543 .sys_acl_set_file_fn
= cap_sys_acl_set_file
,
544 .sys_acl_delete_def_file_fn
= cap_sys_acl_delete_def_file
,
545 .getxattr_fn
= cap_getxattr
,
546 .fgetxattr_fn
= cap_fgetxattr
,
547 .listxattr_fn
= cap_listxattr
,
548 .removexattr_fn
= cap_removexattr
,
549 .fremovexattr_fn
= cap_fremovexattr
,
550 .setxattr_fn
= cap_setxattr
,
551 .fsetxattr_fn
= cap_fsetxattr
554 NTSTATUS
vfs_cap_init(void);
555 NTSTATUS
vfs_cap_init(void)
557 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "cap",
561 /* For CAP functions */
563 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
564 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
565 #define is_hex(s) ((s)[0] == hex_tag)
567 static unsigned char hex2bin_table
[256] = {
568 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
569 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
571 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
572 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
573 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
574 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
575 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
576 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
578 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
587 static unsigned char bin2hex_table
[256] = "0123456789abcdef";
589 /*******************************************************************
590 original code -> ":xx" - CAP format
591 ********************************************************************/
593 static char *capencode(TALLOC_CTX
*ctx
, const char *from
)
600 for (p1
= from
; *p1
; p1
++) {
601 if ((unsigned char)*p1
>= 0x80) {
609 to
= talloc_array(ctx
, char, len
);
614 for (out
= to
; *from
;) {
615 /* buffer husoku error */
616 if ((unsigned char)*from
>= 0x80) {
618 *out
++ = bin2hex (((*from
)>>4)&0x0f);
619 *out
++ = bin2hex ((*from
)&0x0f);
629 /*******************************************************************
631 ********************************************************************/
632 /* ":xx" -> a byte */
634 static char *capdecode(TALLOC_CTX
*ctx
, const char *from
)
641 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]));