s3: Plumb smb_filename through SMB_VFS_RENAME
[samba.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  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "includes.h"
29
30 static char *catia_string_replace(TALLOC_CTX *ctx,
31                         const char *s,
32                         unsigned char oldc,
33                         unsigned char newc)
34 {
35         smb_ucs2_t *tmpbuf = NULL;
36         smb_ucs2_t *ptr = NULL;
37         smb_ucs2_t old = oldc;
38         char *ret = NULL;
39         size_t converted_size;
40
41         if (!s) {
42                 return NULL;
43         }
44
45         if (!push_ucs2_talloc(ctx, &tmpbuf, s, &converted_size)) {
46                 return NULL;
47         }
48
49         ptr = tmpbuf;
50
51         for (;*ptr;ptr++) {
52                 if (*ptr==old) {
53                         *ptr=newc;
54                 }
55         }
56
57         if (!pull_ucs2_talloc(ctx, &ret, tmpbuf, &converted_size)) {
58                 TALLOC_FREE(tmpbuf);
59                 return NULL;
60         }
61         TALLOC_FREE(tmpbuf);
62         return ret;
63 }
64
65 static char *from_unix(TALLOC_CTX *ctx, const char *s)
66 {
67         char *ret = catia_string_replace(ctx, s, '\x22', '\xa8');
68         ret = catia_string_replace(ctx, ret, '\x2a', '\xa4');
69         ret = catia_string_replace(ctx, ret, '\x2f', '\xf8');
70         ret = catia_string_replace(ctx, ret, '\x3a', '\xf7');
71         ret = catia_string_replace(ctx, ret, '\x3c', '\xab');
72         ret = catia_string_replace(ctx, ret, '\x3e', '\xbb');
73         ret = catia_string_replace(ctx, ret, '\x3f', '\xbf');
74         ret = catia_string_replace(ctx, ret, '\x5c', '\xff');
75         ret = catia_string_replace(ctx, ret, '\x7c', '\xa6');
76         return catia_string_replace(ctx, ret, ' ', '\xb1');
77 }
78
79 static char *to_unix(TALLOC_CTX *ctx, const char *s)
80 {
81         char *ret = catia_string_replace(ctx, s, '\xa8', '\x22');
82         ret = catia_string_replace(ctx, ret, '\xa4', '\x2a');
83         ret = catia_string_replace(ctx, ret, '\xf8', '\x2f');
84         ret = catia_string_replace(ctx, ret, '\xf7', '\x3a');
85         ret = catia_string_replace(ctx, ret, '\xab', '\x3c');
86         ret = catia_string_replace(ctx, ret, '\xbb', '\x3e');
87         ret = catia_string_replace(ctx, ret, '\xbf', '\x3f');
88         ret = catia_string_replace(ctx, ret, '\xff', '\x5c');
89         ret = catia_string_replace(ctx, ret, '\xa6', '\x7c');
90         return catia_string_replace(ctx, ret, '\xb1', ' ');
91 }
92
93 static SMB_STRUCT_DIR *catia_opendir(vfs_handle_struct *handle,
94                           const char *fname, const char *mask, uint32 attr)
95 {
96         char *name = to_unix(talloc_tos(), fname);
97
98         if (!name) {
99                 errno = ENOMEM;
100                 return NULL;
101         }
102         return SMB_VFS_NEXT_OPENDIR(handle, name, mask, attr);
103 }
104
105 static SMB_STRUCT_DIRENT *catia_readdir(vfs_handle_struct *handle,
106                                         SMB_STRUCT_DIR *dirp)
107 {
108         SMB_STRUCT_DIRENT *result = NULL;
109         SMB_STRUCT_DIRENT *newdirent = NULL;
110         char *newname;
111         size_t newnamelen;
112
113         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
114         if (result == NULL) {
115                 return result;
116         }
117
118         newname = from_unix(talloc_tos(), result->d_name);
119         if (!newname) {
120                 return NULL;
121         }
122         newnamelen = strlen(newname)+1;
123         newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
124                                                 char,
125                                                 sizeof(SMB_STRUCT_DIRENT)+
126                                                         newnamelen);
127         if (!newdirent) {
128                 return NULL;
129         }
130         memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
131         memcpy(&newdirent->d_name, newname, newnamelen);
132         return newdirent;
133 }
134
135 static int catia_open(vfs_handle_struct *handle,
136                       struct smb_filename *smb_fname,
137                       files_struct *fsp,
138                       int flags,
139                       mode_t mode)
140 {
141         char *name;
142         char *tmp_base_name;
143         int ret;
144
145         name = to_unix(talloc_tos(), smb_fname->base_name);
146         if (!name) {
147                 errno = ENOMEM;
148                 return -1;
149         }
150
151         tmp_base_name = smb_fname->base_name;
152         smb_fname->base_name = name;
153
154         ret = SMB_VFS_NEXT_OPEN(handle, name, fsp, flags, mode);
155
156         smb_fname->base_name = tmp_base_name;
157         TALLOC_FREE(name);
158
159         return ret;
160 }
161
162 static int catia_rename(vfs_handle_struct *handle,
163                         const struct smb_filename *smb_fname_src,
164                         const struct smb_filename *smb_fname_dst)
165 {
166         TALLOC_CTX *ctx = talloc_tos();
167         char *oname = NULL;
168         char *nname = NULL;
169         struct smb_filename *smb_fname_src_tmp = NULL;
170         struct smb_filename *smb_fname_dst_tmp = NULL;
171         NTSTATUS status;
172         int ret = -1;
173
174         oname = to_unix(ctx, smb_fname_src->base_name);
175         nname = to_unix(ctx, smb_fname_dst->base_name);
176         if (!oname || !nname) {
177                 errno = ENOMEM;
178                 goto out;
179         }
180
181         /* Setup temporary smb_filename structs. */
182         status = copy_smb_filename(talloc_tos(), smb_fname_src,
183                                    &smb_fname_src_tmp);
184         if (!NT_STATUS_IS_OK(status)) {
185                 errno = map_errno_from_nt_status(status);
186                 goto out;
187         }
188         status = copy_smb_filename(talloc_tos(), smb_fname_dst,
189                                    &smb_fname_dst_tmp);
190         if (!NT_STATUS_IS_OK(status)) {
191                 errno = map_errno_from_nt_status(status);
192                 goto out;
193         }
194
195         smb_fname_src_tmp->base_name = oname;
196         smb_fname_dst_tmp->base_name = nname;
197
198         DEBUG(10, ("converted old name: %s\n",
199                    smb_fname_str_dbg(smb_fname_src_tmp)));
200         DEBUG(10, ("converted new name: %s\n",
201                    smb_fname_str_dbg(smb_fname_dst_tmp)));
202
203         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
204                                   smb_fname_dst_tmp);
205  out:
206         TALLOC_FREE(oname);
207         TALLOC_FREE(newname);
208         TALLOC_FREE(smb_fname_src_tmp);
209         TALLOC_FREE(smb_fname_dst_tmp);
210         return ret;
211 }
212
213 static int catia_stat(vfs_handle_struct *handle,
214                       struct smb_filename *smb_fname)
215 {
216         char *name;
217         char *tmp_base_name;
218         int ret;
219
220         name = to_unix(talloc_tos(), smb_fname->base_name);
221         if (!name) {
222                 errno = ENOMEM;
223                 return -1;
224         }
225
226         tmp_base_name = smb_fname->base_name;
227         smb_fname->base_name = name;
228
229         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
230
231         smb_fname->base_name = tmp_base_name;
232         TALLOC_FREE(name);
233
234         return ret;
235 }
236
237 static int catia_lstat(vfs_handle_struct *handle,
238                        struct smb_filename *smb_fname)
239 {
240         char *name;
241         char *tmp_base_name;
242         int ret;
243
244         name = to_unix(talloc_tos(), smb_fname->base_name);
245         if (!name) {
246                 errno = ENOMEM;
247                 return -1;
248         }
249
250         tmp_base_name = smb_fname->base_name;
251         smb_fname->base_name = name;
252
253         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
254
255         smb_fname->base_name = tmp_base_name;
256         TALLOC_FREE(name);
257
258         return ret;
259 }
260
261 static int catia_unlink(vfs_handle_struct *handle, const char *path)
262 {
263         char *name = to_unix(talloc_tos(), path);
264
265         if (!name) {
266                 errno = ENOMEM;
267                 return -1;
268         }
269         return SMB_VFS_NEXT_UNLINK(handle, name);
270 }
271
272 static int catia_chmod(vfs_handle_struct *handle,
273                        const char *path, mode_t mode)
274 {
275         char *name = to_unix(talloc_tos(), path);
276
277         if (!name) {
278                 errno = ENOMEM;
279                 return -1;
280         }
281         return SMB_VFS_NEXT_CHMOD(handle, name, mode);
282 }
283
284 static int catia_chown(vfs_handle_struct *handle,
285                        const char *path, uid_t uid, gid_t gid)
286 {
287         char *name = to_unix(talloc_tos(), path);
288
289         if (!name) {
290                 errno = ENOMEM;
291                 return -1;
292         }
293         return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
294 }
295
296 static int catia_lchown(vfs_handle_struct *handle,
297                        const char *path, uid_t uid, gid_t gid)
298 {
299         char *name = to_unix(talloc_tos(), path);
300
301         if (!name) {
302                 errno = ENOMEM;
303                 return -1;
304         }
305         return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
306 }
307
308 static int catia_chdir(vfs_handle_struct *handle,
309                        const char *path)
310 {
311         char *name = to_unix(talloc_tos(), path);
312
313         if (!name) {
314                 errno = ENOMEM;
315                 return -1;
316         }
317         return SMB_VFS_NEXT_CHDIR(handle, name);
318 }
319
320 static char *catia_getwd(vfs_handle_struct *handle, char *buf)
321 {
322         return SMB_VFS_NEXT_GETWD(handle, buf);
323 }
324
325 static int catia_ntimes(vfs_handle_struct *handle,
326                        const char *path, struct smb_file_time *ft)
327 {
328         return SMB_VFS_NEXT_NTIMES(handle, path, ft);
329 }
330
331 static bool catia_symlink(vfs_handle_struct *handle,
332                           const char *oldpath, const char *newpath)
333 {
334         return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
335 }
336
337 static bool catia_readlink(vfs_handle_struct *handle,
338                            const char *path, char *buf, size_t bufsiz)
339 {
340         return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
341 }
342
343 static int catia_link(vfs_handle_struct *handle,
344                       const char *oldpath, const char *newpath)
345 {
346         return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
347 }
348
349 static int catia_mknod(vfs_handle_struct *handle,
350                        const char *path, mode_t mode, SMB_DEV_T dev)
351 {
352         return SMB_VFS_NEXT_MKNOD(handle, path, mode, dev);
353 }
354
355 static char *catia_realpath(vfs_handle_struct *handle,
356                             const char *path, char *resolved_path)
357 {
358         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
359 }
360
361 static NTSTATUS catia_get_nt_acl(vfs_handle_struct *handle,
362                                const char *name, uint32 security_info,
363                                struct  security_descriptor **ppdesc)
364 {
365         return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
366 }
367
368 static int catia_chmod_acl(vfs_handle_struct *handle,
369                            const char *name, mode_t mode)
370 {
371         /* If the underlying VFS doesn't have ACL support... */
372         if (!handle->vfs_next.ops.chmod_acl) {
373                 errno = ENOSYS;
374                 return -1;
375         }
376         return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
377 }
378
379 /* VFS operations structure */
380
381 static vfs_op_tuple catia_op_tuples[] = {
382
383         /* Directory operations */
384
385         {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
386 SMB_VFS_LAYER_TRANSPARENT},
387         {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
388 SMB_VFS_LAYER_TRANSPARENT},
389
390         /* File operations */
391
392         {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
393 SMB_VFS_LAYER_TRANSPARENT},
394         {SMB_VFS_OP(catia_rename),                      SMB_VFS_OP_RENAME,
395         SMB_VFS_LAYER_TRANSPARENT},
396         {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
397 SMB_VFS_LAYER_TRANSPARENT},
398         {SMB_VFS_OP(catia_lstat),                       SMB_VFS_OP_LSTAT,
399 SMB_VFS_LAYER_TRANSPARENT},
400         {SMB_VFS_OP(catia_unlink),                      SMB_VFS_OP_UNLINK,
401         SMB_VFS_LAYER_TRANSPARENT},
402         {SMB_VFS_OP(catia_chmod),                       SMB_VFS_OP_CHMOD,
403 SMB_VFS_LAYER_TRANSPARENT},
404         {SMB_VFS_OP(catia_chown),                       SMB_VFS_OP_CHOWN,
405 SMB_VFS_LAYER_TRANSPARENT},
406         {SMB_VFS_OP(catia_lchown),                      SMB_VFS_OP_LCHOWN,
407 SMB_VFS_LAYER_TRANSPARENT},
408         {SMB_VFS_OP(catia_chdir),                       SMB_VFS_OP_CHDIR,
409 SMB_VFS_LAYER_TRANSPARENT},
410         {SMB_VFS_OP(catia_getwd),                       SMB_VFS_OP_GETWD,
411 SMB_VFS_LAYER_TRANSPARENT},
412         {SMB_VFS_OP(catia_ntimes),                       SMB_VFS_OP_NTIMES,
413 SMB_VFS_LAYER_TRANSPARENT},
414         {SMB_VFS_OP(catia_symlink), SMB_VFS_OP_SYMLINK,
415 SMB_VFS_LAYER_TRANSPARENT},
416         {SMB_VFS_OP(catia_readlink), SMB_VFS_OP_READLINK,
417 SMB_VFS_LAYER_TRANSPARENT},
418         {SMB_VFS_OP(catia_link), SMB_VFS_OP_LINK,
419 SMB_VFS_LAYER_TRANSPARENT},
420         {SMB_VFS_OP(catia_mknod),                       SMB_VFS_OP_MKNOD,
421 SMB_VFS_LAYER_TRANSPARENT},
422         {SMB_VFS_OP(catia_realpath), SMB_VFS_OP_REALPATH,
423 SMB_VFS_LAYER_TRANSPARENT},
424
425         /* NT File ACL operations */
426
427         {SMB_VFS_OP(catia_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
428 SMB_VFS_LAYER_TRANSPARENT},
429
430         /* POSIX ACL operations */
431
432         {SMB_VFS_OP(catia_chmod_acl), SMB_VFS_OP_CHMOD_ACL,
433 SMB_VFS_LAYER_TRANSPARENT},
434
435
436         {NULL,                                          SMB_VFS_OP_NOOP,
437 SMB_VFS_LAYER_NOOP}
438 };
439
440 NTSTATUS vfs_catia_init(void);
441 NTSTATUS vfs_catia_init(void)
442 {
443         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
444 catia_op_tuples);
445 }