s3: VFS: Change SMB_VFS_READLINK to use const struct smb_filename * instead of const...
[sfrench/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, const char *oldpath,
483                        const char *newpath)
484 {
485         char *capold = capencode(talloc_tos(), oldpath);
486         char *capnew = capencode(talloc_tos(), newpath);
487
488         if (!capold || !capnew) {
489                 errno = ENOMEM;
490                 return -1;
491         }
492         return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
493 }
494
495 static int cap_readlink(vfs_handle_struct *handle,
496                         const struct smb_filename *smb_fname,
497                         char *buf,
498                         size_t bufsiz)
499 {
500         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
501         struct smb_filename *cap_smb_fname = NULL;
502         int saved_errno = 0;
503         int ret;
504
505         if (!cappath) {
506                 errno = ENOMEM;
507                 return -1;
508         }
509         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
510                                         cappath,
511                                         NULL,
512                                         NULL,
513                                         smb_fname->flags);
514         if (cap_smb_fname == NULL) {
515                 TALLOC_FREE(cappath);
516                 errno = ENOMEM;
517                 return -1;
518         }
519         ret = SMB_VFS_NEXT_READLINK(handle, cap_smb_fname, buf, bufsiz);
520         if (ret == -1) {
521                 saved_errno = errno;
522         }
523         TALLOC_FREE(cappath);
524         TALLOC_FREE(cap_smb_fname);
525         if (saved_errno != 0) {
526                 errno = saved_errno;
527         }
528         return ret;
529 }
530
531 static int cap_link(vfs_handle_struct *handle,
532                         const struct smb_filename *old_smb_fname,
533                         const struct smb_filename *new_smb_fname)
534 {
535         char *capold = capencode(talloc_tos(), old_smb_fname->base_name);
536         char *capnew = capencode(talloc_tos(), new_smb_fname->base_name);
537         struct smb_filename *old_cap_smb_fname = NULL;
538         struct smb_filename *new_cap_smb_fname = NULL;
539         int saved_errno = 0;
540         int ret;
541
542         if (!capold || !capnew) {
543                 errno = ENOMEM;
544                 return -1;
545         }
546         old_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
547                                         capold,
548                                         NULL,
549                                         NULL,
550                                         old_smb_fname->flags);
551         if (old_cap_smb_fname == NULL) {
552                 TALLOC_FREE(capold);
553                 TALLOC_FREE(capnew);
554                 errno = ENOMEM;
555                 return -1;
556         }
557         new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
558                                         capnew,
559                                         NULL,
560                                         NULL,
561                                         new_smb_fname->flags);
562         if (new_cap_smb_fname == NULL) {
563                 TALLOC_FREE(capold);
564                 TALLOC_FREE(capnew);
565                 TALLOC_FREE(old_cap_smb_fname);
566                 errno = ENOMEM;
567                 return -1;
568         }
569         ret = SMB_VFS_NEXT_LINK(handle, old_cap_smb_fname, new_cap_smb_fname);
570         if (ret == -1) {
571                 saved_errno = errno;
572         }
573         TALLOC_FREE(capold);
574         TALLOC_FREE(capnew);
575         TALLOC_FREE(old_cap_smb_fname);
576         TALLOC_FREE(new_cap_smb_fname);
577         if (saved_errno != 0) {
578                 errno = saved_errno;
579         }
580         return ret;
581 }
582
583 static int cap_mknod(vfs_handle_struct *handle,
584                 const struct smb_filename *smb_fname,
585                 mode_t mode,
586                 SMB_DEV_T dev)
587 {
588         struct smb_filename *cap_smb_fname = NULL;
589         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
590         int ret;
591         int saved_errno = 0;
592
593         if (!cappath) {
594                 errno = ENOMEM;
595                 return -1;
596         }
597         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
598                                         cappath,
599                                         NULL,
600                                         NULL,
601                                         smb_fname->flags);
602         if (cap_smb_fname == NULL) {
603                 TALLOC_FREE(cappath);
604                 errno = ENOMEM;
605                 return -1;
606         }
607         ret = SMB_VFS_NEXT_MKNOD(handle, cap_smb_fname, mode, dev);
608         if (ret == -1) {
609                 saved_errno = errno;
610         }
611         TALLOC_FREE(cappath);
612         TALLOC_FREE(cap_smb_fname);
613         if (saved_errno != 0) {
614                 errno = saved_errno;
615         }
616         return ret;
617 }
618
619 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
620 {
621         /* monyo need capencode'ed and capdecode'ed? */
622         char *cappath = capencode(talloc_tos(), path);
623
624         if (!cappath) {
625                 errno = ENOMEM;
626                 return NULL;
627         }
628         return SMB_VFS_NEXT_REALPATH(handle, cappath);
629 }
630
631 static int cap_chmod_acl(vfs_handle_struct *handle,
632                         const struct smb_filename *smb_fname,
633                         mode_t mode)
634 {
635         struct smb_filename *cap_smb_fname = NULL;
636         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
637         int ret;
638         int saved_errno;
639
640         /* If the underlying VFS doesn't have ACL support... */
641         if (!cappath) {
642                 errno = ENOMEM;
643                 return -1;
644         }
645         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
646                                         cappath,
647                                         NULL,
648                                         NULL,
649                                         smb_fname->flags);
650         if (cap_smb_fname == NULL) {
651                 TALLOC_FREE(cappath);
652                 errno = ENOMEM;
653                 return -1;
654         }
655
656         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, cap_smb_fname, mode);
657         saved_errno = errno;
658         TALLOC_FREE(cappath);
659         TALLOC_FREE(cap_smb_fname);
660         errno = saved_errno;
661         return ret;
662 }
663
664 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
665                                 const struct smb_filename *smb_fname,
666                                 SMB_ACL_TYPE_T type,
667                                 TALLOC_CTX *mem_ctx)
668 {
669         struct smb_filename *cap_smb_fname = NULL;
670         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
671         SMB_ACL_T ret;
672         int saved_errno = 0;
673
674         if (!cappath) {
675                 errno = ENOMEM;
676                 return (SMB_ACL_T)NULL;
677         }
678         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
679                                         cappath,
680                                         NULL,
681                                         NULL,
682                                         smb_fname->flags);
683         if (cap_smb_fname == NULL) {
684                 TALLOC_FREE(cappath);
685                 errno = ENOMEM;
686                 return (SMB_ACL_T)NULL;
687         }
688         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
689                                 type, mem_ctx);
690         if (ret == NULL) {
691                 saved_errno = errno;
692         }
693         TALLOC_FREE(cappath);
694         TALLOC_FREE(cap_smb_fname);
695         if (saved_errno != 0) {
696                 errno = saved_errno;
697         }
698         return ret;
699 }
700
701 static int cap_sys_acl_set_file(vfs_handle_struct *handle,
702                         const struct smb_filename *smb_fname,
703                         SMB_ACL_TYPE_T acltype,
704                         SMB_ACL_T theacl)
705 {
706         struct smb_filename *cap_smb_fname = NULL;
707         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
708         int ret;
709         int saved_errno = 0;
710
711         if (!cappath) {
712                 errno = ENOMEM;
713                 return -1;
714         }
715         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
716                                         cappath,
717                                         NULL,
718                                         NULL,
719                                         smb_fname->flags);
720         if (cap_smb_fname == NULL) {
721                 TALLOC_FREE(cappath);
722                 errno = ENOMEM;
723                 return -1;
724         }
725         ret =  SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cap_smb_fname,
726                                 acltype, theacl);
727         if (ret == -1) {
728                 saved_errno = errno;
729         }
730         TALLOC_FREE(cappath);
731         TALLOC_FREE(cap_smb_fname);
732         if (saved_errno != 0) {
733                 errno = saved_errno;
734         }
735         return ret;
736 }
737
738 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
739                         const struct smb_filename *smb_fname)
740 {
741         struct smb_filename *cap_smb_fname = NULL;
742         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
743         int ret;
744         int saved_errno = 0;
745
746         if (!cappath) {
747                 errno = ENOMEM;
748                 return -1;
749         }
750         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
751                                         cappath,
752                                         NULL,
753                                         NULL,
754                                         smb_fname->flags);
755         if (cap_smb_fname == NULL) {
756                 TALLOC_FREE(cappath);
757                 errno = ENOMEM;
758                 return -1;
759         }
760         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
761         if (ret == -1) {
762                 saved_errno = errno;
763         }
764         TALLOC_FREE(cappath);
765         TALLOC_FREE(cap_smb_fname);
766         if (saved_errno) {
767                 errno = saved_errno;
768         }
769         return ret;
770 }
771
772 static ssize_t cap_getxattr(vfs_handle_struct *handle,
773                         const struct smb_filename *smb_fname,
774                         const char *name,
775                         void *value,
776                         size_t size)
777 {
778         struct smb_filename *cap_smb_fname = NULL;
779         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
780         char *capname = capencode(talloc_tos(), name);
781         ssize_t ret;
782         int saved_errno = 0;
783
784         if (!cappath || !capname) {
785                 errno = ENOMEM;
786                 return -1;
787         }
788         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
789                                         cappath,
790                                         NULL,
791                                         NULL,
792                                         smb_fname->flags);
793         if (cap_smb_fname == NULL) {
794                 TALLOC_FREE(cappath);
795                 TALLOC_FREE(capname);
796                 errno = ENOMEM;
797                 return -1;
798         }
799         ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
800                         capname, value, size);
801         if (ret == -1) {
802                 saved_errno = errno;
803         }
804         TALLOC_FREE(cappath);
805         TALLOC_FREE(capname);
806         TALLOC_FREE(cap_smb_fname);
807         if (saved_errno) {
808                 errno = saved_errno;
809         }
810         return ret;
811 }
812
813 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
814 {
815         char *cappath = capencode(talloc_tos(), path);
816
817         if (!cappath) {
818                 errno = ENOMEM;
819                 return -1;
820         }
821         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
822 }
823
824 static ssize_t cap_listxattr(vfs_handle_struct *handle,
825                                 const struct smb_filename *smb_fname,
826                                 char *list,
827                                 size_t size)
828 {
829         struct smb_filename *cap_smb_fname = NULL;
830         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
831         ssize_t ret;
832         int saved_errno = 0;
833
834         if (!cappath) {
835                 errno = ENOMEM;
836                 return -1;
837         }
838         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
839                                         cappath,
840                                         NULL,
841                                         NULL,
842                                         smb_fname->flags);
843         if (cap_smb_fname == NULL) {
844                 TALLOC_FREE(cappath);
845                 errno = ENOMEM;
846                 return -1;
847         }
848         ret = SMB_VFS_NEXT_LISTXATTR(handle, cap_smb_fname, list, size);
849         if (ret == -1) {
850                 saved_errno = errno;
851         }
852         TALLOC_FREE(cappath);
853         TALLOC_FREE(cap_smb_fname);
854         if (saved_errno) {
855                 errno = saved_errno;
856         }
857         return ret;
858 }
859
860 static int cap_removexattr(vfs_handle_struct *handle,
861                                 const struct smb_filename *smb_fname,
862                                 const char *name)
863 {
864         struct smb_filename *cap_smb_fname = NULL;
865         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
866         char *capname = capencode(talloc_tos(), name);
867         int ret;
868         int saved_errno = 0;
869
870         if (!cappath || !capname) {
871                 errno = ENOMEM;
872                 return -1;
873         }
874         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
875                                         cappath,
876                                         NULL,
877                                         NULL,
878                                         smb_fname->flags);
879         if (cap_smb_fname == NULL) {
880                 TALLOC_FREE(cappath);
881                 TALLOC_FREE(capname);
882                 errno = ENOMEM;
883                 return -1;
884         }
885         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
886         if (ret == -1) {
887                 saved_errno = errno;
888         }
889         TALLOC_FREE(cappath);
890         TALLOC_FREE(capname);
891         TALLOC_FREE(cap_smb_fname);
892         if (saved_errno) {
893                 errno = saved_errno;
894         }
895         return ret;
896 }
897
898 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
899 {
900         char *cappath = capencode(talloc_tos(), path);
901
902         if (!cappath) {
903                 errno = ENOMEM;
904                 return -1;
905         }
906         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
907 }
908
909 static int cap_setxattr(vfs_handle_struct *handle,
910                         const struct smb_filename *smb_fname,
911                         const char *name,
912                         const void *value,
913                         size_t size,
914                         int flags)
915 {
916         struct smb_filename *cap_smb_fname = NULL;
917         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
918         char *capname = capencode(talloc_tos(), name);
919         int ret;
920         int saved_errno = 0;
921
922         if (!cappath || !capname) {
923                 errno = ENOMEM;
924                 return -1;
925         }
926         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
927                                         cappath,
928                                         NULL,
929                                         NULL,
930                                         smb_fname->flags);
931         if (cap_smb_fname == NULL) {
932                 TALLOC_FREE(cappath);
933                 TALLOC_FREE(capname);
934                 errno = ENOMEM;
935                 return -1;
936         }
937         ret = SMB_VFS_NEXT_SETXATTR(handle, cap_smb_fname,
938                                 capname, value, size, flags);
939         if (ret == -1) {
940                 saved_errno = errno;
941         }
942         TALLOC_FREE(cappath);
943         TALLOC_FREE(capname);
944         TALLOC_FREE(cap_smb_fname);
945         if (saved_errno) {
946                 errno = saved_errno;
947         }
948         return ret;
949 }
950
951 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
952 {
953         char *cappath = capencode(talloc_tos(), path);
954
955         if (!cappath) {
956                 errno = ENOMEM;
957                 return -1;
958         }
959         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
960 }
961
962 static struct vfs_fn_pointers vfs_cap_fns = {
963         .disk_free_fn = cap_disk_free,
964         .get_quota_fn = cap_get_quota,
965         .opendir_fn = cap_opendir,
966         .readdir_fn = cap_readdir,
967         .mkdir_fn = cap_mkdir,
968         .rmdir_fn = cap_rmdir,
969         .open_fn = cap_open,
970         .rename_fn = cap_rename,
971         .stat_fn = cap_stat,
972         .lstat_fn = cap_lstat,
973         .unlink_fn = cap_unlink,
974         .chmod_fn = cap_chmod,
975         .chown_fn = cap_chown,
976         .lchown_fn = cap_lchown,
977         .chdir_fn = cap_chdir,
978         .ntimes_fn = cap_ntimes,
979         .symlink_fn = cap_symlink,
980         .readlink_fn = cap_readlink,
981         .link_fn = cap_link,
982         .mknod_fn = cap_mknod,
983         .realpath_fn = cap_realpath,
984         .chmod_acl_fn = cap_chmod_acl,
985         .sys_acl_get_file_fn = cap_sys_acl_get_file,
986         .sys_acl_set_file_fn = cap_sys_acl_set_file,
987         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
988         .getxattr_fn = cap_getxattr,
989         .fgetxattr_fn = cap_fgetxattr,
990         .listxattr_fn = cap_listxattr,
991         .removexattr_fn = cap_removexattr,
992         .fremovexattr_fn = cap_fremovexattr,
993         .setxattr_fn = cap_setxattr,
994         .fsetxattr_fn = cap_fsetxattr
995 };
996
997 NTSTATUS vfs_cap_init(TALLOC_CTX *);
998 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
999 {
1000         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1001                                 &vfs_cap_fns);
1002 }
1003
1004 /* For CAP functions */
1005 #define hex_tag ':'
1006 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
1007 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
1008 #define is_hex(s)               ((s)[0] == hex_tag)
1009
1010 static unsigned char hex2bin_table[256] = {
1011 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1012 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1013 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1014 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1015 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1016 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1017 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1018 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1019 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1020 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1021 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1022 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1023 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1024 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1025 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1026 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1027 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1028 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
1029 };
1030 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1031
1032 /*******************************************************************
1033   original code -> ":xx"  - CAP format
1034 ********************************************************************/
1035
1036 static char *capencode(TALLOC_CTX *ctx, const char *from)
1037 {
1038         char *out = NULL;
1039         const char *p1;
1040         char *to = NULL;
1041         size_t len = 0;
1042
1043         for (p1 = from; *p1; p1++) {
1044                 if ((unsigned char)*p1 >= 0x80) {
1045                         len += 3;
1046                 } else {
1047                         len++;
1048                 }
1049         }
1050         len++;
1051
1052         to = talloc_array(ctx, char, len);
1053         if (!to) {
1054                 return NULL;
1055         }
1056
1057         for (out = to; *from;) {
1058                 /* buffer husoku error */
1059                 if ((unsigned char)*from >= 0x80) {
1060                         *out++ = hex_tag;
1061                         *out++ = bin2hex (((*from)>>4)&0x0f);
1062                         *out++ = bin2hex ((*from)&0x0f);
1063                         from++;
1064                 } else {
1065                         *out++ = *from++;
1066                 }
1067         }
1068         *out = '\0';
1069         return to;
1070 }
1071
1072 /*******************************************************************
1073   CAP -> original code
1074 ********************************************************************/
1075 /* ":xx" -> a byte */
1076
1077 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1078 {
1079         const char *p1;
1080         char *out = NULL;
1081         char *to = NULL;
1082         size_t len = 0;
1083
1084         for (p1 = from; *p1; len++) {
1085                 if (is_hex(p1)) {
1086                         p1 += 3;
1087                 } else {
1088                         p1++;
1089                 }
1090         }
1091         len++;
1092
1093         to = talloc_array(ctx, char, len);
1094         if (!to) {
1095                 return NULL;
1096         }
1097
1098         for (out = to; *from;) {
1099                 if (is_hex(from)) {
1100                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1101                         from += 3;
1102                 } else {
1103                         *out++ = *from++;
1104                 }
1105         }
1106         *out = '\0';
1107         return to;
1108 }