1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <sys/utsname.h>
11 #include "alloc-util.h"
12 #include "architecture.h"
15 #include "format-util.h"
17 #include "hostname-util.h"
18 #include "id128-util.h"
21 #include "path-lookup.h"
22 #include "path-util.h"
23 #include "specifier.h"
24 #include "string-util.h"
26 #include "user-util.h"
29 * Generic infrastructure for replacing %x style specifiers in
30 * strings. Will call a callback for each replacement.
33 /* Any ASCII character or digit: our pool of potential specifiers,
34 * and "%" used for escaping. */
35 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
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
;
49 if (!GREEDY_REALLOC(result
, l
+ 1))
53 for (const char *f
= text
; *f
!= '\0'; f
++, l
--) {
62 for (i
= table
; i
->specifier
; i
++)
63 if (i
->specifier
== *f
)
67 _cleanup_free_
char *w
= NULL
;
70 r
= i
->lookup(i
->specifier
, i
->data
, root
, userdata
, &w
);
79 if (!GREEDY_REALLOC(result
, j
+ k
+ l
+ 1))
81 t
= mempcpy(result
+ j
, w
, k
);
82 } else if (strchr(POSSIBLE_SPECIFIERS
, *f
))
83 /* Oops, an unknown specifier. */
95 if ((size_t) (t
- result
) > max_length
)
99 /* If string ended with a stray %, also end with % */
102 if ((size_t) (t
- result
) > max_length
)
103 return -ENAMETOOLONG
;
107 *ret
= TAKE_PTR(result
);
111 /* Generic handler for simple string replacements */
113 int specifier_string(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
118 if (!isempty(data
)) {
128 int specifier_real_path(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
129 const char *path
= data
;
136 return chase(path
, root
, 0, ret
, NULL
);
139 int specifier_real_directory(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
140 _cleanup_free_
char *path
= NULL
;
145 r
= specifier_real_path(specifier
, data
, root
, userdata
, &path
);
150 return path_extract_directory(path
, ret
);
153 int specifier_id128(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
154 const sd_id128_t
*id
= ASSERT_PTR(data
);
157 n
= new(char, SD_ID128_STRING_MAX
);
161 *ret
= sd_id128_to_string(*id
, n
);
165 int specifier_uuid(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
166 const sd_id128_t
*id
= ASSERT_PTR(data
);
169 n
= new(char, SD_ID128_UUID_STRING_MAX
);
173 *ret
= sd_id128_to_uuid_string(*id
, n
);
177 int specifier_uint64(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
178 const uint64_t *n
= ASSERT_PTR(data
);
180 return asprintf(ret
, "%" PRIu64
, *n
) < 0 ? -ENOMEM
: 0;
183 int specifier_machine_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
189 r
= id128_get_machine(root
, &id
);
190 if (r
< 0) /* Translate error for missing /etc/machine-id file to EUNATCH. */
191 return r
== -ENOENT
? -EUNATCH
: r
;
193 return specifier_id128(specifier
, &id
, root
, userdata
, ret
);
196 int specifier_boot_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
202 r
= sd_id128_get_boot(&id
);
206 return specifier_id128(specifier
, &id
, root
, userdata
, ret
);
209 int specifier_hostname(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
214 n
= gethostname_malloc();
222 int specifier_short_hostname(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
227 n
= gethostname_short_malloc();
235 int specifier_pretty_hostname(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
240 if (get_pretty_hostname(&n
) < 0) {
241 n
= gethostname_short_malloc();
250 int specifier_kernel_release(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
259 n
= strdup(uts
.release
);
267 int specifier_architecture(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
272 t
= strdup(architecture_to_string(uname_architecture()));
280 /* Note: fields in /etc/os-release might quite possibly be missing, even if everything is entirely valid
281 * otherwise. We'll return an empty value or NULL in that case from the functions below. But if the
282 * os-release file is missing, we'll return -EUNATCH. This means that something is seriously wrong with the
285 static int parse_os_release_specifier(const char *root
, const char *id
, char **ret
) {
286 _cleanup_free_
char *v
= NULL
;
291 r
= parse_os_release(root
, id
, &v
);
293 /* parse_os_release() calls parse_env_file() which only sets the return value for
294 * entries found. Let's make sure we set the return value in all cases. */
297 /* Translate error for missing os-release file to EUNATCH. */
298 return r
== -ENOENT
? -EUNATCH
: r
;
301 int specifier_os_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
302 return parse_os_release_specifier(root
, "ID", ret
);
305 int specifier_os_version_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
306 return parse_os_release_specifier(root
, "VERSION_ID", ret
);
309 int specifier_os_build_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
310 return parse_os_release_specifier(root
, "BUILD_ID", ret
);
313 int specifier_os_variant_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
314 return parse_os_release_specifier(root
, "VARIANT_ID", ret
);
317 int specifier_os_image_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
318 return parse_os_release_specifier(root
, "IMAGE_ID", ret
);
321 int specifier_os_image_version(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
322 return parse_os_release_specifier(root
, "IMAGE_VERSION", ret
);
325 int specifier_group_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
326 RuntimeScope scope
= PTR_TO_INT(data
);
331 if (scope
== RUNTIME_SCOPE_GLOBAL
)
334 t
= gid_to_name(scope
== RUNTIME_SCOPE_USER
? getgid() : 0);
342 int specifier_group_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
343 RuntimeScope scope
= PTR_TO_INT(data
);
348 if (scope
== RUNTIME_SCOPE_GLOBAL
)
351 gid
= scope
== RUNTIME_SCOPE_USER
? getgid() : 0;
353 if (asprintf(ret
, UID_FMT
, gid
) < 0)
359 int specifier_user_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
360 RuntimeScope scope
= PTR_TO_INT(data
);
366 if (scope
== RUNTIME_SCOPE_GLOBAL
)
369 uid
= scope
== RUNTIME_SCOPE_USER
? getuid() : 0;
371 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want
372 * to be able to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
374 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain
375 * consistent with specifer_user_id() below.
378 t
= uid_to_name(uid
);
386 int specifier_user_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
387 RuntimeScope scope
= PTR_TO_INT(data
);
392 if (scope
== RUNTIME_SCOPE_GLOBAL
)
395 uid
= scope
== RUNTIME_SCOPE_USER
? getuid() : 0;
397 if (asprintf(ret
, UID_FMT
, uid
) < 0)
403 int specifier_user_home(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
406 /* On PID 1 (which runs as root) this will not result in NSS,
407 * which is good. See above */
409 return get_home_dir(ret
);
412 int specifier_user_shell(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
415 /* On PID 1 (which runs as root) this will not result in NSS,
416 * which is good. See above */
418 return get_shell(ret
);
421 int specifier_tmp_dir(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
428 if (root
) /* If root dir is set, don't honour $TMP or similar */
443 int specifier_var_tmp_dir(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
465 int specifier_escape_strv(char **l
, char ***ret
) {
466 _cleanup_strv_free_
char **z
= NULL
;
471 if (strv_isempty(l
)) {
476 z
= new(char*, strv_length(l
)+1);
480 for (p
= l
, q
= z
; *p
; p
++, q
++) {
482 *q
= specifier_escape(*p
);
493 const Specifier system_and_tmp_specifier_table
[] = {
494 COMMON_SYSTEM_SPECIFIERS
,
495 COMMON_TMP_SPECIFIERS
,