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
12 * Copyright (C) Aravind Srinivasan, 2009
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 #define GLOBAL_SNUM 0xFFFFFFF
33 #define MAP_NUM 0x101 /* max unicode charval / MAP_SIZE */
34 #define T_OFFSET(_v_) ((_v_ % MAP_SIZE))
35 #define T_START(_v_) (((_v_ / MAP_SIZE) * MAP_SIZE))
36 #define T_PICK(_v_) ((_v_ / MAP_SIZE))
38 struct char_mappings {
39 smb_ucs2_t entry[MAP_SIZE][2];
42 struct share_mapping_entry {
44 struct share_mapping_entry *next;
45 struct char_mappings **mappings;
48 struct share_mapping_entry *srt_head = NULL;
50 static bool build_table(struct char_mappings **cmaps, int value)
53 int start = T_START(value);
55 (*cmaps) = talloc_zero(NULL, struct char_mappings);
60 for (i = 0; i < MAP_SIZE;i++) {
61 (*cmaps)->entry[i][vfs_translate_to_unix] = start + i;
62 (*cmaps)->entry[i][vfs_translate_to_windows] = start + i;
68 static void set_tables(struct char_mappings **cmaps,
74 /* set unix -> windows */
75 i = T_OFFSET(unix_map);
76 cmaps[T_PICK(unix_map)]->entry[i][vfs_translate_to_windows] = windows_map;
78 /* set windows -> unix */
79 i = T_OFFSET(windows_map);
80 cmaps[T_PICK(windows_map)]->entry[i][vfs_translate_to_unix] = unix_map;
83 static bool build_ranges(struct char_mappings **cmaps,
88 if (!cmaps[T_PICK(unix_map)]) {
89 if (!build_table(&cmaps[T_PICK(unix_map)], unix_map))
93 if (!cmaps[T_PICK(windows_map)]) {
94 if (!build_table(&cmaps[T_PICK(windows_map)], windows_map))
98 set_tables(cmaps, unix_map, windows_map);
103 static struct share_mapping_entry *get_srt(connection_struct *conn,
104 struct share_mapping_entry **global)
106 struct share_mapping_entry *share;
108 for (share = srt_head; share != NULL; share = share->next) {
109 if (share->snum == GLOBAL_SNUM)
112 if (share->snum == SNUM(conn))
119 static struct share_mapping_entry *add_srt(int snum, const char **mappings)
125 long unix_map, windows_map;
126 struct share_mapping_entry *ret = NULL;
128 ret = (struct share_mapping_entry *)
129 TALLOC_ZERO(NULL, sizeof(struct share_mapping_entry) +
130 (mappings ? (MAP_NUM * sizeof(struct char_mappings *)) : 0));
138 ret->mappings = (struct char_mappings**) ((unsigned char*) ret +
139 sizeof(struct share_mapping_entry));
140 memset(ret->mappings, 0,
141 MAP_NUM * sizeof(struct char_mappings *));
143 ret->mappings = NULL;
148 * catia mappings are of the form :
149 * UNIX char (in 0xnn hex) : WINDOWS char (in 0xnn hex)
151 * multiple mappings are comma seperated in smb.conf
153 for (i=0;mappings[i];i++) {
154 fstrcpy(mapping, mappings[i]);
155 unix_map = strtol(mapping, &tmp, 16);
156 if (unix_map == 0 && errno == EINVAL) {
157 DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
160 windows_map = strtol(++tmp, NULL, 16);
161 if (windows_map == 0 && errno == EINVAL) {
162 DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
166 if (!build_ranges(ret->mappings, unix_map, windows_map)) {
167 DEBUG(0, ("TABLE ERROR - CATIA MAPPINGS - %s\n", mapping));
172 ret->next = srt_head;
178 static bool init_mappings(connection_struct *conn,
179 struct share_mapping_entry **selected_out)
181 const char **mappings = NULL;
182 struct share_mapping_entry *share_level = NULL;
183 struct share_mapping_entry *global = NULL;
185 /* check srt cache */
186 share_level = get_srt(conn, &global);
188 *selected_out = share_level;
189 return (share_level->mappings != NULL);
192 /* see if we have a global setting */
195 mappings = lp_parm_string_list(-1, "catia", "mappings", NULL);
196 global = add_srt(GLOBAL_SNUM, mappings);
199 /* no global setting - what about share level ? */
200 mappings = lp_parm_string_list(SNUM(conn), "catia", "mappings", NULL);
201 share_level = add_srt(SNUM(conn), mappings);
203 if (share_level->mappings) {
204 (*selected_out) = share_level;
206 } else if (global->mappings) {
207 share_level->mappings = global->mappings;
208 (*selected_out) = share_level;
215 static NTSTATUS catia_string_replace_allocate(connection_struct *conn,
220 static smb_ucs2_t *tmpbuf = NULL;
222 struct share_mapping_entry *selected;
223 struct char_mappings *map = NULL;
224 size_t converted_size;
225 TALLOC_CTX *ctx = talloc_tos();
227 if (!init_mappings(conn, &selected)) {
228 /* No mappings found. Just use the old name */
229 *mapped_name = talloc_strdup(NULL, name_in);
232 return NT_STATUS_NO_MEMORY;
237 if ((push_ucs2_talloc(ctx, &tmpbuf, name_in,
238 &converted_size)) == -1) {
239 return map_nt_error_from_unix(errno);
245 map = selected->mappings[T_PICK((*ptr))];
251 *ptr = map->entry[T_OFFSET((*ptr))][direction];
254 if ((pull_ucs2_talloc(ctx, mapped_name, tmpbuf,
255 &converted_size)) == -1) {
257 return map_nt_error_from_unix(errno);
263 static SMB_STRUCT_DIR *catia_opendir(vfs_handle_struct *handle,
268 char *name_mapped = NULL;
272 status = catia_string_replace_allocate(handle->conn, fname,
273 &name_mapped, vfs_translate_to_unix);
274 if (!NT_STATUS_IS_OK(status)) {
275 errno = map_errno_from_nt_status(status);
279 ret = SMB_VFS_NEXT_OPENDIR(handle, name_mapped, mask, attr);
280 TALLOC_FREE(name_mapped);
286 * TRANSLATE_NAME call which converts the given name to
287 * "WINDOWS displayable" name
289 static NTSTATUS catia_translate_name(vfs_handle_struct *handle,
291 enum vfs_translate_direction direction)
297 * Copy the supplied name and free the memory for mapped_name,
298 * already allocated by the caller.
299 * We will be allocating new memory for mapped_name in
300 * catia_string_replace_allocate
302 name = talloc_strdup(talloc_tos(), *mapped_name);
305 return NT_STATUS_NO_MEMORY;
307 TALLOC_FREE(*mapped_name);
308 ret = catia_string_replace_allocate(handle->conn, name,
309 mapped_name, direction);
312 if (!NT_STATUS_IS_OK(ret)) {
316 ret = SMB_VFS_NEXT_TRANSLATE_NAME(handle, mapped_name, direction);
321 static int catia_open(vfs_handle_struct *handle,
322 struct smb_filename *smb_fname,
327 char *name_mapped = NULL;
332 tmp_base_name = smb_fname->base_name;
333 status = catia_string_replace_allocate(handle->conn,
334 smb_fname->base_name,
335 &name_mapped, vfs_translate_to_unix);
336 if (!NT_STATUS_IS_OK(status)) {
337 errno = map_errno_from_nt_status(status);
341 smb_fname->base_name = name_mapped;
342 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
343 smb_fname->base_name = tmp_base_name;
344 TALLOC_FREE(name_mapped);
349 static int catia_rename(vfs_handle_struct *handle,
350 const struct smb_filename *smb_fname_src,
351 const struct smb_filename *smb_fname_dst)
353 TALLOC_CTX *ctx = talloc_tos();
354 struct smb_filename *smb_fname_src_tmp = NULL;
355 struct smb_filename *smb_fname_dst_tmp = NULL;
356 char *src_name_mapped = NULL;
357 char *dst_name_mapped = NULL;
361 status = catia_string_replace_allocate(handle->conn,
362 smb_fname_src->base_name,
363 &src_name_mapped, vfs_translate_to_unix);
364 if (!NT_STATUS_IS_OK(status)) {
365 errno = map_errno_from_nt_status(status);
369 status = catia_string_replace_allocate(handle->conn,
370 smb_fname_dst->base_name,
371 &dst_name_mapped, vfs_translate_to_unix);
372 if (!NT_STATUS_IS_OK(status)) {
373 errno = map_errno_from_nt_status(status);
377 /* Setup temporary smb_filename structs. */
378 status = copy_smb_filename(ctx, smb_fname_src, &smb_fname_src_tmp);
380 if (!NT_STATUS_IS_OK(status)) {
381 errno = map_errno_from_nt_status(status);
385 status = copy_smb_filename(ctx, smb_fname_dst, &smb_fname_dst_tmp);
386 if (!NT_STATUS_IS_OK(status)) {
387 errno = map_errno_from_nt_status(status);
391 smb_fname_src_tmp->base_name = src_name_mapped;
392 smb_fname_dst_tmp->base_name = dst_name_mapped;
393 DEBUG(10, ("converted old name: %s\n",
394 smb_fname_str_dbg(smb_fname_src_tmp)));
395 DEBUG(10, ("converted new name: %s\n",
396 smb_fname_str_dbg(smb_fname_dst_tmp)));
398 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
401 TALLOC_FREE(src_name_mapped);
402 TALLOC_FREE(dst_name_mapped);
403 TALLOC_FREE(smb_fname_src_tmp);
404 TALLOC_FREE(smb_fname_dst_tmp);
408 static int catia_stat(vfs_handle_struct *handle,
409 struct smb_filename *smb_fname)
416 status = catia_string_replace_allocate(handle->conn,
417 smb_fname->base_name,
418 &name, vfs_translate_to_unix);
419 if (!NT_STATUS_IS_OK(status)) {
420 errno = map_errno_from_nt_status(status);
424 tmp_base_name = smb_fname->base_name;
425 smb_fname->base_name = name;
427 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
428 smb_fname->base_name = tmp_base_name;
434 static int catia_lstat(vfs_handle_struct *handle,
435 struct smb_filename *smb_fname)
442 status = catia_string_replace_allocate(handle->conn,
443 smb_fname->base_name,
444 &name, vfs_translate_to_unix);
445 if (!NT_STATUS_IS_OK(status)) {
446 errno = map_errno_from_nt_status(status);
450 tmp_base_name = smb_fname->base_name;
451 smb_fname->base_name = name;
453 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
454 smb_fname->base_name = tmp_base_name;
460 static int catia_unlink(vfs_handle_struct *handle,
461 const struct smb_filename *smb_fname)
463 struct smb_filename *smb_fname_tmp = NULL;
468 status = catia_string_replace_allocate(handle->conn,
469 smb_fname->base_name,
470 &name, vfs_translate_to_unix);
471 if (!NT_STATUS_IS_OK(status)) {
472 errno = map_errno_from_nt_status(status);
476 /* Setup temporary smb_filename structs. */
477 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
478 if (!NT_STATUS_IS_OK(status)) {
479 errno = map_errno_from_nt_status(status);
483 smb_fname_tmp->base_name = name;
484 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
485 TALLOC_FREE(smb_fname_tmp);
491 static int catia_chown(vfs_handle_struct *handle,
500 status = catia_string_replace_allocate(handle->conn, path,
501 &name, vfs_translate_to_unix);
502 if (!NT_STATUS_IS_OK(status)) {
503 errno = map_errno_from_nt_status(status);
507 ret = SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
513 static int catia_lchown(vfs_handle_struct *handle,
522 status = catia_string_replace_allocate(handle->conn, path,
523 &name, vfs_translate_to_unix);
524 if (!NT_STATUS_IS_OK(status)) {
525 errno = map_errno_from_nt_status(status);
529 ret = SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
535 static int catia_rmdir(vfs_handle_struct *handle,
542 status = catia_string_replace_allocate(handle->conn, path,
543 &name, vfs_translate_to_unix);
544 if (!NT_STATUS_IS_OK(status)) {
545 errno = map_errno_from_nt_status(status);
549 ret = SMB_VFS_NEXT_RMDIR(handle, name);
555 static int catia_mkdir(vfs_handle_struct *handle,
563 status = catia_string_replace_allocate(handle->conn, path,
564 &name, vfs_translate_to_unix);
565 if (!NT_STATUS_IS_OK(status)) {
566 errno = map_errno_from_nt_status(status);
570 ret = SMB_VFS_NEXT_MKDIR(handle, name, mode);
576 static int catia_chdir(vfs_handle_struct *handle,
583 status = catia_string_replace_allocate(handle->conn, path,
584 &name, vfs_translate_to_unix);
585 if (!NT_STATUS_IS_OK(status)) {
586 errno = map_errno_from_nt_status(status);
590 ret = SMB_VFS_NEXT_CHDIR(handle, name);
596 static int catia_ntimes(vfs_handle_struct *handle,
597 const struct smb_filename *smb_fname,
598 struct smb_file_time *ft)
600 struct smb_filename *smb_fname_tmp = NULL;
605 status = catia_string_replace_allocate(handle->conn,
606 smb_fname->base_name,
607 &name, vfs_translate_to_unix);
608 if (!NT_STATUS_IS_OK(status)) {
609 errno = map_errno_from_nt_status(status);
613 status = copy_smb_filename(talloc_tos(), smb_fname, &smb_fname_tmp);
614 if (!NT_STATUS_IS_OK(status)) {
615 errno = map_errno_from_nt_status(status);
619 smb_fname_tmp->base_name = name;
620 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
622 TALLOC_FREE(smb_fname_tmp);
628 catia_realpath(vfs_handle_struct *handle, const char *path,
631 char *mapped_name = NULL;
635 status = catia_string_replace_allocate(handle->conn, path,
636 &mapped_name, vfs_translate_to_unix);
637 if (!NT_STATUS_IS_OK(status)) {
638 errno = map_errno_from_nt_status(status);
642 ret = SMB_VFS_NEXT_REALPATH(handle, mapped_name, resolved_path);
643 TALLOC_FREE(mapped_name);
648 static int catia_chflags(struct vfs_handle_struct *handle,
649 const char *path, unsigned int flags)
651 char *mapped_name = NULL;
655 status = catia_string_replace_allocate(handle->conn, path,
656 &mapped_name, vfs_translate_to_unix);
657 if (!NT_STATUS_IS_OK(status)) {
658 errno = map_errno_from_nt_status(status);
662 ret = SMB_VFS_NEXT_CHFLAGS(handle, mapped_name, flags);
663 TALLOC_FREE(mapped_name);
669 catia_streaminfo(struct vfs_handle_struct *handle,
670 struct files_struct *fsp,
673 unsigned int *num_streams,
674 struct stream_struct **streams)
676 char *mapped_name = NULL;
679 status = catia_string_replace_allocate(handle->conn, path,
680 &mapped_name, vfs_translate_to_unix);
681 if (!NT_STATUS_IS_OK(status)) {
682 errno = map_errno_from_nt_status(status);
686 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, mapped_name,
687 mem_ctx, num_streams,streams);
688 TALLOC_FREE(mapped_name);
694 catia_get_nt_acl(struct vfs_handle_struct *handle,
696 uint32 security_info,
697 struct security_descriptor **ppdesc)
699 char *mapped_name = NULL;
702 status = catia_string_replace_allocate(handle->conn,
703 path, &mapped_name, vfs_translate_to_unix);
704 if (!NT_STATUS_IS_OK(status)) {
705 errno = map_errno_from_nt_status(status);
708 status = SMB_VFS_NEXT_GET_NT_ACL(handle, mapped_name,
709 security_info, ppdesc);
710 TALLOC_FREE(mapped_name);
716 catia_chmod_acl(vfs_handle_struct *handle,
720 char *mapped_name = NULL;
724 status = catia_string_replace_allocate(handle->conn,
725 path, &mapped_name, vfs_translate_to_unix);
726 if (!NT_STATUS_IS_OK(status)) {
727 errno = map_errno_from_nt_status(status);
731 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, mapped_name, mode);
732 TALLOC_FREE(mapped_name);
737 catia_sys_acl_get_file(vfs_handle_struct *handle,
741 char *mapped_name = NULL;
745 status = catia_string_replace_allocate(handle->conn,
746 path, &mapped_name, vfs_translate_to_unix);
747 if (!NT_STATUS_IS_OK(status)) {
748 errno = map_errno_from_nt_status(status);
752 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, mapped_name, type);
753 TALLOC_FREE(mapped_name);
759 catia_sys_acl_set_file(vfs_handle_struct *handle,
764 char *mapped_name = NULL;
768 status = catia_string_replace_allocate(handle->conn,
769 path, &mapped_name, vfs_translate_to_unix);
770 if (!NT_STATUS_IS_OK(status)) {
771 errno = map_errno_from_nt_status(status);
775 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, mapped_name, type, theacl);
776 TALLOC_FREE(mapped_name);
782 catia_sys_acl_delete_def_file(vfs_handle_struct *handle,
785 char *mapped_name = NULL;
789 status = catia_string_replace_allocate(handle->conn,
790 path, &mapped_name, vfs_translate_to_unix);
791 if (!NT_STATUS_IS_OK(status)) {
792 errno = map_errno_from_nt_status(status);
796 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, mapped_name);
797 TALLOC_FREE(mapped_name);
803 catia_getxattr(vfs_handle_struct *handle, const char *path,
804 const char *name, void *value, size_t size)
806 char *mapped_name = NULL;
810 status = catia_string_replace_allocate(handle->conn,
811 name, &mapped_name, vfs_translate_to_unix);
812 if (!NT_STATUS_IS_OK(status)) {
813 errno = map_errno_from_nt_status(status);
818 ret = SMB_VFS_NEXT_GETXATTR(handle, path, mapped_name, value, size);
819 TALLOC_FREE(mapped_name);
825 catia_lgetxattr(vfs_handle_struct *handle, const char *path,
826 const char *name, void *value, size_t size)
828 char *mapped_name = NULL;
832 status = catia_string_replace_allocate(handle->conn,
833 name, &mapped_name, vfs_translate_to_unix);
834 if (!NT_STATUS_IS_OK(status)) {
835 errno = map_errno_from_nt_status(status);
840 ret = SMB_VFS_NEXT_LGETXATTR(handle, path, mapped_name, value, size);
841 TALLOC_FREE(mapped_name);
847 catia_listxattr(vfs_handle_struct *handle, const char *path,
848 char *list, size_t size)
850 char *mapped_name = NULL;
854 status = catia_string_replace_allocate(handle->conn,
855 path, &mapped_name, vfs_translate_to_unix);
856 if (!NT_STATUS_IS_OK(status)) {
857 errno = map_errno_from_nt_status(status);
862 ret = SMB_VFS_NEXT_LISTXATTR(handle, mapped_name, list, size);
863 TALLOC_FREE(mapped_name);
869 catia_llistxattr(vfs_handle_struct *handle, const char *path,
870 char *list, size_t size)
872 char *mapped_name = NULL;
876 status = catia_string_replace_allocate(handle->conn,
877 path, &mapped_name, vfs_translate_to_unix);
878 if (!NT_STATUS_IS_OK(status)) {
879 errno = map_errno_from_nt_status(status);
884 ret = SMB_VFS_NEXT_LLISTXATTR(handle, mapped_name, list, size);
885 TALLOC_FREE(mapped_name);
891 catia_removexattr(vfs_handle_struct *handle, const char *path,
894 char *mapped_name = NULL;
898 status = catia_string_replace_allocate(handle->conn,
899 name, &mapped_name, vfs_translate_to_unix);
900 if (!NT_STATUS_IS_OK(status)) {
901 errno = map_errno_from_nt_status(status);
906 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, path, mapped_name);
907 TALLOC_FREE(mapped_name);
913 catia_lremovexattr(vfs_handle_struct *handle, const char *path,
916 char *mapped_name = NULL;
920 status = catia_string_replace_allocate(handle->conn,
921 name, &mapped_name, vfs_translate_to_unix);
922 if (!NT_STATUS_IS_OK(status)) {
923 errno = map_errno_from_nt_status(status);
928 ret = SMB_VFS_NEXT_LREMOVEXATTR(handle, path, mapped_name);
929 TALLOC_FREE(mapped_name);
935 catia_setxattr(vfs_handle_struct *handle, const char *path,
936 const char *name, const void *value, size_t size,
939 char *mapped_name = NULL;
943 status = catia_string_replace_allocate(handle->conn,
944 name, &mapped_name, vfs_translate_to_unix);
945 if (!NT_STATUS_IS_OK(status)) {
946 errno = map_errno_from_nt_status(status);
951 ret = SMB_VFS_NEXT_SETXATTR(handle, path, mapped_name, value, size, flags);
952 TALLOC_FREE(mapped_name);
958 catia_lsetxattr(vfs_handle_struct *handle, const char *path,
959 const char *name, const void *value, size_t size,
962 char *mapped_name = NULL;
966 status = catia_string_replace_allocate(handle->conn,
967 name, &mapped_name, vfs_translate_to_unix);
968 if (!NT_STATUS_IS_OK(status)) {
969 errno = map_errno_from_nt_status(status);
974 ret = SMB_VFS_NEXT_LSETXATTR(handle, path, mapped_name, value, size, flags);
975 TALLOC_FREE(mapped_name);
980 static struct vfs_fn_pointers vfs_catia_fns = {
981 .mkdir = catia_mkdir,
982 .rmdir = catia_rmdir,
983 .opendir = catia_opendir,
985 .rename = catia_rename,
987 .lstat = catia_lstat,
988 .unlink = catia_unlink,
989 .chown = catia_chown,
990 .lchown = catia_lchown,
991 .chdir = catia_chdir,
992 .ntimes = catia_ntimes,
993 .realpath = catia_realpath,
994 .chflags = catia_chflags,
995 .streaminfo = catia_streaminfo,
996 .translate_name = catia_translate_name,
997 .get_nt_acl = catia_get_nt_acl,
998 .chmod_acl = catia_chmod_acl,
999 .sys_acl_get_file = catia_sys_acl_get_file,
1000 .sys_acl_set_file = catia_sys_acl_set_file,
1001 .sys_acl_delete_def_file = catia_sys_acl_delete_def_file,
1002 .getxattr = catia_getxattr,
1003 .lgetxattr = catia_lgetxattr,
1004 .listxattr = catia_listxattr,
1005 .llistxattr = catia_llistxattr,
1006 .removexattr = catia_removexattr,
1007 .lremovexattr = catia_lremovexattr,
1008 .setxattr = catia_setxattr,
1009 .lsetxattr = catia_lsetxattr,
1012 NTSTATUS vfs_catia_init(void)
1014 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",