Blame


1 7b19e0f1 2017-11-05 stsp /*
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>
4 7b19e0f1 2017-11-05 stsp *
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.
8 7b19e0f1 2017-11-05 stsp *
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.
16 7b19e0f1 2017-11-05 stsp */
17 7b19e0f1 2017-11-05 stsp
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>
23 4027f31a 2017-11-04 stsp
24 9d31a1d8 2018-03-11 stsp #include "got_error.h"
25 9d31a1d8 2018-03-11 stsp
26 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
27 1411938b 2018-02-12 stsp
28 4027f31a 2017-11-04 stsp int
29 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
30 4027f31a 2017-11-04 stsp {
31 4027f31a 2017-11-04 stsp return path[0] == '/';
32 4027f31a 2017-11-04 stsp }
33 4027f31a 2017-11-04 stsp
34 4027f31a 2017-11-04 stsp char *
35 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
36 4027f31a 2017-11-04 stsp {
37 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
38 4027f31a 2017-11-04 stsp char *abspath;
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
41 4027f31a 2017-11-04 stsp return NULL;
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
44 4027f31a 2017-11-04 stsp return NULL;
45 4027f31a 2017-11-04 stsp
46 4027f31a 2017-11-04 stsp return abspath;
47 4027f31a 2017-11-04 stsp }
48 4027f31a 2017-11-04 stsp
49 4027f31a 2017-11-04 stsp char *
50 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
51 4027f31a 2017-11-04 stsp {
52 4027f31a 2017-11-04 stsp char *resolved;
53 4027f31a 2017-11-04 stsp
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;
57 4027f31a 2017-11-04 stsp
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;
62 4027f31a 2017-11-04 stsp }
63 4027f31a 2017-11-04 stsp
64 4027f31a 2017-11-04 stsp return resolved;
65 4027f31a 2017-11-04 stsp }
66 e6eac3b8 2018-06-17 stsp
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)
70 e6eac3b8 2018-06-17 stsp {
71 e6eac3b8 2018-06-17 stsp const char *p;
72 e6eac3b8 2018-06-17 stsp char *q;
73 e6eac3b8 2018-06-17 stsp
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;
79 e6eac3b8 2018-06-17 stsp }
80 e6eac3b8 2018-06-17 stsp
81 e6eac3b8 2018-06-17 stsp p = input;
82 e6eac3b8 2018-06-17 stsp q = buf;
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')) {
85 e6eac3b8 2018-06-17 stsp p += 1;
86 e6eac3b8 2018-06-17 stsp
87 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
88 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
89 e6eac3b8 2018-06-17 stsp p += 2;
90 e6eac3b8 2018-06-17 stsp
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')) {
93 e6eac3b8 2018-06-17 stsp p += 3;
94 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
95 e6eac3b8 2018-06-17 stsp while (*--q != '/')
96 e6eac3b8 2018-06-17 stsp continue;
97 e6eac3b8 2018-06-17 stsp
98 e6eac3b8 2018-06-17 stsp } else {
99 e6eac3b8 2018-06-17 stsp *q++ = *p++;
100 e6eac3b8 2018-06-17 stsp }
101 e6eac3b8 2018-06-17 stsp }
102 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
103 e6eac3b8 2018-06-17 stsp *q = 0;
104 f7d20e89 2018-06-17 stsp return NULL;
105 e6eac3b8 2018-06-17 stsp } else
106 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
107 e6eac3b8 2018-06-17 stsp }
108 04ca23f4 2018-07-16 stsp
109 04ca23f4 2018-07-16 stsp const struct got_error *
110 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
111 04ca23f4 2018-07-16 stsp const char *abspath)
112 04ca23f4 2018-07-16 stsp {
113 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
114 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
115 04ca23f4 2018-07-16 stsp
116 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
117 04ca23f4 2018-07-16 stsp len = strlen(abspath);
118 04ca23f4 2018-07-16 stsp if (len_parent >= len)
119 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
120 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
121 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
122 04ca23f4 2018-07-16 stsp if (abspath[len_parent] != '/')
123 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
124 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
125 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
126 04ca23f4 2018-07-16 stsp if (*child == NULL)
127 04ca23f4 2018-07-16 stsp return got_error_from_errno();
128 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
129 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
130 04ca23f4 2018-07-16 stsp free(*child);
131 04ca23f4 2018-07-16 stsp *child = NULL;
132 04ca23f4 2018-07-16 stsp return err;
133 04ca23f4 2018-07-16 stsp }
134 04ca23f4 2018-07-16 stsp return NULL;
135 04ca23f4 2018-07-16 stsp }