s3: VFS: Change SMB_VFS_SETXATTR to use const struct smb_filename * instead of const...
[samba.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, const char *path,
33                               uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
34 {
35         char *cappath = capencode(talloc_tos(), path);
36
37         if (!cappath) {
38                 errno = ENOMEM;
39                 return (uint64_t)-1;
40         }
41         return SMB_VFS_NEXT_DISK_FREE(handle, cappath, bsize, dfree, dsize);
42 }
43
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)
47 {
48         char *cappath = capencode(talloc_tos(), path);
49
50         if (!cappath) {
51                 errno = ENOMEM;
52                 return -1;
53         }
54         return SMB_VFS_NEXT_GET_QUOTA(handle, cappath, qtype, id, dq);
55 }
56
57 static DIR *cap_opendir(vfs_handle_struct *handle,
58                         const struct smb_filename *smb_fname,
59                         const char *mask,
60                         uint32_t attr)
61 {
62         char *capname = capencode(talloc_tos(), smb_fname->base_name);
63         struct smb_filename *cap_smb_fname = NULL;
64
65         if (!capname) {
66                 errno = ENOMEM;
67                 return NULL;
68         }
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;
78         }
79         return SMB_VFS_NEXT_OPENDIR(handle, cap_smb_fname, mask, attr);
80 }
81
82 static struct dirent *cap_readdir(vfs_handle_struct *handle,
83                                       DIR *dirp,
84                                       SMB_STRUCT_STAT *sbuf)
85 {
86         struct dirent *result;
87         struct dirent *newdirent;
88         char *newname;
89         size_t newnamelen;
90         DEBUG(3,("cap: cap_readdir\n"));
91
92         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
93         if (!result) {
94                 return NULL;
95         }
96
97         newname = capdecode(talloc_tos(), result->d_name);
98         if (!newname) {
99                 return NULL;
100         }
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;
107         }
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;
112 }
113
114 static int cap_mkdir(vfs_handle_struct *handle,
115                 const struct smb_filename *smb_fname,
116                 mode_t mode)
117 {
118         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
119         struct smb_filename *cap_smb_fname = NULL;
120
121         if (!cappath) {
122                 errno = ENOMEM;
123                 return -1;
124         }
125
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;
135         }
136
137         return SMB_VFS_NEXT_MKDIR(handle, cap_smb_fname, mode);
138 }
139
140 static int cap_rmdir(vfs_handle_struct *handle,
141                 const struct smb_filename *smb_fname)
142 {
143         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
144         struct smb_filename *cap_smb_fname = NULL;
145
146         if (!cappath) {
147                 errno = ENOMEM;
148                 return -1;
149         }
150
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;
160         }
161
162         return SMB_VFS_NEXT_RMDIR(handle, cap_smb_fname);
163 }
164
165 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
166                     files_struct *fsp, int flags, mode_t mode)
167 {
168         char *cappath;
169         char *tmp_base_name = NULL;
170         int ret;
171
172         cappath = capencode(talloc_tos(), smb_fname->base_name);
173
174         if (!cappath) {
175                 errno = ENOMEM;
176                 return -1;
177         }
178
179         tmp_base_name = smb_fname->base_name;
180         smb_fname->base_name = cappath;
181
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);
184
185         smb_fname->base_name = tmp_base_name;
186         TALLOC_FREE(cappath);
187
188         return ret;
189 }
190
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)
194 {
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;
200
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;
206         }
207
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;
213         }
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;
218         }
219
220         smb_fname_src_tmp->base_name = capold;
221         smb_fname_dst_tmp->base_name = capnew;
222
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);
230
231         return ret;
232 }
233
234 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
235 {
236         char *cappath;
237         char *tmp_base_name = NULL;
238         int ret;
239
240         cappath = capencode(talloc_tos(), smb_fname->base_name);
241
242         if (!cappath) {
243                 errno = ENOMEM;
244                 return -1;
245         }
246
247         tmp_base_name = smb_fname->base_name;
248         smb_fname->base_name = cappath;
249
250         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
251
252         smb_fname->base_name = tmp_base_name;
253         TALLOC_FREE(cappath);
254
255         return ret;
256 }
257
258 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
259 {
260         char *cappath;
261         char *tmp_base_name = NULL;
262         int ret;
263
264         cappath = capencode(talloc_tos(), smb_fname->base_name);
265
266         if (!cappath) {
267                 errno = ENOMEM;
268                 return -1;
269         }
270
271         tmp_base_name = smb_fname->base_name;
272         smb_fname->base_name = cappath;
273
274         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
275
276         smb_fname->base_name = tmp_base_name;
277         TALLOC_FREE(cappath);
278
279         return ret;
280 }
281
282 static int cap_unlink(vfs_handle_struct *handle,
283                       const struct smb_filename *smb_fname)
284 {
285         struct smb_filename *smb_fname_tmp = NULL;
286         char *cappath = NULL;
287         int ret;
288
289         cappath = capencode(talloc_tos(), smb_fname->base_name);
290         if (!cappath) {
291                 errno = ENOMEM;
292                 return -1;
293         }
294
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;
300         }
301
302         smb_fname_tmp->base_name = cappath;
303
304         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
305
306         TALLOC_FREE(smb_fname_tmp);
307         return ret;
308 }
309
310 static int cap_chmod(vfs_handle_struct *handle,
311                         const struct smb_filename *smb_fname,
312                         mode_t mode)
313 {
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;
318
319         if (!cappath) {
320                 errno = ENOMEM;
321                 return -1;
322         }
323
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;
333         }
334
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;
341 }
342
343 static int cap_chown(vfs_handle_struct *handle,
344                         const struct smb_filename *smb_fname,
345                         uid_t uid,
346                         gid_t gid)
347 {
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;
352
353         if (!cappath) {
354                 errno = ENOMEM;
355                 return -1;
356         }
357
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;
367         }
368
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;
375 }
376
377 static int cap_lchown(vfs_handle_struct *handle,
378                         const struct smb_filename *smb_fname,
379                         uid_t uid,
380                         gid_t gid)
381 {
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;
386
387         if (!cappath) {
388                 errno = ENOMEM;
389                 return -1;
390         }
391
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;
401         }
402
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;
409 }
410
411 static int cap_chdir(vfs_handle_struct *handle, const char *path)
412 {
413         char *cappath = capencode(talloc_tos(), path);
414
415         if (!cappath) {
416                 errno = ENOMEM;
417                 return -1;
418         }
419         DEBUG(3,("cap: cap_chdir for %s\n", path));
420         return SMB_VFS_NEXT_CHDIR(handle, cappath);
421 }
422
423 static int cap_ntimes(vfs_handle_struct *handle,
424                       const struct smb_filename *smb_fname,
425                       struct smb_file_time *ft)
426 {
427         struct smb_filename *smb_fname_tmp = NULL;
428         char *cappath = NULL;
429         int ret;
430
431         cappath = capencode(talloc_tos(), smb_fname->base_name);
432
433         if (!cappath) {
434                 errno = ENOMEM;
435                 return -1;
436         }
437
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;
443         }
444
445         smb_fname_tmp->base_name = cappath;
446
447         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
448
449         TALLOC_FREE(smb_fname_tmp);
450         return ret;
451 }
452
453
454 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
455                        const char *newpath)
456 {
457         char *capold = capencode(talloc_tos(), oldpath);
458         char *capnew = capencode(talloc_tos(), newpath);
459
460         if (!capold || !capnew) {
461                 errno = ENOMEM;
462                 return -1;
463         }
464         return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
465 }
466
467 static int cap_readlink(vfs_handle_struct *handle, const char *path,
468                         char *buf, size_t bufsiz)
469 {
470         char *cappath = capencode(talloc_tos(), path);
471
472         if (!cappath) {
473                 errno = ENOMEM;
474                 return -1;
475         }
476         return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
477 }
478
479 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
480 {
481         char *capold = capencode(talloc_tos(), oldpath);
482         char *capnew = capencode(talloc_tos(), newpath);
483
484         if (!capold || !capnew) {
485                 errno = ENOMEM;
486                 return -1;
487         }
488         return SMB_VFS_NEXT_LINK(handle, capold, capnew);
489 }
490
491 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
492 {
493         char *cappath = capencode(talloc_tos(), path);
494
495         if (!cappath) {
496                 errno = ENOMEM;
497                 return -1;
498         }
499         return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
500 }
501
502 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
503 {
504         /* monyo need capencode'ed and capdecode'ed? */
505         char *cappath = capencode(talloc_tos(), path);
506
507         if (!cappath) {
508                 errno = ENOMEM;
509                 return NULL;
510         }
511         return SMB_VFS_NEXT_REALPATH(handle, cappath);
512 }
513
514 static int cap_chmod_acl(vfs_handle_struct *handle,
515                         const struct smb_filename *smb_fname,
516                         mode_t mode)
517 {
518         struct smb_filename *cap_smb_fname = NULL;
519         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
520         int ret;
521         int saved_errno;
522
523         /* If the underlying VFS doesn't have ACL support... */
524         if (!cappath) {
525                 errno = ENOMEM;
526                 return -1;
527         }
528         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
529                                         cappath,
530                                         NULL,
531                                         NULL,
532                                         smb_fname->flags);
533         if (cap_smb_fname == NULL) {
534                 TALLOC_FREE(cappath);
535                 errno = ENOMEM;
536                 return -1;
537         }
538
539         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
540         saved_errno = errno;
541         TALLOC_FREE(cappath);
542         TALLOC_FREE(cap_smb_fname);
543         errno = saved_errno;
544         return ret;
545 }
546
547 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
548                                 const struct smb_filename *smb_fname,
549                                 SMB_ACL_TYPE_T type,
550                                 TALLOC_CTX *mem_ctx)
551 {
552         struct smb_filename *cap_smb_fname = NULL;
553         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
554         SMB_ACL_T ret;
555         int saved_errno = 0;
556
557         if (!cappath) {
558                 errno = ENOMEM;
559                 return (SMB_ACL_T)NULL;
560         }
561         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
562                                         cappath,
563                                         NULL,
564                                         NULL,
565                                         smb_fname->flags);
566         if (cap_smb_fname == NULL) {
567                 TALLOC_FREE(cappath);
568                 errno = ENOMEM;
569                 return (SMB_ACL_T)NULL;
570         }
571         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
572                                 type, mem_ctx);
573         if (ret == NULL) {
574                 saved_errno = errno;
575         }
576         TALLOC_FREE(cappath);
577         TALLOC_FREE(cap_smb_fname);
578         if (saved_errno != 0) {
579                 errno = saved_errno;
580         }
581         return ret;
582 }
583
584 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
585                         const struct smb_filename *smb_fname,
586                         SMB_ACL_TYPE_T acltype,
587                         SMB_ACL_T theacl)
588 {
589         struct smb_filename *cap_smb_fname = NULL;
590         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
591         int ret;
592         int saved_errno = 0;
593
594         if (!cappath) {
595                 errno = ENOMEM;
596                 return -1;
597         }
598         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
599                                         cappath,
600                                         NULL,
601                                         NULL,
602                                         smb_fname->flags);
603         if (cap_smb_fname == NULL) {
604                 TALLOC_FREE(cappath);
605                 errno = ENOMEM;
606                 return -1;
607         }
608         ret =  SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
609                                 acltype, theacl);
610         if (ret == -1) {
611                 saved_errno = errno;
612         }
613         TALLOC_FREE(cappath);
614         TALLOC_FREE(cap_smb_fname);
615         if (saved_errno != 0) {
616                 errno = saved_errno;
617         }
618         return ret;
619 }
620
621 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
622                         const struct smb_filename *smb_fname)
623 {
624         struct smb_filename *cap_smb_fname = NULL;
625         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
626         int ret;
627         int saved_errno = 0;
628
629         if (!cappath) {
630                 errno = ENOMEM;
631                 return -1;
632         }
633         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
634                                         cappath,
635                                         NULL,
636                                         NULL,
637                                         smb_fname->flags);
638         if (cap_smb_fname == NULL) {
639                 TALLOC_FREE(cappath);
640                 errno = ENOMEM;
641                 return -1;
642         }
643         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
644         if (ret == -1) {
645                 saved_errno = errno;
646         }
647         TALLOC_FREE(cappath);
648         TALLOC_FREE(cap_smb_fname);
649         if (saved_errno) {
650                 errno = saved_errno;
651         }
652         return ret;
653 }
654
655 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
656 {
657         char *cappath = capencode(talloc_tos(), path);
658         char *capname = capencode(talloc_tos(), name);
659
660         if (!cappath || !capname) {
661                 errno = ENOMEM;
662                 return -1;
663         }
664         return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
665 }
666
667 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
668 {
669         char *cappath = capencode(talloc_tos(), path);
670
671         if (!cappath) {
672                 errno = ENOMEM;
673                 return -1;
674         }
675         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
676 }
677
678 static ssize_t cap_listxattr(vfs_handle_struct *handle,
679                                 const struct smb_filename *smb_fname,
680                                 char *list,
681                                 size_t size)
682 {
683         struct smb_filename *cap_smb_fname = NULL;
684         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
685         ssize_t ret;
686         int saved_errno = 0;
687
688         if (!cappath) {
689                 errno = ENOMEM;
690                 return -1;
691         }
692         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
693                                         cappath,
694                                         NULL,
695                                         NULL,
696                                         smb_fname->flags);
697         if (cap_smb_fname == NULL) {
698                 TALLOC_FREE(cappath);
699                 errno = ENOMEM;
700                 return -1;
701         }
702         ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
703         if (ret == -1) {
704                 saved_errno = errno;
705         }
706         TALLOC_FREE(cappath);
707         TALLOC_FREE(cap_smb_fname);
708         if (saved_errno) {
709                 errno = saved_errno;
710         }
711         return ret;
712 }
713
714 static int cap_removexattr(vfs_handle_struct *handle,
715                                 const struct smb_filename *smb_fname,
716                                 const char *name)
717 {
718         struct smb_filename *cap_smb_fname = NULL;
719         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
720         char *capname = capencode(talloc_tos(), name);
721         int ret;
722         int saved_errno = 0;
723
724         if (!cappath || !capname) {
725                 errno = ENOMEM;
726                 return -1;
727         }
728         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
729                                         cappath,
730                                         NULL,
731                                         NULL,
732                                         smb_fname->flags);
733         if (cap_smb_fname == NULL) {
734                 TALLOC_FREE(cappath);
735                 TALLOC_FREE(capname);
736                 errno = ENOMEM;
737                 return -1;
738         }
739         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
740         if (ret == -1) {
741                 saved_errno = errno;
742         }
743         TALLOC_FREE(cappath);
744         TALLOC_FREE(capname);
745         TALLOC_FREE(cap_smb_fname);
746         if (saved_errno) {
747                 errno = saved_errno;
748         }
749         return ret;
750 }
751
752 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
753 {
754         char *cappath = capencode(talloc_tos(), path);
755
756         if (!cappath) {
757                 errno = ENOMEM;
758                 return -1;
759         }
760         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
761 }
762
763 static int cap_setxattr(vfs_handle_struct *handle,
764                         const struct smb_filename *smb_fname,
765                         const char *name,
766                         const void *value,
767                         size_t size,
768                         int flags)
769 {
770         struct smb_filename *cap_smb_fname = NULL;
771         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
772         char *capname = capencode(talloc_tos(), name);
773         int ret;
774         int saved_errno = 0;
775
776         if (!cappath || !capname) {
777                 errno = ENOMEM;
778                 return -1;
779         }
780         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
781                                         cappath,
782                                         NULL,
783                                         NULL,
784                                         smb_fname->flags);
785         if (cap_smb_fname == NULL) {
786                 TALLOC_FREE(cappath);
787                 TALLOC_FREE(capname);
788                 errno = ENOMEM;
789                 return -1;
790         }
791         ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
792                                 capname, value, size, flags);
793         if (ret == -1) {
794                 saved_errno = errno;
795         }
796         TALLOC_FREE(cappath);
797         TALLOC_FREE(capname);
798         TALLOC_FREE(cap_smb_fname);
799         if (saved_errno) {
800                 errno = saved_errno;
801         }
802         return ret;
803 }
804
805 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
806 {
807         char *cappath = capencode(talloc_tos(), path);
808
809         if (!cappath) {
810                 errno = ENOMEM;
811                 return -1;
812         }
813         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
814 }
815
816 static struct vfs_fn_pointers vfs_cap_fns = {
817         .disk_free_fn = cap_disk_free,
818         .get_quota_fn = cap_get_quota,
819         .opendir_fn = cap_opendir,
820         .readdir_fn = cap_readdir,
821         .mkdir_fn = cap_mkdir,
822         .rmdir_fn = cap_rmdir,
823         .open_fn = cap_open,
824         .rename_fn = cap_rename,
825         .stat_fn = cap_stat,
826         .lstat_fn = cap_lstat,
827         .unlink_fn = cap_unlink,
828         .chmod_fn = cap_chmod,
829         .chown_fn = cap_chown,
830         .lchown_fn = cap_lchown,
831         .chdir_fn = cap_chdir,
832         .ntimes_fn = cap_ntimes,
833         .symlink_fn = cap_symlink,
834         .readlink_fn = cap_readlink,
835         .link_fn = cap_link,
836         .mknod_fn = cap_mknod,
837         .realpath_fn = cap_realpath,
838         .chmod_acl_fn = cap_chmod_acl,
839         .sys_acl_get_file_fn = cap_sys_acl_get_file,
840         .sys_acl_set_file_fn = cap_sys_acl_set_file,
841         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
842         .getxattr_fn = cap_getxattr,
843         .fgetxattr_fn = cap_fgetxattr,
844         .listxattr_fn = cap_listxattr,
845         .removexattr_fn = cap_removexattr,
846         .fremovexattr_fn = cap_fremovexattr,
847         .setxattr_fn = cap_setxattr,
848         .fsetxattr_fn = cap_fsetxattr
849 };
850
851 NTSTATUS vfs_cap_init(TALLOC_CTX *);
852 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
853 {
854         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
855                                 &vfs_cap_fns);
856 }
857
858 /* For CAP functions */
859 #define hex_tag ':'
860 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
861 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
862 #define is_hex(s)               ((s)[0] == hex_tag)
863
864 static unsigned char hex2bin_table[256] = {
865 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
866 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
867 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
868 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
869 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
870 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
871 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
872 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
873 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
874 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
875 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
876 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
877 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
878 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
879 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
880 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
881 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
882 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
883 };
884 static unsigned char bin2hex_table[256] = "0123456789abcdef";
885
886 /*******************************************************************
887   original code -> ":xx"  - CAP format
888 ********************************************************************/
889
890 static char *capencode(TALLOC_CTX *ctx, const char *from)
891 {
892         char *out = NULL;
893         const char *p1;
894         char *to = NULL;
895         size_t len = 0;
896
897         for (p1 = from; *p1; p1++) {
898                 if ((unsigned char)*p1 >= 0x80) {
899                         len += 3;
900                 } else {
901                         len++;
902                 }
903         }
904         len++;
905
906         to = talloc_array(ctx, char, len);
907         if (!to) {
908                 return NULL;
909         }
910
911         for (out = to; *from;) {
912                 /* buffer husoku error */
913                 if ((unsigned char)*from >= 0x80) {
914                         *out++ = hex_tag;
915                         *out++ = bin2hex (((*from)>>4)&0x0f);
916                         *out++ = bin2hex ((*from)&0x0f);
917                         from++;
918                 } else {
919                         *out++ = *from++;
920                 }
921         }
922         *out = '\0';
923         return to;
924 }
925
926 /*******************************************************************
927   CAP -> original code
928 ********************************************************************/
929 /* ":xx" -> a byte */
930
931 static char *capdecode(TALLOC_CTX *ctx, const char *from)
932 {
933         const char *p1;
934         char *out = NULL;
935         char *to = NULL;
936         size_t len = 0;
937
938         for (p1 = from; *p1; len++) {
939                 if (is_hex(p1)) {
940                         p1 += 3;
941                 } else {
942                         p1++;
943                 }
944         }
945         len++;
946
947         to = talloc_array(ctx, char, len);
948         if (!to) {
949                 return NULL;
950         }
951
952         for (out = to; *from;) {
953                 if (is_hex(from)) {
954                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
955                         from += 3;
956                 } else {
957                         *out++ = *from++;
958                 }
959         }
960         *out = '\0';
961         return to;
962 }