Merge Samba3 and Samba4 together
[kamenim/samba-autobuild/.git] / source3 / modules / vfs_catia.c
1 /*
2  * Catia VFS module
3  *
4  * Implement a fixed mapping of forbidden NT characters in filenames that are
5  * used a lot by the CAD package Catia.
6  *
7  * Yes, this a BAD BAD UGLY INCOMPLETE hack, but it helps quite some people
8  * out there. Catia V4 on AIX uses characters like "<*$ a *lot*, all forbidden
9  * under Windows...
10  *
11  * Copyright (C) Volker Lendecke, 2005
12  *
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 = SMB_VFS_NEXT_READDIR(handle, dirp);
109         SMB_STRUCT_DIRENT *newdirent;
110         char *newname;
111         size_t newnamelen;
112
113         if (result == NULL) {
114                 return result;
115         }
116
117         newname = from_unix(talloc_tos(), result->d_name);
118         if (!newname) {
119                 return NULL;
120         }
121         newnamelen = strlen(newname)+1;
122         newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
123                                                 char,
124                                                 sizeof(SMB_STRUCT_DIRENT)+
125                                                         newnamelen);
126         if (!newdirent) {
127                 return NULL;
128         }
129         memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
130         memcpy(&newdirent->d_name, newname, newnamelen);
131         return newdirent;
132 }
133
134 static int catia_open(vfs_handle_struct *handle,
135                       const char *fname,
136                       files_struct *fsp,
137                       int flags,
138                       mode_t mode)
139 {
140         char *name = to_unix(talloc_tos(), fname);
141
142         if (!name) {
143                 errno = ENOMEM;
144                 return -1;
145         }
146         return SMB_VFS_NEXT_OPEN(handle, name, fsp, flags, mode);
147 }
148
149 static int catia_rename(vfs_handle_struct *handle,
150                         const char *oldname, const char *newname)
151 {
152         TALLOC_CTX *ctx = talloc_tos();
153         char *oname = to_unix(ctx, oldname);
154         char *nname = to_unix(ctx, newname);
155
156         if (!oname || !nname) {
157                 errno = ENOMEM;
158                 return -1;
159         }
160         DEBUG(10, ("converted old name: %s\n", oname));
161         DEBUG(10, ("converted new name: %s\n", nname));
162
163         return SMB_VFS_NEXT_RENAME(handle, oname, nname);
164 }
165
166 static int catia_stat(vfs_handle_struct *handle,
167                       const char *fname, SMB_STRUCT_STAT *sbuf)
168 {
169         char *name = to_unix(talloc_tos(), fname);
170
171         if (!name) {
172                 errno = ENOMEM;
173                 return -1;
174         }
175         return SMB_VFS_NEXT_STAT(handle, name, sbuf);
176 }
177
178 static int catia_lstat(vfs_handle_struct *handle,
179                        const char *path, SMB_STRUCT_STAT *sbuf)
180 {
181         char *name = to_unix(talloc_tos(), path);
182
183         if (!name) {
184                 errno = ENOMEM;
185                 return -1;
186         }
187         return SMB_VFS_NEXT_LSTAT(handle, name, sbuf);
188 }
189
190 static int catia_unlink(vfs_handle_struct *handle, const char *path)
191 {
192         char *name = to_unix(talloc_tos(), path);
193
194         if (!name) {
195                 errno = ENOMEM;
196                 return -1;
197         }
198         return SMB_VFS_NEXT_UNLINK(handle, name);
199 }
200
201 static int catia_chmod(vfs_handle_struct *handle,
202                        const char *path, mode_t mode)
203 {
204         char *name = to_unix(talloc_tos(), path);
205
206         if (!name) {
207                 errno = ENOMEM;
208                 return -1;
209         }
210         return SMB_VFS_NEXT_CHMOD(handle, name, mode);
211 }
212
213 static int catia_chown(vfs_handle_struct *handle,
214                        const char *path, uid_t uid, gid_t gid)
215 {
216         char *name = to_unix(talloc_tos(), path);
217
218         if (!name) {
219                 errno = ENOMEM;
220                 return -1;
221         }
222         return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
223 }
224
225 static int catia_lchown(vfs_handle_struct *handle,
226                        const char *path, uid_t uid, gid_t gid)
227 {
228         char *name = to_unix(talloc_tos(), path);
229
230         if (!name) {
231                 errno = ENOMEM;
232                 return -1;
233         }
234         return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
235 }
236
237 static int catia_chdir(vfs_handle_struct *handle,
238                        const char *path)
239 {
240         char *name = to_unix(talloc_tos(), path);
241
242         if (!name) {
243                 errno = ENOMEM;
244                 return -1;
245         }
246         return SMB_VFS_NEXT_CHDIR(handle, name);
247 }
248
249 static char *catia_getwd(vfs_handle_struct *handle, char *buf)
250 {
251         return SMB_VFS_NEXT_GETWD(handle, buf);
252 }
253
254 static int catia_ntimes(vfs_handle_struct *handle,
255                        const char *path, const struct timespec ts[2])
256 {
257         return SMB_VFS_NEXT_NTIMES(handle, path, ts);
258 }
259
260 static bool catia_symlink(vfs_handle_struct *handle,
261                           const char *oldpath, const char *newpath)
262 {
263         return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
264 }
265
266 static bool catia_readlink(vfs_handle_struct *handle,
267                            const char *path, char *buf, size_t bufsiz)
268 {
269         return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
270 }
271
272 static int catia_link(vfs_handle_struct *handle,
273                       const char *oldpath, const char *newpath)
274 {
275         return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
276 }
277
278 static int catia_mknod(vfs_handle_struct *handle,
279                        const char *path, mode_t mode, SMB_DEV_T dev)
280 {
281         return SMB_VFS_NEXT_MKNOD(handle, path, mode, dev);
282 }
283
284 static char *catia_realpath(vfs_handle_struct *handle,
285                             const char *path, char *resolved_path)
286 {
287         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
288 }
289
290 static NTSTATUS catia_get_nt_acl(vfs_handle_struct *handle,
291                                const char *name, uint32 security_info,
292                                struct  security_descriptor **ppdesc)
293 {
294         return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
295 }
296
297 static int catia_chmod_acl(vfs_handle_struct *handle,
298                            const char *name, mode_t mode)
299 {
300         /* If the underlying VFS doesn't have ACL support... */
301         if (!handle->vfs_next.ops.chmod_acl) {
302                 errno = ENOSYS;
303                 return -1;
304         }
305         return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
306 }
307
308 /* VFS operations structure */
309
310 static vfs_op_tuple catia_op_tuples[] = {
311
312         /* Directory operations */
313
314         {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
315 SMB_VFS_LAYER_TRANSPARENT},
316         {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
317 SMB_VFS_LAYER_TRANSPARENT},
318
319         /* File operations */
320
321         {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
322 SMB_VFS_LAYER_TRANSPARENT},
323         {SMB_VFS_OP(catia_rename),                      SMB_VFS_OP_RENAME,
324         SMB_VFS_LAYER_TRANSPARENT},
325         {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
326 SMB_VFS_LAYER_TRANSPARENT},
327         {SMB_VFS_OP(catia_lstat),                       SMB_VFS_OP_LSTAT,
328 SMB_VFS_LAYER_TRANSPARENT},
329         {SMB_VFS_OP(catia_unlink),                      SMB_VFS_OP_UNLINK,
330         SMB_VFS_LAYER_TRANSPARENT},
331         {SMB_VFS_OP(catia_chmod),                       SMB_VFS_OP_CHMOD,
332 SMB_VFS_LAYER_TRANSPARENT},
333         {SMB_VFS_OP(catia_chown),                       SMB_VFS_OP_CHOWN,
334 SMB_VFS_LAYER_TRANSPARENT},
335         {SMB_VFS_OP(catia_lchown),                      SMB_VFS_OP_LCHOWN,
336 SMB_VFS_LAYER_TRANSPARENT},
337         {SMB_VFS_OP(catia_chdir),                       SMB_VFS_OP_CHDIR,
338 SMB_VFS_LAYER_TRANSPARENT},
339         {SMB_VFS_OP(catia_getwd),                       SMB_VFS_OP_GETWD,
340 SMB_VFS_LAYER_TRANSPARENT},
341         {SMB_VFS_OP(catia_ntimes),                       SMB_VFS_OP_NTIMES,
342 SMB_VFS_LAYER_TRANSPARENT},
343         {SMB_VFS_OP(catia_symlink), SMB_VFS_OP_SYMLINK,
344 SMB_VFS_LAYER_TRANSPARENT},
345         {SMB_VFS_OP(catia_readlink), SMB_VFS_OP_READLINK,
346 SMB_VFS_LAYER_TRANSPARENT},
347         {SMB_VFS_OP(catia_link), SMB_VFS_OP_LINK,
348 SMB_VFS_LAYER_TRANSPARENT},
349         {SMB_VFS_OP(catia_mknod),                       SMB_VFS_OP_MKNOD,
350 SMB_VFS_LAYER_TRANSPARENT},
351         {SMB_VFS_OP(catia_realpath), SMB_VFS_OP_REALPATH,
352 SMB_VFS_LAYER_TRANSPARENT},
353
354         /* NT File ACL operations */
355
356         {SMB_VFS_OP(catia_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
357 SMB_VFS_LAYER_TRANSPARENT},
358
359         /* POSIX ACL operations */
360
361         {SMB_VFS_OP(catia_chmod_acl), SMB_VFS_OP_CHMOD_ACL,
362 SMB_VFS_LAYER_TRANSPARENT},
363
364
365         {NULL,                                          SMB_VFS_OP_NOOP,
366 SMB_VFS_LAYER_NOOP}
367 };
368
369 NTSTATUS vfs_catia_init(void);
370 NTSTATUS vfs_catia_init(void)
371 {
372         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
373 catia_op_tuples);
374 }