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
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.
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.
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/>.
26 #include "lib/util/samba_util.h"
27 #include "lib/util_path.h"
29 struct loadparm_substitution
;
31 #include "source3/param/param_proto.h"
34 * @brief Returns an absolute path to a file concatenating the provided
35 * @a rootpath and @a basename
37 * @param name Filename, relative to @a rootpath
39 * @retval Pointer to a string containing the full path.
42 static char *xx_path(TALLOC_CTX
*mem_ctx
,
48 fname
= talloc_strdup(mem_ctx
, rootpath
);
52 trim_string(fname
,"","/");
54 if (!directory_create_or_exist(fname
, 0755)) {
58 return talloc_asprintf_append(fname
, "/%s", name
);
62 * @brief Returns an absolute path to a file in the Samba lock directory.
64 * @param name File to find, relative to LOCKDIR.
66 * @retval Pointer to a talloc'ed string containing the full path.
69 char *lock_path(TALLOC_CTX
*mem_ctx
, const char *name
)
71 return xx_path(mem_ctx
, name
, lp_lock_directory());
75 * @brief Returns an absolute path to a file in the Samba state directory.
77 * @param name File to find, relative to STATEDIR.
79 * @retval Pointer to a talloc'ed string containing the full path.
82 char *state_path(TALLOC_CTX
*mem_ctx
, const char *name
)
84 return xx_path(mem_ctx
, name
, lp_state_directory());
88 * @brief Returns an absolute path to a file in the Samba cache directory.
90 * @param name File to find, relative to CACHEDIR.
92 * @retval Pointer to a talloc'ed string containing the full path.
95 char *cache_path(TALLOC_CTX
*mem_ctx
, const char *name
)
97 return xx_path(mem_ctx
, name
, lp_cache_directory());
101 * @brief Removes any invalid path components in an absolute POSIX path.
103 * @param ctx Talloc context to return string.
105 * @param abs_path Absolute path string to process.
107 * @retval Pointer to a talloc'ed string containing the absolute full path.
110 char *canonicalize_absolute_path(TALLOC_CTX
*ctx
, const char *pathname_in
)
113 * Note we use +2 here so if pathname_in=="" then we
114 * have space to return "/".
116 char *pathname
= talloc_array(ctx
, char, strlen(pathname_in
)+2);
117 const char *s
= pathname_in
;
119 bool wrote_slash
= false;
121 if (pathname
== NULL
) {
125 /* Always start with a '/'. */
130 /* Deal with '/' or multiples of '/'. */
132 while (s
[0] == '/') {
133 /* Eat trailing '/' */
136 /* Update target with one '/' */
144 /* Deal with "./" or ".\0" */
146 (s
[1] == '/' || s
[1] == '\0')) {
149 while (s
[0] == '/') {
150 /* Eat any trailing '/' */
153 /* Don't write anything to target. */
154 /* wrote_slash is still true. */
157 /* Deal with "../" or "..\0" */
158 if (s
[0] == '.' && s
[1] == '.' &&
159 (s
[2] == '/' || s
[2] == '\0')) {
160 /* Eat the dot dot. */
162 while (s
[0] == '/') {
163 /* Eat any trailing '/' */
167 * As wrote_slash is true, we go back
168 * one character to point p at the slash
175 * Now go back to the slash
176 * before the one that p currently points to.
178 while (p
> pathname
) {
185 * Step forward one to leave the
186 * last written '/' alone.
190 /* Don't write anything to target. */
191 /* wrote_slash is still true. */
195 /* Non-separator character, just copy. */
201 * We finished on a '/'.
202 * Remove the trailing '/', but not if it's
203 * the sole character in the path.
205 if (p
> pathname
+ 1) {
209 /* Terminate and we're done ! */