WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / modules / vfs_cap.c
blobffc35d60407372656366116bed75a27ab02c51c2
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,
33 const struct smb_filename *smb_fname,
34 uint64_t *bsize,
35 uint64_t *dfree,
36 uint64_t *dsize)
38 char *capname = capencode(talloc_tos(), smb_fname->base_name);
39 struct smb_filename *cap_smb_fname = NULL;
41 if (!capname) {
42 errno = ENOMEM;
43 return (uint64_t)-1;
45 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
46 capname,
47 NULL,
48 NULL,
49 smb_fname->flags);
50 if (cap_smb_fname == NULL) {
51 TALLOC_FREE(capname);
52 errno = ENOMEM;
53 return (uint64_t)-1;
55 return SMB_VFS_NEXT_DISK_FREE(handle, cap_smb_fname,
56 bsize, dfree, dsize);
59 static int cap_get_quota(vfs_handle_struct *handle,
60 const struct smb_filename *smb_fname,
61 enum SMB_QUOTA_TYPE qtype,
62 unid_t id,
63 SMB_DISK_QUOTA *dq)
65 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
66 struct smb_filename *cap_smb_fname = NULL;
68 if (!cappath) {
69 errno = ENOMEM;
70 return -1;
72 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
73 cappath,
74 NULL,
75 NULL,
76 smb_fname->flags);
77 if (cap_smb_fname == NULL) {
78 TALLOC_FREE(cappath);
79 errno = ENOMEM;
80 return -1;
82 return SMB_VFS_NEXT_GET_QUOTA(handle, cap_smb_fname, qtype, id, dq);
85 static DIR *cap_opendir(vfs_handle_struct *handle,
86 const struct smb_filename *smb_fname,
87 const char *mask,
88 uint32_t attr)
90 char *capname = capencode(talloc_tos(), smb_fname->base_name);
91 struct smb_filename *cap_smb_fname = NULL;
93 if (!capname) {
94 errno = ENOMEM;
95 return NULL;
97 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
98 capname,
99 NULL,
100 NULL,
101 smb_fname->flags);
102 if (cap_smb_fname == NULL) {
103 TALLOC_FREE(capname);
104 errno = ENOMEM;
105 return NULL;
107 return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
110 static struct dirent *cap_readdir(vfs_handle_struct *handle,
111 DIR *dirp,
112 SMB_STRUCT_STAT *sbuf)
114 struct dirent *result;
115 struct dirent *newdirent;
116 char *newname;
117 size_t newnamelen;
118 DEBUG(3,("cap: cap_readdir\n"));
120 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
121 if (!result) {
122 return NULL;
125 newname = capdecode(talloc_tos(), result->d_name);
126 if (!newname) {
127 return NULL;
129 DEBUG(3,("cap: cap_readdir: %s\n", newname));
130 newnamelen = strlen(newname)+1;
131 newdirent = talloc_size(
132 talloc_tos(), sizeof(struct dirent) + newnamelen);
133 if (!newdirent) {
134 return NULL;
136 talloc_set_name_const(newdirent, "struct dirent");
137 memcpy(newdirent, result, sizeof(struct dirent));
138 memcpy(&newdirent->d_name, newname, newnamelen);
139 return newdirent;
142 static int cap_mkdir(vfs_handle_struct *handle,
143 const struct smb_filename *smb_fname,
144 mode_t mode)
146 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
147 struct smb_filename *cap_smb_fname = NULL;
149 if (!cappath) {
150 errno = ENOMEM;
151 return -1;
154 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
155 cappath,
156 NULL,
157 NULL,
158 smb_fname->flags);
159 if (cap_smb_fname == NULL) {
160 TALLOC_FREE(cappath);
161 errno = ENOMEM;
162 return -1;
165 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
168 static int cap_rmdir(vfs_handle_struct *handle,
169 const struct smb_filename *smb_fname)
171 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
172 struct smb_filename *cap_smb_fname = NULL;
174 if (!cappath) {
175 errno = ENOMEM;
176 return -1;
179 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
180 cappath,
181 NULL,
182 NULL,
183 smb_fname->flags);
184 if (cap_smb_fname == NULL) {
185 TALLOC_FREE(cappath);
186 errno = ENOMEM;
187 return -1;
190 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
193 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
194 files_struct *fsp, int flags, mode_t mode)
196 char *cappath;
197 char *tmp_base_name = NULL;
198 int ret;
200 cappath = capencode(talloc_tos(), smb_fname->base_name);
202 if (!cappath) {
203 errno = ENOMEM;
204 return -1;
207 tmp_base_name = smb_fname->base_name;
208 smb_fname->base_name = cappath;
210 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
211 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
213 smb_fname->base_name = tmp_base_name;
214 TALLOC_FREE(cappath);
216 return ret;
219 static int cap_rename(vfs_handle_struct *handle,
220 const struct smb_filename *smb_fname_src,
221 const struct smb_filename *smb_fname_dst)
223 char *capold = NULL;
224 char *capnew = NULL;
225 struct smb_filename *smb_fname_src_tmp = NULL;
226 struct smb_filename *smb_fname_dst_tmp = NULL;
227 int ret = -1;
229 capold = capencode(talloc_tos(), smb_fname_src->base_name);
230 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
231 if (!capold || !capnew) {
232 errno = ENOMEM;
233 goto out;
236 /* Setup temporary smb_filename structs. */
237 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
238 if (smb_fname_src_tmp == NULL) {
239 errno = ENOMEM;
240 goto out;
242 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
243 if (smb_fname_dst_tmp == NULL) {
244 errno = ENOMEM;
245 goto out;
248 smb_fname_src_tmp->base_name = capold;
249 smb_fname_dst_tmp->base_name = capnew;
251 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
252 smb_fname_dst_tmp);
253 out:
254 TALLOC_FREE(capold);
255 TALLOC_FREE(capnew);
256 TALLOC_FREE(smb_fname_src_tmp);
257 TALLOC_FREE(smb_fname_dst_tmp);
259 return ret;
262 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
264 char *cappath;
265 char *tmp_base_name = NULL;
266 int ret;
268 cappath = capencode(talloc_tos(), smb_fname->base_name);
270 if (!cappath) {
271 errno = ENOMEM;
272 return -1;
275 tmp_base_name = smb_fname->base_name;
276 smb_fname->base_name = cappath;
278 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
280 smb_fname->base_name = tmp_base_name;
281 TALLOC_FREE(cappath);
283 return ret;
286 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
288 char *cappath;
289 char *tmp_base_name = NULL;
290 int ret;
292 cappath = capencode(talloc_tos(), smb_fname->base_name);
294 if (!cappath) {
295 errno = ENOMEM;
296 return -1;
299 tmp_base_name = smb_fname->base_name;
300 smb_fname->base_name = cappath;
302 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
304 smb_fname->base_name = tmp_base_name;
305 TALLOC_FREE(cappath);
307 return ret;
310 static int cap_unlink(vfs_handle_struct *handle,
311 const struct smb_filename *smb_fname)
313 struct smb_filename *smb_fname_tmp = NULL;
314 char *cappath = NULL;
315 int ret;
317 cappath = capencode(talloc_tos(), smb_fname->base_name);
318 if (!cappath) {
319 errno = ENOMEM;
320 return -1;
323 /* Setup temporary smb_filename structs. */
324 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
325 if (smb_fname_tmp == NULL) {
326 errno = ENOMEM;
327 return -1;
330 smb_fname_tmp->base_name = cappath;
332 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
334 TALLOC_FREE(smb_fname_tmp);
335 return ret;
338 static int cap_chmod(vfs_handle_struct *handle,
339 const struct smb_filename *smb_fname,
340 mode_t mode)
342 struct smb_filename *cap_smb_fname = NULL;
343 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
344 int ret;
345 int saved_errno;
347 if (!cappath) {
348 errno = ENOMEM;
349 return -1;
352 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
353 cappath,
354 NULL,
355 NULL,
356 smb_fname->flags);
357 if (cap_smb_fname == NULL) {
358 TALLOC_FREE(cappath);
359 errno = ENOMEM;
360 return -1;
363 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
364 saved_errno = errno;
365 TALLOC_FREE(cappath);
366 TALLOC_FREE(cap_smb_fname);
367 errno = saved_errno;
368 return ret;
371 static int cap_chown(vfs_handle_struct *handle,
372 const struct smb_filename *smb_fname,
373 uid_t uid,
374 gid_t gid)
376 struct smb_filename *cap_smb_fname = NULL;
377 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
378 int ret;
379 int saved_errno;
381 if (!cappath) {
382 errno = ENOMEM;
383 return -1;
386 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
387 cappath,
388 NULL,
389 NULL,
390 smb_fname->flags);
391 if (cap_smb_fname == NULL) {
392 TALLOC_FREE(cappath);
393 errno = ENOMEM;
394 return -1;
397 ret = SMB_VFS_NEXT_CHOWN(handle, cap_smb_fname, uid, gid);
398 saved_errno = errno;
399 TALLOC_FREE(cappath);
400 TALLOC_FREE(cap_smb_fname);
401 errno = saved_errno;
402 return ret;
405 static int cap_lchown(vfs_handle_struct *handle,
406 const struct smb_filename *smb_fname,
407 uid_t uid,
408 gid_t gid)
410 struct smb_filename *cap_smb_fname = NULL;
411 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
412 int ret;
413 int saved_errno;
415 if (!cappath) {
416 errno = ENOMEM;
417 return -1;
420 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
421 cappath,
422 NULL,
423 NULL,
424 smb_fname->flags);
425 if (cap_smb_fname == NULL) {
426 TALLOC_FREE(cappath);
427 errno = ENOMEM;
428 return -1;
431 ret = SMB_VFS_NEXT_LCHOWN(handle, cap_smb_fname, uid, gid);
432 saved_errno = errno;
433 TALLOC_FREE(cappath);
434 TALLOC_FREE(cap_smb_fname);
435 errno = saved_errno;
436 return ret;
439 static int cap_chdir(vfs_handle_struct *handle,
440 const struct smb_filename *smb_fname)
442 struct smb_filename *cap_smb_fname = NULL;
443 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
444 int ret;
445 int saved_errno = 0;
447 if (!cappath) {
448 errno = ENOMEM;
449 return -1;
451 DEBUG(3,("cap: cap_chdir for %s\n", smb_fname->base_name));
453 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
454 cappath,
455 NULL,
456 NULL,
457 smb_fname->flags);
458 if (cap_smb_fname == NULL) {
459 TALLOC_FREE(cappath);
460 errno = ENOMEM;
461 return -1;
463 ret = SMB_VFS_NEXT_CHDIR(handle, cap_smb_fname);
464 if (ret == -1) {
465 saved_errno = errno;
467 TALLOC_FREE(cappath);
468 TALLOC_FREE(cap_smb_fname);
469 if (saved_errno != 0) {
470 errno = saved_errno;
472 return ret;
475 static int cap_ntimes(vfs_handle_struct *handle,
476 const struct smb_filename *smb_fname,
477 struct smb_file_time *ft)
479 struct smb_filename *smb_fname_tmp = NULL;
480 char *cappath = NULL;
481 int ret;
483 cappath = capencode(talloc_tos(), smb_fname->base_name);
485 if (!cappath) {
486 errno = ENOMEM;
487 return -1;
490 /* Setup temporary smb_filename structs. */
491 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
492 if (smb_fname_tmp == NULL) {
493 errno = ENOMEM;
494 return -1;
497 smb_fname_tmp->base_name = cappath;
499 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
501 TALLOC_FREE(smb_fname_tmp);
502 return ret;
506 static int cap_symlink(vfs_handle_struct *handle,
507 const char *link_contents,
508 const struct smb_filename *new_smb_fname)
510 char *capold = capencode(talloc_tos(), link_contents);
511 char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
512 struct smb_filename *new_cap_smb_fname = NULL;
513 int saved_errno = 0;
514 int ret;
516 if (!capold || !capnew) {
517 errno = ENOMEM;
518 return -1;
520 new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
521 capnew,
522 NULL,
523 NULL,
524 new_smb_fname->flags);
525 if (new_cap_smb_fname == NULL) {
526 TALLOC_FREE(capold);
527 TALLOC_FREE(capnew);
528 errno = ENOMEM;
529 return -1;
531 ret = SMB_VFS_NEXT_SYMLINK(handle,
532 capold,
533 new_cap_smb_fname);
534 if (ret == -1) {
535 saved_errno = errno;
537 TALLOC_FREE(capold);
538 TALLOC_FREE(capnew);
539 TALLOC_FREE(new_cap_smb_fname);
540 if (saved_errno != 0) {
541 errno = saved_errno;
543 return ret;
546 static int cap_readlink(vfs_handle_struct *handle,
547 const struct smb_filename *smb_fname,
548 char *buf,
549 size_t bufsiz)
551 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
552 struct smb_filename *cap_smb_fname = NULL;
553 int saved_errno = 0;
554 int ret;
556 if (!cappath) {
557 errno = ENOMEM;
558 return -1;
560 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
561 cappath,
562 NULL,
563 NULL,
564 smb_fname->flags);
565 if (cap_smb_fname == NULL) {
566 TALLOC_FREE(cappath);
567 errno = ENOMEM;
568 return -1;
570 ret = SMB_VFS_NEXT_READLINK(handle, cap_smb_fname, buf, bufsiz);
571 if (ret == -1) {
572 saved_errno = errno;
574 TALLOC_FREE(cappath);
575 TALLOC_FREE(cap_smb_fname);
576 if (saved_errno != 0) {
577 errno = saved_errno;
579 return ret;
582 static int cap_link(vfs_handle_struct *handle,
583 const struct smb_filename *old_smb_fname,
584 const struct smb_filename *new_smb_fname)
586 char *capold = capencode(talloc_tos(), old_smb_fname->base_name);
587 char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
588 struct smb_filename *old_cap_smb_fname = NULL;
589 struct smb_filename *new_cap_smb_fname = NULL;
590 int saved_errno = 0;
591 int ret;
593 if (!capold || !capnew) {
594 errno = ENOMEM;
595 return -1;
597 old_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
598 capold,
599 NULL,
600 NULL,
601 old_smb_fname->flags);
602 if (old_cap_smb_fname == NULL) {
603 TALLOC_FREE(capold);
604 TALLOC_FREE(capnew);
605 errno = ENOMEM;
606 return -1;
608 new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
609 capnew,
610 NULL,
611 NULL,
612 new_smb_fname->flags);
613 if (new_cap_smb_fname == NULL) {
614 TALLOC_FREE(capold);
615 TALLOC_FREE(capnew);
616 TALLOC_FREE(old_cap_smb_fname);
617 errno = ENOMEM;
618 return -1;
620 ret = SMB_VFS_NEXT_LINK(handle, old_cap_smb_fname, new_cap_smb_fname);
621 if (ret == -1) {
622 saved_errno = errno;
624 TALLOC_FREE(capold);
625 TALLOC_FREE(capnew);
626 TALLOC_FREE(old_cap_smb_fname);
627 TALLOC_FREE(new_cap_smb_fname);
628 if (saved_errno != 0) {
629 errno = saved_errno;
631 return ret;
634 static int cap_mknod(vfs_handle_struct *handle,
635 const struct smb_filename *smb_fname,
636 mode_t mode,
637 SMB_DEV_T dev)
639 struct smb_filename *cap_smb_fname = NULL;
640 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
641 int ret;
642 int saved_errno = 0;
644 if (!cappath) {
645 errno = ENOMEM;
646 return -1;
648 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
649 cappath,
650 NULL,
651 NULL,
652 smb_fname->flags);
653 if (cap_smb_fname == NULL) {
654 TALLOC_FREE(cappath);
655 errno = ENOMEM;
656 return -1;
658 ret = SMB_VFS_NEXT_MKNOD(handle, cap_smb_fname, mode, dev);
659 if (ret == -1) {
660 saved_errno = errno;
662 TALLOC_FREE(cappath);
663 TALLOC_FREE(cap_smb_fname);
664 if (saved_errno != 0) {
665 errno = saved_errno;
667 return ret;
670 static struct smb_filename *cap_realpath(vfs_handle_struct *handle,
671 TALLOC_CTX *ctx,
672 const struct smb_filename *smb_fname)
674 /* monyo need capencode'ed and capdecode'ed? */
675 struct smb_filename *cap_smb_fname = NULL;
676 struct smb_filename *return_fname = NULL;
677 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
678 int saved_errno = 0;
680 if (!cappath) {
681 errno = ENOMEM;
682 return NULL;
684 cap_smb_fname = synthetic_smb_fname(ctx,
685 cappath,
686 NULL,
687 NULL,
688 smb_fname->flags);
689 if (cap_smb_fname == NULL) {
690 TALLOC_FREE(cappath);
691 errno = ENOMEM;
692 return NULL;
694 return_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, cap_smb_fname);
695 if (return_fname == NULL) {
696 saved_errno = errno;
698 TALLOC_FREE(cappath);
699 TALLOC_FREE(cap_smb_fname);
700 if (saved_errno != 0) {
701 errno = saved_errno;
703 return return_fname;
706 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
707 const struct smb_filename *smb_fname,
708 SMB_ACL_TYPE_T type,
709 TALLOC_CTX *mem_ctx)
711 struct smb_filename *cap_smb_fname = NULL;
712 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
713 SMB_ACL_T ret;
714 int saved_errno = 0;
716 if (!cappath) {
717 errno = ENOMEM;
718 return (SMB_ACL_T)NULL;
720 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
721 cappath,
722 NULL,
723 NULL,
724 smb_fname->flags);
725 if (cap_smb_fname == NULL) {
726 TALLOC_FREE(cappath);
727 errno = ENOMEM;
728 return (SMB_ACL_T)NULL;
730 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
731 type, mem_ctx);
732 if (ret == NULL) {
733 saved_errno = errno;
735 TALLOC_FREE(cappath);
736 TALLOC_FREE(cap_smb_fname);
737 if (saved_errno != 0) {
738 errno = saved_errno;
740 return ret;
743 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
744 const struct smb_filename *smb_fname,
745 SMB_ACL_TYPE_T acltype,
746 SMB_ACL_T theacl)
748 struct smb_filename *cap_smb_fname = NULL;
749 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
750 int ret;
751 int saved_errno = 0;
753 if (!cappath) {
754 errno = ENOMEM;
755 return -1;
757 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
758 cappath,
759 NULL,
760 NULL,
761 smb_fname->flags);
762 if (cap_smb_fname == NULL) {
763 TALLOC_FREE(cappath);
764 errno = ENOMEM;
765 return -1;
767 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
768 acltype, theacl);
769 if (ret == -1) {
770 saved_errno = errno;
772 TALLOC_FREE(cappath);
773 TALLOC_FREE(cap_smb_fname);
774 if (saved_errno != 0) {
775 errno = saved_errno;
777 return ret;
780 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
781 const struct smb_filename *smb_fname)
783 struct smb_filename *cap_smb_fname = NULL;
784 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
785 int ret;
786 int saved_errno = 0;
788 if (!cappath) {
789 errno = ENOMEM;
790 return -1;
792 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
793 cappath,
794 NULL,
795 NULL,
796 smb_fname->flags);
797 if (cap_smb_fname == NULL) {
798 TALLOC_FREE(cappath);
799 errno = ENOMEM;
800 return -1;
802 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
803 if (ret == -1) {
804 saved_errno = errno;
806 TALLOC_FREE(cappath);
807 TALLOC_FREE(cap_smb_fname);
808 if (saved_errno) {
809 errno = saved_errno;
811 return ret;
814 static ssize_t cap_getxattr(vfs_handle_struct *handle,
815 const struct smb_filename *smb_fname,
816 const char *name,
817 void *value,
818 size_t size)
820 struct smb_filename *cap_smb_fname = NULL;
821 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
822 char *capname = capencode(talloc_tos(), name);
823 ssize_t ret;
824 int saved_errno = 0;
826 if (!cappath || !capname) {
827 errno = ENOMEM;
828 return -1;
830 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
831 cappath,
832 NULL,
833 NULL,
834 smb_fname->flags);
835 if (cap_smb_fname == NULL) {
836 TALLOC_FREE(cappath);
837 TALLOC_FREE(capname);
838 errno = ENOMEM;
839 return -1;
841 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
842 capname, value, size);
843 if (ret == -1) {
844 saved_errno = errno;
846 TALLOC_FREE(cappath);
847 TALLOC_FREE(capname);
848 TALLOC_FREE(cap_smb_fname);
849 if (saved_errno) {
850 errno = saved_errno;
852 return ret;
855 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
857 char *cappath = capencode(talloc_tos(), path);
859 if (!cappath) {
860 errno = ENOMEM;
861 return -1;
863 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
866 static ssize_t cap_listxattr(vfs_handle_struct *handle,
867 const struct smb_filename *smb_fname,
868 char *list,
869 size_t size)
871 struct smb_filename *cap_smb_fname = NULL;
872 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
873 ssize_t ret;
874 int saved_errno = 0;
876 if (!cappath) {
877 errno = ENOMEM;
878 return -1;
880 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
881 cappath,
882 NULL,
883 NULL,
884 smb_fname->flags);
885 if (cap_smb_fname == NULL) {
886 TALLOC_FREE(cappath);
887 errno = ENOMEM;
888 return -1;
890 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
891 if (ret == -1) {
892 saved_errno = errno;
894 TALLOC_FREE(cappath);
895 TALLOC_FREE(cap_smb_fname);
896 if (saved_errno) {
897 errno = saved_errno;
899 return ret;
902 static int cap_removexattr(vfs_handle_struct *handle,
903 const struct smb_filename *smb_fname,
904 const char *name)
906 struct smb_filename *cap_smb_fname = NULL;
907 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
908 char *capname = capencode(talloc_tos(), name);
909 int ret;
910 int saved_errno = 0;
912 if (!cappath || !capname) {
913 errno = ENOMEM;
914 return -1;
916 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
917 cappath,
918 NULL,
919 NULL,
920 smb_fname->flags);
921 if (cap_smb_fname == NULL) {
922 TALLOC_FREE(cappath);
923 TALLOC_FREE(capname);
924 errno = ENOMEM;
925 return -1;
927 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
928 if (ret == -1) {
929 saved_errno = errno;
931 TALLOC_FREE(cappath);
932 TALLOC_FREE(capname);
933 TALLOC_FREE(cap_smb_fname);
934 if (saved_errno) {
935 errno = saved_errno;
937 return ret;
940 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
942 char *cappath = capencode(talloc_tos(), path);
944 if (!cappath) {
945 errno = ENOMEM;
946 return -1;
948 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
951 static int cap_setxattr(vfs_handle_struct *handle,
952 const struct smb_filename *smb_fname,
953 const char *name,
954 const void *value,
955 size_t size,
956 int flags)
958 struct smb_filename *cap_smb_fname = NULL;
959 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
960 char *capname = capencode(talloc_tos(), name);
961 int ret;
962 int saved_errno = 0;
964 if (!cappath || !capname) {
965 errno = ENOMEM;
966 return -1;
968 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
969 cappath,
970 NULL,
971 NULL,
972 smb_fname->flags);
973 if (cap_smb_fname == NULL) {
974 TALLOC_FREE(cappath);
975 TALLOC_FREE(capname);
976 errno = ENOMEM;
977 return -1;
979 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
980 capname, value, size, flags);
981 if (ret == -1) {
982 saved_errno = errno;
984 TALLOC_FREE(cappath);
985 TALLOC_FREE(capname);
986 TALLOC_FREE(cap_smb_fname);
987 if (saved_errno) {
988 errno = saved_errno;
990 return ret;
993 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
995 char *cappath = capencode(talloc_tos(), path);
997 if (!cappath) {
998 errno = ENOMEM;
999 return -1;
1001 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
1004 static struct vfs_fn_pointers vfs_cap_fns = {
1005 .disk_free_fn = cap_disk_free,
1006 .get_quota_fn = cap_get_quota,
1007 .opendir_fn = cap_opendir,
1008 .readdir_fn = cap_readdir,
1009 .mkdir_fn = cap_mkdir,
1010 .rmdir_fn = cap_rmdir,
1011 .open_fn = cap_open,
1012 .rename_fn = cap_rename,
1013 .stat_fn = cap_stat,
1014 .lstat_fn = cap_lstat,
1015 .unlink_fn = cap_unlink,
1016 .chmod_fn = cap_chmod,
1017 .chown_fn = cap_chown,
1018 .lchown_fn = cap_lchown,
1019 .chdir_fn = cap_chdir,
1020 .ntimes_fn = cap_ntimes,
1021 .symlink_fn = cap_symlink,
1022 .readlink_fn = cap_readlink,
1023 .link_fn = cap_link,
1024 .mknod_fn = cap_mknod,
1025 .realpath_fn = cap_realpath,
1026 .sys_acl_get_file_fn = cap_sys_acl_get_file,
1027 .sys_acl_set_file_fn = cap_sys_acl_set_file,
1028 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1029 .getxattr_fn = cap_getxattr,
1030 .fgetxattr_fn = cap_fgetxattr,
1031 .listxattr_fn = cap_listxattr,
1032 .removexattr_fn = cap_removexattr,
1033 .fremovexattr_fn = cap_fremovexattr,
1034 .setxattr_fn = cap_setxattr,
1035 .fsetxattr_fn = cap_fsetxattr
1038 static_decl_vfs;
1039 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1041 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1042 &vfs_cap_fns);
1045 /* For CAP functions */
1046 #define hex_tag ':'
1047 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
1048 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
1049 #define is_hex(s) ((s)[0] == hex_tag)
1051 static unsigned char hex2bin_table[256] = {
1052 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1053 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1054 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1055 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1056 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1057 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1058 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1059 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1060 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1061 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1062 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1063 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1065 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1066 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1067 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1069 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
1071 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1073 /*******************************************************************
1074 original code -> ":xx" - CAP format
1075 ********************************************************************/
1077 static char *capencode(TALLOC_CTX *ctx, const char *from)
1079 char *out = NULL;
1080 const char *p1;
1081 char *to = NULL;
1082 size_t len = 0;
1084 for (p1 = from; *p1; p1++) {
1085 if ((unsigned char)*p1 >= 0x80) {
1086 len += 3;
1087 } else {
1088 len++;
1091 len++;
1093 to = talloc_array(ctx, char, len);
1094 if (!to) {
1095 return NULL;
1098 for (out = to; *from;) {
1099 /* buffer husoku error */
1100 if ((unsigned char)*from >= 0x80) {
1101 *out++ = hex_tag;
1102 *out++ = bin2hex (((*from)>>4)&0x0f);
1103 *out++ = bin2hex ((*from)&0x0f);
1104 from++;
1105 } else {
1106 *out++ = *from++;
1109 *out = '\0';
1110 return to;
1113 /*******************************************************************
1114 CAP -> original code
1115 ********************************************************************/
1116 /* ":xx" -> a byte */
1118 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1120 const char *p1;
1121 char *out = NULL;
1122 char *to = NULL;
1123 size_t len = 0;
1125 for (p1 = from; *p1; len++) {
1126 if (is_hex(p1)) {
1127 p1 += 3;
1128 } else {
1129 p1++;
1132 len++;
1134 to = talloc_array(ctx, char, len);
1135 if (!to) {
1136 return NULL;
1139 for (out = to; *from;) {
1140 if (is_hex(from)) {
1141 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1142 from += 3;
1143 } else {
1144 *out++ = *from++;
1147 *out = '\0';
1148 return to;