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