summaryrefslogtreecommitdiffstats
path: root/src/lib/path-util.c
blob: 90e1e46e92ff0f47d5e5334d01ebfb8dee94ef5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "path-util.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#define PATH_UTIL_MAX_PATH      8*1024
#define PATH_UTIL_MAX_SYMLINKS  80

static int t_getcwd_noalloc(char **dir_r, size_t *asize_r,
			    const char **error_r) ATTR_NULL(2)
{
	/* @UNSAFE */
	char *dir;
	size_t asize = 128;

	dir = t_buffer_get(asize);
	while (getcwd(dir, asize) == NULL) {
		if (errno != ERANGE) {
			*error_r = t_strdup_printf("getcwd() failed: %m");
			return -1;
		}
		asize = nearest_power(asize+1);
		dir = t_buffer_get(asize);
	}
	if (asize_r != NULL)
		*asize_r = asize;
	*dir_r = dir;
	return 0;
}

static int path_normalize(const char *path, bool resolve_links,
			  const char **npath_r, const char **error_r)
{
	/* @UNSAFE */
	unsigned int link_count = 0;
	char *npath, *npath_pos;
	const char *p;
	size_t asize;

	i_assert(path != NULL);
	i_assert(npath_r != NULL);
	i_assert(error_r != NULL);

	if (path[0] != '/') {
		/* relative; initialize npath with current directory */
		if (t_getcwd_noalloc(&npath, &asize, error_r) < 0)
			return -1;
		npath_pos = npath + strlen(npath);
		i_assert(npath[0] == '/');
	} else {
		/* absolute; initialize npath with root */
		asize = 128;
		npath = t_buffer_get(asize);
		npath[0] = '/';
		npath_pos = npath + 1;
	}

	p = path;
	while (*p != '\0') {
		struct stat st;
		ptrdiff_t seglen;
		const char *segend;

		/* skip duplicate slashes */
		while (*p == '/')
			p++;

		/* find end of path segment */
		for (segend = p; *segend != '\0' && *segend != '/'; segend++);

		if (segend == p)
			break; /* '\0' */
		seglen = segend - p;
		if (seglen == 1 && p[0] == '.') {
			/* a reference to this segment; nothing to do */
		} else if (seglen == 2 && p[0] == '.' && p[1] == '.') {
			/* a reference to parent segment; back up to previous
			 * slash */
			i_assert(npath_pos >= npath);
			if ((npath_pos - npath) > 1) {
				if (*(npath_pos-1) == '/')
					npath_pos--;
				for (; *(npath_pos-1) != '/'; npath_pos--);
			}
		} else {
			/* allocate space if necessary */
			i_assert(npath_pos >= npath);
			if ((size_t)((npath_pos - npath) + seglen + 1) >= asize) {
				ptrdiff_t npath_offset = npath_pos - npath;
				asize = nearest_power(npath_offset + seglen + 2);
				npath = t_buffer_reget(npath, asize);
				npath_pos = npath + npath_offset;
			}

			/* make sure npath now ends in slash */
			i_assert(npath_pos > npath);
			if (*(npath_pos-1) != '/') {
				i_assert((size_t)((npath_pos - npath) + 1) < asize);
				*(npath_pos++) = '/';
			}

			/* copy segment to normalized path */
			i_assert(npath_pos >= npath);
			i_assert((size_t)((npath_pos - npath) + seglen) < asize);
			memmove(npath_pos, p, seglen);
			npath_pos += seglen;
		}

		if (resolve_links) {
			/* stat path up to here (segend points to tail) */
			*npath_pos = '\0';
			if (lstat(npath, &st) < 0) {
				*error_r = t_strdup_printf("lstat() failed: %m");
				return -1;
			}

			if (S_ISLNK (st.st_mode)) {
				/* symlink */
				char *npath_link;
				size_t lsize = 128, tlen = strlen(segend), espace;
				size_t ltlen = (link_count == 0 ? 0 : tlen);
				ssize_t ret;

				/* limit link dereferences */
				if (++link_count > PATH_UTIL_MAX_SYMLINKS) {
					errno = ELOOP;
					*error_r = "Too many symlink dereferences";
					return -1;
				}

				/* allocate space for preserving tail of previous symlink and
				   first attempt at reading symlink with room for the tail

				   buffer will look like this:
				   [npath][0][preserved tail][link buffer][room for tail][0]
				 */
				espace = ltlen + tlen + 2;
				i_assert(npath_pos >= npath);
				if ((size_t)((npath_pos - npath) + espace + lsize) >= asize) {
					ptrdiff_t npath_offset = npath_pos - npath;
					asize = nearest_power((npath_offset + espace + lsize) + 1);
					lsize = asize - (npath_offset + espace);
					npath = t_buffer_reget(npath, asize);
					npath_pos = npath + npath_offset;
				}

				if (ltlen > 0) {
					/* preserve tail just after end of npath */
					i_assert(npath_pos >= npath);
					i_assert((size_t)((npath_pos + 1 - npath) + ltlen) < asize);
					memmove(npath_pos + 1, segend, ltlen);
				}

				/* read the symlink after the preserved tail */
				for (;;) {
					npath_link = (npath_pos + 1) + ltlen;

					i_assert(npath_link >= npath_pos);
					i_assert((size_t)((npath_link - npath) + lsize) < asize);

					/* attempt to read the link */
					if ((ret=readlink(npath, npath_link, lsize)) < 0) {
						*error_r = t_strdup_printf("readlink() failed: %m");
						return -1;
					}
					if ((size_t)ret < lsize) {
						/* POSIX doesn't guarantee the presence of a NIL */
						npath_link[ret] = '\0';
						break;
					}

					/* sum of new symlink content length
					 * and path tail length may not
					   exceed maximum */
					if ((size_t)(ret + tlen) >= PATH_UTIL_MAX_PATH) {
						errno = ENAMETOOLONG;
						*error_r = "Resulting path is too long";
						return -1;
					}

					/* try again with bigger buffer,
					   we need to allocate more space as well if lsize == ret,
					   because the returned link may have gotten truncated */
					espace = ltlen + tlen + 2;
					i_assert(npath_pos >= npath);
					if ((size_t)((npath_pos - npath) + espace + lsize) >= asize ||
					    lsize == (size_t)ret) {
						ptrdiff_t npath_offset = npath_pos - npath;
						asize = nearest_power((npath_offset + espace + lsize) + 1);
						lsize = asize - (npath_offset + espace);
						npath = t_buffer_reget(npath, asize);
						npath_pos = npath + npath_offset;
					}
				}

				/* add tail of previous path at end of symlink */
				i_assert(npath_link >= npath);
				if (ltlen > 0) {
					i_assert(npath_pos >= npath);
					i_assert((size_t)((npath_pos - npath) + 1 + tlen) < asize);
					i_assert((size_t)((npath_link - npath) + ret + tlen) < asize);
					memcpy(npath_link + ret, npath_pos + 1, tlen);
				} else {
					i_assert((size_t)((npath_link - npath) + ret + tlen) < asize);
					memcpy(npath_link + ret, segend, tlen);
				}
				*(npath_link+ret+tlen) = '\0';

				/* use as new source path */
				path = segend = npath_link;

				if (path[0] == '/') {
					/* absolute symlink; start over at root */
					npath_pos = npath + 1;
				} else {
					/* relative symlink; back up to previous segment */
					i_assert(npath_pos >= npath);
					if ((npath_pos - npath) > 1) {
						if (*(npath_pos-1) == '/')
							npath_pos--;
						for (; *(npath_pos-1) != '/'; npath_pos--);
					}
				}

			} else if (*segend != '\0' && !S_ISDIR (st.st_mode)) {
				/* not last segment, but not a directory either */
				errno = ENOTDIR;
				*error_r = t_strdup_printf("Not a directory: %s", npath);
				return -1;
			}
		}

		p = segend;
	}

	i_assert(npath_pos >= npath);
	i_assert((size_t)(npath_pos - npath) < asize);

	/* remove any trailing slash */
	if ((npath_pos - npath) > 1 && *(npath_pos-1) == '/')
		npath_pos--;
	*npath_pos = '\0';

	t_buffer_alloc(npath_pos - npath + 1);
	*npath_r = npath;
	return 0;
}

