This split the mangling code up to allow for the possibility of multiple
[kamenim/samba.git] / source3 / 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 struct {
27         char *name;
28         struct mangle_fns *(*init_fn)(void);
29 } mangle_backends[] = {
30         { "hash", mangle_hash_init },
31         { NULL, NULL }
32 };
33
34 /*
35   initialise the mangling subsystem
36 */
37 static void mangle_init(void)
38 {
39         int i;
40         char *method;
41
42         if (mangle_fns) return;
43
44         method = lp_mangling_method();
45
46         /* find the first mangling method that manages to initialise and 
47            matches the "mangling method" parameter */
48         for (i=0; mangle_backends[i].name && !mangle_fns; i++) {
49                 if (!method || !*method || strcmp(method, mangle_backends[i].name) == 0) {
50                         mangle_fns = mangle_backends[i].init_fn();
51                 }
52         }
53
54         if (!mangle_fns) {
55                 DEBUG(0,("Failed to initialise mangling system '%s'\n", method));
56                 exit_server("mangling init failed");
57         }
58 }
59
60
61 /*
62   reset the cache. This is called when smb.conf has been reloaded
63 */
64 void mangle_reset_cache(void)
65 {
66         mangle_init();
67
68         return mangle_fns->reset();
69 }
70
71 /*
72   see if a filename has come out of our mangling code
73 */
74 BOOL mangle_is_mangled(const char *s)
75 {
76         return mangle_fns->is_mangled(s);
77 }
78
79 /*
80   see if a filename matches the rules of a 8.3 filename
81 */
82 BOOL mangle_is_8_3(const char *fname, BOOL check_case)
83 {
84         return mangle_fns->is_8_3(fname, check_case);
85 }
86
87 /*
88   try to reverse map a 8.3 name to the original filename. This doesn't have to 
89   always succeed, as the directory handling code in smbd will scan the directory
90   looking for a matching name if it doesn't. It should succeed most of the time
91   or there will be a huge performance penalty
92 */
93 BOOL mangle_check_cache(char *s)
94 {
95         return mangle_fns->check_cache(s);
96 }
97
98 /* 
99    map a long filename to a 8.3 name. 
100  */
101 BOOL mangle_map(char *OutName, BOOL need83, BOOL cache83, int snum)
102 {
103         /* name mangling can be disabled for speed, in which case
104            we just truncate the string */
105         if (!lp_manglednames(snum)) {
106                 if (need83) {
107                         string_truncate(OutName, 12);
108                 }
109                 return True;
110         }
111
112         /* invoke the inane "mangled map" code */
113         mangle_map_filename(OutName, snum);
114
115         return mangle_fns->name_map(OutName, need83, cache83);
116 }