LCOV - code coverage report
Current view: top level - source3/lib - util_path.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 56 59 94.9 %
Date: 2021-09-23 10:06:22 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  * Samba utility functions
       4             :  * Copyright (C) Andrew Tridgell 1992-1998
       5             :  * Copyright (C) Jeremy Allison 2001-2007
       6             :  * Copyright (C) Simo Sorce 2001
       7             :  * Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
       8             :  * Copyright (C) James Peach 2006
       9             :  *
      10             :  * This program is free software; you can redistribute it and/or modify
      11             :  * it under the terms of the GNU General Public License as published by
      12             :  * the Free Software Foundation; either version 3 of the License, or
      13             :  * (at your option) any later version.
      14             :  *
      15             :  * This program is distributed in the hope that it will be useful,
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :  * GNU General Public License for more details.
      19             :  *
      20             :  * You should have received a copy of the GNU General Public License
      21             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             :  */
      23             : 
      24             : #include "replace.h"
      25             : #include <talloc.h>
      26             : #include "lib/util/samba_util.h"
      27             : #include "lib/util_path.h"
      28             : 
      29             : struct loadparm_substitution;
      30             : struct share_params;
      31             : #include "source3/param/param_proto.h"
      32             : 
      33             : /**
      34             :  * @brief Returns an absolute path to a file concatenating the provided
      35             :  * @a rootpath and @a basename
      36             :  *
      37             :  * @param name Filename, relative to @a rootpath
      38             :  *
      39             :  * @retval Pointer to a string containing the full path.
      40             :  **/
      41             : 
      42      133312 : static char *xx_path(TALLOC_CTX *mem_ctx,
      43             :                      const char *name,
      44             :                      const char *rootpath)
      45             : {
      46      133312 :         char *fname = NULL;
      47             : 
      48      133312 :         fname = talloc_strdup(mem_ctx, rootpath);
      49      133312 :         if (!fname) {
      50           0 :                 return NULL;
      51             :         }
      52      133312 :         trim_string(fname,"","/");
      53             : 
      54      133312 :         if (!directory_create_or_exist(fname, 0755)) {
      55           0 :                 return NULL;
      56             :         }
      57             : 
      58      133312 :         return talloc_asprintf_append(fname, "/%s", name);
      59             : }
      60             : 
      61             : /**
      62             :  * @brief Returns an absolute path to a file in the Samba lock directory.
      63             :  *
      64             :  * @param name File to find, relative to LOCKDIR.
      65             :  *
      66             :  * @retval Pointer to a talloc'ed string containing the full path.
      67             :  **/
      68             : 
      69       41031 : char *lock_path(TALLOC_CTX *mem_ctx, const char *name)
      70             : {
      71       41031 :         return xx_path(mem_ctx, name, lp_lock_directory());
      72             : }
      73             : 
      74             : /**
      75             :  * @brief Returns an absolute path to a file in the Samba state directory.
      76             :  *
      77             :  * @param name File to find, relative to STATEDIR.
      78             :  *
      79             :  * @retval Pointer to a talloc'ed string containing the full path.
      80             :  **/
      81             : 
      82       90761 : char *state_path(TALLOC_CTX *mem_ctx, const char *name)
      83             : {
      84       90761 :         return xx_path(mem_ctx, name, lp_state_directory());
      85             : }
      86             : 
      87             : /**
      88             :  * @brief Returns an absolute path to a file in the Samba cache directory.
      89             :  *
      90             :  * @param name File to find, relative to CACHEDIR.
      91             :  *
      92             :  * @retval Pointer to a talloc'ed string containing the full path.
      93             :  **/
      94             : 
      95        1520 : char *cache_path(TALLOC_CTX *mem_ctx, const char *name)
      96             : {
      97        1520 :         return xx_path(mem_ctx, name, lp_cache_directory());
      98             : }
      99             : 
     100             : /**
     101             :  * @brief Removes any invalid path components in an absolute POSIX path.
     102             :  *
     103             :  * @param ctx Talloc context to return string.
     104             :  *
     105             :  * @param abs_path Absolute path string to process.
     106             :  *
     107             :  * @retval Pointer to a talloc'ed string containing the absolute full path.
     108             :  **/
     109             : 
     110      408185 : char *canonicalize_absolute_path(TALLOC_CTX *ctx, const char *pathname_in)
     111             : {
     112             :         /*
     113             :          * Note we use +2 here so if pathname_in=="" then we
     114             :          * have space to return "/".
     115             :          */
     116      408185 :         char *pathname = talloc_array(ctx, char, strlen(pathname_in)+2);
     117      408185 :         const char *s = pathname_in;
     118      408185 :         char *p = pathname;
     119      408185 :         bool wrote_slash = false;
     120             : 
     121      408185 :         if (pathname == NULL) {
     122           0 :                 return NULL;
     123             :         }
     124             : 
     125             :         /* Always start with a '/'. */
     126      408185 :         *p++ = '/';
     127      408185 :         wrote_slash = true;
     128             : 
     129    33665102 :         while (*s) {
     130             :                 /* Deal with '/' or multiples of '/'. */
     131    32861970 :                 if (s[0] == '/') {
     132    12470904 :                         while (s[0] == '/') {
     133             :                                 /* Eat trailing '/' */
     134     4186189 :                                 s++;
     135             :                         }
     136             :                         /* Update target with one '/' */
     137     4183858 :                         if (!wrote_slash) {
     138     3775676 :                                 *p++ = '/';
     139     3775676 :                                 wrote_slash = true;
     140             :                         }
     141     4183858 :                         continue;
     142             :                 }
     143    28678112 :                 if (wrote_slash) {
     144             :                         /* Deal with "./" or ".\0" */
     145     4318928 :                         if (s[0] == '.' &&
     146      297380 :                                         (s[1] == '/' || s[1] == '\0')) {
     147             :                                 /* Eat the dot. */
     148       81787 :                                 s++;
     149      163613 :                                 while (s[0] == '/') {
     150             :                                         /* Eat any trailing '/' */
     151          39 :                                         s++;
     152             :                                 }
     153             :                                 /* Don't write anything to target. */
     154             :                                 /* wrote_slash is still true. */
     155       81787 :                                 continue;
     156             :                         }
     157             :                         /* Deal with "../" or "..\0" */
     158     4088845 :                         if (s[0] == '.' && s[1] == '.' &&
     159         830 :                                         (s[2] == '/' || s[2] == '\0')) {
     160             :                                 /* Eat the dot dot. */
     161         425 :                                 s += 2;
     162         870 :                                 while (s[0] == '/') {
     163             :                                         /* Eat any trailing '/' */
     164          20 :                                         s++;
     165             :                                 }
     166             :                                 /*
     167             :                                  * As wrote_slash is true, we go back
     168             :                                  * one character to point p at the slash
     169             :                                  * we just saw.
     170             :                                  */
     171         425 :                                 if (p > pathname) {
     172         425 :                                         p--;
     173             :                                 }
     174             :                                 /*
     175             :                                  * Now go back to the slash
     176             :                                  * before the one that p currently points to.
     177             :                                  */
     178        2082 :                                 while (p > pathname) {
     179        1666 :                                         p--;
     180        1666 :                                         if (p[0] == '/') {
     181         404 :                                                 break;
     182             :                                         }
     183             :                                 }
     184             :                                 /*
     185             :                                  * Step forward one to leave the
     186             :                                  * last written '/' alone.
     187             :                                  */
     188         425 :                                 p++;
     189             : 
     190             :                                 /* Don't write anything to target. */
     191             :                                 /* wrote_slash is still true. */
     192         425 :                                 continue;
     193             :                         }
     194             :                 }
     195             :                 /* Non-separator character, just copy. */
     196    28595900 :                 *p++ = *s++;
     197    28595900 :                 wrote_slash = false;
     198             :         }
     199      408185 :         if (wrote_slash) {
     200             :                 /*
     201             :                  * We finished on a '/'.
     202             :                  * Remove the trailing '/', but not if it's
     203             :                  * the sole character in the path.
     204             :                  */
     205       95867 :                 if (p > pathname + 1) {
     206       81590 :                         p--;
     207             :                 }
     208             :         }
     209             :         /* Terminate and we're done ! */
     210      408185 :         *p++ = '\0';
     211      408185 :         return pathname;
     212             : }

Generated by: LCOV version 1.13