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