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