s3: VFS: vfs_cap. Implement readlinkat().
[Samba.git] / source3 / modules / vfs_cap.c
blob700855bc3cda408f4754d3a59c1999a1c68259d6
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_renameat(vfs_handle_struct *handle,
220 files_struct *srcfsp,
221 const struct smb_filename *smb_fname_src,
222 files_struct *dstfsp,
223 const struct smb_filename *smb_fname_dst)
225 char *capold = NULL;
226 char *capnew = NULL;
227 struct smb_filename *smb_fname_src_tmp = NULL;
228 struct smb_filename *smb_fname_dst_tmp = NULL;
229 int ret = -1;
231 capold = capencode(talloc_tos(), smb_fname_src->base_name);
232 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
233 if (!capold || !capnew) {
234 errno = ENOMEM;
235 goto out;
238 /* Setup temporary smb_filename structs. */
239 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
240 if (smb_fname_src_tmp == NULL) {
241 errno = ENOMEM;
242 goto out;
244 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
245 if (smb_fname_dst_tmp == NULL) {
246 errno = ENOMEM;
247 goto out;
250 smb_fname_src_tmp->base_name = capold;
251 smb_fname_dst_tmp->base_name = capnew;
253 ret = SMB_VFS_NEXT_RENAMEAT(handle,
254 srcfsp,
255 smb_fname_src_tmp,
256 dstfsp,
257 smb_fname_dst_tmp);
259 out:
260 TALLOC_FREE(capold);
261 TALLOC_FREE(capnew);
262 TALLOC_FREE(smb_fname_src_tmp);
263 TALLOC_FREE(smb_fname_dst_tmp);
265 return ret;
268 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
270 char *cappath;
271 char *tmp_base_name = NULL;
272 int ret;
274 cappath = capencode(talloc_tos(), smb_fname->base_name);
276 if (!cappath) {
277 errno = ENOMEM;
278 return -1;
281 tmp_base_name = smb_fname->base_name;
282 smb_fname->base_name = cappath;
284 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
286 smb_fname->base_name = tmp_base_name;
287 TALLOC_FREE(cappath);
289 return ret;
292 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
294 char *cappath;
295 char *tmp_base_name = NULL;
296 int ret;
298 cappath = capencode(talloc_tos(), smb_fname->base_name);
300 if (!cappath) {
301 errno = ENOMEM;
302 return -1;
305 tmp_base_name = smb_fname->base_name;
306 smb_fname->base_name = cappath;
308 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
310 smb_fname->base_name = tmp_base_name;
311 TALLOC_FREE(cappath);
313 return ret;
316 static int cap_unlink(vfs_handle_struct *handle,
317 const struct smb_filename *smb_fname)
319 struct smb_filename *smb_fname_tmp = NULL;
320 char *cappath = NULL;
321 int ret;
323 cappath = capencode(talloc_tos(), smb_fname->base_name);
324 if (!cappath) {
325 errno = ENOMEM;
326 return -1;
329 /* Setup temporary smb_filename structs. */
330 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
331 if (smb_fname_tmp == NULL) {
332 errno = ENOMEM;
333 return -1;
336 smb_fname_tmp->base_name = cappath;
338 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
340 TALLOC_FREE(smb_fname_tmp);
341 return ret;
344 static int cap_chmod(vfs_handle_struct *handle,
345 const struct smb_filename *smb_fname,
346 mode_t mode)
348 struct smb_filename *cap_smb_fname = NULL;
349 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
350 int ret;
351 int saved_errno;
353 if (!cappath) {
354 errno = ENOMEM;
355 return -1;
358 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
359 cappath,
360 NULL,
361 NULL,
362 smb_fname->flags);
363 if (cap_smb_fname == NULL) {
364 TALLOC_FREE(cappath);
365 errno = ENOMEM;
366 return -1;
369 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
370 saved_errno = errno;
371 TALLOC_FREE(cappath);
372 TALLOC_FREE(cap_smb_fname);
373 errno = saved_errno;
374 return ret;
377 static int cap_chown(vfs_handle_struct *handle,
378 const struct smb_filename *smb_fname,
379 uid_t uid,
380 gid_t gid)
382 struct smb_filename *cap_smb_fname = NULL;
383 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
384 int ret;
385 int saved_errno;
387 if (!cappath) {
388 errno = ENOMEM;
389 return -1;
392 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
393 cappath,
394 NULL,
395 NULL,
396 smb_fname->flags);
397 if (cap_smb_fname == NULL) {
398 TALLOC_FREE(cappath);
399 errno = ENOMEM;
400 return -1;
403 ret = SMB_VFS_NEXT_CHOWN(handle, cap_smb_fname, uid, gid);
404 saved_errno = errno;
405 TALLOC_FREE(cappath);
406 TALLOC_FREE(cap_smb_fname);
407 errno = saved_errno;
408 return ret;
411 static int cap_lchown(vfs_handle_struct *handle,
412 const struct smb_filename *smb_fname,
413 uid_t uid,
414 gid_t gid)
416 struct smb_filename *cap_smb_fname = NULL;
417 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
418 int ret;
419 int saved_errno;
421 if (!cappath) {
422 errno = ENOMEM;
423 return -1;
426 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
427 cappath,
428 NULL,
429 NULL,
430 smb_fname->flags);
431 if (cap_smb_fname == NULL) {
432 TALLOC_FREE(cappath);
433 errno = ENOMEM;
434 return -1;
437 ret = SMB_VFS_NEXT_LCHOWN(handle, cap_smb_fname, uid, gid);
438 saved_errno = errno;
439 TALLOC_FREE(cappath);
440 TALLOC_FREE(cap_smb_fname);
441 errno = saved_errno;
442 return ret;
445 static int cap_chdir(vfs_handle_struct *handle,
446 const struct smb_filename *smb_fname)
448 struct smb_filename *cap_smb_fname = NULL;
449 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
450 int ret;
451 int saved_errno = 0;
453 if (!cappath) {
454 errno = ENOMEM;
455 return -1;
457 DEBUG(3,("cap: cap_chdir for %s\n", smb_fname->base_name));
459 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
460 cappath,
461 NULL,
462 NULL,
463 smb_fname->flags);
464 if (cap_smb_fname == NULL) {
465 TALLOC_FREE(cappath);
466 errno = ENOMEM;
467 return -1;
469 ret = SMB_VFS_NEXT_CHDIR(handle, cap_smb_fname);
470 if (ret == -1) {
471 saved_errno = errno;
473 TALLOC_FREE(cappath);
474 TALLOC_FREE(cap_smb_fname);
475 if (saved_errno != 0) {
476 errno = saved_errno;
478 return ret;
481 static int cap_ntimes(vfs_handle_struct *handle,
482 const struct smb_filename *smb_fname,
483 struct smb_file_time *ft)
485 struct smb_filename *smb_fname_tmp = NULL;
486 char *cappath = NULL;
487 int ret;
489 cappath = capencode(talloc_tos(), smb_fname->base_name);
491 if (!cappath) {
492 errno = ENOMEM;
493 return -1;
496 /* Setup temporary smb_filename structs. */
497 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
498 if (smb_fname_tmp == NULL) {
499 errno = ENOMEM;
500 return -1;
503 smb_fname_tmp->base_name = cappath;
505 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
507 TALLOC_FREE(smb_fname_tmp);
508 return ret;
512 static int cap_symlink(vfs_handle_struct *handle,
513 const char *link_contents,
514 const struct smb_filename *new_smb_fname)
516 char *capold = capencode(talloc_tos(), link_contents);
517 char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
518 struct smb_filename *new_cap_smb_fname = NULL;
519 int saved_errno = 0;
520 int ret;
522 if (!capold || !capnew) {
523 errno = ENOMEM;
524 return -1;
526 new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
527 capnew,
528 NULL,
529 NULL,
530 new_smb_fname->flags);
531 if (new_cap_smb_fname == NULL) {
532 TALLOC_FREE(capold);
533 TALLOC_FREE(capnew);
534 errno = ENOMEM;
535 return -1;
537 ret = SMB_VFS_NEXT_SYMLINK(handle,
538 capold,
539 new_cap_smb_fname);
540 if (ret == -1) {
541 saved_errno = errno;
543 TALLOC_FREE(capold);
544 TALLOC_FREE(capnew);
545 TALLOC_FREE(new_cap_smb_fname);
546 if (saved_errno != 0) {
547 errno = saved_errno;
549 return ret;
552 static int cap_readlink(vfs_handle_struct *handle,
553 const struct smb_filename *smb_fname,
554 char *buf,
555 size_t bufsiz)
557 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
558 struct smb_filename *cap_smb_fname = NULL;
559 int saved_errno = 0;
560 int ret;
562 if (!cappath) {
563 errno = ENOMEM;
564 return -1;
566 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
567 cappath,
568 NULL,
569 NULL,
570 smb_fname->flags);
571 if (cap_smb_fname == NULL) {
572 TALLOC_FREE(cappath);
573 errno = ENOMEM;
574 return -1;
576 ret = SMB_VFS_NEXT_READLINK(handle, cap_smb_fname, buf, bufsiz);
577 if (ret == -1) {
578 saved_errno = errno;
580 TALLOC_FREE(cappath);
581 TALLOC_FREE(cap_smb_fname);
582 if (saved_errno != 0) {
583 errno = saved_errno;
585 return ret;
588 static int cap_readlinkat(vfs_handle_struct *handle,
589 files_struct *dirfsp,
590 const struct smb_filename *smb_fname,
591 char *buf,
592 size_t bufsiz)
594 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
595 struct smb_filename *cap_smb_fname = NULL;
596 int saved_errno = 0;
597 int ret;
599 if (!cappath) {
600 errno = ENOMEM;
601 return -1;
603 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
604 cappath,
605 NULL,
606 NULL,
607 smb_fname->flags);
608 if (cap_smb_fname == NULL) {
609 TALLOC_FREE(cappath);
610 errno = ENOMEM;
611 return -1;
613 ret = SMB_VFS_NEXT_READLINKAT(handle,
614 dirfsp,
615 cap_smb_fname,
616 buf,
617 bufsiz);
618 if (ret == -1) {
619 saved_errno = errno;
621 TALLOC_FREE(cappath);
622 TALLOC_FREE(cap_smb_fname);
623 if (saved_errno != 0) {
624 errno = saved_errno;
626 return ret;
629 static int cap_linkat(vfs_handle_struct *handle,
630 files_struct *srcfsp,
631 const struct smb_filename *old_smb_fname,
632 files_struct *dstfsp,
633 const struct smb_filename *new_smb_fname,
634 int flags)
636 char *capold = capencode(talloc_tos(), old_smb_fname->base_name);
637 char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
638 struct smb_filename *old_cap_smb_fname = NULL;
639 struct smb_filename *new_cap_smb_fname = NULL;
640 int saved_errno = 0;
641 int ret;
643 if (!capold || !capnew) {
644 errno = ENOMEM;
645 return -1;
647 old_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
648 capold,
649 NULL,
650 NULL,
651 old_smb_fname->flags);
652 if (old_cap_smb_fname == NULL) {
653 TALLOC_FREE(capold);
654 TALLOC_FREE(capnew);
655 errno = ENOMEM;
656 return -1;
658 new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
659 capnew,
660 NULL,
661 NULL,
662 new_smb_fname->flags);
663 if (new_cap_smb_fname == NULL) {
664 TALLOC_FREE(capold);
665 TALLOC_FREE(capnew);
666 TALLOC_FREE(old_cap_smb_fname);
667 errno = ENOMEM;
668 return -1;
670 ret = SMB_VFS_NEXT_LINKAT(handle,
671 srcfsp,
672 old_cap_smb_fname,
673 dstfsp,
674 new_cap_smb_fname,
675 flags);
676 if (ret == -1) {
677 saved_errno = errno;
679 TALLOC_FREE(capold);
680 TALLOC_FREE(capnew);
681 TALLOC_FREE(old_cap_smb_fname);
682 TALLOC_FREE(new_cap_smb_fname);
683 if (saved_errno != 0) {
684 errno = saved_errno;
686 return ret;
689 static int cap_mknodat(vfs_handle_struct *handle,
690 files_struct *dirfsp,
691 const struct smb_filename *smb_fname,
692 mode_t mode,
693 SMB_DEV_T dev)
695 struct smb_filename *cap_smb_fname = NULL;
696 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
697 int ret;
698 int saved_errno = 0;
700 if (!cappath) {
701 errno = ENOMEM;
702 return -1;
704 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
705 cappath,
706 NULL,
707 NULL,
708 smb_fname->flags);
709 if (cap_smb_fname == NULL) {
710 TALLOC_FREE(cappath);
711 errno = ENOMEM;
712 return -1;
714 ret = SMB_VFS_NEXT_MKNODAT(handle,
715 dirfsp,
716 cap_smb_fname,
717 mode,
718 dev);
719 if (ret == -1) {
720 saved_errno = errno;
722 TALLOC_FREE(cappath);
723 TALLOC_FREE(cap_smb_fname);
724 if (saved_errno != 0) {
725 errno = saved_errno;
727 return ret;
730 static struct smb_filename *cap_realpath(vfs_handle_struct *handle,
731 TALLOC_CTX *ctx,
732 const struct smb_filename *smb_fname)
734 /* monyo need capencode'ed and capdecode'ed? */
735 struct smb_filename *cap_smb_fname = NULL;
736 struct smb_filename *return_fname = NULL;
737 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
738 int saved_errno = 0;
740 if (!cappath) {
741 errno = ENOMEM;
742 return NULL;
744 cap_smb_fname = synthetic_smb_fname(ctx,
745 cappath,
746 NULL,
747 NULL,
748 smb_fname->flags);
749 if (cap_smb_fname == NULL) {
750 TALLOC_FREE(cappath);
751 errno = ENOMEM;
752 return NULL;
754 return_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, cap_smb_fname);
755 if (return_fname == NULL) {
756 saved_errno = errno;
758 TALLOC_FREE(cappath);
759 TALLOC_FREE(cap_smb_fname);
760 if (saved_errno != 0) {
761 errno = saved_errno;
763 return return_fname;
766 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
767 const struct smb_filename *smb_fname,
768 SMB_ACL_TYPE_T type,
769 TALLOC_CTX *mem_ctx)
771 struct smb_filename *cap_smb_fname = NULL;
772 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
773 SMB_ACL_T ret;
774 int saved_errno = 0;
776 if (!cappath) {
777 errno = ENOMEM;
778 return (SMB_ACL_T)NULL;
780 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
781 cappath,
782 NULL,
783 NULL,
784 smb_fname->flags);
785 if (cap_smb_fname == NULL) {
786 TALLOC_FREE(cappath);
787 errno = ENOMEM;
788 return (SMB_ACL_T)NULL;
790 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
791 type, mem_ctx);
792 if (ret == NULL) {
793 saved_errno = errno;
795 TALLOC_FREE(cappath);
796 TALLOC_FREE(cap_smb_fname);
797 if (saved_errno != 0) {
798 errno = saved_errno;
800 return ret;
803 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
804 const struct smb_filename *smb_fname,
805 SMB_ACL_TYPE_T acltype,
806 SMB_ACL_T theacl)
808 struct smb_filename *cap_smb_fname = NULL;
809 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
810 int ret;
811 int saved_errno = 0;
813 if (!cappath) {
814 errno = ENOMEM;
815 return -1;
817 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
818 cappath,
819 NULL,
820 NULL,
821 smb_fname->flags);
822 if (cap_smb_fname == NULL) {
823 TALLOC_FREE(cappath);
824 errno = ENOMEM;
825 return -1;
827 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
828 acltype, theacl);
829 if (ret == -1) {
830 saved_errno = errno;
832 TALLOC_FREE(cappath);
833 TALLOC_FREE(cap_smb_fname);
834 if (saved_errno != 0) {
835 errno = saved_errno;
837 return ret;
840 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
841 const struct smb_filename *smb_fname)
843 struct smb_filename *cap_smb_fname = NULL;
844 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
845 int ret;
846 int saved_errno = 0;
848 if (!cappath) {
849 errno = ENOMEM;
850 return -1;
852 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
853 cappath,
854 NULL,
855 NULL,
856 smb_fname->flags);
857 if (cap_smb_fname == NULL) {
858 TALLOC_FREE(cappath);
859 errno = ENOMEM;
860 return -1;
862 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
863 if (ret == -1) {
864 saved_errno = errno;
866 TALLOC_FREE(cappath);
867 TALLOC_FREE(cap_smb_fname);
868 if (saved_errno) {
869 errno = saved_errno;
871 return ret;
874 static ssize_t cap_getxattr(vfs_handle_struct *handle,
875 const struct smb_filename *smb_fname,
876 const char *name,
877 void *value,
878 size_t size)
880 struct smb_filename *cap_smb_fname = NULL;
881 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
882 char *capname = capencode(talloc_tos(), name);
883 ssize_t ret;
884 int saved_errno = 0;
886 if (!cappath || !capname) {
887 errno = ENOMEM;
888 return -1;
890 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
891 cappath,
892 NULL,
893 NULL,
894 smb_fname->flags);
895 if (cap_smb_fname == NULL) {
896 TALLOC_FREE(cappath);
897 TALLOC_FREE(capname);
898 errno = ENOMEM;
899 return -1;
901 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
902 capname, value, size);
903 if (ret == -1) {
904 saved_errno = errno;
906 TALLOC_FREE(cappath);
907 TALLOC_FREE(capname);
908 TALLOC_FREE(cap_smb_fname);
909 if (saved_errno) {
910 errno = saved_errno;
912 return ret;
915 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
917 char *cappath = capencode(talloc_tos(), path);
919 if (!cappath) {
920 errno = ENOMEM;
921 return -1;
923 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
926 static ssize_t cap_listxattr(vfs_handle_struct *handle,
927 const struct smb_filename *smb_fname,
928 char *list,
929 size_t size)
931 struct smb_filename *cap_smb_fname = NULL;
932 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
933 ssize_t ret;
934 int saved_errno = 0;
936 if (!cappath) {
937 errno = ENOMEM;
938 return -1;
940 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
941 cappath,
942 NULL,
943 NULL,
944 smb_fname->flags);
945 if (cap_smb_fname == NULL) {
946 TALLOC_FREE(cappath);
947 errno = ENOMEM;
948 return -1;
950 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
951 if (ret == -1) {
952 saved_errno = errno;
954 TALLOC_FREE(cappath);
955 TALLOC_FREE(cap_smb_fname);
956 if (saved_errno) {
957 errno = saved_errno;
959 return ret;
962 static int cap_removexattr(vfs_handle_struct *handle,
963 const struct smb_filename *smb_fname,
964 const char *name)
966 struct smb_filename *cap_smb_fname = NULL;
967 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
968 char *capname = capencode(talloc_tos(), name);
969 int ret;
970 int saved_errno = 0;
972 if (!cappath || !capname) {
973 errno = ENOMEM;
974 return -1;
976 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
977 cappath,
978 NULL,
979 NULL,
980 smb_fname->flags);
981 if (cap_smb_fname == NULL) {
982 TALLOC_FREE(cappath);
983 TALLOC_FREE(capname);
984 errno = ENOMEM;
985 return -1;
987 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
988 if (ret == -1) {
989 saved_errno = errno;
991 TALLOC_FREE(cappath);
992 TALLOC_FREE(capname);
993 TALLOC_FREE(cap_smb_fname);
994 if (saved_errno) {
995 errno = saved_errno;
997 return ret;
1000 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
1002 char *cappath = capencode(talloc_tos(), path);
1004 if (!cappath) {
1005 errno = ENOMEM;
1006 return -1;
1008 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
1011 static int cap_setxattr(vfs_handle_struct *handle,
1012 const struct smb_filename *smb_fname,
1013 const char *name,
1014 const void *value,
1015 size_t size,
1016 int flags)
1018 struct smb_filename *cap_smb_fname = NULL;
1019 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
1020 char *capname = capencode(talloc_tos(), name);
1021 int ret;
1022 int saved_errno = 0;
1024 if (!cappath || !capname) {
1025 errno = ENOMEM;
1026 return -1;
1028 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
1029 cappath,
1030 NULL,
1031 NULL,
1032 smb_fname->flags);
1033 if (cap_smb_fname == NULL) {
1034 TALLOC_FREE(cappath);
1035 TALLOC_FREE(capname);
1036 errno = ENOMEM;
1037 return -1;
1039 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
1040 capname, value, size, flags);
1041 if (ret == -1) {
1042 saved_errno = errno;
1044 TALLOC_FREE(cappath);
1045 TALLOC_FREE(capname);
1046 TALLOC_FREE(cap_smb_fname);
1047 if (saved_errno) {
1048 errno = saved_errno;
1050 return ret;
1053 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
1055 char *cappath = capencode(talloc_tos(), path);
1057 if (!cappath) {
1058 errno = ENOMEM;
1059 return -1;
1061 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
1064 static struct vfs_fn_pointers vfs_cap_fns = {
1065 .disk_free_fn = cap_disk_free,
1066 .get_quota_fn = cap_get_quota,
1067 .opendir_fn = cap_opendir,
1068 .readdir_fn = cap_readdir,
1069 .mkdir_fn = cap_mkdir,
1070 .rmdir_fn = cap_rmdir,
1071 .open_fn = cap_open,
1072 .renameat_fn = cap_renameat,
1073 .stat_fn = cap_stat,
1074 .lstat_fn = cap_lstat,
1075 .unlink_fn = cap_unlink,
1076 .chmod_fn = cap_chmod,
1077 .chown_fn = cap_chown,
1078 .lchown_fn = cap_lchown,
1079 .chdir_fn = cap_chdir,
1080 .ntimes_fn = cap_ntimes,
1081 .symlink_fn = cap_symlink,
1082 .readlink_fn = cap_readlink,
1083 .readlinkat_fn = cap_readlinkat,
1084 .linkat_fn = cap_linkat,
1085 .mknodat_fn = cap_mknodat,
1086 .realpath_fn = cap_realpath,
1087 .sys_acl_get_file_fn = cap_sys_acl_get_file,
1088 .sys_acl_set_file_fn = cap_sys_acl_set_file,
1089 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1090 .getxattr_fn = cap_getxattr,
1091 .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1092 .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1093 .fgetxattr_fn = cap_fgetxattr,
1094 .listxattr_fn = cap_listxattr,
1095 .removexattr_fn = cap_removexattr,
1096 .fremovexattr_fn = cap_fremovexattr,
1097 .setxattr_fn = cap_setxattr,
1098 .fsetxattr_fn = cap_fsetxattr
1101 static_decl_vfs;
1102 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1104 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1105 &vfs_cap_fns);
1108 /* For CAP functions */
1109 #define hex_tag ':'
1110 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
1111 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
1112 #define is_hex(s) ((s)[0] == hex_tag)
1114 static unsigned char hex2bin_table[256] = {
1115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1118 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1119 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1120 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1122 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1123 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
1134 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1136 /*******************************************************************
1137 original code -> ":xx" - CAP format
1138 ********************************************************************/
1140 static char *capencode(TALLOC_CTX *ctx, const char *from)
1142 char *out = NULL;
1143 const char *p1;
1144 char *to = NULL;
1145 size_t len = 0;
1147 for (p1 = from; *p1; p1++) {
1148 if ((unsigned char)*p1 >= 0x80) {
1149 len += 3;
1150 } else {
1151 len++;
1154 len++;
1156 to = talloc_array(ctx, char, len);
1157 if (!to) {
1158 return NULL;
1161 for (out = to; *from;) {
1162 /* buffer husoku error */
1163 if ((unsigned char)*from >= 0x80) {
1164 *out++ = hex_tag;
1165 *out++ = bin2hex (((*from)>>4)&0x0f);
1166 *out++ = bin2hex ((*from)&0x0f);
1167 from++;
1168 } else {
1169 *out++ = *from++;
1172 *out = '\0';
1173 return to;
1176 /*******************************************************************
1177 CAP -> original code
1178 ********************************************************************/
1179 /* ":xx" -> a byte */
1181 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1183 const char *p1;
1184 char *out = NULL;
1185 char *to = NULL;
1186 size_t len = 0;
1188 for (p1 = from; *p1; len++) {
1189 if (is_hex(p1)) {
1190 p1 += 3;
1191 } else {
1192 p1++;
1195 len++;
1197 to = talloc_array(ctx, char, len);
1198 if (!to) {
1199 return NULL;
1202 for (out = to; *from;) {
1203 if (is_hex(from)) {
1204 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1205 from += 3;
1206 } else {
1207 *out++ = *from++;
1210 *out = '\0';
1211 return to;