s3:smbd: rename has_ctdb_public_ip to has_cluster_movable_ip
[Samba.git] / source3 / lib / util_path.c
blobc34b734384cfc75fd7244fc474d5ae3622b43cbb
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
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/>.
24 #include "replace.h"
25 #include <talloc.h>
26 #include "lib/util/samba_util.h"
27 #include "lib/util_path.h"
29 struct loadparm_substitution;
30 struct share_params;
31 #include "source3/param/param_proto.h"
33 /**
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.
40 **/
42 static char *xx_path(TALLOC_CTX *mem_ctx,
43 const char *name,
44 const char *rootpath)
46 char *fname = NULL;
48 fname = talloc_strdup(mem_ctx, rootpath);
49 if (!fname) {
50 return NULL;
52 trim_string(fname,"","/");
54 if (!directory_create_or_exist(fname, 0755)) {
55 return NULL;
58 return talloc_asprintf_append(fname, "/%s", name);
61 /**
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.
67 **/
69 char *lock_path(TALLOC_CTX *mem_ctx, const char *name)
71 return xx_path(mem_ctx, name, lp_lock_directory());
74 /**
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.
80 **/
82 char *state_path(TALLOC_CTX *mem_ctx, const char *name)
84 return xx_path(mem_ctx, name, lp_state_directory());
87 /**
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.
93 **/
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;
118 char *p = pathname;
119 bool wrote_slash = false;
121 if (pathname == NULL) {
122 return NULL;
125 /* Always start with a '/'. */
126 *p++ = '/';
127 wrote_slash = true;
129 while (*s) {
130 /* Deal with '/' or multiples of '/'. */
131 if (s[0] == '/') {
132 while (s[0] == '/') {
133 /* Eat trailing '/' */
134 s++;
136 /* Update target with one '/' */
137 if (!wrote_slash) {
138 *p++ = '/';
139 wrote_slash = true;
141 continue;
143 if (wrote_slash) {
144 /* Deal with "./" or ".\0" */
145 if (s[0] == '.' &&
146 (s[1] == '/' || s[1] == '\0')) {
147 /* Eat the dot. */
148 s++;
149 while (s[0] == '/') {
150 /* Eat any trailing '/' */
151 s++;
153 /* Don't write anything to target. */
154 /* wrote_slash is still true. */
155 continue;
157 /* Deal with "../" or "..\0" */
158 if (s[0] == '.' && s[1] == '.' &&
159 (s[2] == '/' || s[2] == '\0')) {
160 /* Eat the dot dot. */
161 s += 2;
162 while (s[0] == '/') {
163 /* Eat any trailing '/' */
164 s++;
167 * As wrote_slash is true, we go back
168 * one character to point p at the slash
169 * we just saw.
171 if (p > pathname) {
172 p--;
175 * Now go back to the slash
176 * before the one that p currently points to.
178 while (p > pathname) {
179 p--;
180 if (p[0] == '/') {
181 break;
185 * Step forward one to leave the
186 * last written '/' alone.
188 p++;
190 /* Don't write anything to target. */
191 /* wrote_slash is still true. */
192 continue;
195 /* Non-separator character, just copy. */
196 *p++ = *s++;
197 wrote_slash = false;
199 if (wrote_slash) {
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) {
206 p--;
209 /* Terminate and we're done ! */
210 *p++ = '\0';
211 return pathname;