vfs: Convert cap_unlink to cp_smb_filename
[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, const char *path,
33         bool small_query, uint64_t *bsize,
34         uint64_t *dfree, uint64_t *dsize)
35 {
36         char *cappath = capencode(talloc_tos(), path);
37
38         if (!cappath) {
39                 errno = ENOMEM;
40                 return (uint64_t)-1;
41         }
42         return SMB_VFS_NEXT_DISK_FREE(handle, cappath, small_query, bsize,
43                                         dfree, dsize);
44 }
45
46 static DIR *cap_opendir(vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
47 {
48         char *capname = capencode(talloc_tos(), fname);
49
50         if (!capname) {
51                 errno = ENOMEM;
52                 return NULL;
53         }
54         return SMB_VFS_NEXT_OPENDIR(handle, capname, mask, attr);
55 }
56
57 static struct dirent *cap_readdir(vfs_handle_struct *handle,
58                                       DIR *dirp,
59                                       SMB_STRUCT_STAT *sbuf)
60 {
61         struct dirent *result;
62         struct dirent *newdirent;
63         char *newname;
64         size_t newnamelen;
65         DEBUG(3,("cap: cap_readdir\n"));
66
67         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
68         if (!result) {
69                 return NULL;
70         }
71
72         newname = capdecode(talloc_tos(), result->d_name);
73         if (!newname) {
74                 return NULL;
75         }
76         DEBUG(3,("cap: cap_readdir: %s\n", newname));
77         newnamelen = strlen(newname)+1;
78         newdirent = (struct dirent *)talloc_array(talloc_tos(),
79                         char,
80                         sizeof(struct dirent)+
81                                 newnamelen);
82         if (!newdirent) {
83                 return NULL;
84         }
85         memcpy(newdirent, result, sizeof(struct dirent));
86         memcpy(&newdirent->d_name, newname, newnamelen);
87         return newdirent;
88 }
89
90 static int cap_mkdir(vfs_handle_struct *handle, const char *path, mode_t mode)
91 {
92         char *cappath = capencode(talloc_tos(), path);
93
94         if (!cappath) {
95                 errno = ENOMEM;
96                 return -1;
97         }
98         return SMB_VFS_NEXT_MKDIR(handle, cappath, mode);
99 }
100
101 static int cap_rmdir(vfs_handle_struct *handle, const char *path)
102 {
103         char *cappath = capencode(talloc_tos(), path);
104
105         if (!cappath) {
106                 errno = ENOMEM;
107                 return -1;
108         }
109         return SMB_VFS_NEXT_RMDIR(handle, cappath);
110 }
111
112 static int cap_open(vfs_handle_struct *handle, struct smb_filename *smb_fname,
113                     files_struct *fsp, int flags, mode_t mode)
114 {
115         char *cappath;
116         char *tmp_base_name = NULL;
117         int ret;
118
119         cappath = capencode(talloc_tos(), smb_fname->base_name);
120
121         if (!cappath) {
122                 errno = ENOMEM;
123                 return -1;
124         }
125
126         tmp_base_name = smb_fname->base_name;
127         smb_fname->base_name = cappath;
128
129         DEBUG(3,("cap: cap_open for %s\n", smb_fname_str_dbg(smb_fname)));
130         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
131
132         smb_fname->base_name = tmp_base_name;
133         TALLOC_FREE(cappath);
134
135         return ret;
136 }
137
138 static int cap_rename(vfs_handle_struct *handle,
139                       const struct smb_filename *smb_fname_src,
140                       const struct smb_filename *smb_fname_dst)
141 {
142         char *capold = NULL;
143         char *capnew = NULL;
144         struct smb_filename *smb_fname_src_tmp = NULL;
145         struct smb_filename *smb_fname_dst_tmp = NULL;
146         int ret = -1;
147
148         capold = capencode(talloc_tos(), smb_fname_src->base_name);
149         capnew = capencode(talloc_tos(), smb_fname_dst->base_name);
150         if (!capold || !capnew) {
151                 errno = ENOMEM;
152                 goto out;
153         }
154
155         /* Setup temporary smb_filename structs. */
156         smb_fname_src_tmp = cp_smb_filename(talloc_tos(), smb_fname_src);
157         if (smb_fname_src_tmp == NULL) {
158                 errno = ENOMEM;
159                 goto out;
160         }
161         smb_fname_dst_tmp = cp_smb_filename(talloc_tos(), smb_fname_dst);
162         if (smb_fname_dst_tmp == NULL) {
163                 errno = ENOMEM;
164                 goto out;
165         }
166
167         smb_fname_src_tmp->base_name = capold;
168         smb_fname_dst_tmp->base_name = capnew;
169
170         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
171                                   smb_fname_dst_tmp);
172  out:
173         TALLOC_FREE(capold);
174         TALLOC_FREE(capnew);
175         TALLOC_FREE(smb_fname_src_tmp);
176         TALLOC_FREE(smb_fname_dst_tmp);
177
178         return ret;
179 }
180
181 static int cap_stat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
182 {
183         char *cappath;
184         char *tmp_base_name = NULL;
185         int ret;
186
187         cappath = capencode(talloc_tos(), smb_fname->base_name);
188
189         if (!cappath) {
190                 errno = ENOMEM;
191                 return -1;
192         }
193
194         tmp_base_name = smb_fname->base_name;
195         smb_fname->base_name = cappath;
196
197         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
198
199         smb_fname->base_name = tmp_base_name;
200         TALLOC_FREE(cappath);
201
202         return ret;
203 }
204
205 static int cap_lstat(vfs_handle_struct *handle, struct smb_filename *smb_fname)
206 {
207         char *cappath;
208         char *tmp_base_name = NULL;
209         int ret;
210
211         cappath = capencode(talloc_tos(), smb_fname->base_name);
212
213         if (!cappath) {
214                 errno = ENOMEM;
215                 return -1;
216         }
217
218         tmp_base_name = smb_fname->base_name;
219         smb_fname->base_name = cappath;
220
221         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
222
223         smb_fname->base_name = tmp_base_name;
224         TALLOC_FREE(cappath);
225
226         return ret;
227 }
228
229 static int cap_unlink(vfs_handle_struct *handle,
230                       const struct smb_filename *smb_fname)
231 {
232         struct smb_filename *smb_fname_tmp = NULL;
233         char *cappath = NULL;
234         int ret;
235
236         cappath = capencode(talloc_tos(), smb_fname->base_name);
237         if (!cappath) {
238                 errno = ENOMEM;
239                 return -1;
240         }
241
242         /* Setup temporary smb_filename structs. */
243         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
244         if (smb_fname_tmp == NULL) {
245                 errno = ENOMEM;
246                 return -1;
247         }
248
249         smb_fname_tmp->base_name = cappath;
250
251         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
252
253         TALLOC_FREE(smb_fname_tmp);
254         return ret;
255 }
256
257 static int cap_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
258 {
259         char *cappath = capencode(talloc_tos(), path);
260
261         if (!cappath) {
262                 errno = ENOMEM;
263                 return -1;
264         }
265         return SMB_VFS_NEXT_CHMOD(handle, cappath, mode);
266 }
267
268 static int cap_chown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
269 {
270         char *cappath = capencode(talloc_tos(), path);
271
272         if (!cappath) {
273                 errno = ENOMEM;
274                 return -1;
275         }
276         return SMB_VFS_NEXT_CHOWN(handle, cappath, uid, gid);
277 }
278
279 static int cap_lchown(vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
280 {
281         char *cappath = capencode(talloc_tos(), path);
282
283         if (!cappath) {
284                 errno = ENOMEM;
285                 return -1;
286         }
287         return SMB_VFS_NEXT_LCHOWN(handle, cappath, uid, gid);
288 }
289
290 static int cap_chdir(vfs_handle_struct *handle, const char *path)
291 {
292         char *cappath = capencode(talloc_tos(), path);
293
294         if (!cappath) {
295                 errno = ENOMEM;
296                 return -1;
297         }
298         DEBUG(3,("cap: cap_chdir for %s\n", path));
299         return SMB_VFS_NEXT_CHDIR(handle, cappath);
300 }
301
302 static int cap_ntimes(vfs_handle_struct *handle,
303                       const struct smb_filename *smb_fname,
304                       struct smb_file_time *ft)
305 {
306         struct smb_filename *smb_fname_tmp = NULL;
307         char *cappath = NULL;
308         NTSTATUS status;
309         int ret;
310
311         cappath = capencode(talloc_tos(), smb_fname->base_name);
312
313         if (!cappath) {
314                 errno = ENOMEM;
315                 return -1;
316         }
317
318         /* Setup temporary smb_filename structs. */
319         status = copy_smb_filename(talloc_tos(), smb_fname,
320                                    &smb_fname_tmp);
321         if (!NT_STATUS_IS_OK(status)) {
322                 errno = map_errno_from_nt_status(status);
323                 return -1;
324         }
325
326         smb_fname_tmp->base_name = cappath;
327
328         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
329
330         TALLOC_FREE(smb_fname_tmp);
331         return ret;
332 }
333
334
335 static int cap_symlink(vfs_handle_struct *handle, const char *oldpath,
336                        const char *newpath)
337 {
338         char *capold = capencode(talloc_tos(), oldpath);
339         char *capnew = capencode(talloc_tos(), newpath);
340
341         if (!capold || !capnew) {
342                 errno = ENOMEM;
343                 return -1;
344         }
345         return SMB_VFS_NEXT_SYMLINK(handle, capold, capnew);
346 }
347
348 static int cap_readlink(vfs_handle_struct *handle, const char *path,
349                         char *buf, size_t bufsiz)
350 {
351         char *cappath = capencode(talloc_tos(), path);
352
353         if (!cappath) {
354                 errno = ENOMEM;
355                 return -1;
356         }
357         return SMB_VFS_NEXT_READLINK(handle, cappath, buf, bufsiz);
358 }
359
360 static int cap_link(vfs_handle_struct *handle, const char *oldpath, const char *newpath)
361 {
362         char *capold = capencode(talloc_tos(), oldpath);
363         char *capnew = capencode(talloc_tos(), newpath);
364
365         if (!capold || !capnew) {
366                 errno = ENOMEM;
367                 return -1;
368         }
369         return SMB_VFS_NEXT_LINK(handle, capold, capnew);
370 }
371
372 static int cap_mknod(vfs_handle_struct *handle, const char *path, mode_t mode, SMB_DEV_T dev)
373 {
374         char *cappath = capencode(talloc_tos(), path);
375
376         if (!cappath) {
377                 errno = ENOMEM;
378                 return -1;
379         }
380         return SMB_VFS_NEXT_MKNOD(handle, cappath, mode, dev);
381 }
382
383 static char *cap_realpath(vfs_handle_struct *handle, const char *path)
384 {
385         /* monyo need capencode'ed and capdecode'ed? */
386         char *cappath = capencode(talloc_tos(), path);
387
388         if (!cappath) {
389                 errno = ENOMEM;
390                 return NULL;
391         }
392         return SMB_VFS_NEXT_REALPATH(handle, cappath);
393 }
394
395 static int cap_chmod_acl(vfs_handle_struct *handle, const char *path, mode_t mode)
396 {
397         char *cappath = capencode(talloc_tos(), path);
398
399         /* If the underlying VFS doesn't have ACL support... */
400         if (!cappath) {
401                 errno = ENOMEM;
402                 return -1;
403         }
404         return SMB_VFS_NEXT_CHMOD_ACL(handle, cappath, mode);
405 }
406
407 static SMB_ACL_T cap_sys_acl_get_file(vfs_handle_struct *handle,
408                                       const char *path, SMB_ACL_TYPE_T type,
409                                       TALLOC_CTX *mem_ctx)
410 {
411         char *cappath = capencode(talloc_tos(), path);
412
413         if (!cappath) {
414                 errno = ENOMEM;
415                 return (SMB_ACL_T)NULL;
416         }
417         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, cappath, type, mem_ctx);
418 }
419
420 static int cap_sys_acl_set_file(vfs_handle_struct *handle, const char *path, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl)
421 {
422         char *cappath = capencode(talloc_tos(), path);
423
424         if (!cappath) {
425                 errno = ENOMEM;
426                 return -1;
427         }
428         return SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, cappath, acltype, theacl);
429 }
430
431 static int cap_sys_acl_delete_def_file(vfs_handle_struct *handle, const char *path)
432 {
433         char *cappath = capencode(talloc_tos(), path);
434
435         if (!cappath) {
436                 errno = ENOMEM;
437                 return -1;
438         }
439         return SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, cappath);
440 }
441
442 static ssize_t cap_getxattr(vfs_handle_struct *handle, const char *path, const char *name, void *value, size_t size)
443 {
444         char *cappath = capencode(talloc_tos(), path);
445         char *capname = capencode(talloc_tos(), name);
446
447         if (!cappath || !capname) {
448                 errno = ENOMEM;
449                 return -1;
450         }
451         return SMB_VFS_NEXT_GETXATTR(handle, cappath, capname, value, size);
452 }
453
454 static ssize_t cap_fgetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, void *value, size_t size)
455 {
456         char *cappath = capencode(talloc_tos(), path);
457
458         if (!cappath) {
459                 errno = ENOMEM;
460                 return -1;
461         }
462         return SMB_VFS_NEXT_FGETXATTR(handle, fsp, cappath, value, size);
463 }
464
465 static ssize_t cap_listxattr(vfs_handle_struct *handle, const char *path, char *list, size_t size)
466 {
467         char *cappath = capencode(talloc_tos(), path);
468
469         if (!cappath) {
470                 errno = ENOMEM;
471                 return -1;
472         }
473         return SMB_VFS_NEXT_LISTXATTR(handle, cappath, list, size);
474 }
475
476 static int cap_removexattr(vfs_handle_struct *handle, const char *path, const char *name)
477 {
478         char *cappath = capencode(talloc_tos(), path);
479         char *capname = capencode(talloc_tos(), name);
480
481         if (!cappath || !capname) {
482                 errno = ENOMEM;
483                 return -1;
484         }
485         return SMB_VFS_NEXT_REMOVEXATTR(handle, cappath, capname);
486 }
487
488 static int cap_fremovexattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path)
489 {
490         char *cappath = capencode(talloc_tos(), path);
491
492         if (!cappath) {
493                 errno = ENOMEM;
494                 return -1;
495         }
496         return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, cappath);
497 }
498
499 static int cap_setxattr(vfs_handle_struct *handle, const char *path, const char *name, const void *value, size_t size, int flags)
500 {
501         char *cappath = capencode(talloc_tos(), path);
502         char *capname = capencode(talloc_tos(), name);
503
504         if (!cappath || !capname) {
505                 errno = ENOMEM;
506                 return -1;
507         }
508         return SMB_VFS_NEXT_SETXATTR(handle, cappath, capname, value, size, flags);
509 }
510
511 static int cap_fsetxattr(vfs_handle_struct *handle, struct files_struct *fsp, const char *path, const void *value, size_t size, int flags)
512 {
513         char *cappath = capencode(talloc_tos(), path);
514
515         if (!cappath) {
516                 errno = ENOMEM;
517                 return -1;
518         }
519         return SMB_VFS_NEXT_FSETXATTR(handle, fsp, cappath, value, size, flags);
520 }
521
522 static struct vfs_fn_pointers vfs_cap_fns = {
523         .disk_free_fn = cap_disk_free,
524         .opendir_fn = cap_opendir,
525         .readdir_fn = cap_readdir,
526         .mkdir_fn = cap_mkdir,
527         .rmdir_fn = cap_rmdir,
528         .open_fn = cap_open,
529         .rename_fn = cap_rename,
530         .stat_fn = cap_stat,
531         .lstat_fn = cap_lstat,
532         .unlink_fn = cap_unlink,
533         .chmod_fn = cap_chmod,
534         .chown_fn = cap_chown,
535         .lchown_fn = cap_lchown,
536         .chdir_fn = cap_chdir,
537         .ntimes_fn = cap_ntimes,
538         .symlink_fn = cap_symlink,
539         .readlink_fn = cap_readlink,
540         .link_fn = cap_link,
541         .mknod_fn = cap_mknod,
542         .realpath_fn = cap_realpath,
543         .chmod_acl_fn = cap_chmod_acl,
544         .sys_acl_get_file_fn = cap_sys_acl_get_file,
545         .sys_acl_set_file_fn = cap_sys_acl_set_file,
546         .sys_acl_delete_def_file_fn = cap_sys_acl_delete_def_file,
547         .getxattr_fn = cap_getxattr,
548         .fgetxattr_fn = cap_fgetxattr,
549         .listxattr_fn = cap_listxattr,
550         .removexattr_fn = cap_removexattr,
551         .fremovexattr_fn = cap_fremovexattr,
552         .setxattr_fn = cap_setxattr,
553         .fsetxattr_fn = cap_fsetxattr
554 };
555
556 NTSTATUS vfs_cap_init(void);
557 NTSTATUS vfs_cap_init(void)
558 {
559         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "cap",
560                                 &vfs_cap_fns);
561 }
562
563 /* For CAP functions */
564 #define hex_tag ':'
565 #define hex2bin(c)              hex2bin_table[(unsigned char)(c)]
566 #define bin2hex(c)              bin2hex_table[(unsigned char)(c)]
567 #define is_hex(s)               ((s)[0] == hex_tag)
568
569 static unsigned char hex2bin_table[256] = {
570 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 */
571 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 */
572 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 */
573 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 */
574 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x40 */
575 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
576 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 */
577 0000, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0000, /* 0x60 */
578 0000, 0000, 0000, 0000, 0000, 0000, 0000, 0000,
579 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 */
580 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 */
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 */
582 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 */
583 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 */
584 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 */
585 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 */
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 */
587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xf0 */
588 };
589 static unsigned char bin2hex_table[256] = "0123456789abcdef";
590
591 /*******************************************************************
592   original code -> ":xx"  - CAP format
593 ********************************************************************/
594
595 static char *capencode(TALLOC_CTX *ctx, const char *from)
596 {
597         char *out = NULL;
598         const char *p1;
599         char *to = NULL;
600         size_t len = 0;
601
602         for (p1 = from; *p1; p1++) {
603                 if ((unsigned char)*p1 >= 0x80) {
604                         len += 3;
605                 } else {
606                         len++;
607                 }
608         }
609         len++;
610
611         to = talloc_array(ctx, char, len);
612         if (!to) {
613                 return NULL;
614         }
615
616         for (out = to; *from;) {
617                 /* buffer husoku error */
618                 if ((unsigned char)*from >= 0x80) {
619                         *out++ = hex_tag;
620                         *out++ = bin2hex (((*from)>>4)&0x0f);
621                         *out++ = bin2hex ((*from)&0x0f);
622                         from++;
623                 } else {
624                         *out++ = *from++;
625                 }
626         }
627         *out = '\0';
628         return to;
629 }
630
631 /*******************************************************************
632   CAP -> original code
633 ********************************************************************/
634 /* ":xx" -> a byte */
635
636 static char *capdecode(TALLOC_CTX *ctx, const char *from)
637 {
638         const char *p1;
639         char *out = NULL;
640         char *to = NULL;
641         size_t len = 0;
642
643         for (p1 = from; *p1; len++) {
644                 if (is_hex(p1)) {
645                         p1 += 3;
646                 } else {
647                         p1++;
648                 }
649         }
650         len++;
651
652         to = talloc_array(ctx, char, len);
653         if (!to) {
654                 return NULL;
655         }
656
657         for (out = to; *from;) {
658                 if (is_hex(from)) {
659                         *out++ = (hex2bin(from[1])<<4) | (hex2bin(from[2]));
660                         from += 3;
661                 } else {
662                         *out++ = *from++;
663                 }
664         }
665         *out = '\0';
666         return to;
667 }