Fixed build warning "passing arg from incompatible pointer type"
[Samba.git] / source / modules / vfs_catia.c
blob51ee435433d68fd5c20c240b690e54655f96aa3f
1 /*
2 * Catia VFS module
4 * Implement a fixed mapping of forbidden NT characters in filenames that are
5 * used a lot by the CAD package Catia.
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...
11 * Copyright (C) Volker Lendecke, 2005
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.
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.
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/>.
28 #include "includes.h"
30 static char *catia_string_replace(TALLOC_CTX *ctx,
31 const char *s,
32 unsigned char oldc,
33 unsigned char newc)
35 smb_ucs2_t *tmpbuf = NULL;
36 smb_ucs2_t *ptr = NULL;
37 smb_ucs2_t old = oldc;
38 char *ret = NULL;
40 if (!s) {
41 return NULL;
44 if (push_ucs2_talloc(ctx, &tmpbuf, s) == -1) {
45 return NULL;
48 ptr = tmpbuf;
50 for (;*ptr;ptr++) {
51 if (*ptr==old) {
52 *ptr=newc;
56 if (pull_ucs2_talloc(ctx, &ret, tmpbuf) == -1) {
57 TALLOC_FREE(tmpbuf);
58 return NULL;
60 TALLOC_FREE(tmpbuf);
61 return ret;
64 static char *from_unix(TALLOC_CTX *ctx, const char *s)
66 char *ret = catia_string_replace(ctx, s, '\x22', '\xa8');
67 ret = catia_string_replace(ctx, ret, '\x2a', '\xa4');
68 ret = catia_string_replace(ctx, ret, '\x2f', '\xf8');
69 ret = catia_string_replace(ctx, ret, '\x3a', '\xf7');
70 ret = catia_string_replace(ctx, ret, '\x3c', '\xab');
71 ret = catia_string_replace(ctx, ret, '\x3e', '\xbb');
72 ret = catia_string_replace(ctx, ret, '\x3f', '\xbf');
73 ret = catia_string_replace(ctx, ret, '\x5c', '\xff');
74 ret = catia_string_replace(ctx, ret, '\x7c', '\xa6');
75 return catia_string_replace(ctx, ret, ' ', '\xb1');
78 static char *to_unix(TALLOC_CTX *ctx, const char *s)
80 char *ret = catia_string_replace(ctx, s, '\xa8', '\x22');
81 ret = catia_string_replace(ctx, ret, '\xa4', '\x2a');
82 ret = catia_string_replace(ctx, ret, '\xf8', '\x2f');
83 ret = catia_string_replace(ctx, ret, '\xf7', '\x3a');
84 ret = catia_string_replace(ctx, ret, '\xab', '\x3c');
85 ret = catia_string_replace(ctx, ret, '\xbb', '\x3e');
86 ret = catia_string_replace(ctx, ret, '\xbf', '\x3f');
87 ret = catia_string_replace(ctx, ret, '\xff', '\x5c');
88 ret = catia_string_replace(ctx, ret, '\xa6', '\x7c');
89 return catia_string_replace(ctx, ret, '\xb1', ' ');
92 static SMB_STRUCT_DIR *catia_opendir(vfs_handle_struct *handle,
93 const char *fname, const char *mask, uint32 attr)
95 char *name = to_unix(talloc_tos(), fname);
97 if (!name) {
98 errno = ENOMEM;
99 return NULL;
101 return SMB_VFS_NEXT_OPENDIR(handle, name, mask, attr);
104 static SMB_STRUCT_DIRENT *catia_readdir(vfs_handle_struct *handle,
105 SMB_STRUCT_DIR *dirp)
107 SMB_STRUCT_DIRENT *result = SMB_VFS_NEXT_READDIR(handle, dirp);
108 SMB_STRUCT_DIRENT *newdirent;
109 char *newname;
110 size_t newnamelen;
112 if (result == NULL) {
113 return result;
116 newname = from_unix(talloc_tos(), result->d_name);
117 if (!newname) {
118 return NULL;
120 newnamelen = strlen(newname)+1;
121 newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
122 char,
123 sizeof(SMB_STRUCT_DIRENT)+
124 newnamelen);
125 if (!newdirent) {
126 return NULL;
128 memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
129 memcpy(&newdirent->d_name, newname, newnamelen);
130 return newdirent;
133 static int catia_open(vfs_handle_struct *handle,
134 const char *fname,
135 files_struct *fsp,
136 int flags,
137 mode_t mode)
139 char *name = to_unix(talloc_tos(), fname);
141 if (!name) {
142 errno = ENOMEM;
143 return -1;
145 return SMB_VFS_NEXT_OPEN(handle, name, fsp, flags, mode);
148 static int catia_rename(vfs_handle_struct *handle,
149 const char *oldname, const char *newname)
151 TALLOC_CTX *ctx = talloc_tos();
152 char *oname = to_unix(ctx, oldname);
153 char *nname = to_unix(ctx, newname);
155 if (!oname || !nname) {
156 errno = ENOMEM;
157 return -1;
159 DEBUG(10, ("converted old name: %s\n", oname));
160 DEBUG(10, ("converted new name: %s\n", nname));
162 return SMB_VFS_NEXT_RENAME(handle, oname, nname);
165 static int catia_stat(vfs_handle_struct *handle,
166 const char *fname, SMB_STRUCT_STAT *sbuf)
168 char *name = to_unix(talloc_tos(), fname);
170 if (!name) {
171 errno = ENOMEM;
172 return -1;
174 return SMB_VFS_NEXT_STAT(handle, name, sbuf);
177 static int catia_lstat(vfs_handle_struct *handle,
178 const char *path, SMB_STRUCT_STAT *sbuf)
180 char *name = to_unix(talloc_tos(), path);
182 if (!name) {
183 errno = ENOMEM;
184 return -1;
186 return SMB_VFS_NEXT_LSTAT(handle, name, sbuf);
189 static int catia_unlink(vfs_handle_struct *handle, const char *path)
191 char *name = to_unix(talloc_tos(), path);
193 if (!name) {
194 errno = ENOMEM;
195 return -1;
197 return SMB_VFS_NEXT_UNLINK(handle, name);
200 static int catia_chmod(vfs_handle_struct *handle,
201 const char *path, mode_t mode)
203 char *name = to_unix(talloc_tos(), path);
205 if (!name) {
206 errno = ENOMEM;
207 return -1;
209 return SMB_VFS_NEXT_CHMOD(handle, name, mode);
212 static int catia_chown(vfs_handle_struct *handle,
213 const char *path, uid_t uid, gid_t gid)
215 char *name = to_unix(talloc_tos(), path);
217 if (!name) {
218 errno = ENOMEM;
219 return -1;
221 return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
224 static int catia_lchown(vfs_handle_struct *handle,
225 const char *path, uid_t uid, gid_t gid)
227 char *name = to_unix(talloc_tos(), path);
229 if (!name) {
230 errno = ENOMEM;
231 return -1;
233 return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
236 static int catia_chdir(vfs_handle_struct *handle,
237 const char *path)
239 char *name = to_unix(talloc_tos(), path);
241 if (!name) {
242 errno = ENOMEM;
243 return -1;
245 return SMB_VFS_NEXT_CHDIR(handle, name);
248 static char *catia_getwd(vfs_handle_struct *handle, char *buf)
250 return SMB_VFS_NEXT_GETWD(handle, buf);
253 static int catia_ntimes(vfs_handle_struct *handle,
254 const char *path, const struct timespec ts[2])
256 return SMB_VFS_NEXT_NTIMES(handle, path, ts);
259 static bool catia_symlink(vfs_handle_struct *handle,
260 const char *oldpath, const char *newpath)
262 return SMB_VFS_NEXT_SYMLINK(handle, oldpath, newpath);
265 static bool catia_readlink(vfs_handle_struct *handle,
266 const char *path, char *buf, size_t bufsiz)
268 return SMB_VFS_NEXT_READLINK(handle, path, buf, bufsiz);
271 static int catia_link(vfs_handle_struct *handle,
272 const char *oldpath, const char *newpath)
274 return SMB_VFS_NEXT_LINK(handle, oldpath, newpath);
277 static int catia_mknod(vfs_handle_struct *handle,
278 const char *path, mode_t mode, SMB_DEV_T dev)
280 return SMB_VFS_NEXT_MKNOD(handle, path, mode, dev);
283 static char *catia_realpath(vfs_handle_struct *handle,
284 const char *path, char *resolved_path)
286 return SMB_VFS_NEXT_REALPATH(handle, path, resolved_path);
289 static NTSTATUS catia_get_nt_acl(vfs_handle_struct *handle,
290 const char *name, uint32 security_info,
291 struct security_descriptor **ppdesc)
293 return SMB_VFS_NEXT_GET_NT_ACL(handle, name, security_info, ppdesc);
296 static NTSTATUS catia_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
297 const char *name, uint32 security_info_sent,
298 struct security_descriptor *psd)
300 return SMB_VFS_NEXT_SET_NT_ACL(handle, fsp, name, security_info_sent,
301 psd);
304 static int catia_chmod_acl(vfs_handle_struct *handle,
305 const char *name, mode_t mode)
307 /* If the underlying VFS doesn't have ACL support... */
308 if (!handle->vfs_next.ops.chmod_acl) {
309 errno = ENOSYS;
310 return -1;
312 return SMB_VFS_NEXT_CHMOD_ACL(handle, name, mode);
315 /* VFS operations structure */
317 static vfs_op_tuple catia_op_tuples[] = {
319 /* Directory operations */
321 {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
322 SMB_VFS_LAYER_TRANSPARENT},
323 {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
324 SMB_VFS_LAYER_TRANSPARENT},
326 /* File operations */
328 {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
329 SMB_VFS_LAYER_TRANSPARENT},
330 {SMB_VFS_OP(catia_rename), SMB_VFS_OP_RENAME,
331 SMB_VFS_LAYER_TRANSPARENT},
332 {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
333 SMB_VFS_LAYER_TRANSPARENT},
334 {SMB_VFS_OP(catia_lstat), SMB_VFS_OP_LSTAT,
335 SMB_VFS_LAYER_TRANSPARENT},
336 {SMB_VFS_OP(catia_unlink), SMB_VFS_OP_UNLINK,
337 SMB_VFS_LAYER_TRANSPARENT},
338 {SMB_VFS_OP(catia_chmod), SMB_VFS_OP_CHMOD,
339 SMB_VFS_LAYER_TRANSPARENT},
340 {SMB_VFS_OP(catia_chown), SMB_VFS_OP_CHOWN,
341 SMB_VFS_LAYER_TRANSPARENT},
342 {SMB_VFS_OP(catia_lchown), SMB_VFS_OP_LCHOWN,
343 SMB_VFS_LAYER_TRANSPARENT},
344 {SMB_VFS_OP(catia_chdir), SMB_VFS_OP_CHDIR,
345 SMB_VFS_LAYER_TRANSPARENT},
346 {SMB_VFS_OP(catia_getwd), SMB_VFS_OP_GETWD,
347 SMB_VFS_LAYER_TRANSPARENT},
348 {SMB_VFS_OP(catia_ntimes), SMB_VFS_OP_NTIMES,
349 SMB_VFS_LAYER_TRANSPARENT},
350 {SMB_VFS_OP(catia_symlink), SMB_VFS_OP_SYMLINK,
351 SMB_VFS_LAYER_TRANSPARENT},
352 {SMB_VFS_OP(catia_readlink), SMB_VFS_OP_READLINK,
353 SMB_VFS_LAYER_TRANSPARENT},
354 {SMB_VFS_OP(catia_link), SMB_VFS_OP_LINK,
355 SMB_VFS_LAYER_TRANSPARENT},
356 {SMB_VFS_OP(catia_mknod), SMB_VFS_OP_MKNOD,
357 SMB_VFS_LAYER_TRANSPARENT},
358 {SMB_VFS_OP(catia_realpath), SMB_VFS_OP_REALPATH,
359 SMB_VFS_LAYER_TRANSPARENT},
361 /* NT File ACL operations */
363 {SMB_VFS_OP(catia_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
364 SMB_VFS_LAYER_TRANSPARENT},
365 {SMB_VFS_OP(catia_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
366 SMB_VFS_LAYER_TRANSPARENT},
368 /* POSIX ACL operations */
370 {SMB_VFS_OP(catia_chmod_acl), SMB_VFS_OP_CHMOD_ACL,
371 SMB_VFS_LAYER_TRANSPARENT},
374 {NULL, SMB_VFS_OP_NOOP,
375 SMB_VFS_LAYER_NOOP}
378 NTSTATUS vfs_catia_init(void);
379 NTSTATUS vfs_catia_init(void)
381 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
382 catia_op_tuples);