s3: VFS: Change SMB_VFS_CHDIR to use const struct smb_filename * instead of const...
[Samba.git] / source3 / modules / vfs_cap.c
blob44a81bbcefb6d4a9a08197ee469cbb3637d5fbe5
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 char *cap_realpath(vfs_handle_struct *handle, const char *path)
672 /* monyo need capencode'ed and capdecode'ed? */
673 char *cappath = capencode(talloc_tos(), path);
675 if (!cappath) {
676 errno = ENOMEM;
677 return NULL;
679 return SMB_VFS_NEXT_REALPATH(handle, cappath);
682 static int cap_chmod_acl(vfs_handle_struct *handle,
683 const struct smb_filename *smb_fname,
684 mode_t mode)
686 struct smb_filename *cap_smb_fname = NULL;
687 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
688 int ret;
689 int saved_errno;
691 /* If the underlying VFS doesn't have ACL support... */
692 if (!cappath) {
693 errno = ENOMEM;
694 return -1;
696 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
697 cappath,
698 NULL,
699 NULL,
700 smb_fname->flags);
701 if (cap_smb_fname == NULL) {
702 TALLOC_FREE(cappath);
703 errno = ENOMEM;
704 return -1;
707 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
708 saved_errno = errno;
709 TALLOC_FREE(cappath);
710 TALLOC_FREE(cap_smb_fname);
711 errno = saved_errno;
712 return ret;
715 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
716 const struct smb_filename *smb_fname,
717 SMB_ACL_TYPE_T type,
718 TALLOC_CTX *mem_ctx)
720 struct smb_filename *cap_smb_fname = NULL;
721 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
722 SMB_ACL_T ret;
723 int saved_errno = 0;
725 if (!cappath) {
726 errno = ENOMEM;
727 return (SMB_ACL_T)NULL;
729 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
730 cappath,
731 NULL,
732 NULL,
733 smb_fname->flags);
734 if (cap_smb_fname == NULL) {
735 TALLOC_FREE(cappath);
736 errno = ENOMEM;
737 return (SMB_ACL_T)NULL;
739 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
740 type, mem_ctx);
741 if (ret == NULL) {
742 saved_errno = errno;
744 TALLOC_FREE(cappath);
745 TALLOC_FREE(cap_smb_fname);
746 if (saved_errno != 0) {
747 errno = saved_errno;
749 return ret;
752 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
753 const struct smb_filename *smb_fname,
754 SMB_ACL_TYPE_T acltype,
755 SMB_ACL_T theacl)
757 struct smb_filename *cap_smb_fname = NULL;
758 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
759 int ret;
760 int saved_errno = 0;
762 if (!cappath) {
763 errno = ENOMEM;
764 return -1;
766 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
767 cappath,
768 NULL,
769 NULL,
770 smb_fname->flags);
771 if (cap_smb_fname == NULL) {
772 TALLOC_FREE(cappath);
773 errno = ENOMEM;
774 return -1;
776 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
777 acltype, theacl);
778 if (ret == -1) {
779 saved_errno = errno;
781 TALLOC_FREE(cappath);
782 TALLOC_FREE(cap_smb_fname);
783 if (saved_errno != 0) {
784 errno = saved_errno;
786 return ret;
789 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
790 const struct smb_filename *smb_fname)
792 struct smb_filename *cap_smb_fname = NULL;
793 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
794 int ret;
795 int saved_errno = 0;
797 if (!cappath) {
798 errno = ENOMEM;
799 return -1;
801 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
802 cappath,
803 NULL,
804 NULL,
805 smb_fname->flags);
806 if (cap_smb_fname == NULL) {
807 TALLOC_FREE(cappath);
808 errno = ENOMEM;
809 return -1;
811 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
812 if (ret == -1) {
813 saved_errno = errno;
815 TALLOC_FREE(cappath);
816 TALLOC_FREE(cap_smb_fname);
817 if (saved_errno) {
818 errno = saved_errno;
820 return ret;
823 static ssize_t cap_getxattr(vfs_handle_struct *handle,
824 const struct smb_filename *smb_fname,
825 const char *name,
826 void *value,
827 size_t size)
829 struct smb_filename *cap_smb_fname = NULL;
830 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
831 char *capname = capencode(talloc_tos(), name);
832 ssize_t ret;
833 int saved_errno = 0;
835 if (!cappath || !capname) {
836 errno = ENOMEM;
837 return -1;
839 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
840 cappath,
841 NULL,
842 NULL,
843 smb_fname->flags);
844 if (cap_smb_fname == NULL) {
845 TALLOC_FREE(cappath);
846 TALLOC_FREE(capname);
847 errno = ENOMEM;
848 return -1;
850 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
851 capname, value, size);
852 if (ret == -1) {
853 saved_errno = errno;
855 TALLOC_FREE(cappath);
856 TALLOC_FREE(capname);
857 TALLOC_FREE(cap_smb_fname);
858 if (saved_errno) {
859 errno = saved_errno;
861 return ret;
864 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
866 char *cappath = capencode(talloc_tos(), path);
868 if (!cappath) {
869 errno = ENOMEM;
870 return -1;
872 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
875 static ssize_t cap_listxattr(vfs_handle_struct *handle,
876 const struct smb_filename *smb_fname,
877 char *list,
878 size_t size)
880 struct smb_filename *cap_smb_fname = NULL;
881 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
882 ssize_t ret;
883 int saved_errno = 0;
885 if (!cappath) {
886 errno = ENOMEM;
887 return -1;
889 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
890 cappath,
891 NULL,
892 NULL,
893 smb_fname->flags);
894 if (cap_smb_fname == NULL) {
895 TALLOC_FREE(cappath);
896 errno = ENOMEM;
897 return -1;
899 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
900 if (ret == -1) {
901 saved_errno = errno;
903 TALLOC_FREE(cappath);
904 TALLOC_FREE(cap_smb_fname);
905 if (saved_errno) {
906 errno = saved_errno;
908 return ret;
911 static int cap_removexattr(vfs_handle_struct *handle,
912 const struct smb_filename *smb_fname,
913 const char *name)
915 struct smb_filename *cap_smb_fname = NULL;
916 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
917 char *capname = capencode(talloc_tos(), name);
918 int ret;
919 int saved_errno = 0;
921 if (!cappath || !capname) {
922 errno = ENOMEM;
923 return -1;
925 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
926 cappath,
927 NULL,
928 NULL,
929 smb_fname->flags);
930 if (cap_smb_fname == NULL) {
931 TALLOC_FREE(cappath);
932 TALLOC_FREE(capname);
933 errno = ENOMEM;
934 return -1;
936 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
937 if (ret == -1) {
938 saved_errno = errno;
940 TALLOC_FREE(cappath);
941 TALLOC_FREE(capname);
942 TALLOC_FREE(cap_smb_fname);
943 if (saved_errno) {
944 errno = saved_errno;
946 return ret;
949 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
951 char *cappath = capencode(talloc_tos(), path);
953 if (!cappath) {
954 errno = ENOMEM;
955 return -1;
957 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
960 static int cap_setxattr(vfs_handle_struct *handle,
961 const struct smb_filename *smb_fname,
962 const char *name,
963 const void *value,
964 size_t size,
965 int flags)
967 struct smb_filename *cap_smb_fname = NULL;
968 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
969 char *capname = capencode(talloc_tos(), name);
970 int ret;
971 int saved_errno = 0;
973 if (!cappath || !capname) {
974 errno = ENOMEM;
975 return -1;
977 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
978 cappath,
979 NULL,
980 NULL,
981 smb_fname->flags);
982 if (cap_smb_fname == NULL) {
983 TALLOC_FREE(cappath);
984 TALLOC_FREE(capname);
985 errno = ENOMEM;
986 return -1;
988 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
989 capname, value, size, flags);
990 if (ret == -1) {
991 saved_errno = errno;
993 TALLOC_FREE(cappath);
994 TALLOC_FREE(capname);
995 TALLOC_FREE(cap_smb_fname);
996 if (saved_errno) {
997 errno = saved_errno;
999 return ret;
1002 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
1004 char *cappath = capencode(talloc_tos(), path);
1006 if (!cappath) {
1007 errno = ENOMEM;
1008 return -1;
1010 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
1013 static struct vfs_fn_pointers vfs_cap_fns = {
1014 .disk_free_fn = cap_disk_free,
1015 .get_quota_fn = cap_get_quota,
1016 .opendir_fn = cap_opendir,
1017 .readdir_fn = cap_readdir,
1018 .mkdir_fn = cap_mkdir,
1019 .rmdir_fn = cap_rmdir,
1020 .open_fn = cap_open,
1021 .rename_fn = cap_rename,
1022 .stat_fn = cap_stat,
1023 .lstat_fn = cap_lstat,
1024 .unlink_fn = cap_unlink,
1025 .chmod_fn = cap_chmod,
1026 .chown_fn = cap_chown,
1027 .lchown_fn = cap_lchown,
1028 .chdir_fn = cap_chdir,
1029 .ntimes_fn = cap_ntimes,
1030 .symlink_fn = cap_symlink,
1031 .readlink_fn = cap_readlink,
1032 .link_fn = cap_link,
1033 .mknod_fn = cap_mknod,
1034 .realpath_fn = cap_realpath,
1035 .chmod_acl_fn = cap_chmod_acl,
1036 .sys_acl_get_file_fn = cap_sys_acl_get_file,
1037 .sys_acl_set_file_fn = cap_sys_acl_set_file,
1038 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1039 .getxattr_fn = cap_getxattr,
1040 .fgetxattr_fn = cap_fgetxattr,
1041 .listxattr_fn = cap_listxattr,
1042 .removexattr_fn = cap_removexattr,
1043 .fremovexattr_fn = cap_fremovexattr,
1044 .setxattr_fn = cap_setxattr,
1045 .fsetxattr_fn = cap_fsetxattr
1048 NTSTATUS vfs_cap_init(TALLOC_CTX *);
1049 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1051 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1052 &vfs_cap_fns);
1055 /* For CAP functions */
1056 #define hex_tag ':'
1057 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
1058 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
1059 #define is_hex(s) ((s)[0] == hex_tag)
1061 static unsigned char hex2bin_table[256] = {
1062 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1063 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1065 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1066 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1067 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1069 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1070 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1071 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1072 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1073 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1075 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1079 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
1081 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1083 /*******************************************************************
1084 original code -> ":xx" - CAP format
1085 ********************************************************************/
1087 static char *capencode(TALLOC_CTX *ctx, const char *from)
1089 char *out = NULL;
1090 const char *p1;
1091 char *to = NULL;
1092 size_t len = 0;
1094 for (p1 = from; *p1; p1++) {
1095 if ((unsigned char)*p1 >= 0x80) {
1096 len += 3;
1097 } else {
1098 len++;
1101 len++;
1103 to = talloc_array(ctx, char, len);
1104 if (!to) {
1105 return NULL;
1108 for (out = to; *from;) {
1109 /* buffer husoku error */
1110 if ((unsigned char)*from >= 0x80) {
1111 *out++ = hex_tag;
1112 *out++ = bin2hex (((*from)>>4)&0x0f);
1113 *out++ = bin2hex ((*from)&0x0f);
1114 from++;
1115 } else {
1116 *out++ = *from++;
1119 *out = '\0';
1120 return to;
1123 /*******************************************************************
1124 CAP -> original code
1125 ********************************************************************/
1126 /* ":xx" -> a byte */
1128 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1130 const char *p1;
1131 char *out = NULL;
1132 char *to = NULL;
1133 size_t len = 0;
1135 for (p1 = from; *p1; len++) {
1136 if (is_hex(p1)) {
1137 p1 += 3;
1138 } else {
1139 p1++;
1142 len++;
1144 to = talloc_array(ctx, char, len);
1145 if (!to) {
1146 return NULL;
1149 for (out = to; *from;) {
1150 if (is_hex(from)) {
1151 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1152 from += 3;
1153 } else {
1154 *out++ = *from++;
1157 *out = '\0';
1158 return to;