r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[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 void catia_string_replace(char *s, unsigned char oldc, unsigned 
31                                  char newc)
32 {
33         static smb_ucs2_t tmpbuf[sizeof(pstring)];
34         smb_ucs2_t *ptr = tmpbuf;
35         smb_ucs2_t old = oldc;
36
37         push_ucs2(NULL, tmpbuf, s, sizeof(tmpbuf), STR_TERMINATE);
38
39         for (;*ptr;ptr++)
40                 if (*ptr==old) *ptr=newc;
41
42         pull_ucs2(NULL, s, tmpbuf, -1, sizeof(tmpbuf), STR_TERMINATE);
43 }
44
45 static void from_unix(char *s)
46 {
47         catia_string_replace(s, '\x22', '\xa8');
48         catia_string_replace(s, '\x2a', '\xa4');
49         catia_string_replace(s, '\x2f', '\xf8');
50         catia_string_replace(s, '\x3a', '\xf7');
51         catia_string_replace(s, '\x3c', '\xab');
52         catia_string_replace(s, '\x3e', '\xbb');
53         catia_string_replace(s, '\x3f', '\xbf');
54         catia_string_replace(s, '\x5c', '\xff');
55         catia_string_replace(s, '\x7c', '\xa6');
56         catia_string_replace(s, ' ', '\xb1');
57 }
58
59 static void to_unix(char *s)
60 {
61         catia_string_replace(s, '\xa8', '\x22');
62         catia_string_replace(s, '\xa4', '\x2a');
63         catia_string_replace(s, '\xf8', '\x2f');
64         catia_string_replace(s, '\xf7', '\x3a');
65         catia_string_replace(s, '\xab', '\x3c');
66         catia_string_replace(s, '\xbb', '\x3e');
67         catia_string_replace(s, '\xbf', '\x3f');
68         catia_string_replace(s, '\xff', '\x5c');
69         catia_string_replace(s, '\xa6', '\x7c');
70         catia_string_replace(s, '\xb1', ' ');
71 }
72
73 static SMB_STRUCT_DIR *catia_opendir(vfs_handle_struct *handle,
74                           const char *fname, const char *mask, uint32 attr)
75 {
76         pstring name;
77         pstrcpy(name, fname);
78         to_unix(name);
79
80         return SMB_VFS_NEXT_OPENDIR(handle, name, mask, attr);
81 }
82
83 static SMB_STRUCT_DIRENT *catia_readdir(vfs_handle_struct *handle, 
84                                         SMB_STRUCT_DIR *dirp)
85 {
86         SMB_STRUCT_DIRENT *result = SMB_VFS_NEXT_READDIR(handle, dirp);
87
88         if (result == NULL)
89                 return result;
90
91         from_unix(result->d_name);
92         return result;
93 }
94
95 static int catia_open(vfs_handle_struct *handle,
96                       const char *fname, files_struct *fsp, int flags, mode_t mode)
97 {
98         pstring name;
99
100         pstrcpy(name, fname);
101         to_unix(name);
102  
103         return SMB_VFS_NEXT_OPEN(handle, name, fsp, flags, mode);
104 }
105
106 static int catia_rename(vfs_handle_struct *handle,
107                         const char *oldname, const char *newname)
108 {
109         pstring oname, nname;
110
111         pstrcpy(oname, oldname);
112         to_unix(oname);
113         pstrcpy(nname, newname);
114         to_unix(nname);
115
116         DEBUG(10, ("converted old name: %s\n", oname));
117         DEBUG(10, ("converted new name: %s\n", nname));
118  
119         return SMB_VFS_NEXT_RENAME(handle, oname, nname);
120 }
121
122 static int catia_stat(vfs_handle_struct *handle,
123                       const char *fname, SMB_STRUCT_STAT *sbuf)
124 {
125         pstring name;
126         pstrcpy(name, fname);
127         to_unix(name);
128
129         return SMB_VFS_NEXT_STAT(handle, name, sbuf);
130 }
131
132 static int catia_lstat(vfs_handle_struct *handle,
133                        const char *path, SMB_STRUCT_STAT *sbuf)
134 {
135         pstring name;
136         pstrcpy(name, path);
137         to_unix(name);
138
139         return SMB_VFS_NEXT_LSTAT(handle, name, sbuf);
140 }
141
142 static int catia_unlink(vfs_handle_struct *handle, const char *path)
143 {
144         pstring name;
145         pstrcpy(name, path);
146         to_unix(name);
147
148         return SMB_VFS_NEXT_UNLINK(handle, name);
149 }
150
151 static int catia_chmod(vfs_handle_struct *handle,
152                        const char *path, mode_t mode)
153 {
154         pstring name;
155         pstrcpy(name, path);
156         to_unix(name);
157
158         return SMB_VFS_NEXT_CHMOD(handle, name, mode);
159 }
160
161 static int catia_chown(vfs_handle_struct *handle,
162                        const char *path, uid_t uid, gid_t gid)
163 {
164         pstring name;
165         pstrcpy(name, path);
166         to_unix(name);
167
168         return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
169 }
170
171 static int catia_lchown(vfs_handle_struct *handle,
172                        const char *path, uid_t uid, gid_t gid)
173 {
174         pstring name;
175         pstrcpy(name, path);
176         to_unix(name);
177
178         return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
179 }
180
181 static int catia_chdir(vfs_handle_struct *handle,
182                        const char *path)
183 {
184         pstring name;
185         pstrcpy(name, path);
186         to_unix(name);
187
188         return SMB_VFS_NEXT_CHDIR(handle, name);
189 }
190
191 static char *catia_getwd(vfs_handle_struct *handle, char *buf)
192 {
193         return SMB_VFS_NEXT_GETWD(handle, buf);
194 }
195
196 static int catia_ntimes(vfs_handle_struct *handle,
197                        const char *path, const struct timespec ts[2])
198 {
199         return SMB_VFS_NEXT_NTIMES(handle, path, ts);
200 }
201
202 static BOOL catia_symlink(vfs_handle_struct *handle,
203                           const char *oldpath, const char *newpath)
204 {
205         return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
206 }
207
208 static BOOL catia_readlink(vfs_handle_struct *handle,
209                            const char *path, char *buf, size_t bufsiz)
210 {
211         return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
212 }
213
214 static int catia_link(vfs_handle_struct *handle,
215                       const char *oldpath, const char *newpath)
216 {
217         return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
218 }
219
220 static int catia_mknod(vfs_handle_struct *handle,
221                        const char *path, mode_t mode, SMB_DEV_T dev)
222 {
223         return SMB_VFS_NEXT_MKNOD(handle, path, mode, dev);
224 }
225
226 static char *catia_realpath(vfs_handle_struct *handle,
227                             const char *path, char *resolved_path)
228 {
229         return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
230 }
231
232 static size_t catia_get_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
233                                const char *name, uint32 security_info,
234                                struct  security_descriptor **ppdesc)
235 {
236         return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp, name, security_info,
237                                        ppdesc);
238 }
239
240 static NTSTATUS catia_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp, 
241                              const char *name, uint32 security_info_sent,
242                              struct security_descriptor *psd)
243 {
244         return SMB_VFS_NEXT_SET_NT_ACL(handle, fsp, name, security_info_sent,
245                                        psd);
246 }
247
248 static int catia_chmod_acl(vfs_handle_struct *handle,
249                            const char *name, mode_t mode)
250 {
251         /* If the underlying VFS doesn't have ACL support... */
252         if (!handle->vfs_next.ops.chmod_acl) {
253                 errno = ENOSYS;
254                 return -1;
255         }
256         return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
257 }
258
259 /* VFS operations structure */
260
261 static vfs_op_tuple catia_op_tuples[] = {
262
263         /* Directory operations */
264
265         {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR, 
266 SMB_VFS_LAYER_TRANSPARENT},
267         {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR, 
268 SMB_VFS_LAYER_TRANSPARENT},
269
270         /* File operations */
271
272         {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN, 
273 SMB_VFS_LAYER_TRANSPARENT},
274         {SMB_VFS_OP(catia_rename),                      SMB_VFS_OP_RENAME, 
275         SMB_VFS_LAYER_TRANSPARENT},
276         {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT, 
277 SMB_VFS_LAYER_TRANSPARENT},
278         {SMB_VFS_OP(catia_lstat),                       SMB_VFS_OP_LSTAT,  
279 SMB_VFS_LAYER_TRANSPARENT},
280         {SMB_VFS_OP(catia_unlink),                      SMB_VFS_OP_UNLINK, 
281         SMB_VFS_LAYER_TRANSPARENT},
282         {SMB_VFS_OP(catia_chmod),                       SMB_VFS_OP_CHMOD,  
283 SMB_VFS_LAYER_TRANSPARENT},
284         {SMB_VFS_OP(catia_chown),                       SMB_VFS_OP_CHOWN,  
285 SMB_VFS_LAYER_TRANSPARENT},
286         {SMB_VFS_OP(catia_lchown),                      SMB_VFS_OP_LCHOWN,  
287 SMB_VFS_LAYER_TRANSPARENT},
288         {SMB_VFS_OP(catia_chdir),                       SMB_VFS_OP_CHDIR,  
289 SMB_VFS_LAYER_TRANSPARENT},
290         {SMB_VFS_OP(catia_getwd),                       SMB_VFS_OP_GETWD,  
291 SMB_VFS_LAYER_TRANSPARENT},
292         {SMB_VFS_OP(catia_ntimes),                       SMB_VFS_OP_NTIMES,  
293 SMB_VFS_LAYER_TRANSPARENT},
294         {SMB_VFS_OP(catia_symlink), SMB_VFS_OP_SYMLINK, 
295 SMB_VFS_LAYER_TRANSPARENT},
296         {SMB_VFS_OP(catia_readlink), SMB_VFS_OP_READLINK, 
297 SMB_VFS_LAYER_TRANSPARENT},
298         {SMB_VFS_OP(catia_link), SMB_VFS_OP_LINK, 
299 SMB_VFS_LAYER_TRANSPARENT},
300         {SMB_VFS_OP(catia_mknod),                       SMB_VFS_OP_MKNOD,  
301 SMB_VFS_LAYER_TRANSPARENT},
302         {SMB_VFS_OP(catia_realpath), SMB_VFS_OP_REALPATH, 
303 SMB_VFS_LAYER_TRANSPARENT},
304
305         /* NT File ACL operations */
306
307         {SMB_VFS_OP(catia_get_nt_acl), SMB_VFS_OP_GET_NT_ACL, 
308 SMB_VFS_LAYER_TRANSPARENT},
309         {SMB_VFS_OP(catia_set_nt_acl), SMB_VFS_OP_SET_NT_ACL, 
310 SMB_VFS_LAYER_TRANSPARENT},
311
312         /* POSIX ACL operations */
313
314         {SMB_VFS_OP(catia_chmod_acl), SMB_VFS_OP_CHMOD_ACL, 
315 SMB_VFS_LAYER_TRANSPARENT},
316
317
318         {NULL,                                          SMB_VFS_OP_NOOP,   
319 SMB_VFS_LAYER_NOOP}
320 };
321
322 NTSTATUS vfs_catia_init(void);
323 NTSTATUS vfs_catia_init(void)
324 {
325         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia", 
326 catia_op_tuples);
327 }