Debian Codegraph @

src/shared/parse-helpers.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/socket.h>
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "extract-word.h"
8 #include "ip-protocol-list.h"
9 #include "log.h"
10 #include "mountpoint-util.h"
11 #include "parse-helpers.h"
12 #include "parse-util.h"
13 #include "path-util.h"
14 #include "set.h"
15 #include "string-util.h"
16 #include "utf8.h"
17
18 static bool validate_api_vfs(const char *path, PathSimplifyWarnFlags flags) {
19
20 assert(path);
21
22 if ((flags & (PATH_CHECK_NON_API_VFS|PATH_CHECK_NON_API_VFS_DEV_OK)) == 0)
23 return true;
24
25 if (!path_below_api_vfs(path))
26 return true;
27
28 if (FLAGS_SET(flags, PATH_CHECK_NON_API_VFS_DEV_OK) && path_startswith(path, "/dev"))
29 return true;
30
31 return false;
32 }
33
34 int path_simplify_and_warn(
35 char *path,
36 PathSimplifyWarnFlags flags,
37 const char *unit,
38 const char *filename,
39 unsigned line,
40 const char *lvalue) {
41
42 bool fatal = flags & PATH_CHECK_FATAL;
43 int level = fatal ? LOG_ERR : LOG_WARNING;
44
45 assert(path);
46 assert(!FLAGS_SET(flags, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
47 assert(!FLAGS_SET(flags, PATH_CHECK_NON_API_VFS | PATH_CHECK_NON_API_VFS_DEV_OK));
48 assert(lvalue);
49
50 if (!utf8_is_valid(path))
51 return log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
52
53 if (flags & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) {
54 bool absolute;
55
56 absolute = path_is_absolute(path);
57
58 if (!absolute && (flags & PATH_CHECK_ABSOLUTE))
59 return log_syntax(unit, level, filename, line, SYNTHETIC_ERRNO(EINVAL),
60 "%s= path is not absolute%s: %s",
61 lvalue, fatal ? "" : ", ignoring", path);
62
63 if (absolute && (flags & PATH_CHECK_RELATIVE))
64 return log_syntax(unit, level, filename, line, SYNTHETIC_ERRNO(EINVAL),
65 "%s= path is absolute%s: %s",
66 lvalue, fatal ? "" : ", ignoring", path);
67 }
68
69 path_simplify_full(path, flags & PATH_KEEP_TRAILING_SLASH ? PATH_SIMPLIFY_KEEP_TRAILING_SLASH : 0);
70
71 if (!path_is_valid(path))
72 return log_syntax(unit, level, filename, line, SYNTHETIC_ERRNO(EINVAL),
73 "%s= path has invalid length (%zu bytes)%s.",
74 lvalue, strlen(path), fatal ? "" : ", ignoring");
75
76 if (!path_is_normalized(path))
77 return log_syntax(unit, level, filename, line, SYNTHETIC_ERRNO(EINVAL),
78 "%s= path is not normalized%s: %s",
79 lvalue, fatal ? "" : ", ignoring", path);
80
81 if (!validate_api_vfs(path, flags))
82 return log_syntax(unit, level, filename, line, SYNTHETIC_ERRNO(EINVAL),
83 "%s= path is below API VFS%s: %s",
84 lvalue, fatal ? ", refusing" : ", ignoring",
85 path);
86
87 return 0;
88 }
89
90 int parse_address_families(const char *rvalue, Set **families, bool *is_allowlist) {
91 bool invert = false;
92 int r;
93
94 assert(rvalue);
95 assert(families);
96 assert(is_allowlist);
97
98 if (isempty(rvalue)) {
99 *families = set_free(*families);
100 *is_allowlist = false;
101 return 0;
102 }
103
104 if (streq(rvalue, "none")) {
105 *families = set_free(*families);
106 *is_allowlist = true;
107 return 0;
108 }
109
110 if (rvalue[0] == '~') {
111 invert = true;
112 rvalue++;
113 }
114
115 if (!*families) {
116 *families = set_new(NULL);
117 if (!*families)
118 return -ENOMEM;
119
120 *is_allowlist = !invert;
121 }
122
123 for (const char *p = rvalue;;) {
124 _cleanup_free_ char *word = NULL;
125
126 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
127 if (r == 0)
128 return 0;
129 if (r < 0)
130 return r;
131
132 int af = af_from_name(word);
133 if (af < 0)
134 return af;
135
136 /* If we previously wanted to forbid an address family and now we want to allow it, then
137 * just remove it from the list. */
138 if (invert != *is_allowlist) {
139 r = set_put(*families, INT_TO_PTR(af));
140 if (r < 0)
141 return r;
142 } else
143 set_remove(*families, INT_TO_PTR(af));
144 }
145 }
146
147 static int parse_af_token(
148 const char *token,
149 int *family,
150 int *ip_protocol,
151 uint16_t *nr_ports,
152 uint16_t *port_min) {
153
154 int af;
155
156 assert(token);
157 assert(family);
158
159 af = af_from_ipv4_ipv6(token);
160 if (af == AF_UNSPEC)
161 return -EINVAL;
162
163 *family = af;
164 return 0;
165 }
166
167 static int parse_ip_protocol_token(
168 const char *token,
169 int *family,
170 int *ip_protocol,
171 uint16_t *nr_ports,
172 uint16_t *port_min) {
173
174 int proto;
175
176 assert(token);
177 assert(ip_protocol);
178
179 proto = ip_protocol_from_tcp_udp(token);
180 if (proto < 0)
181 return -EINVAL;
182
183 *ip_protocol = proto;
184 return 0;
185 }
186
187 static int parse_ip_ports_token(
188 const char *token,
189 int *family,
190 int *ip_protocol,
191 uint16_t *nr_ports,
192 uint16_t *port_min) {
193
194 int r;
195
196 assert(token);
197 assert(nr_ports);
198 assert(port_min);
199
200 if (streq(token, "any"))
201 *nr_ports = *port_min = 0;
202 else {
203 uint16_t mn = 0, mx = 0;
204 r = parse_ip_port_range(token, &mn, &mx, /* allow_zero= */ true);
205 if (r < 0)
206 return r;
207
208 *nr_ports = mx - mn + 1;
209 *port_min = mn;
210 }
211
212 return 0;
213 }
214
215 typedef int (*parse_token_f)(
216 const char *,
217 int *,
218 int *,
219 uint16_t *,
220 uint16_t *);
221
222 int parse_socket_bind_item(
223 const char *str,
224 int *address_family,
225 int *ip_protocol,
226 uint16_t *nr_ports,
227 uint16_t *port_min) {
228
229 /* Order of token parsers is important. */
230 const parse_token_f parsers[] = {
231 &parse_af_token,
232 &parse_ip_protocol_token,
233 &parse_ip_ports_token,
234 };
235 parse_token_f const *parser_ptr = parsers;
236 int af = AF_UNSPEC, proto = 0, r;
237 uint16_t nr = 0, mn = 0;
238 const char *p = ASSERT_PTR(str);
239
240 assert(address_family);
241 assert(ip_protocol);
242 assert(nr_ports);
243 assert(port_min);
244
245 if (isempty(p))
246 return -EINVAL;
247
248 for (;;) {
249 _cleanup_free_ char *token = NULL;
250
251 r = extract_first_word(&p, &token, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
252 if (r == 0)
253 break;
254 if (r < 0)
255 return r;
256
257 if (isempty(token))
258 return -EINVAL;
259
260 while (parser_ptr != parsers + ELEMENTSOF(parsers)) {
261 r = (*parser_ptr)(token, &af, &proto, &nr, &mn);
262 if (r == -ENOMEM)
263 return r;
264
265 ++parser_ptr;
266 /* Continue to next token if parsing succeeded,
267 * otherwise apply next parser to the same token.
268 */
269 if (r >= 0)
270 break;
271 }
272 if (parser_ptr == parsers + ELEMENTSOF(parsers))
273 break;
274 }
275
276 /* Failed to parse a token. */
277 if (r < 0)
278 return r;
279
280 /* Parsers applied successfully, but end of the string not reached. */
281 if (p)
282 return -EINVAL;
283
284 *address_family = af;
285 *ip_protocol = proto;
286 *nr_ports = nr;
287 *port_min = mn;
288
289 return 0;
290 }
291
292 int config_parse_path_or_ignore(
293 const char *unit,
294 const char *filename,
295 unsigned line,
296 const char *section,
297 unsigned section_line,
298 const char *lvalue,
299 int ltype,
300 const char *rvalue,
301 void *data,
302 void *userdata) {
303
304 _cleanup_free_ char *n = NULL;
305 bool fatal = ltype;
306 char **s = ASSERT_PTR(data);
307 int r;
308
309 assert(filename);
310 assert(lvalue);
311 assert(rvalue);
312
313 if (isempty(rvalue))
314 goto finalize;
315
316 n = strdup(rvalue);
317 if (!n)
318 return log_oom();
319
320 if (streq(n, "-"))
321 goto finalize;
322
323 r = path_simplify_and_warn(n, PATH_CHECK_ABSOLUTE | (fatal ? PATH_CHECK_FATAL : 0), unit, filename, line, lvalue);
324 if (r < 0)
325 return fatal ? -ENOEXEC : 0;
326
327 finalize:
328 return free_and_replace(*s, n);
329 }