s3: Change SMB_VFS_OPEN to take an smb_filename struct
[metze/samba/wip.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 char *oldname, const char *newname)
164 {
165         TALLOC_CTX *ctx = talloc_tos();
166         char *oname = to_unix(ctx, oldname);
167         char *nname = to_unix(ctx, newname);
168
169         if (!oname || !nname) {
170                 errno = ENOMEM;
171                 return -1;
172         }
173         DEBUG(10, ("converted old name: %s\n", oname));
174         DEBUG(10, ("converted new name: %s\n", nname));
175
176         return SMB_VFS_NEXT_RENAME(handle, oname, nname);
177 }
178
179 static int catia_stat(vfs_handle_struct *handle,
180                       const char *fname, SMB_STRUCT_STAT *sbuf)
181 {
182         char *name = to_unix(talloc_tos(), fname);
183
184         if (!name) {
185                 errno = ENOMEM;
186                 return -1;
187         }
188         return SMB_VFS_NEXT_STAT(handle, name, sbuf);
189 }
190
191 static int catia_lstat(vfs_handle_struct *handle,
192                        const char *path, SMB_STRUCT_STAT *sbuf)
193 {
194         char *name = to_unix(talloc_tos(), path);
195
196         if (!name) {
197                 errno = ENOMEM;
198                 return -1;
199         }
200         return SMB_VFS_NEXT_LSTAT(handle, name, sbuf);
201 }
202
203 static int catia_unlink(vfs_handle_struct *handle, const char *path)
204 {
205         char *name = to_unix(talloc_tos(), path);
206
207         if (!name) {
208                 errno = ENOMEM;
209                 return -1;
210         }
211         return SMB_VFS_NEXT_UNLINK(handle, name);
212 }
213
214 static int catia_chmod(vfs_handle_struct *handle,
215                        const char *path, mode_t mode)
216 {
217         char *name = to_unix(talloc_tos(), path);
218
219         if (!name) {
220                 errno = ENOMEM;
221                 return -1;
222         }
223         return SMB_VFS_NEXT_CHMOD(handle, name, mode);
224 }
225
226 static int catia_chown(vfs_handle_struct *handle,
227                        const char *path, uid_t uid, gid_t gid)
228 {
229         char *name = to_unix(talloc_tos(), path);
230
231         if (!name) {
232                 errno = ENOMEM;
233                 return -1;
234         }
235         return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
236 }
237
238 static int catia_lchown(vfs_handle_struct *handle,
239                        const char *path, uid_t uid, gid_t gid)
240 {
241         char *name = to_unix(talloc_tos(), path);
242
243         if (!name) {
244                 errno = ENOMEM;
245                 return -1;
246         }
247         return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
248 }
249
250 static int catia_chdir(vfs_handle_struct *handle,
251                        const char *path)
252 {
253         char *name = to_unix(talloc_tos(), path);
254
255         if (!name) {
256                 errno = ENOMEM;
257                 return -1;
258         }
259         return SMB_VFS_NEXT_CHDIR(handle, name);
260 }
261
262 static char *catia_getwd(vfs_handle_struct *handle, char *buf)
263 {
264         return SMB_VFS_NEXT_GETWD(handle, buf);
265 }
266
267 static int catia_ntimes(vfs_handle_struct *handle,
268                        const char *path, struct smb_file_time *ft)
269 {
270         return SMB_VFS_NEXT_NTIMES(handle, path, ft);
271 }
272
273 static bool catia_symlink(vfs_handle_struct *handle,
274                           const char *oldpath, const char *newpath)
275 {
276         return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
277 }
278
279 static bool catia_readlink(vfs_handle_struct *handle,
280                            const char *path, char *buf, size_t bufsiz)
281 {
282         return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
283 }
284
285 static int catia_link(vfs_handle_struct *handle,
286                       const char *oldpath, const char *newpath)
287 {
288         return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
289 }
290
291 static int catia_mknod(vfs_handle_struct *handle,
292                        const char *path, mode_t mode, SMB_DEV_T dev)
293 {
294         return SMB_VFS_NEXT_MKNOD(handle, path, mode, dev);
295 }
296
297 static char *catia_realpath(vfs_handle_struct *handle,
298                             const char *path, char *resolved_path)
299 {
300         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
301 }
302
303 static NTSTATUS catia_get_nt_acl(vfs_handle_struct *handle,
304                                const char *name, uint32 security_info,
305                                struct  security_descriptor **ppdesc)
306 {
307         return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
308 }
309
310 static int catia_chmod_acl(vfs_handle_struct *handle,
311                            const char *name, mode_t mode)
312 {
313         /* If the underlying VFS doesn't have ACL support... */
314         if (!handle->vfs_next.ops.chmod_acl) {
315                 errno = ENOSYS;
316                 return -1;
317         }
318         return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
319 }
320
321 /* VFS operations structure */
322
323 static vfs_op_tuple catia_op_tuples[] = {
324
325         /* Directory operations */
326
327         {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
328 SMB_VFS_LAYER_TRANSPARENT},
329         {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
330 SMB_VFS_LAYER_TRANSPARENT},
331
332         /* File operations */
333
334         {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
335 SMB_VFS_LAYER_TRANSPARENT},
336         {SMB_VFS_OP(catia_rename),                      SMB_VFS_OP_RENAME,
337         SMB_VFS_LAYER_TRANSPARENT},
338         {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
339 SMB_VFS_LAYER_TRANSPARENT},
340         {SMB_VFS_OP(catia_lstat),                       SMB_VFS_OP_LSTAT,
341 SMB_VFS_LAYER_TRANSPARENT},
342         {SMB_VFS_OP(catia_unlink),                      SMB_VFS_OP_UNLINK,
343         SMB_VFS_LAYER_TRANSPARENT},
344         {SMB_VFS_OP(catia_chmod),                       SMB_VFS_OP_CHMOD,
345 SMB_VFS_LAYER_TRANSPARENT},
346         {SMB_VFS_OP(catia_chown),                       SMB_VFS_OP_CHOWN,
347 SMB_VFS_LAYER_TRANSPARENT},
348         {SMB_VFS_OP(catia_lchown),                      SMB_VFS_OP_LCHOWN,
349 SMB_VFS_LAYER_TRANSPARENT},
350         {SMB_VFS_OP(catia_chdir),                       SMB_VFS_OP_CHDIR,
351 SMB_VFS_LAYER_TRANSPARENT},
352         {SMB_VFS_OP(catia_getwd),                       SMB_VFS_OP_GETWD,
353 SMB_VFS_LAYER_TRANSPARENT},
354         {SMB_VFS_OP(catia_ntimes),                       SMB_VFS_OP_NTIMES,
355 SMB_VFS_LAYER_TRANSPARENT},
356         {SMB_VFS_OP(catia_symlink), SMB_VFS_OP_SYMLINK,
357 SMB_VFS_LAYER_TRANSPARENT},
358         {SMB_VFS_OP(catia_readlink), SMB_VFS_OP_READLINK,
359 SMB_VFS_LAYER_TRANSPARENT},
360         {SMB_VFS_OP(catia_link), SMB_VFS_OP_LINK,
361 SMB_VFS_LAYER_TRANSPARENT},
362         {SMB_VFS_OP(catia_mknod),                       SMB_VFS_OP_MKNOD,
363 SMB_VFS_LAYER_TRANSPARENT},
364         {SMB_VFS_OP(catia_realpath), SMB_VFS_OP_REALPATH,
365 SMB_VFS_LAYER_TRANSPARENT},
366
367         /* NT File ACL operations */
368
369         {SMB_VFS_OP(catia_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
370 SMB_VFS_LAYER_TRANSPARENT},
371
372         /* POSIX ACL operations */
373
374         {SMB_VFS_OP(catia_chmod_acl), SMB_VFS_OP_CHMOD_ACL,
375 SMB_VFS_LAYER_TRANSPARENT},
376
377
378         {NULL,                                          SMB_VFS_OP_NOOP,
379 SMB_VFS_LAYER_NOOP}
380 };
381
382 NTSTATUS vfs_catia_init(void);
383 NTSTATUS vfs_catia_init(void)
384 {
385         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
386 catia_op_tuples);
387 }