| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <sys/stat.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-varlink.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "build.h" |
| 11 |
#include "build-path.h" |
| 12 |
#include "creds-util.h" |
| 13 |
#include "dns-rr.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "fileio.h" |
| 17 |
#include "format-table.h" |
| 18 |
#include "format-util.h" |
| 19 |
#include "fs-util.h" |
| 20 |
#include "hexdecoct.h" |
| 21 |
#include "imds-util.h" |
| 22 |
#include "in-addr-util.h" |
| 23 |
#include "io-util.h" |
| 24 |
#include "iovec-util.h" |
| 25 |
#include "json-util.h" |
| 26 |
#include "log.h" |
| 27 |
#include "main-func.h" |
| 28 |
#include "options.h" |
| 29 |
#include "parse-argument.h" |
| 30 |
#include "pcrextend-util.h" |
| 31 |
#include "pretty-print.h" |
| 32 |
#include "string-util.h" |
| 33 |
#include "strv.h" |
| 34 |
#include "time-util.h" |
| 35 |
#include "tmpfile-util.h" |
| 36 |
|
| 37 |
static enum { |
| 38 |
ACTION_SUMMARY, |
| 39 |
ACTION_GET, |
| 40 |
ACTION_USERDATA, |
| 41 |
ACTION_IMPORT, |
| 42 |
_ACTION_INVALID = -EINVAL, |
| 43 |
} arg_action = _ACTION_INVALID; |
| 44 |
static char *arg_key = NULL; |
| 45 |
static ImdsWellKnown arg_well_known = _IMDS_WELL_KNOWN_INVALID; |
| 46 |
static int arg_cache = -1; |
| 47 |
static usec_t arg_refresh_usec = 0; |
| 48 |
static bool arg_refresh_usec_set = false; |
| 49 |
|
| 50 |
STATIC_DESTRUCTOR_REGISTER(arg_key, freep); |
| 51 |
|
| 52 |
static int help(void) { |
| 53 |
_cleanup_free_ char *link = NULL; |
| 54 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 55 |
int r; |
| 56 |
|
| 57 |
r = terminal_urlify_man("systemd-imds", "1", &link); |
| 58 |
if (r < 0) |
| 59 |
return log_oom(); |
| 60 |
|
| 61 |
r = option_parser_get_help_table(&options); |
| 62 |
if (r < 0) |
| 63 |
return r; |
| 64 |
|
| 65 |
printf("%s [OPTIONS...] [KEY]\n" |
| 66 |
"\n%sIMDS data acquisition.%s\n\n", |
| 67 |
program_invocation_short_name, |
| 68 |
ansi_highlight(), |
| 69 |
ansi_normal()); |
| 70 |
|
| 71 |
r = table_print_or_warn(options); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
printf("\nSee the %s for details.\n", link); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static int parse_argv(int argc, char *argv[]) { |
| 80 |
int r; |
| 81 |
|
| 82 |
assert(argc >= 0); |
| 83 |
assert(argv); |
| 84 |
|
| 85 |
OptionParser opts = { argc, argv }; |
| 86 |
|
| 87 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 88 |
switch (c) { |
| 89 |
|
| 90 |
OPTION_COMMON_HELP: |
| 91 |
return help(); |
| 92 |
|
| 93 |
OPTION_COMMON_VERSION: |
| 94 |
return version(); |
| 95 |
|
| 96 |
OPTION('K', "well-known", "KEY", |
| 97 |
"Select well-known key/base, one of:" |
| 98 |
" hostname, region, zone, ipv4-public, ipv6-public, ssh-key," |
| 99 |
" userdata, userdata-base, userdata-base64"): { |
| 100 |
if (isempty(opts.arg)) { |
| 101 |
arg_well_known = _IMDS_WELL_KNOWN_INVALID; |
| 102 |
break; |
| 103 |
} |
| 104 |
|
| 105 |
if (streq(opts.arg, "help")) |
| 106 |
return DUMP_STRING_TABLE(imds_well_known, ImdsWellKnown, _IMDS_WELL_KNOWN_MAX); |
| 107 |
|
| 108 |
ImdsWellKnown wk = imds_well_known_from_string(opts.arg); |
| 109 |
if (wk < 0) |
| 110 |
return log_error_errno(wk, "Failed to parse --well-known= argument: %s", opts.arg); |
| 111 |
|
| 112 |
arg_well_known = wk; |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
OPTION_LONG("refresh", "SEC", "Set minimum freshness time for returned data"): { |
| 117 |
if (isempty(opts.arg)) { |
| 118 |
arg_refresh_usec_set = false; |
| 119 |
break; |
| 120 |
} |
| 121 |
|
| 122 |
usec_t t; |
| 123 |
r = parse_sec(opts.arg, &t); |
| 124 |
if (r < 0) |
| 125 |
return log_error_errno(r, "Failed to parse refresh timeout: %s", opts.arg); |
| 126 |
|
| 127 |
arg_refresh_usec = t; |
| 128 |
arg_refresh_usec_set = true; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
OPTION_LONG("cache", "BOOL", "Control cache use"): |
| 133 |
r = parse_tristate_argument_with_auto("--cache=", opts.arg, &arg_cache); |
| 134 |
if (r < 0) |
| 135 |
return r; |
| 136 |
break; |
| 137 |
|
| 138 |
OPTION('u', "userdata", NULL, "Dump user data"): |
| 139 |
arg_action = ACTION_USERDATA; |
| 140 |
break; |
| 141 |
|
| 142 |
OPTION_LONG("import", NULL, |
| 143 |
"Import system credentials from IMDS userdata" |
| 144 |
" and place them in /run/credstore/"): |
| 145 |
arg_action = ACTION_IMPORT; |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
char **args = option_parser_get_args(&opts); |
| 150 |
size_t n_args = option_parser_get_n_args(&opts); |
| 151 |
|
| 152 |
if (IN_SET(arg_action, ACTION_USERDATA, ACTION_IMPORT)) { |
| 153 |
if (n_args != 0) |
| 154 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No parameters expected."); |
| 155 |
|
| 156 |
} else { |
| 157 |
assert(arg_action < 0); |
| 158 |
|
| 159 |
if (n_args > 1) |
| 160 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "None or one argument expected."); |
| 161 |
|
| 162 |
if (n_args == 0 && arg_well_known < 0) |
| 163 |
arg_action = ACTION_SUMMARY; |
| 164 |
else { |
| 165 |
if (arg_well_known < 0) |
| 166 |
arg_well_known = IMDS_BASE; |
| 167 |
|
| 168 |
if (n_args > 0) { |
| 169 |
if (!imds_key_is_valid(args[0])) |
| 170 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified IMDS key is not valid, refusing: %s", args[0]); |
| 171 |
|
| 172 |
if (!imds_well_known_can_suffix(arg_well_known)) |
| 173 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Well known key '%s' does not take a key suffix, refusing.", imds_well_known_to_string(arg_well_known)); |
| 174 |
|
| 175 |
r = free_and_strdup_warn(&arg_key, args[0]); |
| 176 |
if (r < 0) |
| 177 |
return r; |
| 178 |
} |
| 179 |
|
| 180 |
arg_action = ACTION_GET; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return 1; |
| 185 |
} |
| 186 |
|
| 187 |
static int acquire_imds_key( |
| 188 |
sd_varlink *link, |
| 189 |
ImdsWellKnown wk, |
| 190 |
const char *key, |
| 191 |
struct iovec *ret) { |
| 192 |
|
| 193 |
int r; |
| 194 |
|
| 195 |
assert(link); |
| 196 |
assert(wk >= 0); |
| 197 |
assert(wk < _IMDS_WELL_KNOWN_MAX); |
| 198 |
assert(ret); |
| 199 |
|
| 200 |
const char *error_id = NULL; |
| 201 |
sd_json_variant *reply = NULL; |
| 202 |
r = sd_varlink_callbo( |
| 203 |
link, |
| 204 |
"io.systemd.InstanceMetadata.Get", |
| 205 |
&reply, |
| 206 |
&error_id, |
| 207 |
SD_JSON_BUILD_PAIR_CONDITION(wk != IMDS_BASE, "wellKnown", JSON_BUILD_STRING_UNDERSCORIFY(imds_well_known_to_string(wk))), |
| 208 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("key", key), |
| 209 |
SD_JSON_BUILD_PAIR_CONDITION(arg_refresh_usec_set, "refreshUSec", SD_JSON_BUILD_UNSIGNED(arg_refresh_usec)), |
| 210 |
SD_JSON_BUILD_PAIR_CONDITION(arg_cache >= 0, "cache", SD_JSON_BUILD_BOOLEAN(arg_cache))); |
| 211 |
if (r < 0) |
| 212 |
return log_error_errno(r, "Failed to issue io.systemd.InstanceMetadata.Get(): %m"); |
| 213 |
if (error_id) { |
| 214 |
if (STR_IN_SET(error_id, "io.systemd.InstanceMetadata.KeyNotFound", "io.systemd.InstanceMetadata.WellKnownKeyUnset")) { |
| 215 |
*ret = (struct iovec) {}; |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
return log_error_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to issue io.systemd.InstanceMetadata.Get(): %s", error_id); |
| 220 |
} |
| 221 |
|
| 222 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 223 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 224 |
{ "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, 0, SD_JSON_MANDATORY }, |
| 225 |
{}, |
| 226 |
}; |
| 227 |
r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &data); |
| 228 |
if (r < 0) |
| 229 |
return r; |
| 230 |
|
| 231 |
*ret = TAKE_STRUCT(data); |
| 232 |
return 1; |
| 233 |
} |
| 234 |
|
| 235 |
static int acquire_imds_key_as_string( |
| 236 |
sd_varlink *link, |
| 237 |
ImdsWellKnown wk, |
| 238 |
const char *key, |
| 239 |
char **ret) { |
| 240 |
|
| 241 |
int r; |
| 242 |
|
| 243 |
assert(link); |
| 244 |
assert(wk >= 0); |
| 245 |
assert(wk < _IMDS_WELL_KNOWN_MAX); |
| 246 |
assert(ret); |
| 247 |
|
| 248 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 249 |
r = acquire_imds_key(link, wk, key, &data); |
| 250 |
if (r < 0) |
| 251 |
return r; |
| 252 |
if (r == 0) { |
| 253 |
*ret = NULL; |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
_cleanup_free_ char *s = NULL; |
| 258 |
r = make_cstring(data.iov_base, data.iov_len, MAKE_CSTRING_REFUSE_TRAILING_NUL, &s); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
*ret = TAKE_PTR(s); |
| 263 |
return 1; |
| 264 |
} |
| 265 |
|
| 266 |
static int acquire_imds_key_as_ip_address( |
| 267 |
sd_varlink *link, |
| 268 |
ImdsWellKnown wk, |
| 269 |
const char *key, |
| 270 |
int family, |
| 271 |
union in_addr_union *ret) { |
| 272 |
int r; |
| 273 |
|
| 274 |
assert(link); |
| 275 |
assert(wk >= 0); |
| 276 |
assert(wk < _IMDS_WELL_KNOWN_MAX); |
| 277 |
assert(ret); |
| 278 |
|
| 279 |
_cleanup_free_ char *s = NULL; |
| 280 |
r = acquire_imds_key_as_string(link, wk, key, &s); |
| 281 |
if (r < 0) |
| 282 |
return r; |
| 283 |
if (r == 0 || isempty(s)) { |
| 284 |
*ret = (union in_addr_union) {}; |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
r = in_addr_from_string(family, s, ret); |
| 289 |
if (r < 0) |
| 290 |
return r; |
| 291 |
|
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
static int action_summary(sd_varlink *link) { |
| 296 |
int r; |
| 297 |
|
| 298 |
assert(link); |
| 299 |
|
| 300 |
_cleanup_(table_unrefp) Table *table = table_new_vertical(); |
| 301 |
if (!table) |
| 302 |
return log_oom(); |
| 303 |
|
| 304 |
const char *error_id = NULL; |
| 305 |
sd_json_variant *reply = NULL; |
| 306 |
r = sd_varlink_call( |
| 307 |
link, |
| 308 |
"io.systemd.InstanceMetadata.GetVendorInfo", |
| 309 |
/* parameters= */ NULL, |
| 310 |
&reply, |
| 311 |
&error_id); |
| 312 |
if (r < 0) |
| 313 |
return log_error_errno(r, "Failed to issue io.systemd.InstanceMetadata.GetVendorInfo(): %m"); |
| 314 |
if (error_id) |
| 315 |
return log_error_errno(sd_varlink_error_to_errno(error_id, reply), "Failed to issue io.systemd.InstanceMetadata.GetVendorInfo(): %s", error_id); |
| 316 |
|
| 317 |
const char *vendor = NULL; |
| 318 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 319 |
{ "vendor", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, 0, 0 }, |
| 320 |
{} |
| 321 |
}; |
| 322 |
r = sd_json_dispatch(reply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &vendor); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
if (vendor) { |
| 326 |
r = table_add_many(table, |
| 327 |
TABLE_FIELD, "Vendor", |
| 328 |
TABLE_SET_JSON_FIELD_NAME, "vendor", |
| 329 |
TABLE_STRING, vendor); |
| 330 |
if (r < 0) |
| 331 |
return table_log_add_error(r); |
| 332 |
} |
| 333 |
|
| 334 |
static const struct { |
| 335 |
ImdsWellKnown well_known; |
| 336 |
const char *field; |
| 337 |
} wktable[] = { |
| 338 |
{ IMDS_HOSTNAME, "Hostname" }, |
| 339 |
{ IMDS_REGION, "Region" }, |
| 340 |
{ IMDS_ZONE, "Zone" }, |
| 341 |
{ IMDS_IPV4_PUBLIC, "Public IPv4 Address" }, |
| 342 |
{ IMDS_IPV6_PUBLIC, "Public IPv6 Address" }, |
| 343 |
}; |
| 344 |
FOREACH_ELEMENT(i, wktable) { |
| 345 |
_cleanup_free_ char *text = NULL; |
| 346 |
|
| 347 |
r = acquire_imds_key_as_string(link, i->well_known, /* key= */ NULL, &text); |
| 348 |
if (r < 0) |
| 349 |
return r; |
| 350 |
if (r == 0 || isempty(text)) |
| 351 |
continue; |
| 352 |
|
| 353 |
r = table_add_many(table, |
| 354 |
TABLE_FIELD, i->field, |
| 355 |
TABLE_SET_JSON_FIELD_NAME, imds_well_known_to_string(i->well_known), |
| 356 |
TABLE_STRING, text); |
| 357 |
if (r < 0) |
| 358 |
return table_log_add_error(r); |
| 359 |
} |
| 360 |
|
| 361 |
if (table_isempty(table)) |
| 362 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "No well-known IMDS data available."); |
| 363 |
|
| 364 |
return table_print_or_warn(table); |
| 365 |
} |
| 366 |
|
| 367 |
static const char* detect_json_object(const char *text) { |
| 368 |
assert(text); |
| 369 |
|
| 370 |
/* Checks if the provided text looks like a JSON object. It checks if the first non-whitespace |
| 371 |
* characters are {" or {}. */ |
| 372 |
|
| 373 |
text += strspn(text, WHITESPACE); |
| 374 |
if (*text != '{') |
| 375 |
return NULL; |
| 376 |
|
| 377 |
const char *e = text + 1; |
| 378 |
e += strspn(e, WHITESPACE); |
| 379 |
if (!IN_SET(*e, '"', '}')) |
| 380 |
return NULL; |
| 381 |
|
| 382 |
return text; |
| 383 |
} |
| 384 |
|
| 385 |
static int write_credential(const char *dir, const char *name, const struct iovec *data) { |
| 386 |
int r; |
| 387 |
|
| 388 |
assert(dir); |
| 389 |
assert(name); |
| 390 |
|
| 391 |
_cleanup_close_ int dfd = open_mkdir(dir, O_CLOEXEC|O_PATH, 0700); |
| 392 |
if (dfd < 0) |
| 393 |
return log_error_errno(dfd, "Failed to open credential directory '%s': %m", dir); |
| 394 |
|
| 395 |
if (faccessat(dfd, name, F_OK, AT_SYMLINK_NOFOLLOW) < 0) { |
| 396 |
if (errno != ENOENT) |
| 397 |
return log_error_errno(errno, "Failed to check if '%s' exists in credential directory '%s': %m", name, dir); |
| 398 |
} else { |
| 399 |
log_notice("Skipping importing of credential '%s', it already exists locally in '%s'.", name, dir); |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
_cleanup_free_ char *t = NULL; |
| 404 |
_cleanup_close_ int fd = open_tmpfile_linkable_at(dfd, name, O_WRONLY|O_CLOEXEC, &t); |
| 405 |
if (fd < 0) |
| 406 |
return log_error_errno(fd, "Failed to create credential file '%s/%s': %m", dir, name); |
| 407 |
|
| 408 |
CLEANUP_TMPFILE_AT(dfd, t); |
| 409 |
|
| 410 |
r = loop_write(fd, data->iov_base, data->iov_len); |
| 411 |
if (r < 0) |
| 412 |
return log_error_errno(r, "Failed to write credential file '%s/%s': %m", dir, name); |
| 413 |
|
| 414 |
if (fchmod(fd, 0400) < 0) |
| 415 |
return log_error_errno(errno, "Failed to set access mode on credential file '%s/%s': %m", dir, name); |
| 416 |
|
| 417 |
r = link_tmpfile_at(fd, dfd, t, name, /* flags= */ 0); |
| 418 |
if (r < 0) |
| 419 |
return log_error_errno(r, "Failed to move credential file '%s/%s' into place: %m", dir, name); |
| 420 |
|
| 421 |
t = mfree(t); /* Disarm auto-cleanup */ |
| 422 |
return 1; |
| 423 |
} |
| 424 |
|
| 425 |
typedef struct CredentialData { |
| 426 |
const char *name; |
| 427 |
const char *text; |
| 428 |
struct iovec data, encrypted; |
| 429 |
} CredentialData; |
| 430 |
|
| 431 |
static void credential_data_done(CredentialData *d) { |
| 432 |
assert(d); |
| 433 |
|
| 434 |
iovec_done(&d->data); |
| 435 |
iovec_done(&d->encrypted); |
| 436 |
} |
| 437 |
|
| 438 |
static int import_credential_one(CredentialData *d) { |
| 439 |
int r; |
| 440 |
|
| 441 |
assert(d); |
| 442 |
assert(d->name); |
| 443 |
|
| 444 |
log_debug("Importing credential '%s' from IMDS.", d->name); |
| 445 |
|
| 446 |
const char *dir = "/run/credstore"; |
| 447 |
struct iovec *v, _v; |
| 448 |
if (d->text) { |
| 449 |
_v = IOVEC_MAKE_STRING(d->text); |
| 450 |
v = &_v; |
| 451 |
} else if (iovec_is_set(&d->data)) |
| 452 |
v = &d->data; |
| 453 |
else if (iovec_is_set(&d->encrypted)) { |
| 454 |
dir = "/run/credstore.encrypted"; |
| 455 |
v = &d->encrypted; |
| 456 |
} else |
| 457 |
assert_not_reached(); |
| 458 |
|
| 459 |
r = write_credential(dir, d->name, v); |
| 460 |
if (r <= 0) |
| 461 |
return r; |
| 462 |
|
| 463 |
log_info("Imported credential '%s' from IMDS (%s).", d->name, FORMAT_BYTES(v->iov_len)); |
| 464 |
return 1; |
| 465 |
} |
| 466 |
|
| 467 |
static int import_credentials(const char *text) { |
| 468 |
int r; |
| 469 |
|
| 470 |
assert(text); |
| 471 |
|
| 472 |
/* We cannot be sure if the data is actually intended for us. Hence let's be somewhat defensive, and |
| 473 |
* accept data in two ways: either immediately as a JSON object, or alternatively marked with a first |
| 474 |
* line of "#systemd-userdata". The latter mimics the markers cloud-init employs. */ |
| 475 |
|
| 476 |
const char *e = startswith(text, "#systemd-userdata\n"); |
| 477 |
if (!e) { |
| 478 |
e = detect_json_object(text); |
| 479 |
if (!e) { |
| 480 |
log_info("IMDS user data does not look like JSON or systemd userdata, not processing."); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
log_debug("Detected JSON userdata"); |
| 486 |
|
| 487 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 488 |
unsigned line = 0, column = 0; |
| 489 |
r = sd_json_parse(e, /* flags= */ 0, &j, &line, &column); |
| 490 |
if (r < 0) { |
| 491 |
if (line > 0) |
| 492 |
log_syntax(/* unit= */ NULL, LOG_WARNING, /* filename= */ NULL, line, r, "JSON parse failure."); |
| 493 |
else |
| 494 |
log_error_errno(r, "Failed to parse IMDS userdata JSON: %m"); |
| 495 |
return 0; |
| 496 |
} |
| 497 |
|
| 498 |
static const sd_json_dispatch_field top_table[] = { |
| 499 |
{ "systemd.credentials", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_variant_noref, 0, 0 }, |
| 500 |
{}, |
| 501 |
}; |
| 502 |
|
| 503 |
sd_json_variant *creds = NULL; |
| 504 |
r = sd_json_dispatch(j, top_table, SD_JSON_ALLOW_EXTENSIONS|SD_JSON_LOG, &creds); |
| 505 |
if (r < 0) |
| 506 |
return r; |
| 507 |
|
| 508 |
unsigned n_imported = 0; |
| 509 |
int ret = 0; |
| 510 |
if (creds) { |
| 511 |
log_debug("Found 'systemd.credentials' field"); |
| 512 |
|
| 513 |
sd_json_variant *c; |
| 514 |
JSON_VARIANT_ARRAY_FOREACH(c, creds) { |
| 515 |
static const sd_json_dispatch_field credential_table[] = { |
| 516 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CredentialData, name), SD_JSON_MANDATORY }, |
| 517 |
{ "text", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(CredentialData, text), 0 }, |
| 518 |
{ "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(CredentialData, data), 0 }, |
| 519 |
{ "encrypted", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(CredentialData, encrypted), 0 }, |
| 520 |
{}, |
| 521 |
}; |
| 522 |
|
| 523 |
_cleanup_(credential_data_done) CredentialData d = {}; |
| 524 |
r = sd_json_dispatch(c, credential_table, SD_JSON_LOG|SD_JSON_WARNING, &d); |
| 525 |
if (r < 0) { |
| 526 |
RET_GATHER(ret, r); |
| 527 |
continue; |
| 528 |
} |
| 529 |
|
| 530 |
if (!credential_name_valid(d.name)) { |
| 531 |
RET_GATHER(ret, log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "Credential name '%s' is not valid, refusing.", d.name)); |
| 532 |
continue; |
| 533 |
} |
| 534 |
|
| 535 |
if ((!!d.text + !!iovec_is_set(&d.data) + !!iovec_is_set(&d.encrypted)) != 1) { |
| 536 |
RET_GATHER(ret, log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "Exactly one of 'text', 'data', 'encrypted' must be set for credential '%s', refusing.", d.name)); |
| 537 |
continue; |
| 538 |
} |
| 539 |
|
| 540 |
r = import_credential_one(&d); |
| 541 |
if (r < 0) |
| 542 |
RET_GATHER(ret, r); |
| 543 |
else if (r > 0) |
| 544 |
n_imported++; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
log_full(n_imported == 0 ? LOG_DEBUG : LOG_INFO, "Imported %u credentials from IMDS.", n_imported); |
| 549 |
return ret; |
| 550 |
} |
| 551 |
|
| 552 |
static int add_public_address_to_json_array(sd_json_variant **array, int family, const union in_addr_union *addr) { |
| 553 |
int r; |
| 554 |
|
| 555 |
assert(array); |
| 556 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 557 |
assert(addr); |
| 558 |
|
| 559 |
if (in_addr_is_null(family, addr)) |
| 560 |
return 0; |
| 561 |
|
| 562 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 563 |
if (dns_resource_record_new_address(&rr, family, addr, "_public") < 0) |
| 564 |
return log_oom(); |
| 565 |
|
| 566 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *rrj = NULL; |
| 567 |
r = dns_resource_record_to_json(rr, &rrj); |
| 568 |
if (r < 0) |
| 569 |
return log_error_errno(r, "Failed to convert A RR to JSON: %m"); |
| 570 |
|
| 571 |
r = sd_json_variant_append_array(array, rrj); |
| 572 |
if (r < 0) |
| 573 |
return log_error_errno(r, "Failed to append A RR to JSON array: %m"); |
| 574 |
|
| 575 |
log_debug("Writing IMDS RR for: %s", dns_resource_record_to_string(rr)); |
| 576 |
return 1; |
| 577 |
} |
| 578 |
|
| 579 |
static int import_imds_public_addresses(sd_varlink *link) { |
| 580 |
int r, ret = 0; |
| 581 |
|
| 582 |
assert(link); |
| 583 |
|
| 584 |
/* Creates local RRs (honoured by systemd-resolved) for our public addresses. */ |
| 585 |
|
| 586 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL; |
| 587 |
|
| 588 |
union in_addr_union u = {}; |
| 589 |
r = acquire_imds_key_as_ip_address(link, IMDS_IPV4_PUBLIC, /* key= */ NULL, AF_INET, &u); |
| 590 |
if (r < 0) |
| 591 |
RET_GATHER(ret, r); |
| 592 |
else if (r > 0) { |
| 593 |
r = add_public_address_to_json_array(&aj, AF_INET, &u); |
| 594 |
if (r < 0) |
| 595 |
return r; |
| 596 |
} |
| 597 |
|
| 598 |
u = (union in_addr_union) {}; |
| 599 |
r = acquire_imds_key_as_ip_address(link, IMDS_IPV6_PUBLIC, /* key= */ NULL, AF_INET6, &u); |
| 600 |
if (r < 0) |
| 601 |
RET_GATHER(ret, r); |
| 602 |
else if (r > 0) { |
| 603 |
r = add_public_address_to_json_array(&aj, AF_INET6, &u); |
| 604 |
if (r < 0) |
| 605 |
return r; |
| 606 |
} |
| 607 |
|
| 608 |
if (sd_json_variant_elements(aj) == 0) { |
| 609 |
log_debug("No IMDS public addresses known, not writing our RRs."); |
| 610 |
return 0; |
| 611 |
} |
| 612 |
|
| 613 |
_cleanup_free_ char *text = NULL; |
| 614 |
r = sd_json_variant_format(aj, SD_JSON_FORMAT_NEWLINE, &text); |
| 615 |
if (r < 0) |
| 616 |
return log_error_errno(r, "Failed to format JSON text: %m"); |
| 617 |
|
| 618 |
r = write_string_file("/run/systemd/resolve/static.d/imds-public.rr", text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755); |
| 619 |
if (r < 0) |
| 620 |
return log_error_errno(r, "Failed to write IMDS RR data: %m"); |
| 621 |
|
| 622 |
log_debug("IMDS public addresses written out."); |
| 623 |
return 1; |
| 624 |
} |
| 625 |
|
| 626 |
static int import_imds_ssh_key(sd_varlink *link) { |
| 627 |
int r; |
| 628 |
|
| 629 |
assert(link); |
| 630 |
|
| 631 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 632 |
r = acquire_imds_key(link, IMDS_SSH_KEY, /* key= */ NULL, &data); |
| 633 |
if (r < 0) |
| 634 |
return r; |
| 635 |
if (r == 0 || !iovec_is_set(&data)) { |
| 636 |
log_debug("No SSH key supplied via IMDS, not importing."); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
r = write_credential("/run/credstore", "ssh.authorized_keys.root", &data); |
| 641 |
if (r <= 0) |
| 642 |
return r; |
| 643 |
|
| 644 |
log_info("Imported SSH key as credential 'ssh.authorized_keys.root'."); |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
static int import_imds_hostname(sd_varlink *link) { |
| 649 |
int r; |
| 650 |
|
| 651 |
assert(link); |
| 652 |
|
| 653 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 654 |
r = acquire_imds_key(link, IMDS_HOSTNAME, /* key= */ NULL, &data); |
| 655 |
if (r < 0) |
| 656 |
return r; |
| 657 |
if (r == 0 || !iovec_is_set(&data)) { |
| 658 |
log_debug("No hostname supplied via IMDS, not importing."); |
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
r = write_credential("/run/credstore", "firstboot.hostname", &data); |
| 663 |
if (r <= 0) |
| 664 |
return r; |
| 665 |
|
| 666 |
log_info("Imported hostname as credential 'firstboot.hostname'."); |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
static int acquire_imds_userdata(sd_varlink *link, struct iovec *ret) { |
| 671 |
int r; |
| 672 |
|
| 673 |
assert(link); |
| 674 |
assert(ret); |
| 675 |
|
| 676 |
/* First try our private namespace, if the concept exists, and then fall back to the singleton */ |
| 677 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 678 |
r = acquire_imds_key(link, IMDS_USERDATA_BASE, "/systemd-userdata", &data); |
| 679 |
if (r == 0) |
| 680 |
r = acquire_imds_key(link, IMDS_USERDATA, /* key= */ NULL, &data); |
| 681 |
if (r < 0) |
| 682 |
return r; |
| 683 |
if (r > 0) { |
| 684 |
if (!iovec_is_set(&data)) { /* Treat empty user data like empty */ |
| 685 |
*ret = (struct iovec) {}; |
| 686 |
return 0; |
| 687 |
} |
| 688 |
|
| 689 |
*ret = TAKE_STRUCT(data); |
| 690 |
return 1; |
| 691 |
} |
| 692 |
|
| 693 |
r = acquire_imds_key(link, IMDS_USERDATA_BASE64, /* key= */ NULL, &data); |
| 694 |
if (r < 0) |
| 695 |
return r; |
| 696 |
_cleanup_(iovec_done) struct iovec decoded = {}; |
| 697 |
if (r > 0) { |
| 698 |
r = unbase64mem_full(data.iov_base, data.iov_len, /* secure= */ false, &decoded.iov_base, &decoded.iov_len); |
| 699 |
if (r < 0) |
| 700 |
return r; |
| 701 |
} |
| 702 |
|
| 703 |
if (!iovec_is_set(&decoded)) { /* Treat empty user data like empty */ |
| 704 |
*ret = (struct iovec) {}; |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|
| 708 |
*ret = TAKE_STRUCT(decoded); |
| 709 |
return 1; |
| 710 |
} |
| 711 |
|
| 712 |
static int action_get(sd_varlink *link) { |
| 713 |
int r; |
| 714 |
|
| 715 |
assert(link); |
| 716 |
|
| 717 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 718 |
r = acquire_imds_key(link, arg_well_known, arg_key, &data); |
| 719 |
if (r < 0) |
| 720 |
return r; |
| 721 |
if (r == 0) |
| 722 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Key not available."); |
| 723 |
|
| 724 |
r = loop_write(STDOUT_FILENO, data.iov_base, data.iov_len); |
| 725 |
if (r < 0) |
| 726 |
return log_error_errno(r, "Failed to write data to standard output: %m"); |
| 727 |
|
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
static int action_userdata(sd_varlink *link) { |
| 732 |
int r; |
| 733 |
|
| 734 |
assert(link); |
| 735 |
|
| 736 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 737 |
r = acquire_imds_userdata(link, &data); |
| 738 |
if (r < 0) |
| 739 |
return r; |
| 740 |
if (r == 0) |
| 741 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "User data not available."); |
| 742 |
|
| 743 |
r = loop_write(STDOUT_FILENO, data.iov_base, data.iov_len); |
| 744 |
if (r < 0) |
| 745 |
return log_error_errno(r, "Failed to write data to standard output: %m"); |
| 746 |
|
| 747 |
return 0; |
| 748 |
} |
| 749 |
|
| 750 |
static int remove_userdata(const char *path) { |
| 751 |
assert(path); |
| 752 |
|
| 753 |
if (unlink(path) < 0) { |
| 754 |
|
| 755 |
if (errno != ENOENT) |
| 756 |
log_debug_errno(errno, "Failed to remove '%s', ignoring: %m", path); |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
log_debug("Removed '%s'.", path); |
| 762 |
return 1; |
| 763 |
} |
| 764 |
|
| 765 |
static int save_userdata(const struct iovec *data, const char *path) { |
| 766 |
int r; |
| 767 |
|
| 768 |
assert(data); |
| 769 |
assert(path); |
| 770 |
|
| 771 |
if (!iovec_is_set(data)) |
| 772 |
return remove_userdata(path); |
| 773 |
|
| 774 |
r = write_data_file_atomic_at(AT_FDCWD, path, data, WRITE_DATA_FILE_MKDIR_0755); |
| 775 |
if (r < 0) |
| 776 |
return log_error_errno(r, "Failed to save userdata to '%s': %m", path); |
| 777 |
|
| 778 |
log_debug("Saved userdata to '%s'.", path); |
| 779 |
return 1; |
| 780 |
} |
| 781 |
|
| 782 |
static int action_import(sd_varlink *link) { |
| 783 |
int r; |
| 784 |
|
| 785 |
assert(link); |
| 786 |
|
| 787 |
int ret = 0; |
| 788 |
RET_GATHER(ret, import_imds_public_addresses(link)); |
| 789 |
RET_GATHER(ret, import_imds_hostname(link)); |
| 790 |
RET_GATHER(ret, import_imds_ssh_key(link)); |
| 791 |
|
| 792 |
_cleanup_(iovec_done) struct iovec data = {}; |
| 793 |
r = acquire_imds_userdata(link, &data); |
| 794 |
if (r < 0) |
| 795 |
return RET_GATHER(ret, r); |
| 796 |
if (r == 0) { |
| 797 |
log_info("No IMDS data available, not importing credentials."); |
| 798 |
(void) remove_userdata("/run/systemd/imds/userdata"); |
| 799 |
return ret; |
| 800 |
} |
| 801 |
|
| 802 |
/* Measure the userdata before we use it */ |
| 803 |
(void) pcrextend_imds_userdata_now(&data); |
| 804 |
|
| 805 |
/* Keep a pristine copy of the userdata we actually applied. (Note that this data is typically also |
| 806 |
* kept as cached item on systemd-imdsd, but that one is possibly subject to cache invalidation, |
| 807 |
* while this one is supposed to pin the data actually in effect.) */ |
| 808 |
(void) save_userdata(&data, "/run/systemd/imds/userdata"); |
| 809 |
|
| 810 |
/* Ensure no inner NUL byte */ |
| 811 |
if (memchr(data.iov_base, 0, data.iov_len)) { |
| 812 |
log_info("IMDS user data contains NUL byte, not processing."); |
| 813 |
return ret; |
| 814 |
} |
| 815 |
|
| 816 |
/* Turn this into a proper C string */ |
| 817 |
if (!iovec_append(&data, &IOVEC_MAKE_BYTE(0))) |
| 818 |
return log_oom(); |
| 819 |
|
| 820 |
return RET_GATHER(ret, import_credentials(data.iov_base)); |
| 821 |
} |
| 822 |
|
| 823 |
static int run(int argc, char* argv[]) { |
| 824 |
int r; |
| 825 |
|
| 826 |
log_setup(); |
| 827 |
|
| 828 |
r = parse_argv(argc, argv); |
| 829 |
if (r <= 0) |
| 830 |
return r; |
| 831 |
|
| 832 |
_cleanup_(sd_varlink_unrefp) sd_varlink *link = NULL; |
| 833 |
r = sd_varlink_connect_address(&link, "/run/systemd/io.systemd.InstanceMetadata"); |
| 834 |
if (r < 0) { |
| 835 |
if (r != -ENOENT && !ERRNO_IS_NEG_DISCONNECT(r)) |
| 836 |
return log_error_errno(r, "Failed to connect to systemd-imdsd: %m"); |
| 837 |
|
| 838 |
log_debug_errno(r, "Couldn't connect to /run/systemd/io.systemd.InstanceMetadata, will try to fork off systemd-imdsd as child now."); |
| 839 |
|
| 840 |
/* Try to fork off systemd-imdsd as a child as a fallback. If we have privileges and the |
| 841 |
* SO_FWMARK trickery is not necessary, then this might just work. */ |
| 842 |
_cleanup_free_ char *p = NULL; |
| 843 |
_cleanup_close_ int pin_fd = |
| 844 |
pin_callout_binary(LIBEXECDIR "/systemd-imdsd", &p); |
| 845 |
if (pin_fd < 0) |
| 846 |
return log_error_errno(pin_fd, "Failed to pick up imdsd binary: %m"); |
| 847 |
|
| 848 |
r = sd_varlink_connect_exec(&link, p, /* argv[]= */ NULL); |
| 849 |
if (r < 0) |
| 850 |
return log_error_errno(r, "Failed to connect to imdsd service: %m"); |
| 851 |
} |
| 852 |
|
| 853 |
switch (arg_action) { |
| 854 |
|
| 855 |
case ACTION_SUMMARY: |
| 856 |
return action_summary(link); |
| 857 |
|
| 858 |
case ACTION_GET: |
| 859 |
return action_get(link); |
| 860 |
|
| 861 |
case ACTION_USERDATA: |
| 862 |
return action_userdata(link); |
| 863 |
|
| 864 |
case ACTION_IMPORT: |
| 865 |
return action_import(link); |
| 866 |
|
| 867 |
default: |
| 868 |
assert_not_reached(); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |