s3: VFS: Change SMB_VFS_SYMLINK to use const struct smb_filename * instead of const...
[kai/samba-autobuild/.git] / source3 / modules / vfs_cap.c
1 /*
2  * CAP VFS module for Samba 3.x Version 0.3
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
9  *
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.
14  *
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.
19  *
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/>.
22  */
23
24
25 #include "includes.h"
26 #include "smbd/smbd.h"
27
28 /* cap functions */
29 static char *capencode(TALLOC_CTX *ctx, const char *from);
30 static char *capdecode(TALLOC_CTX *ctx, const char *from);
31
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)
37 {
38         char *capname = capencode(talloc_tos(), smb_fname->base_name);
39         struct smb_filename *cap_smb_fname = NULL;
40
41         if (!capname) {
42                 errno = ENOMEM;
43                 return (uint64_t)-1;
44         }
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;
54         }
55         return SMB_VFS_NEXT_DISK_FREE(handle, cap_smb_fname,
56                         bsize, dfree, dsize);
57 }
58
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)
64 {
65         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
66         struct smb_filename *cap_smb_fname = NULL;
67
68         if (!cappath) {
69                 errno = ENOMEM;
70                 return -1;
71         }
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;
81         }
82         return SMB_VFS_NEXT_GET_QUOTA(handle, cap_smb_fname, qtype, id, dq);
83 }
84
85 static DIR *cap_opendir(vfs_handle_struct *handle,
86                         const struct smb_filename *smb_fname,
87                         const char *mask,
88                         uint32_t attr)
89 {
90         char *capname = capencode(talloc_tos(), smb_fname->base_name);
91         struct smb_filename *cap_smb_fname = NULL;
92
93         if (!capname) {
94                 errno = ENOMEM;
95                 return NULL;
96         }
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;
106         }
107         return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
108 }
109
110 static struct dirent *cap_readdir(vfs_handle_struct *handle,
111                                       DIR *dirp,
112                                       SMB_STRUCT_STAT *sbuf)
113 {
114         struct dirent *result;
115         struct dirent *newdirent;
116         char *newname;
117         size_t newnamelen;
118         DEBUG(3,("cap: cap_readdir\n"));
119
120         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
121         if (!result) {
122                 return NULL;
123         }
124
125         newname = capdecode(talloc_tos(), result->d_name);
126         if (!newname) {
127                 return NULL;
128         }
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;
135         }
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;
140 }
141
142 static int cap_mkdir(vfs_handle_struct *handle,
143                 const struct smb_filename *smb_fname,
144                 mode_t mode)
145 {
146         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
147         struct smb_filename *cap_smb_fname = NULL;
148
149         if (!cappath) {
150                 errno = ENOMEM;
151                 return -1;
152         }
153
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;
163         }
164
165         return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
166 }
167
168 static int cap_rmdir(vfs_handle_struct *handle,
169                 const struct smb_filename *smb_fname)
170 {
171         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
172         struct smb_filename *cap_smb_fname = NULL;
173
174         if (!cappath) {
175                 errno = ENOMEM;
176                 return -1;
177         }
178
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;
188         }
189
190         return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
191 }
192
193 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
194                     files_struct *fsp, int flags, mode_t mode)
195 {
196         char *cappath;
197         char *tmp_base_name = NULL;
198         int ret;
199
200         cappath = capencode(talloc_tos(), smb_fname->base_name);
201
202         if (!cappath) {
203                 errno = ENOMEM;
204                 return -1;
205         }
206
207         tmp_base_name = smb_fname->base_name;
208         smb_fname->base_name = cappath;
209
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);
212
213         smb_fname->base_name = tmp_base_name;
214         TALLOC_FREE(cappath);
215
216         return ret;
217 }
218
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)
222 {
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;
228
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;
234         }
235
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;
241         }
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;
246         }
247
248         smb_fname_src_tmp->base_name = capold;
249         smb_fname_dst_tmp->base_name = capnew;
250
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);
258
259         return ret;
260 }
261
262 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
263 {
264         char *cappath;
265         char *tmp_base_name = NULL;
266         int ret;
267
268         cappath = capencode(talloc_tos(), smb_fname->base_name);
269
270         if (!cappath) {
271                 errno = ENOMEM;
272                 return -1;
273         }
274
275         tmp_base_name = smb_fname->base_name;
276         smb_fname->base_name = cappath;
277
278         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
279
280         smb_fname->base_name = tmp_base_name;
281         TALLOC_FREE(cappath);
282
283         return ret;
284 }
285
286 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
287 {
288         char *cappath;
289         char *tmp_base_name = NULL;
290         int ret;
291
292         cappath = capencode(talloc_tos(), smb_fname->base_name);
293
294         if (!cappath) {
295                 errno = ENOMEM;
296                 return -1;
297         }
298
299         tmp_base_name = smb_fname->base_name;
300         smb_fname->base_name = cappath;
301
302         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
303
304         smb_fname->base_name = tmp_base_name;
305         TALLOC_FREE(cappath);
306
307         return ret;
308 }
309
310 static int cap_unlink(vfs_handle_struct *handle,
311                       const struct smb_filename *smb_fname)
312 {
313         struct smb_filename *smb_fname_tmp = NULL;
314         char *cappath = NULL;
315         int ret;
316
317         cappath = capencode(talloc_tos(), smb_fname->base_name);
318         if (!cappath) {
319                 errno = ENOMEM;
320                 return -1;
321         }
322
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;
328         }
329
330         smb_fname_tmp->base_name = cappath;
331
332         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
333
334         TALLOC_FREE(smb_fname_tmp);
335         return ret;
336 }
337
338 static int cap_chmod(vfs_handle_struct *handle,
339                         const struct smb_filename *smb_fname,
340                         mode_t mode)
341 {
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;
346
347         if (!cappath) {
348                 errno = ENOMEM;
349                 return -1;
350         }
351
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;
361         }
362
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;
369 }
370
371 static int cap_chown(vfs_handle_struct *handle,
372                         const struct smb_filename *smb_fname,
373                         uid_t uid,
374                         gid_t gid)
375 {
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;
380
381         if (!cappath) {
382                 errno = ENOMEM;
383                 return -1;
384         }
385
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;
395         }
396
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;
403 }
404
405 static int cap_lchown(vfs_handle_struct *handle,
406                         const struct smb_filename *smb_fname,
407                         uid_t uid,
408                         gid_t gid)
409 {
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;
414
415         if (!cappath) {
416                 errno = ENOMEM;
417                 return -1;
418         }
419
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;
429         }
430
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;
437 }
438
439 static int cap_chdir(vfs_handle_struct *handle, const char *path)
440 {
441         char *cappath = capencode(talloc_tos(), path);
442
443         if (!cappath) {
444                 errno = ENOMEM;
445                 return -1;
446         }
447         DEBUG(3,("cap: cap_chdir for %s\n", path));
448         return SMB_VFS_NEXT_CHDIR(handle, cappath);
449 }
450
451 static int cap_ntimes(vfs_handle_struct *handle,
452                       const struct smb_filename *smb_fname,
453                       struct smb_file_time *ft)
454 {
455         struct smb_filename *smb_fname_tmp = NULL;
456         char *cappath = NULL;
457         int ret;
458
459         cappath = capencode(talloc_tos(), smb_fname->base_name);
460
461         if (!cappath) {
462                 errno = ENOMEM;
463                 return -1;
464         }
465
466         /* Setup temporary smb_filename structs. */
467         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
468         if (smb_fname_tmp == NULL) {
469                 errno = ENOMEM;
470                 return -1;
471         }
472
473         smb_fname_tmp->base_name = cappath;
474
475         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
476
477         TALLOC_FREE(smb_fname_tmp);
478         return ret;
479 }
480
481
482 static int cap_symlink(vfs_handle_struct *handle,
483                         const char *link_contents,
484                         const struct smb_filename *new_smb_fname)
485 {
486         char *capold = capencode(talloc_tos(), link_contents);
487         char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
488         struct smb_filename *new_cap_smb_fname = NULL;
489         int saved_errno = 0;
490         int ret;
491
492         if (!capold || !capnew) {
493                 errno = ENOMEM;
494                 return -1;
495         }
496         new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
497                                         capnew,
498                                         NULL,
499                                         NULL,
500                                         new_smb_fname->flags);
501         if (new_cap_smb_fname == NULL) {
502                 TALLOC_FREE(capold);
503                 TALLOC_FREE(capnew);
504                 errno = ENOMEM;
505                 return -1;
506         }
507         ret = SMB_VFS_NEXT_SYMLINK(handle,
508                         capold,
509                         new_cap_smb_fname);
510         if (ret == -1) {
511                 saved_errno = errno;
512         }
513         TALLOC_FREE(capold);
514         TALLOC_FREE(capnew);
515         TALLOC_FREE(new_cap_smb_fname);
516         if (saved_errno != 0) {
517                 errno = saved_errno;
518         }
519         return ret;
520 }
521
522 static int cap_readlink(vfs_handle_struct *handle,
523                         const struct smb_filename *smb_fname,
524                         char *buf,
525                         size_t bufsiz)
526 {
527         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
528         struct smb_filename *cap_smb_fname = NULL;
529         int saved_errno = 0;
530         int ret;
531
532         if (!cappath) {
533                 errno = ENOMEM;
534                 return -1;
535         }
536         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
537                                         cappath,
538                                         NULL,
539                                         NULL,
540                                         smb_fname->flags);
541         if (cap_smb_fname == NULL) {
542                 TALLOC_FREE(cappath);
543                 errno = ENOMEM;
544                 return -1;
545         }
546         ret = SMB_VFS_NEXT_READLINK(handle, cap_smb_fname, buf, bufsiz);
547         if (ret == -1) {
548                 saved_errno = errno;
549         }
550         TALLOC_FREE(cappath);
551         TALLOC_FREE(cap_smb_fname);
552         if (saved_errno != 0) {
553                 errno = saved_errno;
554         }
555         return ret;
556 }
557
558 static int cap_link(vfs_handle_struct *handle,
559                         const struct smb_filename *old_smb_fname,
560                         const struct smb_filename *new_smb_fname)
561 {
562         char *capold = capencode(talloc_tos(), old_smb_fname->base_name);
563         char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
564         struct smb_filename *old_cap_smb_fname = NULL;
565         struct smb_filename *new_cap_smb_fname = NULL;
566         int saved_errno = 0;
567         int ret;
568
569         if (!capold || !capnew) {
570                 errno = ENOMEM;
571                 return -1;
572         }
573         old_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
574                                         capold,
575                                         NULL,
576                                         NULL,
577                                         old_smb_fname->flags);
578         if (old_cap_smb_fname == NULL) {
579                 TALLOC_FREE(capold);
580                 TALLOC_FREE(capnew);
581                 errno = ENOMEM;
582                 return -1;
583         }
584         new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
585                                         capnew,
586                                         NULL,
587                                         NULL,
588                                         new_smb_fname->flags);
589         if (new_cap_smb_fname == NULL) {
590                 TALLOC_FREE(capold);
591                 TALLOC_FREE(capnew);
592                 TALLOC_FREE(old_cap_smb_fname);
593                 errno = ENOMEM;
594                 return -1;
595         }
596         ret = SMB_VFS_NEXT_LINK(handle, old_cap_smb_fname, new_cap_smb_fname);
597         if (ret == -1) {
598                 saved_errno = errno;
599         }
600         TALLOC_FREE(capold);
601         TALLOC_FREE(capnew);
602         TALLOC_FREE(old_cap_smb_fname);
603         TALLOC_FREE(new_cap_smb_fname);
604         if (saved_errno != 0) {
605                 errno = saved_errno;
606         }
607         return ret;
608 }
609
610 static int cap_mknod(vfs_handle_struct *handle,
611                 const struct smb_filename *smb_fname,
612                 mode_t mode,
613                 SMB_DEV_T dev)
614 {
615         struct smb_filename *cap_smb_fname = NULL;
616         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
617         int ret;
618         int saved_errno = 0;
619
620         if (!cappath) {
621                 errno = ENOMEM;
622                 return -1;
623         }
624         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
625                                         cappath,
626                                         NULL,
627                                         NULL,
628                                         smb_fname->flags);
629         if (cap_smb_fname == NULL) {
630                 TALLOC_FREE(cappath);
631                 errno = ENOMEM;
632                 return -1;
633         }
634         ret = SMB_VFS_NEXT_MKNOD(handle, cap_smb_fname, mode, dev);
635         if (ret == -1) {
636                 saved_errno = errno;
637         }
638         TALLOC_FREE(cappath);
639         TALLOC_FREE(cap_smb_fname);
640         if (saved_errno != 0) {
641                 errno = saved_errno;
642         }
643         return ret;
644 }
645
646 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
647 {
648         /* monyo need capencode'ed and capdecode'ed? */
649         char *cappath = capencode(talloc_tos(), path);
650
651         if (!cappath) {
652                 errno = ENOMEM;
653                 return NULL;
654         }
655         return SMB_VFS_NEXT_REALPATH(handle, cappath);
656 }
657
658 static int cap_chmod_acl(vfs_handle_struct *handle,
659                         const struct smb_filename *smb_fname,
660                         mode_t mode)
661 {
662         struct smb_filename *cap_smb_fname = NULL;
663         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
664         int ret;
665         int saved_errno;
666
667         /* If the underlying VFS doesn't have ACL support... */
668         if (!cappath) {
669                 errno = ENOMEM;
670                 return -1;
671         }
672         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
673                                         cappath,
674                                         NULL,
675                                         NULL,
676                                         smb_fname->flags);
677         if (cap_smb_fname == NULL) {
678                 TALLOC_FREE(cappath);
679                 errno = ENOMEM;
680                 return -1;
681         }
682
683         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
684         saved_errno = errno;
685         TALLOC_FREE(cappath);
686         TALLOC_FREE(cap_smb_fname);
687         errno = saved_errno;
688         return ret;
689 }
690
691 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
692                                 const struct smb_filename *smb_fname,
693                                 SMB_ACL_TYPE_T type,
694                                 TALLOC_CTX *mem_ctx)
695 {
696         struct smb_filename *cap_smb_fname = NULL;
697         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
698         SMB_ACL_T ret;
699         int saved_errno = 0;
700
701         if (!cappath) {
702                 errno = ENOMEM;
703                 return (SMB_ACL_T)NULL;
704         }
705         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
706                                         cappath,
707                                         NULL,
708                                         NULL,
709                                         smb_fname->flags);
710         if (cap_smb_fname == NULL) {
711                 TALLOC_FREE(cappath);
712                 errno = ENOMEM;
713                 return (SMB_ACL_T)NULL;
714         }
715         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
716                                 type, mem_ctx);
717         if (ret == NULL) {
718                 saved_errno = errno;
719         }
720         TALLOC_FREE(cappath);
721         TALLOC_FREE(cap_smb_fname);
722         if (saved_errno != 0) {
723                 errno = saved_errno;
724         }
725         return ret;
726 }
727
728 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
729                         const struct smb_filename *smb_fname,
730                         SMB_ACL_TYPE_T acltype,
731                         SMB_ACL_T theacl)
732 {
733         struct smb_filename *cap_smb_fname = NULL;
734         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
735         int ret;
736         int saved_errno = 0;
737
738         if (!cappath) {
739                 errno = ENOMEM;
740                 return -1;
741         }
742         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
743                                         cappath,
744                                         NULL,
745                                         NULL,
746                                         smb_fname->flags);
747         if (cap_smb_fname == NULL) {
748                 TALLOC_FREE(cappath);
749                 errno = ENOMEM;
750                 return -1;
751         }
752         ret =  SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
753                                 acltype, theacl);
754         if (ret == -1) {
755                 saved_errno = errno;
756         }
757         TALLOC_FREE(cappath);
758         TALLOC_FREE(cap_smb_fname);
759         if (saved_errno != 0) {
760                 errno = saved_errno;
761         }
762         return ret;
763 }
764
765 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
766                         const struct smb_filename *smb_fname)
767 {
768         struct smb_filename *cap_smb_fname = NULL;
769         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
770         int ret;
771         int saved_errno = 0;
772
773         if (!cappath) {
774                 errno = ENOMEM;
775                 return -1;
776         }
777         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
778                                         cappath,
779                                         NULL,
780                                         NULL,
781                                         smb_fname->flags);
782         if (cap_smb_fname == NULL) {
783                 TALLOC_FREE(cappath);
784                 errno = ENOMEM;
785                 return -1;
786         }
787         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
788         if (ret == -1) {
789                 saved_errno = errno;
790         }
791         TALLOC_FREE(cappath);
792         TALLOC_FREE(cap_smb_fname);
793         if (saved_errno) {
794                 errno = saved_errno;
795         }
796         return ret;
797 }
798
799 static ssize_t cap_getxattr(vfs_handle_struct *handle,
800                         const struct smb_filename *smb_fname,
801                         const char *name,
802                         void *value,
803                         size_t size)
804 {
805         struct smb_filename *cap_smb_fname = NULL;
806         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
807         char *capname = capencode(talloc_tos(), name);
808         ssize_t ret;
809         int saved_errno = 0;
810
811         if (!cappath || !capname) {
812                 errno = ENOMEM;
813                 return -1;
814         }
815         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
816                                         cappath,
817                                         NULL,
818                                         NULL,
819                                         smb_fname->flags);
820         if (cap_smb_fname == NULL) {
821                 TALLOC_FREE(cappath);
822                 TALLOC_FREE(capname);
823                 errno = ENOMEM;
824                 return -1;
825         }
826         ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
827                         capname, value, size);
828         if (ret == -1) {
829                 saved_errno = errno;
830         }
831         TALLOC_FREE(cappath);
832         TALLOC_FREE(capname);
833         TALLOC_FREE(cap_smb_fname);
834         if (saved_errno) {
835                 errno = saved_errno;
836         }
837         return ret;
838 }
839
840 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
841 {
842         char *cappath = capencode(talloc_tos(), path);
843
844         if (!cappath) {
845                 errno = ENOMEM;
846                 return -1;
847         }
848         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
849 }
850
851 static ssize_t cap_listxattr(vfs_handle_struct *handle,
852                                 const struct smb_filename *smb_fname,
853                                 char *list,
854                                 size_t size)
855 {
856         struct smb_filename *cap_smb_fname = NULL;
857         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
858         ssize_t ret;
859         int saved_errno = 0;
860
861         if (!cappath) {
862                 errno = ENOMEM;
863                 return -1;
864         }
865         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
866                                         cappath,
867                                         NULL,
868                                         NULL,
869                                         smb_fname->flags);
870         if (cap_smb_fname == NULL) {
871                 TALLOC_FREE(cappath);
872                 errno = ENOMEM;
873                 return -1;
874         }
875         ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
876         if (ret == -1) {
877                 saved_errno = errno;
878         }
879         TALLOC_FREE(cappath);
880         TALLOC_FREE(cap_smb_fname);
881         if (saved_errno) {
882                 errno = saved_errno;
883         }
884         return ret;
885 }
886
887 static int cap_removexattr(vfs_handle_struct *handle,
888                                 const struct smb_filename *smb_fname,
889                                 const char *name)
890 {
891         struct smb_filename *cap_smb_fname = NULL;
892         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
893         char *capname = capencode(talloc_tos(), name);
894         int ret;
895         int saved_errno = 0;
896
897         if (!cappath || !capname) {
898                 errno = ENOMEM;
899                 return -1;
900         }
901         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
902                                         cappath,
903                                         NULL,
904                                         NULL,
905                                         smb_fname->flags);
906         if (cap_smb_fname == NULL) {
907                 TALLOC_FREE(cappath);
908                 TALLOC_FREE(capname);
909                 errno = ENOMEM;
910                 return -1;
911         }
912         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
913         if (ret == -1) {
914                 saved_errno = errno;
915         }
916         TALLOC_FREE(cappath);
917         TALLOC_FREE(capname);
918         TALLOC_FREE(cap_smb_fname);
919         if (saved_errno) {
920                 errno = saved_errno;
921         }
922         return ret;
923 }
924
925 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
926 {
927         char *cappath = capencode(talloc_tos(), path);
928
929         if (!cappath) {
930                 errno = ENOMEM;
931                 return -1;
932         }
933         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
934 }
935
936 static int cap_setxattr(vfs_handle_struct *handle,
937                         const struct smb_filename *smb_fname,
938                         const char *name,
939                         const void *value,
940                         size_t size,
941                         int flags)
942 {
943         struct smb_filename *cap_smb_fname = NULL;
944         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
945         char *capname = capencode(talloc_tos(), name);
946         int ret;
947         int saved_errno = 0;
948
949         if (!cappath || !capname) {
950                 errno = ENOMEM;
951                 return -1;
952         }
953         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
954                                         cappath,
955                                         NULL,
956                                         NULL,
957                                         smb_fname->flags);
958         if (cap_smb_fname == NULL) {
959                 TALLOC_FREE(cappath);
960                 TALLOC_FREE(capname);
961                 errno = ENOMEM;
962                 return -1;
963         }
964         ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
965                                 capname, value, size, flags);
966         if (ret == -1) {
967                 saved_errno = errno;
968         }
969         TALLOC_FREE(cappath);
970         TALLOC_FREE(capname);
971         TALLOC_FREE(cap_smb_fname);
972         if (saved_errno) {
973                 errno = saved_errno;
974         }
975         return ret;
976 }
977
978 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
979 {
980         char *cappath = capencode(talloc_tos(), path);
981
982         if (!cappath) {
983                 errno = ENOMEM;
984                 return -1;
985         }
986         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
987 }
988
989 static struct vfs_fn_pointers vfs_cap_fns = {
990         .disk_free_fn = cap_disk_free,
991         .get_quota_fn = cap_get_quota,
992         .opendir_fn = cap_opendir,
993         .readdir_fn = cap_readdir,
994         .mkdir_fn = cap_mkdir,
995         .rmdir_fn = cap_rmdir,
996         .open_fn = cap_open,
997         .rename_fn = cap_rename,
998         .stat_fn = cap_stat,
999         .lstat_fn = cap_lstat,
1000         .unlink_fn = cap_unlink,
1001         .chmod_fn = cap_chmod,
1002         .chown_fn = cap_chown,
1003         .lchown_fn = cap_lchown,
1004         .chdir_fn = cap_chdir,
1005         .ntimes_fn = cap_ntimes,
1006         .symlink_fn = cap_symlink,
1007         .readlink_fn = cap_readlink,
1008         .link_fn = cap_link,
1009         .mknod_fn = cap_mknod,
1010         .realpath_fn = cap_realpath,
1011         .chmod_acl_fn = cap_chmod_acl,
1012         .sys_acl_get_file_fn = cap_sys_acl_get_file,
1013         .sys_acl_set_file_fn = cap_sys_acl_set_file,
1014         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1015         .getxattr_fn = cap_getxattr,
1016         .fgetxattr_fn = cap_fgetxattr,
1017         .listxattr_fn = cap_listxattr,
1018         .removexattr_fn = cap_removexattr,
1019         .fremovexattr_fn = cap_fremovexattr,
1020         .setxattr_fn = cap_setxattr,
1021         .fsetxattr_fn = cap_fsetxattr
1022 };
1023
1024 NTSTATUS vfs_cap_init(TALLOC_CTX *);
1025 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1026 {
1027         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1028                                 &vfs_cap_fns);
1029 }
1030
1031 /* For CAP functions */
1032 #define hex_tag ':'
1033 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
1034 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
1035 #define is_hex(s)               ((s)[0] == hex_tag)
1036
1037 static unsigned char hex2bin_table[256] = {
1038 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1039 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1040 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1041 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1042 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1043 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1044 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1045 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1046 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1047 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1048 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1049 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1050 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1051 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1052 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1053 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1054 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1055 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
1056 };
1057 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1058
1059 /*******************************************************************
1060   original code -> ":xx"  - CAP format
1061 ********************************************************************/
1062
1063 static char *capencode(TALLOC_CTX *ctx, const char *from)
1064 {
1065         char *out = NULL;
1066         const char *p1;
1067         char *to = NULL;
1068         size_t len = 0;
1069
1070         for (p1 = from; *p1; p1++) {
1071                 if ((unsigned char)*p1 >= 0x80) {
1072                         len += 3;
1073                 } else {
1074                         len++;
1075                 }
1076         }
1077         len++;
1078
1079         to = talloc_array(ctx, char, len);
1080         if (!to) {
1081                 return NULL;
1082         }
1083
1084         for (out = to; *from;) {
1085                 /* buffer husoku error */
1086                 if ((unsigned char)*from >= 0x80) {
1087                         *out++ = hex_tag;
1088                         *out++ = bin2hex (((*from)>>4)&0x0f);
1089                         *out++ = bin2hex ((*from)&0x0f);
1090                         from++;
1091                 } else {
1092                         *out++ = *from++;
1093                 }
1094         }
1095         *out = '\0';
1096         return to;
1097 }
1098
1099 /*******************************************************************
1100   CAP -> original code
1101 ********************************************************************/
1102 /* ":xx" -> a byte */
1103
1104 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1105 {
1106         const char *p1;
1107         char *out = NULL;
1108         char *to = NULL;
1109         size_t len = 0;
1110
1111         for (p1 = from; *p1; len++) {
1112                 if (is_hex(p1)) {
1113                         p1 += 3;
1114                 } else {
1115                         p1++;
1116                 }
1117         }
1118         len++;
1119
1120         to = talloc_array(ctx, char, len);
1121         if (!to) {
1122                 return NULL;
1123         }
1124
1125         for (out = to; *from;) {
1126                 if (is_hex(from)) {
1127                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1128                         from += 3;
1129                 } else {
1130                         *out++ = *from++;
1131                 }
1132         }
1133         *out = '\0';
1134         return to;
1135 }