3b691c03503eae1c98e6926548bee0fe20cc4934
[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                                         SMB_STRUCT_STAT *sbuf)
108 {
109         SMB_STRUCT_DIRENT *result = NULL;
110         SMB_STRUCT_DIRENT *newdirent = NULL;
111         char *newname;
112         size_t newnamelen;
113
114         result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
115         if (result == NULL) {
116                 return result;
117         }
118
119         newname = from_unix(talloc_tos(), result->d_name);
120         if (!newname) {
121                 return NULL;
122         }
123         newnamelen = strlen(newname)+1;
124         newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
125                                                 char,
126                                                 sizeof(SMB_STRUCT_DIRENT)+
127                                                         newnamelen);
128         if (!newdirent) {
129                 return NULL;
130         }
131         memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
132         memcpy(&newdirent->d_name, newname, newnamelen);
133         return newdirent;
134 }
135
136 static int catia_open(vfs_handle_struct *handle,
137                       struct smb_filename *smb_fname,
138                       files_struct *fsp,
139                       int flags,
140                       mode_t mode)
141 {
142         char *name;
143         char *tmp_base_name;
144         int ret;
145
146         name = to_unix(talloc_tos(), smb_fname->base_name);
147         if (!name) {
148                 errno = ENOMEM;
149                 return -1;
150         }
151
152         tmp_base_name = smb_fname->base_name;
153         smb_fname->base_name = name;
154
155         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
156
157         smb_fname->base_name = tmp_base_name;
158         TALLOC_FREE(name);
159
160         return ret;
161 }
162
163 static int catia_rename(vfs_handle_struct *handle,
164                         const struct smb_filename *smb_fname_src,
165                         const struct smb_filename *smb_fname_dst)
166 {
167         TALLOC_CTX *ctx = talloc_tos();
168         char *oname = NULL;
169         char *nname = NULL;
170         struct smb_filename *smb_fname_src_tmp = NULL;
171         struct smb_filename *smb_fname_dst_tmp = NULL;
172         NTSTATUS status;
173         int ret = -1;
174
175         oname = to_unix(ctx, smb_fname_src->base_name);
176         nname = to_unix(ctx, smb_fname_dst->base_name);
177         if (!oname || !nname) {
178                 errno = ENOMEM;
179                 goto out;
180         }
181
182         /* Setup temporary smb_filename structs. */
183         status = copy_smb_filename(talloc_tos(), smb_fname_src,
184                                    &smb_fname_src_tmp);
185         if (!NT_STATUS_IS_OK(status)) {
186                 errno = map_errno_from_nt_status(status);
187                 goto out;
188         }
189         status = copy_smb_filename(talloc_tos(), smb_fname_dst,
190                                    &smb_fname_dst_tmp);
191         if (!NT_STATUS_IS_OK(status)) {
192                 errno = map_errno_from_nt_status(status);
193                 goto out;
194         }
195
196         smb_fname_src_tmp->base_name = oname;
197         smb_fname_dst_tmp->base_name = nname;
198
199         DEBUG(10, ("converted old name: %s\n",
200                    smb_fname_str_dbg(smb_fname_src_tmp)));
201         DEBUG(10, ("converted new name: %s\n",
202                    smb_fname_str_dbg(smb_fname_dst_tmp)));
203
204         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
205                                   smb_fname_dst_tmp);
206  out:
207         TALLOC_FREE(oname);
208         TALLOC_FREE(nname);
209         TALLOC_FREE(smb_fname_src_tmp);
210         TALLOC_FREE(smb_fname_dst_tmp);
211         return ret;
212 }
213
214 static int catia_stat(vfs_handle_struct *handle,
215                       struct smb_filename *smb_fname)
216 {
217         char *name;
218         char *tmp_base_name;
219         int ret;
220
221         name = to_unix(talloc_tos(), smb_fname->base_name);
222         if (!name) {
223                 errno = ENOMEM;
224                 return -1;
225         }
226
227         tmp_base_name = smb_fname->base_name;
228         smb_fname->base_name = name;
229
230         ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
231
232         smb_fname->base_name = tmp_base_name;
233         TALLOC_FREE(name);
234
235         return ret;
236 }
237
238 static int catia_lstat(vfs_handle_struct *handle,
239                        struct smb_filename *smb_fname)
240 {
241         char *name;
242         char *tmp_base_name;
243         int ret;
244
245         name = to_unix(talloc_tos(), smb_fname->base_name);
246         if (!name) {
247                 errno = ENOMEM;
248                 return -1;
249         }
250
251         tmp_base_name = smb_fname->base_name;
252         smb_fname->base_name = name;
253
254         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
255
256         smb_fname->base_name = tmp_base_name;
257         TALLOC_FREE(name);
258
259         return ret;
260 }
261
262 static int catia_unlink(vfs_handle_struct *handle,
263                         const struct smb_filename *smb_fname)
264 {
265         struct smb_filename *smb_fname_tmp = NULL;
266         char *name = NULL;
267         NTSTATUS status;
268         int ret;
269
270         name = to_unix(talloc_tos(), smb_fname->base_name);
271         if (!name) {
272                 errno = ENOMEM;
273                 return -1;
274         }
275
276         /* Setup temporary smb_filename structs. */
277         status = copy_smb_filename(talloc_tos(), smb_fname,
278                                    &smb_fname_tmp);
279         if (!NT_STATUS_IS_OK(status)) {
280                 errno = map_errno_from_nt_status(status);
281                 return -1;
282         }
283
284         smb_fname_tmp->base_name = name;
285
286         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
287
288         TALLOC_FREE(smb_fname_tmp);
289         return ret;
290 }
291
292 static int catia_chmod(vfs_handle_struct *handle,
293                        const char *path, mode_t mode)
294 {
295         char *name = to_unix(talloc_tos(), path);
296
297         if (!name) {
298                 errno = ENOMEM;
299                 return -1;
300         }
301         return SMB_VFS_NEXT_CHMOD(handle, name, mode);
302 }
303
304 static int catia_chown(vfs_handle_struct *handle,
305                        const char *path, uid_t uid, gid_t gid)
306 {
307         char *name = to_unix(talloc_tos(), path);
308
309         if (!name) {
310                 errno = ENOMEM;
311                 return -1;
312         }
313         return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
314 }
315
316 static int catia_lchown(vfs_handle_struct *handle,
317                        const char *path, uid_t uid, gid_t gid)
318 {
319         char *name = to_unix(talloc_tos(), path);
320
321         if (!name) {
322                 errno = ENOMEM;
323                 return -1;
324         }
325         return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
326 }
327
328 static int catia_chdir(vfs_handle_struct *handle,
329                        const char *path)
330 {
331         char *name = to_unix(talloc_tos(), path);
332
333         if (!name) {
334                 errno = ENOMEM;
335                 return -1;
336         }
337         return SMB_VFS_NEXT_CHDIR(handle, name);
338 }
339
340 /* VFS operations structure */
341
342 static vfs_op_tuple catia_op_tuples[] = {
343
344         /* Directory operations */
345
346         {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
347 SMB_VFS_LAYER_TRANSPARENT},
348         {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
349 SMB_VFS_LAYER_TRANSPARENT},
350
351         /* File operations */
352
353         {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
354 SMB_VFS_LAYER_TRANSPARENT},
355         {SMB_VFS_OP(catia_rename),                      SMB_VFS_OP_RENAME,
356         SMB_VFS_LAYER_TRANSPARENT},
357         {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
358 SMB_VFS_LAYER_TRANSPARENT},
359         {SMB_VFS_OP(catia_lstat),                       SMB_VFS_OP_LSTAT,
360 SMB_VFS_LAYER_TRANSPARENT},
361         {SMB_VFS_OP(catia_unlink),                      SMB_VFS_OP_UNLINK,
362         SMB_VFS_LAYER_TRANSPARENT},
363         {SMB_VFS_OP(catia_chmod),                       SMB_VFS_OP_CHMOD,
364 SMB_VFS_LAYER_TRANSPARENT},
365         {SMB_VFS_OP(catia_chown),                       SMB_VFS_OP_CHOWN,
366 SMB_VFS_LAYER_TRANSPARENT},
367         {SMB_VFS_OP(catia_lchown),                      SMB_VFS_OP_LCHOWN,
368 SMB_VFS_LAYER_TRANSPARENT},
369         {SMB_VFS_OP(catia_chdir),                       SMB_VFS_OP_CHDIR,
370 SMB_VFS_LAYER_TRANSPARENT},
371
372         {NULL,                                          SMB_VFS_OP_NOOP,
373 SMB_VFS_LAYER_NOOP}
374 };
375
376 NTSTATUS vfs_catia_init(void);
377 NTSTATUS vfs_catia_init(void)
378 {
379         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
380 catia_op_tuples);
381 }