replmd: check for duplicate values in MOD_REPLACE case
[Samba.git] / source3 / modules / vfs_cap.c
blobe4d30cb4e5aa803c47c23548c122337fb27173ba
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 int cap_chmod_acl(vfs_handle_struct *handle,
707 const struct smb_filename *smb_fname,
708 mode_t mode)
710 struct smb_filename *cap_smb_fname = NULL;
711 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
712 int ret;
713 int saved_errno;
715 /* If the underlying VFS doesn't have ACL support... */
716 if (!cappath) {
717 errno = ENOMEM;
718 return -1;
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 -1;
731 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
732 saved_errno = errno;
733 TALLOC_FREE(cappath);
734 TALLOC_FREE(cap_smb_fname);
735 errno = saved_errno;
736 return ret;
739 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
740 const struct smb_filename *smb_fname,
741 SMB_ACL_TYPE_T type,
742 TALLOC_CTX *mem_ctx)
744 struct smb_filename *cap_smb_fname = NULL;
745 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
746 SMB_ACL_T ret;
747 int saved_errno = 0;
749 if (!cappath) {
750 errno = ENOMEM;
751 return (SMB_ACL_T)NULL;
753 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
754 cappath,
755 NULL,
756 NULL,
757 smb_fname->flags);
758 if (cap_smb_fname == NULL) {
759 TALLOC_FREE(cappath);
760 errno = ENOMEM;
761 return (SMB_ACL_T)NULL;
763 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
764 type, mem_ctx);
765 if (ret == NULL) {
766 saved_errno = errno;
768 TALLOC_FREE(cappath);
769 TALLOC_FREE(cap_smb_fname);
770 if (saved_errno != 0) {
771 errno = saved_errno;
773 return ret;
776 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
777 const struct smb_filename *smb_fname,
778 SMB_ACL_TYPE_T acltype,
779 SMB_ACL_T theacl)
781 struct smb_filename *cap_smb_fname = NULL;
782 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
783 int ret;
784 int saved_errno = 0;
786 if (!cappath) {
787 errno = ENOMEM;
788 return -1;
790 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
791 cappath,
792 NULL,
793 NULL,
794 smb_fname->flags);
795 if (cap_smb_fname == NULL) {
796 TALLOC_FREE(cappath);
797 errno = ENOMEM;
798 return -1;
800 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
801 acltype, theacl);
802 if (ret == -1) {
803 saved_errno = errno;
805 TALLOC_FREE(cappath);
806 TALLOC_FREE(cap_smb_fname);
807 if (saved_errno != 0) {
808 errno = saved_errno;
810 return ret;
813 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
814 const struct smb_filename *smb_fname)
816 struct smb_filename *cap_smb_fname = NULL;
817 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
818 int ret;
819 int saved_errno = 0;
821 if (!cappath) {
822 errno = ENOMEM;
823 return -1;
825 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
826 cappath,
827 NULL,
828 NULL,
829 smb_fname->flags);
830 if (cap_smb_fname == NULL) {
831 TALLOC_FREE(cappath);
832 errno = ENOMEM;
833 return -1;
835 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
836 if (ret == -1) {
837 saved_errno = errno;
839 TALLOC_FREE(cappath);
840 TALLOC_FREE(cap_smb_fname);
841 if (saved_errno) {
842 errno = saved_errno;
844 return ret;
847 static ssize_t cap_getxattr(vfs_handle_struct *handle,
848 const struct smb_filename *smb_fname,
849 const char *name,
850 void *value,
851 size_t size)
853 struct smb_filename *cap_smb_fname = NULL;
854 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
855 char *capname = capencode(talloc_tos(), name);
856 ssize_t ret;
857 int saved_errno = 0;
859 if (!cappath || !capname) {
860 errno = ENOMEM;
861 return -1;
863 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
864 cappath,
865 NULL,
866 NULL,
867 smb_fname->flags);
868 if (cap_smb_fname == NULL) {
869 TALLOC_FREE(cappath);
870 TALLOC_FREE(capname);
871 errno = ENOMEM;
872 return -1;
874 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
875 capname, value, size);
876 if (ret == -1) {
877 saved_errno = errno;
879 TALLOC_FREE(cappath);
880 TALLOC_FREE(capname);
881 TALLOC_FREE(cap_smb_fname);
882 if (saved_errno) {
883 errno = saved_errno;
885 return ret;
888 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
890 char *cappath = capencode(talloc_tos(), path);
892 if (!cappath) {
893 errno = ENOMEM;
894 return -1;
896 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
899 static ssize_t cap_listxattr(vfs_handle_struct *handle,
900 const struct smb_filename *smb_fname,
901 char *list,
902 size_t size)
904 struct smb_filename *cap_smb_fname = NULL;
905 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
906 ssize_t ret;
907 int saved_errno = 0;
909 if (!cappath) {
910 errno = ENOMEM;
911 return -1;
913 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
914 cappath,
915 NULL,
916 NULL,
917 smb_fname->flags);
918 if (cap_smb_fname == NULL) {
919 TALLOC_FREE(cappath);
920 errno = ENOMEM;
921 return -1;
923 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
924 if (ret == -1) {
925 saved_errno = errno;
927 TALLOC_FREE(cappath);
928 TALLOC_FREE(cap_smb_fname);
929 if (saved_errno) {
930 errno = saved_errno;
932 return ret;
935 static int cap_removexattr(vfs_handle_struct *handle,
936 const struct smb_filename *smb_fname,
937 const char *name)
939 struct smb_filename *cap_smb_fname = NULL;
940 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
941 char *capname = capencode(talloc_tos(), name);
942 int ret;
943 int saved_errno = 0;
945 if (!cappath || !capname) {
946 errno = ENOMEM;
947 return -1;
949 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
950 cappath,
951 NULL,
952 NULL,
953 smb_fname->flags);
954 if (cap_smb_fname == NULL) {
955 TALLOC_FREE(cappath);
956 TALLOC_FREE(capname);
957 errno = ENOMEM;
958 return -1;
960 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
961 if (ret == -1) {
962 saved_errno = errno;
964 TALLOC_FREE(cappath);
965 TALLOC_FREE(capname);
966 TALLOC_FREE(cap_smb_fname);
967 if (saved_errno) {
968 errno = saved_errno;
970 return ret;
973 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
975 char *cappath = capencode(talloc_tos(), path);
977 if (!cappath) {
978 errno = ENOMEM;
979 return -1;
981 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
984 static int cap_setxattr(vfs_handle_struct *handle,
985 const struct smb_filename *smb_fname,
986 const char *name,
987 const void *value,
988 size_t size,
989 int flags)
991 struct smb_filename *cap_smb_fname = NULL;
992 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
993 char *capname = capencode(talloc_tos(), name);
994 int ret;
995 int saved_errno = 0;
997 if (!cappath || !capname) {
998 errno = ENOMEM;
999 return -1;
1001 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
1002 cappath,
1003 NULL,
1004 NULL,
1005 smb_fname->flags);
1006 if (cap_smb_fname == NULL) {
1007 TALLOC_FREE(cappath);
1008 TALLOC_FREE(capname);
1009 errno = ENOMEM;
1010 return -1;
1012 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
1013 capname, value, size, flags);
1014 if (ret == -1) {
1015 saved_errno = errno;
1017 TALLOC_FREE(cappath);
1018 TALLOC_FREE(capname);
1019 TALLOC_FREE(cap_smb_fname);
1020 if (saved_errno) {
1021 errno = saved_errno;
1023 return ret;
1026 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
1028 char *cappath = capencode(talloc_tos(), path);
1030 if (!cappath) {
1031 errno = ENOMEM;
1032 return -1;
1034 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
1037 static struct vfs_fn_pointers vfs_cap_fns = {
1038 .disk_free_fn = cap_disk_free,
1039 .get_quota_fn = cap_get_quota,
1040 .opendir_fn = cap_opendir,
1041 .readdir_fn = cap_readdir,
1042 .mkdir_fn = cap_mkdir,
1043 .rmdir_fn = cap_rmdir,
1044 .open_fn = cap_open,
1045 .rename_fn = cap_rename,
1046 .stat_fn = cap_stat,
1047 .lstat_fn = cap_lstat,
1048 .unlink_fn = cap_unlink,
1049 .chmod_fn = cap_chmod,
1050 .chown_fn = cap_chown,
1051 .lchown_fn = cap_lchown,
1052 .chdir_fn = cap_chdir,
1053 .ntimes_fn = cap_ntimes,
1054 .symlink_fn = cap_symlink,
1055 .readlink_fn = cap_readlink,
1056 .link_fn = cap_link,
1057 .mknod_fn = cap_mknod,
1058 .realpath_fn = cap_realpath,
1059 .chmod_acl_fn = cap_chmod_acl,
1060 .sys_acl_get_file_fn = cap_sys_acl_get_file,
1061 .sys_acl_set_file_fn = cap_sys_acl_set_file,
1062 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1063 .getxattr_fn = cap_getxattr,
1064 .fgetxattr_fn = cap_fgetxattr,
1065 .listxattr_fn = cap_listxattr,
1066 .removexattr_fn = cap_removexattr,
1067 .fremovexattr_fn = cap_fremovexattr,
1068 .setxattr_fn = cap_setxattr,
1069 .fsetxattr_fn = cap_fsetxattr
1072 NTSTATUS vfs_cap_init(TALLOC_CTX *);
1073 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1075 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1076 &vfs_cap_fns);
1079 /* For CAP functions */
1080 #define hex_tag ':'
1081 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
1082 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
1083 #define is_hex(s) ((s)[0] == hex_tag)
1085 static unsigned char hex2bin_table[256] = {
1086 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1087 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1089 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1090 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1091 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1093 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1094 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1099 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
1105 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1107 /*******************************************************************
1108 original code -> ":xx" - CAP format
1109 ********************************************************************/
1111 static char *capencode(TALLOC_CTX *ctx, const char *from)
1113 char *out = NULL;
1114 const char *p1;
1115 char *to = NULL;
1116 size_t len = 0;
1118 for (p1 = from; *p1; p1++) {
1119 if ((unsigned char)*p1 >= 0x80) {
1120 len += 3;
1121 } else {
1122 len++;
1125 len++;
1127 to = talloc_array(ctx, char, len);
1128 if (!to) {
1129 return NULL;
1132 for (out = to; *from;) {
1133 /* buffer husoku error */
1134 if ((unsigned char)*from >= 0x80) {
1135 *out++ = hex_tag;
1136 *out++ = bin2hex (((*from)>>4)&0x0f);
1137 *out++ = bin2hex ((*from)&0x0f);
1138 from++;
1139 } else {
1140 *out++ = *from++;
1143 *out = '\0';
1144 return to;
1147 /*******************************************************************
1148 CAP -> original code
1149 ********************************************************************/
1150 /* ":xx" -> a byte */
1152 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1154 const char *p1;
1155 char *out = NULL;
1156 char *to = NULL;
1157 size_t len = 0;
1159 for (p1 = from; *p1; len++) {
1160 if (is_hex(p1)) {
1161 p1 += 3;
1162 } else {
1163 p1++;
1166 len++;
1168 to = talloc_array(ctx, char, len);
1169 if (!to) {
1170 return NULL;
1173 for (out = to; *from;) {
1174 if (is_hex(from)) {
1175 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1176 from += 3;
1177 } else {
1178 *out++ = *from++;
1181 *out = '\0';
1182 return to;