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
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/>.
30 static char *catia_string_replace(TALLOC_CTX *ctx,
35 smb_ucs2_t *tmpbuf = NULL;
36 smb_ucs2_t *ptr = NULL;
37 smb_ucs2_t old = oldc;
39 size_t converted_size;
45 if (!push_ucs2_talloc(ctx, &tmpbuf, s, &converted_size)) {
57 if (!pull_ucs2_talloc(ctx, &ret, tmpbuf, &converted_size)) {
65 static char *from_unix(TALLOC_CTX *ctx, const char *s)
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');
79 static char *to_unix(TALLOC_CTX *ctx, const char *s)
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', ' ');
93 static SMB_STRUCT_DIR *catia_opendir(vfs_handle_struct *handle,
94 const char *fname, const char *mask, uint32 attr)
96 char *name = to_unix(talloc_tos(), fname);
102 return SMB_VFS_NEXT_OPENDIR(handle, name, mask, attr);
105 static SMB_STRUCT_DIRENT *catia_readdir(vfs_handle_struct *handle,
106 SMB_STRUCT_DIR *dirp)
108 SMB_STRUCT_DIRENT *result = NULL;
109 SMB_STRUCT_DIRENT *newdirent = NULL;
113 result = SMB_VFS_NEXT_READDIR(handle, dirp, NULL);
114 if (result == NULL) {
118 newname = from_unix(talloc_tos(), result->d_name);
122 newnamelen = strlen(newname)+1;
123 newdirent = (SMB_STRUCT_DIRENT *)TALLOC_ARRAY(talloc_tos(),
125 sizeof(SMB_STRUCT_DIRENT)+
130 memcpy(newdirent, result, sizeof(SMB_STRUCT_DIRENT));
131 memcpy(&newdirent->d_name, newname, newnamelen);
135 static int catia_open(vfs_handle_struct *handle,
136 struct smb_filename *smb_fname,
145 name = to_unix(talloc_tos(), smb_fname->base_name);
151 tmp_base_name = smb_fname->base_name;
152 smb_fname->base_name = name;
154 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
156 smb_fname->base_name = tmp_base_name;
162 static int catia_rename(vfs_handle_struct *handle,
163 const struct smb_filename *smb_fname_src,
164 const struct smb_filename *smb_fname_dst)
166 TALLOC_CTX *ctx = talloc_tos();
169 struct smb_filename *smb_fname_src_tmp = NULL;
170 struct smb_filename *smb_fname_dst_tmp = NULL;
174 oname = to_unix(ctx, smb_fname_src->base_name);
175 nname = to_unix(ctx, smb_fname_dst->base_name);
176 if (!oname || !nname) {
181 /* Setup temporary smb_filename structs. */
182 status = copy_smb_filename(talloc_tos(), smb_fname_src,
184 if (!NT_STATUS_IS_OK(status)) {
185 errno = map_errno_from_nt_status(status);
188 status = copy_smb_filename(talloc_tos(), smb_fname_dst,
190 if (!NT_STATUS_IS_OK(status)) {
191 errno = map_errno_from_nt_status(status);
195 smb_fname_src_tmp->base_name = oname;
196 smb_fname_dst_tmp->base_name = nname;
198 DEBUG(10, ("converted old name: %s\n",
199 smb_fname_str_dbg(smb_fname_src_tmp)));
200 DEBUG(10, ("converted new name: %s\n",
201 smb_fname_str_dbg(smb_fname_dst_tmp)));
203 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
208 TALLOC_FREE(smb_fname_src_tmp);
209 TALLOC_FREE(smb_fname_dst_tmp);
213 static int catia_stat(vfs_handle_struct *handle,
214 struct smb_filename *smb_fname)
220 name = to_unix(talloc_tos(), smb_fname->base_name);
226 tmp_base_name = smb_fname->base_name;
227 smb_fname->base_name = name;
229 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
231 smb_fname->base_name = tmp_base_name;
237 static int catia_lstat(vfs_handle_struct *handle,
238 struct smb_filename *smb_fname)
244 name = to_unix(talloc_tos(), smb_fname->base_name);
250 tmp_base_name = smb_fname->base_name;
251 smb_fname->base_name = name;
253 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
255 smb_fname->base_name = tmp_base_name;
261 static int catia_unlink(vfs_handle_struct *handle,
262 const struct smb_filename *smb_fname)
264 struct smb_filename *smb_fname_tmp = NULL;
269 name = to_unix(talloc_tos(), smb_fname->base_name);
275 /* Setup temporary smb_filename structs. */
276 status = copy_smb_filename(talloc_tos(), smb_fname,
278 if (!NT_STATUS_IS_OK(status)) {
279 errno = map_errno_from_nt_status(status);
283 smb_fname_tmp->base_name = name;
285 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
287 TALLOC_FREE(smb_fname_tmp);
291 static int catia_chmod(vfs_handle_struct *handle,
292 const char *path, mode_t mode)
294 char *name = to_unix(talloc_tos(), path);
300 return SMB_VFS_NEXT_CHMOD(handle, name, mode);
303 static int catia_chown(vfs_handle_struct *handle,
304 const char *path, uid_t uid, gid_t gid)
306 char *name = to_unix(talloc_tos(), path);
312 return SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
315 static int catia_lchown(vfs_handle_struct *handle,
316 const char *path, uid_t uid, gid_t gid)
318 char *name = to_unix(talloc_tos(), path);
324 return SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
327 static int catia_chdir(vfs_handle_struct *handle,
330 char *name = to_unix(talloc_tos(), path);
336 return SMB_VFS_NEXT_CHDIR(handle, name);
339 /* VFS operations structure */
341 static vfs_op_tuple catia_op_tuples[] = {
343 /* Directory operations */
345 {SMB_VFS_OP(catia_opendir), SMB_VFS_OP_OPENDIR,
346 SMB_VFS_LAYER_TRANSPARENT},
347 {SMB_VFS_OP(catia_readdir), SMB_VFS_OP_READDIR,
348 SMB_VFS_LAYER_TRANSPARENT},
350 /* File operations */
352 {SMB_VFS_OP(catia_open), SMB_VFS_OP_OPEN,
353 SMB_VFS_LAYER_TRANSPARENT},
354 {SMB_VFS_OP(catia_rename), SMB_VFS_OP_RENAME,
355 SMB_VFS_LAYER_TRANSPARENT},
356 {SMB_VFS_OP(catia_stat), SMB_VFS_OP_STAT,
357 SMB_VFS_LAYER_TRANSPARENT},
358 {SMB_VFS_OP(catia_lstat), SMB_VFS_OP_LSTAT,
359 SMB_VFS_LAYER_TRANSPARENT},
360 {SMB_VFS_OP(catia_unlink), SMB_VFS_OP_UNLINK,
361 SMB_VFS_LAYER_TRANSPARENT},
362 {SMB_VFS_OP(catia_chmod), SMB_VFS_OP_CHMOD,
363 SMB_VFS_LAYER_TRANSPARENT},
364 {SMB_VFS_OP(catia_chown), SMB_VFS_OP_CHOWN,
365 SMB_VFS_LAYER_TRANSPARENT},
366 {SMB_VFS_OP(catia_lchown), SMB_VFS_OP_LCHOWN,
367 SMB_VFS_LAYER_TRANSPARENT},
368 {SMB_VFS_OP(catia_chdir), SMB_VFS_OP_CHDIR,
369 SMB_VFS_LAYER_TRANSPARENT},
371 {NULL, SMB_VFS_OP_NOOP,
375 NTSTATUS vfs_catia_init(void);
376 NTSTATUS vfs_catia_init(void)
378 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",