]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/specifier.c
cd651768bd80725c11e4bead8c942cca370f890d
[thirdparty/systemd.git] / src / shared / specifier.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdlib.h>
7 #include <sys/utsname.h>
8
9 #include "sd-id128.h"
10
11 #include "alloc-util.h"
12 #include "architecture.h"
13 #include "chase-symlinks.h"
14 #include "fd-util.h"
15 #include "format-util.h"
16 #include "fs-util.h"
17 #include "hostname-util.h"
18 #include "id128-util.h"
19 #include "macro.h"
20 #include "os-util.h"
21 #include "path-lookup.h"
22 #include "path-util.h"
23 #include "specifier.h"
24 #include "string-util.h"
25 #include "strv.h"
26 #include "user-util.h"
27
28 /*
29 * Generic infrastructure for replacing %x style specifiers in
30 * strings. Will call a callback for each replacement.
31 */
32
33 /* Any ASCII character or digit: our pool of potential specifiers,
34 * and "%" used for escaping. */
35 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
36
37 int specifier_printf(const char *text, size_t max_length, const Specifier table[], const char *root, const void *userdata, char **ret) {
38 _cleanup_free_ char *result = NULL;
39 bool percent = false;
40 size_t l;
41 char *t;
42 int r;
43
44 assert(ret);
45 assert(text);
46 assert(table);
47
48 l = strlen(text);
49 if (!GREEDY_REALLOC(result, l + 1))
50 return -ENOMEM;
51 t = result;
52
53 for (const char *f = text; *f != '\0'; f++, l--) {
54 if (percent) {
55 percent = false;
56
57 if (*f == '%')
58 *(t++) = '%';
59 else {
60 const Specifier *i;
61
62 for (i = table; i->specifier; i++)
63 if (i->specifier == *f)
64 break;
65
66 if (i->lookup) {
67 _cleanup_free_ char *w = NULL;
68 size_t k, j;
69
70 r = i->lookup(i->specifier, i->data, root, userdata, &w);
71 if (r < 0)
72 return r;
73 if (isempty(w))
74 continue;
75
76 j = t - result;
77 k = strlen(w);
78
79 if (!GREEDY_REALLOC(result, j + k + l + 1))
80 return -ENOMEM;
81 memcpy(result + j, w, k);
82 t = result + j + k;
83 } else if (strchr(POSSIBLE_SPECIFIERS, *f))
84 /* Oops, an unknown specifier. */
85 return -EBADSLT;
86 else {
87 *(t++) = '%';
88 *(t++) = *f;
89 }
90 }
91 } else if (*f == '%')
92 percent = true;
93 else
94 *(t++) = *f;
95
96 if ((size_t) (t - result) > max_length)
97 return -ENAMETOOLONG;
98 }
99
100 /* If string ended with a stray %, also end with % */
101 if (percent) {
102 *(t++) = '%';
103 if ((size_t) (t - result) > max_length)
104 return -ENAMETOOLONG;
105 }
106 *(t++) = 0;
107
108 *ret = TAKE_PTR(result);
109 return 0;
110 }
111
112 /* Generic handler for simple string replacements */
113
114 int specifier_string(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
115 char *n = NULL;
116
117 assert(ret);
118
119 if (!isempty(data)) {
120 n = strdup(data);
121 if (!n)
122 return -ENOMEM;
123 }
124
125 *ret = n;
126 return 0;
127 }
128
129 int specifier_real_path(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
130 const char *path = data;
131
132 assert(ret);
133
134 if (!path)
135 return -ENOENT;
136
137 return chase_symlinks(path, root, 0, ret, NULL);
138 }
139
140 int specifier_real_directory(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
141 _cleanup_free_ char *path = NULL;
142 int r;
143
144 assert(ret);
145
146 r = specifier_real_path(specifier, data, root, userdata, &path);
147 if (r < 0)
148 return r;
149
150 assert(path);
151 return path_extract_directory(path, ret);
152 }
153
154 int specifier_id128(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
155 const sd_id128_t *id = ASSERT_PTR(data);
156 char *n;
157
158 n = new(char, SD_ID128_STRING_MAX);
159 if (!n)
160 return -ENOMEM;
161
162 *ret = sd_id128_to_string(*id, n);
163 return 0;
164 }
165
166 int specifier_uuid(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
167 const sd_id128_t *id = ASSERT_PTR(data);
168 char *n;
169
170 n = new(char, SD_ID128_UUID_STRING_MAX);
171 if (!n)
172 return -ENOMEM;
173
174 *ret = sd_id128_to_uuid_string(*id, n);
175 return 0;
176 }
177
178 int specifier_uint64(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
179 const uint64_t *n = ASSERT_PTR(data);
180
181 return asprintf(ret, "%" PRIu64, *n) < 0 ? -ENOMEM : 0;
182 }
183
184 int specifier_machine_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
185 sd_id128_t id;
186 int r;
187
188 assert(ret);
189
190 if (root) {
191 _cleanup_close_ int fd = -1;
192
193 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
194 if (fd < 0)
195 /* Translate error for missing os-release file to EUNATCH. */
196 return fd == -ENOENT ? -EUNATCH : fd;
197
198 r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
199 } else
200 r = sd_id128_get_machine(&id);
201 if (r < 0)
202 return r;
203
204 return specifier_id128(specifier, &id, root, userdata, ret);
205 }
206
207 int specifier_boot_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
208 sd_id128_t id;
209 int r;
210
211 assert(ret);
212
213 r = sd_id128_get_boot(&id);
214 if (r < 0)
215 return r;
216
217 return specifier_id128(specifier, &id, root, userdata, ret);
218 }
219
220 int specifier_hostname(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
221 char *n;
222
223 assert(ret);
224
225 n = gethostname_malloc();
226 if (!n)
227 return -ENOMEM;
228
229 *ret = n;
230 return 0;
231 }
232
233 int specifier_short_hostname(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
234 char *n;
235
236 assert(ret);
237
238 n = gethostname_short_malloc();
239 if (!n)
240 return -ENOMEM;
241
242 *ret = n;
243 return 0;
244 }
245
246 int specifier_pretty_hostname(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
247 char *n = NULL;
248
249 assert(ret);
250
251 if (get_pretty_hostname(&n) < 0) {
252 n = gethostname_short_malloc();
253 if (!n)
254 return -ENOMEM;
255 }
256
257 *ret = n;
258 return 0;
259 }
260
261 int specifier_kernel_release(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
262 struct utsname uts;
263 char *n;
264
265 assert(ret);
266
267 if (uname(&uts) < 0)
268 return -errno;
269
270 n = strdup(uts.release);
271 if (!n)
272 return -ENOMEM;
273
274 *ret = n;
275 return 0;
276 }
277
278 int specifier_architecture(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
279 char *t;
280
281 assert(ret);
282
283 t = strdup(architecture_to_string(uname_architecture()));
284 if (!t)
285 return -ENOMEM;
286
287 *ret = t;
288 return 0;
289 }
290
291 /* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
292 * otherwise. We'll return an empty value or NULL in that case from the functions below. But if the
293 * os-release file is missing, we'll return -EUNATCH. This means that something is seriously wrong with the
294 * installation. */
295
296 static int parse_os_release_specifier(const char *root, const char *id, char **ret) {
297 char *v = NULL;
298 int r;
299
300 assert(ret);
301
302 r = parse_os_release(root, id, &v);
303 if (r >= 0)
304 /* parse_os_release() calls parse_env_file() which only sets the return value for
305 * entries found. Let's make sure we set the return value in all cases. */
306 *ret = v;
307
308 /* Translate error for missing os-release file to EUNATCH. */
309 return r == -ENOENT ? -EUNATCH : r;
310 }
311
312 int specifier_os_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
313 return parse_os_release_specifier(root, "ID", ret);
314 }
315
316 int specifier_os_version_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
317 return parse_os_release_specifier(root, "VERSION_ID", ret);
318 }
319
320 int specifier_os_build_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
321 return parse_os_release_specifier(root, "BUILD_ID", ret);
322 }
323
324 int specifier_os_variant_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
325 return parse_os_release_specifier(root, "VARIANT_ID", ret);
326 }
327
328 int specifier_os_image_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
329 return parse_os_release_specifier(root, "IMAGE_ID", ret);
330 }
331
332 int specifier_os_image_version(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
333 return parse_os_release_specifier(root, "IMAGE_VERSION", ret);
334 }
335
336 int specifier_group_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
337 LookupScope scope = PTR_TO_INT(data);
338 char *t;
339
340 assert(ret);
341
342 if (scope == LOOKUP_SCOPE_GLOBAL)
343 return -EINVAL;
344
345 t = gid_to_name(scope == LOOKUP_SCOPE_USER ? getgid() : 0);
346 if (!t)
347 return -ENOMEM;
348
349 *ret = t;
350 return 0;
351 }
352
353 int specifier_group_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
354 LookupScope scope = PTR_TO_INT(data);
355 gid_t gid;
356
357 assert(ret);
358
359 if (scope == LOOKUP_SCOPE_GLOBAL)
360 return -EINVAL;
361
362 gid = scope == LOOKUP_SCOPE_USER ? getgid() : 0;
363
364 if (asprintf(ret, UID_FMT, gid) < 0)
365 return -ENOMEM;
366
367 return 0;
368 }
369
370 int specifier_user_name(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
371 LookupScope scope = PTR_TO_INT(data);
372 uid_t uid;
373 char *t;
374
375 assert(ret);
376
377 if (scope == LOOKUP_SCOPE_GLOBAL)
378 return -EINVAL;
379
380 uid = scope == LOOKUP_SCOPE_USER ? getuid() : 0;
381
382 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want
383 * to be able to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
384
385 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain
386 * consistent with specifer_user_id() below.
387 */
388
389 t = uid_to_name(uid);
390 if (!t)
391 return -ENOMEM;
392
393 *ret = t;
394 return 0;
395 }
396
397 int specifier_user_id(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
398 LookupScope scope = PTR_TO_INT(data);
399 uid_t uid;
400
401 assert(ret);
402
403 if (scope == LOOKUP_SCOPE_GLOBAL)
404 return -EINVAL;
405
406 uid = scope == LOOKUP_SCOPE_USER ? getuid() : 0;
407
408 if (asprintf(ret, UID_FMT, uid) < 0)
409 return -ENOMEM;
410
411 return 0;
412 }
413
414 int specifier_user_home(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
415 assert(ret);
416
417 /* On PID 1 (which runs as root) this will not result in NSS,
418 * which is good. See above */
419
420 return get_home_dir(ret);
421 }
422
423 int specifier_user_shell(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
424 assert(ret);
425
426 /* On PID 1 (which runs as root) this will not result in NSS,
427 * which is good. See above */
428
429 return get_shell(ret);
430 }
431
432 int specifier_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
433 const char *p;
434 char *copy;
435 int r;
436
437 assert(ret);
438
439 if (root) /* If root dir is set, don't honour $TMP or similar */
440 p = "/tmp";
441 else {
442 r = tmp_dir(&p);
443 if (r < 0)
444 return r;
445 }
446 copy = strdup(p);
447 if (!copy)
448 return -ENOMEM;
449
450 *ret = copy;
451 return 0;
452 }
453
454 int specifier_var_tmp_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
455 const char *p;
456 char *copy;
457 int r;
458
459 assert(ret);
460
461 if (root)
462 p = "/var/tmp";
463 else {
464 r = var_tmp_dir(&p);
465 if (r < 0)
466 return r;
467 }
468 copy = strdup(p);
469 if (!copy)
470 return -ENOMEM;
471
472 *ret = copy;
473 return 0;
474 }
475
476 int specifier_escape_strv(char **l, char ***ret) {
477 char **z, **p, **q;
478
479 assert(ret);
480
481 if (strv_isempty(l)) {
482 *ret = NULL;
483 return 0;
484 }
485
486 z = new(char*, strv_length(l)+1);
487 if (!z)
488 return -ENOMEM;
489
490 for (p = l, q = z; *p; p++, q++) {
491
492 *q = specifier_escape(*p);
493 if (!*q) {
494 strv_free(z);
495 return -ENOMEM;
496 }
497 }
498
499 *q = NULL;
500 *ret = z;
501
502 return 0;
503 }
504
505 const Specifier system_and_tmp_specifier_table[] = {
506 COMMON_SYSTEM_SPECIFIERS,
507 COMMON_TMP_SPECIFIERS,
508 {}
509 };