| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "boot-entry.h" |
| 5 |
#include "chase.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "fileio.h" |
| 8 |
#include "log.h" |
| 9 |
#include "os-util.h" |
| 10 |
#include "path-util.h" |
| 11 |
#include "string-table.h" |
| 12 |
#include "string-util.h" |
| 13 |
#include "strv.h" |
| 14 |
|
| 15 |
bool boot_entry_token_valid(const char *p) { |
| 16 |
return string_is_safe(p, STRING_FILENAME); |
| 17 |
} |
| 18 |
|
| 19 |
static int entry_token_load_one(int rfd, const char *dir, BootEntryTokenType *type, char **token) { |
| 20 |
_cleanup_free_ char *buf = NULL, *p = NULL; |
| 21 |
_cleanup_fclose_ FILE *f = NULL; |
| 22 |
int r; |
| 23 |
|
| 24 |
assert(wildcard_fd_is_valid(rfd)); |
| 25 |
assert(dir); |
| 26 |
assert(type); |
| 27 |
assert(*type == BOOT_ENTRY_TOKEN_AUTO); |
| 28 |
assert(token); |
| 29 |
|
| 30 |
p = path_join(dir, "entry-token"); |
| 31 |
if (!p) |
| 32 |
return log_oom(); |
| 33 |
|
| 34 |
r = chase_and_fopenat_unlocked(rfd, rfd, p, /* chase_flags= */ 0, "re", NULL, &f); |
| 35 |
if (r == -ENOENT) |
| 36 |
return 0; |
| 37 |
if (r < 0) |
| 38 |
return log_error_errno(r, "Failed to chase and open '%s': %m", p); |
| 39 |
|
| 40 |
r = read_line(f, NAME_MAX, &buf); |
| 41 |
if (r < 0) |
| 42 |
return log_error_errno(r, "Failed to read %s: %m", p); |
| 43 |
|
| 44 |
if (isempty(buf)) |
| 45 |
return 0; |
| 46 |
|
| 47 |
if (!boot_entry_token_valid(buf)) { |
| 48 |
log_debug("Invalid entry token specified in %s, ignoring.", p); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
*token = TAKE_PTR(buf); |
| 53 |
*type = BOOT_ENTRY_TOKEN_LITERAL; |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
static int entry_token_load(int rfd, const char *conf_root, BootEntryTokenType *type, char **token) { |
| 58 |
int r; |
| 59 |
|
| 60 |
assert(wildcard_fd_is_valid(rfd)); |
| 61 |
assert(type); |
| 62 |
assert(*type == BOOT_ENTRY_TOKEN_AUTO); |
| 63 |
assert(token); |
| 64 |
|
| 65 |
if (conf_root) |
| 66 |
return entry_token_load_one(rfd, conf_root, type, token); |
| 67 |
|
| 68 |
FOREACH_STRING(path, "/etc/kernel", "/usr/lib/kernel") { |
| 69 |
r = entry_token_load_one(rfd, path, type, token); |
| 70 |
if (r != 0) |
| 71 |
return r; |
| 72 |
} |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
static int entry_token_from_machine_id(sd_id128_t machine_id, BootEntryTokenType *type, char **token) { |
| 78 |
char *p; |
| 79 |
|
| 80 |
assert(type); |
| 81 |
assert(IN_SET(*type, BOOT_ENTRY_TOKEN_AUTO, BOOT_ENTRY_TOKEN_MACHINE_ID)); |
| 82 |
assert(token); |
| 83 |
|
| 84 |
if (sd_id128_is_null(machine_id)) |
| 85 |
return 0; |
| 86 |
|
| 87 |
p = strdup(SD_ID128_TO_STRING(machine_id)); |
| 88 |
if (!p) |
| 89 |
return log_oom(); |
| 90 |
|
| 91 |
*token = p; |
| 92 |
*type = BOOT_ENTRY_TOKEN_MACHINE_ID; |
| 93 |
return 1; |
| 94 |
} |
| 95 |
|
| 96 |
static int entry_token_from_os_release(int rfd, BootEntryTokenType *type, char **token) { |
| 97 |
_cleanup_free_ char *id = NULL, *image_id = NULL; |
| 98 |
int r; |
| 99 |
|
| 100 |
assert(wildcard_fd_is_valid(rfd)); |
| 101 |
assert(type); |
| 102 |
assert(IN_SET(*type, BOOT_ENTRY_TOKEN_AUTO, BOOT_ENTRY_TOKEN_OS_IMAGE_ID, BOOT_ENTRY_TOKEN_OS_ID)); |
| 103 |
assert(token); |
| 104 |
|
| 105 |
switch (*type) { |
| 106 |
case BOOT_ENTRY_TOKEN_AUTO: |
| 107 |
r = parse_os_release_at(rfd, |
| 108 |
"IMAGE_ID", &image_id, |
| 109 |
"ID", &id); |
| 110 |
break; |
| 111 |
|
| 112 |
case BOOT_ENTRY_TOKEN_OS_IMAGE_ID: |
| 113 |
r = parse_os_release_at(rfd, "IMAGE_ID", &image_id); |
| 114 |
break; |
| 115 |
|
| 116 |
case BOOT_ENTRY_TOKEN_OS_ID: |
| 117 |
r = parse_os_release_at(rfd, "ID", &id); |
| 118 |
break; |
| 119 |
|
| 120 |
default: |
| 121 |
assert_not_reached(); |
| 122 |
} |
| 123 |
if (r == -ENOENT) |
| 124 |
return 0; |
| 125 |
if (r < 0) |
| 126 |
return log_error_errno(r, "Failed to load /etc/os-release: %m"); |
| 127 |
|
| 128 |
if (!isempty(image_id) && boot_entry_token_valid(image_id)) { |
| 129 |
*token = TAKE_PTR(image_id); |
| 130 |
*type = BOOT_ENTRY_TOKEN_OS_IMAGE_ID; |
| 131 |
return 1; |
| 132 |
} |
| 133 |
|
| 134 |
if (!isempty(id) && boot_entry_token_valid(id)) { |
| 135 |
*token = TAKE_PTR(id); |
| 136 |
*type = BOOT_ENTRY_TOKEN_OS_ID; |
| 137 |
return 1; |
| 138 |
} |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
int boot_entry_token_ensure_at( |
| 144 |
int rfd, |
| 145 |
const char *conf_root, |
| 146 |
sd_id128_t machine_id, |
| 147 |
bool machine_id_is_random, |
| 148 |
BootEntryTokenType *type, |
| 149 |
char **token) { |
| 150 |
|
| 151 |
int r; |
| 152 |
|
| 153 |
assert(wildcard_fd_is_valid(rfd)); |
| 154 |
assert(type); |
| 155 |
assert(token); |
| 156 |
|
| 157 |
/* Returns -EUNATCH if the selected token is not set */ |
| 158 |
|
| 159 |
if (*token) |
| 160 |
return 0; /* Already set. */ |
| 161 |
|
| 162 |
switch (*type) { |
| 163 |
|
| 164 |
case BOOT_ENTRY_TOKEN_AUTO: |
| 165 |
r = entry_token_load(rfd, conf_root, type, token); |
| 166 |
if (r != 0) |
| 167 |
return r; |
| 168 |
|
| 169 |
if (!machine_id_is_random) { |
| 170 |
r = entry_token_from_machine_id(machine_id, type, token); |
| 171 |
if (r != 0) |
| 172 |
return r; |
| 173 |
} |
| 174 |
|
| 175 |
r = entry_token_from_os_release(rfd, type, token); |
| 176 |
if (r != 0) |
| 177 |
return r; |
| 178 |
|
| 179 |
if (machine_id_is_random) { |
| 180 |
r = entry_token_from_machine_id(machine_id, type, token); |
| 181 |
if (r != 0) |
| 182 |
return r; |
| 183 |
} |
| 184 |
|
| 185 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), |
| 186 |
"No machine ID set, and /etc/os-release carries no ID=/IMAGE_ID= fields."); |
| 187 |
|
| 188 |
case BOOT_ENTRY_TOKEN_MACHINE_ID: |
| 189 |
r = entry_token_from_machine_id(machine_id, type, token); |
| 190 |
if (r != 0) |
| 191 |
return r; |
| 192 |
|
| 193 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), "No machine ID set."); |
| 194 |
|
| 195 |
case BOOT_ENTRY_TOKEN_OS_IMAGE_ID: |
| 196 |
r = entry_token_from_os_release(rfd, type, token); |
| 197 |
if (r != 0) |
| 198 |
return r; |
| 199 |
|
| 200 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), |
| 201 |
"IMAGE_ID= field not set in /etc/os-release."); |
| 202 |
|
| 203 |
case BOOT_ENTRY_TOKEN_OS_ID: |
| 204 |
r = entry_token_from_os_release(rfd, type, token); |
| 205 |
if (r != 0) |
| 206 |
return r; |
| 207 |
|
| 208 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), |
| 209 |
"ID= field not set in /etc/os-release."); |
| 210 |
|
| 211 |
case BOOT_ENTRY_TOKEN_LITERAL: |
| 212 |
/* In this case, the token should be already set by the user input. */ |
| 213 |
return log_error_errno(SYNTHETIC_ERRNO(EUNATCH), "Literal token indicated but not specified."); |
| 214 |
|
| 215 |
default: |
| 216 |
assert_not_reached(); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
int boot_entry_token_ensure( |
| 221 |
const char *root, |
| 222 |
const char *conf_root, |
| 223 |
sd_id128_t machine_id, |
| 224 |
bool machine_id_is_random, |
| 225 |
BootEntryTokenType *type, |
| 226 |
char **token) { |
| 227 |
|
| 228 |
assert(token); |
| 229 |
|
| 230 |
if (*token) |
| 231 |
return 0; /* Already set. */ |
| 232 |
|
| 233 |
_cleanup_close_ int rfd = XAT_FDROOT; |
| 234 |
if (!empty_or_root(root)) { |
| 235 |
rfd = open(root, O_CLOEXEC | O_DIRECTORY | O_PATH); |
| 236 |
if (rfd < 0) |
| 237 |
return -errno; |
| 238 |
} |
| 239 |
|
| 240 |
return boot_entry_token_ensure_at(rfd, conf_root, machine_id, machine_id_is_random, type, token); |
| 241 |
} |
| 242 |
|
| 243 |
int parse_boot_entry_token_type(const char *s, BootEntryTokenType *type, char **token) { |
| 244 |
assert(s); |
| 245 |
assert(type); |
| 246 |
assert(token); |
| 247 |
|
| 248 |
/* |
| 249 |
* This function is intended to be used in command line parsers, to handle token that are passed in. |
| 250 |
* |
| 251 |
* NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS! |
| 252 |
* Hence, do not pass in uninitialized pointers. |
| 253 |
*/ |
| 254 |
|
| 255 |
if (streq(s, "auto")) { |
| 256 |
*type = BOOT_ENTRY_TOKEN_AUTO; |
| 257 |
*token = mfree(*token); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
if (streq(s, "machine-id")) { |
| 262 |
*type = BOOT_ENTRY_TOKEN_MACHINE_ID; |
| 263 |
*token = mfree(*token); |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
if (streq(s, "os-image-id")) { |
| 268 |
*type = BOOT_ENTRY_TOKEN_OS_IMAGE_ID; |
| 269 |
*token = mfree(*token); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
if (streq(s, "os-id")) { |
| 274 |
*type = BOOT_ENTRY_TOKEN_OS_ID; |
| 275 |
*token = mfree(*token); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
const char *e = startswith(s, "literal:"); |
| 280 |
if (e) { |
| 281 |
if (!boot_entry_token_valid(e)) |
| 282 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 283 |
"Invalid entry token literal is specified for --entry-token=."); |
| 284 |
|
| 285 |
*type = BOOT_ENTRY_TOKEN_LITERAL; |
| 286 |
return free_and_strdup_warn(token, e); |
| 287 |
} |
| 288 |
|
| 289 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 290 |
"Unexpected parameter for --entry-token=: %s", s); |
| 291 |
} |
| 292 |
|
| 293 |
static const char *const boot_entry_token_type_table[] = { |
| 294 |
[BOOT_ENTRY_TOKEN_MACHINE_ID] = "machine-id", |
| 295 |
[BOOT_ENTRY_TOKEN_OS_IMAGE_ID] = "os-image-id", |
| 296 |
[BOOT_ENTRY_TOKEN_OS_ID] = "os-id", |
| 297 |
[BOOT_ENTRY_TOKEN_LITERAL] = "literal", |
| 298 |
[BOOT_ENTRY_TOKEN_AUTO] = "auto", |
| 299 |
}; |
| 300 |
|
| 301 |
DEFINE_STRING_TABLE_LOOKUP(boot_entry_token_type, BootEntryTokenType); |