LCOV - code coverage report
Current view: top level - source3/smbd - mangle.c (source / functions) Hit Total Coverage
Test: coverage report for master 6248eab5 Lines: 32 37 86.5 %
Date: 2021-08-25 13:27:56 Functions: 9 9 100.0 %

          Line data    Source code
       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/smbd.h"
      22             : #include "smbd/globals.h"
      23             : #include "mangle.h"
      24             : 
      25             : /* this allows us to add more mangling backends */
      26             : static const struct {
      27             :         const char *name;
      28             :         const 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        1300 : static void mangle_init(void)
      41             : {
      42             :         int i;
      43             :         const char *method;
      44             : 
      45        1300 :         if (mangle_fns)
      46        1096 :                 return;
      47             : 
      48         204 :         method = lp_mangling_method();
      49             : 
      50             :         /* find the first mangling method that manages to initialise and
      51             :            matches the "mangling method" parameter */
      52         724 :         for (i=0; mangle_backends[i].name && !mangle_fns; i++) {
      53         520 :                 if (!method || !*method || strcmp(method, mangle_backends[i].name) == 0) {
      54         204 :                         mangle_fns = mangle_backends[i].init_fn();
      55             :                 }
      56             :         }
      57             : 
      58         204 :         if (!mangle_fns) {
      59           0 :                 DEBUG(0,("Failed to initialise mangling system '%s'\n", method));
      60           0 :                 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        1300 : void mangle_reset_cache(void)
      69             : {
      70        1300 :         mangle_init();
      71        1300 :         mangle_fns->reset();
      72        1300 : }
      73             : 
      74         112 : void mangle_change_to_posix(void)
      75             : {
      76         112 :         mangle_fns = NULL;
      77         112 :         lp_set_mangling_method("posix");
      78         112 :         mangle_reset_cache();
      79         112 : }
      80             : 
      81             : /*
      82             :   see if a filename has come out of our mangling code
      83             : */
      84      867899 : bool mangle_is_mangled(const char *s, const struct share_params *p)
      85             : {
      86      867899 :         return mangle_fns->is_mangled(s, p);
      87             : }
      88             : 
      89             : /*
      90             :   see if a filename matches the rules of a 8.3 filename
      91             : */
      92      495489 : bool mangle_is_8_3(const char *fname, bool check_case,
      93             :                    const struct share_params *p)
      94             : {
      95      495489 :         return mangle_fns->is_8_3(fname, check_case, False, p);
      96             : }
      97             : 
      98        6950 : bool mangle_is_8_3_wildcards(const char *fname, bool check_case,
      99             :                              const struct share_params *p)
     100             : {
     101        6950 :         return mangle_fns->is_8_3(fname, check_case, True, p);
     102             : }
     103             : 
     104      510134 : bool mangle_must_mangle(const char *fname,
     105             :                    const struct share_params *p)
     106             : {
     107      510134 :         if (lp_mangled_names(p) == MANGLED_NAMES_NO) {
     108           0 :                 return False;
     109             :         }
     110      510134 :         return mangle_fns->must_mangle(fname, p);
     111             : }
     112             : 
     113             : /*
     114             :   try to reverse map a 8.3 name to the original filename. This doesn't have to
     115             :   always succeed, as the directory handling code in smbd will scan the directory
     116             :   looking for a matching name if it doesn't. It should succeed most of the time
     117             :   or there will be a huge performance penalty
     118             : */
     119         196 : bool mangle_lookup_name_from_8_3(TALLOC_CTX *ctx,
     120             :                         const char *in,
     121             :                         char **out, /* talloced on the given context. */
     122             :                         const struct share_params *p)
     123             : {
     124         196 :         return mangle_fns->lookup_name_from_8_3(ctx, in, out, p);
     125             : }
     126             : 
     127             : /*
     128             :    mangle a long filename to a 8.3 name.
     129             :    Return True if we did mangle the name (ie. out is filled in).
     130             :    False on error.
     131             :    JRA.
     132             :  */
     133             : 
     134       42623 : bool name_to_8_3(const char *in,
     135             :                 char out[13],
     136             :                 bool cache83,
     137             :                 const struct share_params *p)
     138             : {
     139       42623 :         memset(out,'\0',13);
     140             : 
     141             :         /* name mangling can be disabled for speed, in which case
     142             :            we just truncate the string */
     143       42623 :         if (lp_mangled_names(p) == MANGLED_NAMES_NO) {
     144           0 :                 strlcpy(out, in, 13);
     145           0 :                 return True;
     146             :         }
     147             : 
     148       42623 :         return mangle_fns->name_to_8_3(in,
     149             :                                 out,
     150             :                                 cache83,
     151             :                                 lp_default_case(p->service),
     152             :                                 p);
     153             : }

Generated by: LCOV version 1.13