| 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 3 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, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 18 | */
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "includes.h"
 | 
|---|
| 21 | #include "smbd/globals.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /* this allows us to add more mangling backends */
 | 
|---|
| 24 | static const struct {
 | 
|---|
| 25 |         const char *name;
 | 
|---|
| 26 |         const struct mangle_fns *(*init_fn)(void);
 | 
|---|
| 27 | } mangle_backends[] = {
 | 
|---|
| 28 |         { "hash", mangle_hash_init },
 | 
|---|
| 29 |         { "hash2", mangle_hash2_init },
 | 
|---|
| 30 |         { "posix", posix_mangle_init },
 | 
|---|
| 31 |         /*{ "tdb", mangle_tdb_init }, */
 | 
|---|
| 32 |         { NULL, NULL }
 | 
|---|
| 33 | };
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /*
 | 
|---|
| 36 |   initialise the mangling subsystem
 | 
|---|
| 37 | */
 | 
|---|
| 38 | static void mangle_init(void)
 | 
|---|
| 39 | {
 | 
|---|
| 40 |         int i;
 | 
|---|
| 41 |         const char *method;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |         if (mangle_fns)
 | 
|---|
| 44 |                 return;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |         method = lp_mangling_method();
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         /* find the first mangling method that manages to initialise and
 | 
|---|
| 49 |            matches the "mangling method" parameter */
 | 
|---|
| 50 |         for (i=0; mangle_backends[i].name && !mangle_fns; i++) {
 | 
|---|
| 51 |                 if (!method || !*method || strcmp(method, mangle_backends[i].name) == 0) {
 | 
|---|
| 52 |                         mangle_fns = mangle_backends[i].init_fn();
 | 
|---|
| 53 |                 }
 | 
|---|
| 54 |         }
 | 
|---|
| 55 | 
 | 
|---|
| 56 |         if (!mangle_fns) {
 | 
|---|
| 57 |                 DEBUG(0,("Failed to initialise mangling system '%s'\n", method));
 | 
|---|
| 58 |                 exit_server("mangling init failed");
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | 
 | 
|---|
| 63 | /*
 | 
|---|
| 64 |   reset the cache. This is called when smb.conf has been reloaded
 | 
|---|
| 65 | */
 | 
|---|
| 66 | void mangle_reset_cache(void)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |         mangle_init();
 | 
|---|
| 69 |         mangle_fns->reset();
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | void mangle_change_to_posix(void)
 | 
|---|
| 73 | {
 | 
|---|
| 74 |         mangle_fns = NULL;
 | 
|---|
| 75 |         lp_set_mangling_method("posix");
 | 
|---|
| 76 |         mangle_reset_cache();
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | /*
 | 
|---|
| 80 |   see if a filename has come out of our mangling code
 | 
|---|
| 81 | */
 | 
|---|
| 82 | bool mangle_is_mangled(const char *s, const struct share_params *p)
 | 
|---|
| 83 | {
 | 
|---|
| 84 |         return mangle_fns->is_mangled(s, p);
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | /*
 | 
|---|
| 88 |   see if a filename matches the rules of a 8.3 filename
 | 
|---|
| 89 | */
 | 
|---|
| 90 | bool mangle_is_8_3(const char *fname, bool check_case,
 | 
|---|
| 91 |                    const struct share_params *p)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |         return mangle_fns->is_8_3(fname, check_case, False, p);
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | bool mangle_is_8_3_wildcards(const char *fname, bool check_case,
 | 
|---|
| 97 |                              const struct share_params *p)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |         return mangle_fns->is_8_3(fname, check_case, True, p);
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | bool mangle_must_mangle(const char *fname,
 | 
|---|
| 103 |                    const struct share_params *p)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |         if (!lp_manglednames(p)) {
 | 
|---|
| 106 |                 return False;
 | 
|---|
| 107 |         }
 | 
|---|
| 108 |         return mangle_fns->must_mangle(fname, p);
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | /*
 | 
|---|
| 112 |   try to reverse map a 8.3 name to the original filename. This doesn't have to
 | 
|---|
| 113 |   always succeed, as the directory handling code in smbd will scan the directory
 | 
|---|
| 114 |   looking for a matching name if it doesn't. It should succeed most of the time
 | 
|---|
| 115 |   or there will be a huge performance penalty
 | 
|---|
| 116 | */
 | 
|---|
| 117 | bool mangle_lookup_name_from_8_3(TALLOC_CTX *ctx,
 | 
|---|
| 118 |                         const char *in,
 | 
|---|
| 119 |                         char **out, /* talloced on the given context. */
 | 
|---|
| 120 |                         const struct share_params *p)
 | 
|---|
| 121 | {
 | 
|---|
| 122 |         return mangle_fns->lookup_name_from_8_3(ctx, in, out, p);
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | /*
 | 
|---|
| 126 |    mangle a long filename to a 8.3 name.
 | 
|---|
| 127 |    Return True if we did mangle the name (ie. out is filled in).
 | 
|---|
| 128 |    False on error.
 | 
|---|
| 129 |    JRA.
 | 
|---|
| 130 |  */
 | 
|---|
| 131 | 
 | 
|---|
| 132 | bool name_to_8_3(const char *in,
 | 
|---|
| 133 |                 char out[13],
 | 
|---|
| 134 |                 bool cache83,
 | 
|---|
| 135 |                 const struct share_params *p)
 | 
|---|
| 136 | {
 | 
|---|
| 137 |         memset(out,'\0',13);
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         /* name mangling can be disabled for speed, in which case
 | 
|---|
| 140 |            we just truncate the string */
 | 
|---|
| 141 |         if (!lp_manglednames(p)) {
 | 
|---|
| 142 |                 strlcpy(out, in, 13);
 | 
|---|
| 143 |                 return True;
 | 
|---|
| 144 |         }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         return mangle_fns->name_to_8_3(in,
 | 
|---|
| 147 |                                 out,
 | 
|---|
| 148 |                                 cache83,
 | 
|---|
| 149 |                                 lp_defaultcase(p->service),
 | 
|---|
| 150 |                                 p);
 | 
|---|
| 151 | }
 | 
|---|