1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <sys/utsname.h>
11 #include "alloc-util.h"
12 #include "architecture.h"
14 #include "format-util.h"
16 #include "hostname-util.h"
17 #include "id128-util.h"
20 #include "specifier.h"
21 #include "string-util.h"
23 #include "user-util.h"
26 * Generic infrastructure for replacing %x style specifiers in
27 * strings. Will call a callback for each replacement.
30 /* Any ASCII character or digit: our pool of potential specifiers,
31 * and "%" used for escaping. */
32 #define POSSIBLE_SPECIFIERS ALPHANUMERICAL "%"
34 int specifier_printf(const char *text
, size_t max_length
, const Specifier table
[], const char *root
, const void *userdata
, char **ret
) {
35 _cleanup_free_
char *result
= NULL
;
46 if (!GREEDY_REALLOC(result
, l
+ 1))
50 for (f
= text
; *f
!= '\0'; f
++, l
--) {
57 for (i
= table
; i
->specifier
; i
++)
58 if (i
->specifier
== *f
)
62 _cleanup_free_
char *w
= NULL
;
65 r
= i
->lookup(i
->specifier
, i
->data
, root
, userdata
, &w
);
72 if (!GREEDY_REALLOC(result
, j
+ k
+ l
+ 1))
74 memcpy(result
+ j
, w
, k
);
76 } else if (strchr(POSSIBLE_SPECIFIERS
, *f
))
77 /* Oops, an unknown specifier. */
91 if ((size_t) (t
- result
) > max_length
)
95 /* If string ended with a stray %, also end with % */
98 if ((size_t) (t
- result
) > max_length
)
103 *ret
= TAKE_PTR(result
);
107 /* Generic handler for simple string replacements */
109 int specifier_string(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
112 n
= strdup(strempty(data
));
120 int specifier_machine_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
126 _cleanup_close_
int fd
= -1;
128 fd
= chase_symlinks_and_open("/etc/machine-id", root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
, NULL
);
132 r
= id128_read_fd(fd
, ID128_PLAIN
, &id
);
134 r
= sd_id128_get_machine(&id
);
138 n
= new(char, SD_ID128_STRING_MAX
);
142 *ret
= sd_id128_to_string(id
, n
);
146 int specifier_boot_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
151 r
= sd_id128_get_boot(&id
);
155 n
= new(char, SD_ID128_STRING_MAX
);
159 *ret
= sd_id128_to_string(id
, n
);
163 int specifier_host_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
166 n
= gethostname_malloc();
174 int specifier_short_host_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
177 n
= gethostname_short_malloc();
185 int specifier_kernel_release(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
194 n
= strdup(uts
.release
);
202 int specifier_architecture(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
205 t
= strdup(architecture_to_string(uname_architecture()));
213 static int specifier_os_release_common(const char *field
, const char *root
, char **ret
) {
217 r
= parse_os_release(root
, field
, &t
);
221 /* fields in /etc/os-release might quite possibly be missing, even if everything is entirely
222 * valid otherwise. Let's hence return "" in that case. */
232 int specifier_os_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
233 return specifier_os_release_common("ID", root
, ret
);
236 int specifier_os_version_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
237 return specifier_os_release_common("VERSION_ID", root
, ret
);
240 int specifier_os_build_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
241 return specifier_os_release_common("BUILD_ID", root
, ret
);
244 int specifier_os_variant_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
245 return specifier_os_release_common("VARIANT_ID", root
, ret
);
248 int specifier_os_image_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
249 return specifier_os_release_common("IMAGE_ID", root
, ret
);
252 int specifier_os_image_version(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
253 return specifier_os_release_common("IMAGE_VERSION", root
, ret
);
256 int specifier_group_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
259 t
= gid_to_name(getgid());
267 int specifier_group_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
268 if (asprintf(ret
, UID_FMT
, getgid()) < 0)
274 int specifier_user_name(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
277 /* If we are UID 0 (root), this will not result in NSS, otherwise it might. This is good, as we want to be able
278 * to run this in PID 1, where our user ID is 0, but where NSS lookups are not allowed.
280 * We don't use getusername_malloc() here, because we don't want to look at $USER, to remain consistent with
281 * specifer_user_id() below.
284 t
= uid_to_name(getuid());
292 int specifier_user_id(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
294 if (asprintf(ret
, UID_FMT
, getuid()) < 0)
300 int specifier_user_home(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
302 /* On PID 1 (which runs as root) this will not result in NSS,
303 * which is good. See above */
305 return get_home_dir(ret
);
308 int specifier_user_shell(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
310 /* On PID 1 (which runs as root) this will not result in NSS,
311 * which is good. See above */
313 return get_shell(ret
);
316 int specifier_tmp_dir(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
321 if (root
) /* If root dir is set, don't honour $TMP or similar */
336 int specifier_var_tmp_dir(char specifier
, const void *data
, const char *root
, const void *userdata
, char **ret
) {
356 int specifier_escape_strv(char **l
, char ***ret
) {
361 if (strv_isempty(l
)) {
366 z
= new(char*, strv_length(l
)+1);
370 for (p
= l
, q
= z
; *p
; p
++, q
++) {
372 *q
= specifier_escape(*p
);
385 const Specifier system_and_tmp_specifier_table
[] = {
386 COMMON_SYSTEM_SPECIFIERS
,
387 COMMON_TMP_SPECIFIERS
,