s3: VFS: Change SMB_VFS_MKNOD to use const struct smb_filename * instead of const...
[Samba.git] / source3 / modules / vfs_cap.c
blobeba58961926f3d743a784bb014a24303957e802b
1 /*
2 * CAP VFS module for Samba 3.x Version 0.3
4 * Copyright (C) Tim Potter, 1999-2000
5 * Copyright (C) Alexander Bokovoy, 2002-2003
6 * Copyright (C) Stefan (metze) Metzmacher, 2003
7 * Copyright (C) TAKAHASHI Motonobu (monyo), 2003
8 * Copyright (C) Jeremy Allison, 2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "smbd/smbd.h"
28 /* cap functions */
29 static char *capencode(TALLOC_CTX *ctx, const char *from);
30 static char *capdecode(TALLOC_CTX *ctx, const char *from);
32 static uint64_t cap_disk_free(vfs_handle_struct *handle, const char *path,
33 uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
35 char *cappath = capencode(talloc_tos(), path);
37 if (!cappath) {
38 errno = ENOMEM;
39 return (uint64_t)-1;
41 return SMB_VFS_NEXT_DISK_FREE(handle, cappath, bsize, dfree, dsize);
44 static int cap_get_quota(vfs_handle_struct *handle, const char *path,
45 enum SMB_QUOTA_TYPE qtype, unid_t id,
46 SMB_DISK_QUOTA *dq)
48 char *cappath = capencode(talloc_tos(), path);
50 if (!cappath) {
51 errno = ENOMEM;
52 return -1;
54 return SMB_VFS_NEXT_GET_QUOTA(handle, cappath, qtype, id, dq);
57 static DIR *cap_opendir(vfs_handle_struct *handle,
58 const struct smb_filename *smb_fname,
59 const char *mask,
60 uint32_t attr)
62 char *capname = capencode(talloc_tos(), smb_fname->base_name);
63 struct smb_filename *cap_smb_fname = NULL;
65 if (!capname) {
66 errno = ENOMEM;
67 return NULL;
69 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
70 capname,
71 NULL,
72 NULL,
73 smb_fname->flags);
74 if (cap_smb_fname == NULL) {
75 TALLOC_FREE(capname);
76 errno = ENOMEM;
77 return NULL;
79 return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
82 static struct dirent *cap_readdir(vfs_handle_struct *handle,
83 DIR *dirp,
84 SMB_STRUCT_STAT *sbuf)
86 struct dirent *result;
87 struct dirent *newdirent;
88 char *newname;
89 size_t newnamelen;
90 DEBUG(3,("cap: cap_readdir\n"));
92 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
93 if (!result) {
94 return NULL;
97 newname = capdecode(talloc_tos(), result->d_name);
98 if (!newname) {
99 return NULL;
101 DEBUG(3,("cap: cap_readdir: %s\n", newname));
102 newnamelen = strlen(newname)+1;
103 newdirent = talloc_size(
104 talloc_tos(), sizeof(struct dirent) + newnamelen);
105 if (!newdirent) {
106 return NULL;
108 talloc_set_name_const(newdirent, "struct dirent");
109 memcpy(newdirent, result, sizeof(struct dirent));
110 memcpy(&newdirent->d_name, newname, newnamelen);
111 return newdirent;
114 static int cap_mkdir(vfs_handle_struct *handle,
115 const struct smb_filename *smb_fname,
116 mode_t mode)
118 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
119 struct smb_filename *cap_smb_fname = NULL;
121 if (!cappath) {
122 errno = ENOMEM;
123 return -1;
126 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
127 cappath,
128 NULL,
129 NULL,
130 smb_fname->flags);
131 if (cap_smb_fname == NULL) {
132 TALLOC_FREE(cappath);
133 errno = ENOMEM;
134 return -1;
137 return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
140 static int cap_rmdir(vfs_handle_struct *handle,
141 const struct smb_filename *smb_fname)
143 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
144 struct smb_filename *cap_smb_fname = NULL;
146 if (!cappath) {
147 errno = ENOMEM;
148 return -1;
151 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
152 cappath,
153 NULL,
154 NULL,
155 smb_fname->flags);
156 if (cap_smb_fname == NULL) {
157 TALLOC_FREE(cappath);
158 errno = ENOMEM;
159 return -1;
162 return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
165 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
166 files_struct *fsp, int flags, mode_t mode)
168 char *cappath;
169 char *tmp_base_name = NULL;
170 int ret;
172 cappath = capencode(talloc_tos(), smb_fname->base_name);
174 if (!cappath) {
175 errno = ENOMEM;
176 return -1;
179 tmp_base_name = smb_fname->base_name;
180 smb_fname->base_name = cappath;
182 DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
183 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
185 smb_fname->base_name = tmp_base_name;
186 TALLOC_FREE(cappath);
188 return ret;
191 static int cap_rename(vfs_handle_struct *handle,
192 const struct smb_filename *smb_fname_src,
193 const struct smb_filename *smb_fname_dst)
195 char *capold = NULL;
196 char *capnew = NULL;
197 struct smb_filename *smb_fname_src_tmp = NULL;
198 struct smb_filename *smb_fname_dst_tmp = NULL;
199 int ret = -1;
201 capold = capencode(talloc_tos(), smb_fname_src->base_name);
202 capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
203 if (!capold || !capnew) {
204 errno = ENOMEM;
205 goto out;
208 /* Setup temporary smb_filename structs. */
209 smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
210 if (smb_fname_src_tmp == NULL) {
211 errno = ENOMEM;
212 goto out;
214 smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
215 if (smb_fname_dst_tmp == NULL) {
216 errno = ENOMEM;
217 goto out;
220 smb_fname_src_tmp->base_name = capold;
221 smb_fname_dst_tmp->base_name = capnew;
223 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
224 smb_fname_dst_tmp);
225 out:
226 TALLOC_FREE(capold);
227 TALLOC_FREE(capnew);
228 TALLOC_FREE(smb_fname_src_tmp);
229 TALLOC_FREE(smb_fname_dst_tmp);
231 return ret;
234 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
236 char *cappath;
237 char *tmp_base_name = NULL;
238 int ret;
240 cappath = capencode(talloc_tos(), smb_fname->base_name);
242 if (!cappath) {
243 errno = ENOMEM;
244 return -1;
247 tmp_base_name = smb_fname->base_name;
248 smb_fname->base_name = cappath;
250 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
252 smb_fname->base_name = tmp_base_name;
253 TALLOC_FREE(cappath);
255 return ret;
258 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
260 char *cappath;
261 char *tmp_base_name = NULL;
262 int ret;
264 cappath = capencode(talloc_tos(), smb_fname->base_name);
266 if (!cappath) {
267 errno = ENOMEM;
268 return -1;
271 tmp_base_name = smb_fname->base_name;
272 smb_fname->base_name = cappath;
274 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
276 smb_fname->base_name = tmp_base_name;
277 TALLOC_FREE(cappath);
279 return ret;
282 static int cap_unlink(vfs_handle_struct *handle,
283 const struct smb_filename *smb_fname)
285 struct smb_filename *smb_fname_tmp = NULL;
286 char *cappath = NULL;
287 int ret;
289 cappath = capencode(talloc_tos(), smb_fname->base_name);
290 if (!cappath) {
291 errno = ENOMEM;
292 return -1;
295 /* Setup temporary smb_filename structs. */
296 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
297 if (smb_fname_tmp == NULL) {
298 errno = ENOMEM;
299 return -1;
302 smb_fname_tmp->base_name = cappath;
304 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
306 TALLOC_FREE(smb_fname_tmp);
307 return ret;
310 static int cap_chmod(vfs_handle_struct *handle,
311 const struct smb_filename *smb_fname,
312 mode_t mode)
314 struct smb_filename *cap_smb_fname = NULL;
315 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
316 int ret;
317 int saved_errno;
319 if (!cappath) {
320 errno = ENOMEM;
321 return -1;
324 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
325 cappath,
326 NULL,
327 NULL,
328 smb_fname->flags);
329 if (cap_smb_fname == NULL) {
330 TALLOC_FREE(cappath);
331 errno = ENOMEM;
332 return -1;
335 ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
336 saved_errno = errno;
337 TALLOC_FREE(cappath);
338 TALLOC_FREE(cap_smb_fname);
339 errno = saved_errno;
340 return ret;
343 static int cap_chown(vfs_handle_struct *handle,
344 const struct smb_filename *smb_fname,
345 uid_t uid,
346 gid_t gid)
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_CHOWN(handle, cap_smb_fname, uid, gid);
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_lchown(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_LCHOWN(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_chdir(vfs_handle_struct *handle, const char *path)
413 char *cappath = capencode(talloc_tos(), path);
415 if (!cappath) {
416 errno = ENOMEM;
417 return -1;
419 DEBUG(3,("cap: cap_chdir for %s\n", path));
420 return SMB_VFS_NEXT_CHDIR(handle, cappath);
423 static int cap_ntimes(vfs_handle_struct *handle,
424 const struct smb_filename *smb_fname,
425 struct smb_file_time *ft)
427 struct smb_filename *smb_fname_tmp = NULL;
428 char *cappath = NULL;
429 int ret;
431 cappath = capencode(talloc_tos(), smb_fname->base_name);
433 if (!cappath) {
434 errno = ENOMEM;
435 return -1;
438 /* Setup temporary smb_filename structs. */
439 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
440 if (smb_fname_tmp == NULL) {
441 errno = ENOMEM;
442 return -1;
445 smb_fname_tmp->base_name = cappath;
447 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
449 TALLOC_FREE(smb_fname_tmp);
450 return ret;
454 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
455 const char *newpath)
457 char *capold = capencode(talloc_tos(), oldpath);
458 char *capnew = capencode(talloc_tos(), newpath);
460 if (!capold || !capnew) {
461 errno = ENOMEM;
462 return -1;
464 return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
467 static int cap_readlink(vfs_handle_struct *handle, const char *path,
468 char *buf, size_t bufsiz)
470 char *cappath = capencode(talloc_tos(), path);
472 if (!cappath) {
473 errno = ENOMEM;
474 return -1;
476 return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
479 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
481 char *capold = capencode(talloc_tos(), oldpath);
482 char *capnew = capencode(talloc_tos(), newpath);
484 if (!capold || !capnew) {
485 errno = ENOMEM;
486 return -1;
488 return SMB_VFS_NEXT_LINK(handle, capold, capnew);
491 static int cap_mknod(vfs_handle_struct *handle,
492 const struct smb_filename *smb_fname,
493 mode_t mode,
494 SMB_DEV_T dev)
496 struct smb_filename *cap_smb_fname = NULL;
497 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
498 int ret;
499 int saved_errno = 0;
501 if (!cappath) {
502 errno = ENOMEM;
503 return -1;
505 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
506 cappath,
507 NULL,
508 NULL,
509 smb_fname->flags);
510 if (cap_smb_fname == NULL) {
511 TALLOC_FREE(cappath);
512 errno = ENOMEM;
513 return -1;
515 ret = SMB_VFS_NEXT_MKNOD(handle, cap_smb_fname, mode, dev);
516 if (ret == -1) {
517 saved_errno = errno;
519 TALLOC_FREE(cappath);
520 TALLOC_FREE(cap_smb_fname);
521 if (saved_errno != 0) {
522 errno = saved_errno;
524 return ret;
527 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
529 /* monyo need capencode'ed and capdecode'ed? */
530 char *cappath = capencode(talloc_tos(), path);
532 if (!cappath) {
533 errno = ENOMEM;
534 return NULL;
536 return SMB_VFS_NEXT_REALPATH(handle, cappath);
539 static int cap_chmod_acl(vfs_handle_struct *handle,
540 const struct smb_filename *smb_fname,
541 mode_t mode)
543 struct smb_filename *cap_smb_fname = NULL;
544 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
545 int ret;
546 int saved_errno;
548 /* If the underlying VFS doesn't have ACL support... */
549 if (!cappath) {
550 errno = ENOMEM;
551 return -1;
553 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
554 cappath,
555 NULL,
556 NULL,
557 smb_fname->flags);
558 if (cap_smb_fname == NULL) {
559 TALLOC_FREE(cappath);
560 errno = ENOMEM;
561 return -1;
564 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
565 saved_errno = errno;
566 TALLOC_FREE(cappath);
567 TALLOC_FREE(cap_smb_fname);
568 errno = saved_errno;
569 return ret;
572 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
573 const struct smb_filename *smb_fname,
574 SMB_ACL_TYPE_T type,
575 TALLOC_CTX *mem_ctx)
577 struct smb_filename *cap_smb_fname = NULL;
578 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
579 SMB_ACL_T ret;
580 int saved_errno = 0;
582 if (!cappath) {
583 errno = ENOMEM;
584 return (SMB_ACL_T)NULL;
586 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
587 cappath,
588 NULL,
589 NULL,
590 smb_fname->flags);
591 if (cap_smb_fname == NULL) {
592 TALLOC_FREE(cappath);
593 errno = ENOMEM;
594 return (SMB_ACL_T)NULL;
596 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
597 type, mem_ctx);
598 if (ret == NULL) {
599 saved_errno = errno;
601 TALLOC_FREE(cappath);
602 TALLOC_FREE(cap_smb_fname);
603 if (saved_errno != 0) {
604 errno = saved_errno;
606 return ret;
609 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
610 const struct smb_filename *smb_fname,
611 SMB_ACL_TYPE_T acltype,
612 SMB_ACL_T theacl)
614 struct smb_filename *cap_smb_fname = NULL;
615 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
616 int ret;
617 int saved_errno = 0;
619 if (!cappath) {
620 errno = ENOMEM;
621 return -1;
623 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
624 cappath,
625 NULL,
626 NULL,
627 smb_fname->flags);
628 if (cap_smb_fname == NULL) {
629 TALLOC_FREE(cappath);
630 errno = ENOMEM;
631 return -1;
633 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
634 acltype, theacl);
635 if (ret == -1) {
636 saved_errno = errno;
638 TALLOC_FREE(cappath);
639 TALLOC_FREE(cap_smb_fname);
640 if (saved_errno != 0) {
641 errno = saved_errno;
643 return ret;
646 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
647 const struct smb_filename *smb_fname)
649 struct smb_filename *cap_smb_fname = NULL;
650 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
651 int ret;
652 int saved_errno = 0;
654 if (!cappath) {
655 errno = ENOMEM;
656 return -1;
658 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
659 cappath,
660 NULL,
661 NULL,
662 smb_fname->flags);
663 if (cap_smb_fname == NULL) {
664 TALLOC_FREE(cappath);
665 errno = ENOMEM;
666 return -1;
668 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
669 if (ret == -1) {
670 saved_errno = errno;
672 TALLOC_FREE(cappath);
673 TALLOC_FREE(cap_smb_fname);
674 if (saved_errno) {
675 errno = saved_errno;
677 return ret;
680 static ssize_t cap_getxattr(vfs_handle_struct *handle,
681 const struct smb_filename *smb_fname,
682 const char *name,
683 void *value,
684 size_t size)
686 struct smb_filename *cap_smb_fname = NULL;
687 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
688 char *capname = capencode(talloc_tos(), name);
689 ssize_t ret;
690 int saved_errno = 0;
692 if (!cappath || !capname) {
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 TALLOC_FREE(capname);
704 errno = ENOMEM;
705 return -1;
707 ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
708 capname, value, size);
709 if (ret == -1) {
710 saved_errno = errno;
712 TALLOC_FREE(cappath);
713 TALLOC_FREE(capname);
714 TALLOC_FREE(cap_smb_fname);
715 if (saved_errno) {
716 errno = saved_errno;
718 return ret;
721 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
723 char *cappath = capencode(talloc_tos(), path);
725 if (!cappath) {
726 errno = ENOMEM;
727 return -1;
729 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
732 static ssize_t cap_listxattr(vfs_handle_struct *handle,
733 const struct smb_filename *smb_fname,
734 char *list,
735 size_t size)
737 struct smb_filename *cap_smb_fname = NULL;
738 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
739 ssize_t ret;
740 int saved_errno = 0;
742 if (!cappath) {
743 errno = ENOMEM;
744 return -1;
746 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
747 cappath,
748 NULL,
749 NULL,
750 smb_fname->flags);
751 if (cap_smb_fname == NULL) {
752 TALLOC_FREE(cappath);
753 errno = ENOMEM;
754 return -1;
756 ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
757 if (ret == -1) {
758 saved_errno = errno;
760 TALLOC_FREE(cappath);
761 TALLOC_FREE(cap_smb_fname);
762 if (saved_errno) {
763 errno = saved_errno;
765 return ret;
768 static int cap_removexattr(vfs_handle_struct *handle,
769 const struct smb_filename *smb_fname,
770 const char *name)
772 struct smb_filename *cap_smb_fname = NULL;
773 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
774 char *capname = capencode(talloc_tos(), name);
775 int ret;
776 int saved_errno = 0;
778 if (!cappath || !capname) {
779 errno = ENOMEM;
780 return -1;
782 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
783 cappath,
784 NULL,
785 NULL,
786 smb_fname->flags);
787 if (cap_smb_fname == NULL) {
788 TALLOC_FREE(cappath);
789 TALLOC_FREE(capname);
790 errno = ENOMEM;
791 return -1;
793 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
794 if (ret == -1) {
795 saved_errno = errno;
797 TALLOC_FREE(cappath);
798 TALLOC_FREE(capname);
799 TALLOC_FREE(cap_smb_fname);
800 if (saved_errno) {
801 errno = saved_errno;
803 return ret;
806 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
808 char *cappath = capencode(talloc_tos(), path);
810 if (!cappath) {
811 errno = ENOMEM;
812 return -1;
814 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
817 static int cap_setxattr(vfs_handle_struct *handle,
818 const struct smb_filename *smb_fname,
819 const char *name,
820 const void *value,
821 size_t size,
822 int flags)
824 struct smb_filename *cap_smb_fname = NULL;
825 char *cappath = capencode(talloc_tos(), smb_fname->base_name);
826 char *capname = capencode(talloc_tos(), name);
827 int ret;
828 int saved_errno = 0;
830 if (!cappath || !capname) {
831 errno = ENOMEM;
832 return -1;
834 cap_smb_fname = synthetic_smb_fname(talloc_tos(),
835 cappath,
836 NULL,
837 NULL,
838 smb_fname->flags);
839 if (cap_smb_fname == NULL) {
840 TALLOC_FREE(cappath);
841 TALLOC_FREE(capname);
842 errno = ENOMEM;
843 return -1;
845 ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
846 capname, value, size, flags);
847 if (ret == -1) {
848 saved_errno = errno;
850 TALLOC_FREE(cappath);
851 TALLOC_FREE(capname);
852 TALLOC_FREE(cap_smb_fname);
853 if (saved_errno) {
854 errno = saved_errno;
856 return ret;
859 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
861 char *cappath = capencode(talloc_tos(), path);
863 if (!cappath) {
864 errno = ENOMEM;
865 return -1;
867 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
870 static struct vfs_fn_pointers vfs_cap_fns = {
871 .disk_free_fn = cap_disk_free,
872 .get_quota_fn = cap_get_quota,
873 .opendir_fn = cap_opendir,
874 .readdir_fn = cap_readdir,
875 .mkdir_fn = cap_mkdir,
876 .rmdir_fn = cap_rmdir,
877 .open_fn = cap_open,
878 .rename_fn = cap_rename,
879 .stat_fn = cap_stat,
880 .lstat_fn = cap_lstat,
881 .unlink_fn = cap_unlink,
882 .chmod_fn = cap_chmod,
883 .chown_fn = cap_chown,
884 .lchown_fn = cap_lchown,
885 .chdir_fn = cap_chdir,
886 .ntimes_fn = cap_ntimes,
887 .symlink_fn = cap_symlink,
888 .readlink_fn = cap_readlink,
889 .link_fn = cap_link,
890 .mknod_fn = cap_mknod,
891 .realpath_fn = cap_realpath,
892 .chmod_acl_fn = cap_chmod_acl,
893 .sys_acl_get_file_fn = cap_sys_acl_get_file,
894 .sys_acl_set_file_fn = cap_sys_acl_set_file,
895 .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
896 .getxattr_fn = cap_getxattr,
897 .fgetxattr_fn = cap_fgetxattr,
898 .listxattr_fn = cap_listxattr,
899 .removexattr_fn = cap_removexattr,
900 .fremovexattr_fn = cap_fremovexattr,
901 .setxattr_fn = cap_setxattr,
902 .fsetxattr_fn = cap_fsetxattr
905 NTSTATUS vfs_cap_init(TALLOC_CTX *);
906 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
908 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
909 &vfs_cap_fns);
912 /* For CAP functions */
913 #define hex_tag ':'
914 #define hex2bin(c) hex2bin_table[(unsigned char)(c)]
915 #define bin2hex(c) bin2hex_table[(unsigned char)(c)]
916 #define is_hex(s) ((s)[0] == hex_tag)
918 static unsigned char hex2bin_table[256] = {
919 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
920 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
921 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
922 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
923 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
924 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
925 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
926 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
927 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
928 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
929 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
930 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
931 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
932 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
933 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
934 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
935 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
936 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xf0 */
938 static unsigned char bin2hex_table[256] = "0123456789abcdef";
940 /*******************************************************************
941 original code -> ":xx" - CAP format
942 ********************************************************************/
944 static char *capencode(TALLOC_CTX *ctx, const char *from)
946 char *out = NULL;
947 const char *p1;
948 char *to = NULL;
949 size_t len = 0;
951 for (p1 = from; *p1; p1++) {
952 if ((unsigned char)*p1 >= 0x80) {
953 len += 3;
954 } else {
955 len++;
958 len++;
960 to = talloc_array(ctx, char, len);
961 if (!to) {
962 return NULL;
965 for (out = to; *from;) {
966 /* buffer husoku error */
967 if ((unsigned char)*from >= 0x80) {
968 *out++ = hex_tag;
969 *out++ = bin2hex (((*from)>>4)&0x0f);
970 *out++ = bin2hex ((*from)&0x0f);
971 from++;
972 } else {
973 *out++ = *from++;
976 *out = '\0';
977 return to;
980 /*******************************************************************
981 CAP -> original code
982 ********************************************************************/
983 /* ":xx" -> a byte */
985 static char *capdecode(TALLOC_CTX *ctx, const char *from)
987 const char *p1;
988 char *out = NULL;
989 char *to = NULL;
990 size_t len = 0;
992 for (p1 = from; *p1; len++) {
993 if (is_hex(p1)) {
994 p1 += 3;
995 } else {
996 p1++;
999 len++;
1001 to = talloc_array(ctx, char, len);
1002 if (!to) {
1003 return NULL;
1006 for (out = to; *from;) {
1007 if (is_hex(from)) {
1008 *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1009 from += 3;
1010 } else {
1011 *out++ = *from++;
1014 *out = '\0';
1015 return to;