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
13 * Copyright (C) Guenter Kukkukk, 2013
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
31 #include "smbd/smbd.h"
33 static int vfs_catia_debug_level = DBGC_VFS;
36 #define DBGC_CLASS vfs_catia_debug_level
38 #define GLOBAL_SNUM 0xFFFFFFF
40 #define MAP_NUM 0x101 /* max unicode charval / MAP_SIZE */
41 #define T_OFFSET(_v_) ((_v_ % MAP_SIZE))
42 #define T_START(_v_) (((_v_ / MAP_SIZE) * MAP_SIZE))
43 #define T_PICK(_v_) ((_v_ / MAP_SIZE))
45 struct char_mappings {
46 smb_ucs2_t entry[MAP_SIZE][2];
49 struct share_mapping_entry {
51 struct share_mapping_entry *next;
52 struct char_mappings **mappings;
55 struct share_mapping_entry *srt_head = NULL;
57 static bool build_table(struct char_mappings **cmaps, int value)
60 int start = T_START(value);
62 (*cmaps) = talloc_zero(NULL, struct char_mappings);
67 for (i = 0; i < MAP_SIZE;i++) {
68 (*cmaps)->entry[i][vfs_translate_to_unix] = start + i;
69 (*cmaps)->entry[i][vfs_translate_to_windows] = start + i;
75 static void set_tables(struct char_mappings **cmaps,
81 /* set unix -> windows */
82 i = T_OFFSET(unix_map);
83 cmaps[T_PICK(unix_map)]->entry[i][vfs_translate_to_windows] = windows_map;
85 /* set windows -> unix */
86 i = T_OFFSET(windows_map);
87 cmaps[T_PICK(windows_map)]->entry[i][vfs_translate_to_unix] = unix_map;
90 static bool build_ranges(struct char_mappings **cmaps,
95 if (!cmaps[T_PICK(unix_map)]) {
96 if (!build_table(&cmaps[T_PICK(unix_map)], unix_map))
100 if (!cmaps[T_PICK(windows_map)]) {
101 if (!build_table(&cmaps[T_PICK(windows_map)], windows_map))
105 set_tables(cmaps, unix_map, windows_map);
110 static struct share_mapping_entry *get_srt(connection_struct *conn,
111 struct share_mapping_entry **global)
113 struct share_mapping_entry *share;
115 for (share = srt_head; share != NULL; share = share->next) {
116 if (share->snum == GLOBAL_SNUM)
119 if (share->snum == SNUM(conn))
126 static struct share_mapping_entry *add_srt(int snum, const char **mappings)
132 long unix_map, windows_map;
133 struct share_mapping_entry *ret = NULL;
135 ret = (struct share_mapping_entry *)
136 TALLOC_ZERO(NULL, sizeof(struct share_mapping_entry) +
137 (mappings ? (MAP_NUM * sizeof(struct char_mappings *)) : 0));
145 ret->mappings = (struct char_mappings**) ((unsigned char*) ret +
146 sizeof(struct share_mapping_entry));
147 memset(ret->mappings, 0,
148 MAP_NUM * sizeof(struct char_mappings *));
150 ret->mappings = NULL;
155 * catia mappings are of the form :
156 * UNIX char (in 0xnn hex) : WINDOWS char (in 0xnn hex)
158 * multiple mappings are comma separated in smb.conf
160 for (i=0;mappings[i];i++) {
161 fstrcpy(mapping, mappings[i]);
162 unix_map = strtol(mapping, &tmp, 16);
163 if (unix_map == 0 && errno == EINVAL) {
164 DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
167 windows_map = strtol(++tmp, NULL, 16);
168 if (windows_map == 0 && errno == EINVAL) {
169 DEBUG(0, ("INVALID CATIA MAPPINGS - %s\n", mapping));
173 if (!build_ranges(ret->mappings, unix_map, windows_map)) {
174 DEBUG(0, ("TABLE ERROR - CATIA MAPPINGS - %s\n", mapping));
179 ret->next = srt_head;
185 static bool init_mappings(connection_struct *conn,
186 struct share_mapping_entry **selected_out)
188 const char **mappings = NULL;
189 struct share_mapping_entry *share_level = NULL;
190 struct share_mapping_entry *global = NULL;
192 /* check srt cache */
193 share_level = get_srt(conn, &global);
195 *selected_out = share_level;
196 return (share_level->mappings != NULL);
199 /* see if we have a global setting */
202 mappings = lp_parm_string_list(-1, "catia", "mappings", NULL);
203 global = add_srt(GLOBAL_SNUM, mappings);
206 /* no global setting - what about share level ? */
207 mappings = lp_parm_string_list(SNUM(conn), "catia", "mappings", NULL);
208 share_level = add_srt(SNUM(conn), mappings);
210 if (share_level->mappings) {
211 (*selected_out) = share_level;
214 if (global->mappings) {
215 share_level->mappings = global->mappings;
216 (*selected_out) = share_level;
223 static NTSTATUS catia_string_replace_allocate(connection_struct *conn,
226 enum vfs_translate_direction direction)
228 static smb_ucs2_t *tmpbuf = NULL;
230 struct share_mapping_entry *selected;
231 struct char_mappings *map = NULL;
232 size_t converted_size;
233 TALLOC_CTX *ctx = talloc_tos();
235 if (!init_mappings(conn, &selected)) {
236 /* No mappings found. Just use the old name */
237 *mapped_name = talloc_strdup(NULL, name_in);
240 return NT_STATUS_NO_MEMORY;
245 if ((push_ucs2_talloc(ctx, &tmpbuf, name_in,
246 &converted_size)) == false) {
247 return map_nt_error_from_unix(errno);
253 map = selected->mappings[T_PICK((*ptr))];
259 *ptr = map->entry[T_OFFSET((*ptr))][direction];
262 if ((pull_ucs2_talloc(ctx, mapped_name, tmpbuf,
263 &converted_size)) == false) {
265 return map_nt_error_from_unix(errno);
271 static DIR *catia_opendir(vfs_handle_struct *handle,
276 char *name_mapped = NULL;
280 status = catia_string_replace_allocate(handle->conn, fname,
281 &name_mapped, vfs_translate_to_unix);
282 if (!NT_STATUS_IS_OK(status)) {
283 errno = map_errno_from_nt_status(status);
287 ret = SMB_VFS_NEXT_OPENDIR(handle, name_mapped, mask, attr);
288 TALLOC_FREE(name_mapped);
294 * TRANSLATE_NAME call which converts the given name to
295 * "WINDOWS displayable" name
297 static NTSTATUS catia_translate_name(struct vfs_handle_struct *handle,
298 const char *orig_name,
299 enum vfs_translate_direction direction,
305 NTSTATUS status, ret;
308 * Copy the supplied name and free the memory for mapped_name,
309 * already allocated by the caller.
310 * We will be allocating new memory for mapped_name in
311 * catia_string_replace_allocate
313 name = talloc_strdup(talloc_tos(), orig_name);
316 return NT_STATUS_NO_MEMORY;
318 status = catia_string_replace_allocate(handle->conn, name,
319 &mapped_name, direction);
322 if (!NT_STATUS_IS_OK(status)) {
326 ret = SMB_VFS_NEXT_TRANSLATE_NAME(handle, mapped_name, direction,
327 mem_ctx, pmapped_name);
329 if (NT_STATUS_EQUAL(ret, NT_STATUS_NONE_MAPPED)) {
330 *pmapped_name = talloc_move(mem_ctx, &mapped_name);
331 /* we need to return the former translation result here */
334 TALLOC_FREE(mapped_name);
340 static int catia_open(vfs_handle_struct *handle,
341 struct smb_filename *smb_fname,
346 char *name_mapped = NULL;
351 tmp_base_name = smb_fname->base_name;
352 status = catia_string_replace_allocate(handle->conn,
353 smb_fname->base_name,
354 &name_mapped, vfs_translate_to_unix);
355 if (!NT_STATUS_IS_OK(status)) {
356 errno = map_errno_from_nt_status(status);
360 smb_fname->base_name = name_mapped;
361 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
362 smb_fname->base_name = tmp_base_name;
363 TALLOC_FREE(name_mapped);
368 static int catia_rename(vfs_handle_struct *handle,
369 const struct smb_filename *smb_fname_src,
370 const struct smb_filename *smb_fname_dst)
372 TALLOC_CTX *ctx = talloc_tos();
373 struct smb_filename *smb_fname_src_tmp = NULL;
374 struct smb_filename *smb_fname_dst_tmp = NULL;
375 char *src_name_mapped = NULL;
376 char *dst_name_mapped = NULL;
380 status = catia_string_replace_allocate(handle->conn,
381 smb_fname_src->base_name,
382 &src_name_mapped, vfs_translate_to_unix);
383 if (!NT_STATUS_IS_OK(status)) {
384 errno = map_errno_from_nt_status(status);
388 status = catia_string_replace_allocate(handle->conn,
389 smb_fname_dst->base_name,
390 &dst_name_mapped, vfs_translate_to_unix);
391 if (!NT_STATUS_IS_OK(status)) {
392 errno = map_errno_from_nt_status(status);
396 /* Setup temporary smb_filename structs. */
397 smb_fname_src_tmp = cp_smb_filename(ctx, smb_fname_src);
398 if (smb_fname_src_tmp == NULL) {
403 smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
404 if (smb_fname_dst_tmp == NULL) {
409 smb_fname_src_tmp->base_name = src_name_mapped;
410 smb_fname_dst_tmp->base_name = dst_name_mapped;
411 DEBUG(10, ("converted old name: %s\n",
412 smb_fname_str_dbg(smb_fname_src_tmp)));
413 DEBUG(10, ("converted new name: %s\n",
414 smb_fname_str_dbg(smb_fname_dst_tmp)));
416 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
419 TALLOC_FREE(src_name_mapped);
420 TALLOC_FREE(dst_name_mapped);
421 TALLOC_FREE(smb_fname_src_tmp);
422 TALLOC_FREE(smb_fname_dst_tmp);
426 static int catia_stat(vfs_handle_struct *handle,
427 struct smb_filename *smb_fname)
434 status = catia_string_replace_allocate(handle->conn,
435 smb_fname->base_name,
436 &name, vfs_translate_to_unix);
437 if (!NT_STATUS_IS_OK(status)) {
438 errno = map_errno_from_nt_status(status);
442 tmp_base_name = smb_fname->base_name;
443 smb_fname->base_name = name;
445 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
446 smb_fname->base_name = tmp_base_name;
452 static int catia_lstat(vfs_handle_struct *handle,
453 struct smb_filename *smb_fname)
460 status = catia_string_replace_allocate(handle->conn,
461 smb_fname->base_name,
462 &name, vfs_translate_to_unix);
463 if (!NT_STATUS_IS_OK(status)) {
464 errno = map_errno_from_nt_status(status);
468 tmp_base_name = smb_fname->base_name;
469 smb_fname->base_name = name;
471 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
472 smb_fname->base_name = tmp_base_name;
478 static int catia_unlink(vfs_handle_struct *handle,
479 const struct smb_filename *smb_fname)
481 struct smb_filename *smb_fname_tmp = NULL;
486 status = catia_string_replace_allocate(handle->conn,
487 smb_fname->base_name,
488 &name, vfs_translate_to_unix);
489 if (!NT_STATUS_IS_OK(status)) {
490 errno = map_errno_from_nt_status(status);
494 /* Setup temporary smb_filename structs. */
495 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
496 if (smb_fname_tmp == NULL) {
501 smb_fname_tmp->base_name = name;
502 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_tmp);
503 TALLOC_FREE(smb_fname_tmp);
509 static int catia_chown(vfs_handle_struct *handle,
518 status = catia_string_replace_allocate(handle->conn, path,
519 &name, vfs_translate_to_unix);
520 if (!NT_STATUS_IS_OK(status)) {
521 errno = map_errno_from_nt_status(status);
525 ret = SMB_VFS_NEXT_CHOWN(handle, name, uid, gid);
531 static int catia_lchown(vfs_handle_struct *handle,
540 status = catia_string_replace_allocate(handle->conn, path,
541 &name, vfs_translate_to_unix);
542 if (!NT_STATUS_IS_OK(status)) {
543 errno = map_errno_from_nt_status(status);
547 ret = SMB_VFS_NEXT_LCHOWN(handle, name, uid, gid);
553 static int catia_chmod(vfs_handle_struct *handle, const char *path, mode_t mode)
559 status = catia_string_replace_allocate(handle->conn, path,
560 &name, vfs_translate_to_unix);
561 if (!NT_STATUS_IS_OK(status)) {
562 errno = map_errno_from_nt_status(status);
566 ret = SMB_VFS_NEXT_CHMOD(handle, name, mode);
572 static int catia_rmdir(vfs_handle_struct *handle,
579 status = catia_string_replace_allocate(handle->conn, path,
580 &name, vfs_translate_to_unix);
581 if (!NT_STATUS_IS_OK(status)) {
582 errno = map_errno_from_nt_status(status);
586 ret = SMB_VFS_NEXT_RMDIR(handle, name);
592 static int catia_mkdir(vfs_handle_struct *handle,
600 status = catia_string_replace_allocate(handle->conn, path,
601 &name, vfs_translate_to_unix);
602 if (!NT_STATUS_IS_OK(status)) {
603 errno = map_errno_from_nt_status(status);
607 ret = SMB_VFS_NEXT_MKDIR(handle, name, mode);
613 static int catia_chdir(vfs_handle_struct *handle,
620 status = catia_string_replace_allocate(handle->conn, path,
621 &name, vfs_translate_to_unix);
622 if (!NT_STATUS_IS_OK(status)) {
623 errno = map_errno_from_nt_status(status);
627 ret = SMB_VFS_NEXT_CHDIR(handle, name);
633 static int catia_ntimes(vfs_handle_struct *handle,
634 const struct smb_filename *smb_fname,
635 struct smb_file_time *ft)
637 struct smb_filename *smb_fname_tmp = NULL;
642 status = catia_string_replace_allocate(handle->conn,
643 smb_fname->base_name,
644 &name, vfs_translate_to_unix);
645 if (!NT_STATUS_IS_OK(status)) {
646 errno = map_errno_from_nt_status(status);
650 smb_fname_tmp = cp_smb_filename(talloc_tos(), smb_fname);
651 if (smb_fname_tmp == NULL) {
656 smb_fname_tmp->base_name = name;
657 ret = SMB_VFS_NEXT_NTIMES(handle, smb_fname_tmp, ft);
659 TALLOC_FREE(smb_fname_tmp);
665 catia_realpath(vfs_handle_struct *handle, const char *path)
667 char *mapped_name = NULL;
671 status = catia_string_replace_allocate(handle->conn, path,
672 &mapped_name, vfs_translate_to_unix);
673 if (!NT_STATUS_IS_OK(status)) {
674 errno = map_errno_from_nt_status(status);
678 ret = SMB_VFS_NEXT_REALPATH(handle, mapped_name);
679 TALLOC_FREE(mapped_name);
684 static int catia_chflags(struct vfs_handle_struct *handle,
685 const char *path, unsigned int flags)
687 char *mapped_name = NULL;
691 status = catia_string_replace_allocate(handle->conn, path,
692 &mapped_name, vfs_translate_to_unix);
693 if (!NT_STATUS_IS_OK(status)) {
694 errno = map_errno_from_nt_status(status);
698 ret = SMB_VFS_NEXT_CHFLAGS(handle, mapped_name, flags);
699 TALLOC_FREE(mapped_name);
705 catia_streaminfo(struct vfs_handle_struct *handle,
706 struct files_struct *fsp,
709 unsigned int *num_streams,
710 struct stream_struct **streams)
712 char *mapped_name = NULL;
715 status = catia_string_replace_allocate(handle->conn, path,
716 &mapped_name, vfs_translate_to_unix);
717 if (!NT_STATUS_IS_OK(status)) {
718 errno = map_errno_from_nt_status(status);
722 status = SMB_VFS_NEXT_STREAMINFO(handle, fsp, mapped_name,
723 mem_ctx, num_streams,streams);
724 TALLOC_FREE(mapped_name);
730 catia_get_nt_acl(struct vfs_handle_struct *handle,
732 uint32_t security_info,
734 struct security_descriptor **ppdesc)
736 char *mapped_name = NULL;
739 status = catia_string_replace_allocate(handle->conn,
740 path, &mapped_name, vfs_translate_to_unix);
741 if (!NT_STATUS_IS_OK(status)) {
742 errno = map_errno_from_nt_status(status);
745 status = SMB_VFS_NEXT_GET_NT_ACL(handle, mapped_name,
746 security_info, mem_ctx, ppdesc);
747 TALLOC_FREE(mapped_name);
753 catia_chmod_acl(vfs_handle_struct *handle,
757 char *mapped_name = NULL;
761 status = catia_string_replace_allocate(handle->conn,
762 path, &mapped_name, vfs_translate_to_unix);
763 if (!NT_STATUS_IS_OK(status)) {
764 errno = map_errno_from_nt_status(status);
768 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, mapped_name, mode);
769 TALLOC_FREE(mapped_name);
774 catia_sys_acl_get_file(vfs_handle_struct *handle,
779 char *mapped_name = NULL;
783 status = catia_string_replace_allocate(handle->conn,
784 path, &mapped_name, vfs_translate_to_unix);
785 if (!NT_STATUS_IS_OK(status)) {
786 errno = map_errno_from_nt_status(status);
790 ret = SMB_VFS_NEXT_SYS_ACL_GET_FILE(handle, mapped_name, type, mem_ctx);
791 TALLOC_FREE(mapped_name);
797 catia_sys_acl_set_file(vfs_handle_struct *handle,
802 char *mapped_name = NULL;
806 status = catia_string_replace_allocate(handle->conn,
807 path, &mapped_name, vfs_translate_to_unix);
808 if (!NT_STATUS_IS_OK(status)) {
809 errno = map_errno_from_nt_status(status);
813 ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle, mapped_name, type, theacl);
814 TALLOC_FREE(mapped_name);
820 catia_sys_acl_delete_def_file(vfs_handle_struct *handle,
823 char *mapped_name = NULL;
827 status = catia_string_replace_allocate(handle->conn,
828 path, &mapped_name, vfs_translate_to_unix);
829 if (!NT_STATUS_IS_OK(status)) {
830 errno = map_errno_from_nt_status(status);
834 ret = SMB_VFS_NEXT_SYS_ACL_DELETE_DEF_FILE(handle, mapped_name);
835 TALLOC_FREE(mapped_name);
841 catia_getxattr(vfs_handle_struct *handle, const char *path,
842 const char *name, void *value, size_t size)
844 char *mapped_name = NULL;
848 status = catia_string_replace_allocate(handle->conn,
849 name, &mapped_name, vfs_translate_to_unix);
850 if (!NT_STATUS_IS_OK(status)) {
851 errno = map_errno_from_nt_status(status);
856 ret = SMB_VFS_NEXT_GETXATTR(handle, path, mapped_name, value, size);
857 TALLOC_FREE(mapped_name);
863 catia_listxattr(vfs_handle_struct *handle, const char *path,
864 char *list, size_t size)
866 char *mapped_name = NULL;
870 status = catia_string_replace_allocate(handle->conn,
871 path, &mapped_name, vfs_translate_to_unix);
872 if (!NT_STATUS_IS_OK(status)) {
873 errno = map_errno_from_nt_status(status);
878 ret = SMB_VFS_NEXT_LISTXATTR(handle, mapped_name, list, size);
879 TALLOC_FREE(mapped_name);
885 catia_removexattr(vfs_handle_struct *handle, const char *path,
888 char *mapped_name = NULL;
892 status = catia_string_replace_allocate(handle->conn,
893 name, &mapped_name, vfs_translate_to_unix);
894 if (!NT_STATUS_IS_OK(status)) {
895 errno = map_errno_from_nt_status(status);
900 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, path, mapped_name);
901 TALLOC_FREE(mapped_name);
907 catia_setxattr(vfs_handle_struct *handle, const char *path,
908 const char *name, const void *value, size_t size,
911 char *mapped_name = NULL;
915 status = catia_string_replace_allocate(handle->conn,
916 name, &mapped_name, vfs_translate_to_unix);
917 if (!NT_STATUS_IS_OK(status)) {
918 errno = map_errno_from_nt_status(status);
923 ret = SMB_VFS_NEXT_SETXATTR(handle, path, mapped_name, value, size, flags);
924 TALLOC_FREE(mapped_name);
929 static struct vfs_fn_pointers vfs_catia_fns = {
930 .mkdir_fn = catia_mkdir,
931 .rmdir_fn = catia_rmdir,
932 .opendir_fn = catia_opendir,
933 .open_fn = catia_open,
934 .rename_fn = catia_rename,
935 .stat_fn = catia_stat,
936 .lstat_fn = catia_lstat,
937 .unlink_fn = catia_unlink,
938 .chown_fn = catia_chown,
939 .lchown_fn = catia_lchown,
940 .chmod_fn = catia_chmod,
941 .chdir_fn = catia_chdir,
942 .ntimes_fn = catia_ntimes,
943 .realpath_fn = catia_realpath,
944 .chflags_fn = catia_chflags,
945 .streaminfo_fn = catia_streaminfo,
946 .translate_name_fn = catia_translate_name,
947 .get_nt_acl_fn = catia_get_nt_acl,
948 .chmod_acl_fn = catia_chmod_acl,
949 .sys_acl_get_file_fn = catia_sys_acl_get_file,
950 .sys_acl_set_file_fn = catia_sys_acl_set_file,
951 .sys_acl_delete_def_file_fn = catia_sys_acl_delete_def_file,
952 .getxattr_fn = catia_getxattr,
953 .listxattr_fn = catia_listxattr,
954 .removexattr_fn = catia_removexattr,
955 .setxattr_fn = catia_setxattr,
958 NTSTATUS vfs_catia_init(void)
962 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "catia",
964 if (!NT_STATUS_IS_OK(ret))
967 vfs_catia_debug_level = debug_add_class("catia");
968 if (vfs_catia_debug_level == -1) {
969 vfs_catia_debug_level = DBGC_VFS;
970 DEBUG(0, ("vfs_catia: Couldn't register custom debugging "
973 DEBUG(10, ("vfs_catia: Debug class number of "
974 "'catia': %d\n", vfs_catia_debug_level));