ctdb-vacuum: revert "Do not delete VACUUM MIGRATED records immediately"
[Samba.git] / source3 / modules / vfs_cap.c
blobb5a19060f04ecc799856b322c89460522c22b2c6
1 /*
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/>.
25 #include "includes.h"
26 #include "smbd/smbd.h"
28 /* cap functions */
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);
37 if (!cappath) {
38 errno = ENOMEM;
39 return (uint64_t)-1;
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);
48 if (!capname) {
49 errno = ENOMEM;
50 return NULL;
52 return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
55 static struct dirent *cap_readdir(vfs_handle_struct *handle,
56 DIR *dirp,
57 SMB_STRUCT_STAT *sbuf)
59 struct dirent *result;
60 struct dirent *newdirent;
61 char *newname;
62 size_t newnamelen;
63 DEBUG(3,("cap: cap_readdir\n"));
65 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
66 if (!result) {
67 return NULL;
70 newname = capdecode(talloc_tos(), result->d_name);
71 if (!newname) {
72 return NULL;
74 DEBUG(3,("cap: cap_readdir: %s\n", newname));
75 newnamelen = strlen(newname)+1;
76 newdirent = (struct dirent *)talloc_array(talloc_tos(),
77 char,
78 sizeof(struct dirent)+
79 newnamelen);
80 if (!newdirent) {
81 return NULL;
83 memcpy(newdirent, result, sizeof(struct dirent));
84 memcpy(&newdirent->d_name, newname, newnamelen);
85 return newdirent;
88 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
90 char *cappath = capencode(talloc_tos(), path);
92 if (!cappath) {
93 errno = ENOMEM;
94 return -1;
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);
103 if (!cappath) {
104 errno = ENOMEM;
105 return -1;
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)
113 char *cappath;
114 char *tmp_base_name = NULL;
115 int ret;
117 cappath = capencode(talloc_tos(), smb_fname->base_name);
119 if (!cappath) {
120 errno = ENOMEM;
121 return -1;
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);
133 return ret;
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)
140 char *capold = NULL;
141 char *capnew = NULL;
142 struct smb_filename *smb_fname_src_tmp = NULL;
143 struct smb_filename *smb_fname_dst_tmp = NULL;
144 int ret = -1;
146 capold = capencode(talloc_tos(), smb_fname_src->base_name);
147 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
148 if (!capold || !capnew) {
149 errno = ENOMEM;
150 goto out;
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) {
156 errno = ENOMEM;
157 goto out;
159 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
160 if (smb_fname_dst_tmp == NULL) {
161 errno = ENOMEM;
162 goto out;
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,
169 smb_fname_dst_tmp);
170 out:
171 TALLOC_FREE(capold);
172 TALLOC_FREE(capnew);
173 TALLOC_FREE(smb_fname_src_tmp);
174 TALLOC_FREE(smb_fname_dst_tmp);
176 return ret;
179 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
181 char *cappath;
182 char *tmp_base_name = NULL;
183 int ret;
185 cappath = capencode(talloc_tos(), smb_fname->base_name);
187 if (!cappath) {
188 errno = ENOMEM;
189 return -1;
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);
200 return ret;
203 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
205 char *cappath;
206 char *tmp_base_name = NULL;
207 int ret;
209 cappath = capencode(talloc_tos(), smb_fname->base_name);
211 if (!cappath) {
212 errno = ENOMEM;
213 return -1;
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);
224 return ret;
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;
232 int ret;
234 cappath = capencode(talloc_tos(), smb_fname->base_name);
235 if (!cappath) {
236 errno = ENOMEM;
237 return -1;
240 /* Setup temporary smb_filename structs. */
241 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
242 if (smb_fname_tmp == NULL) {
243 errno = ENOMEM;
244 return -1;
247 smb_fname_tmp->base_name = cappath;
249 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
251 TALLOC_FREE(smb_fname_tmp);
252 return ret;
255 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
257 char *cappath = capencode(talloc_tos(), path);
259 if (!cappath) {
260 errno = ENOMEM;
261 return -1;
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);
270 if (!cappath) {
271 errno = ENOMEM;
272 return -1;
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);
281 if (!cappath) {
282 errno = ENOMEM;
283 return -1;
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);
292 if (!cappath) {
293 errno = ENOMEM;
294 return -1;
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;
306 int ret;
308 cappath = capencode(talloc_tos(), smb_fname->base_name);
310 if (!cappath) {
311 errno = ENOMEM;
312 return -1;
315 /* Setup temporary smb_filename structs. */
316 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
317 if (smb_fname_tmp == NULL) {
318 errno = ENOMEM;
319 return -1;
322 smb_fname_tmp->base_name = cappath;
324 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
326 TALLOC_FREE(smb_fname_tmp);
327 return ret;
331 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
332 const char *newpath)
334 char *capold = capencode(talloc_tos(), oldpath);
335 char *capnew = capencode(talloc_tos(), newpath);
337 if (!capold || !capnew) {
338 errno = ENOMEM;
339 return -1;
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);
349 if (!cappath) {
350 errno = ENOMEM;
351 return -1;
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) {
362 errno = ENOMEM;
363 return -1;
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);
372 if (!cappath) {
373 errno = ENOMEM;
374 return -1;
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);
384 if (!cappath) {
385 errno = ENOMEM;
386 return NULL;
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... */
396 if (!cappath) {
397 errno = ENOMEM;
398 return -1;
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,
405 TALLOC_CTX *mem_ctx)
407 char *cappath = capencode(talloc_tos(), path);
409 if (!cappath) {
410 errno = ENOMEM;
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);
420 if (!cappath) {
421 errno = ENOMEM;
422 return -1;
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);
431 if (!cappath) {
432 errno = ENOMEM;
433 return -1;
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) {
444 errno = ENOMEM;
445 return -1;
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);
454 if (!cappath) {
455 errno = ENOMEM;
456 return -1;
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);
465 if (!cappath) {
466 errno = ENOMEM;
467 return -1;
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) {
478 errno = ENOMEM;
479 return -1;
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);
488 if (!cappath) {
489 errno = ENOMEM;
490 return -1;
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) {
501 errno = ENOMEM;
502 return -1;
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);
511 if (!cappath) {
512 errno = ENOMEM;
513 return -1;
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,
524 .open_fn = cap_open,
525 .rename_fn = cap_rename,
526 .stat_fn = cap_stat,
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,
536 .link_fn = cap_link,
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",
556 &vfs_cap_fns);
559 /* For CAP functions */
560 #define hex_tag ':'
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)
593 char *out = NULL;
594 const char *p1;
595 char *to = NULL;
596 size_t len = 0;
598 for (p1 = from; *p1; p1++) {
599 if ((unsigned char)*p1 >= 0x80) {
600 len += 3;
601 } else {
602 len++;
605 len++;
607 to = talloc_array(ctx, char, len);
608 if (!to) {
609 return NULL;
612 for (out = to; *from;) {
613 /* buffer husoku error */
614 if ((unsigned char)*from >= 0x80) {
615 *out++ = hex_tag;
616 *out++ = bin2hex (((*from)>>4)&0x0f);
617 *out++ = bin2hex ((*from)&0x0f);
618 from++;
619 } else {
620 *out++ = *from++;
623 *out = '\0';
624 return to;
627 /*******************************************************************
628 CAP -> original code
629 ********************************************************************/
630 /* ":xx" -> a byte */
632 static char *capdecode(TALLOC_CTX *ctx, const char *from)
634 const char *p1;
635 char *out = NULL;
636 char *to = NULL;
637 size_t len = 0;
639 for (p1 = from; *p1; len++) {
640 if (is_hex(p1)) {
641 p1 += 3;
642 } else {
643 p1++;
646 len++;
648 to = talloc_array(ctx, char, len);
649 if (!to) {
650 return NULL;
653 for (out = to; *from;) {
654 if (is_hex(from)) {
655 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
656 from += 3;
657 } else {
658 *out++ = *from++;
661 *out = '\0';
662 return to;