int t_normpath(const char *path, const char **npath_r, const char **error_r)
{
	return path_normalize(path, FALSE, npath_r, error_r);
}

int t_normpath_to(const char *path, const char *root, const char **npath_r,
		  const char **error_r)
{
	i_assert(path != NULL);
	i_assert(root != NULL);
	i_assert(npath_r != NULL);

	if (*path == '/')
		return t_normpath(path, npath_r, error_r);

	return t_normpath(t_strconcat(root, "/", path, NULL), npath_r, error_r);
}

int t_realpath(const char *path, const char **npath_r, const char **error_r)
{
	return path_normalize(path, TRUE, npath_r, error_r);
}

int t_realpath_to(const char *path, const char *root, const char **npath_r,
		  const char **error_r)
{
	i_assert(path != NULL);
	i_assert(root != NULL);
	i_assert(npath_r != NULL);

	if (*path == '/')
		return t_realpath(path, npath_r, error_r);

	return t_realpath(t_strconcat(root, "/", path, NULL), npath_r, error_r);
}

int t_abspath(const char *path, const char **abspath_r, const char **error_r)
{
	i_assert(path != NULL);
	i_assert(abspath_r != NULL);
	i_assert(error_r != NULL);

	if (*path == '/') {
		*abspath_r = path;
		return 0;
	}

	const char *dir, *error;
	if (t_get_working_dir(&dir, &error) < 0) {
		*error_r = t_strconcat("Failed to get working directory: ",
				       error, NULL);
		return -1;
	}
	*abspath_r = t_strconcat(dir, "/", path, NULL);
	return 0;
}

const char *t_abspath_to(const char *path, const char *root)
{
	i_assert(path != NULL);
	i_assert(root != NULL);

	if (*path == '/')
		return path;

	return t_strconcat(root, "/", path, NULL);
}

int t_get_working_dir(const char **dir_r, const char **error_r)
{
	char *dir;

	i_assert(dir_r != NULL);
	i_assert(error_r != NULL);
	if (t_getcwd_noalloc(&dir, NULL, error_r) < 0)
		return -1;

	t_buffer_alloc(strlen(dir) + 1);
	*dir_r = dir;
	return 0;
}

int t_readlink(const char *path, const char **dest_r, const char **error_r)
{
	i_assert(error_r != NULL);

	/* @UNSAFE */
	ssize_t ret;
	char *dest;
	size_t size = 128;

	dest = t_buffer_get(size);
	while ((ret = readlink(path, dest, size)) >= (ssize_t)size) {
		size = nearest_power(size+1);
		dest = t_buffer_get(size);
	}
	if (ret < 0) {
		*error_r = t_strdup_printf("readlink() failed: %m");
		return -1;
	}

	dest[ret] = '\0';
	t_buffer_alloc(ret + 1);
	*dest_r = dest;
	return 0;
}

bool t_binary_abspath(const char **binpath, const char **error_r)
{
	const char *path_env, *const *paths;
	string_t *path;

	if (**binpath == '/') {
		/* already have absolute path */
		return TRUE;
	} else if (strchr(*binpath, '/') != NULL) {
		/* relative to current directory */
		const char *error;
		if (t_abspath(*binpath, binpath, &error) < 0) {
			*error_r = t_strdup_printf("t_abspath(%s) failed: %s",
						   *binpath, error);
			return FALSE;
		}
		return TRUE;
	} else if ((path_env = getenv("PATH")) != NULL) {
		/* we have to find our executable from path */
		path = t_str_new(256);
		paths = t_strsplit(path_env, ":");
		for (; *paths != NULL; paths++) {
			str_append(path, *paths);
			str_append_c(path, '/');
			str_append(path, *binpath);
			if (access(str_c(path), X_OK) == 0) {
				*binpath = str_c(path);
				return TRUE;
			}
			str_truncate(path, 0);
		}
		*error_r = "Could not find the wanted executable from PATH";
		return FALSE;
	} else {
		*error_r = "PATH environment variable undefined";
		return FALSE;
	}
}