e2b4eb5745e3fc13727c9f638950adad5b1feab0
[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         }
214         if (global->mappings) {
215                 share_level->mappings = global->mappings;
216                 (*selected_out) = share_level;
217                 return True;
218         }
219
220         return False;
221 }
222
223 static NTSTATUS catia_string_replace_allocate(connection_struct *conn,
224                                               const char *name_in,
225                                               char **mapped_name,
226                                         enum vfs_translate_direction direction)
227 {
228         static smb_ucs2_t *tmpbuf = NULL;
229         smb_ucs2_t *ptr;
230         struct share_mapping_entry *selected;
231         struct char_mappings *map = NULL;
232         size_t converted_size;
233         TALLOC_CTX *ctx = talloc_tos();
234
235         if (!init_mappings(conn, &selected)) {
236                 /* No mappings found. Just use the old name */
237                 *mapped_name = talloc_strdup(NULL, name_in);
238                 if (!*mapped_name) {
239                         errno = ENOMEM;
240                         return NT_STATUS_NO_MEMORY;
241                 }
242                 return NT_STATUS_OK;
243         }
244
245         if ((push_ucs2_talloc(ctx, &tmpbuf, name_in,
246                               &converted_size)) == false) {
247                 return map_nt_error_from_unix(errno);
248         }
249         ptr = tmpbuf;
250         for(;*ptr;ptr++) {
251                 if (*ptr == 0)
252                         break;
253                 map = selected->mappings[T_PICK((*ptr))];
254
255                 /* nothing to do */
256                 if (!map)
257                         continue;
258
259                 *ptr = map->entry[T_OFFSET((*ptr))][direction];
260         }
261
262         if ((pull_ucs2_talloc(ctx, mapped_name, tmpbuf,
263                               &converted_size)) == false) {
264                 TALLOC_FREE(tmpbuf);
265                 return map_nt_error_from_unix(errno);
266         }
267         TALLOC_FREE(tmpbuf);
268         return NT_STATUS_OK;
269 }
270
271 static DIR *catia_opendir(vfs_handle_struct *handle,
272                         const struct smb_filename *smb_fname,
273                         const char *mask,
274                         uint32_t attr)
275 {
276         char *name_mapped = NULL;
277         NTSTATUS status;
278         DIR *ret;
279         struct smb_filename *mapped_smb_fname = NULL;
280
281         status = catia_string_replace_allocate(handle->conn,
282                                 smb_fname->base_name,
283                                 &name_mapped,
284                                 vfs_translate_to_unix);
285         if (!NT_STATUS_IS_OK(status)) {
286                 errno = map_errno_from_nt_status(status);
287                 return NULL;
288         }
289
290         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
291                                 name_mapped,
292                                 NULL,
293                                 NULL);
294         if (mapped_smb_fname == NULL) {
295                 TALLOC_FREE(mapped_smb_fname);
296                 errno = ENOMEM;
297                 return NULL;
298         }
299
300         ret = SMB_VFS_NEXT_OPENDIR(handle, mapped_smb_fname, mask, attr);
301
302         TALLOC_FREE(name_mapped);
303         TALLOC_FREE(mapped_smb_fname);
304
305         return ret;
306 }
307
308 /*
309  * TRANSLATE_NAME call which converts the given name to
310  * "WINDOWS displayable" name
311  */
312 static NTSTATUS catia_translate_name(struct vfs_handle_struct *handle,
313                                      const char *orig_name,
314                                      enum vfs_translate_direction direction,
315                                      TALLOC_CTX *mem_ctx,
316                                      char **pmapped_name)
317 {
318         char *name = NULL;
319         char *mapped_name;
320         NTSTATUS status, ret;
321
322         /*
323          * Copy the supplied name and free the memory for mapped_name,
324          * already allocated by the caller.
325          * We will be allocating new memory for mapped_name in
326          * catia_string_replace_allocate
327          */
328         name = talloc_strdup(talloc_tos(), orig_name);
329         if (!name) {
330                 errno = ENOMEM;
331                 return NT_STATUS_NO_MEMORY;
332         }
333         status = catia_string_replace_allocate(handle->conn, name,
334                         &mapped_name, direction);
335
336         TALLOC_FREE(name);
337         if (!NT_STATUS_IS_OK(status)) {
338                 return status;
339         }
340
341         ret = SMB_VFS_NEXT_TRANSLATE_NAME(handle, mapped_name, direction,
342                                           mem_ctx, pmapped_name);
343
344         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
345                 *pmapped_name = talloc_move(mem_ctx, &mapped_name);
346                 /* we need to return the former translation result here */
347                 ret = status;
348         } else {
349                 TALLOC_FREE(mapped_name);
350         }
351
352         return ret;
353 }
354
355 static int catia_open(vfs_handle_struct *handle,
356                       struct smb_filename *smb_fname,
357                       files_struct *fsp,
358                       int flags,
359                       mode_t mode)
360 {
361         char *name_mapped = NULL;
362         char *tmp_base_name;
363         int ret;
364         NTSTATUS status;
365
366         tmp_base_name = smb_fname->base_name;
367         status = catia_string_replace_allocate(handle->conn,
368                                         smb_fname->base_name,
369                                         &name_mapped, vfs_translate_to_unix);
370         if (!NT_STATUS_IS_OK(status)) {
371                 errno = map_errno_from_nt_status(status);
372                 return -1;
373         }
374
375         smb_fname->base_name = name_mapped;
376         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
377         smb_fname->base_name = tmp_base_name;
378         TALLOC_FREE(name_mapped);
379
380         return ret;
381 }
382
383 static int catia_rename(vfs_handle_struct *handle,
384                         const struct smb_filename *smb_fname_src,
385                         const struct smb_filename *smb_fname_dst)
386 {
387         TALLOC_CTX *ctx = talloc_tos();
388         struct smb_filename *smb_fname_src_tmp = NULL;
389         struct smb_filename *smb_fname_dst_tmp = NULL;
390         char *src_name_mapped = NULL;
391         char *dst_name_mapped = NULL;
392         NTSTATUS status;
393         int ret = -1;
394
395         status = catia_string_replace_allocate(handle->conn,
396                                 smb_fname_src->base_name,
397                                 &src_name_mapped, vfs_translate_to_unix);
398         if (!NT_STATUS_IS_OK(status)) {
399                 errno = map_errno_from_nt_status(status);
400                 return -1;
401         }
402
403         status = catia_string_replace_allocate(handle->conn,
404                                 smb_fname_dst->base_name,
405                                 &dst_name_mapped, vfs_translate_to_unix);
406         if (!NT_STATUS_IS_OK(status)) {
407                 errno = map_errno_from_nt_status(status);
408                 return -1;
409         }
410
411         /* Setup temporary smb_filename structs. */
412         smb_fname_src_tmp = cp_smb_filename(ctx, smb_fname_src);
413         if (smb_fname_src_tmp == NULL) {
414                 errno = ENOMEM;
415                 goto out;
416         }
417
418         smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
419         if (smb_fname_dst_tmp == NULL) {
420                 errno = ENOMEM;
421                 goto out;
422         }
423
424         smb_fname_src_tmp->base_name = src_name_mapped;
425         smb_fname_dst_tmp->base_name = dst_name_mapped; 
426         DEBUG(10, ("converted old name: %s\n",
427                                 smb_fname_str_dbg(smb_fname_src_tmp)));
428         DEBUG(10, ("converted new name: %s\n",
429                                 smb_fname_str_dbg(smb_fname_dst_tmp)));
430
431         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
432                         smb_fname_dst_tmp);
433 out:
434         TALLOC_FREE(src_name_mapped);
435         TALLOC_FREE(dst_name_mapped);
436         TALLOC_FREE(smb_fname_src_tmp);
437         TALLOC_FREE(smb_fname_dst_tmp);
438         return ret;
439 }
440
441 static int catia_stat(vfs_handle_struct *handle,
442                       struct smb_filename *smb_fname)
443 {
444         char *name = NULL;
445         char *tmp_base_name;
446         int ret;
447         NTSTATUS status;
448
449         status = catia_string_replace_allocate(handle->conn,
450                                 smb_fname->base_name,
451                                 &name, vfs_translate_to_unix);
452         if (!NT_STATUS_IS_OK(status)) {
453                 errno = map_errno_from_nt_status(status);
454                 return -1;
455         }
456
457         tmp_base_name = smb_fname->base_name;
458         smb_fname->base_name = name;
459
460         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
461         smb_fname->base_name = tmp_base_name;
462
463         TALLOC_FREE(name);
464         return ret;
465 }
466
467 static int catia_lstat(vfs_handle_struct *handle,
468                        struct smb_filename *smb_fname)
469 {
470         char *name = NULL;
471         char *tmp_base_name;
472         int ret;
473         NTSTATUS status;
474
475         status = catia_string_replace_allocate(handle->conn,
476                                 smb_fname->base_name,
477                                 &name, vfs_translate_to_unix);
478         if (!NT_STATUS_IS_OK(status)) {
479                 errno = map_errno_from_nt_status(status);
480                 return -1;
481         }
482
483         tmp_base_name = smb_fname->base_name;
484         smb_fname->base_name = name;
485
486         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
487         smb_fname->base_name = tmp_base_name;
488         TALLOC_FREE(name);
489
490         return ret;
491 }
492
493 static int catia_unlink(vfs_handle_struct *handle,
494                         const struct smb_filename *smb_fname)
495 {
496         struct smb_filename *smb_fname_tmp = NULL;
497         char *name = NULL;
498         NTSTATUS status;
499         int ret;
500
501         status = catia_string_replace_allocate(handle->conn,
502                                         smb_fname->base_name,
503                                         &name, vfs_translate_to_unix);
504         if (!NT_STATUS_IS_OK(status)) {
505                 errno = map_errno_from_nt_status(status);
506                 return -1;
507         }
508
509         /* Setup temporary smb_filename structs. */
510         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
511         if (smb_fname_tmp == NULL) {
512                 errno = ENOMEM;
513                 return -1;
514         }
515
516         smb_fname_tmp->base_name = name;
517         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
518         TALLOC_FREE(smb_fname_tmp);
519         TALLOC_FREE(name);
520
521         return ret;
522 }
523
524 static int catia_chown(vfs_handle_struct *handle,
525                        const struct smb_filename *smb_fname,
526                        uid_t uid,
527                        gid_t gid)
528 {
529         char *name = NULL;
530         NTSTATUS status;
531         int ret;
532         int saved_errno;
533         struct smb_filename *catia_smb_fname = NULL;
534
535         status = catia_string_replace_allocate(handle->conn,
536                                         smb_fname->base_name,
537                                         &name,
538                                         vfs_translate_to_unix);
539         if (!NT_STATUS_IS_OK(status)) {
540                 errno = map_errno_from_nt_status(status);
541                 return -1;
542         }
543         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
544                                         name,
545                                         NULL,
546                                         NULL);
547         if (catia_smb_fname == NULL) {
548                 TALLOC_FREE(name);
549                 errno = ENOMEM;
550                 return -1;
551         }
552
553         ret = SMB_VFS_NEXT_CHOWN(handle, catia_smb_fname, uid, gid);
554         saved_errno = errno;
555         TALLOC_FREE(name);
556         TALLOC_FREE(catia_smb_fname);
557         errno = saved_errno;
558         return ret;
559 }
560
561 static int catia_lchown(vfs_handle_struct *handle,
562                         const struct smb_filename *smb_fname,
563                         uid_t uid,
564                         gid_t gid)
565 {
566         char *name = NULL;
567         NTSTATUS status;
568         int ret;
569         int saved_errno;
570         struct smb_filename *catia_smb_fname = NULL;
571
572         status = catia_string_replace_allocate(handle->conn,
573                                         smb_fname->base_name,
574                                         &name,
575                                         vfs_translate_to_unix);
576         if (!NT_STATUS_IS_OK(status)) {
577                 errno = map_errno_from_nt_status(status);
578                 return -1;
579         }
580         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
581                                         name,
582                                         NULL,
583                                         NULL);
584         if (catia_smb_fname == NULL) {
585                 TALLOC_FREE(name);
586                 errno = ENOMEM;
587                 return -1;
588         }
589
590         ret = SMB_VFS_NEXT_LCHOWN(handle, catia_smb_fname, uid, gid);
591         saved_errno = errno;
592         TALLOC_FREE(name);
593         TALLOC_FREE(catia_smb_fname);
594         errno = saved_errno;
595         return ret;
596 }
597
598 static int catia_chmod(vfs_handle_struct *handle,
599                         const struct smb_filename *smb_fname,
600                         mode_t mode)
601 {
602         char *name = NULL;
603         NTSTATUS status;
604         int ret;
605         int saved_errno;
606         struct smb_filename *catia_smb_fname = NULL;
607
608         status = catia_string_replace_allocate(handle->conn,
609                                         smb_fname->base_name,
610                                         &name,
611                                         vfs_translate_to_unix);
612         if (!NT_STATUS_IS_OK(status)) {
613                 errno = map_errno_from_nt_status(status);
614                 return -1;
615         }
616         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
617                                         name,
618                                         NULL,
619                                         NULL);
620         if (catia_smb_fname == NULL) {
621                 TALLOC_FREE(name);
622                 errno = ENOMEM;
623                 return -1;
624         }
625
626         ret = SMB_VFS_NEXT_CHMOD(handle, catia_smb_fname, mode);
627         saved_errno = errno;
628         TALLOC_FREE(name);
629         TALLOC_FREE(catia_smb_fname);
630         errno = saved_errno;
631         return ret;
632 }
633
634 static int catia_rmdir(vfs_handle_struct *handle,
635                        const struct smb_filename *smb_fname)
636 {
637         char *name = NULL;
638         NTSTATUS status;
639         int ret;
640         struct smb_filename *catia_smb_fname = NULL;
641
642         status = catia_string_replace_allocate(handle->conn,
643                                 smb_fname->base_name,
644                                 &name,
645                                 vfs_translate_to_unix);
646         if (!NT_STATUS_IS_OK(status)) {
647                 errno = map_errno_from_nt_status(status);
648                 return -1;
649         }
650         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
651                                         name,
652                                         NULL,
653                                         NULL);
654         if (catia_smb_fname == NULL) {
655                 TALLOC_FREE(name);
656                 errno = ENOMEM;
657                 return -1;
658         }
659
660         ret = SMB_VFS_NEXT_RMDIR(handle, catia_smb_fname);
661         TALLOC_FREE(name);
662         TALLOC_FREE(catia_smb_fname);
663
664         return ret;
665 }
666
667 static int catia_mkdir(vfs_handle_struct *handle,
668                        const struct smb_filename *smb_fname,
669                        mode_t mode)
670 {
671         char *name = NULL;
672         NTSTATUS status;
673         int ret;
674         struct smb_filename *catia_smb_fname = NULL;
675
676         status = catia_string_replace_allocate(handle->conn,
677                                 smb_fname->base_name,
678                                 &name,
679                                 vfs_translate_to_unix);
680         if (!NT_STATUS_IS_OK(status)) {
681                 errno = map_errno_from_nt_status(status);
682                 return -1;
683         }
684         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
685                                         name,
686                                         NULL,
687                                         NULL);
688         if (catia_smb_fname == NULL) {
689                 TALLOC_FREE(name);
690                 errno = ENOMEM;
691                 return -1;
692         }
693
694         ret = SMB_VFS_NEXT_MKDIR(handle, catia_smb_fname, mode);
695         TALLOC_FREE(name);
696         TALLOC_FREE(catia_smb_fname);
697
698         return ret;
699 }
700
701 static int catia_chdir(vfs_handle_struct *handle,
702                        const char *path)
703 {
704         char *name = NULL;
705         NTSTATUS status;
706         int ret;
707
708         status = catia_string_replace_allocate(handle->conn, path,
709                                         &name, vfs_translate_to_unix);
710         if (!NT_STATUS_IS_OK(status)) {
711                 errno = map_errno_from_nt_status(status);
712                 return -1;
713         }
714
715         ret = SMB_VFS_NEXT_CHDIR(handle, name);
716         TALLOC_FREE(name);
717
718         return ret;
719 }
720
721 static int catia_ntimes(vfs_handle_struct *handle,
722                         const struct smb_filename *smb_fname,
723                         struct smb_file_time *ft)
724 {
725         struct smb_filename *smb_fname_tmp = NULL;
726         char *name = NULL;
727         NTSTATUS status;
728         int ret;
729
730         status = catia_string_replace_allocate(handle->conn,
731                                 smb_fname->base_name,
732                                 &name, vfs_translate_to_unix);
733         if (!NT_STATUS_IS_OK(status)) {
734                 errno = map_errno_from_nt_status(status);
735                 return -1;
736         }
737
738         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
739         if (smb_fname_tmp == NULL) {
740                 errno = ENOMEM;
741                 return -1;
742         }
743
744         smb_fname_tmp->base_name = name;
745         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
746         TALLOC_FREE(name);
747         TALLOC_FREE(smb_fname_tmp);
748
749         return ret;
750 }
751
752 static char *
753 catia_realpath(vfs_handle_struct *handle, const char *path)
754 {
755         char *mapped_name = NULL;
756         NTSTATUS status;
757         char *ret = NULL;
758
759         status = catia_string_replace_allocate(handle->conn, path,
760                                         &mapped_name, vfs_translate_to_unix);
761         if (!NT_STATUS_IS_OK(status)) {
762                 errno = map_errno_from_nt_status(status);
763                 return NULL;
764         }
765
766         ret = SMB_VFS_NEXT_REALPATH(handle, mapped_name);
767         TALLOC_FREE(mapped_name);
768
769         return ret;
770 }
771
772 static int catia_chflags(struct vfs_handle_struct *handle,
773                          const char *path, unsigned int flags)
774 {
775         char *mapped_name = NULL;
776         NTSTATUS status;
777         int ret;
778
779         status = catia_string_replace_allocate(handle->conn, path,
780                                         &mapped_name, vfs_translate_to_unix);
781         if (!NT_STATUS_IS_OK(status)) {
782                 errno = map_errno_from_nt_status(status);
783                 return -1;
784         }
785
786         ret = SMB_VFS_NEXT_CHFLAGS(handle, mapped_name, flags);
787         TALLOC_FREE(mapped_name);
788
789         return ret;
790 }
791
792 static NTSTATUS
793 catia_streaminfo(struct vfs_handle_struct *handle,
794                  struct files_struct *fsp,
795                  const struct smb_filename *smb_fname,
796                  TALLOC_CTX *mem_ctx,
797                  unsigned int *_num_streams,
798                  struct stream_struct **_streams)
799 {
800         char *mapped_name = NULL;
801         NTSTATUS status;
802         int i;
803         struct smb_filename *catia_smb_fname = NULL;
804         unsigned int num_streams = 0;
805         struct stream_struct *streams = NULL;
806
807         *_num_streams = 0;
808         *_streams = NULL;
809
810         status = catia_string_replace_allocate(handle->conn,
811                                 smb_fname->base_name,
812                                 &mapped_name,
813                                 vfs_translate_to_unix);
814         if (!NT_STATUS_IS_OK(status)) {
815                 errno = map_errno_from_nt_status(status);
816                 return status;
817         }
818
819         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
820                                         mapped_name,
821                                         NULL,
822                                         NULL);
823         if (catia_smb_fname == NULL) {
824                 TALLOC_FREE(mapped_name);
825                 return NT_STATUS_NO_MEMORY;
826         }
827
828         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, catia_smb_fname,
829                                          mem_ctx, &num_streams, &streams);
830         TALLOC_FREE(mapped_name);
831         TALLOC_FREE(catia_smb_fname);
832         if (!NT_STATUS_IS_OK(status)) {
833                 return status;
834         }
835
836         /*
837          * Translate stream names just like the base names
838          */
839         for (i = 0; i < num_streams; i++) {
840                 /*
841                  * Strip ":" prefix and ":$DATA" suffix to get a
842                  * "pure" stream name and only translate that.
843                  */
844                 void *old_ptr = streams[i].name;
845                 char *stream_name = streams[i].name + 1;
846                 char *stream_type = strrchr_m(stream_name, ':');
847
848                 if (stream_type != NULL) {
849                         *stream_type = '\0';
850                         stream_type += 1;
851                 }
852
853                 status = catia_string_replace_allocate(handle->conn, stream_name,
854                                                        &mapped_name, vfs_translate_to_windows);
855                 if (!NT_STATUS_IS_OK(status)) {
856                         TALLOC_FREE(streams);
857                         return status;
858                 }
859
860                 if (stream_type != NULL) {
861                         streams[i].name = talloc_asprintf(streams, ":%s:%s",
862                                                           mapped_name, stream_type);
863                 } else {
864                         streams[i].name = talloc_asprintf(streams, ":%s",
865                                                           mapped_name);
866                 }
867                 TALLOC_FREE(mapped_name);
868                 TALLOC_FREE(old_ptr);
869                 if (streams[i].name == NULL) {
870                         TALLOC_FREE(streams);
871                         return NT_STATUS_NO_MEMORY;
872                 }
873         }
874
875         *_num_streams = num_streams;
876         *_streams = streams;
877         return NT_STATUS_OK;
878 }
879
880 static NTSTATUS
881 catia_get_nt_acl(struct vfs_handle_struct *handle,
882                  const struct smb_filename *smb_fname,
883                  uint32_t security_info,
884                  TALLOC_CTX *mem_ctx,
885                  struct security_descriptor **ppdesc)
886 {
887         char *mapped_name = NULL;
888         const char *path = smb_fname->base_name;
889         struct smb_filename *mapped_smb_fname = NULL;
890         NTSTATUS status;
891
892         status = catia_string_replace_allocate(handle->conn,
893                                 path, &mapped_name, vfs_translate_to_unix);
894         if (!NT_STATUS_IS_OK(status)) {
895                 errno = map_errno_from_nt_status(status);
896                 return status;
897         }
898         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
899                                         mapped_name,
900                                         NULL,
901                                         NULL);
902         if (mapped_smb_fname == NULL) {
903                 TALLOC_FREE(mapped_name);
904                 return NT_STATUS_NO_MEMORY;
905         }
906
907         status = SMB_VFS_NEXT_GET_NT_ACL(handle, mapped_smb_fname,
908                                          security_info, mem_ctx, ppdesc);
909         TALLOC_FREE(mapped_name);
910         TALLOC_FREE(mapped_smb_fname);
911
912         return status;
913 }
914
915 static int
916 catia_chmod_acl(vfs_handle_struct *handle,
917                 const struct smb_filename *smb_fname,
918                 mode_t mode)
919 {
920         char *mapped_name = NULL;
921         struct smb_filename *mapped_smb_fname = NULL;
922         NTSTATUS status;
923         int ret;
924         int saved_errno;
925
926         status = catia_string_replace_allocate(handle->conn,
927                                 smb_fname->base_name,
928                                 &mapped_name,
929                                 vfs_translate_to_unix);
930         if (!NT_STATUS_IS_OK(status)) {
931                 errno = map_errno_from_nt_status(status);
932                 return -1;
933         }
934
935         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
936                                         mapped_name,
937                                         NULL,
938                                         NULL);
939         if (mapped_smb_fname == NULL) {
940                 TALLOC_FREE(mapped_name);
941                 errno = ENOMEM;
942                 return -1;
943         }
944         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, mapped_smb_fname, mode);
945         saved_errno = errno;
946         TALLOC_FREE(mapped_name);
947         TALLOC_FREE(mapped_smb_fname);
948         errno = saved_errno;
949         return ret;
950 }
951
952 static SMB_ACL_T
953 catia_sys_acl_get_file(vfs_handle_struct *handle,
954                        const char *path,
955                        SMB_ACL_TYPE_T type,
956                        TALLOC_CTX *mem_ctx)
957 {
958         char *mapped_name = NULL;
959         NTSTATUS status;
960         SMB_ACL_T ret;
961
962         status = catia_string_replace_allocate(handle->conn,
963                                 path, &mapped_name, vfs_translate_to_unix);
964         if (!NT_STATUS_IS_OK(status)) {
965                 errno = map_errno_from_nt_status(status);
966                 return NULL;
967         }
968
969         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, mapped_name, type, mem_ctx);
970         TALLOC_FREE(mapped_name);
971
972         return ret;
973 }
974
975 static int
976 catia_sys_acl_set_file(vfs_handle_struct *handle,
977                        const char *path,
978                        SMB_ACL_TYPE_T type,
979                        SMB_ACL_T theacl)
980 {
981         char *mapped_name = NULL;
982         NTSTATUS status;
983         int ret;
984
985         status = catia_string_replace_allocate(handle->conn,
986                                 path, &mapped_name, vfs_translate_to_unix);
987         if (!NT_STATUS_IS_OK(status)) {
988                 errno = map_errno_from_nt_status(status);
989                 return -1;
990         }
991
992         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, mapped_name, type, theacl);
993         TALLOC_FREE(mapped_name);
994
995         return ret;
996 }
997
998 static int
999 catia_sys_acl_delete_def_file(vfs_handle_struct *handle,
1000                               const char *path)
1001 {
1002         char *mapped_name = NULL;
1003         NTSTATUS status;
1004         int ret;
1005
1006         status = catia_string_replace_allocate(handle->conn,
1007                                 path, &mapped_name, vfs_translate_to_unix);
1008         if (!NT_STATUS_IS_OK(status)) {
1009                 errno = map_errno_from_nt_status(status);
1010                 return -1;
1011         }
1012
1013         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, mapped_name);
1014         TALLOC_FREE(mapped_name);
1015
1016         return ret;
1017 }
1018
1019 static ssize_t
1020 catia_getxattr(vfs_handle_struct *handle, const char *path,
1021                const char *name, void *value, size_t size)
1022 {
1023         char *mapped_name = NULL;
1024         NTSTATUS status;
1025         ssize_t ret;
1026
1027         status = catia_string_replace_allocate(handle->conn,
1028                                 name, &mapped_name, vfs_translate_to_unix);
1029         if (!NT_STATUS_IS_OK(status)) {
1030                 errno = map_errno_from_nt_status(status);
1031                 return -1;
1032         }
1033
1034
1035         ret = SMB_VFS_NEXT_GETXATTR(handle, path, mapped_name, value, size);
1036         TALLOC_FREE(mapped_name);
1037
1038         return ret;
1039 }
1040
1041 static ssize_t
1042 catia_listxattr(vfs_handle_struct *handle, const char *path,
1043                 char *list, size_t size)
1044 {
1045         char *mapped_name = NULL;
1046         NTSTATUS status;
1047         ssize_t ret;
1048
1049         status = catia_string_replace_allocate(handle->conn,
1050                                 path, &mapped_name, vfs_translate_to_unix);
1051         if (!NT_STATUS_IS_OK(status)) {
1052                 errno = map_errno_from_nt_status(status);
1053                 return -1;
1054         }
1055
1056
1057         ret = SMB_VFS_NEXT_LISTXATTR(handle, mapped_name, list, size);
1058         TALLOC_FREE(mapped_name);
1059
1060         return ret;
1061 }
1062
1063 static int
1064 catia_removexattr(vfs_handle_struct *handle, const char *path,
1065                   const char *name)
1066 {
1067         char *mapped_name = NULL;
1068         NTSTATUS status;
1069         ssize_t ret;
1070
1071         status = catia_string_replace_allocate(handle->conn,
1072                                 name, &mapped_name, vfs_translate_to_unix);
1073         if (!NT_STATUS_IS_OK(status)) {
1074                 errno = map_errno_from_nt_status(status);
1075                 return -1;
1076         }
1077
1078
1079         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, path, mapped_name);
1080         TALLOC_FREE(mapped_name);
1081
1082         return ret;
1083 }
1084
1085 static int
1086 catia_setxattr(vfs_handle_struct *handle, const char *path,
1087                const char *name, const void *value, size_t size,
1088                int flags)
1089 {
1090         char *mapped_name = NULL;
1091         NTSTATUS status;
1092         ssize_t ret;
1093
1094         status = catia_string_replace_allocate(handle->conn,
1095                                 name, &mapped_name, vfs_translate_to_unix);
1096         if (!NT_STATUS_IS_OK(status)) {
1097                 errno = map_errno_from_nt_status(status);
1098                 return -1;
1099         }
1100
1101
1102         ret = SMB_VFS_NEXT_SETXATTR(handle, path, mapped_name, value, size, flags);
1103         TALLOC_FREE(mapped_name);
1104
1105         return ret;
1106 }
1107
1108 static struct vfs_fn_pointers vfs_catia_fns = {
1109         .mkdir_fn = catia_mkdir,
1110         .rmdir_fn = catia_rmdir,
1111         .opendir_fn = catia_opendir,
1112         .open_fn = catia_open,
1113         .rename_fn = catia_rename,
1114         .stat_fn = catia_stat,
1115         .lstat_fn = catia_lstat,
1116         .unlink_fn = catia_unlink,
1117         .chown_fn = catia_chown,
1118         .lchown_fn = catia_lchown,
1119         .chmod_fn = catia_chmod,
1120         .chdir_fn = catia_chdir,
1121         .ntimes_fn = catia_ntimes,
1122         .realpath_fn = catia_realpath,
1123         .chflags_fn = catia_chflags,
1124         .streaminfo_fn = catia_streaminfo,
1125         .translate_name_fn = catia_translate_name,
1126         .get_nt_acl_fn = catia_get_nt_acl,
1127         .chmod_acl_fn = catia_chmod_acl,
1128         .sys_acl_get_file_fn = catia_sys_acl_get_file,
1129         .sys_acl_set_file_fn = catia_sys_acl_set_file,
1130         .sys_acl_delete_def_file_fn = catia_sys_acl_delete_def_file,
1131         .getxattr_fn = catia_getxattr,
1132         .listxattr_fn = catia_listxattr,
1133         .removexattr_fn = catia_removexattr,
1134         .setxattr_fn = catia_setxattr,
1135 };
1136
1137 static_decl_vfs;
1138 NTSTATUS vfs_catia_init(void)
1139 {
1140         NTSTATUS ret;
1141
1142         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
1143                                 &vfs_catia_fns);
1144         if (!NT_STATUS_IS_OK(ret))
1145                 return ret;
1146
1147         vfs_catia_debug_level = debug_add_class("catia");
1148         if (vfs_catia_debug_level == -1) {
1149                 vfs_catia_debug_level = DBGC_VFS;
1150                 DEBUG(0, ("vfs_catia: Couldn't register custom debugging "
1151                           "class!\n"));
1152         } else {
1153                 DEBUG(10, ("vfs_catia: Debug class number of "
1154                            "'catia': %d\n", vfs_catia_debug_level));
1155         }
1156
1157         return ret;
1158
1159 }