0ee7c36a361c6576472436b2e4ba69b5e1721d87
[nivanova/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  * Catia V4 on AIX uses characters like "<*$ a *lot*, all forbidden under
8  * Windows...
9  *
10  * Copyright (C) Volker Lendecke, 2005
11  * Copyright (C) Aravind Srinivasan, 2009
12  * Copyright (C) Guenter Kukkukk, 2013
13  * Copyright (C) Ralph Boehme, 2017
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 #include "lib/util/tevent_unix.h"
33 #include "lib/util/tevent_ntstatus.h"
34
35 static int vfs_catia_debug_level = DBGC_VFS;
36
37 #undef DBGC_CLASS
38 #define DBGC_CLASS vfs_catia_debug_level
39
40 #define GLOBAL_SNUM     0xFFFFFFF
41 #define MAP_SIZE        0xFF
42 #define MAP_NUM         0x101 /* max unicode charval / MAP_SIZE */
43 #define T_OFFSET(_v_)   ((_v_ % MAP_SIZE))
44 #define T_START(_v_)    (((_v_ / MAP_SIZE) * MAP_SIZE))
45 #define T_PICK(_v_)     ((_v_ / MAP_SIZE))
46
47 struct char_mappings {
48         smb_ucs2_t entry[MAP_SIZE][2];
49 };
50
51 struct share_mapping_entry {
52         int snum;
53         struct share_mapping_entry *next;
54         struct char_mappings **mappings;
55 };
56
57 struct catia_cache {
58         bool is_fsp_ext;
59         const struct catia_cache * const *busy;
60         char *orig_fname;
61         char *fname;
62         char *orig_base_fname;
63         char *base_fname;
64 };
65
66 struct share_mapping_entry *srt_head = NULL;
67
68 static bool build_table(struct char_mappings **cmaps, int value)
69 {
70         int i;
71         int start = T_START(value);
72
73         (*cmaps) = talloc_zero(NULL, struct char_mappings);
74
75         if (!*cmaps)
76                 return False;
77
78         for (i = 0; i < MAP_SIZE;i++) {
79                 (*cmaps)->entry[i][vfs_translate_to_unix] = start + i;
80                 (*cmaps)->entry[i][vfs_translate_to_windows] = start + i;
81         }
82
83         return True;
84 }
85
86 static void set_tables(struct char_mappings **cmaps,
87                        long unix_map,
88                        long windows_map)
89 {
90         int i;
91
92         /* set unix -> windows */
93         i = T_OFFSET(unix_map);
94         cmaps[T_PICK(unix_map)]->entry[i][vfs_translate_to_windows] = windows_map;
95
96         /* set windows -> unix */
97         i = T_OFFSET(windows_map);
98         cmaps[T_PICK(windows_map)]->entry[i][vfs_translate_to_unix] = unix_map;
99 }
100
101 static bool build_ranges(struct char_mappings **cmaps,
102                          long unix_map,
103                          long windows_map)
104 {
105
106         if (!cmaps[T_PICK(unix_map)]) {
107                 if (!build_table(&cmaps[T_PICK(unix_map)], unix_map))
108                         return False;
109         }
110
111         if (!cmaps[T_PICK(windows_map)]) {
112                 if (!build_table(&cmaps[T_PICK(windows_map)], windows_map))
113                         return False;
114         }
115
116         set_tables(cmaps, unix_map, windows_map);
117
118         return True;
119 }
120
121 static struct share_mapping_entry *get_srt(connection_struct *conn,
122                                            struct share_mapping_entry **global)
123 {
124         struct share_mapping_entry *share;
125
126         for (share = srt_head; share != NULL; share = share->next) {
127                 if (share->snum == GLOBAL_SNUM)
128                         (*global) = share;
129
130                 if (share->snum == SNUM(conn))
131                         return share;
132         }
133
134         return share;
135 }
136
137 static struct share_mapping_entry *add_srt(int snum, const char **mappings)
138 {
139
140         char *tmp;
141         fstring mapping;
142         int i;
143         long unix_map, windows_map;
144         struct share_mapping_entry *ret = NULL;
145
146         ret = (struct share_mapping_entry *)
147                 TALLOC_ZERO(NULL, sizeof(struct share_mapping_entry) +
148                 (mappings ? (MAP_NUM * sizeof(struct char_mappings *)) : 0));
149
150         if (!ret)
151                 return ret;
152
153         ret->snum = snum;
154
155         ret->next = srt_head;
156         srt_head = ret;
157
158         if (mappings) {
159                 ret->mappings = (struct char_mappings**) ((unsigned char*) ret +
160                     sizeof(struct share_mapping_entry));
161                 memset(ret->mappings, 0,
162                     MAP_NUM * sizeof(struct char_mappings *));
163         } else {
164                 ret->mappings = NULL;
165                 return ret;
166         }
167
168         /*
169          * catia mappings are of the form :
170          * UNIX char (in 0xnn hex) : WINDOWS char (in 0xnn hex)
171          *
172          * multiple mappings are comma separated in smb.conf
173          */
174         for (i=0;mappings[i];i++) {
175                 fstrcpy(mapping, mappings[i]);
176                 unix_map = strtol(mapping, &tmp, 16);
177                 if (unix_map == 0 && errno == EINVAL) {
178                         DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
179                         continue;
180                 }
181                 windows_map = strtol(++tmp, NULL, 16);
182                 if (windows_map == 0 && errno == EINVAL) {
183                         DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
184                         continue;
185                 }
186
187                 if (!build_ranges(ret->mappings, unix_map, windows_map)) {
188                         DEBUG(0, ("TABLE ERROR - CATIA MAPPINGS - %s\n", mapping));
189                         continue;
190                 }
191         }
192
193         return ret;
194 }
195
196 static bool init_mappings(connection_struct *conn,
197                           struct share_mapping_entry **selected_out)
198 {
199         const char **mappings = NULL;
200         struct share_mapping_entry *share_level = NULL;
201         struct share_mapping_entry *global = NULL;
202
203         /* check srt cache */
204         share_level = get_srt(conn, &global);
205         if (share_level) {
206                 *selected_out = share_level;
207                 return (share_level->mappings != NULL);
208         }
209
210         /* see if we have a global setting */
211         if (!global) {
212                 /* global setting */
213                 mappings = lp_parm_string_list(-1, "catia", "mappings", NULL);
214                 global = add_srt(GLOBAL_SNUM, mappings);
215         }
216
217         /* no global setting - what about share level ? */
218         mappings = lp_parm_string_list(SNUM(conn), "catia", "mappings", NULL);
219         share_level = add_srt(SNUM(conn), mappings);
220
221         if (share_level->mappings) {
222                 (*selected_out) = share_level;
223                 return True;
224         }
225         if (global->mappings) {
226                 share_level->mappings = global->mappings;
227                 (*selected_out) = share_level;
228                 return True;
229         }
230
231         return False;
232 }
233
234 static NTSTATUS catia_string_replace_allocate(connection_struct *conn,
235                                               const char *name_in,
236                                               char **mapped_name,
237                                         enum vfs_translate_direction direction)
238 {
239         static smb_ucs2_t *tmpbuf = NULL;
240         smb_ucs2_t *ptr;
241         struct share_mapping_entry *selected;
242         struct char_mappings *map = NULL;
243         size_t converted_size;
244         TALLOC_CTX *ctx = talloc_tos();
245
246         if (!init_mappings(conn, &selected)) {
247                 /* No mappings found. Just use the old name */
248                 *mapped_name = talloc_strdup(NULL, name_in);
249                 if (!*mapped_name) {
250                         errno = ENOMEM;
251                         return NT_STATUS_NO_MEMORY;
252                 }
253                 return NT_STATUS_OK;
254         }
255
256         if ((push_ucs2_talloc(ctx, &tmpbuf, name_in,
257                               &converted_size)) == false) {
258                 return map_nt_error_from_unix(errno);
259         }
260         ptr = tmpbuf;
261         for(;*ptr;ptr++) {
262                 if (*ptr == 0)
263                         break;
264                 map = selected->mappings[T_PICK((*ptr))];
265
266                 /* nothing to do */
267                 if (!map)
268                         continue;
269
270                 *ptr = map->entry[T_OFFSET((*ptr))][direction];
271         }
272
273         if ((pull_ucs2_talloc(ctx, mapped_name, tmpbuf,
274                               &converted_size)) == false) {
275                 TALLOC_FREE(tmpbuf);
276                 return map_nt_error_from_unix(errno);
277         }
278         TALLOC_FREE(tmpbuf);
279         return NT_STATUS_OK;
280 }
281
282 static DIR *catia_opendir(vfs_handle_struct *handle,
283                         const struct smb_filename *smb_fname,
284                         const char *mask,
285                         uint32_t attr)
286 {
287         char *name_mapped = NULL;
288         NTSTATUS status;
289         DIR *ret;
290         struct smb_filename *mapped_smb_fname = NULL;
291
292         status = catia_string_replace_allocate(handle->conn,
293                                 smb_fname->base_name,
294                                 &name_mapped,
295                                 vfs_translate_to_unix);
296         if (!NT_STATUS_IS_OK(status)) {
297                 errno = map_errno_from_nt_status(status);
298                 return NULL;
299         }
300
301         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
302                                 name_mapped,
303                                 NULL,
304                                 NULL,
305                                 smb_fname->flags);
306         if (mapped_smb_fname == NULL) {
307                 TALLOC_FREE(mapped_smb_fname);
308                 errno = ENOMEM;
309                 return NULL;
310         }
311
312         ret = SMB_VFS_NEXT_OPENDIR(handle, mapped_smb_fname, mask, attr);
313
314         TALLOC_FREE(name_mapped);
315         TALLOC_FREE(mapped_smb_fname);
316
317         return ret;
318 }
319
320 /*
321  * TRANSLATE_NAME call which converts the given name to
322  * "WINDOWS displayable" name
323  */
324 static NTSTATUS catia_translate_name(struct vfs_handle_struct *handle,
325                                      const char *orig_name,
326                                      enum vfs_translate_direction direction,
327                                      TALLOC_CTX *mem_ctx,
328                                      char **pmapped_name)
329 {
330         char *name = NULL;
331         char *mapped_name;
332         NTSTATUS status, ret;
333
334         /*
335          * Copy the supplied name and free the memory for mapped_name,
336          * already allocated by the caller.
337          * We will be allocating new memory for mapped_name in
338          * catia_string_replace_allocate
339          */
340         name = talloc_strdup(talloc_tos(), orig_name);
341         if (!name) {
342                 errno = ENOMEM;
343                 return NT_STATUS_NO_MEMORY;
344         }
345         status = catia_string_replace_allocate(handle->conn, name,
346                         &mapped_name, direction);
347
348         TALLOC_FREE(name);
349         if (!NT_STATUS_IS_OK(status)) {
350                 return status;
351         }
352
353         ret = SMB_VFS_NEXT_TRANSLATE_NAME(handle, mapped_name, direction,
354                                           mem_ctx, pmapped_name);
355
356         if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
357                 *pmapped_name = talloc_move(mem_ctx, &mapped_name);
358                 /* we need to return the former translation result here */
359                 ret = status;
360         } else {
361                 TALLOC_FREE(mapped_name);
362         }
363
364         return ret;
365 }
366
367 #define CATIA_DEBUG_CC(lvl, cc, fsp) \
368         catia_debug_cc((lvl), (cc), (fsp), __location__);
369
370 static void catia_debug_cc(int lvl,
371                            struct catia_cache *cc,
372                            files_struct *fsp,
373                            const char *location)
374 {
375         DEBUG(lvl, ("%s: cc [0x%p] cc->busy [0x%p] "
376                     "is_fsp_ext [%s] "
377                     "fsp [0x%p] fsp name [%s] "
378                     "orig_fname [%s] "
379                     "fname [%s] "
380                     "orig_base_fname [%s] "
381                     "base_fname [%s]\n",
382                     location,
383                     cc, cc->busy,
384                     cc->is_fsp_ext ? "yes" : "no",
385                     fsp, fsp_str_dbg(fsp),
386                     cc->orig_fname, cc->fname,
387                     cc->orig_base_fname, cc->base_fname));
388 }
389
390 static void catia_free_cc(struct catia_cache **_cc,
391                           vfs_handle_struct *handle,
392                           files_struct *fsp)
393 {
394         struct catia_cache *cc = *_cc;
395
396         if (cc->is_fsp_ext) {
397                 VFS_REMOVE_FSP_EXTENSION(handle, fsp);
398                 cc = NULL;
399         } else {
400                 TALLOC_FREE(cc);
401         }
402
403         *_cc = NULL;
404 }
405
406 static struct catia_cache *catia_validate_and_apply_cc(
407                                        vfs_handle_struct *handle,
408                                        files_struct *fsp,
409                                        const struct catia_cache * const *busy,
410                                        bool *make_tmp_cache)
411 {
412         struct catia_cache *cc = NULL;
413
414         *make_tmp_cache = false;
415
416         cc = (struct catia_cache *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
417         if (cc == NULL) {
418                 return NULL;
419         }
420
421         if (cc->busy != NULL) {
422                 if (cc->busy == busy) {
423                         /* This should never happen */
424                         CATIA_DEBUG_CC(0, cc, fsp);
425                         smb_panic(__location__);
426                 }
427
428                 /*
429                  * Recursion. Validate names, the names in the fsp's should be
430                  * the translated names we had set.
431                  */
432
433                 if ((cc->fname != fsp->fsp_name->base_name)
434                     ||
435                     ((fsp->base_fsp != NULL) &&
436                      (cc->base_fname != fsp->base_fsp->fsp_name->base_name)))
437                 {
438                         CATIA_DEBUG_CC(10, cc, fsp);
439
440                         /*
441                          * Names changed. Setting don't expose the cache on the
442                          * fsp and ask the caller to create a temporary cache.
443                          */
444                         *make_tmp_cache = true;
445                         return NULL;
446                 }
447
448                 /*
449                  * Ok, a validated cache while in a recursion, just let the
450                  * caller detect that cc->busy is != busy and there's
451                  * nothing else to do.
452                  */
453                 CATIA_DEBUG_CC(10, cc, fsp);
454                 return cc;
455         }
456
457         /* Not in a recursion */
458
459         if ((cc->orig_fname != fsp->fsp_name->base_name)
460             ||
461             ((fsp->base_fsp != NULL) &&
462              (cc->orig_base_fname != fsp->base_fsp->fsp_name->base_name)))
463         {
464                 /*
465                  * fsp names changed, this can happen in an rename op.
466                  * Trigger recreation as a full fledged fsp extension.
467                  */
468
469                 CATIA_DEBUG_CC(10, cc, fsp);
470                 catia_free_cc(&cc, handle, fsp);
471                 return NULL;
472         }
473
474
475         /*
476          * Ok, we found a valid cache entry, no recursion. Just set translated
477          * names from the cache and mark the cc as busy.
478          */
479         fsp->fsp_name->base_name = cc->fname;
480         if (fsp->base_fsp != NULL) {
481                 fsp->base_fsp->fsp_name->base_name = cc->base_fname;
482         }
483
484         cc->busy = busy;
485         CATIA_DEBUG_CC(10, cc, fsp);
486         return cc;
487 }
488
489 #define CATIA_FETCH_FSP_PRE_NEXT(mem_ctx, handle, fsp, _cc) \
490         catia_fetch_fsp_pre_next((mem_ctx), (handle), (fsp), (_cc), __func__);
491
492 static int catia_fetch_fsp_pre_next(TALLOC_CTX *mem_ctx,
493                                     vfs_handle_struct *handle,
494                                     files_struct *fsp,
495                                     struct catia_cache **_cc,
496                                     const char *function)
497 {
498         const struct catia_cache * const *busy =
499                 (const struct catia_cache * const *)_cc;
500         struct catia_cache *cc = NULL;
501         NTSTATUS status;
502         bool make_tmp_cache = false;
503
504         *_cc = NULL;
505
506         DBG_DEBUG("Called from [%s]\n", function);
507
508         cc = catia_validate_and_apply_cc(handle,
509                                          fsp,
510                                          busy,
511                                          &make_tmp_cache);
512         if (cc != NULL) {
513                 if (cc->busy != busy) {
514                         return 0;
515                 }
516                 *_cc = cc;
517                 return 0;
518         }
519
520         if (!make_tmp_cache) {
521                 cc = (struct catia_cache *)VFS_ADD_FSP_EXTENSION(
522                         handle, fsp, struct catia_cache, NULL);
523                 if (cc == NULL) {
524                         return -1;
525                 }
526                 *cc = (struct catia_cache) {
527                         .is_fsp_ext = true,
528                 };
529
530                 mem_ctx = VFS_MEMCTX_FSP_EXTENSION(handle, fsp);
531                 if (mem_ctx == NULL) {
532                         DBG_ERR("VFS_MEMCTX_FSP_EXTENSION failed\n");
533                         catia_free_cc(&cc, handle, fsp);
534                         return -1;
535                 }
536         } else {
537                 cc = talloc_zero(mem_ctx, struct catia_cache);
538                 if (cc == NULL) {
539                         return -1;
540                 }
541                 mem_ctx = cc;
542         }
543
544
545         status = catia_string_replace_allocate(handle->conn,
546                                                fsp->fsp_name->base_name,
547                                                &cc->fname,
548                                                vfs_translate_to_unix);
549         if (!NT_STATUS_IS_OK(status)) {
550                 catia_free_cc(&cc, handle, fsp);
551                 errno = map_errno_from_nt_status(status);
552                 return -1;
553         }
554         talloc_steal(mem_ctx, cc->fname);
555
556         if (fsp->base_fsp != NULL) {
557                 status = catia_string_replace_allocate(
558                         handle->conn,
559                         fsp->base_fsp->fsp_name->base_name,
560                         &cc->base_fname,
561                         vfs_translate_to_unix);
562                 if (!NT_STATUS_IS_OK(status)) {
563                         catia_free_cc(&cc, handle, fsp);
564                         errno = map_errno_from_nt_status(status);
565                         return -1;
566                 }
567                 talloc_steal(mem_ctx, cc->base_fname);
568         }
569
570         cc->orig_fname = fsp->fsp_name->base_name;
571         fsp->fsp_name->base_name = cc->fname;
572
573         if (fsp->base_fsp != NULL) {
574                 cc->orig_base_fname = fsp->base_fsp->fsp_name->base_name;
575                 fsp->base_fsp->fsp_name->base_name = cc->base_fname;
576         }
577
578         cc->busy = busy;
579         CATIA_DEBUG_CC(10, cc, fsp);
580
581         *_cc = cc;
582
583         return 0;
584 }
585
586 #define CATIA_FETCH_FSP_POST_NEXT(_cc, fsp) do { \
587         int saved_errno = errno; \
588         catia_fetch_fsp_post_next((_cc), (fsp), __func__); \
589         errno = saved_errno; \
590 } while(0)
591
592 static void catia_fetch_fsp_post_next(struct catia_cache **_cc,
593                                       files_struct *fsp,
594                                       const char *function)
595 {
596         const struct catia_cache * const *busy =
597                 (const struct catia_cache * const *)_cc;
598         struct catia_cache *cc = *_cc;
599
600         DBG_DEBUG("Called from [%s]\n", function);
601
602         if (cc == NULL) {
603                 /*
604                  * This can happen when recursing in the VFS on the fsp when the
605                  * pre_next func noticed the recursion and set out cc pointer to
606                  * NULL.
607                  */
608                 return;
609         }
610
611         if (cc->busy != busy) {
612                 CATIA_DEBUG_CC(0, cc, fsp);
613                 smb_panic(__location__);
614                 return;
615         }
616
617         cc->busy = NULL;
618         *_cc = NULL;
619
620         fsp->fsp_name->base_name = cc->orig_fname;
621         if (fsp->base_fsp != NULL) {
622                 fsp->base_fsp->fsp_name->base_name = cc->orig_base_fname;
623         }
624
625         CATIA_DEBUG_CC(10, cc, fsp);
626
627         if (!cc->is_fsp_ext) {
628                 TALLOC_FREE(cc);
629         }
630
631         return;
632 }
633
634 static int catia_open(vfs_handle_struct *handle,
635                       struct smb_filename *smb_fname,
636                       files_struct *fsp,
637                       int flags,
638                       mode_t mode)
639 {
640         struct catia_cache *cc = NULL;
641         char *orig_smb_fname = smb_fname->base_name;
642         char *mapped_smb_fname = NULL;
643         NTSTATUS status;
644         int ret;
645
646         status = catia_string_replace_allocate(handle->conn,
647                                                smb_fname->base_name,
648                                                &mapped_smb_fname,
649                                                vfs_translate_to_unix);
650         if (!NT_STATUS_IS_OK(status)) {
651                 return -1;
652         }
653
654         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
655         if (ret != 0) {
656                 TALLOC_FREE(mapped_smb_fname);
657                 return ret;
658         }
659
660         smb_fname->base_name = mapped_smb_fname;
661         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
662         smb_fname->base_name = orig_smb_fname;
663
664         TALLOC_FREE(mapped_smb_fname);
665         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
666
667         return ret;
668 }
669
670 static int catia_rename(vfs_handle_struct *handle,
671                         const struct smb_filename *smb_fname_src,
672                         const struct smb_filename *smb_fname_dst)
673 {
674         TALLOC_CTX *ctx = talloc_tos();
675         struct smb_filename *smb_fname_src_tmp = NULL;
676         struct smb_filename *smb_fname_dst_tmp = NULL;
677         char *src_name_mapped = NULL;
678         char *dst_name_mapped = NULL;
679         NTSTATUS status;
680         int ret = -1;
681
682         status = catia_string_replace_allocate(handle->conn,
683                                 smb_fname_src->base_name,
684                                 &src_name_mapped, vfs_translate_to_unix);
685         if (!NT_STATUS_IS_OK(status)) {
686                 errno = map_errno_from_nt_status(status);
687                 return -1;
688         }
689
690         status = catia_string_replace_allocate(handle->conn,
691                                 smb_fname_dst->base_name,
692                                 &dst_name_mapped, vfs_translate_to_unix);
693         if (!NT_STATUS_IS_OK(status)) {
694                 errno = map_errno_from_nt_status(status);
695                 return -1;
696         }
697
698         /* Setup temporary smb_filename structs. */
699         smb_fname_src_tmp = cp_smb_filename(ctx, smb_fname_src);
700         if (smb_fname_src_tmp == NULL) {
701                 errno = ENOMEM;
702                 goto out;
703         }
704
705         smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
706         if (smb_fname_dst_tmp == NULL) {
707                 errno = ENOMEM;
708                 goto out;
709         }
710
711         smb_fname_src_tmp->base_name = src_name_mapped;
712         smb_fname_dst_tmp->base_name = dst_name_mapped; 
713         DEBUG(10, ("converted old name: %s\n",
714                                 smb_fname_str_dbg(smb_fname_src_tmp)));
715         DEBUG(10, ("converted new name: %s\n",
716                                 smb_fname_str_dbg(smb_fname_dst_tmp)));
717
718         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
719                         smb_fname_dst_tmp);
720 out:
721         TALLOC_FREE(src_name_mapped);
722         TALLOC_FREE(dst_name_mapped);
723         TALLOC_FREE(smb_fname_src_tmp);
724         TALLOC_FREE(smb_fname_dst_tmp);
725         return ret;
726 }
727
728 static int catia_stat(vfs_handle_struct *handle,
729                       struct smb_filename *smb_fname)
730 {
731         char *name = NULL;
732         char *tmp_base_name;
733         int ret;
734         NTSTATUS status;
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         tmp_base_name = smb_fname->base_name;
745         smb_fname->base_name = name;
746
747         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
748         smb_fname->base_name = tmp_base_name;
749
750         TALLOC_FREE(name);
751         return ret;
752 }
753
754 static int catia_lstat(vfs_handle_struct *handle,
755                        struct smb_filename *smb_fname)
756 {
757         char *name = NULL;
758         char *tmp_base_name;
759         int ret;
760         NTSTATUS status;
761
762         status = catia_string_replace_allocate(handle->conn,
763                                 smb_fname->base_name,
764                                 &name, vfs_translate_to_unix);
765         if (!NT_STATUS_IS_OK(status)) {
766                 errno = map_errno_from_nt_status(status);
767                 return -1;
768         }
769
770         tmp_base_name = smb_fname->base_name;
771         smb_fname->base_name = name;
772
773         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
774         smb_fname->base_name = tmp_base_name;
775         TALLOC_FREE(name);
776
777         return ret;
778 }
779
780 static int catia_unlink(vfs_handle_struct *handle,
781                         const struct smb_filename *smb_fname)
782 {
783         struct smb_filename *smb_fname_tmp = NULL;
784         char *name = NULL;
785         NTSTATUS status;
786         int ret;
787
788         status = catia_string_replace_allocate(handle->conn,
789                                         smb_fname->base_name,
790                                         &name, vfs_translate_to_unix);
791         if (!NT_STATUS_IS_OK(status)) {
792                 errno = map_errno_from_nt_status(status);
793                 return -1;
794         }
795
796         /* Setup temporary smb_filename structs. */
797         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
798         if (smb_fname_tmp == NULL) {
799                 errno = ENOMEM;
800                 return -1;
801         }
802
803         smb_fname_tmp->base_name = name;
804         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
805         TALLOC_FREE(smb_fname_tmp);
806         TALLOC_FREE(name);
807
808         return ret;
809 }
810
811 static int catia_chown(vfs_handle_struct *handle,
812                        const struct smb_filename *smb_fname,
813                        uid_t uid,
814                        gid_t gid)
815 {
816         char *name = NULL;
817         NTSTATUS status;
818         int ret;
819         int saved_errno;
820         struct smb_filename *catia_smb_fname = NULL;
821
822         status = catia_string_replace_allocate(handle->conn,
823                                         smb_fname->base_name,
824                                         &name,
825                                         vfs_translate_to_unix);
826         if (!NT_STATUS_IS_OK(status)) {
827                 errno = map_errno_from_nt_status(status);
828                 return -1;
829         }
830         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
831                                         name,
832                                         NULL,
833                                         NULL,
834                                         smb_fname->flags);
835         if (catia_smb_fname == NULL) {
836                 TALLOC_FREE(name);
837                 errno = ENOMEM;
838                 return -1;
839         }
840
841         ret = SMB_VFS_NEXT_CHOWN(handle, catia_smb_fname, uid, gid);
842         saved_errno = errno;
843         TALLOC_FREE(name);
844         TALLOC_FREE(catia_smb_fname);
845         errno = saved_errno;
846         return ret;
847 }
848
849 static int catia_lchown(vfs_handle_struct *handle,
850                         const struct smb_filename *smb_fname,
851                         uid_t uid,
852                         gid_t gid)
853 {
854         char *name = NULL;
855         NTSTATUS status;
856         int ret;
857         int saved_errno;
858         struct smb_filename *catia_smb_fname = NULL;
859
860         status = catia_string_replace_allocate(handle->conn,
861                                         smb_fname->base_name,
862                                         &name,
863                                         vfs_translate_to_unix);
864         if (!NT_STATUS_IS_OK(status)) {
865                 errno = map_errno_from_nt_status(status);
866                 return -1;
867         }
868         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
869                                         name,
870                                         NULL,
871                                         NULL,
872                                         smb_fname->flags);
873         if (catia_smb_fname == NULL) {
874                 TALLOC_FREE(name);
875                 errno = ENOMEM;
876                 return -1;
877         }
878
879         ret = SMB_VFS_NEXT_LCHOWN(handle, catia_smb_fname, uid, gid);
880         saved_errno = errno;
881         TALLOC_FREE(name);
882         TALLOC_FREE(catia_smb_fname);
883         errno = saved_errno;
884         return ret;
885 }
886
887 static int catia_chmod(vfs_handle_struct *handle,
888                         const struct smb_filename *smb_fname,
889                         mode_t mode)
890 {
891         char *name = NULL;
892         NTSTATUS status;
893         int ret;
894         int saved_errno;
895         struct smb_filename *catia_smb_fname = NULL;
896
897         status = catia_string_replace_allocate(handle->conn,
898                                         smb_fname->base_name,
899                                         &name,
900                                         vfs_translate_to_unix);
901         if (!NT_STATUS_IS_OK(status)) {
902                 errno = map_errno_from_nt_status(status);
903                 return -1;
904         }
905         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
906                                         name,
907                                         NULL,
908                                         NULL,
909                                         smb_fname->flags);
910         if (catia_smb_fname == NULL) {
911                 TALLOC_FREE(name);
912                 errno = ENOMEM;
913                 return -1;
914         }
915
916         ret = SMB_VFS_NEXT_CHMOD(handle, catia_smb_fname, mode);
917         saved_errno = errno;
918         TALLOC_FREE(name);
919         TALLOC_FREE(catia_smb_fname);
920         errno = saved_errno;
921         return ret;
922 }
923
924 static int catia_rmdir(vfs_handle_struct *handle,
925                        const struct smb_filename *smb_fname)
926 {
927         char *name = NULL;
928         NTSTATUS status;
929         int ret;
930         struct smb_filename *catia_smb_fname = NULL;
931
932         status = catia_string_replace_allocate(handle->conn,
933                                 smb_fname->base_name,
934                                 &name,
935                                 vfs_translate_to_unix);
936         if (!NT_STATUS_IS_OK(status)) {
937                 errno = map_errno_from_nt_status(status);
938                 return -1;
939         }
940         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
941                                         name,
942                                         NULL,
943                                         NULL,
944                                         smb_fname->flags);
945         if (catia_smb_fname == NULL) {
946                 TALLOC_FREE(name);
947                 errno = ENOMEM;
948                 return -1;
949         }
950
951         ret = SMB_VFS_NEXT_RMDIR(handle, catia_smb_fname);
952         TALLOC_FREE(name);
953         TALLOC_FREE(catia_smb_fname);
954
955         return ret;
956 }
957
958 static int catia_mkdir(vfs_handle_struct *handle,
959                        const struct smb_filename *smb_fname,
960                        mode_t mode)
961 {
962         char *name = NULL;
963         NTSTATUS status;
964         int ret;
965         struct smb_filename *catia_smb_fname = NULL;
966
967         status = catia_string_replace_allocate(handle->conn,
968                                 smb_fname->base_name,
969                                 &name,
970                                 vfs_translate_to_unix);
971         if (!NT_STATUS_IS_OK(status)) {
972                 errno = map_errno_from_nt_status(status);
973                 return -1;
974         }
975         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
976                                         name,
977                                         NULL,
978                                         NULL,
979                                         smb_fname->flags);
980         if (catia_smb_fname == NULL) {
981                 TALLOC_FREE(name);
982                 errno = ENOMEM;
983                 return -1;
984         }
985
986         ret = SMB_VFS_NEXT_MKDIR(handle, catia_smb_fname, mode);
987         TALLOC_FREE(name);
988         TALLOC_FREE(catia_smb_fname);
989
990         return ret;
991 }
992
993 static int catia_chdir(vfs_handle_struct *handle,
994                        const char *path)
995 {
996         char *name = NULL;
997         NTSTATUS status;
998         int ret;
999
1000         status = catia_string_replace_allocate(handle->conn, path,
1001                                         &name, vfs_translate_to_unix);
1002         if (!NT_STATUS_IS_OK(status)) {
1003                 errno = map_errno_from_nt_status(status);
1004                 return -1;
1005         }
1006
1007         ret = SMB_VFS_NEXT_CHDIR(handle, name);
1008         TALLOC_FREE(name);
1009
1010         return ret;
1011 }
1012
1013 static int catia_ntimes(vfs_handle_struct *handle,
1014                         const struct smb_filename *smb_fname,
1015                         struct smb_file_time *ft)
1016 {
1017         struct smb_filename *smb_fname_tmp = NULL;
1018         char *name = NULL;
1019         NTSTATUS status;
1020         int ret;
1021
1022         status = catia_string_replace_allocate(handle->conn,
1023                                 smb_fname->base_name,
1024                                 &name, vfs_translate_to_unix);
1025         if (!NT_STATUS_IS_OK(status)) {
1026                 errno = map_errno_from_nt_status(status);
1027                 return -1;
1028         }
1029
1030         smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
1031         if (smb_fname_tmp == NULL) {
1032                 errno = ENOMEM;
1033                 return -1;
1034         }
1035
1036         smb_fname_tmp->base_name = name;
1037         ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
1038         TALLOC_FREE(name);
1039         TALLOC_FREE(smb_fname_tmp);
1040
1041         return ret;
1042 }
1043
1044 static char *
1045 catia_realpath(vfs_handle_struct *handle, const char *path)
1046 {
1047         char *mapped_name = NULL;
1048         NTSTATUS status;
1049         char *ret = NULL;
1050
1051         status = catia_string_replace_allocate(handle->conn, path,
1052                                         &mapped_name, vfs_translate_to_unix);
1053         if (!NT_STATUS_IS_OK(status)) {
1054                 errno = map_errno_from_nt_status(status);
1055                 return NULL;
1056         }
1057
1058         ret = SMB_VFS_NEXT_REALPATH(handle, mapped_name);
1059         TALLOC_FREE(mapped_name);
1060
1061         return ret;
1062 }
1063
1064 static int catia_chflags(struct vfs_handle_struct *handle,
1065                          const char *path, unsigned int flags)
1066 {
1067         char *mapped_name = NULL;
1068         NTSTATUS status;
1069         int ret;
1070
1071         status = catia_string_replace_allocate(handle->conn, path,
1072                                         &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         ret = SMB_VFS_NEXT_CHFLAGS(handle, mapped_name, flags);
1079         TALLOC_FREE(mapped_name);
1080
1081         return ret;
1082 }
1083
1084 static NTSTATUS
1085 catia_streaminfo(struct vfs_handle_struct *handle,
1086                  struct files_struct *fsp,
1087                  const struct smb_filename *smb_fname,
1088                  TALLOC_CTX *mem_ctx,
1089                  unsigned int *_num_streams,
1090                  struct stream_struct **_streams)
1091 {
1092         char *mapped_name = NULL;
1093         NTSTATUS status;
1094         unsigned int i;
1095         struct smb_filename *catia_smb_fname = NULL;
1096         unsigned int num_streams = 0;
1097         struct stream_struct *streams = NULL;
1098
1099         *_num_streams = 0;
1100         *_streams = NULL;
1101
1102         status = catia_string_replace_allocate(handle->conn,
1103                                 smb_fname->base_name,
1104                                 &mapped_name,
1105                                 vfs_translate_to_unix);
1106         if (!NT_STATUS_IS_OK(status)) {
1107                 errno = map_errno_from_nt_status(status);
1108                 return status;
1109         }
1110
1111         catia_smb_fname = synthetic_smb_fname(talloc_tos(),
1112                                         mapped_name,
1113                                         NULL,
1114                                         NULL,
1115                                         smb_fname->flags);
1116         if (catia_smb_fname == NULL) {
1117                 TALLOC_FREE(mapped_name);
1118                 return NT_STATUS_NO_MEMORY;
1119         }
1120
1121         status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, catia_smb_fname,
1122                                          mem_ctx, &num_streams, &streams);
1123         TALLOC_FREE(mapped_name);
1124         TALLOC_FREE(catia_smb_fname);
1125         if (!NT_STATUS_IS_OK(status)) {
1126                 return status;
1127         }
1128
1129         /*
1130          * Translate stream names just like the base names
1131          */
1132         for (i = 0; i < num_streams; i++) {
1133                 /*
1134                  * Strip ":" prefix and ":$DATA" suffix to get a
1135                  * "pure" stream name and only translate that.
1136                  */
1137                 void *old_ptr = streams[i].name;
1138                 char *stream_name = streams[i].name + 1;
1139                 char *stream_type = strrchr_m(stream_name, ':');
1140
1141                 if (stream_type != NULL) {
1142                         *stream_type = '\0';
1143                         stream_type += 1;
1144                 }
1145
1146                 status = catia_string_replace_allocate(handle->conn, stream_name,
1147                                                        &mapped_name, vfs_translate_to_windows);
1148                 if (!NT_STATUS_IS_OK(status)) {
1149                         TALLOC_FREE(streams);
1150                         return status;
1151                 }
1152
1153                 if (stream_type != NULL) {
1154                         streams[i].name = talloc_asprintf(streams, ":%s:%s",
1155                                                           mapped_name, stream_type);
1156                 } else {
1157                         streams[i].name = talloc_asprintf(streams, ":%s",
1158                                                           mapped_name);
1159                 }
1160                 TALLOC_FREE(mapped_name);
1161                 TALLOC_FREE(old_ptr);
1162                 if (streams[i].name == NULL) {
1163                         TALLOC_FREE(streams);
1164                         return NT_STATUS_NO_MEMORY;
1165                 }
1166         }
1167
1168         *_num_streams = num_streams;
1169         *_streams = streams;
1170         return NT_STATUS_OK;
1171 }
1172
1173 static NTSTATUS
1174 catia_get_nt_acl(struct vfs_handle_struct *handle,
1175                  const struct smb_filename *smb_fname,
1176                  uint32_t security_info,
1177                  TALLOC_CTX *mem_ctx,
1178                  struct security_descriptor **ppdesc)
1179 {
1180         char *mapped_name = NULL;
1181         const char *path = smb_fname->base_name;
1182         struct smb_filename *mapped_smb_fname = NULL;
1183         NTSTATUS status;
1184
1185         status = catia_string_replace_allocate(handle->conn,
1186                                 path, &mapped_name, vfs_translate_to_unix);
1187         if (!NT_STATUS_IS_OK(status)) {
1188                 errno = map_errno_from_nt_status(status);
1189                 return status;
1190         }
1191         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
1192                                         mapped_name,
1193                                         NULL,
1194                                         NULL,
1195                                         smb_fname->flags);
1196         if (mapped_smb_fname == NULL) {
1197                 TALLOC_FREE(mapped_name);
1198                 return NT_STATUS_NO_MEMORY;
1199         }
1200
1201         status = SMB_VFS_NEXT_GET_NT_ACL(handle, mapped_smb_fname,
1202                                          security_info, mem_ctx, ppdesc);
1203         TALLOC_FREE(mapped_name);
1204         TALLOC_FREE(mapped_smb_fname);
1205
1206         return status;
1207 }
1208
1209 static int
1210 catia_chmod_acl(vfs_handle_struct *handle,
1211                 const struct smb_filename *smb_fname,
1212                 mode_t mode)
1213 {
1214         char *mapped_name = NULL;
1215         struct smb_filename *mapped_smb_fname = NULL;
1216         NTSTATUS status;
1217         int ret;
1218         int saved_errno;
1219
1220         status = catia_string_replace_allocate(handle->conn,
1221                                 smb_fname->base_name,
1222                                 &mapped_name,
1223                                 vfs_translate_to_unix);
1224         if (!NT_STATUS_IS_OK(status)) {
1225                 errno = map_errno_from_nt_status(status);
1226                 return -1;
1227         }
1228
1229         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
1230                                         mapped_name,
1231                                         NULL,
1232                                         NULL,
1233                                         smb_fname->flags);
1234         if (mapped_smb_fname == NULL) {
1235                 TALLOC_FREE(mapped_name);
1236                 errno = ENOMEM;
1237                 return -1;
1238         }
1239         ret = SMB_VFS_NEXT_CHMOD_ACL(handle, mapped_smb_fname, mode);
1240         saved_errno = errno;
1241         TALLOC_FREE(mapped_name);
1242         TALLOC_FREE(mapped_smb_fname);
1243         errno = saved_errno;
1244         return ret;
1245 }
1246
1247 static SMB_ACL_T
1248 catia_sys_acl_get_file(vfs_handle_struct *handle,
1249                        const char *path,
1250                        SMB_ACL_TYPE_T type,
1251                        TALLOC_CTX *mem_ctx)
1252 {
1253         char *mapped_name = NULL;
1254         NTSTATUS status;
1255         SMB_ACL_T ret;
1256
1257         status = catia_string_replace_allocate(handle->conn,
1258                                 path, &mapped_name, vfs_translate_to_unix);
1259         if (!NT_STATUS_IS_OK(status)) {
1260                 errno = map_errno_from_nt_status(status);
1261                 return NULL;
1262         }
1263
1264         ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, mapped_name, type, mem_ctx);
1265         TALLOC_FREE(mapped_name);
1266
1267         return ret;
1268 }
1269
1270 static int
1271 catia_sys_acl_set_file(vfs_handle_struct *handle,
1272                        const char *path,
1273                        SMB_ACL_TYPE_T type,
1274                        SMB_ACL_T theacl)
1275 {
1276         char *mapped_name = NULL;
1277         NTSTATUS status;
1278         int ret;
1279
1280         status = catia_string_replace_allocate(handle->conn,
1281                                 path, &mapped_name, vfs_translate_to_unix);
1282         if (!NT_STATUS_IS_OK(status)) {
1283                 errno = map_errno_from_nt_status(status);
1284                 return -1;
1285         }
1286
1287         ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, mapped_name, type, theacl);
1288         TALLOC_FREE(mapped_name);
1289
1290         return ret;
1291 }
1292
1293 static int
1294 catia_sys_acl_delete_def_file(vfs_handle_struct *handle,
1295                               const char *path)
1296 {
1297         char *mapped_name = NULL;
1298         NTSTATUS status;
1299         int ret;
1300
1301         status = catia_string_replace_allocate(handle->conn,
1302                                 path, &mapped_name, vfs_translate_to_unix);
1303         if (!NT_STATUS_IS_OK(status)) {
1304                 errno = map_errno_from_nt_status(status);
1305                 return -1;
1306         }
1307
1308         ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, mapped_name);
1309         TALLOC_FREE(mapped_name);
1310
1311         return ret;
1312 }
1313
1314 static ssize_t
1315 catia_getxattr(vfs_handle_struct *handle, const char *path,
1316                const char *name, void *value, size_t size)
1317 {
1318         char *mapped_name = NULL;
1319         NTSTATUS status;
1320         ssize_t ret;
1321
1322         status = catia_string_replace_allocate(handle->conn,
1323                                 name, &mapped_name, vfs_translate_to_unix);
1324         if (!NT_STATUS_IS_OK(status)) {
1325                 errno = map_errno_from_nt_status(status);
1326                 return -1;
1327         }
1328
1329
1330         ret = SMB_VFS_NEXT_GETXATTR(handle, path, mapped_name, value, size);
1331         TALLOC_FREE(mapped_name);
1332
1333         return ret;
1334 }
1335
1336 static ssize_t
1337 catia_listxattr(vfs_handle_struct *handle, const char *path,
1338                 char *list, size_t size)
1339 {
1340         char *mapped_name = NULL;
1341         NTSTATUS status;
1342         ssize_t ret;
1343
1344         status = catia_string_replace_allocate(handle->conn,
1345                                 path, &mapped_name, vfs_translate_to_unix);
1346         if (!NT_STATUS_IS_OK(status)) {
1347                 errno = map_errno_from_nt_status(status);
1348                 return -1;
1349         }
1350
1351
1352         ret = SMB_VFS_NEXT_LISTXATTR(handle, mapped_name, list, size);
1353         TALLOC_FREE(mapped_name);
1354
1355         return ret;
1356 }
1357
1358 static int
1359 catia_removexattr(vfs_handle_struct *handle, const char *path,
1360                   const char *name)
1361 {
1362         char *mapped_name = NULL;
1363         NTSTATUS status;
1364         ssize_t ret;
1365
1366         status = catia_string_replace_allocate(handle->conn,
1367                                 name, &mapped_name, vfs_translate_to_unix);
1368         if (!NT_STATUS_IS_OK(status)) {
1369                 errno = map_errno_from_nt_status(status);
1370                 return -1;
1371         }
1372
1373
1374         ret = SMB_VFS_NEXT_REMOVEXATTR(handle, path, mapped_name);
1375         TALLOC_FREE(mapped_name);
1376
1377         return ret;
1378 }
1379
1380 static int
1381 catia_setxattr(vfs_handle_struct *handle, const char *path,
1382                const char *name, const void *value, size_t size,
1383                int flags)
1384 {
1385         char *mapped_name = NULL;
1386         NTSTATUS status;
1387         ssize_t ret;
1388
1389         status = catia_string_replace_allocate(handle->conn,
1390                                 name, &mapped_name, vfs_translate_to_unix);
1391         if (!NT_STATUS_IS_OK(status)) {
1392                 errno = map_errno_from_nt_status(status);
1393                 return -1;
1394         }
1395
1396
1397         ret = SMB_VFS_NEXT_SETXATTR(handle, path, mapped_name, value, size, flags);
1398         TALLOC_FREE(mapped_name);
1399
1400         return ret;
1401 }
1402
1403 static int catia_fstat(vfs_handle_struct *handle,
1404                        files_struct *fsp,
1405                        SMB_STRUCT_STAT *sbuf)
1406 {
1407         struct catia_cache *cc = NULL;
1408         int ret;
1409
1410         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1411         if (ret != 0) {
1412                 return ret;
1413         }
1414
1415         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
1416
1417         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1418
1419         return ret;
1420 }
1421
1422 static ssize_t catia_pread(vfs_handle_struct *handle,
1423                            files_struct *fsp, void *data,
1424                            size_t n, off_t offset)
1425 {
1426         struct catia_cache *cc = NULL;
1427         ssize_t result;
1428         int ret;
1429
1430         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1431         if (ret != 0) {
1432                 return ret;
1433         }
1434
1435         result = SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1436
1437         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1438
1439         return result;
1440 }
1441
1442 static ssize_t catia_pwrite(vfs_handle_struct *handle,
1443                             files_struct *fsp, const void *data,
1444                             size_t n, off_t offset)
1445 {
1446         struct catia_cache *cc = NULL;
1447         ssize_t result;
1448         int ret;
1449
1450         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1451         if (ret != 0) {
1452                 return ret;
1453         }
1454
1455         result = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
1456
1457         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1458
1459         return result;
1460 }
1461
1462 static int catia_ftruncate(struct vfs_handle_struct *handle,
1463                            struct files_struct *fsp,
1464                            off_t offset)
1465 {
1466         struct catia_cache *cc = NULL;
1467         int ret;
1468
1469         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1470         if (ret != 0) {
1471                 return ret;
1472         }
1473
1474         ret = SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1475
1476         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1477
1478         return ret;
1479 }
1480
1481 static int catia_fallocate(struct vfs_handle_struct *handle,
1482                            struct files_struct *fsp,
1483                            uint32_t mode,
1484                            off_t offset,
1485                            off_t len)
1486 {
1487         struct catia_cache *cc = NULL;
1488         int ret;
1489
1490         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1491         if (ret != 0) {
1492                 return ret;
1493         }
1494
1495         ret = SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1496
1497         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1498
1499         return ret;
1500 }
1501
1502 static ssize_t catia_fgetxattr(struct vfs_handle_struct *handle,
1503                                struct files_struct *fsp,
1504                                const char *name,
1505                                void *value,
1506                                size_t size)
1507 {
1508         char *mapped_xattr_name = NULL;
1509         NTSTATUS status;
1510         ssize_t result;
1511
1512         status = catia_string_replace_allocate(handle->conn,
1513                                                name, &mapped_xattr_name,
1514                                                vfs_translate_to_unix);
1515         if (!NT_STATUS_IS_OK(status)) {
1516                 errno = map_errno_from_nt_status(status);
1517                 return -1;
1518         }
1519
1520         result = SMB_VFS_NEXT_FGETXATTR(handle, fsp, mapped_xattr_name,
1521                                         value, size);
1522
1523         TALLOC_FREE(mapped_xattr_name);
1524
1525         return result;
1526 }
1527
1528 static ssize_t catia_flistxattr(struct vfs_handle_struct *handle,
1529                                 struct files_struct *fsp,
1530                                 char *list,
1531                                 size_t size)
1532 {
1533         struct catia_cache *cc = NULL;
1534         ssize_t result;
1535         int ret;
1536
1537         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1538         if (ret != 0) {
1539                 return ret;
1540         }
1541
1542         result = SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1543
1544         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1545
1546         return result;
1547 }
1548
1549 static int catia_fremovexattr(struct vfs_handle_struct *handle,
1550                               struct files_struct *fsp,
1551                               const char *name)
1552 {
1553         char *mapped_name = NULL;
1554         NTSTATUS status;
1555         int ret;
1556
1557         status = catia_string_replace_allocate(handle->conn,
1558                                 name, &mapped_name, vfs_translate_to_unix);
1559         if (!NT_STATUS_IS_OK(status)) {
1560                 errno = map_errno_from_nt_status(status);
1561                 return -1;
1562         }
1563
1564         ret = SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, mapped_name);
1565
1566         TALLOC_FREE(mapped_name);
1567
1568         return ret;
1569 }
1570
1571 static int catia_fsetxattr(struct vfs_handle_struct *handle,
1572                            struct files_struct *fsp,
1573                            const char *name,
1574                            const void *value,
1575                            size_t size,
1576                            int flags)
1577 {
1578         char *mapped_xattr_name = NULL;
1579         NTSTATUS status;
1580         int ret;
1581
1582         status = catia_string_replace_allocate(
1583                 handle->conn, name, &mapped_xattr_name, vfs_translate_to_unix);
1584         if (!NT_STATUS_IS_OK(status)) {
1585                 errno = map_errno_from_nt_status(status);
1586                 return -1;
1587         }
1588
1589         ret = SMB_VFS_NEXT_FSETXATTR(handle, fsp, mapped_xattr_name,
1590                                      value, size, flags);
1591
1592         TALLOC_FREE(mapped_xattr_name);
1593
1594         return ret;
1595 }
1596
1597 static SMB_ACL_T catia_sys_acl_get_fd(vfs_handle_struct *handle,
1598                                       files_struct *fsp,
1599                                       TALLOC_CTX *mem_ctx)
1600 {
1601         struct catia_cache *cc = NULL;
1602         struct smb_acl_t *result = NULL;
1603         int ret;
1604
1605         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1606         if (ret != 0) {
1607                 return NULL;
1608         }
1609
1610         result = SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1611
1612         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1613
1614         return result;
1615 }
1616
1617 static int catia_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1618                                      files_struct *fsp,
1619                                      TALLOC_CTX *mem_ctx,
1620                                      char **blob_description,
1621                                      DATA_BLOB *blob)
1622 {
1623         struct catia_cache *cc = NULL;
1624         int ret;
1625
1626         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1627         if (ret != 0) {
1628                 return ret;
1629         }
1630
1631         ret = SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1632                                                blob_description, blob);
1633
1634         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1635
1636         return ret;
1637 }
1638
1639 static int catia_sys_acl_set_fd(vfs_handle_struct *handle,
1640                                 files_struct *fsp,
1641                                 SMB_ACL_T theacl)
1642 {
1643         struct catia_cache *cc = NULL;
1644         int ret;
1645
1646         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1647         if (ret != 0) {
1648                 return ret;
1649         }
1650
1651         ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1652
1653         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1654
1655         return ret;
1656 }
1657
1658 static int catia_fchmod_acl(vfs_handle_struct *handle,
1659                             files_struct *fsp,
1660                             mode_t mode)
1661 {
1662         struct catia_cache *cc = NULL;
1663         int ret;
1664
1665         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1666         if (ret != 0) {
1667                 return ret;
1668         }
1669
1670         ret = SMB_VFS_NEXT_FCHMOD_ACL(handle, fsp, mode);
1671
1672         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1673
1674         return ret;
1675 }
1676
1677 static NTSTATUS catia_fget_nt_acl(vfs_handle_struct *handle,
1678                                   files_struct *fsp,
1679                                   uint32_t security_info,
1680                                   TALLOC_CTX *mem_ctx,
1681                                   struct security_descriptor **ppdesc)
1682 {
1683         struct catia_cache *cc = NULL;
1684         NTSTATUS status;
1685         int ret;
1686
1687         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1688         if (ret != 0) {
1689                 return map_nt_error_from_unix(errno);
1690         }
1691
1692         status = SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1693                                           mem_ctx, ppdesc);
1694
1695         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1696
1697         return status;
1698 }
1699
1700 static NTSTATUS catia_fset_nt_acl(vfs_handle_struct *handle,
1701                                   files_struct *fsp,
1702                                   uint32_t security_info_sent,
1703                                   const struct security_descriptor *psd)
1704 {
1705         struct catia_cache *cc = NULL;
1706         NTSTATUS status;
1707         int ret;
1708
1709         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1710         if (ret != 0) {
1711                 return map_nt_error_from_unix(errno);
1712         }
1713
1714         status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
1715
1716         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1717
1718         return status;
1719 }
1720
1721 static NTSTATUS catia_fset_dos_attributes(struct vfs_handle_struct *handle,
1722                                           struct files_struct *fsp,
1723                                           uint32_t dosmode)
1724 {
1725         struct catia_cache *cc = NULL;
1726         NTSTATUS status;
1727         int ret;
1728
1729         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1730         if (ret != 0) {
1731                 return map_nt_error_from_unix(errno);
1732         }
1733
1734         status = SMB_VFS_NEXT_FSET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1735
1736         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1737
1738         return status;
1739 }
1740
1741 static NTSTATUS catia_fget_dos_attributes(struct vfs_handle_struct *handle,
1742                                           struct files_struct *fsp,
1743                                           uint32_t *dosmode)
1744 {
1745         struct catia_cache *cc = NULL;
1746         NTSTATUS status;
1747         int ret;
1748
1749         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1750         if (ret != 0) {
1751                 return map_nt_error_from_unix(errno);
1752         }
1753
1754         status = SMB_VFS_NEXT_FGET_DOS_ATTRIBUTES(handle, fsp, dosmode);
1755
1756         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1757
1758         return status;
1759 }
1760
1761 static int catia_fchown(vfs_handle_struct *handle,
1762                         files_struct *fsp,
1763                         uid_t uid,
1764                         gid_t gid)
1765 {
1766         struct catia_cache *cc = NULL;
1767         int ret;
1768
1769         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1770         if (ret != 0) {
1771                 return ret;
1772         }
1773
1774         ret = SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1775
1776         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1777
1778         return ret;
1779 }
1780
1781 static int catia_fchmod(vfs_handle_struct *handle,
1782                         files_struct *fsp,
1783                         mode_t mode)
1784 {
1785         struct catia_cache *cc = NULL;
1786         int ret;
1787
1788         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1789         if (ret != 0) {
1790                 return ret;
1791         }
1792
1793         ret = SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1794
1795         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1796
1797         return ret;
1798 }
1799
1800 struct catia_pread_state {
1801         ssize_t ret;
1802         struct vfs_aio_state vfs_aio_state;
1803         struct files_struct *fsp;
1804         struct catia_cache *cc;
1805 };
1806
1807 static void catia_pread_done(struct tevent_req *subreq);
1808
1809 static struct tevent_req *catia_pread_send(struct vfs_handle_struct *handle,
1810                                            TALLOC_CTX *mem_ctx,
1811                                            struct tevent_context *ev,
1812                                            struct files_struct *fsp,
1813                                            void *data,
1814                                            size_t n,
1815                                            off_t offset)
1816 {
1817         struct tevent_req *req = NULL, *subreq = NULL;
1818         struct catia_pread_state *state = NULL;
1819         int ret;
1820
1821         req = tevent_req_create(mem_ctx, &state,
1822                                 struct catia_pread_state);
1823         if (req == NULL) {
1824                 return NULL;
1825         }
1826         state->fsp = fsp;
1827
1828         ret = CATIA_FETCH_FSP_PRE_NEXT(state, handle, fsp, &state->cc);
1829         if (ret != 0) {
1830                 tevent_req_error(req, errno);
1831                 return tevent_req_post(req, ev);
1832         }
1833
1834         subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp, data,
1835                                          n, offset);
1836         if (tevent_req_nomem(subreq, req)) {
1837                 return tevent_req_post(req, ev);
1838         }
1839         tevent_req_set_callback(subreq, catia_pread_done, req);
1840
1841         return req;
1842 }
1843
1844 static void catia_pread_done(struct tevent_req *subreq)
1845 {
1846         struct tevent_req *req = tevent_req_callback_data(
1847                 subreq, struct tevent_req);
1848         struct catia_pread_state *state = tevent_req_data(
1849                 req, struct catia_pread_state);
1850
1851         state->ret = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1852         TALLOC_FREE(subreq);
1853
1854         CATIA_FETCH_FSP_POST_NEXT(&state->cc, state->fsp);
1855
1856         tevent_req_done(req);
1857 }
1858
1859 static ssize_t catia_pread_recv(struct tevent_req *req,
1860                                 struct vfs_aio_state *vfs_aio_state)
1861 {
1862         struct catia_pread_state *state = tevent_req_data(
1863                 req, struct catia_pread_state);
1864
1865         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1866                 return -1;
1867         }
1868
1869         *vfs_aio_state = state->vfs_aio_state;
1870         return state->ret;
1871 }
1872
1873 struct catia_pwrite_state {
1874         ssize_t ret;
1875         struct vfs_aio_state vfs_aio_state;
1876         struct files_struct *fsp;
1877         struct catia_cache *cc;
1878 };
1879
1880 static void catia_pwrite_done(struct tevent_req *subreq);
1881
1882 static struct tevent_req *catia_pwrite_send(struct vfs_handle_struct *handle,
1883                                             TALLOC_CTX *mem_ctx,
1884                                             struct tevent_context *ev,
1885                                             struct files_struct *fsp,
1886                                             const void *data,
1887                                             size_t n,
1888                                             off_t offset)
1889 {
1890         struct tevent_req *req = NULL, *subreq = NULL;
1891         struct catia_pwrite_state *state = NULL;
1892         int ret;
1893
1894         req = tevent_req_create(mem_ctx, &state,
1895                                 struct catia_pwrite_state);
1896         if (req == NULL) {
1897                 return NULL;
1898         }
1899         state->fsp = fsp;
1900
1901         ret = CATIA_FETCH_FSP_PRE_NEXT(state, handle, fsp, &state->cc);
1902         if (ret != 0) {
1903                 tevent_req_error(req, errno);
1904                 return tevent_req_post(req, ev);
1905         }
1906
1907         subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp, data,
1908                                           n, offset);
1909         if (tevent_req_nomem(subreq, req)) {
1910                 return tevent_req_post(req, ev);
1911         }
1912         tevent_req_set_callback(subreq, catia_pwrite_done, req);
1913
1914         return req;
1915 }
1916
1917 static void catia_pwrite_done(struct tevent_req *subreq)
1918 {
1919         struct tevent_req *req = tevent_req_callback_data(
1920                 subreq, struct tevent_req);
1921         struct catia_pwrite_state *state = tevent_req_data(
1922                 req, struct catia_pwrite_state);
1923
1924         state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1925         TALLOC_FREE(subreq);
1926
1927         CATIA_FETCH_FSP_POST_NEXT(&state->cc, state->fsp);
1928
1929         tevent_req_done(req);
1930 }
1931
1932 static ssize_t catia_pwrite_recv(struct tevent_req *req,
1933                                 struct vfs_aio_state *vfs_aio_state)
1934 {
1935         struct catia_pwrite_state *state = tevent_req_data(
1936                 req, struct catia_pwrite_state);
1937
1938         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1939                 return -1;
1940         }
1941
1942         *vfs_aio_state = state->vfs_aio_state;
1943         return state->ret;
1944 }
1945
1946 static off_t catia_lseek(vfs_handle_struct *handle,
1947                          files_struct *fsp,
1948                          off_t offset,
1949                          int whence)
1950 {
1951         struct catia_cache *cc = NULL;
1952         ssize_t result;
1953         int ret;
1954
1955         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1956         if (ret != 0) {
1957                 return -1;
1958         }
1959
1960         result = SMB_VFS_NEXT_LSEEK(handle, fsp, offset, whence);
1961
1962         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1963
1964         return result;
1965 }
1966
1967 static int catia_fsync(vfs_handle_struct *handle, files_struct *fsp)
1968 {
1969         struct catia_cache *cc = NULL;
1970         int ret;
1971
1972         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
1973         if (ret != 0) {
1974                 return -1;
1975         }
1976
1977         ret = SMB_VFS_NEXT_FSYNC(handle, fsp);
1978
1979         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
1980
1981         return ret;
1982 }
1983
1984 struct catia_fsync_state {
1985         int ret;
1986         struct vfs_aio_state vfs_aio_state;
1987         struct files_struct *fsp;
1988         struct catia_cache *cc;
1989 };
1990
1991 static void catia_fsync_done(struct tevent_req *subreq);
1992
1993 static struct tevent_req *catia_fsync_send(struct vfs_handle_struct *handle,
1994                                            TALLOC_CTX *mem_ctx,
1995                                            struct tevent_context *ev,
1996                                            struct files_struct *fsp)
1997 {
1998         struct tevent_req *req = NULL, *subreq = NULL;
1999         struct catia_fsync_state *state = NULL;
2000         int ret;
2001
2002         req = tevent_req_create(mem_ctx, &state,
2003                                 struct catia_fsync_state);
2004         if (req == NULL) {
2005                 return NULL;
2006         }
2007         state->fsp = fsp;
2008
2009         ret = CATIA_FETCH_FSP_PRE_NEXT(state, handle, fsp, &state->cc);
2010         if (ret != 0) {
2011                 tevent_req_error(req, errno);
2012                 return tevent_req_post(req, ev);
2013         }
2014
2015         subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
2016         if (tevent_req_nomem(subreq, req)) {
2017                 return tevent_req_post(req, ev);
2018         }
2019         tevent_req_set_callback(subreq, catia_fsync_done, req);
2020
2021         return req;
2022 }
2023
2024 static void catia_fsync_done(struct tevent_req *subreq)
2025 {
2026         struct tevent_req *req = tevent_req_callback_data(
2027                 subreq, struct tevent_req);
2028         struct catia_fsync_state *state = tevent_req_data(
2029                 req, struct catia_fsync_state);
2030
2031         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
2032         TALLOC_FREE(subreq);
2033
2034         CATIA_FETCH_FSP_POST_NEXT(&state->cc, state->fsp);
2035
2036         tevent_req_done(req);
2037 }
2038
2039 static int catia_fsync_recv(struct tevent_req *req,
2040                             struct vfs_aio_state *vfs_aio_state)
2041 {
2042         struct catia_fsync_state *state = tevent_req_data(
2043                 req, struct catia_fsync_state);
2044
2045         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
2046                 return -1;
2047         }
2048
2049         *vfs_aio_state = state->vfs_aio_state;
2050         return state->ret;
2051 }
2052
2053 static bool catia_lock(vfs_handle_struct *handle,
2054                        files_struct *fsp,
2055                        int op,
2056                        off_t offset,
2057                        off_t count,
2058                        int type)
2059 {
2060         struct catia_cache *cc = NULL;
2061         bool ok;
2062         int ret;
2063
2064         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2065         if (ret != 0) {
2066                 return -1;
2067         }
2068
2069         ok = SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
2070
2071         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2072
2073         return ok;
2074 }
2075
2076 static int catia_kernel_flock(struct vfs_handle_struct *handle,
2077                               struct files_struct *fsp,
2078                               uint32_t share_mode,
2079                               uint32_t access_mask)
2080 {
2081         struct catia_cache *cc = NULL;
2082         int ret;
2083
2084         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2085         if (ret != 0) {
2086                 return -1;
2087         }
2088
2089         ret = SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp, share_mode, access_mask);
2090
2091         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2092
2093         return ret;
2094 }
2095
2096 static int catia_linux_setlease(vfs_handle_struct *handle,
2097                                 files_struct *fsp,
2098                                 int leasetype)
2099 {
2100         struct catia_cache *cc = NULL;
2101         int ret;
2102
2103         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2104         if (ret != 0) {
2105                 return -1;
2106         }
2107
2108         ret = SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
2109
2110         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2111
2112         return ret;
2113 }
2114
2115 static bool catia_getlock(vfs_handle_struct *handle,
2116                           files_struct *fsp,
2117                           off_t *poffset,
2118                           off_t *pcount,
2119                           int *ptype,
2120                           pid_t *ppid)
2121 {
2122         struct catia_cache *cc = NULL;
2123         int ret;
2124         bool ok;
2125
2126         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2127         if (ret != 0) {
2128                 return -1;
2129         }
2130
2131         ok = SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset, pcount, ptype, ppid);
2132
2133         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2134
2135         return ok;
2136 }
2137
2138 static bool catia_strict_lock(struct vfs_handle_struct *handle,
2139                               struct files_struct *fsp,
2140                               struct lock_struct *plock)
2141 {
2142         struct catia_cache *cc = NULL;
2143         int ret;
2144         bool ok;
2145
2146         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2147         if (ret != 0) {
2148                 return -1;
2149         }
2150
2151         ok = SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
2152
2153         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2154
2155         return ok;
2156 }
2157
2158 static void catia_strict_unlock(struct vfs_handle_struct *handle,
2159                                 struct files_struct *fsp,
2160                                 struct lock_struct *plock)
2161 {
2162         struct catia_cache *cc = NULL;
2163         int ret;
2164
2165         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2166         if (ret != 0) {
2167                 smb_panic("CATIA_FETCH_FSP_PRE_NEXT failed\n");
2168         }
2169
2170         SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
2171
2172         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2173 }
2174
2175 static NTSTATUS catia_fsctl(struct vfs_handle_struct *handle,
2176                             struct files_struct *fsp,
2177                             TALLOC_CTX *ctx,
2178                             uint32_t function,
2179                             uint16_t req_flags,
2180                             const uint8_t *_in_data,
2181                             uint32_t in_len,
2182                             uint8_t **_out_data,
2183                             uint32_t max_out_len,
2184                             uint32_t *out_len)
2185 {
2186         NTSTATUS result;
2187         struct catia_cache *cc = NULL;
2188         int ret;
2189
2190         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2191         if (ret != 0) {
2192                 return map_nt_error_from_unix(errno);
2193         }
2194
2195         result = SMB_VFS_NEXT_FSCTL(handle,
2196                                 fsp,
2197                                 ctx,
2198                                 function,
2199                                 req_flags,
2200                                 _in_data,
2201                                 in_len,
2202                                 _out_data,
2203                                 max_out_len,
2204                                 out_len);
2205
2206         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2207
2208         return result;
2209 }
2210
2211 static NTSTATUS catia_get_compression(vfs_handle_struct *handle,
2212                                       TALLOC_CTX *mem_ctx,
2213                                       struct files_struct *fsp,
2214                                       struct smb_filename *smb_fname,
2215                                       uint16_t *_compression_fmt)
2216 {
2217         NTSTATUS result;
2218         struct catia_cache *cc = NULL;
2219         int ret;
2220
2221         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2222         if (ret != 0) {
2223                 return map_nt_error_from_unix(errno);
2224         }
2225
2226         result = SMB_VFS_NEXT_GET_COMPRESSION(handle, mem_ctx, fsp, smb_fname,
2227                                               _compression_fmt);
2228
2229         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2230
2231         return result;
2232 }
2233
2234 static NTSTATUS catia_set_compression(vfs_handle_struct *handle,
2235                                       TALLOC_CTX *mem_ctx,
2236                                       struct files_struct *fsp,
2237                                       uint16_t compression_fmt)
2238 {
2239         NTSTATUS result;
2240         struct catia_cache *cc = NULL;
2241         int ret;
2242
2243         ret = CATIA_FETCH_FSP_PRE_NEXT(talloc_tos(), handle, fsp, &cc);
2244         if (ret != 0) {
2245                 return map_nt_error_from_unix(errno);
2246         }
2247
2248         result = SMB_VFS_NEXT_SET_COMPRESSION(handle, mem_ctx, fsp,
2249                                               compression_fmt);
2250
2251         CATIA_FETCH_FSP_POST_NEXT(&cc, fsp);
2252
2253         return result;
2254 }
2255
2256 static NTSTATUS catia_readdir_attr(struct vfs_handle_struct *handle,
2257                                    const struct smb_filename *smb_fname_in,
2258                                    TALLOC_CTX *mem_ctx,
2259                                    struct readdir_attr_data **pattr_data)
2260 {
2261         struct smb_filename *smb_fname;
2262         char *fname = NULL;
2263         NTSTATUS status;
2264
2265         status = catia_string_replace_allocate(handle->conn,
2266                                                smb_fname_in->base_name,
2267                                                &fname,
2268                                                vfs_translate_to_unix);
2269         if (!NT_STATUS_IS_OK(status)) {
2270                 errno = map_errno_from_nt_status(status);
2271                 return status;
2272         }
2273
2274         smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
2275                                         &smb_fname_in->st, 0);
2276
2277         status = SMB_VFS_NEXT_READDIR_ATTR(handle, smb_fname, mem_ctx, pattr_data);
2278
2279         TALLOC_FREE(smb_fname);
2280         return status;
2281 }
2282
2283 static NTSTATUS catia_get_dos_attributes(struct vfs_handle_struct *handle,
2284                                          struct smb_filename *smb_fname,
2285                                          uint32_t *dosmode)
2286 {
2287         char *mapped_name = NULL;
2288         const char *path = smb_fname->base_name;
2289         struct smb_filename *mapped_smb_fname = NULL;
2290         NTSTATUS status;
2291
2292         status = catia_string_replace_allocate(handle->conn,
2293                                 path, &mapped_name, vfs_translate_to_unix);
2294         if (!NT_STATUS_IS_OK(status)) {
2295                 errno = map_errno_from_nt_status(status);
2296                 return status;
2297         }
2298         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
2299                                         mapped_name,
2300                                         NULL,
2301                                         NULL,
2302                                         smb_fname->flags);
2303         if (mapped_smb_fname == NULL) {
2304                 TALLOC_FREE(mapped_name);
2305                 return NT_STATUS_NO_MEMORY;
2306         }
2307
2308         status = SMB_VFS_NEXT_GET_DOS_ATTRIBUTES(handle,
2309                                                  mapped_smb_fname,
2310                                                  dosmode);
2311         TALLOC_FREE(mapped_name);
2312         TALLOC_FREE(mapped_smb_fname);
2313
2314         return status;
2315 }
2316
2317 static NTSTATUS catia_set_dos_attributes(struct vfs_handle_struct *handle,
2318                                          const struct smb_filename *smb_fname,
2319                                          uint32_t dosmode)
2320 {
2321         char *mapped_name = NULL;
2322         const char *path = smb_fname->base_name;
2323         struct smb_filename *mapped_smb_fname = NULL;
2324         NTSTATUS status;
2325
2326         status = catia_string_replace_allocate(handle->conn,
2327                                 path, &mapped_name, vfs_translate_to_unix);
2328         if (!NT_STATUS_IS_OK(status)) {
2329                 errno = map_errno_from_nt_status(status);
2330                 return status;
2331         }
2332         mapped_smb_fname = synthetic_smb_fname(talloc_tos(),
2333                                         mapped_name,
2334                                         NULL,
2335                                         NULL,
2336                                         smb_fname->flags);
2337         if (mapped_smb_fname == NULL) {
2338                 TALLOC_FREE(mapped_name);
2339                 return NT_STATUS_NO_MEMORY;
2340         }
2341
2342         status = SMB_VFS_NEXT_SET_DOS_ATTRIBUTES(handle,
2343                                                  mapped_smb_fname,
2344                                                  dosmode);
2345         TALLOC_FREE(mapped_name);
2346         TALLOC_FREE(mapped_smb_fname);
2347
2348         return status;
2349 }
2350
2351 static struct vfs_fn_pointers vfs_catia_fns = {
2352         /* Directory operations */
2353         .mkdir_fn = catia_mkdir,
2354         .rmdir_fn = catia_rmdir,
2355         .opendir_fn = catia_opendir,
2356         .readdir_attr_fn = catia_readdir_attr,
2357
2358         /* File operations */
2359         .open_fn = catia_open,
2360         .pread_fn = catia_pread,
2361         .pread_send_fn = catia_pread_send,
2362         .pread_recv_fn = catia_pread_recv,
2363         .pwrite_fn = catia_pwrite,
2364         .pwrite_send_fn = catia_pwrite_send,
2365         .pwrite_recv_fn = catia_pwrite_recv,
2366         .lseek_fn = catia_lseek,
2367         .rename_fn = catia_rename,
2368         .fsync_fn = catia_fsync,
2369         .fsync_send_fn = catia_fsync_send,
2370         .fsync_recv_fn = catia_fsync_recv,
2371         .stat_fn = catia_stat,
2372         .fstat_fn = catia_fstat,
2373         .lstat_fn = catia_lstat,
2374         .unlink_fn = catia_unlink,
2375         .chmod_fn = catia_chmod,
2376         .fchmod_fn = catia_fchmod,
2377         .chown_fn = catia_chown,
2378         .fchown_fn = catia_fchown,
2379         .lchown_fn = catia_lchown,
2380         .chdir_fn = catia_chdir,
2381         .ntimes_fn = catia_ntimes,
2382         .ftruncate_fn = catia_ftruncate,
2383         .fallocate_fn = catia_fallocate,
2384         .lock_fn = catia_lock,
2385         .kernel_flock_fn = catia_kernel_flock,
2386         .linux_setlease_fn = catia_linux_setlease,
2387         .getlock_fn = catia_getlock,
2388         .realpath_fn = catia_realpath,
2389         .chflags_fn = catia_chflags,
2390         .streaminfo_fn = catia_streaminfo,
2391         .strict_lock_fn = catia_strict_lock,
2392         .strict_unlock_fn = catia_strict_unlock,
2393         .translate_name_fn = catia_translate_name,
2394         .fsctl_fn = catia_fsctl,
2395         .get_dos_attributes_fn = catia_get_dos_attributes,
2396         .set_dos_attributes_fn = catia_set_dos_attributes,
2397         .fset_dos_attributes_fn = catia_fset_dos_attributes,
2398         .fget_dos_attributes_fn = catia_fget_dos_attributes,
2399         .get_compression_fn = catia_get_compression,
2400         .set_compression_fn = catia_set_compression,
2401
2402         /* NT ACL operations. */
2403         .get_nt_acl_fn = catia_get_nt_acl,
2404         .fget_nt_acl_fn = catia_fget_nt_acl,
2405         .fset_nt_acl_fn = catia_fset_nt_acl,
2406
2407         /* POSIX ACL operations. */
2408         .chmod_acl_fn = catia_chmod_acl,
2409         .fchmod_acl_fn = catia_fchmod_acl,
2410
2411         .sys_acl_get_file_fn = catia_sys_acl_get_file,
2412         .sys_acl_get_fd_fn = catia_sys_acl_get_fd,
2413         .sys_acl_blob_get_fd_fn = catia_sys_acl_blob_get_fd,
2414         .sys_acl_set_file_fn = catia_sys_acl_set_file,
2415         .sys_acl_set_fd_fn = catia_sys_acl_set_fd,
2416         .sys_acl_delete_def_file_fn = catia_sys_acl_delete_def_file,
2417
2418         /* EA operations. */
2419         .getxattr_fn = catia_getxattr,
2420         .listxattr_fn = catia_listxattr,
2421         .removexattr_fn = catia_removexattr,
2422         .setxattr_fn = catia_setxattr,
2423         .fgetxattr_fn = catia_fgetxattr,
2424         .flistxattr_fn = catia_flistxattr,
2425         .fremovexattr_fn = catia_fremovexattr,
2426         .fsetxattr_fn = catia_fsetxattr,
2427 };
2428
2429 static_decl_vfs;
2430 NTSTATUS vfs_catia_init(void)
2431 {
2432         NTSTATUS ret;
2433
2434         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
2435                                 &vfs_catia_fns);
2436         if (!NT_STATUS_IS_OK(ret))
2437                 return ret;
2438
2439         vfs_catia_debug_level = debug_add_class("catia");
2440         if (vfs_catia_debug_level == -1) {
2441                 vfs_catia_debug_level = DBGC_VFS;
2442                 DEBUG(0, ("vfs_catia: Couldn't register custom debugging "
2443                           "class!\n"));
2444         } else {
2445                 DEBUG(10, ("vfs_catia: Debug class number of "
2446                            "'catia': %d\n", vfs_catia_debug_level));
2447         }
2448
2449         return ret;
2450
2451 }