| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-hwdb.h" |
| 4 |
|
| 5 |
#include "dropin.h" |
| 6 |
#include "fileio.h" |
| 7 |
#include "generator.h" |
| 8 |
#include "imds-util.h" |
| 9 |
#include "initrd-util.h" |
| 10 |
#include "log.h" |
| 11 |
#include "parse-util.h" |
| 12 |
#include "proc-cmdline.h" |
| 13 |
#include "special.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "virt.h" |
| 16 |
|
| 17 |
static int arg_enabled = -1; /* Whether we shall offer local IMDS APIs */ |
| 18 |
static int arg_import = -1; /* Whether we shall import IMDS credentials, SSH keys, … into the local system */ |
| 19 |
static ImdsNetworkMode arg_network_mode = IMDS_NETWORK_DEFAULT; |
| 20 |
|
| 21 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 22 |
int r; |
| 23 |
|
| 24 |
assert(key); |
| 25 |
|
| 26 |
if (proc_cmdline_key_streq(key, "systemd.imds")) { |
| 27 |
if (proc_cmdline_value_missing(key, value)) |
| 28 |
return 0; |
| 29 |
|
| 30 |
r = parse_tristate_full(value, "auto", &arg_enabled); |
| 31 |
if (r < 0) |
| 32 |
return log_warning_errno(r, "Failed to parse systemd.imds= value: %m"); |
| 33 |
|
| 34 |
} else if (proc_cmdline_key_streq(key, "systemd.imds.import")) { |
| 35 |
if (proc_cmdline_value_missing(key, value)) |
| 36 |
return 0; |
| 37 |
|
| 38 |
r = parse_boolean(value); |
| 39 |
if (r < 0) |
| 40 |
return log_warning_errno(r, "Failed to parse systemd.imds.import= value: %m"); |
| 41 |
|
| 42 |
arg_import = r; |
| 43 |
} else if (proc_cmdline_key_streq(key, "systemd.imds.network")) { |
| 44 |
if (proc_cmdline_value_missing(key, value)) |
| 45 |
return 0; |
| 46 |
|
| 47 |
ImdsNetworkMode m = imds_network_mode_from_string(value); |
| 48 |
if (m < 0) |
| 49 |
return log_warning_errno(m, "Failed to parse systemd.imds.network= value: %m"); |
| 50 |
|
| 51 |
arg_network_mode = m; |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static int smbios_get_modalias(char **ret) { |
| 58 |
int r; |
| 59 |
|
| 60 |
assert(ret); |
| 61 |
|
| 62 |
_cleanup_free_ char *modalias = NULL; |
| 63 |
r = read_virtual_file("/sys/devices/virtual/dmi/id/modalias", SIZE_MAX, &modalias, /* ret_size= */ NULL); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
|
| 67 |
truncate_nl(modalias); |
| 68 |
|
| 69 |
/* To detect Azure we need to check the chassis asset tag. Unfortunately the kernel does not include |
| 70 |
* it in the modalias string right now. Let's hence append it manually. This matches similar logic in |
| 71 |
* rules.d/60-dmi-id.rules. */ |
| 72 |
_cleanup_free_ char *cat = NULL; |
| 73 |
r = read_virtual_file("/sys/devices/virtual/dmi/id/chassis_asset_tag", SIZE_MAX, &cat, /* ret_size= */ NULL); |
| 74 |
if (r < 0) |
| 75 |
log_debug_errno(r, "Failed to read chassis asset tag, ignoring: %m"); |
| 76 |
else { |
| 77 |
truncate_nl(cat); |
| 78 |
|
| 79 |
if (!string_has_cc(cat, /* ok= */ NULL) && !isempty(cat) && !strextend(&modalias, "cat", cat, ":")) |
| 80 |
return -ENOMEM; |
| 81 |
} |
| 82 |
|
| 83 |
log_debug("Constructed SMBIOS modalias string: %s", modalias); |
| 84 |
*ret = TAKE_PTR(modalias); |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
static int smbios_query(void) { |
| 89 |
int r; |
| 90 |
|
| 91 |
/* Let's check whether the DMI device's hwdb data suggests IMDS support is available. Note, we cannot |
| 92 |
* ask udev for this, as we typically run long before udev. Hence we'll do the hwdb lookup via |
| 93 |
* sd-hwdb directly. */ |
| 94 |
|
| 95 |
_cleanup_free_ char *modalias = NULL; |
| 96 |
r = smbios_get_modalias(&modalias); |
| 97 |
if (r == -ENOENT) { |
| 98 |
log_debug("No DMI device found, assuming IMDS is not available."); |
| 99 |
return false; |
| 100 |
} |
| 101 |
if (r < 0) |
| 102 |
return log_error_errno(r, "Failed to read DMI modalias: %m"); |
| 103 |
|
| 104 |
_cleanup_(sd_hwdb_unrefp) sd_hwdb *hwdb = NULL; |
| 105 |
r = sd_hwdb_new(&hwdb); |
| 106 |
if (r < 0) |
| 107 |
return log_error_errno(r, "Failed to open hwdb: %m"); |
| 108 |
|
| 109 |
r = sd_hwdb_seek(hwdb, modalias); |
| 110 |
if (r < 0) |
| 111 |
return log_error_errno(r, "Failed to seek in hwdb for '%s': %m", modalias); |
| 112 |
|
| 113 |
for (;;) { |
| 114 |
const char *key, *value; |
| 115 |
r = sd_hwdb_enumerate(hwdb, &key, &value); |
| 116 |
if (r < 0) |
| 117 |
return log_error_errno(r, "Failed to enumerate hwdb entry for '%s': %m", modalias); |
| 118 |
if (r == 0) |
| 119 |
break; |
| 120 |
|
| 121 |
if (streq(key, "IMDS_VENDOR")) |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
log_debug("IMDS_VENDOR= property for DMI device not set, assuming IMDS is not available."); |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 130 |
int r; |
| 131 |
|
| 132 |
r = proc_cmdline_parse(parse_proc_cmdline_item, /* userdata= */ NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
| 133 |
if (r < 0) |
| 134 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 135 |
|
| 136 |
if (arg_enabled < 0) { |
| 137 |
Virtualization v = detect_container(); |
| 138 |
if (v < 0) |
| 139 |
log_debug_errno(v, "Container detection failed, ignoring: %m"); |
| 140 |
if (v > 0) { |
| 141 |
log_debug("Running in a container, disabling IMDS logic."); |
| 142 |
arg_enabled = false; |
| 143 |
} else { |
| 144 |
r = smbios_query(); |
| 145 |
if (r < 0) |
| 146 |
return r; |
| 147 |
arg_enabled = r > 0; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (!arg_enabled) { |
| 152 |
log_debug("IMDS not enabled, skipping generator."); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
log_info("IMDS support enabled, pulling in IMDS units."); |
| 157 |
|
| 158 |
/* Enable IMDS early networking, so that we can actually reach the IMDS server. */ |
| 159 |
if (arg_network_mode != IMDS_NETWORK_OFF) { |
| 160 |
r = generator_add_symlink(dest_early, SPECIAL_SYSINIT_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/systemd-imds-early-network.service"); |
| 161 |
if (r < 0) |
| 162 |
return log_error_errno(r, "Failed to hook in systemd-imds-early-network.service: %m"); |
| 163 |
} |
| 164 |
|
| 165 |
/* Enable the IMDS service socket */ |
| 166 |
r = generator_add_symlink(dest_early, SPECIAL_SOCKETS_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/systemd-imdsd.socket"); |
| 167 |
if (r < 0) |
| 168 |
return log_error_errno(r, "Failed to hook in systemd-imdsd.socket: %m"); |
| 169 |
|
| 170 |
/* We now know the SMBIOS device exists, hence it's safe now to order the IMDS service after it, so |
| 171 |
* that it has all properties properly initialized. */ |
| 172 |
r = write_drop_in( |
| 173 |
dest_early, |
| 174 |
"[email protected]", |
| 175 |
50, "dmi-id", |
| 176 |
"# Automatically generated by systemd-imds-generator\n\n" |
| 177 |
"[Unit]\n" |
| 178 |
"Wants=sys-devices-virtual-dmi-id.device\n" |
| 179 |
"After=sys-devices-virtual-dmi-id.device\n"); |
| 180 |
if (r < 0) |
| 181 |
return log_error_errno(r, "Failed to hook DMI id device before [email protected]: %m"); |
| 182 |
|
| 183 |
if (arg_import < 0) |
| 184 |
arg_import = in_initrd(); |
| 185 |
if (arg_import > 0) { |
| 186 |
/* Enable the import of IMDS data */ |
| 187 |
r = generator_add_symlink(dest_early, SPECIAL_SYSINIT_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/systemd-imds-import.service"); |
| 188 |
if (r < 0) |
| 189 |
return log_error_errno(r, "Failed to hook in systemd-imds-import.service: %m"); |
| 190 |
} |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
DEFINE_MAIN_GENERATOR_FUNCTION(run); |