2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 e6eac3b8 2018-06-17 stsp * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
5 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
9 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 4027f31a 2017-11-04 stsp #include <limits.h>
19 4027f31a 2017-11-04 stsp #include <stdlib.h>
20 4027f31a 2017-11-04 stsp #include <unistd.h>
21 4027f31a 2017-11-04 stsp #include <stdio.h>
22 4027f31a 2017-11-04 stsp #include <string.h>
24 9d31a1d8 2018-03-11 stsp #include "got_error.h"
26 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
29 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
31 4027f31a 2017-11-04 stsp return path[0] == '/';
35 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
37 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
38 4027f31a 2017-11-04 stsp char *abspath;
40 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
41 4027f31a 2017-11-04 stsp return NULL;
43 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
44 4027f31a 2017-11-04 stsp return NULL;
46 4027f31a 2017-11-04 stsp return abspath;
50 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
52 4027f31a 2017-11-04 stsp char *resolved;
54 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
55 4027f31a 2017-11-04 stsp if (resolved == NULL)
56 4027f31a 2017-11-04 stsp return NULL;
58 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
59 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
60 4027f31a 2017-11-04 stsp free(resolved);
61 4027f31a 2017-11-04 stsp resolved = abspath;
64 4027f31a 2017-11-04 stsp return resolved;
67 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
68 f7d20e89 2018-06-17 stsp const struct got_error *
69 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
71 e6eac3b8 2018-06-17 stsp const char *p;
74 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
75 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
76 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
77 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
78 f7d20e89 2018-06-17 stsp return NULL;
83 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
84 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
87 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
88 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
91 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
92 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
94 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
95 e6eac3b8 2018-06-17 stsp while (*--q != '/')
99 e6eac3b8 2018-06-17 stsp *q++ = *p++;
102 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
104 f7d20e89 2018-06-17 stsp return NULL;
106 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);