version incremented
[samba] / source / smbd / mangle.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Name mangling interface
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static struct mangle_fns *mangle_fns;
24
25 /* this allows us to add more mangling backends */
26 static const struct {
27         const char *name;
28         struct mangle_fns *(*init_fn)(void);
29 } mangle_backends[] = {
30         { "hash", mangle_hash_init },
31         { "hash2", mangle_hash2_init },
32         { "posix", posix_mangle_init },
33         /*{ "tdb", mangle_tdb_init }, */
34         { NULL, NULL }
35 };
36
37 /*
38   initialise the mangling subsystem
39 */
40 static void mangle_init(void)
41 {
42         int i;
43         const char *method;
44
45         if (mangle_fns)
46                 return;
47
48         method = lp_mangling_method();
49
50         /* find the first mangling method that manages to initialise and
51            matches the "mangling method" parameter */
52         for (i=0; mangle_backends[i].name && !mangle_fns; i++) {
53                 if (!method || !*method || strcmp(method, mangle_backends[i].name) == 0) {
54                         mangle_fns = mangle_backends[i].init_fn();
55                 }
56         }
57
58         if (!mangle_fns) {
59                 DEBUG(0,("Failed to initialise mangling system '%s'\n", method));
60                 exit_server("mangling init failed");
61         }
62 }
63
64
65 /*
66   reset the cache. This is called when smb.conf has been reloaded
67 */
68 void mangle_reset_cache(void)
69 {
70         mangle_init();
71         mangle_fns->reset();
72 }
73
74 void mangle_change_to_posix(void)
75 {
76         mangle_fns = NULL;
77         lp_set_mangling_method("posix");
78         mangle_reset_cache();
79 }
80
81 /*
82   see if a filename has come out of our mangling code
83 */
84 BOOL mangle_is_mangled(const char *s, int snum)
85 {
86         return mangle_fns->is_mangled(s, snum);
87 }
88
89 /*
90   see if a filename matches the rules of a 8.3 filename
91 */
92 BOOL mangle_is_8_3(const char *fname, BOOL check_case, int snum)
93 {
94         return mangle_fns->is_8_3(fname, check_case, False, snum);
95 }
96
97 BOOL mangle_is_8_3_wildcards(const char *fname, BOOL check_case, int snum)
98 {
99         return mangle_fns->is_8_3(fname, check_case, True, snum);
100 }
101
102 /*
103   try to reverse map a 8.3 name to the original filename. This doesn't have to 
104   always succeed, as the directory handling code in smbd will scan the directory
105   looking for a matching name if it doesn't. It should succeed most of the time
106   or there will be a huge performance penalty
107 */
108 BOOL mangle_check_cache(char *s, size_t maxlen, int snum)
109 {
110         return mangle_fns->check_cache(s, maxlen, snum);
111 }
112
113 /* 
114    map a long filename to a 8.3 name. 
115  */
116
117 void mangle_map(pstring OutName, BOOL need83, BOOL cache83, int snum)
118 {
119         /* name mangling can be disabled for speed, in which case
120            we just truncate the string */
121         if (!lp_manglednames(snum)) {
122                 if (need83) {
123                         string_truncate(OutName, 12);
124                 }
125                 return;
126         }
127
128         /* invoke the inane "mangled map" code */
129         mangle_map_filename(OutName, snum);
130         mangle_fns->name_map(OutName, need83, cache83, lp_defaultcase(snum), snum);
131 }