vfs_catia: use translate direction enum instead of int
[kamenim/samba-autobuild/.git] / source3 / modules / vfs_catia.c
1 /*
2  * Catia VFS module
3  *
4  * Implement a fixed mapping of forbidden NT characters in filenames that are
5  * used a lot by the CAD package Catia.
6  *
7  * Yes, this a BAD BAD UGLY INCOMPLETE hack, but it helps quite some people
8  * out there. Catia V4 on AIX uses characters like "<*$ a *lot*, all forbidden
9  * under Windows...
10  *
11  * Copyright (C) Volker Lendecke, 2005
12  * Copyright (C) Aravind Srinivasan, 2009
13  * Copyright (C) Guenter Kukkukk, 2013
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, see <http://www.gnu.org/licenses/>.
27  */
28
29
30 #include "includes.h"
31 #include "smbd/smbd.h"
32
33 static int vfs_catia_debug_level = DBGC_VFS;
34
35 #undef DBGC_CLASS
36 #define DBGC_CLASS vfs_catia_debug_level
37
38 #define GLOBAL_SNUM     0xFFFFFFF
39 #define MAP_SIZE        0xFF
40 #define MAP_NUM         0x101 /* max unicode charval / MAP_SIZE */
41 #define T_OFFSET(_v_)   ((_v_ % MAP_SIZE))
42 #define T_START(_v_)    (((_v_ / MAP_SIZE) * MAP_SIZE))
43 #define T_PICK(_v_)     ((_v_ / MAP_SIZE))
44
45 struct char_mappings {
46         smb_ucs2_t entry[MAP_SIZE][2];
47 };
48
49 struct share_mapping_entry {
50         int snum;
51         struct share_mapping_entry *next;
52         struct char_mappings **mappings;
53 };
54
55 struct share_mapping_entry *srt_head = NULL;
56
57 static bool build_table(struct char_mappings **cmaps, int value)
58 {
59         int i;
60         int start = T_START(value);
61
62         (*cmaps) = talloc_zero(NULL, struct char_mappings);
63
64         if (!*cmaps)
65                 return False;
66
67         for (i = 0; i < MAP_SIZE;i++) {
68                 (*cmaps)->entry[i][vfs_translate_to_unix] = start + i;
69                 (*cmaps)->entry[i][vfs_translate_to_windows] = start + i;
70         }
71
72         return True;
73 }
74
75 static void set_tables(struct char_mappings **cmaps,
76                        long unix_map,
77                        long windows_map)
78 {
79         int i;
80
81         /* set unix -> windows */
82         i = T_OFFSET(unix_map);
83         cmaps[T_PICK(unix_map)]->entry[i][vfs_translate_to_windows] = windows_map;
84
85         /* set windows -> unix */
86         i = T_OFFSET(windows_map);
87         cmaps[T_PICK(windows_map)]->entry[i][vfs_translate_to_unix] = unix_map;
88 }
89
90 static bool build_ranges(struct char_mappings **cmaps,
91                          long unix_map,
92                          long windows_map)
93 {
94
95         if (!cmaps[T_PICK(unix_map)]) {
96                 if (!build_table(&cmaps[T_PICK(unix_map)], unix_map))
97                         return False;
98         }
99
100         if (!cmaps[T_PICK(windows_map)]) {
101                 if (!build_table(&cmaps[T_PICK(windows_map)], windows_map))
102                         return False;
103         }
104
105         set_tables(cmaps, unix_map, windows_map);
106
107         return True;
108 }
109
110 static struct share_mapping_entry *get_srt(connection_struct *conn,
111                                            struct share_mapping_entry **global)
112 {
113         struct share_mapping_entry *share;
114
115         for (share = srt_head; share != NULL; share = share->next) {
116                 if (share->snum == GLOBAL_SNUM)
117                         (*global) = share;
118
119                 if (share->snum == SNUM(conn))
120                         return share;
121         }
122
123         return share;
124 }
125
126 static struct share_mapping_entry *add_srt(int snum, const char **mappings)
127 {
128
129         char *tmp;
130         fstring mapping;
131         int i;
132         long unix_map, windows_map;
133         struct share_mapping_entry *ret = NULL;
134
135         ret = (struct share_mapping_entry *)
136                 TALLOC_ZERO(NULL, sizeof(struct share_mapping_entry) +
137                 (mappings ? (MAP_NUM * sizeof(struct char_mappings *)) : 0));
138
139         if (!ret)
140                 return ret;
141
142         ret->snum = snum;
143
144         if (mappings) {
145                 ret->mappings = (struct char_mappings**) ((unsigned char*) ret +
146                     sizeof(struct share_mapping_entry));
147                 memset(ret->mappings, 0,
148                     MAP_NUM * sizeof(struct char_mappings *));
149         } else {
150                 ret->mappings = NULL;
151                 return ret;
152         }
153
154         /*
155          * catia mappings are of the form :
156          * UNIX char (in 0xnn hex) : WINDOWS char (in 0xnn hex)
157          *
158          * multiple mappings are comma separated in smb.conf
159          */
160         for (i=0;mappings[i];i++) {
161                 fstrcpy(mapping, mappings[i]);
162                 unix_map = strtol(mapping, &tmp, 16);
163                 if (unix_map == 0 && errno == EINVAL) {
164                         DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
165                         continue;
166                 }
167                 windows_map = strtol(++tmp, NULL, 16);
168                 if (windows_map == 0 && errno == EINVAL) {
169                         DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
170                         continue;
171                 }
172
173                 if (!build_ranges(ret->mappings, unix_map, windows_map)) {
174                         DEBUG(0, ("TABLE ERROR - CATIA MAPPINGS - %s\n", mapping));
175                         continue;
176                 }
177         }
178
179         ret->next = srt_head;
180         srt_head = ret;
181
182         return ret;
183 }
184
185 static bool init_mappings(connection_struct *conn,
186                           struct share_mapping_entry **selected_out)
187 {
188         const char **mappings = NULL;
189         struct share_mapping_entry *share_level = NULL;
190         struct share_mapping_entry *global = NULL;
191
192         /* check srt cache */
193         share_level = get_srt(conn, &global);
194         if (share_level) {
195                 *selected_out = share_level;
196                 return (share_level->mappings != NULL);
197         }
198
199         /* see if we have a global setting */
200         if (!global) {
201                 /* global setting */
202                 mappings = lp_parm_string_list(-1, "catia", "mappings", NULL);
203                 global = add_srt(GLOBAL_SNUM, mappings);
204         }
205
206         /* no global setting - what about share level ? */
207         mappings = lp_parm_string_list(SNUM(conn), "catia", "mappings", NULL);
208         share_level = add_srt(SNUM(conn), mappings);
209
210         if (share_level->mappings) {
211                 (*selected_out) = share_level;
212                 return True;
213         } else if (global->mappings) {
214                 share_level->mappings = global->mappings;
215                 (*selected_out) = share_level;
216                 return True;
217         }
218
219         return False;
220 }
221
222 static NTSTATUS catia_string_replace_allocate(connection_struct *conn,
223                                               const char *name_in,
224                                               char **mapped_name,
225                                         enum vfs_translate_direction direction)
226 {
227         static smb_ucs2_t *tmpbuf = NULL;
228         smb_ucs2_t *ptr;
229         struct share_mapping_entry *selected;
230         struct char_mappings *map = NULL;
231         size_t converted_size;
232         TALLOC_CTX *ctx = talloc_tos();
233
234         if (!init_mappings(conn, &selected)) {
235                 /* No mappings found. Just use the old name */
236                 *mapped_name = talloc_strdup(NULL, name_in);
237                 if (!*mapped_name) {
238                         errno = ENOMEM;
239                         return NT_STATUS_NO_MEMORY;
240                 }
241                 return NT_STATUS_OK;
242         }
243
244         if ((push_ucs2_talloc(ctx, &tmpbuf, name_in,
245                               &converted_size)) == false) {
246                 return map_nt_error_from_unix(errno);
247         }
248         ptr = tmpbuf;
249         for(;*ptr;ptr++) {
250                 if (*ptr == 0)
251                         break;
252                 map = selected->mappings[T_PICK((*ptr))];
253
254                 /* nothing to do */
255                 if (!map)
256                         continue;
257
258                 *ptr = map->entry[T_OFFSET((*ptr))][direction];
259         }
260
261         if ((pull_ucs2_talloc(ctx, mapped_name, tmpbuf,
262                               &converted_size)) == false) {
263                 TALLOC_FREE(tmpbuf);
264                 return map_nt_error_from_unix(errno);
265         }
266         TALLOC_FREE(tmpbuf);
267         return NT_STATUS_OK;
268 }
269
270 static DIR *catia_opendir(vfs_handle_struct *handle,
271                                      const char *fname,
272                                      const char *mask,
273                                      uint32 attr)
274 {
275         char *name_mapped = NULL;
276         NTSTATUS status;
277         DIR *ret;
278
279         status = catia_string_replace_allocate(handle->conn, fname,
280                                         &name_mapped, vfs_translate_to_unix);
281         if (!NT_STATUS_IS_OK(status)) {
282                 errno = map_errno_from_nt_status(status);
283                 return NULL;
284         }
285
286         ret = SMB_VFS_NEXT_OPENDIR(handle, name_mapped, mask, attr);
287         TALLOC_FREE(name_mapped);
288
289         return ret;
290 }
291
292 /*
293  * TRANSLATE_NAME call which converts the given name to
294  * "WINDOWS displayable" name
295  */
296 static NTSTATUS catia_translate_name(struct vfs_handle_struct *handle,
297                                      const char *orig_name,
298                                      enum vfs_translate_direction direction,
299                                      TALLOC_CTX *mem_ctx,
300                                      char **pmapped_name)
301 {
302         char *name = NULL;
303         char *mapped_name;
304         NTSTATUS status, ret;
305
306         /*
307          * Copy the supplied name and free the memory for mapped_name,
308          * already allocated by the caller.
309          * We will be allocating new memory for mapped_name in
310          * catia_string_replace_allocate
311          */
312         name = talloc_strdup(talloc_tos(), orig_name);
313         if (!name) {
314                 errno = ENOMEM;
315                 return NT_STATUS_NO_MEMORY;
316         }
317         status = catia_string_replace_allocate(handle->conn, name,
318                         &mapped_name, direction);
319
320         TALLOC_FREE(name);
321         if (!NT_STATUS_IS_OK(status)) {
322                 return status;
323         }
324
325         ret = SMB_VFS_NEXT_TRANSLATE_NAME(handle, mapped_name, direction,
326                                           mem_ctx, pmapped_name);
327
328         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
329                 *pmapped_name = talloc_move(mem_ctx, &mapped_name);
330                 /* we need to return the former translation result here */
331                 ret = status;
332         } else {
333                 TALLOC_FREE(mapped_name);
334         }
335
336         return ret;
337 }
338
339 static int catia_open(vfs_handle_struct *handle,
340                       struct smb_filename *smb_fname,
341                       files_struct *fsp,
342                       int flags,
343                       mode_t mode)
344 {
345         char *name_mapped = NULL;
346         char *tmp_base_name;
347         int ret;
348         NTSTATUS status;
349
350         tmp_base_name = smb_fname->base_name;
351         status = catia_string_replace_allocate(handle->conn,
352                                         smb_fname->base_name,
353                                         &name_mapped, vfs_translate_to_unix);
354         if (!NT_STATUS_IS_OK(status)) {
355                 errno = map_errno_from_nt_status(status);
356                 return -1;
357         }
358
359         smb_fname->base_name = name_mapped;
360         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
361         smb_fname->base_name = tmp_base_name;
362         TALLOC_FREE(name_mapped);
363
364         return ret;
365 }
366
367 static int catia_rename(vfs_handle_struct *handle,
368                         const struct smb_filename *smb_fname_src,
369                         const struct smb_filename *smb_fname_dst)
370 {
371         TALLOC_CTX *ctx = talloc_tos();
372         struct smb_filename *smb_fname_src_tmp = NULL;
373         struct smb_filename *smb_fname_dst_tmp = NULL;
374         char *src_name_mapped = NULL;
375         char *dst_name_mapped = NULL;
376         NTSTATUS status;
377         int ret = -1;
378
379         status = catia_string_replace_allocate(handle->conn,
380                                 smb_fname_src->base_name,
381                                 &src_name_mapped, vfs_translate_to_unix);
382         if (!NT_STATUS_IS_OK(status)) {
383                 errno = map_errno_from_nt_status(status);
384                 return -1;
385         }
386
387         status = catia_string_replace_allocate(handle->conn,
388                                 smb_fname_dst->base_name,
389                                 &dst_name_mapped, vfs_translate_to_unix);
390         if (!NT_STATUS_IS_OK(status)) {
391                 errno = map_errno_from_nt_status(status);
392                 return -1;
393         }
394
395         /* Setup temporary smb_filename structs. */
396         smb_fname_src_tmp = cp_smb_filename(ctx, smb_fname_src);
397         if (smb_fname_src_tmp == NULL) {
398                 errno = ENOMEM;
399                 goto out;
400         }
401
402         smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
403         if (smb_fname_dst_tmp == NULL) {
404                 errno = ENOMEM;
405                 goto out;
406         }
407
408         smb_fname_src_tmp->base_name = src_name_mapped;
409         smb_fname_dst_tmp->base_name = dst_name_mapped; 
410         DEBUG(10, ("converted old name: %s\n",
411                                 smb_fname_str_dbg(smb_fname_src_tmp)));
412         DEBUG(10, ("converted new name: %s\n",
413                                 smb_fname_str_dbg(smb_fname_dst_tmp)));
414
415         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
416                         smb_fname_dst_tmp);
417 out:
418         TALLOC_FREE(src_name_mapped);
419         TALLOC_FREE(dst_name_mapped);
420         TALLOC_FREE(smb_fname_src_tmp);
421         TALLOC_FREE(smb_fname_dst_tmp);
422         return ret;
423 }
424
425 static int catia_stat(vfs_handle_struct *handle,
426                       struct smb_filename *smb_fname)
427 {
428         char *name = NULL;
429         char *tmp_base_name;
430         int ret;
431         NTSTATUS status;
432
433         status = catia_string_replace_allocate(handle->conn,
434                                 smb_fname->base_name,
435                                 &name, vfs_translate_to_unix);
436         if (!NT_STATUS_IS_OK(status)) {
437                 errno = map_errno_from_nt_status(status);
438                 return -1;
439         }
440
441         tmp_base_name = smb_fname->base_name;
442         smb_fname->base_name = name;
443
444         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
445         smb_fname->base_name = tmp_base_name;
446
447         TALLOC_FREE(name);
448         return ret;
449 }
450
451 static int catia_lstat(vfs_handle_struct *handle,
452                        struct smb_filename *smb_fname)
453 {
454         char *name = NULL;
455         char *tmp_base_name;
456         int ret;
457         NTSTATUS status;
458
459         status = catia_string_replace_allocate(handle->conn,
460                                 smb_fname->base_name,
461                                 &name, vfs_translate_to_unix);
462         if (!NT_STATUS_IS_OK(status)) {
463                 errno = map_errno_from_nt_status(status);
464                 return -1;
465         }
466
467         tmp_base_name = smb_fname->base_name;
468         smb_fname->base_name = name;
469
470         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
471         smb_fname->base_name = tmp_base_name;
472         TALLOC_FREE(name);
473
474         return ret;
475 }
476
477 static int catia_unlink(vfs_handle_struct *handle,
478                         const struct smb_filename *smb_fname)
479 {
480         struct smb_filename *smb_fname_tmp = NULL;
481         char *name = NULL;
482         NTSTATUS status;
483         int ret;
484
485         status = catia_string_replace_allocate(handle->conn,
486                                         smb_fname->base_name,
487                                         &name, vfs_translate_to_unix);
488         if (!NT_STATUS_IS_OK(status)) {
489                 errno = map_errno_from_nt_status(status);
490                 return -1;
491         }
492
493         /* Setup temporary smb_filename structs. */
494         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
495         if (smb_fname_tmp == NULL) {
496                 errno = ENOMEM;
497                 return -1;
498         }
499
500         smb_fname_tmp->base_name = name;
501         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
502         TALLOC_FREE(smb_fname_tmp);
503         TALLOC_FREE(name);
504
505         return ret;
506 }
507
508 static int catia_chown(vfs_handle_struct *handle,
509                        const char *path,
510                        uid_t uid,
511                        gid_t gid)
512 {
513         char *name = NULL;
514         NTSTATUS status;
515         int ret;
516
517         status = catia_string_replace_allocate(handle->conn, path,
518                                         &name, vfs_translate_to_unix);
519         if (!NT_STATUS_IS_OK(status)) {
520                 errno = map_errno_from_nt_status(status);
521                 return -1;
522         }
523
524         ret = SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
525         TALLOC_FREE(name);
526
527         return ret;
528 }
529
530 static int catia_lchown(vfs_handle_struct *handle,
531                         const char *path,
532                         uid_t uid,
533                         gid_t gid)
534 {
535         char *name = NULL;
536         NTSTATUS status;
537         int ret;
538
539         status = catia_string_replace_allocate(handle->conn, path,
540                                         &name, vfs_translate_to_unix);
541         if (!NT_STATUS_IS_OK(status)) {
542                 errno = map_errno_from_nt_status(status);
543                 return -1;
544         }
545
546         ret = SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
547         TALLOC_FREE(name);
548
549         return ret;
550 }
551
552 static int catia_rmdir(vfs_handle_struct *handle,
553                        const char *path)
554 {
555         char *name = NULL;
556         NTSTATUS status;
557         int ret;
558
559         status = catia_string_replace_allocate(handle->conn, path,
560                                         &name, vfs_translate_to_unix);
561         if (!NT_STATUS_IS_OK(status)) {
562                 errno = map_errno_from_nt_status(status);
563                 return -1;
564         }
565
566         ret = SMB_VFS_NEXT_RMDIR(handle, name);
567         TALLOC_FREE(name);
568
569         return ret;
570 }
571
572 static int catia_mkdir(vfs_handle_struct *handle,
573                        const char *path,
574                        mode_t mode)
575 {
576         char *name = NULL;
577         NTSTATUS status;
578         int ret;
579
580         status = catia_string_replace_allocate(handle->conn, path,
581                                          &name, vfs_translate_to_unix);
582         if (!NT_STATUS_IS_OK(status)) {
583                 errno = map_errno_from_nt_status(status);
584                 return -1;
585         }
586
587         ret = SMB_VFS_NEXT_MKDIR(handle, name, mode);
588         TALLOC_FREE(name);
589
590         return ret;
591 }
592
593 static int catia_chdir(vfs_handle_struct *handle,
594                        const char *path)
595 {
596         char *name = NULL;
597         NTSTATUS status;
598         int ret;
599
600         status = catia_string_replace_allocate(handle->conn, path,
601                                         &name, vfs_translate_to_unix);
602         if (!NT_STATUS_IS_OK(status)) {
603                 errno = map_errno_from_nt_status(status);
604                 return -1;
605         }
606
607         ret = SMB_VFS_NEXT_CHDIR(handle, name);
608         TALLOC_FREE(name);
609
610         return ret;
611 }
612
613 static int catia_ntimes(vfs_handle_struct *handle,
614                         const struct smb_filename *smb_fname,
615                         struct smb_file_time *ft)
616 {
617         struct smb_filename *smb_fname_tmp = NULL;
618         char *name = NULL;
619         NTSTATUS status;
620         int ret;
621
622         status = catia_string_replace_allocate(handle->conn,
623                                 smb_fname->base_name,
624                                 &name, vfs_translate_to_unix);
625         if (!NT_STATUS_IS_OK(status)) {
626                 errno = map_errno_from_nt_status(status);
627                 return -1;
628         }
629
630         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
631         if (smb_fname_tmp == NULL) {
632                 errno = ENOMEM;
633                 return -1;
634         }
635
636         smb_fname_tmp->base_name = name;
637         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
638         TALLOC_FREE(name);
639         TALLOC_FREE(smb_fname_tmp);
640
641         return ret;
642 }
643
644 static char *
645 catia_realpath(vfs_handle_struct *handle, const char *path)
646 {
647         char *mapped_name = NULL;
648         NTSTATUS status;
649         char *ret = NULL;
650
651         status = catia_string_replace_allocate(handle->conn, path,
652                                         &mapped_name, vfs_translate_to_unix);
653         if (!NT_STATUS_IS_OK(status)) {
654                 errno = map_errno_from_nt_status(status);
655                 return NULL;
656         }
657
658         ret = SMB_VFS_NEXT_REALPATH(handle, mapped_name);
659         TALLOC_FREE(mapped_name);
660
661         return ret;
662 }
663
664 static int catia_chflags(struct vfs_handle_struct *handle,
665                          const char *path, unsigned int flags)
666 {
667         char *mapped_name = NULL;
668         NTSTATUS status;
669         int ret;
670
671         status = catia_string_replace_allocate(handle->conn, path,
672                                         &mapped_name, vfs_translate_to_unix);
673         if (!NT_STATUS_IS_OK(status)) {
674                 errno = map_errno_from_nt_status(status);
675                 return -1;
676         }
677
678         ret = SMB_VFS_NEXT_CHFLAGS(handle, mapped_name, flags);
679         TALLOC_FREE(mapped_name);
680
681         return ret;
682 }
683
684 static NTSTATUS
685 catia_streaminfo(struct vfs_handle_struct *handle,
686                  struct files_struct *fsp,
687                  const char *path,
688                  TALLOC_CTX *mem_ctx,
689                  unsigned int *num_streams,
690                  struct stream_struct **streams)
691 {
692         char *mapped_name = NULL;
693         NTSTATUS status;
694
695         status = catia_string_replace_allocate(handle->conn, path,
696                                         &mapped_name, vfs_translate_to_unix);
697         if (!NT_STATUS_IS_OK(status)) {
698                 errno = map_errno_from_nt_status(status);
699                 return status;
700         }
701
702         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, mapped_name,
703                                          mem_ctx, num_streams,streams);
704         TALLOC_FREE(mapped_name);
705
706         return status;
707 }
708
709 static NTSTATUS
710 catia_get_nt_acl(struct vfs_handle_struct *handle,
711                  const char *path,
712                  uint32 security_info,
713                  TALLOC_CTX *mem_ctx,
714                  struct security_descriptor **ppdesc)
715 {
716         char *mapped_name = NULL;
717         NTSTATUS status;
718
719         status = catia_string_replace_allocate(handle->conn,
720                                 path, &mapped_name, vfs_translate_to_unix);
721         if (!NT_STATUS_IS_OK(status)) {
722                 errno = map_errno_from_nt_status(status);
723                 return status;
724         }
725         status = SMB_VFS_NEXT_GET_NT_ACL(handle, mapped_name,
726                                          security_info, mem_ctx, ppdesc);
727         TALLOC_FREE(mapped_name);
728
729         return status;
730 }
731
732 static int
733 catia_chmod_acl(vfs_handle_struct *handle,
734                 const char *path,
735                 mode_t mode)
736 {
737         char *mapped_name = NULL;
738         NTSTATUS status;
739         int ret;
740
741         status = catia_string_replace_allocate(handle->conn,
742                                 path, &mapped_name, vfs_translate_to_unix);
743         if (!NT_STATUS_IS_OK(status)) {
744                 errno = map_errno_from_nt_status(status);
745                 return -1;
746         }
747
748         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, mapped_name, mode);
749         TALLOC_FREE(mapped_name);
750         return ret;
751 }
752
753 static SMB_ACL_T
754 catia_sys_acl_get_file(vfs_handle_struct *handle,
755                        const char *path,
756                        SMB_ACL_TYPE_T type,
757                        TALLOC_CTX *mem_ctx)
758 {
759         char *mapped_name = NULL;
760         NTSTATUS status;
761         SMB_ACL_T ret;
762
763         status = catia_string_replace_allocate(handle->conn,
764                                 path, &mapped_name, vfs_translate_to_unix);
765         if (!NT_STATUS_IS_OK(status)) {
766                 errno = map_errno_from_nt_status(status);
767                 return NULL;
768         }
769
770         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, mapped_name, type, mem_ctx);
771         TALLOC_FREE(mapped_name);
772
773         return ret;
774 }
775
776 static int
777 catia_sys_acl_set_file(vfs_handle_struct *handle,
778                        const char *path,
779                        SMB_ACL_TYPE_T type,
780                        SMB_ACL_T theacl)
781 {
782         char *mapped_name = NULL;
783         NTSTATUS status;
784         int ret;
785
786         status = catia_string_replace_allocate(handle->conn,
787                                 path, &mapped_name, vfs_translate_to_unix);
788         if (!NT_STATUS_IS_OK(status)) {
789                 errno = map_errno_from_nt_status(status);
790                 return -1;
791         }
792
793         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, mapped_name, type, theacl);
794         TALLOC_FREE(mapped_name);
795
796         return ret;
797 }
798
799 static int
800 catia_sys_acl_delete_def_file(vfs_handle_struct *handle,
801                               const char *path)
802 {
803         char *mapped_name = NULL;
804         NTSTATUS status;
805         int ret;
806
807         status = catia_string_replace_allocate(handle->conn,
808                                 path, &mapped_name, vfs_translate_to_unix);
809         if (!NT_STATUS_IS_OK(status)) {
810                 errno = map_errno_from_nt_status(status);
811                 return -1;
812         }
813
814         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, mapped_name);
815         TALLOC_FREE(mapped_name);
816
817         return ret;
818 }
819
820 static ssize_t
821 catia_getxattr(vfs_handle_struct *handle, const char *path,
822                const char *name, void *value, size_t size)
823 {
824         char *mapped_name = NULL;
825         NTSTATUS status;
826         ssize_t ret;
827
828         status = catia_string_replace_allocate(handle->conn,
829                                 name, &mapped_name, vfs_translate_to_unix);
830         if (!NT_STATUS_IS_OK(status)) {
831                 errno = map_errno_from_nt_status(status);
832                 return -1;
833         }
834
835
836         ret = SMB_VFS_NEXT_GETXATTR(handle, path, mapped_name, value, size);
837         TALLOC_FREE(mapped_name);
838
839         return ret;
840 }
841
842 static ssize_t
843 catia_listxattr(vfs_handle_struct *handle, const char *path,
844                 char *list, size_t size)
845 {
846         char *mapped_name = NULL;
847         NTSTATUS status;
848         ssize_t ret;
849
850         status = catia_string_replace_allocate(handle->conn,
851                                 path, &mapped_name, vfs_translate_to_unix);
852         if (!NT_STATUS_IS_OK(status)) {
853                 errno = map_errno_from_nt_status(status);
854                 return -1;
855         }
856
857
858         ret = SMB_VFS_NEXT_LISTXATTR(handle, mapped_name, list, size);
859         TALLOC_FREE(mapped_name);
860
861         return ret;
862 }
863
864 static int
865 catia_removexattr(vfs_handle_struct *handle, const char *path,
866                   const char *name)
867 {
868         char *mapped_name = NULL;
869         NTSTATUS status;
870         ssize_t ret;
871
872         status = catia_string_replace_allocate(handle->conn,
873                                 name, &mapped_name, vfs_translate_to_unix);
874         if (!NT_STATUS_IS_OK(status)) {
875                 errno = map_errno_from_nt_status(status);
876                 return -1;
877         }
878
879
880         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, path, mapped_name);
881         TALLOC_FREE(mapped_name);
882
883         return ret;
884 }
885
886 static int
887 catia_setxattr(vfs_handle_struct *handle, const char *path,
888                const char *name, const void *value, size_t size,
889                int flags)
890 {
891         char *mapped_name = NULL;
892         NTSTATUS status;
893         ssize_t ret;
894
895         status = catia_string_replace_allocate(handle->conn,
896                                 name, &mapped_name, vfs_translate_to_unix);
897         if (!NT_STATUS_IS_OK(status)) {
898                 errno = map_errno_from_nt_status(status);
899                 return -1;
900         }
901
902
903         ret = SMB_VFS_NEXT_SETXATTR(handle, path, mapped_name, value, size, flags);
904         TALLOC_FREE(mapped_name);
905
906         return ret;
907 }
908
909 static struct vfs_fn_pointers vfs_catia_fns = {
910         .mkdir_fn = catia_mkdir,
911         .rmdir_fn = catia_rmdir,
912         .opendir_fn = catia_opendir,
913         .open_fn = catia_open,
914         .rename_fn = catia_rename,
915         .stat_fn = catia_stat,
916         .lstat_fn = catia_lstat,
917         .unlink_fn = catia_unlink,
918         .chown_fn = catia_chown,
919         .lchown_fn = catia_lchown,
920         .chdir_fn = catia_chdir,
921         .ntimes_fn = catia_ntimes,
922         .realpath_fn = catia_realpath,
923         .chflags_fn = catia_chflags,
924         .streaminfo_fn = catia_streaminfo,
925         .translate_name_fn = catia_translate_name,
926         .get_nt_acl_fn = catia_get_nt_acl,
927         .chmod_acl_fn = catia_chmod_acl,
928         .sys_acl_get_file_fn = catia_sys_acl_get_file,
929         .sys_acl_set_file_fn = catia_sys_acl_set_file,
930         .sys_acl_delete_def_file_fn = catia_sys_acl_delete_def_file,
931         .getxattr_fn = catia_getxattr,
932         .listxattr_fn = catia_listxattr,
933         .removexattr_fn = catia_removexattr,
934         .setxattr_fn = catia_setxattr,
935 };
936
937 NTSTATUS vfs_catia_init(void)
938 {
939         NTSTATUS ret;
940
941         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
942                                 &vfs_catia_fns);
943         if (!NT_STATUS_IS_OK(ret))
944                 return ret;
945
946         vfs_catia_debug_level = debug_add_class("catia");
947         if (vfs_catia_debug_level == -1) {
948                 vfs_catia_debug_level = DBGC_VFS;
949                 DEBUG(0, ("vfs_catia: Couldn't register custom debugging "
950                           "class!\n"));
951         } else {
952                 DEBUG(10, ("vfs_catia: Debug class number of "
953                            "'catia': %d\n", vfs_catia_debug_level));
954         }
955
956         return ret;
957
958 }