VFS: Remove SMB_VFS_SETXATTR, no longer used
[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->twrp,
50                                         smb_fname->flags);
51         if (cap_smb_fname == NULL) {
52                 TALLOC_FREE(capname);
53                 errno = ENOMEM;
54                 return (uint64_t)-1;
55         }
56         return SMB_VFS_NEXT_DISK_FREE(handle, cap_smb_fname,
57                         bsize, dfree, dsize);
58 }
59
60 static int cap_get_quota(vfs_handle_struct *handle,
61                         const struct smb_filename *smb_fname,
62                         enum SMB_QUOTA_TYPE qtype,
63                         unid_t id,
64                         SMB_DISK_QUOTA *dq)
65 {
66         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
67         struct smb_filename *cap_smb_fname = NULL;
68
69         if (!cappath) {
70                 errno = ENOMEM;
71                 return -1;
72         }
73         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
74                                         cappath,
75                                         NULL,
76                                         NULL,
77                                         smb_fname->twrp,
78                                         smb_fname->flags);
79         if (cap_smb_fname == NULL) {
80                 TALLOC_FREE(cappath);
81                 errno = ENOMEM;
82                 return -1;
83         }
84         return SMB_VFS_NEXT_GET_QUOTA(handle, cap_smb_fname, qtype, id, dq);
85 }
86
87 static struct dirent *cap_readdir(vfs_handle_struct *handle,
88                                   struct files_struct *dirfsp,
89                                   DIR *dirp,
90                                   SMB_STRUCT_STAT *sbuf)
91 {
92         struct dirent *result;
93         struct dirent *newdirent;
94         char *newname;
95         size_t newnamelen;
96         DEBUG(3,("cap: cap_readdir\n"));
97
98         result = SMB_VFS_NEXT_READDIR(handle, dirfsp, dirp, NULL);
99         if (!result) {
100                 return NULL;
101         }
102
103         newname = capdecode(talloc_tos(), result->d_name);
104         if (!newname) {
105                 return NULL;
106         }
107         DEBUG(3,("cap: cap_readdir: %s\n", newname));
108         newnamelen = strlen(newname)+1;
109         newdirent = talloc_size(
110                 talloc_tos(), sizeof(struct dirent) + newnamelen);
111         if (!newdirent) {
112                 return NULL;
113         }
114         talloc_set_name_const(newdirent, "struct dirent");
115         memcpy(newdirent, result, sizeof(struct dirent));
116         memcpy(&newdirent->d_name, newname, newnamelen);
117         return newdirent;
118 }
119
120 static int cap_mkdirat(vfs_handle_struct *handle,
121                 struct files_struct *dirfsp,
122                 const struct smb_filename *smb_fname,
123                 mode_t mode)
124 {
125         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
126         struct smb_filename *cap_smb_fname = NULL;
127
128         if (!cappath) {
129                 errno = ENOMEM;
130                 return -1;
131         }
132
133         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
134                                         cappath,
135                                         NULL,
136                                         NULL,
137                                         smb_fname->twrp,
138                                         smb_fname->flags);
139         if (cap_smb_fname == NULL) {
140                 TALLOC_FREE(cappath);
141                 errno = ENOMEM;
142                 return -1;
143         }
144
145         return SMB_VFS_NEXT_MKDIRAT(handle,
146                         dirfsp,
147                         cap_smb_fname,
148                         mode);
149 }
150
151 static int cap_openat(vfs_handle_struct *handle,
152                       const struct files_struct *dirfsp,
153                       const struct smb_filename *smb_fname_in,
154                       files_struct *fsp,
155                       int flags,
156                       mode_t mode)
157 {
158         char *cappath = NULL;
159         struct smb_filename *smb_fname = NULL;
160         int ret;
161         int saved_errno = 0;
162
163         cappath = capencode(talloc_tos(), smb_fname_in->base_name);
164         if (cappath == NULL) {
165                 errno = ENOMEM;
166                 return -1;
167         }
168
169         smb_fname = cp_smb_filename(talloc_tos(), smb_fname_in);
170         if (smb_fname == NULL) {
171                 TALLOC_FREE(cappath);
172                 errno = ENOMEM;
173                 return -1;
174         }
175         smb_fname->base_name = cappath;
176
177         DBG_DEBUG("cap_open for %s\n", smb_fname_str_dbg(smb_fname));
178         ret = SMB_VFS_NEXT_OPENAT(handle,
179                                   dirfsp,
180                                   smb_fname,
181                                   fsp,
182                                   flags,
183                                   mode);
184
185         if (ret == -1) {
186                 saved_errno = errno;
187         }
188         TALLOC_FREE(cappath);
189         TALLOC_FREE(smb_fname);
190         if (saved_errno != 0) {
191                 errno = saved_errno;
192         }
193         return ret;
194 }
195
196 static int cap_renameat(vfs_handle_struct *handle,
197                         files_struct *srcfsp,
198                         const struct smb_filename *smb_fname_src,
199                         files_struct *dstfsp,
200                         const struct smb_filename *smb_fname_dst)
201 {
202         char *capold = NULL;
203         char *capnew = NULL;
204         struct smb_filename *smb_fname_src_tmp = NULL;
205         struct smb_filename *smb_fname_dst_tmp = NULL;
206         int ret = -1;
207
208         capold = capencode(talloc_tos(), smb_fname_src->base_name);
209         capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
210         if (!capold || !capnew) {
211                 errno = ENOMEM;
212                 goto out;
213         }
214
215         /* Setup temporary smb_filename structs. */
216         smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
217         if (smb_fname_src_tmp == NULL) {
218                 errno = ENOMEM;
219                 goto out;
220         }
221         smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
222         if (smb_fname_dst_tmp == NULL) {
223                 errno = ENOMEM;
224                 goto out;
225         }
226
227         smb_fname_src_tmp->base_name = capold;
228         smb_fname_dst_tmp->base_name = capnew;
229
230         ret = SMB_VFS_NEXT_RENAMEAT(handle,
231                                 srcfsp,
232                                 smb_fname_src_tmp,
233                                 dstfsp,
234                                 smb_fname_dst_tmp);
235
236  out:
237         TALLOC_FREE(capold);
238         TALLOC_FREE(capnew);
239         TALLOC_FREE(smb_fname_src_tmp);
240         TALLOC_FREE(smb_fname_dst_tmp);
241
242         return ret;
243 }
244
245 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
246 {
247         char *cappath;
248         char *tmp_base_name = NULL;
249         int ret;
250
251         cappath = capencode(talloc_tos(), smb_fname->base_name);
252
253         if (!cappath) {
254                 errno = ENOMEM;
255                 return -1;
256         }
257
258         tmp_base_name = smb_fname->base_name;
259         smb_fname->base_name = cappath;
260
261         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
262
263         smb_fname->base_name = tmp_base_name;
264         TALLOC_FREE(cappath);
265
266         return ret;
267 }
268
269 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
270 {
271         char *cappath;
272         char *tmp_base_name = NULL;
273         int ret;
274
275         cappath = capencode(talloc_tos(), smb_fname->base_name);
276
277         if (!cappath) {
278                 errno = ENOMEM;
279                 return -1;
280         }
281
282         tmp_base_name = smb_fname->base_name;
283         smb_fname->base_name = cappath;
284
285         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
286
287         smb_fname->base_name = tmp_base_name;
288         TALLOC_FREE(cappath);
289
290         return ret;
291 }
292
293 static int cap_unlinkat(vfs_handle_struct *handle,
294                         struct files_struct *dirfsp,
295                         const struct smb_filename *smb_fname,
296                         int flags)
297 {
298         struct smb_filename *full_fname = NULL;
299         struct smb_filename *smb_fname_tmp = NULL;
300         char *cappath = NULL;
301         int ret;
302
303         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
304                                                   dirfsp,
305                                                   smb_fname);
306         if (full_fname == NULL) {
307                 return -1;
308         }
309
310         cappath = capencode(talloc_tos(), full_fname->base_name);
311         if (!cappath) {
312                 TALLOC_FREE(full_fname);
313                 errno = ENOMEM;
314                 return -1;
315         }
316
317         /* Setup temporary smb_filename structs. */
318         smb_fname_tmp = cp_smb_filename(talloc_tos(), full_fname);
319         TALLOC_FREE(full_fname);
320         if (smb_fname_tmp == NULL) {
321                 errno = ENOMEM;
322                 return -1;
323         }
324
325         smb_fname_tmp->base_name = cappath;
326
327         ret = SMB_VFS_NEXT_UNLINKAT(handle,
328                         dirfsp->conn->cwd_fsp,
329                         smb_fname_tmp,
330                         flags);
331
332         TALLOC_FREE(smb_fname_tmp);
333         return ret;
334 }
335
336 static int cap_chmod(vfs_handle_struct *handle,
337                         const struct smb_filename *smb_fname,
338                         mode_t mode)
339 {
340         struct smb_filename *cap_smb_fname = NULL;
341         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
342         int ret;
343         int saved_errno;
344
345         if (!cappath) {
346                 errno = ENOMEM;
347                 return -1;
348         }
349
350         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
351                                         cappath,
352                                         NULL,
353                                         NULL,
354                                         smb_fname->twrp,
355                                         smb_fname->flags);
356         if (cap_smb_fname == NULL) {
357                 TALLOC_FREE(cappath);
358                 errno = ENOMEM;
359                 return -1;
360         }
361
362         ret = SMB_VFS_NEXT_CHMOD(handle, cap_smb_fname, mode);
363         saved_errno = errno;
364         TALLOC_FREE(cappath);
365         TALLOC_FREE(cap_smb_fname);
366         errno = saved_errno;
367         return ret;
368 }
369
370 static int cap_lchown(vfs_handle_struct *handle,
371                         const struct smb_filename *smb_fname,
372                         uid_t uid,
373                         gid_t gid)
374 {
375         struct smb_filename *cap_smb_fname = NULL;
376         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
377         int ret;
378         int saved_errno;
379
380         if (!cappath) {
381                 errno = ENOMEM;
382                 return -1;
383         }
384
385         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
386                                         cappath,
387                                         NULL,
388                                         NULL,
389                                         smb_fname->twrp,
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_LCHOWN(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_chdir(vfs_handle_struct *handle,
406                         const struct smb_filename *smb_fname)
407 {
408         struct smb_filename *cap_smb_fname = NULL;
409         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
410         int ret;
411         int saved_errno = 0;
412
413         if (!cappath) {
414                 errno = ENOMEM;
415                 return -1;
416         }
417         DEBUG(3,("cap: cap_chdir for %s\n", smb_fname->base_name));
418
419         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
420                                         cappath,
421                                         NULL,
422                                         NULL,
423                                         smb_fname->twrp,
424                                         smb_fname->flags);
425         if (cap_smb_fname == NULL) {
426                 TALLOC_FREE(cappath);
427                 errno = ENOMEM;
428                 return -1;
429         }
430         ret = SMB_VFS_NEXT_CHDIR(handle, cap_smb_fname);
431         if (ret == -1) {
432                 saved_errno = errno;
433         }
434         TALLOC_FREE(cappath);
435         TALLOC_FREE(cap_smb_fname);
436         if (saved_errno != 0) {
437                 errno = saved_errno;
438         }
439         return ret;
440 }
441
442 static int cap_ntimes(vfs_handle_struct *handle,
443                       const struct smb_filename *smb_fname,
444                       struct smb_file_time *ft)
445 {
446         struct smb_filename *smb_fname_tmp = NULL;
447         char *cappath = NULL;
448         int ret;
449
450         cappath = capencode(talloc_tos(), smb_fname->base_name);
451
452         if (!cappath) {
453                 errno = ENOMEM;
454                 return -1;
455         }
456
457         /* Setup temporary smb_filename structs. */
458         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
459         if (smb_fname_tmp == NULL) {
460                 errno = ENOMEM;
461                 return -1;
462         }
463
464         smb_fname_tmp->base_name = cappath;
465
466         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
467
468         TALLOC_FREE(smb_fname_tmp);
469         return ret;
470 }
471
472 static int cap_symlinkat(vfs_handle_struct *handle,
473                         const struct smb_filename *link_contents,
474                         struct files_struct *dirfsp,
475                         const struct smb_filename *new_smb_fname)
476 {
477         struct smb_filename *full_fname = NULL;
478         char *capold = capencode(talloc_tos(), link_contents->base_name);
479         char *capnew = NULL;
480         struct smb_filename *new_link_target = NULL;
481         struct smb_filename *new_cap_smb_fname = NULL;
482         int saved_errno = 0;
483         int ret;
484
485         if (!capold || !capnew) {
486                 errno = ENOMEM;
487                 return -1;
488         }
489
490         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
491                                                 dirfsp,
492                                                 new_smb_fname);
493         if (full_fname == NULL) {
494                 return -1;
495         }
496
497         capnew = capencode(talloc_tos(), full_fname->base_name);
498         if (!capnew) {
499                 TALLOC_FREE(full_fname);
500                 errno = ENOMEM;
501                 return -1;
502         }
503
504         new_link_target = synthetic_smb_fname(talloc_tos(),
505                                               capold,
506                                               NULL,
507                                               NULL,
508                                               new_smb_fname->twrp,
509                                               new_smb_fname->flags);
510         if (new_link_target == NULL) {
511                 TALLOC_FREE(full_fname);
512                 TALLOC_FREE(capold);
513                 TALLOC_FREE(capnew);
514                 errno = ENOMEM;
515                 return -1;
516         }
517
518         new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
519                                         capnew,
520                                         NULL,
521                                         NULL,
522                                         new_smb_fname->twrp,
523                                         new_smb_fname->flags);
524         if (new_cap_smb_fname == NULL) {
525                 TALLOC_FREE(full_fname);
526                 TALLOC_FREE(capold);
527                 TALLOC_FREE(capnew);
528                 TALLOC_FREE(new_link_target);
529                 errno = ENOMEM;
530                 return -1;
531         }
532         ret = SMB_VFS_NEXT_SYMLINKAT(handle,
533                         new_link_target,
534                         handle->conn->cwd_fsp,
535                         new_cap_smb_fname);
536         if (ret == -1) {
537                 saved_errno = errno;
538         }
539         TALLOC_FREE(full_fname);
540         TALLOC_FREE(capold);
541         TALLOC_FREE(capnew);
542         TALLOC_FREE(new_link_target);
543         TALLOC_FREE(new_cap_smb_fname);
544         if (saved_errno != 0) {
545                 errno = saved_errno;
546         }
547         return ret;
548 }
549
550 static int cap_readlinkat(vfs_handle_struct *handle,
551                         const struct files_struct *dirfsp,
552                         const struct smb_filename *smb_fname,
553                         char *buf,
554                         size_t bufsiz)
555 {
556         struct smb_filename *full_fname = NULL;
557         struct smb_filename *cap_smb_fname = NULL;
558         char *cappath = NULL;
559         int saved_errno = 0;
560         int ret;
561
562         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
563                                                 dirfsp,
564                                                 smb_fname);
565         if (full_fname == NULL) {
566                 return -1;
567         }
568
569         cappath = capencode(talloc_tos(), full_fname->base_name);
570         if (cappath == NULL) {
571                 TALLOC_FREE(full_fname);
572                 errno = ENOMEM;
573                 return -1;
574         }
575         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
576                                         cappath,
577                                         NULL,
578                                         NULL,
579                                         smb_fname->twrp,
580                                         smb_fname->flags);
581         if (cap_smb_fname == NULL) {
582                 TALLOC_FREE(full_fname);
583                 TALLOC_FREE(cappath);
584                 errno = ENOMEM;
585                 return -1;
586         }
587         ret = SMB_VFS_NEXT_READLINKAT(handle,
588                         handle->conn->cwd_fsp,
589                         cap_smb_fname,
590                         buf,
591                         bufsiz);
592         if (ret == -1) {
593                 saved_errno = errno;
594         }
595         TALLOC_FREE(full_fname);
596         TALLOC_FREE(cappath);
597         TALLOC_FREE(cap_smb_fname);
598         if (saved_errno != 0) {
599                 errno = saved_errno;
600         }
601         return ret;
602 }
603
604 static int cap_linkat(vfs_handle_struct *handle,
605                 files_struct *srcfsp,
606                 const struct smb_filename *old_smb_fname,
607                 files_struct *dstfsp,
608                 const struct smb_filename *new_smb_fname,
609                 int flags)
610 {
611         struct smb_filename *old_full_fname = NULL;
612         struct smb_filename *new_full_fname = NULL;
613         char *capold = NULL;
614         char *capnew = NULL;
615         struct smb_filename *old_cap_smb_fname = NULL;
616         struct smb_filename *new_cap_smb_fname = NULL;
617         int saved_errno = 0;
618         int ret;
619
620         /* Process 'old' name. */
621         old_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
622                                                 srcfsp,
623                                                 old_smb_fname);
624         if (old_full_fname == NULL) {
625                 goto nomem_out;
626         }
627         capold = capencode(talloc_tos(), old_full_fname->base_name);
628         if (capold == NULL) {
629                 goto nomem_out;
630         }
631         TALLOC_FREE(old_full_fname);
632         old_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
633                                         capold,
634                                         NULL,
635                                         NULL,
636                                         old_smb_fname->twrp,
637                                         old_smb_fname->flags);
638         if (old_cap_smb_fname == NULL) {
639                 goto nomem_out;
640         }
641
642         /* Process 'new' name. */
643         new_full_fname = full_path_from_dirfsp_atname(talloc_tos(),
644                                                 dstfsp,
645                                                 new_smb_fname);
646         if (new_full_fname == NULL) {
647                 goto nomem_out;
648         }
649         capnew = capencode(talloc_tos(), new_full_fname->base_name);
650         if (capnew == NULL) {
651                 goto nomem_out;
652         }
653         TALLOC_FREE(new_full_fname);
654         new_cap_smb_fname = synthetic_smb_fname(talloc_tos(),
655                                         capnew,
656                                         NULL,
657                                         NULL,
658                                         new_smb_fname->twrp,
659                                         new_smb_fname->flags);
660         if (new_cap_smb_fname == NULL) {
661                 goto nomem_out;
662         }
663
664         ret = SMB_VFS_NEXT_LINKAT(handle,
665                         handle->conn->cwd_fsp,
666                         old_cap_smb_fname,
667                         handle->conn->cwd_fsp,
668                         new_cap_smb_fname,
669                         flags);
670         if (ret == -1) {
671                 saved_errno = errno;
672         }
673         TALLOC_FREE(old_full_fname);
674         TALLOC_FREE(old_full_fname);
675         TALLOC_FREE(capold);
676         TALLOC_FREE(capnew);
677         TALLOC_FREE(old_cap_smb_fname);
678         TALLOC_FREE(new_cap_smb_fname);
679         if (saved_errno != 0) {
680                 errno = saved_errno;
681         }
682         return ret;
683
684   nomem_out:
685
686         TALLOC_FREE(old_full_fname);
687         TALLOC_FREE(old_full_fname);
688         TALLOC_FREE(capold);
689         TALLOC_FREE(capnew);
690         TALLOC_FREE(old_cap_smb_fname);
691         TALLOC_FREE(new_cap_smb_fname);
692         errno = ENOMEM;
693         return -1;
694 }
695
696 static int cap_mknodat(vfs_handle_struct *handle,
697                 files_struct *dirfsp,
698                 const struct smb_filename *smb_fname,
699                 mode_t mode,
700                 SMB_DEV_T dev)
701 {
702         struct smb_filename *full_fname = NULL;
703         struct smb_filename *cap_smb_fname = NULL;
704         char *cappath = NULL;
705         int ret;
706         int saved_errno = 0;
707
708         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
709                                                 dirfsp,
710                                                 smb_fname);
711         if (full_fname == NULL) {
712                 return -1;
713         }
714
715         cappath = capencode(talloc_tos(), full_fname->base_name);
716         if (!cappath) {
717                 TALLOC_FREE(full_fname);
718                 errno = ENOMEM;
719                 return -1;
720         }
721         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
722                                         cappath,
723                                         NULL,
724                                         NULL,
725                                         smb_fname->twrp,
726                                         smb_fname->flags);
727         if (cap_smb_fname == NULL) {
728                 TALLOC_FREE(full_fname);
729                 TALLOC_FREE(cappath);
730                 errno = ENOMEM;
731                 return -1;
732         }
733         ret = SMB_VFS_NEXT_MKNODAT(handle,
734                         handle->conn->cwd_fsp,
735                         cap_smb_fname,
736                         mode,
737                         dev);
738         if (ret == -1) {
739                 saved_errno = errno;
740         }
741         TALLOC_FREE(full_fname);
742         TALLOC_FREE(cappath);
743         TALLOC_FREE(cap_smb_fname);
744         if (saved_errno != 0) {
745                 errno = saved_errno;
746         }
747         return ret;
748 }
749
750 static struct smb_filename *cap_realpath(vfs_handle_struct *handle,
751                         TALLOC_CTX *ctx,
752                         const struct smb_filename *smb_fname)
753 {
754         /* monyo need capencode'ed and capdecode'ed? */
755         struct smb_filename *cap_smb_fname = NULL;
756         struct smb_filename *return_fname = NULL;
757         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
758         int saved_errno = 0;
759
760         if (!cappath) {
761                 errno = ENOMEM;
762                 return NULL;
763         }
764         cap_smb_fname = synthetic_smb_fname(ctx,
765                                         cappath,
766                                         NULL,
767                                         NULL,
768                                         smb_fname->twrp,
769                                         smb_fname->flags);
770         if (cap_smb_fname == NULL) {
771                 TALLOC_FREE(cappath);
772                 errno = ENOMEM;
773                 return NULL;
774         }
775         return_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, cap_smb_fname);
776         if (return_fname == NULL) {
777                 saved_errno = errno;
778         }
779         TALLOC_FREE(cappath);
780         TALLOC_FREE(cap_smb_fname);
781         if (saved_errno != 0) {
782                 errno = saved_errno;
783         }
784         return return_fname;
785 }
786
787 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
788                                 const struct smb_filename *smb_fname,
789                                 SMB_ACL_TYPE_T type,
790                                 TALLOC_CTX *mem_ctx)
791 {
792         struct smb_filename *cap_smb_fname = NULL;
793         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
794         SMB_ACL_T ret;
795         int saved_errno = 0;
796
797         if (!cappath) {
798                 errno = ENOMEM;
799                 return (SMB_ACL_T)NULL;
800         }
801         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
802                                         cappath,
803                                         NULL,
804                                         NULL,
805                                         smb_fname->twrp,
806                                         smb_fname->flags);
807         if (cap_smb_fname == NULL) {
808                 TALLOC_FREE(cappath);
809                 errno = ENOMEM;
810                 return (SMB_ACL_T)NULL;
811         }
812         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cap_smb_fname,
813                                 type, mem_ctx);
814         if (ret == NULL) {
815                 saved_errno = errno;
816         }
817         TALLOC_FREE(cappath);
818         TALLOC_FREE(cap_smb_fname);
819         if (saved_errno != 0) {
820                 errno = saved_errno;
821         }
822         return ret;
823 }
824
825 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle,
826                         const struct smb_filename *smb_fname)
827 {
828         struct smb_filename *cap_smb_fname = NULL;
829         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
830         int ret;
831         int saved_errno = 0;
832
833         if (!cappath) {
834                 errno = ENOMEM;
835                 return -1;
836         }
837         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
838                                         cappath,
839                                         NULL,
840                                         NULL,
841                                         smb_fname->twrp,
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_SYS_ACL_DELETE_DEF_FILE(handle, cap_smb_fname);
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 ssize_t cap_getxattr(vfs_handle_struct *handle,
861                         const struct smb_filename *smb_fname,
862                         const char *name,
863                         void *value,
864                         size_t size)
865 {
866         struct smb_filename *cap_smb_fname = NULL;
867         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
868         char *capname = capencode(talloc_tos(), name);
869         ssize_t ret;
870         int saved_errno = 0;
871
872         if (!cappath || !capname) {
873                 errno = ENOMEM;
874                 return -1;
875         }
876         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
877                                         cappath,
878                                         NULL,
879                                         NULL,
880                                         smb_fname->twrp,
881                                         smb_fname->flags);
882         if (cap_smb_fname == NULL) {
883                 TALLOC_FREE(cappath);
884                 TALLOC_FREE(capname);
885                 errno = ENOMEM;
886                 return -1;
887         }
888         ret = SMB_VFS_NEXT_GETXATTR(handle, cap_smb_fname,
889                         capname, value, size);
890         if (ret == -1) {
891                 saved_errno = errno;
892         }
893         TALLOC_FREE(cappath);
894         TALLOC_FREE(capname);
895         TALLOC_FREE(cap_smb_fname);
896         if (saved_errno) {
897                 errno = saved_errno;
898         }
899         return ret;
900 }
901
902 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
903 {
904         char *cappath = capencode(talloc_tos(), path);
905
906         if (!cappath) {
907                 errno = ENOMEM;
908                 return -1;
909         }
910         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
911 }
912
913 static int cap_removexattr(vfs_handle_struct *handle,
914                                 const struct smb_filename *smb_fname,
915                                 const char *name)
916 {
917         struct smb_filename *cap_smb_fname = NULL;
918         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
919         char *capname = capencode(talloc_tos(), name);
920         int ret;
921         int saved_errno = 0;
922
923         if (!cappath || !capname) {
924                 errno = ENOMEM;
925                 return -1;
926         }
927         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
928                                         cappath,
929                                         NULL,
930                                         NULL,
931                                         smb_fname->twrp,
932                                         smb_fname->flags);
933         if (cap_smb_fname == NULL) {
934                 TALLOC_FREE(cappath);
935                 TALLOC_FREE(capname);
936                 errno = ENOMEM;
937                 return -1;
938         }
939         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, cap_smb_fname, capname);
940         if (ret == -1) {
941                 saved_errno = errno;
942         }
943         TALLOC_FREE(cappath);
944         TALLOC_FREE(capname);
945         TALLOC_FREE(cap_smb_fname);
946         if (saved_errno) {
947                 errno = saved_errno;
948         }
949         return ret;
950 }
951
952 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
953 {
954         char *cappath = capencode(talloc_tos(), path);
955
956         if (!cappath) {
957                 errno = ENOMEM;
958                 return -1;
959         }
960         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
961 }
962
963 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
964 {
965         char *cappath = capencode(talloc_tos(), path);
966
967         if (!cappath) {
968                 errno = ENOMEM;
969                 return -1;
970         }
971         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
972 }
973
974 static NTSTATUS cap_create_dfs_pathat(vfs_handle_struct *handle,
975                         files_struct *dirfsp,
976                         const struct smb_filename *smb_fname,
977                         const struct referral *reflist,
978                         size_t referral_count)
979 {
980         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
981         struct smb_filename *cap_smb_fname = NULL;
982         NTSTATUS status;
983
984         if (cappath == NULL) {
985                 return NT_STATUS_NO_MEMORY;
986         }
987         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
988                                         cappath,
989                                         NULL,
990                                         NULL,
991                                         smb_fname->twrp,
992                                         smb_fname->flags);
993         if (cap_smb_fname == NULL) {
994                 TALLOC_FREE(cappath);
995                 return NT_STATUS_NO_MEMORY;
996         }
997         status = SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
998                         dirfsp,
999                         cap_smb_fname,
1000                         reflist,
1001                         referral_count);
1002         TALLOC_FREE(cappath);
1003         TALLOC_FREE(cap_smb_fname);
1004         return status;
1005 }
1006
1007 static NTSTATUS cap_read_dfs_pathat(struct vfs_handle_struct *handle,
1008                         TALLOC_CTX *mem_ctx,
1009                         struct files_struct *dirfsp,
1010                         struct smb_filename *smb_fname,
1011                         struct referral **ppreflist,
1012                         size_t *preferral_count)
1013 {
1014         char *cappath = capencode(talloc_tos(), smb_fname->base_name);
1015         struct smb_filename *cap_smb_fname = NULL;
1016         NTSTATUS status;
1017
1018         if (cappath == NULL) {
1019                 return NT_STATUS_NO_MEMORY;
1020         }
1021         cap_smb_fname = synthetic_smb_fname(talloc_tos(),
1022                                 cappath,
1023                                 NULL,
1024                                 NULL,
1025                                 smb_fname->twrp,
1026                                 smb_fname->flags);
1027         if (cap_smb_fname == NULL) {
1028                 TALLOC_FREE(cappath);
1029                 return NT_STATUS_NO_MEMORY;
1030         }
1031
1032         status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
1033                         mem_ctx,
1034                         dirfsp,
1035                         cap_smb_fname,
1036                         ppreflist,
1037                         preferral_count);
1038
1039         if (NT_STATUS_IS_OK(status)) {
1040                 /* Return any stat(2) info. */
1041                 smb_fname->st = cap_smb_fname->st;
1042         }
1043
1044         TALLOC_FREE(cappath);
1045         TALLOC_FREE(cap_smb_fname);
1046         return status;
1047 }
1048
1049 static struct vfs_fn_pointers vfs_cap_fns = {
1050         .disk_free_fn = cap_disk_free,
1051         .get_quota_fn = cap_get_quota,
1052         .readdir_fn = cap_readdir,
1053         .mkdirat_fn = cap_mkdirat,
1054         .openat_fn = cap_openat,
1055         .renameat_fn = cap_renameat,
1056         .stat_fn = cap_stat,
1057         .lstat_fn = cap_lstat,
1058         .unlinkat_fn = cap_unlinkat,
1059         .chmod_fn = cap_chmod,
1060         .lchown_fn = cap_lchown,
1061         .chdir_fn = cap_chdir,
1062         .ntimes_fn = cap_ntimes,
1063         .symlinkat_fn = cap_symlinkat,
1064         .readlinkat_fn = cap_readlinkat,
1065         .linkat_fn = cap_linkat,
1066         .mknodat_fn = cap_mknodat,
1067         .realpath_fn = cap_realpath,
1068         .sys_acl_get_file_fn = cap_sys_acl_get_file,
1069         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
1070         .getxattr_fn = cap_getxattr,
1071         .getxattrat_send_fn = vfs_not_implemented_getxattrat_send,
1072         .getxattrat_recv_fn = vfs_not_implemented_getxattrat_recv,
1073         .fgetxattr_fn = cap_fgetxattr,
1074         .removexattr_fn = cap_removexattr,
1075         .fremovexattr_fn = cap_fremovexattr,
1076         .fsetxattr_fn = cap_fsetxattr,
1077         .create_dfs_pathat_fn = cap_create_dfs_pathat,
1078         .read_dfs_pathat_fn = cap_read_dfs_pathat
1079 };
1080
1081 static_decl_vfs;
1082 NTSTATUS vfs_cap_init(TALLOC_CTX *ctx)
1083 {
1084         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
1085                                 &vfs_cap_fns);
1086 }
1087
1088 /* For CAP functions */
1089 #define hex_tag ':'
1090 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
1091 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
1092 #define is_hex(s)               ((s)[0] == hex_tag)
1093
1094 static unsigned char hex2bin_table[256] = {
1095 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
1096 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
1097 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
1098 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
1099 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
1100 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
1102 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
1103 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
1104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
1105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
1106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
1107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
1108 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
1109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
1110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
1111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
1112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
1113 };
1114 static unsigned char bin2hex_table[256] = "0123456789abcdef";
1115
1116 /*******************************************************************
1117   original code -> ":xx"  - CAP format
1118 ********************************************************************/
1119
1120 static char *capencode(TALLOC_CTX *ctx, const char *from)
1121 {
1122         char *out = NULL;
1123         const char *p1;
1124         char *to = NULL;
1125         size_t len = 0;
1126
1127         for (p1 = from; *p1; p1++) {
1128                 if ((unsigned char)*p1 >= 0x80) {
1129                         len += 3;
1130                 } else {
1131                         len++;
1132                 }
1133         }
1134         len++;
1135
1136         to = talloc_array(ctx, char, len);
1137         if (!to) {
1138                 return NULL;
1139         }
1140
1141         for (out = to; *from;) {
1142                 /* buffer husoku error */
1143                 if ((unsigned char)*from >= 0x80) {
1144                         *out++ = hex_tag;
1145                         *out++ = bin2hex (((*from)>>4)&0x0f);
1146                         *out++ = bin2hex ((*from)&0x0f);
1147                         from++;
1148                 } else {
1149                         *out++ = *from++;
1150                 }
1151         }
1152         *out = '\0';
1153         return to;
1154 }
1155
1156 /*******************************************************************
1157   CAP -> original code
1158 ********************************************************************/
1159 /* ":xx" -> a byte */
1160
1161 static char *capdecode(TALLOC_CTX *ctx, const char *from)
1162 {
1163         const char *p1;
1164         char *out = NULL;
1165         char *to = NULL;
1166         size_t len = 0;
1167
1168         for (p1 = from; *p1; len++) {
1169                 if (is_hex(p1)) {
1170                         p1 += 3;
1171                 } else {
1172                         p1++;
1173                 }
1174         }
1175         len++;
1176
1177         to = talloc_array(ctx, char, len);
1178         if (!to) {
1179                 return NULL;
1180         }
1181
1182         for (out = to; *from;) {
1183                 if (is_hex(from)) {
1184                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
1185                         from += 3;
1186                 } else {
1187                         *out++ = *from++;
1188                 }
1189         }
1190         *out = '\0';
1191         return to;
1192 }