| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <net/if.h> |
| 5 |
#include <sys/xattr.h> |
| 6 |
#include <unistd.h> |
| 7 |
|
| 8 |
#include "sd-bus.h" |
| 9 |
#include "sd-device.h" |
| 10 |
#include "sd-event.h" |
| 11 |
#include "sd-json.h" |
| 12 |
#include "sd-netlink.h" |
| 13 |
|
| 14 |
#include "alloc-util.h" |
| 15 |
#include "build-path.h" |
| 16 |
#include "build.h" |
| 17 |
#include "bus-polkit.h" |
| 18 |
#include "chase.h" |
| 19 |
#include "copy.h" |
| 20 |
#include "creds-util.h" |
| 21 |
#include "curl-util.h" |
| 22 |
#include "device-private.h" |
| 23 |
#include "dns-rr.h" |
| 24 |
#include "errno-util.h" |
| 25 |
#include "escape.h" |
| 26 |
#include "event-util.h" |
| 27 |
#include "exit-status.h" |
| 28 |
#include "fd-util.h" |
| 29 |
#include "format-ifname.h" |
| 30 |
#include "format-table.h" |
| 31 |
#include "hash-funcs.h" |
| 32 |
#include "hashmap.h" |
| 33 |
#include "imds-util.h" |
| 34 |
#include "in-addr-util.h" |
| 35 |
#include "io-util.h" |
| 36 |
#include "iovec-util.h" |
| 37 |
#include "json-util.h" |
| 38 |
#include "log.h" |
| 39 |
#include "main-func.h" |
| 40 |
#include "netlink-util.h" |
| 41 |
#include "options.h" |
| 42 |
#include "parse-argument.h" |
| 43 |
#include "parse-util.h" |
| 44 |
#include "path-util.h" |
| 45 |
#include "pretty-print.h" |
| 46 |
#include "proc-cmdline.h" |
| 47 |
#include "socket-util.h" |
| 48 |
#include "string-util.h" |
| 49 |
#include "strv.h" |
| 50 |
#include "time-util.h" |
| 51 |
#include "tmpfile-util.h" |
| 52 |
#include "utf8.h" |
| 53 |
#include "varlink-io.systemd.InstanceMetadata.h" |
| 54 |
#include "varlink-util.h" |
| 55 |
#include "web-util.h" |
| 56 |
#include "xattr-util.h" |
| 57 |
|
| 58 |
/* This implements a client to the AWS' and Azure's "Instance Metadata Service", as well as GCP's "VM |
| 59 |
* Metadata", i.e.: |
| 60 |
* |
| 61 |
* https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html |
| 62 |
* https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service |
| 63 |
* https://docs.cloud.google.com/compute/docs/metadata/overview |
| 64 |
* https://docs.hetzner.cloud/reference/cloud#description/server-metadata |
| 65 |
* |
| 66 |
* Some notes: |
| 67 |
* - IMDS service are heavily rate-limited, and hence we want to centralize requests in one place and cache |
| 68 |
* - In order to isolate IMDS access this expects that traffic to the IMDS address 169.254.169.254 is |
| 69 |
* generally prohibited (via a prohibit route), but our service uses fwmark 0x7FFF0815, which (via source |
| 70 |
* routing) can bypass this route. |
| 71 |
* - To be robust to situations with multiple interfaces, if we have no hint which interface we shall use, |
| 72 |
* we'll fork our own binary off, once for each interface, and communicate to it via Varlink. |
| 73 |
* - This is supposed to run under its own UID, but with CAP_NET_ADMIN held (since we want to use |
| 74 |
* IP_UNICAST_IF + SO_MARK) |
| 75 |
* - This daemon either be invoked manually from the command line, to do a single request, mostly for |
| 76 |
* debugging purposes. Or it can be invoked as a Varlink service, which is the primary intended mode of |
| 77 |
* operation. |
| 78 |
*/ |
| 79 |
|
| 80 |
#define TOKEN_SIZE_MAX (4096U) |
| 81 |
#define DATA_SIZE_MAX (4*1024*1024U) |
| 82 |
#define FWMARK_DEFAULT UINT32_C(0x7FFF0815) |
| 83 |
#define REFRESH_USEC_DEFAULT (15U * USEC_PER_MINUTE) |
| 84 |
#define REFRESH_USEC_MIN (1U * USEC_PER_SEC) |
| 85 |
#define DIRECT_OVERALL_TIMEOUT_USEC (40U * USEC_PER_SEC) /* a bit shorter than the default D-Bus/Varlink method call time-out) */ |
| 86 |
#define INDIRECT_OVERALL_TIMEOUT_USEC (DIRECT_OVERALL_TIMEOUT_USEC + 5U * USEC_PER_SEC) |
| 87 |
#define RETRY_MIN_USEC (20U * USEC_PER_MSEC) |
| 88 |
#define RETRY_MAX_USEC (3U * USEC_PER_SEC) |
| 89 |
#define RETRY_MAX 10U |
| 90 |
|
| 91 |
/* Which endpoint configuration source has been used, in order of preference */ |
| 92 |
typedef enum EndpointSource { |
| 93 |
ENDPOINT_USER, /* Explicit command line options */ |
| 94 |
ENDPOINT_ENVIRONMENT, /* Fallback environment variables */ |
| 95 |
ENDPOINT_PROC_CMDLINE, /* Acquired via kernel command line */ |
| 96 |
ENDPOINT_CREDENTIALS, /* Acquired via system credentials */ |
| 97 |
ENDPOINT_UDEV, /* Acquired via udev SMBIOS object */ |
| 98 |
_ENDPOINT_SOURCE_MAX, |
| 99 |
_ENDPOINT_SOURCE_INVALID = -EINVAL, |
| 100 |
} EndpointSource; |
| 101 |
|
| 102 |
static char *arg_ifname = NULL; |
| 103 |
static usec_t arg_refresh_usec = REFRESH_USEC_DEFAULT; |
| 104 |
static uint32_t arg_fwmark = FWMARK_DEFAULT; |
| 105 |
static bool arg_fwmark_set = true; |
| 106 |
static ImdsWellKnown arg_well_known = _IMDS_WELL_KNOWN_INVALID; |
| 107 |
static char* arg_key = NULL; |
| 108 |
static bool arg_cache = true; |
| 109 |
static bool arg_wait = false; |
| 110 |
static bool arg_varlink = false; |
| 111 |
static ImdsNetworkMode arg_network_mode = _IMDS_NETWORK_MODE_INVALID; |
| 112 |
static bool arg_setup_network = false; |
| 113 |
|
| 114 |
/* The follow configure the IMDS service endpoint details */ |
| 115 |
static EndpointSource arg_endpoint_source = _ENDPOINT_SOURCE_INVALID; |
| 116 |
static char *arg_vendor = NULL; |
| 117 |
static char *arg_token_url = NULL; |
| 118 |
static char *arg_refresh_header_name = NULL; |
| 119 |
static char *arg_data_url = NULL; |
| 120 |
static char *arg_data_url_suffix = NULL; |
| 121 |
static char *arg_token_header_name = NULL; |
| 122 |
static char **arg_extra_header = NULL; |
| 123 |
static struct in_addr arg_address_ipv4 = {}; |
| 124 |
static struct in6_addr arg_address_ipv6 = {}; |
| 125 |
static char *arg_well_known_key[_IMDS_WELL_KNOWN_MAX] = {}; |
| 126 |
|
| 127 |
static void imds_well_known_key_free(typeof(arg_well_known_key) *array) { |
| 128 |
FOREACH_ARRAY(i, *array, _IMDS_WELL_KNOWN_MAX) |
| 129 |
free(*i); |
| 130 |
} |
| 131 |
|
| 132 |
STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep); |
| 133 |
STATIC_DESTRUCTOR_REGISTER(arg_key, freep); |
| 134 |
STATIC_DESTRUCTOR_REGISTER(arg_vendor, freep); |
| 135 |
STATIC_DESTRUCTOR_REGISTER(arg_token_url, freep); |
| 136 |
STATIC_DESTRUCTOR_REGISTER(arg_refresh_header_name, freep); |
| 137 |
STATIC_DESTRUCTOR_REGISTER(arg_data_url, freep); |
| 138 |
STATIC_DESTRUCTOR_REGISTER(arg_data_url_suffix, freep); |
| 139 |
STATIC_DESTRUCTOR_REGISTER(arg_token_header_name, freep); |
| 140 |
STATIC_DESTRUCTOR_REGISTER(arg_extra_header, strv_freep); |
| 141 |
STATIC_DESTRUCTOR_REGISTER(arg_well_known_key, imds_well_known_key_free); |
| 142 |
|
| 143 |
typedef struct Context Context; |
| 144 |
|
| 145 |
typedef struct ChildData { |
| 146 |
/* If there are multiple network interfaces, and we are not sure where to look for things, we'll fork |
| 147 |
* additional instances of ourselves, one for each interface. */ |
| 148 |
Context *context; |
| 149 |
int ifindex; |
| 150 |
sd_varlink *link; /* outgoing varlink connection towards the child */ |
| 151 |
bool retry; /* If true then new information came to light and we should restart the request */ |
| 152 |
} ChildData; |
| 153 |
|
| 154 |
struct Context { |
| 155 |
/* Fields shared between requests (these remain allocated between Varlink requests) */ |
| 156 |
sd_event *event; |
| 157 |
sd_netlink *rtnl; |
| 158 |
bool rtnl_attached; |
| 159 |
sd_bus *system_bus; /* for polkit */ |
| 160 |
CurlGlue *glue; |
| 161 |
struct iovec token; /* token in binary */ |
| 162 |
char *token_string; /* token as string, once complete and validated */ |
| 163 |
int cache_dir_fd; |
| 164 |
Hashmap *polkit_registry; |
| 165 |
|
| 166 |
/* Request-specific fields (these get reset whenever we start processing a new Varlink call) */ |
| 167 |
int ifindex; |
| 168 |
usec_t timestamp; /* CLOCK_BOOTTIME */ |
| 169 |
int cache_fd; |
| 170 |
char *cache_filename, *cache_temporary_filename; |
| 171 |
uint64_t data_size; |
| 172 |
usec_t refresh_usec; |
| 173 |
char *key; |
| 174 |
ImdsWellKnown well_known; |
| 175 |
bool write_stdout; |
| 176 |
struct iovec write_iovec; |
| 177 |
bool cache; |
| 178 |
bool wait; |
| 179 |
sd_varlink *current_link; /* incoming varlink connection we are processing */ |
| 180 |
uint32_t fwmark; |
| 181 |
bool fwmark_set; |
| 182 |
sd_event_source *overall_timeout_source; |
| 183 |
|
| 184 |
/* Mode 1 "direct": we go directly to the network (this is done if we know the interface index to |
| 185 |
* use) */ |
| 186 |
CurlSlot *slot_token; |
| 187 |
CurlSlot *slot_data; |
| 188 |
struct curl_slist *request_header_token, *request_header_data; |
| 189 |
sd_event_source *retry_source; |
| 190 |
unsigned n_retry; |
| 191 |
usec_t retry_interval_usec; |
| 192 |
|
| 193 |
/* Mode 2 "indirect": we fork off a number of children which go to the network on behalf of us, |
| 194 |
* because we have multiple network interfaces to deal with. */ |
| 195 |
Hashmap *child_data; |
| 196 |
sd_netlink_slot *address_change_slot; |
| 197 |
}; |
| 198 |
|
| 199 |
#define CONTEXT_NULL \ |
| 200 |
(Context) { \ |
| 201 |
.cache_dir_fd = -EBADF, \ |
| 202 |
.cache_fd = -EBADF, \ |
| 203 |
.well_known = _IMDS_WELL_KNOWN_INVALID, \ |
| 204 |
} |
| 205 |
|
| 206 |
/* Log helpers that cap at debug logging if we are operating on behalf of a Varlink client */ |
| 207 |
#define context_log_errno(c, level, r, fmt, ...) \ |
| 208 |
log_full_errno((c)->current_link ? LOG_DEBUG : (level), r, fmt, ##__VA_ARGS__) |
| 209 |
#define context_log(c, level, fmt, ...) \ |
| 210 |
log_full((c)->current_link ? LOG_DEBUG : (level), fmt, ##__VA_ARGS__) |
| 211 |
#define context_log_oom(c) \ |
| 212 |
(c)->current_link ? log_oom_debug() : log_oom() |
| 213 |
|
| 214 |
static int context_acquire_data(Context *c); |
| 215 |
static int context_acquire_token(Context *c); |
| 216 |
static int context_spawn_child(Context *c, int ifindex, sd_varlink **ret); |
| 217 |
|
| 218 |
static ChildData* child_data_free(ChildData *cd) { |
| 219 |
if (!cd) |
| 220 |
return NULL; |
| 221 |
|
| 222 |
if (cd->context) |
| 223 |
hashmap_remove(cd->context->child_data, INT_TO_PTR(cd->ifindex)); |
| 224 |
|
| 225 |
sd_varlink_close_unref(cd->link); |
| 226 |
return mfree(cd); |
| 227 |
} |
| 228 |
|
| 229 |
DEFINE_TRIVIAL_CLEANUP_FUNC(ChildData*, child_data_free); |
| 230 |
|
| 231 |
static void context_reset_token(Context *c) { |
| 232 |
assert(c); |
| 233 |
|
| 234 |
iovec_done(&c->token); |
| 235 |
c->token_string = mfree(c->token_string); |
| 236 |
} |
| 237 |
|
| 238 |
static void context_flush_token(Context *c) { |
| 239 |
|
| 240 |
if (c->cache_dir_fd >= 0) |
| 241 |
(void) unlinkat(c->cache_dir_fd, "token", /* flags= */ 0); |
| 242 |
|
| 243 |
context_reset_token(c); |
| 244 |
} |
| 245 |
|
| 246 |
static void context_reset_for_refresh(Context *c) { |
| 247 |
assert(c); |
| 248 |
|
| 249 |
/* Flush out all fields, up to the point we can restart the current request */ |
| 250 |
|
| 251 |
c->slot_token = curl_slot_unref(c->slot_token); |
| 252 |
c->slot_data = curl_slot_unref(c->slot_data); |
| 253 |
|
| 254 |
sym_curl_slist_free_all(c->request_header_token); |
| 255 |
c->request_header_token = NULL; |
| 256 |
sym_curl_slist_free_all(c->request_header_data); |
| 257 |
c->request_header_data = NULL; |
| 258 |
|
| 259 |
c->cache_fd = safe_close(c->cache_fd); |
| 260 |
c->cache_filename = mfree(c->cache_filename); |
| 261 |
|
| 262 |
if (c->cache_temporary_filename && c->cache_dir_fd >= 0) |
| 263 |
(void) unlinkat(c->cache_dir_fd, c->cache_temporary_filename, /* flags= */ 0); |
| 264 |
|
| 265 |
c->cache_temporary_filename = mfree(c->cache_temporary_filename); |
| 266 |
|
| 267 |
iovec_done(&c->write_iovec); |
| 268 |
|
| 269 |
c->child_data = hashmap_free(c->child_data); |
| 270 |
c->data_size = 0; |
| 271 |
|
| 272 |
(void) sd_event_source_set_enabled(c->retry_source, SD_EVENT_OFF); |
| 273 |
} |
| 274 |
|
| 275 |
static void context_reset_full(Context *c) { |
| 276 |
assert(c); |
| 277 |
|
| 278 |
/* Flush out all fields relevant to the current request, comprehensively */ |
| 279 |
|
| 280 |
context_reset_for_refresh(c); |
| 281 |
c->key = mfree(c->key); |
| 282 |
c->well_known = _IMDS_WELL_KNOWN_INVALID; |
| 283 |
c->current_link = sd_varlink_unref(c->current_link); |
| 284 |
c->address_change_slot = sd_netlink_slot_unref(c->address_change_slot); |
| 285 |
c->retry_source = sd_event_source_unref(c->retry_source); |
| 286 |
c->overall_timeout_source = sd_event_source_unref(c->overall_timeout_source); |
| 287 |
c->cache_dir_fd = safe_close(c->cache_dir_fd); |
| 288 |
} |
| 289 |
|
| 290 |
static void context_new_request(Context *c) { |
| 291 |
assert(c); |
| 292 |
|
| 293 |
/* Flush everything out from the previous request */ |
| 294 |
context_reset_full(c); |
| 295 |
|
| 296 |
/* Reinitialize settings from defaults. */ |
| 297 |
c->ifindex = 0; |
| 298 |
c->timestamp = now(CLOCK_BOOTTIME); |
| 299 |
c->refresh_usec = arg_refresh_usec; |
| 300 |
c->cache = arg_cache; |
| 301 |
c->wait = arg_wait; |
| 302 |
c->fwmark = arg_fwmark; |
| 303 |
c->fwmark_set = arg_fwmark_set; |
| 304 |
c->n_retry = 0; |
| 305 |
} |
| 306 |
|
| 307 |
static void context_done(Context *c) { |
| 308 |
assert(c); |
| 309 |
|
| 310 |
/* Flush out everything specific to the current request first */ |
| 311 |
context_reset_full(c); |
| 312 |
context_reset_token(c); |
| 313 |
|
| 314 |
/* And then also flush out everything shared between requests */ |
| 315 |
c->glue = curl_glue_unref(c->glue); |
| 316 |
c->rtnl = sd_netlink_unref(c->rtnl); |
| 317 |
c->event = sd_event_unref(c->event); |
| 318 |
c->polkit_registry = hashmap_free(c->polkit_registry); |
| 319 |
c->system_bus = sd_bus_flush_close_unref(c->system_bus); |
| 320 |
} |
| 321 |
|
| 322 |
static int context_fail_full(Context *c, int r, const char *varlink_error) { |
| 323 |
assert(c); |
| 324 |
assert(r != 0); |
| 325 |
|
| 326 |
/* Called whenever the current retrieval fails asynchronously. Returns 0 so callers in |
| 327 |
* int-returning paths can `return context_fail_full(...)` directly. */ |
| 328 |
|
| 329 |
r = -abs(r); |
| 330 |
|
| 331 |
if (varlink_error) |
| 332 |
context_log_errno(c, LOG_ERR, r, "Operation failed (%s).", varlink_error); |
| 333 |
else |
| 334 |
context_log_errno(c, LOG_ERR, r, "Operation failed (%m)."); |
| 335 |
|
| 336 |
/* If we are running in Varlink mode, return the error on the connection */ |
| 337 |
if (c->current_link) { |
| 338 |
if (varlink_error) |
| 339 |
(void) sd_varlink_error(c->current_link, varlink_error, NULL); |
| 340 |
else |
| 341 |
(void) sd_varlink_error_errno(c->current_link, r); |
| 342 |
} else |
| 343 |
/* Otherwise terminate the whole process. */ |
| 344 |
sd_event_exit(c->event, r); |
| 345 |
|
| 346 |
context_reset_full(c); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
static int context_fail(Context *c, int r) { |
| 351 |
return context_fail_full(c, r, /* varlink_error= */ NULL); |
| 352 |
} |
| 353 |
|
| 354 |
static void context_success(Context *c) { |
| 355 |
int r; |
| 356 |
|
| 357 |
assert(c); |
| 358 |
|
| 359 |
/* Called whenever the current retrieval succeeds asynchronously */ |
| 360 |
|
| 361 |
context_log(c, LOG_DEBUG, "Operation succeeded."); |
| 362 |
|
| 363 |
if (c->current_link) { |
| 364 |
r = sd_varlink_replybo( |
| 365 |
c->current_link, |
| 366 |
JSON_BUILD_PAIR_IOVEC_BASE64("data", &c->write_iovec), |
| 367 |
SD_JSON_BUILD_PAIR_CONDITION(c->ifindex > 0, "interface", SD_JSON_BUILD_INTEGER(c->ifindex))); |
| 368 |
if (r < 0) |
| 369 |
context_log_errno(c, LOG_WARNING, r, "Failed to reply to Varlink call, ignoring: %m"); |
| 370 |
} else |
| 371 |
sd_event_exit(c->event, 0); |
| 372 |
|
| 373 |
context_reset_full(c); |
| 374 |
} |
| 375 |
|
| 376 |
static int setsockopt_callback(void *userdata, curl_socket_t curlfd, curlsocktype purpose) { |
| 377 |
Context *c = ASSERT_PTR(userdata); |
| 378 |
int r; |
| 379 |
|
| 380 |
assert(curlfd >= 0); |
| 381 |
|
| 382 |
if (purpose != CURLSOCKTYPE_IPCXN) |
| 383 |
return CURL_SOCKOPT_OK; |
| 384 |
|
| 385 |
r = socket_set_unicast_if(curlfd, AF_UNSPEC, c->ifindex); |
| 386 |
if (r < 0) { |
| 387 |
context_log_errno(c, LOG_ERR, r, "Failed to bind HTTP socket to interface: %m"); |
| 388 |
return CURL_SOCKOPT_ERROR; |
| 389 |
} |
| 390 |
|
| 391 |
if (c->fwmark_set && |
| 392 |
setsockopt(curlfd, SOL_SOCKET, SO_MARK, &c->fwmark, sizeof(c->fwmark)) < 0) { |
| 393 |
context_log_errno(c, LOG_ERR, errno, "Failed to set firewall mark on HTTP socket: %m"); |
| 394 |
return CURL_SOCKOPT_ERROR; |
| 395 |
} |
| 396 |
|
| 397 |
return CURL_SOCKOPT_OK; |
| 398 |
} |
| 399 |
|
| 400 |
static int context_combine_key(Context *c, char **ret) { |
| 401 |
assert(ret); |
| 402 |
|
| 403 |
/* Combines the well known key with the explicitly configured key */ |
| 404 |
|
| 405 |
char *s; |
| 406 |
if (c->well_known < 0 || c->well_known == IMDS_BASE) { |
| 407 |
if (!c->key) |
| 408 |
return -ENODATA; |
| 409 |
|
| 410 |
s = strdup(c->key); |
| 411 |
} else { |
| 412 |
const char *wk = arg_well_known_key[c->well_known]; |
| 413 |
if (!wk) |
| 414 |
return -ENODATA; |
| 415 |
if (c->key) |
| 416 |
s = strjoin(wk, c->key); |
| 417 |
else |
| 418 |
s = strdup(wk); |
| 419 |
} |
| 420 |
if (!s) |
| 421 |
return -ENOMEM; |
| 422 |
|
| 423 |
*ret = TAKE_PTR(s); |
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
static const char *context_get_runtime_directory(Context *c) { |
| 428 |
assert(c); |
| 429 |
|
| 430 |
/* Returns the discovered runtime directory, but only if caching is enabled. */ |
| 431 |
|
| 432 |
if (!c->cache) { |
| 433 |
context_log(c, LOG_DEBUG, "Cache disabled."); |
| 434 |
return NULL; |
| 435 |
} |
| 436 |
|
| 437 |
const char *e = secure_getenv("RUNTIME_DIRECTORY"); |
| 438 |
if (!e) { |
| 439 |
context_log(c, LOG_DEBUG, "Not using cache as $RUNTIME_DIRECTORY is not set."); |
| 440 |
return NULL; |
| 441 |
} |
| 442 |
|
| 443 |
return e; |
| 444 |
} |
| 445 |
|
| 446 |
static int context_save_ifname(Context *c) { |
| 447 |
int r; |
| 448 |
|
| 449 |
assert(c); |
| 450 |
|
| 451 |
/* Saves the used interface name for later retrievals, so that we don't have to wildcard search on |
| 452 |
* all interfaces anymore. */ |
| 453 |
|
| 454 |
if (c->ifindex <= 0) |
| 455 |
return 0; |
| 456 |
|
| 457 |
const char *d = context_get_runtime_directory(c); |
| 458 |
if (!d) |
| 459 |
return 0; |
| 460 |
|
| 461 |
_cleanup_close_ int dirfd = open(d, O_PATH|O_CLOEXEC); |
| 462 |
if (dirfd < 0) |
| 463 |
return context_log_errno(c, LOG_ERR, errno, "Failed to open runtime directory: %m"); |
| 464 |
|
| 465 |
_cleanup_free_ char *ifname = NULL; |
| 466 |
r = rtnl_get_ifname_full(&c->rtnl, c->ifindex, &ifname, /* ret_altnames= */ NULL); |
| 467 |
if (r < 0) |
| 468 |
return context_log_errno(c, LOG_ERR, r, "Failed to resolve interface index %i: %m", c->ifindex); |
| 469 |
|
| 470 |
r = write_string_file_at(dirfd, "ifname", ifname, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); |
| 471 |
if (r < 0) |
| 472 |
return context_log_errno(c, LOG_ERR, r, "Failed to write 'ifname' file: %m"); |
| 473 |
|
| 474 |
return 1; |
| 475 |
} |
| 476 |
|
| 477 |
typedef enum CacheResult { |
| 478 |
CACHE_RESULT_DISABLED, /* caching is disabled */ |
| 479 |
CACHE_RESULT_HIT, /* found a positive entry */ |
| 480 |
CACHE_RESULT_MISS, /* did not find an entry */ |
| 481 |
CACHE_RESULT_KEY_NOT_FOUND, /* found a negative entry */ |
| 482 |
CACHE_RESULT_NOT_CACHEABLE, /* not suitable for caching */ |
| 483 |
_CACHE_RESULT_MAX, |
| 484 |
_CACHE_RESULT_INVALID = -EINVAL, |
| 485 |
_CACHE_RESULT_ERRNO_MAX = -ERRNO_MAX, |
| 486 |
} CacheResult; |
| 487 |
|
| 488 |
static CacheResult context_process_cache(Context *c) { |
| 489 |
int r; |
| 490 |
|
| 491 |
assert(c); |
| 492 |
|
| 493 |
assert(c->key || c->well_known >= 0); |
| 494 |
assert(c->cache_fd < 0); |
| 495 |
assert(!c->cache_filename); |
| 496 |
assert(!c->cache_temporary_filename); |
| 497 |
|
| 498 |
/* Checks the local cache – if we have one – for the current request */ |
| 499 |
|
| 500 |
if (c->cache_dir_fd < 0) { |
| 501 |
const char *e = context_get_runtime_directory(c); |
| 502 |
if (!e) |
| 503 |
return CACHE_RESULT_DISABLED; |
| 504 |
|
| 505 |
char ifname[IF_NAMESIZE]; |
| 506 |
r = format_ifname(c->ifindex, ifname); |
| 507 |
if (r < 0) |
| 508 |
return context_log_errno(c, LOG_ERR, r, "Failed to format interface name: %m"); |
| 509 |
|
| 510 |
if (!filename_is_valid(ifname)) |
| 511 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EINVAL), "Network interface name '%s' is not a valid filename, refusing.", ifname); |
| 512 |
|
| 513 |
_cleanup_free_ char *cache_dir = path_join("cache", ifname); |
| 514 |
if (!cache_dir) |
| 515 |
return context_log_oom(c); |
| 516 |
|
| 517 |
r = chase(cache_dir, |
| 518 |
e, |
| 519 |
CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY|CHASE_PREFIX_ROOT, |
| 520 |
/* ret_path= */ NULL, |
| 521 |
&c->cache_dir_fd); |
| 522 |
if (r < 0) |
| 523 |
return context_log_errno(c, LOG_ERR, r, "Failed to open cache directory: %m"); |
| 524 |
} |
| 525 |
|
| 526 |
_cleanup_free_ char *k = NULL; |
| 527 |
r = context_combine_key(c, &k); |
| 528 |
if (r < 0) |
| 529 |
return context_log_errno(c, LOG_ERR, r, "Failed to combine IMDS key: %m"); |
| 530 |
|
| 531 |
_cleanup_free_ char *escaped = xescape(k, "/."); |
| 532 |
if (!escaped) |
| 533 |
return context_log_oom(c); |
| 534 |
|
| 535 |
_cleanup_free_ char *fn = strjoin("key-", escaped); |
| 536 |
if (!fn) |
| 537 |
return context_log_oom(c); |
| 538 |
|
| 539 |
if (!filename_is_valid(fn)) { |
| 540 |
context_log(c, LOG_WARNING, "Cache filename for '%s' is not valid, not caching.", fn); |
| 541 |
return CACHE_RESULT_NOT_CACHEABLE; |
| 542 |
} |
| 543 |
|
| 544 |
c->cache_filename = TAKE_PTR(fn); |
| 545 |
|
| 546 |
_cleanup_close_ int fd = openat(c->cache_dir_fd, c->cache_filename, O_RDONLY|O_CLOEXEC); |
| 547 |
if (fd < 0) { |
| 548 |
if (errno != ENOENT) |
| 549 |
return context_log_errno(c, LOG_ERR, errno, "Failed to open cache file '%s': %m", c->cache_filename); |
| 550 |
} else { |
| 551 |
_cleanup_free_ char *d = NULL; |
| 552 |
size_t l; |
| 553 |
|
| 554 |
context_log(c, LOG_DEBUG, "Found cached file '%s'.", c->cache_filename); |
| 555 |
|
| 556 |
r = fgetxattr_malloc(fd, "user.imds.timestamp", &d, &l); |
| 557 |
if (r < 0) |
| 558 |
return context_log_errno(c, LOG_ERR, r, "Failed to read timestamp from cache file: %m"); |
| 559 |
if (l != sizeof(usec_t)) |
| 560 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EBADMSG), "Invalid timestamp xattr on cache file '%s': %m", c->cache_filename); |
| 561 |
|
| 562 |
usec_t *u = (usec_t*) d; |
| 563 |
if (usec_add(*u, c->refresh_usec) > c->timestamp) { |
| 564 |
_cleanup_free_ char *result = NULL; |
| 565 |
r = fgetxattr_malloc(fd, "user.imds.result", &result, /* ret_size= */ NULL); |
| 566 |
if (r == -ENODATA) { |
| 567 |
/* No user.imds.result xattr means: hit! */ |
| 568 |
if (c->write_stdout) { |
| 569 |
r = copy_bytes(fd, STDOUT_FILENO, /* max_bytes= */ UINT64_MAX, /* copy_flags= */ 0); |
| 570 |
if (r < 0) |
| 571 |
return context_log_errno(c, LOG_ERR, r, "Failed to write cached data to standard output: %m"); |
| 572 |
} else { |
| 573 |
assert(!iovec_is_set(&c->write_iovec)); |
| 574 |
r = read_full_file_at(fd, /* filename= */ NULL, (char**) &c->write_iovec.iov_base, &c->write_iovec.iov_len); |
| 575 |
if (r < 0) |
| 576 |
return context_log_errno(c, LOG_ERR, r, "Failed to read cache data: %m"); |
| 577 |
} |
| 578 |
|
| 579 |
return CACHE_RESULT_HIT; |
| 580 |
} |
| 581 |
if (r < 0) |
| 582 |
return context_log_errno(c, LOG_ERR, r, "Failed to read 'user.imds.result' extended attribute: %m"); |
| 583 |
|
| 584 |
if (streq(result, "key-not-found")) |
| 585 |
return CACHE_RESULT_KEY_NOT_FOUND; |
| 586 |
|
| 587 |
context_log(c, LOG_WARNING, "Unexpected 'user.imds.result' extended attribute value, ignoring: %s", result); |
| 588 |
(void) unlinkat(c->cache_dir_fd, c->cache_filename, /* flags= */ 0); |
| 589 |
} else { |
| 590 |
context_log(c, LOG_DEBUG, "Cached data is older than '%s', ignoring.", FORMAT_TIMESPAN(c->refresh_usec, 0)); |
| 591 |
(void) unlinkat(c->cache_dir_fd, c->cache_filename, /* flags= */ 0); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
/* So the above was not conclusive, let's then at least try to reuse the token */ |
| 596 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 597 |
r = sd_json_parse_file_at(/* f= */ NULL, c->cache_dir_fd, "token", /* flags= */ 0, &j, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 598 |
if (r == -ENOENT) { |
| 599 |
context_log_errno(c, LOG_DEBUG, r, "No cached token"); |
| 600 |
return CACHE_RESULT_MISS; |
| 601 |
} |
| 602 |
if (r < 0) |
| 603 |
return context_log_errno(c, LOG_ERR, r, "Failed to read cached token: %m"); |
| 604 |
|
| 605 |
struct { |
| 606 |
const char *token; |
| 607 |
uint64_t until; |
| 608 |
} d = {}; |
| 609 |
|
| 610 |
static const sd_json_dispatch_field table[] = { |
| 611 |
{ "token", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(d, token), SD_JSON_MANDATORY }, |
| 612 |
{ "validUntilUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, voffsetof(d, until), SD_JSON_MANDATORY }, |
| 613 |
{} |
| 614 |
}; |
| 615 |
|
| 616 |
r = sd_json_dispatch(j, table, SD_JSON_ALLOW_EXTENSIONS, &d); |
| 617 |
if (r < 0) |
| 618 |
return context_log_errno(c, LOG_ERR, r, "Failed to decode cached token data: %m"); |
| 619 |
|
| 620 |
if (d.until > c->timestamp) { |
| 621 |
c->token_string = strdup(d.token); |
| 622 |
if (!c->token_string) |
| 623 |
return context_log_oom(c); |
| 624 |
|
| 625 |
context_log(c, LOG_INFO, "Reusing cached token."); |
| 626 |
} else |
| 627 |
context_log(c, LOG_DEBUG, "Cached token is stale, not using."); |
| 628 |
|
| 629 |
return CACHE_RESULT_MISS; |
| 630 |
} |
| 631 |
|
| 632 |
static int on_retry(sd_event_source *s, uint64_t usec, void *userdata) { |
| 633 |
Context *c = ASSERT_PTR(userdata); |
| 634 |
int r; |
| 635 |
|
| 636 |
assert(s); |
| 637 |
|
| 638 |
/* Invoked whenever the retry timer event elapses and we need to retry again */ |
| 639 |
|
| 640 |
context_log(c, LOG_DEBUG, "Retrying..."); |
| 641 |
|
| 642 |
/* Maybe some other instance was successful in the meantime and already found something? */ |
| 643 |
CacheResult cr = context_process_cache(c); |
| 644 |
if (cr < 0) { |
| 645 |
context_fail(c, cr); |
| 646 |
return 0; |
| 647 |
} |
| 648 |
if (cr == CACHE_RESULT_HIT) { |
| 649 |
context_success(c); |
| 650 |
return 0; |
| 651 |
} |
| 652 |
if (cr == CACHE_RESULT_KEY_NOT_FOUND) { |
| 653 |
context_fail(c, context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Cache reports: key not found")); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
r = context_acquire_token(c); |
| 658 |
if (r < 0) { |
| 659 |
context_fail(c, r); |
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
r = context_acquire_data(c); |
| 664 |
if (r < 0) |
| 665 |
context_fail(c, r); |
| 666 |
|
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
static int context_schedule_retry(Context *c) { |
| 671 |
int r; |
| 672 |
|
| 673 |
assert(c); |
| 674 |
|
| 675 |
/* Schedules a new retry via a timer event */ |
| 676 |
|
| 677 |
if (c->n_retry >= RETRY_MAX) |
| 678 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EUCLEAN), "Retry limits reached, refusing."); |
| 679 |
|
| 680 |
if (c->n_retry == 0) |
| 681 |
c->retry_interval_usec = RETRY_MIN_USEC; |
| 682 |
else if (c->retry_interval_usec < RETRY_MAX_USEC / 2) |
| 683 |
c->retry_interval_usec *= 2; |
| 684 |
else |
| 685 |
c->retry_interval_usec = RETRY_MAX_USEC; |
| 686 |
|
| 687 |
c->n_retry++; |
| 688 |
context_log(c, LOG_DEBUG, "Retry attempt #%u in %s...", c->n_retry, FORMAT_TIMESPAN(c->retry_interval_usec, USEC_PER_MSEC)); |
| 689 |
|
| 690 |
context_reset_for_refresh(c); |
| 691 |
|
| 692 |
r = event_reset_time_relative( |
| 693 |
c->event, |
| 694 |
&c->retry_source, |
| 695 |
CLOCK_BOOTTIME, |
| 696 |
c->retry_interval_usec, |
| 697 |
/* accuracy= */ 0, |
| 698 |
on_retry, |
| 699 |
c, |
| 700 |
/* priority= */ 0, |
| 701 |
"imds-retry", |
| 702 |
/* force_reset= */ true); |
| 703 |
if (r < 0) |
| 704 |
return context_log_errno(c, LOG_ERR, r, "Failed to reset retry timer event source: %m"); |
| 705 |
|
| 706 |
return 0; |
| 707 |
} |
| 708 |
|
| 709 |
static int context_acquire_http_status(Context *c, CURL *curl, long *ret_status) { |
| 710 |
assert(c); |
| 711 |
assert(ret_status); |
| 712 |
|
| 713 |
/* Acquires the HTTP status code, and does some generic validation that applies to both the token and |
| 714 |
* the data transfer. |
| 715 |
* |
| 716 |
* Error handling as per: |
| 717 |
* https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instance-metadata-returns |
| 718 |
* https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service#rate-limiting |
| 719 |
*/ |
| 720 |
|
| 721 |
long status; |
| 722 |
CURLcode code = sym_curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); |
| 723 |
if (code != CURLE_OK) |
| 724 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", sym_curl_easy_strerror(code)); |
| 725 |
|
| 726 |
context_log(c, LOG_DEBUG, "Got HTTP error code %li.", status); |
| 727 |
|
| 728 |
if (status == 403) |
| 729 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EADDRNOTAVAIL), "IMDS is not available"); |
| 730 |
|
| 731 |
/* Automatically retry on some transient errors from HTTP */ |
| 732 |
if (IN_SET(status, |
| 733 |
503, /* AWS + GCP */ |
| 734 |
429 /* Azure + GCP */)) { |
| 735 |
*ret_status = 0; |
| 736 |
return 0; /* no immediate answer, please schedule retry */ |
| 737 |
} |
| 738 |
|
| 739 |
if (status < 200 || status > 600) |
| 740 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request finished with unexpected code %li.", status); |
| 741 |
|
| 742 |
*ret_status = status; |
| 743 |
return 1; /* valid answer */ |
| 744 |
} |
| 745 |
|
| 746 |
static int context_validate_token_http_status(Context *c, long status) { |
| 747 |
assert(c); |
| 748 |
|
| 749 |
/* Specific HTTP status checks for the token transfer */ |
| 750 |
|
| 751 |
if (status >= 300) |
| 752 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request for token finished with unexpected code %li.", status); |
| 753 |
|
| 754 |
return 1; /* all good */ |
| 755 |
} |
| 756 |
|
| 757 |
static int context_validate_data_http_status(Context *c, long status) { |
| 758 |
int r; |
| 759 |
|
| 760 |
assert(c); |
| 761 |
|
| 762 |
/* Specific HTTP status checks for the data transfer */ |
| 763 |
|
| 764 |
if (status == 401 && arg_token_url) { |
| 765 |
/* We need a new token */ |
| 766 |
context_log(c, LOG_DEBUG, "Server requested a new token..."); |
| 767 |
|
| 768 |
/* Count token requests as a retry */ |
| 769 |
if (c->n_retry >= RETRY_MAX) |
| 770 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EUCLEAN), "Retry limits reached, refusing."); |
| 771 |
c->n_retry++; |
| 772 |
|
| 773 |
context_flush_token(c); |
| 774 |
context_reset_for_refresh(c); |
| 775 |
|
| 776 |
r = context_acquire_token(c); |
| 777 |
if (r < 0) |
| 778 |
return r; |
| 779 |
|
| 780 |
r = context_acquire_data(c); |
| 781 |
if (r < 0) |
| 782 |
return r; |
| 783 |
|
| 784 |
return 0; /* restarted right-away */ |
| 785 |
} |
| 786 |
|
| 787 |
if (status == 404) { |
| 788 |
_cleanup_free_ char *key = NULL; |
| 789 |
r = context_combine_key(c, &key); |
| 790 |
if (r < 0) |
| 791 |
return context_log_errno(c, LOG_ERR, r, "Failed to combine IMDS key: %m"); |
| 792 |
|
| 793 |
/* Do negative caching for not found */ |
| 794 |
if (c->cache_fd >= 0) { |
| 795 |
if (fsetxattr(c->cache_fd, "user.imds.result", "key-not-found", STRLEN("key-not-found"), /* flags= */ 0) < 0) |
| 796 |
context_log_errno(c, LOG_DEBUG, errno, "Failed to set result xattr on '%s', ignoring: %m", c->cache_filename); |
| 797 |
else { |
| 798 |
r = link_tmpfile_at(c->cache_fd, c->cache_dir_fd, c->cache_temporary_filename, c->cache_filename, LINK_TMPFILE_REPLACE); |
| 799 |
if (r < 0) |
| 800 |
return context_log_errno(c, LOG_ERR, r, "Failed to move cache file into place: %m"); |
| 801 |
|
| 802 |
c->cache_fd = safe_close(c->cache_fd); |
| 803 |
c->cache_temporary_filename = mfree(c->cache_temporary_filename); |
| 804 |
|
| 805 |
context_log(c, LOG_DEBUG, "Cached negative entry for '%s'.", key); |
| 806 |
} |
| 807 |
} |
| 808 |
|
| 809 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Key '%s' not found.", key); |
| 810 |
} |
| 811 |
|
| 812 |
if (status >= 300) |
| 813 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request for data finished with unexpected code %li.", status); |
| 814 |
|
| 815 |
return 1; /* all good */ |
| 816 |
} |
| 817 |
|
| 818 |
static int context_validate_token(Context *c) { |
| 819 |
int r; |
| 820 |
|
| 821 |
assert(c); |
| 822 |
|
| 823 |
/* Validates that the downloaded token data actually forms a valid string */ |
| 824 |
|
| 825 |
_cleanup_free_ char *t = NULL; |
| 826 |
r = make_cstring( |
| 827 |
c->token.iov_base, |
| 828 |
c->token.iov_len, |
| 829 |
MAKE_CSTRING_REFUSE_TRAILING_NUL, |
| 830 |
&t); |
| 831 |
if (r < 0) |
| 832 |
return context_log_errno(c, LOG_ERR, r, "Failed to convert token into C string: %m"); |
| 833 |
|
| 834 |
if (string_has_cc(t, NULL) || |
| 835 |
!utf8_is_valid(t)) |
| 836 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EINVAL), "Token not valid UTF-8 or contains control characters, refusing."); |
| 837 |
|
| 838 |
free_and_replace(c->token_string, t); |
| 839 |
return 1; /* all good */ |
| 840 |
} |
| 841 |
|
| 842 |
static int context_save_token(Context *c) { |
| 843 |
int r; |
| 844 |
|
| 845 |
assert(c); |
| 846 |
assert(c->token_string); |
| 847 |
|
| 848 |
/* Save the acquired token in the cache, so that we can reuse it later */ |
| 849 |
|
| 850 |
if (c->cache_dir_fd < 0) |
| 851 |
return 0; |
| 852 |
|
| 853 |
/* Only store half the valid time, to make sure we have ample time to use it */ |
| 854 |
usec_t until = usec_add(c->timestamp, c->refresh_usec/2); |
| 855 |
|
| 856 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL; |
| 857 |
r = sd_json_buildo( |
| 858 |
&j, |
| 859 |
SD_JSON_BUILD_PAIR_STRING("token", c->token_string), |
| 860 |
SD_JSON_BUILD_PAIR_UNSIGNED("validUntilUSec", until)); |
| 861 |
if (r < 0) |
| 862 |
return context_log_errno(c, LOG_ERR, r, "Failed to build token JSON: %m"); |
| 863 |
|
| 864 |
_cleanup_free_ char *t = NULL; |
| 865 |
r = sd_json_variant_format(j, SD_JSON_FORMAT_NEWLINE, &t); |
| 866 |
if (r < 0) |
| 867 |
return context_log_errno(c, LOG_ERR, r, "Failed to format JSON: %m"); |
| 868 |
|
| 869 |
r = write_string_file_at(c->cache_dir_fd, "token", t, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600); |
| 870 |
if (r < 0) |
| 871 |
return context_log_errno(c, LOG_ERR, r, "Failed to write token cache file: %m"); |
| 872 |
|
| 873 |
return 0; |
| 874 |
} |
| 875 |
|
| 876 |
static int context_save_data(Context *c) { |
| 877 |
int r; |
| 878 |
|
| 879 |
assert(c); |
| 880 |
|
| 881 |
/* Finalize saving of the acquired data in the cache */ |
| 882 |
|
| 883 |
if (c->cache_fd < 0) |
| 884 |
return 0; |
| 885 |
|
| 886 |
r = link_tmpfile_at(c->cache_fd, c->cache_dir_fd, c->cache_temporary_filename, c->cache_filename, LINK_TMPFILE_REPLACE); |
| 887 |
if (r < 0) |
| 888 |
return context_log_errno(c, LOG_ERR, r, "Failed to move cache file into place: %m"); |
| 889 |
|
| 890 |
c->cache_fd = safe_close(c->cache_fd); |
| 891 |
c->cache_temporary_filename = mfree(c->cache_temporary_filename); |
| 892 |
|
| 893 |
context_log(c, LOG_DEBUG, "Cached data."); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
static int curl_on_finished(CurlSlot *slot, CURL *curl, CURLcode result, void *userdata) { |
| 898 |
Context *c = ASSERT_PTR(userdata); |
| 899 |
int r; |
| 900 |
|
| 901 |
/* Called whenever libcurl did its thing and reports a download being complete or having failed */ |
| 902 |
|
| 903 |
switch (result) { |
| 904 |
|
| 905 |
case CURLE_OK: /* yay! */ |
| 906 |
/* If we managed to get a HTTP reply, this is good enough, let's pin the interface now for |
| 907 |
* later calls */ |
| 908 |
(void) context_save_ifname(c); |
| 909 |
break; |
| 910 |
|
| 911 |
case CURLE_WRITE_ERROR: |
| 912 |
/* CURLE_WRITE_ERROR we'll see if the data callbacks failed already. We'll try to look at the |
| 913 |
* HTTP status below, and use that ideally. */ |
| 914 |
break; |
| 915 |
|
| 916 |
case CURLE_COULDNT_CONNECT: |
| 917 |
case CURLE_OPERATION_TIMEDOUT: |
| 918 |
case CURLE_GOT_NOTHING: |
| 919 |
case CURLE_SEND_ERROR: |
| 920 |
case CURLE_RECV_ERROR: |
| 921 |
context_log(c, LOG_INFO, "Connection error from curl: %s", sym_curl_easy_strerror(result)); |
| 922 |
|
| 923 |
/* Automatically retry on some transient errors from curl itself */ |
| 924 |
r = context_schedule_retry(c); |
| 925 |
if (r < 0) |
| 926 |
return context_fail(c, r); |
| 927 |
|
| 928 |
return 0; |
| 929 |
|
| 930 |
default: |
| 931 |
return context_fail_full( |
| 932 |
c, |
| 933 |
context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EHOSTDOWN), "Transfer failed: %s", sym_curl_easy_strerror(result)), |
| 934 |
"io.systemd.InstanceMetadata.CommunicationFailure"); |
| 935 |
} |
| 936 |
|
| 937 |
long status; |
| 938 |
r = context_acquire_http_status(c, curl, &status); |
| 939 |
if (r == -EADDRNOTAVAIL) |
| 940 |
return context_fail_full(c, r, "io.systemd.InstanceMetadata.NotAvailable"); |
| 941 |
if (r < 0) |
| 942 |
return context_fail(c, r); |
| 943 |
if (r == 0) { /* We shall retry */ |
| 944 |
(void) context_schedule_retry(c); |
| 945 |
return 0; |
| 946 |
} |
| 947 |
if (result != CURLE_OK) /* if getting the HTTP status didn't work, propagate a generic error */ |
| 948 |
return context_fail(c, SYNTHETIC_ERRNO(ENOTRECOVERABLE)); |
| 949 |
|
| 950 |
if (slot == c->slot_token) { |
| 951 |
r = context_validate_token_http_status(c, status); |
| 952 |
if (r < 0) |
| 953 |
return context_fail(c, r); |
| 954 |
|
| 955 |
r = context_validate_token(c); |
| 956 |
if (r < 0) |
| 957 |
return context_fail(c, r); |
| 958 |
|
| 959 |
context_log(c, LOG_DEBUG, "Token successfully acquired."); |
| 960 |
|
| 961 |
r = context_save_token(c); |
| 962 |
if (r < 0) |
| 963 |
return context_fail(c, r); |
| 964 |
|
| 965 |
r = context_acquire_data(c); |
| 966 |
if (r < 0) |
| 967 |
return context_fail(c, r); |
| 968 |
|
| 969 |
} else if (slot == c->slot_data) { |
| 970 |
|
| 971 |
r = context_validate_data_http_status(c, status); |
| 972 |
if (r == -ENOENT) |
| 973 |
return context_fail_full(c, r, "io.systemd.InstanceMetadata.KeyNotFound"); |
| 974 |
if (r < 0) |
| 975 |
return context_fail(c, r); |
| 976 |
if (r == 0) /* Immediately restarted */ |
| 977 |
return 0; |
| 978 |
|
| 979 |
context_log(c, LOG_DEBUG, "Data download successful."); |
| 980 |
|
| 981 |
r = context_save_data(c); |
| 982 |
if (r < 0) |
| 983 |
return context_fail(c, r); |
| 984 |
|
| 985 |
context_success(c); |
| 986 |
} else |
| 987 |
assert_not_reached(); |
| 988 |
|
| 989 |
return 0; |
| 990 |
} |
| 991 |
|
| 992 |
static int context_acquire_glue(Context *c) { |
| 993 |
int r; |
| 994 |
|
| 995 |
assert(c); |
| 996 |
|
| 997 |
/* Allocates a curl object if we don't have one yet */ |
| 998 |
|
| 999 |
if (c->glue) |
| 1000 |
return 0; |
| 1001 |
|
| 1002 |
r = curl_glue_new(&c->glue, c->event); |
| 1003 |
if (r < 0) |
| 1004 |
return context_log_errno(c, LOG_ERR, r, "Failed to allocate curl glue: %m"); |
| 1005 |
|
| 1006 |
return 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static size_t data_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
| 1010 |
Context *c = ASSERT_PTR(userdata); |
| 1011 |
size_t sz = size * nmemb; |
| 1012 |
int r; |
| 1013 |
|
| 1014 |
/* Called whenever we receive new payload from the server */ |
| 1015 |
assert(contents); |
| 1016 |
|
| 1017 |
/* If we managed to get a HTTP reply, this is good enough, let's pin the interface now for later calls */ |
| 1018 |
(void) context_save_ifname(c); |
| 1019 |
|
| 1020 |
/* Before we use the acquired data, let's verify the HTTP status, if there's a failure or we need to |
| 1021 |
* restart, abort the write here. Note that the curl_on_finished() call will then check the HTTP |
| 1022 |
* status again and act on it. */ |
| 1023 |
long status; |
| 1024 |
r = context_acquire_http_status(c, curl_slot_get_easy(c->slot_data), &status); |
| 1025 |
if (r <= 0) |
| 1026 |
return 0; /* fail the thing, so that curl_on_finished() can handle this failure or retry request */ |
| 1027 |
if (status >= 300) /* any status equal or above 300 needs to be handled by curl_on_finished() too */ |
| 1028 |
return 0; |
| 1029 |
|
| 1030 |
if (sz > UINT64_MAX - c->data_size || |
| 1031 |
c->data_size + sz > DATA_SIZE_MAX) { |
| 1032 |
context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(E2BIG), "Data too large, refusing."); |
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
|
| 1036 |
c->data_size += sz; |
| 1037 |
|
| 1038 |
if (c->write_stdout) |
| 1039 |
(void) fwrite(contents, 1, sz, stdout); |
| 1040 |
else if (!iovec_append(&c->write_iovec, &IOVEC_MAKE(contents, sz))) { |
| 1041 |
context_log_oom(c); |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (c->cache_fd >= 0) { |
| 1046 |
r = loop_write(c->cache_fd, contents, sz); |
| 1047 |
if (r < 0) { |
| 1048 |
context_log_errno(c, LOG_ERR, r, "Failed to write data to cache: %m"); |
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
return sz; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static int context_acquire_data(Context *c) { |
| 1057 |
int r; |
| 1058 |
|
| 1059 |
assert(c); |
| 1060 |
assert(c->key || c->well_known >= 0); |
| 1061 |
|
| 1062 |
/* Called to initiate getting the actual IMDS key payload */ |
| 1063 |
|
| 1064 |
if (arg_token_url && !c->token_string) |
| 1065 |
return 0; /* If we need a token first, let's not do anything */ |
| 1066 |
|
| 1067 |
_cleanup_free_ char *k = NULL; |
| 1068 |
r = context_combine_key(c, &k); |
| 1069 |
if (r < 0) |
| 1070 |
return context_log_errno(c, LOG_ERR, r, "Failed to combine key: %m"); |
| 1071 |
|
| 1072 |
context_log(c, LOG_INFO, "Requesting data for key '%s'.", k); |
| 1073 |
|
| 1074 |
if (c->cache_dir_fd >= 0 && |
| 1075 |
c->cache_filename && |
| 1076 |
c->cache_fd < 0) { |
| 1077 |
c->cache_fd = open_tmpfile_linkable_at(c->cache_dir_fd, c->cache_filename, O_WRONLY|O_CLOEXEC, &c->cache_temporary_filename); |
| 1078 |
if (c->cache_fd < 0) |
| 1079 |
return context_log_errno(c, LOG_ERR, c->cache_fd, "Failed to create cache file '%s': %m", c->cache_filename); |
| 1080 |
|
| 1081 |
if (fchmod(c->cache_fd, 0600) < 0) |
| 1082 |
return context_log_errno(c, LOG_ERR, errno, "Failed to adjust cache node access mode: %m"); |
| 1083 |
|
| 1084 |
if (fsetxattr(c->cache_fd, "user.imds.timestamp", &c->timestamp, sizeof(c->timestamp), /* flags= */ 0) < 0) |
| 1085 |
return context_log_errno(c, LOG_ERR, errno, "Failed to set timestamp xattr on '%s': %m", c->cache_filename); |
| 1086 |
} |
| 1087 |
|
| 1088 |
r = context_acquire_glue(c); |
| 1089 |
if (r < 0) |
| 1090 |
return r; |
| 1091 |
|
| 1092 |
_cleanup_free_ char *url = strjoin(arg_data_url, k, arg_data_url_suffix); |
| 1093 |
if (!url) |
| 1094 |
return context_log_oom(c); |
| 1095 |
|
| 1096 |
_cleanup_(curl_easy_cleanupp) CURL *easy = NULL; |
| 1097 |
r = curl_glue_make(&easy, url); |
| 1098 |
if (r < 0) |
| 1099 |
return context_log_errno(c, LOG_ERR, r, "Failed to create CURL request for data: %m"); |
| 1100 |
|
| 1101 |
if (c->token_string) { |
| 1102 |
_cleanup_free_ char *token_header = strjoin(arg_token_header_name, ": ", c->token_string); |
| 1103 |
if (!token_header) |
| 1104 |
return context_log_oom(c); |
| 1105 |
|
| 1106 |
r = curl_append_to_header(&c->request_header_data, STRV_MAKE(token_header)); |
| 1107 |
if (r < 0) |
| 1108 |
return context_log_errno(c, LOG_ERR, r, "Failed to create curl header: %m"); |
| 1109 |
} |
| 1110 |
|
| 1111 |
r = curl_append_to_header(&c->request_header_data, arg_extra_header); |
| 1112 |
if (r < 0) |
| 1113 |
return context_log_errno(c, LOG_ERR, r, "Failed to create curl header: %m"); |
| 1114 |
|
| 1115 |
if (c->request_header_data) |
| 1116 |
if (sym_curl_easy_setopt(easy, CURLOPT_HTTPHEADER, c->request_header_data) != CURLE_OK) |
| 1117 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set HTTP request header."); |
| 1118 |
|
| 1119 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, data_write_callback) != CURLE_OK) |
| 1120 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL write function."); |
| 1121 |
|
| 1122 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEDATA, c) != CURLE_OK) |
| 1123 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL write function userdata."); |
| 1124 |
|
| 1125 |
if (sym_curl_easy_setopt(easy, CURLOPT_SOCKOPTFUNCTION, setsockopt_callback) != CURLE_OK) |
| 1126 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt function."); |
| 1127 |
|
| 1128 |
if (sym_curl_easy_setopt(easy, CURLOPT_SOCKOPTDATA, c) != CURLE_OK) |
| 1129 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt function userdata."); |
| 1130 |
|
| 1131 |
if (sym_curl_easy_setopt(easy, CURLOPT_LOCALPORT, 1L) != CURLE_OK) |
| 1132 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt local port"); |
| 1133 |
|
| 1134 |
if (sym_curl_easy_setopt(easy, CURLOPT_LOCALPORTRANGE, 1023L) != CURLE_OK) |
| 1135 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt local port range"); |
| 1136 |
|
| 1137 |
r = curl_glue_perform_async(c->glue, easy, curl_on_finished, c, &c->slot_data); |
| 1138 |
if (r < 0) |
| 1139 |
return context_log_errno(c, LOG_ERR, r, "Failed to add CURL request to glue: %m"); |
| 1140 |
TAKE_PTR(easy); |
| 1141 |
|
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
|
| 1145 |
static size_t token_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) { |
| 1146 |
Context *c = ASSERT_PTR(userdata); |
| 1147 |
size_t sz = size * nmemb; |
| 1148 |
int r; |
| 1149 |
|
| 1150 |
/* Called whenever we get data from the token download */ |
| 1151 |
assert(contents); |
| 1152 |
|
| 1153 |
/* If we managed to get a HTTP reply, this is good enough, let's pin the interface now for later calls */ |
| 1154 |
(void) context_save_ifname(c); |
| 1155 |
|
| 1156 |
/* Before we use acquired data, let's verify the HTTP status */ |
| 1157 |
long status; |
| 1158 |
r = context_acquire_http_status(c, curl_slot_get_easy(c->slot_token), &status); |
| 1159 |
if (r <= 0) |
| 1160 |
return 0; /* fail the thing, so that curl_on_finished() can handle this failure or retry request */ |
| 1161 |
if (status >= 300) /* any status equal or above 300 needs to be handled by curl_on_finished() */ |
| 1162 |
return 0; |
| 1163 |
|
| 1164 |
if (sz > SIZE_MAX - c->token.iov_len || |
| 1165 |
c->token.iov_len + sz > TOKEN_SIZE_MAX) { |
| 1166 |
context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(E2BIG), "IMDS token too large."); |
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (!iovec_append(&c->token, &IOVEC_MAKE(contents, sz))) { |
| 1171 |
context_log_oom(c); |
| 1172 |
return 0; |
| 1173 |
} |
| 1174 |
|
| 1175 |
return sz; |
| 1176 |
} |
| 1177 |
|
| 1178 |
static int context_acquire_token(Context *c) { |
| 1179 |
int r; |
| 1180 |
|
| 1181 |
assert(c); |
| 1182 |
|
| 1183 |
/* Called to initiate getting the token if we need one. */ |
| 1184 |
|
| 1185 |
if (c->token_string || !arg_token_url) |
| 1186 |
return 0; |
| 1187 |
|
| 1188 |
context_log(c, LOG_INFO, "Requesting token."); |
| 1189 |
|
| 1190 |
r = context_acquire_glue(c); |
| 1191 |
if (r < 0) |
| 1192 |
return r; |
| 1193 |
|
| 1194 |
_cleanup_(curl_easy_cleanupp) CURL *easy = NULL; |
| 1195 |
r = curl_glue_make(&easy, arg_token_url); |
| 1196 |
if (r < 0) |
| 1197 |
return context_log_errno(c, LOG_ERR, r, "Failed to create CURL request for API token: %m"); |
| 1198 |
|
| 1199 |
if (arg_refresh_header_name) { |
| 1200 |
_cleanup_free_ char *ttl_header = NULL; |
| 1201 |
if (asprintf(&ttl_header, |
| 1202 |
"%s: %" PRIu64, |
| 1203 |
arg_refresh_header_name, |
| 1204 |
DIV_ROUND_UP(c->refresh_usec, USEC_PER_SEC)) < 0) |
| 1205 |
return context_log_oom(c); |
| 1206 |
|
| 1207 |
c->request_header_token = curl_slist_new(ttl_header, NULL); |
| 1208 |
if (!c->request_header_token) |
| 1209 |
return context_log_oom(c); |
| 1210 |
} |
| 1211 |
|
| 1212 |
if (sym_curl_easy_setopt(easy, CURLOPT_HTTPHEADER, c->request_header_token) != CURLE_OK) |
| 1213 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set HTTP request header."); |
| 1214 |
|
| 1215 |
if (sym_curl_easy_setopt(easy, CURLOPT_CUSTOMREQUEST, "PUT") != CURLE_OK) |
| 1216 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set HTTP request method."); |
| 1217 |
|
| 1218 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, token_write_callback) != CURLE_OK) |
| 1219 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL write function."); |
| 1220 |
|
| 1221 |
if (sym_curl_easy_setopt(easy, CURLOPT_WRITEDATA, c) != CURLE_OK) |
| 1222 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL write function userdata."); |
| 1223 |
|
| 1224 |
if (sym_curl_easy_setopt(easy, CURLOPT_SOCKOPTFUNCTION, setsockopt_callback) != CURLE_OK) |
| 1225 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt function."); |
| 1226 |
|
| 1227 |
if (sym_curl_easy_setopt(easy, CURLOPT_SOCKOPTDATA, c) != CURLE_OK) |
| 1228 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to set CURL setsockopt function userdata."); |
| 1229 |
|
| 1230 |
r = curl_glue_perform_async(c->glue, easy, curl_on_finished, c, &c->slot_token); |
| 1231 |
if (r < 0) |
| 1232 |
return context_log_errno(c, LOG_ERR, r, "Failed to add CURL request to glue: %m"); |
| 1233 |
TAKE_PTR(easy); |
| 1234 |
|
| 1235 |
return 0; |
| 1236 |
} |
| 1237 |
|
| 1238 |
static int vl_on_reply(sd_varlink *link, sd_json_variant *m, const char *error_id, sd_varlink_reply_flags_t flags, void *userdata) { |
| 1239 |
ChildData *cd = ASSERT_PTR(userdata); |
| 1240 |
Context *c = ASSERT_PTR(cd->context); |
| 1241 |
int r; |
| 1242 |
|
| 1243 |
assert(link); |
| 1244 |
assert(m); |
| 1245 |
|
| 1246 |
/* When we spawned off worker instances of ourselves (one for each local network interface), then |
| 1247 |
* we'll get a response from them via a Varlink reply. Handle it. */ |
| 1248 |
|
| 1249 |
if (error_id) { |
| 1250 |
r = sd_varlink_error_to_errno(error_id, m); |
| 1251 |
if (r == -EBADR) |
| 1252 |
context_log_errno(c, LOG_WARNING, r, "Varlink error from interface %i: %s", cd->ifindex, error_id); |
| 1253 |
else |
| 1254 |
context_log_errno(c, LOG_WARNING, r, "Varlink error from interface %i: %m", cd->ifindex); |
| 1255 |
|
| 1256 |
/* Propagate these errors immediately */ |
| 1257 |
if (streq(error_id, "io.systemd.InstanceMetadata.KeyNotFound")) { |
| 1258 |
context_fail_full(c, -ENOENT, error_id); |
| 1259 |
return 0; |
| 1260 |
} |
| 1261 |
if (streq(error_id, "io.systemd.InstanceMetadata.WellKnownKeyUnset")) { |
| 1262 |
context_fail_full(c, -ENODATA, error_id); |
| 1263 |
return 0; |
| 1264 |
} |
| 1265 |
if (streq(error_id, "io.systemd.InstanceMetadata.NotAvailable")) { |
| 1266 |
context_fail_full(c, -EADDRNOTAVAIL, error_id); |
| 1267 |
return 0; |
| 1268 |
} |
| 1269 |
|
| 1270 |
/* The other errors we consider transient. Let's see if we shall immediately restart the request. */ |
| 1271 |
if (cd->retry) { |
| 1272 |
context_log(c, LOG_DEBUG, "Child for network interface %i was scheduled for immediate retry, executing now.", cd->ifindex); |
| 1273 |
cd->link = sd_varlink_close_unref(cd->link); |
| 1274 |
cd->retry = false; |
| 1275 |
|
| 1276 |
r = context_spawn_child(c, cd->ifindex, &cd->link); |
| 1277 |
if (r < 0) { |
| 1278 |
context_fail(c, r); |
| 1279 |
return 0; |
| 1280 |
} |
| 1281 |
|
| 1282 |
sd_varlink_set_userdata(cd->link, cd); |
| 1283 |
return 0; |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* We shall not retry immediately. In that case, we give up on the child, and propagate the |
| 1287 |
* error if it was the last child, otherwise we continue until the last one dies too. */ |
| 1288 |
cd = child_data_free(cd); |
| 1289 |
|
| 1290 |
if (hashmap_isempty(c->child_data) && !c->wait) { |
| 1291 |
/* This is the last child, propagate the error */ |
| 1292 |
context_log(c, LOG_DEBUG, "Last child failed, propagating error."); |
| 1293 |
|
| 1294 |
if (streq(error_id, "io.systemd.InstanceMetadata.CommunicationFailure")) |
| 1295 |
context_fail_full(c, -EHOSTDOWN, error_id); |
| 1296 |
else if (streq(error_id, "io.systemd.InstanceMetadata.Timeout")) |
| 1297 |
context_fail_full(c, -ETIMEDOUT, error_id); |
| 1298 |
else |
| 1299 |
context_fail_full(c, r, error_id); |
| 1300 |
|
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
context_log(c, LOG_DEBUG, "Pending children remaining, continuing to wait."); |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
assert(!iovec_is_set(&c->write_iovec)); |
| 1309 |
|
| 1310 |
static const sd_json_dispatch_field table[] = { |
| 1311 |
{ "data", SD_JSON_VARIANT_STRING, json_dispatch_unbase64_iovec, offsetof(Context, write_iovec), SD_JSON_MANDATORY }, |
| 1312 |
{ "interface", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(Context, ifindex), 0 }, |
| 1313 |
{} |
| 1314 |
}; |
| 1315 |
|
| 1316 |
r = sd_json_dispatch(m, table, SD_JSON_ALLOW_EXTENSIONS, c); |
| 1317 |
if (r < 0) { |
| 1318 |
context_fail(c, context_log_errno(c, LOG_ERR, r, "Failed to decode reply data: %m")); |
| 1319 |
return 0; |
| 1320 |
} |
| 1321 |
|
| 1322 |
if (c->write_stdout) { |
| 1323 |
r = loop_write(STDOUT_FILENO, c->write_iovec.iov_base, c->write_iovec.iov_len); |
| 1324 |
if (r < 0) { |
| 1325 |
context_fail(c, context_log_errno(c, LOG_ERR, r, "Failed to output data: %m")); |
| 1326 |
return 0; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
|
| 1330 |
context_success(c); |
| 1331 |
return 0; |
| 1332 |
} |
| 1333 |
|
| 1334 |
static int context_load_ifname(Context *c) { |
| 1335 |
int r; |
| 1336 |
|
| 1337 |
assert(c); |
| 1338 |
|
| 1339 |
/* Tries to load the previously used interface name, so that we don't have to wildcard search on all |
| 1340 |
* interfaces. */ |
| 1341 |
|
| 1342 |
const char *e = context_get_runtime_directory(c); |
| 1343 |
if (!e) |
| 1344 |
return 0; |
| 1345 |
|
| 1346 |
_cleanup_close_ int dirfd = open(e, O_PATH|O_CLOEXEC); |
| 1347 |
if (dirfd < 0) |
| 1348 |
return context_log_errno(c, LOG_ERR, errno, "Failed to open runtime directory: %m"); |
| 1349 |
|
| 1350 |
_cleanup_free_ char *ifname = NULL; |
| 1351 |
r = read_one_line_file_at(dirfd, "ifname", &ifname); |
| 1352 |
if (r == -ENOENT) |
| 1353 |
return 0; |
| 1354 |
if (r < 0) |
| 1355 |
return context_log_errno(c, LOG_ERR, r, "Failed to load 'ifname' file from runtime directory: %m"); |
| 1356 |
|
| 1357 |
if (!ifname_valid(ifname)) |
| 1358 |
return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EINVAL), "Loaded interface name not valid, refusing: %s", ifname); |
| 1359 |
|
| 1360 |
c->ifindex = rtnl_resolve_interface(&c->rtnl, ifname); |
| 1361 |
if (c->ifindex < 0) { |
| 1362 |
(void) unlinkat(dirfd, "ifname", /* flags= */ 0); |
| 1363 |
context_log_errno(c, LOG_ERR, c->ifindex, "Failed to resolve saved interface name '%s', assuming interface disappeared, ignoring: %m", ifname); |
| 1364 |
c->ifindex = 0; |
| 1365 |
return 0; |
| 1366 |
} |
| 1367 |
|
| 1368 |
log_debug("Using previously pinned interface '%s' (ifindex: %i).", ifname, c->ifindex); |
| 1369 |
return 1; |
| 1370 |
} |
| 1371 |
|
| 1372 |
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR( |
| 1373 |
child_data_hash_ops, |
| 1374 |
void, |
| 1375 |
trivial_hash_func, |
| 1376 |
trivial_compare_func, |
| 1377 |
ChildData, |
| 1378 |
child_data_free); |
| 1379 |
|
| 1380 |
static int context_spawn_child(Context *c, int ifindex, sd_varlink **ret) { |
| 1381 |
int r; |
| 1382 |
|
| 1383 |
assert(c); |
| 1384 |
assert(ifindex > 0); |
| 1385 |
assert(ret); |
| 1386 |
|
| 1387 |
/* If we don't know yet on which network interface the IMDS server can be found, let's spawn separate |
| 1388 |
* instances of ourselves, one for each interface, and collect the results. We communicate with |
| 1389 |
* each one via Varlink, the same way as clients talk to us. */ |
| 1390 |
|
| 1391 |
context_log(c, LOG_DEBUG, "Spawning child for interface '%i'.", ifindex); |
| 1392 |
|
| 1393 |
_cleanup_free_ char *p = NULL; |
| 1394 |
_cleanup_close_ int fd = pin_callout_binary(LIBEXECDIR "/systemd-imdsd", &p); |
| 1395 |
if (fd < 0) |
| 1396 |
return context_log_errno(c, LOG_ERR, fd, "Failed to find imdsd binary: %m"); |
| 1397 |
|
| 1398 |
_cleanup_strv_free_ char **argv = strv_new( |
| 1399 |
p, |
| 1400 |
"--vendor", strempty(arg_vendor), |
| 1401 |
"--token-url", strempty(arg_token_url), |
| 1402 |
"--refresh-header-name", strempty(arg_refresh_header_name), |
| 1403 |
"--data-url", strempty(arg_data_url), |
| 1404 |
"--data-url-suffix", strempty(arg_data_url_suffix), |
| 1405 |
"--token-header-name", strempty(arg_token_header_name), |
| 1406 |
"--address-ipv4", in4_addr_is_null(&arg_address_ipv4) ? "" : IN4_ADDR_TO_STRING(&arg_address_ipv4), |
| 1407 |
"--address-ipv6", in6_addr_is_null(&arg_address_ipv6) ? "" : IN6_ADDR_TO_STRING(&arg_address_ipv6)); |
| 1408 |
if (!argv) |
| 1409 |
return log_oom(); |
| 1410 |
|
| 1411 |
STRV_FOREACH(i, arg_extra_header) |
| 1412 |
if (strv_extend_strv(&argv, STRV_MAKE("--extra-header", *i), /* filter_duplicates= */ false) < 0) |
| 1413 |
return log_oom(); |
| 1414 |
|
| 1415 |
for (ImdsWellKnown wk = 0; wk < _IMDS_WELL_KNOWN_MAX; wk++) { |
| 1416 |
if (!arg_well_known_key[wk]) |
| 1417 |
continue; |
| 1418 |
|
| 1419 |
if (strv_extendf(&argv, "--well-known-key=%s:%s", imds_well_known_to_string(wk), arg_well_known_key[wk]) < 0) |
| 1420 |
return log_oom(); |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (DEBUG_LOGGING) { |
| 1424 |
_cleanup_free_ char *cmdline = quote_command_line(argv, SHELL_ESCAPE_EMPTY); |
| 1425 |
log_debug("About to fork off: %s", strnull(cmdline)); |
| 1426 |
} |
| 1427 |
|
| 1428 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 1429 |
r = sd_varlink_connect_exec(&vl, p, argv); |
| 1430 |
if (r < 0) |
| 1431 |
return context_log_errno(c, LOG_ERR, r, "Failed to fork off imdsd binary for interface %i: %m", ifindex); |
| 1432 |
|
| 1433 |
r = sd_varlink_attach_event( |
| 1434 |
vl, |
| 1435 |
c->event, |
| 1436 |
SD_EVENT_PRIORITY_NORMAL); |
| 1437 |
if (r < 0) |
| 1438 |
return context_log_errno(c, LOG_ERR, r, "Failed to attach Varlink connection to event loop: %m"); |
| 1439 |
|
| 1440 |
r = sd_varlink_bind_reply(vl, vl_on_reply); |
| 1441 |
if (r < 0) |
| 1442 |
return context_log_errno(c, LOG_ERR, r, "Failed to bind reply callback: %m"); |
| 1443 |
|
| 1444 |
r = sd_varlink_invokebo( |
| 1445 |
vl, |
| 1446 |
"io.systemd.InstanceMetadata.Get", |
| 1447 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("key", c->key), |
| 1448 |
SD_JSON_BUILD_PAIR_CONDITION(c->well_known >= 0, "wellKnown", JSON_BUILD_STRING_UNDERSCORIFY(imds_well_known_to_string(c->well_known))), |
| 1449 |
SD_JSON_BUILD_PAIR_INTEGER("interface", ifindex), |
| 1450 |
SD_JSON_BUILD_PAIR_INTEGER("refreshUSec", c->refresh_usec), |
| 1451 |
SD_JSON_BUILD_PAIR_BOOLEAN("cache", c->cache), |
| 1452 |
SD_JSON_BUILD_PAIR_CONDITION(c->fwmark_set, "firewallMark", SD_JSON_BUILD_UNSIGNED(c->fwmark)), |
| 1453 |
SD_JSON_BUILD_PAIR_CONDITION(!c->fwmark_set, "firewallMark", SD_JSON_BUILD_NULL)); /* explicitly turn off fwmark, if not set */ |
| 1454 |
if (r < 0) |
| 1455 |
return context_log_errno(c, LOG_ERR, r, "Failed to issue Get() command to Varlink child: %m"); |
| 1456 |
|
| 1457 |
*ret = TAKE_PTR(vl); |
| 1458 |
return 0; |
| 1459 |
} |
| 1460 |
|
| 1461 |
static int context_spawn_new_child(Context *c, int ifindex) { |
| 1462 |
int r; |
| 1463 |
|
| 1464 |
assert(c); |
| 1465 |
|
| 1466 |
/* Spawn a child, and keep track of it */ |
| 1467 |
|
| 1468 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 1469 |
r = context_spawn_child(c, ifindex, &vl); |
| 1470 |
if (r < 0) |
| 1471 |
return r; |
| 1472 |
|
| 1473 |
_cleanup_(child_data_freep) ChildData *cd = new(ChildData, 1); |
| 1474 |
if (!cd) |
| 1475 |
return context_log_oom(c); |
| 1476 |
|
| 1477 |
*cd = (ChildData) { |
| 1478 |
.ifindex = ifindex, |
| 1479 |
.link = sd_varlink_ref(vl), |
| 1480 |
}; |
| 1481 |
|
| 1482 |
sd_varlink_set_userdata(vl, cd); |
| 1483 |
|
| 1484 |
if (hashmap_ensure_put(&c->child_data, &child_data_hash_ops, INT_TO_PTR(ifindex), cd) < 0) |
| 1485 |
return context_log_oom(c); |
| 1486 |
|
| 1487 |
cd->context = c; |
| 1488 |
TAKE_PTR(cd); |
| 1489 |
|
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
|
| 1493 |
static int on_address_change(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
| 1494 |
Context *c = ASSERT_PTR(userdata); |
| 1495 |
int ifindex, r; |
| 1496 |
|
| 1497 |
assert(rtnl); |
| 1498 |
assert(m); |
| 1499 |
|
| 1500 |
/* Called whenever an address appears on the network stack. We use that as hint that it is worth to |
| 1501 |
* invoke a child processing that interface (either for the first time, or again) */ |
| 1502 |
|
| 1503 |
r = sd_rtnl_message_addr_get_ifindex(m, &ifindex); |
| 1504 |
if (r < 0) { |
| 1505 |
context_log_errno(c, LOG_WARNING, r, "rtnl: could not get ifindex from message, ignoring: %m"); |
| 1506 |
return 0; |
| 1507 |
} |
| 1508 |
if (ifindex <= 0) { |
| 1509 |
context_log(c, LOG_WARNING, "rtnl: received address message with invalid ifindex %d, ignoring.", ifindex); |
| 1510 |
return 0; |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (ifindex == LOOPBACK_IFINDEX) { |
| 1514 |
context_log(c, LOG_DEBUG, "Ignoring loopback device."); |
| 1515 |
return 0; |
| 1516 |
} |
| 1517 |
|
| 1518 |
if (!c->key && c->well_known < 0) |
| 1519 |
return 0; |
| 1520 |
|
| 1521 |
ChildData *existing = hashmap_get(c->child_data, INT_TO_PTR(ifindex)); |
| 1522 |
if (existing) { |
| 1523 |
/* We already have an attempt ongoing for this one? Remember there's a reason now to retry |
| 1524 |
* this, because new connectivity appeared. */ |
| 1525 |
context_log(c, LOG_DEBUG, "Child for network interface %i already spawned off, scheduling for immediate retry.", ifindex); |
| 1526 |
existing->retry = true; |
| 1527 |
return 0; |
| 1528 |
} |
| 1529 |
|
| 1530 |
return context_spawn_new_child(c, ifindex); |
| 1531 |
} |
| 1532 |
|
| 1533 |
static int context_acquire_rtnl_with_match(Context *c) { |
| 1534 |
int r; |
| 1535 |
|
| 1536 |
assert(c); |
| 1537 |
assert(c->event); |
| 1538 |
|
| 1539 |
/* Acquire a netlink connection and a match if we don't have one yet */ |
| 1540 |
|
| 1541 |
if (!c->rtnl) { |
| 1542 |
r = sd_netlink_open(&c->rtnl); |
| 1543 |
if (r < 0) |
| 1544 |
return context_log_errno(c, LOG_ERR, r, "Failed to connect to netlink: %m"); |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (!c->rtnl_attached) { |
| 1548 |
/* The netlink connection might have created previously via rtnl_resolve_interface() – which |
| 1549 |
* however didn't attach it to our event loop. Do so now. */ |
| 1550 |
r = sd_netlink_attach_event(c->rtnl, c->event, SD_EVENT_PRIORITY_NORMAL); |
| 1551 |
if (r < 0) |
| 1552 |
return context_log_errno(c, LOG_ERR, r, "Failed to attach netlink socket to event loop: %m"); |
| 1553 |
|
| 1554 |
c->rtnl_attached = true; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (!c->address_change_slot) { |
| 1558 |
r = sd_netlink_add_match(c->rtnl, &c->address_change_slot, RTM_NEWADDR, on_address_change, /* destroy_callback= */ NULL, c, "newaddr"); |
| 1559 |
if (r < 0) |
| 1560 |
return context_log_errno(c, LOG_ERR, r, "Failed to subscribe to RTM_NEWADDR events: %m"); |
| 1561 |
} |
| 1562 |
|
| 1563 |
return 0; |
| 1564 |
} |
| 1565 |
|
| 1566 |
static int context_spawn_children(Context *c) { |
| 1567 |
int r; |
| 1568 |
|
| 1569 |
assert(c); |
| 1570 |
assert(c->key || c->well_known >= 0); |
| 1571 |
|
| 1572 |
/* If we don't know yet on which interface to query, let's see which interfaces there are and spawn |
| 1573 |
* ourselves, once on each */ |
| 1574 |
|
| 1575 |
r = context_acquire_rtnl_with_match(c); |
| 1576 |
if (r < 0) |
| 1577 |
return r; |
| 1578 |
|
| 1579 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
| 1580 |
r = sd_rtnl_message_new_addr(c->rtnl, &req, RTM_GETADDR, /* ifindex= */ 0, AF_UNSPEC); |
| 1581 |
if (r < 0) |
| 1582 |
return r; |
| 1583 |
|
| 1584 |
r = sd_netlink_message_set_request_dump(req, true); |
| 1585 |
if (r < 0) |
| 1586 |
return r; |
| 1587 |
|
| 1588 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *reply = NULL; |
| 1589 |
r = sd_netlink_call(c->rtnl, req, 0, &reply); |
| 1590 |
if (r < 0) |
| 1591 |
return r; |
| 1592 |
|
| 1593 |
for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
| 1594 |
r = on_address_change(c->rtnl, i, c); |
| 1595 |
if (r < 0) |
| 1596 |
return r; |
| 1597 |
} |
| 1598 |
|
| 1599 |
return 0; |
| 1600 |
} |
| 1601 |
|
| 1602 |
static int imds_configured(int level) { |
| 1603 |
/* Checks if we have enough endpoint information to operate */ |
| 1604 |
|
| 1605 |
if (arg_endpoint_source < 0) |
| 1606 |
return log_full_errno(level, SYNTHETIC_ERRNO(EOPNOTSUPP), "No IMDS endpoint information provided or detected, cannot operate."); |
| 1607 |
|
| 1608 |
if (!arg_data_url) |
| 1609 |
return log_full_errno(level, SYNTHETIC_ERRNO(EOPNOTSUPP), "No data base URL provided."); |
| 1610 |
|
| 1611 |
if (!!arg_token_url != !!arg_token_header_name) |
| 1612 |
return log_full_errno(level, SYNTHETIC_ERRNO(EOPNOTSUPP), "Incomplete token parameters configured for endpoint."); |
| 1613 |
|
| 1614 |
return 0; |
| 1615 |
} |
| 1616 |
|
| 1617 |
static int setup_network(void) { |
| 1618 |
int r; |
| 1619 |
|
| 1620 |
/* Generates a .network file based on the IMDS endpoint information we have */ |
| 1621 |
|
| 1622 |
if (arg_network_mode == IMDS_NETWORK_OFF) { |
| 1623 |
log_debug("IMDS networking turned off, not generating .network file."); |
| 1624 |
return 0; |
| 1625 |
} |
| 1626 |
|
| 1627 |
_cleanup_close_ int network_dir_fd = -EBADF; |
| 1628 |
r = chase("/run/systemd/network", |
| 1629 |
/* root= */ NULL, |
| 1630 |
CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, |
| 1631 |
/* ret_path= */ NULL, |
| 1632 |
&network_dir_fd); |
| 1633 |
if (r < 0) |
| 1634 |
return log_error_errno(r, "Failed to open .network directory: %m"); |
| 1635 |
|
| 1636 |
_cleanup_free_ char *t = NULL; |
| 1637 |
_cleanup_fclose_ FILE *f = NULL; |
| 1638 |
r = fopen_tmpfile_linkable_at(network_dir_fd, "85-imds-early.network", O_WRONLY|O_CLOEXEC, &t, &f); |
| 1639 |
if (r < 0) |
| 1640 |
return log_error_errno(r, "Failed to create 85-imds-early.network file: %m"); |
| 1641 |
|
| 1642 |
CLEANUP_TMPFILE_AT(network_dir_fd, t); |
| 1643 |
|
| 1644 |
fputs("# Generated by systemd-imdsd, do not edit.\n" |
| 1645 |
"#\n" |
| 1646 |
"# This configures Ethernet devices on cloud hosts that support IMDS, given that\n" |
| 1647 |
"# before doing IMDS we need to activate the network.\n", f); |
| 1648 |
|
| 1649 |
if (arg_network_mode != IMDS_NETWORK_UNLOCKED && |
| 1650 |
(in4_addr_is_set(&arg_address_ipv4) || in6_addr_is_set(&arg_address_ipv6))) |
| 1651 |
fputs("#\n" |
| 1652 |
"# Note: this will create a 'prohibit' route to the IMDS endpoint,\n" |
| 1653 |
"# blocking direct access to IMDS. Direct IMDS access is then only\n" |
| 1654 |
"# available to traffic marked with fwmark 0x7FFF0815, which can be\n" |
| 1655 |
"# set via SO_MARK and various other methods, which require\n" |
| 1656 |
"# privileges.\n", |
| 1657 |
f); |
| 1658 |
|
| 1659 |
fputs("\n" |
| 1660 |
"[Match]\n" |
| 1661 |
"Type=ether\n" |
| 1662 |
"Kind=!*\n" |
| 1663 |
"\n" |
| 1664 |
"[Network]\n" |
| 1665 |
"DHCP=yes\n" |
| 1666 |
"LinkLocalAddressing=ipv6\n" |
| 1667 |
"\n" |
| 1668 |
"[DHCP]\n" |
| 1669 |
"UseTimezone=yes\n" |
| 1670 |
"UseHostname=yes\n" |
| 1671 |
"UseMTU=yes\n", f); |
| 1672 |
|
| 1673 |
if (in4_addr_is_set(&arg_address_ipv4)) |
| 1674 |
fputs("\n" |
| 1675 |
"[Link]\n" |
| 1676 |
"RequiredFamilyForOnline=ipv4\n", f); |
| 1677 |
else if (in6_addr_is_set(&arg_address_ipv6)) |
| 1678 |
fputs("\n" |
| 1679 |
"[Link]\n" |
| 1680 |
"RequiredFamilyForOnline=ipv6\n", f); |
| 1681 |
|
| 1682 |
if (arg_network_mode != IMDS_NETWORK_UNLOCKED) { |
| 1683 |
if (in4_addr_is_set(&arg_address_ipv4)) |
| 1684 |
fprintf(f, |
| 1685 |
"\n" |
| 1686 |
"# Prohibit regular access to IMDS (IPv4)\n" |
| 1687 |
"[Route]\n" |
| 1688 |
"Destination=%s\n" |
| 1689 |
"Type=prohibit\n", |
| 1690 |
IN4_ADDR_TO_STRING(&arg_address_ipv4)); |
| 1691 |
|
| 1692 |
if (in6_addr_is_set(&arg_address_ipv6)) |
| 1693 |
fprintf(f, |
| 1694 |
"\n" |
| 1695 |
"# Prohibit regular access to IMDS (IPv6)\n" |
| 1696 |
"[Route]\n" |
| 1697 |
"Destination=%s\n" |
| 1698 |
"Type=prohibit\n", |
| 1699 |
IN6_ADDR_TO_STRING(&arg_address_ipv6)); |
| 1700 |
} |
| 1701 |
|
| 1702 |
if (in4_addr_is_set(&arg_address_ipv4)) |
| 1703 |
fprintf(f, |
| 1704 |
"\n" |
| 1705 |
"# Always allow IMDS access via a special routing table (IPv4)\n" |
| 1706 |
"[Route]\n" |
| 1707 |
"Destination=%s\n" |
| 1708 |
"Scope=link\n" |
| 1709 |
"Table=0x7FFF0815\n" |
| 1710 |
"\n" |
| 1711 |
"# Sockets marked with firewall mark 0x7FFF0815 get access to the IMDS route by\n" |
| 1712 |
"# using the 0x7FFF0815 table populated above.\n" |
| 1713 |
"[RoutingPolicyRule]\n" |
| 1714 |
"Family=ipv4\n" |
| 1715 |
"FirewallMark=0x7FFF0815\n" |
| 1716 |
"Table=0x7FFF0815\n", |
| 1717 |
IN4_ADDR_TO_STRING(&arg_address_ipv4)); |
| 1718 |
|
| 1719 |
if (in6_addr_is_set(&arg_address_ipv6)) |
| 1720 |
fprintf(f, |
| 1721 |
"\n" |
| 1722 |
"# Always allow IMDS access via a special routing table (IPv6)\n" |
| 1723 |
"[Route]\n" |
| 1724 |
"Destination=%s\n" |
| 1725 |
"Table=0x7FFF0815\n" |
| 1726 |
"\n" |
| 1727 |
"# Sockets marked with firewall mark 0x7FFF0815 get access to the IMDS route by\n" |
| 1728 |
"# using the 0x7FFF0815 table populated above.\n" |
| 1729 |
"[RoutingPolicyRule]\n" |
| 1730 |
"Family=ipv6\n" |
| 1731 |
"FirewallMark=0x7FFF0815\n" |
| 1732 |
"Table=0x7FFF0815\n", |
| 1733 |
IN6_ADDR_TO_STRING(&arg_address_ipv6)); |
| 1734 |
|
| 1735 |
if (fchmod(fileno(f), 0644) < 0) |
| 1736 |
return log_error_errno(errno, "Failed to set access mode for 85-imds-early.network: %m"); |
| 1737 |
|
| 1738 |
r = flink_tmpfile_at(f, network_dir_fd, t, "85-imds-early.network", LINK_TMPFILE_REPLACE); |
| 1739 |
if (r < 0) |
| 1740 |
return log_error_errno(r, "Failed to move 85-imds-early.network into place: %m"); |
| 1741 |
|
| 1742 |
t = mfree(t); /* disarm auto-cleanup */ |
| 1743 |
|
| 1744 |
log_info("Created 85-imds-early.network."); |
| 1745 |
return 0; |
| 1746 |
} |
| 1747 |
|
| 1748 |
static int add_address_to_json_array(sd_json_variant **array, int family, const union in_addr_union *addr) { |
| 1749 |
int r; |
| 1750 |
|
| 1751 |
assert(array); |
| 1752 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 1753 |
assert(addr); |
| 1754 |
|
| 1755 |
/* Appends the specified IP address, turned into A/AAAA RRs to the specified JSON array */ |
| 1756 |
|
| 1757 |
if (in_addr_is_null(family, addr)) |
| 1758 |
return 0; |
| 1759 |
|
| 1760 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 1761 |
if (dns_resource_record_new_address(&rr, family, addr, "_imds") < 0) |
| 1762 |
return log_oom(); |
| 1763 |
|
| 1764 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *rrj = NULL; |
| 1765 |
r = dns_resource_record_to_json(rr, &rrj); |
| 1766 |
if (r < 0) |
| 1767 |
return log_error_errno(r, "Failed to convert A RR to JSON: %m"); |
| 1768 |
|
| 1769 |
r = sd_json_variant_append_array(array, rrj); |
| 1770 |
if (r < 0) |
| 1771 |
return log_error_errno(r, "Failed to append A RR to JSON array: %m"); |
| 1772 |
|
| 1773 |
log_debug("Writing IMDS RR for: %s", dns_resource_record_to_string(rr)); |
| 1774 |
return 1; |
| 1775 |
} |
| 1776 |
|
| 1777 |
static int setup_address_rrs(void) { |
| 1778 |
int r; |
| 1779 |
|
| 1780 |
/* Creates local RRs (honoured by systemd-resolved) for the IMDS endpoint addresses. */ |
| 1781 |
|
| 1782 |
if (arg_network_mode == IMDS_NETWORK_OFF) { |
| 1783 |
log_debug("IMDS networking turned off, not generating .rr file."); |
| 1784 |
return 0; |
| 1785 |
} |
| 1786 |
|
| 1787 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *aj = NULL; |
| 1788 |
|
| 1789 |
union in_addr_union u = { .in = arg_address_ipv4 }; |
| 1790 |
r = add_address_to_json_array(&aj, AF_INET, &u); |
| 1791 |
if (r < 0) |
| 1792 |
return r; |
| 1793 |
|
| 1794 |
u = (union in_addr_union) { .in6 = arg_address_ipv6 }; |
| 1795 |
r = add_address_to_json_array(&aj, AF_INET6, &u); |
| 1796 |
if (r < 0) |
| 1797 |
return r; |
| 1798 |
|
| 1799 |
if (sd_json_variant_elements(aj) == 0) { |
| 1800 |
log_debug("No IMDS endpoint addresses known, not writing out RRs."); |
| 1801 |
return 0; |
| 1802 |
} |
| 1803 |
|
| 1804 |
_cleanup_free_ char *text = NULL; |
| 1805 |
r = sd_json_variant_format(aj, SD_JSON_FORMAT_NEWLINE, &text); |
| 1806 |
if (r < 0) |
| 1807 |
return log_error_errno(r, "Failed to format JSON text: %m"); |
| 1808 |
|
| 1809 |
r = write_string_file("/run/systemd/resolve/static.d/imds-endpoint.rr", text, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755); |
| 1810 |
if (r < 0) |
| 1811 |
return log_error_errno(r, "Failed to write IMDS RR data: %m"); |
| 1812 |
|
| 1813 |
log_info("Created imds-endpoint.rr."); |
| 1814 |
return 0; |
| 1815 |
} |
| 1816 |
|
| 1817 |
static int on_overall_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
| 1818 |
Context *c = ASSERT_PTR(userdata); |
| 1819 |
|
| 1820 |
assert(s); |
| 1821 |
|
| 1822 |
/* Invoked whenever the overall time-out event elapses, and we just give up */ |
| 1823 |
|
| 1824 |
context_fail_full(c, context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ETIMEDOUT), "Overall timeout reached."), "io.systemd.InstanceMetadata.Timeout"); |
| 1825 |
return 0; |
| 1826 |
} |
| 1827 |
|
| 1828 |
static int context_start_overall_timeout(Context *c, usec_t usec) { |
| 1829 |
int r; |
| 1830 |
|
| 1831 |
assert(c); |
| 1832 |
|
| 1833 |
r = event_reset_time_relative( |
| 1834 |
c->event, |
| 1835 |
&c->overall_timeout_source, |
| 1836 |
CLOCK_BOOTTIME, |
| 1837 |
usec, |
| 1838 |
/* accuracy= */ 0, |
| 1839 |
on_overall_timeout, |
| 1840 |
c, |
| 1841 |
/* priority= */ 0, |
| 1842 |
"imds-overall-timeout", |
| 1843 |
/* force_reset= */ true); |
| 1844 |
if (r < 0) |
| 1845 |
return context_log_errno(c, LOG_ERR, r, "Failed to reset retry timer event source: %m"); |
| 1846 |
|
| 1847 |
return 0; |
| 1848 |
} |
| 1849 |
|
| 1850 |
static int cmdline_run(void) { |
| 1851 |
int r; |
| 1852 |
|
| 1853 |
/* Process the request when invoked via the command line (i.e. not via Varlink) */ |
| 1854 |
|
| 1855 |
r = imds_configured(LOG_INFO); |
| 1856 |
if (r == -EOPNOTSUPP) |
| 1857 |
return EXIT_NOTCONFIGURED; |
| 1858 |
if (r < 0) |
| 1859 |
return r; |
| 1860 |
|
| 1861 |
if (arg_setup_network) { |
| 1862 |
r = setup_network(); |
| 1863 |
return RET_GATHER(r, setup_address_rrs()); |
| 1864 |
} |
| 1865 |
|
| 1866 |
assert(arg_key || arg_well_known >= 0); |
| 1867 |
|
| 1868 |
_cleanup_(context_done) Context c = CONTEXT_NULL; |
| 1869 |
c.write_stdout = true; |
| 1870 |
context_new_request(&c); |
| 1871 |
|
| 1872 |
c.well_known = arg_well_known; |
| 1873 |
if (arg_key) { |
| 1874 |
c.key = strdup(arg_key); |
| 1875 |
if (!c.key) |
| 1876 |
return context_log_oom(&c); |
| 1877 |
} |
| 1878 |
|
| 1879 |
if (arg_ifname) { |
| 1880 |
c.ifindex = rtnl_resolve_interface_or_warn(&c.rtnl, arg_ifname); |
| 1881 |
if (c.ifindex < 0) |
| 1882 |
return c.ifindex; |
| 1883 |
} else { |
| 1884 |
/* Try to load the previously cached interface */ |
| 1885 |
r = context_load_ifname(&c); |
| 1886 |
if (r < 0) |
| 1887 |
return r; |
| 1888 |
} |
| 1889 |
|
| 1890 |
r = sd_event_default(&c.event); |
| 1891 |
if (r < 0) |
| 1892 |
return context_log_errno(&c, LOG_ERR, r, "Failed to allocate event loop: %m"); |
| 1893 |
|
| 1894 |
if (c.ifindex > 0) { |
| 1895 |
CacheResult cr = context_process_cache(&c); |
| 1896 |
if (cr < 0) |
| 1897 |
return cr; |
| 1898 |
if (cr == CACHE_RESULT_HIT) |
| 1899 |
return 0; |
| 1900 |
if (cr == CACHE_RESULT_KEY_NOT_FOUND) |
| 1901 |
return context_log_errno(&c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Cache reports: key not found"); |
| 1902 |
|
| 1903 |
r = context_acquire_token(&c); |
| 1904 |
if (r < 0) |
| 1905 |
return r; |
| 1906 |
|
| 1907 |
r = context_acquire_data(&c); |
| 1908 |
if (r < 0) |
| 1909 |
return r; |
| 1910 |
|
| 1911 |
r = context_start_overall_timeout(&c, DIRECT_OVERALL_TIMEOUT_USEC); |
| 1912 |
if (r < 0) |
| 1913 |
return r; |
| 1914 |
} else { |
| 1915 |
/* Couldn't find anything, let's spawn off parallel clients for all interfaces */ |
| 1916 |
r = context_spawn_children(&c); |
| 1917 |
if (r < 0) |
| 1918 |
return r; |
| 1919 |
|
| 1920 |
r = context_start_overall_timeout(&c, INDIRECT_OVERALL_TIMEOUT_USEC); |
| 1921 |
if (r < 0) |
| 1922 |
return r; |
| 1923 |
} |
| 1924 |
|
| 1925 |
r = sd_event_loop(c.event); |
| 1926 |
if (r < 0) |
| 1927 |
return r; |
| 1928 |
|
| 1929 |
return 0; |
| 1930 |
} |
| 1931 |
|
| 1932 |
static int context_acquire_system_bus(Context *c) { |
| 1933 |
int r; |
| 1934 |
|
| 1935 |
assert(c); |
| 1936 |
|
| 1937 |
/* Connect to the bus if we haven't yet */ |
| 1938 |
|
| 1939 |
if (c->system_bus) |
| 1940 |
return 0; |
| 1941 |
|
| 1942 |
r = sd_bus_default_system(&c->system_bus); |
| 1943 |
if (r < 0) |
| 1944 |
return r; |
| 1945 |
|
| 1946 |
r = sd_bus_attach_event(c->system_bus, c->event, SD_EVENT_PRIORITY_NORMAL); |
| 1947 |
if (r < 0) |
| 1948 |
return r; |
| 1949 |
|
| 1950 |
return 0; |
| 1951 |
} |
| 1952 |
|
| 1953 |
static JSON_DISPATCH_ENUM_DEFINE(dispatch_well_known, ImdsWellKnown, imds_well_known_from_string); |
| 1954 |
|
| 1955 |
static int dispatch_fwmark(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 1956 |
Context *c = ASSERT_PTR(userdata); |
| 1957 |
int r; |
| 1958 |
|
| 1959 |
/* Parses a firewall mark passed via Varlink/JSON. Note that any 32bit fwmark is valid, hence we keep |
| 1960 |
* track if it is set or not in a separate boolean. */ |
| 1961 |
|
| 1962 |
if (sd_json_variant_is_null(variant)) { |
| 1963 |
c->fwmark_set = false; |
| 1964 |
return 0; |
| 1965 |
} |
| 1966 |
|
| 1967 |
r = sd_json_dispatch_uint32(name, variant, flags, &c->fwmark); |
| 1968 |
if (r < 0) |
| 1969 |
return r; |
| 1970 |
|
| 1971 |
c->fwmark_set = true; |
| 1972 |
return 0; |
| 1973 |
} |
| 1974 |
|
| 1975 |
static int vl_method_get(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1976 |
Context *c = ASSERT_PTR(userdata); |
| 1977 |
int r; |
| 1978 |
|
| 1979 |
assert(link); |
| 1980 |
|
| 1981 |
if (!c->event) |
| 1982 |
c->event = sd_event_ref(sd_varlink_get_event(link)); |
| 1983 |
|
| 1984 |
context_new_request(c); |
| 1985 |
|
| 1986 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 1987 |
{ "wellKnown", SD_JSON_VARIANT_STRING, dispatch_well_known, offsetof(Context, well_known), 0 }, |
| 1988 |
{ "key", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Context, key), 0 }, |
| 1989 |
{ "interface", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_ifindex, offsetof(Context, ifindex), 0 }, |
| 1990 |
{ "refreshUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(Context, refresh_usec), 0 }, |
| 1991 |
{ "firewallMark", _SD_JSON_VARIANT_TYPE_INVALID, dispatch_fwmark, 0, 0 }, |
| 1992 |
{ "cache", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Context, cache), 0 }, |
| 1993 |
{ "wait", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Context, wait), 0 }, |
| 1994 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 1995 |
{} |
| 1996 |
}; |
| 1997 |
|
| 1998 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, c); |
| 1999 |
if (r != 0) |
| 2000 |
return r; |
| 2001 |
|
| 2002 |
if (c->key) { |
| 2003 |
if (!imds_key_is_valid(c->key)) |
| 2004 |
return sd_varlink_error_invalid_parameter_name(link, "key"); |
| 2005 |
|
| 2006 |
if (c->well_known < 0) |
| 2007 |
c->well_known = IMDS_BASE; |
| 2008 |
else if (!imds_well_known_can_suffix(c->well_known)) |
| 2009 |
return sd_varlink_error_invalid_parameter_name(link, "key"); |
| 2010 |
} else if (c->well_known < 0) |
| 2011 |
return sd_varlink_error_invalid_parameter_name(link, "key"); |
| 2012 |
|
| 2013 |
if (c->refresh_usec < REFRESH_USEC_MIN) |
| 2014 |
c->refresh_usec = REFRESH_USEC_MIN; |
| 2015 |
|
| 2016 |
uid_t peer_uid; |
| 2017 |
r = sd_varlink_get_peer_uid(link, &peer_uid); |
| 2018 |
if (r < 0) |
| 2019 |
return r; |
| 2020 |
|
| 2021 |
if (peer_uid != 0 && peer_uid != getuid()) { |
| 2022 |
/* Ask polkit if client is not privileged */ |
| 2023 |
|
| 2024 |
r = context_acquire_system_bus(c); |
| 2025 |
if (r < 0) |
| 2026 |
return r; |
| 2027 |
|
| 2028 |
const char* l[5]; |
| 2029 |
size_t k = 0; |
| 2030 |
if (c->well_known >= 0) { |
| 2031 |
l[k++] = "wellKnown"; |
| 2032 |
l[k++] = imds_well_known_to_string(c->well_known); |
| 2033 |
} |
| 2034 |
if (c->key) { |
| 2035 |
l[k++] = "key"; |
| 2036 |
l[k++] = c->key; |
| 2037 |
} |
| 2038 |
l[k] = NULL; |
| 2039 |
|
| 2040 |
r = varlink_verify_polkit_async( |
| 2041 |
link, |
| 2042 |
c->system_bus, |
| 2043 |
"io.systemd.imds.get", |
| 2044 |
l, |
| 2045 |
&c->polkit_registry); |
| 2046 |
if (r <= 0) |
| 2047 |
return r; |
| 2048 |
} |
| 2049 |
|
| 2050 |
if (imds_configured(LOG_DEBUG) < 0) |
| 2051 |
return sd_varlink_error(link, "io.systemd.InstanceMetadata.NotSupported", NULL); |
| 2052 |
|
| 2053 |
/* Up to this point we only validated/parsed stuff. Now we actually execute stuff, hence from now on |
| 2054 |
* we need to go through context_fail() when failing (context_success() if we succeed early), to |
| 2055 |
* release resources we might have allocated. */ |
| 2056 |
assert(!c->current_link); |
| 2057 |
c->current_link = sd_varlink_ref(link); |
| 2058 |
|
| 2059 |
_cleanup_free_ char *k = NULL; /* initialize here, to avoid that this remains uninitialized due to the gotos below */ |
| 2060 |
|
| 2061 |
if (c->ifindex <= 0) { |
| 2062 |
/* Try to load the previously used network interface */ |
| 2063 |
r = context_load_ifname(c); |
| 2064 |
if (r < 0) |
| 2065 |
goto fail; |
| 2066 |
} |
| 2067 |
|
| 2068 |
r = context_combine_key(c, &k); |
| 2069 |
if (r == -ENODATA) { |
| 2070 |
context_fail_full(c, r, "io.systemd.InstanceMetadata.WellKnownKeyUnset"); |
| 2071 |
return r; |
| 2072 |
} |
| 2073 |
if (r < 0) |
| 2074 |
goto fail; |
| 2075 |
|
| 2076 |
context_log(c, LOG_DEBUG, "Will request '%s' now.", k); |
| 2077 |
|
| 2078 |
if (c->ifindex > 0) { |
| 2079 |
CacheResult cr = context_process_cache(c); |
| 2080 |
if (cr < 0) { |
| 2081 |
r = cr; |
| 2082 |
goto fail; |
| 2083 |
} |
| 2084 |
if (cr == CACHE_RESULT_HIT) { |
| 2085 |
context_success(c); |
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
if (cr == CACHE_RESULT_KEY_NOT_FOUND) { |
| 2089 |
r = context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Cache reports: key not found"); |
| 2090 |
context_fail_full(c, r, "io.systemd.InstanceMetadata.KeyNotFound"); |
| 2091 |
return r; |
| 2092 |
} |
| 2093 |
|
| 2094 |
r = context_acquire_token(c); |
| 2095 |
if (r < 0) |
| 2096 |
goto fail; |
| 2097 |
|
| 2098 |
r = context_acquire_data(c); |
| 2099 |
if (r < 0) |
| 2100 |
goto fail; |
| 2101 |
|
| 2102 |
r = context_start_overall_timeout(c, DIRECT_OVERALL_TIMEOUT_USEC); |
| 2103 |
if (r < 0) |
| 2104 |
goto fail; |
| 2105 |
} else { |
| 2106 |
r = context_spawn_children(c); |
| 2107 |
if (r < 0) |
| 2108 |
goto fail; |
| 2109 |
|
| 2110 |
r = context_start_overall_timeout(c, INDIRECT_OVERALL_TIMEOUT_USEC); |
| 2111 |
if (r < 0) |
| 2112 |
goto fail; |
| 2113 |
} |
| 2114 |
|
| 2115 |
context_log(c, LOG_DEBUG, "Incoming method call is now pending"); |
| 2116 |
return 1; |
| 2117 |
|
| 2118 |
fail: |
| 2119 |
context_fail(c, r); |
| 2120 |
return r; |
| 2121 |
} |
| 2122 |
|
| 2123 |
static int vl_method_get_vendor_info(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 2124 |
Context *c = ASSERT_PTR(userdata); |
| 2125 |
int r; |
| 2126 |
|
| 2127 |
assert(link); |
| 2128 |
|
| 2129 |
r = sd_varlink_dispatch(link, parameters, /* dispatch_table= */ NULL, c); |
| 2130 |
if (r != 0) |
| 2131 |
return r; |
| 2132 |
|
| 2133 |
/* NB! We allow access to this call without Polkit */ |
| 2134 |
|
| 2135 |
if (imds_configured(LOG_DEBUG) < 0) |
| 2136 |
return sd_varlink_error(link, "io.systemd.InstanceMetadata.NotSupported", NULL); |
| 2137 |
|
| 2138 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *wkj = NULL; |
| 2139 |
for (ImdsWellKnown i = 0; i < _IMDS_WELL_KNOWN_MAX; i++) { |
| 2140 |
if (!arg_well_known_key[i]) |
| 2141 |
continue; |
| 2142 |
|
| 2143 |
r = sd_json_variant_set_field_string(&wkj, imds_well_known_to_string(i), arg_well_known_key[i]); |
| 2144 |
if (r < 0) |
| 2145 |
return r; |
| 2146 |
} |
| 2147 |
|
| 2148 |
return sd_varlink_replybo( |
| 2149 |
link, |
| 2150 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("vendor", arg_vendor), |
| 2151 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("tokenUrl", arg_token_url), |
| 2152 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("refreshHeaderName", arg_refresh_header_name), |
| 2153 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("dataUrl", arg_data_url), |
| 2154 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("dataUrlSuffix", arg_data_url_suffix), |
| 2155 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("tokenHeaderName", arg_token_header_name), |
| 2156 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("extraHeader", arg_extra_header), |
| 2157 |
JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("addressIPv4", &arg_address_ipv4), |
| 2158 |
JSON_BUILD_PAIR_IN6_ADDR_NON_NULL("addressIPv6", &arg_address_ipv6), |
| 2159 |
JSON_BUILD_PAIR_VARIANT_NON_EMPTY("wellKnown", wkj)); |
| 2160 |
} |
| 2161 |
|
| 2162 |
static int vl_server(void) { |
| 2163 |
_cleanup_(context_done) Context c = CONTEXT_NULL; |
| 2164 |
int r; |
| 2165 |
|
| 2166 |
/* Invocation as Varlink service */ |
| 2167 |
|
| 2168 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL; |
| 2169 |
r = varlink_server_new( |
| 2170 |
&varlink_server, |
| 2171 |
SD_VARLINK_SERVER_INHERIT_USERDATA, |
| 2172 |
&c); |
| 2173 |
if (r < 0) |
| 2174 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 2175 |
|
| 2176 |
r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_InstanceMetadata); |
| 2177 |
if (r < 0) |
| 2178 |
return log_error_errno(r, "Failed to add Varlink interface: %m"); |
| 2179 |
|
| 2180 |
r = sd_varlink_server_bind_method_many( |
| 2181 |
varlink_server, |
| 2182 |
"io.systemd.InstanceMetadata.Get", vl_method_get, |
| 2183 |
"io.systemd.InstanceMetadata.GetVendorInfo", vl_method_get_vendor_info); |
| 2184 |
if (r < 0) |
| 2185 |
return log_error_errno(r, "Failed to bind Varlink methods: %m"); |
| 2186 |
|
| 2187 |
r = sd_varlink_server_loop_auto(varlink_server); |
| 2188 |
if (r < 0) |
| 2189 |
return log_error_errno(r, "Failed to run Varlink event loop: %m"); |
| 2190 |
|
| 2191 |
return 0; |
| 2192 |
} |
| 2193 |
|
| 2194 |
static int help(void) { |
| 2195 |
_cleanup_free_ char *link = NULL; |
| 2196 |
_cleanup_(table_unrefp) Table *options = NULL, *endpoint_options = NULL; |
| 2197 |
int r; |
| 2198 |
|
| 2199 |
r = terminal_urlify_man("[email protected]", "8", &link); |
| 2200 |
if (r < 0) |
| 2201 |
return log_oom(); |
| 2202 |
|
| 2203 |
r = option_parser_get_help_table(&options); |
| 2204 |
if (r < 0) |
| 2205 |
return r; |
| 2206 |
|
| 2207 |
r = option_parser_get_help_table_group("Manual Endpoint Configuration", &endpoint_options); |
| 2208 |
if (r < 0) |
| 2209 |
return r; |
| 2210 |
|
| 2211 |
(void) table_sync_column_widths(0, options, endpoint_options); |
| 2212 |
|
| 2213 |
printf("%1$s [OPTIONS...] KEY\n" |
| 2214 |
"\n%2$sLow-level IMDS data acquisition.%3$s\n" |
| 2215 |
"\n%4$sOptions:%5$s\n", |
| 2216 |
program_invocation_short_name, |
| 2217 |
ansi_highlight(), |
| 2218 |
ansi_normal(), |
| 2219 |
ansi_underline(), |
| 2220 |
ansi_normal()); |
| 2221 |
|
| 2222 |
r = table_print_or_warn(options); |
| 2223 |
if (r < 0) |
| 2224 |
return r; |
| 2225 |
|
| 2226 |
printf("\n%sManual Endpoint Configuration:%s\n", |
| 2227 |
ansi_underline(), |
| 2228 |
ansi_normal()); |
| 2229 |
|
| 2230 |
r = table_print_or_warn(endpoint_options); |
| 2231 |
if (r < 0) |
| 2232 |
return r; |
| 2233 |
|
| 2234 |
printf("\nSee the %s for details.\n", link); |
| 2235 |
return 0; |
| 2236 |
} |
| 2237 |
|
| 2238 |
static bool http_header_name_valid(const char *a) { |
| 2239 |
return a && ascii_is_valid(a) && !string_has_cc(a, /* ok= */ NULL) && !strchr(a, ':'); |
| 2240 |
} |
| 2241 |
|
| 2242 |
static int parse_argv(int argc, char *argv[]) { |
| 2243 |
int r; |
| 2244 |
|
| 2245 |
assert(argc >= 0); |
| 2246 |
assert(argv); |
| 2247 |
|
| 2248 |
OptionParser opts = { argc, argv }; |
| 2249 |
|
| 2250 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 2251 |
switch (c) { |
| 2252 |
|
| 2253 |
OPTION_COMMON_HELP: |
| 2254 |
return help(); |
| 2255 |
|
| 2256 |
OPTION_COMMON_VERSION: |
| 2257 |
return version(); |
| 2258 |
|
| 2259 |
OPTION('i', "interface", "INTERFACE", "Use the specified interface"): |
| 2260 |
if (isempty(opts.arg)) { |
| 2261 |
arg_ifname = mfree(arg_ifname); |
| 2262 |
break; |
| 2263 |
} |
| 2264 |
|
| 2265 |
if (!ifname_valid_full(opts.arg, IFNAME_VALID_ALTERNATIVE|IFNAME_VALID_NUMERIC)) |
| 2266 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Interface name '%s' is not valid.", opts.arg); |
| 2267 |
|
| 2268 |
r = free_and_strdup_warn(&arg_ifname, opts.arg); |
| 2269 |
if (r < 0) |
| 2270 |
return r; |
| 2271 |
|
| 2272 |
break; |
| 2273 |
|
| 2274 |
OPTION_LONG("refresh", "SEC", "Set token refresh time"): { |
| 2275 |
if (isempty(opts.arg)) { |
| 2276 |
arg_refresh_usec = REFRESH_USEC_DEFAULT; |
| 2277 |
break; |
| 2278 |
} |
| 2279 |
|
| 2280 |
usec_t t; |
| 2281 |
r = parse_sec(opts.arg, &t); |
| 2282 |
if (r < 0) |
| 2283 |
return log_error_errno(r, "Failed to parse refresh timeout: %s", opts.arg); |
| 2284 |
if (t < REFRESH_USEC_MIN) { |
| 2285 |
log_info("Increasing specified refresh time to %s, lower values are not supported.", |
| 2286 |
FORMAT_TIMESPAN(REFRESH_USEC_MIN, 0)); |
| 2287 |
arg_refresh_usec = REFRESH_USEC_MIN; |
| 2288 |
} else |
| 2289 |
arg_refresh_usec = t; |
| 2290 |
break; |
| 2291 |
} |
| 2292 |
|
| 2293 |
OPTION_LONG("fwmark", "INTEGER", "Choose firewall mark for HTTP traffic"): |
| 2294 |
if (isempty(opts.arg)) { |
| 2295 |
arg_fwmark_set = false; |
| 2296 |
break; |
| 2297 |
} |
| 2298 |
|
| 2299 |
if (streq(opts.arg, "default")) { |
| 2300 |
arg_fwmark = FWMARK_DEFAULT; |
| 2301 |
arg_fwmark_set = true; |
| 2302 |
break; |
| 2303 |
} |
| 2304 |
|
| 2305 |
r = safe_atou32(opts.arg, &arg_fwmark); |
| 2306 |
if (r < 0) |
| 2307 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse --fwmark= parameter: %s", opts.arg); |
| 2308 |
|
| 2309 |
arg_fwmark_set = true; |
| 2310 |
break; |
| 2311 |
|
| 2312 |
OPTION_LONG("cache", "BOOL", "Enable/disable cache use"): |
| 2313 |
r = parse_boolean_argument("--cache", opts.arg, &arg_cache); |
| 2314 |
if (r < 0) |
| 2315 |
return r; |
| 2316 |
break; |
| 2317 |
|
| 2318 |
OPTION_LONG("wait", "BOOL", "Whether to wait for connectivity"): |
| 2319 |
r = parse_boolean_argument("--wait", opts.arg, &arg_wait); |
| 2320 |
if (r < 0) |
| 2321 |
return r; |
| 2322 |
break; |
| 2323 |
|
| 2324 |
OPTION_SHORT('w', NULL, "Same as --wait=yes"): |
| 2325 |
arg_wait = true; |
| 2326 |
break; |
| 2327 |
|
| 2328 |
OPTION('K', "well-known", "KEY", "Select well-known key"): { |
| 2329 |
if (isempty(opts.arg)) { |
| 2330 |
arg_well_known = _IMDS_WELL_KNOWN_INVALID; |
| 2331 |
break; |
| 2332 |
} |
| 2333 |
|
| 2334 |
ImdsWellKnown wk = imds_well_known_from_string(opts.arg); |
| 2335 |
if (wk < 0) |
| 2336 |
return log_error_errno(wk, "Failed to parse --well-known= parameter: %m"); |
| 2337 |
|
| 2338 |
arg_well_known = wk; |
| 2339 |
break; |
| 2340 |
} |
| 2341 |
|
| 2342 |
OPTION_LONG("setup-network", NULL, "Generate .network and .rr files"): |
| 2343 |
arg_setup_network = true; |
| 2344 |
break; |
| 2345 |
|
| 2346 |
/* The following all configure endpoint information explicitly */ |
| 2347 |
OPTION_GROUP("Manual Endpoint Configuration"): {} |
| 2348 |
|
| 2349 |
OPTION_LONG("vendor", "VENDOR", "Specify IMDS vendor literally"): |
| 2350 |
if (isempty(opts.arg)) { |
| 2351 |
arg_vendor = mfree(arg_vendor); |
| 2352 |
break; |
| 2353 |
} |
| 2354 |
|
| 2355 |
r = free_and_strdup_warn(&arg_vendor, opts.arg); |
| 2356 |
if (r < 0) |
| 2357 |
return r; |
| 2358 |
break; |
| 2359 |
|
| 2360 |
OPTION_LONG("token-url", "URL", "URL for acquiring token"): |
| 2361 |
if (isempty(opts.arg)) { |
| 2362 |
arg_token_url = mfree(arg_token_url); |
| 2363 |
break; |
| 2364 |
} |
| 2365 |
|
| 2366 |
if (!http_url_is_valid(opts.arg)) |
| 2367 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid URL: %s", opts.arg); |
| 2368 |
|
| 2369 |
r = free_and_strdup_warn(&arg_token_url, opts.arg); |
| 2370 |
if (r < 0) |
| 2371 |
return r; |
| 2372 |
break; |
| 2373 |
|
| 2374 |
OPTION_LONG("refresh-header-name", "NAME", "Header name for passing refresh time"): |
| 2375 |
if (isempty(opts.arg)) { |
| 2376 |
arg_refresh_header_name = mfree(arg_refresh_header_name); |
| 2377 |
break; |
| 2378 |
} |
| 2379 |
|
| 2380 |
if (!http_header_name_valid(opts.arg)) |
| 2381 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid HTTP header name: %s", opts.arg); |
| 2382 |
|
| 2383 |
r = free_and_strdup_warn(&arg_refresh_header_name, opts.arg); |
| 2384 |
if (r < 0) |
| 2385 |
return r; |
| 2386 |
break; |
| 2387 |
|
| 2388 |
OPTION_LONG("data-url", "URL", "Base URL for acquiring data"): |
| 2389 |
if (isempty(opts.arg)) { |
| 2390 |
arg_data_url = mfree(arg_data_url); |
| 2391 |
break; |
| 2392 |
} |
| 2393 |
|
| 2394 |
if (!http_url_is_valid(opts.arg)) |
| 2395 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid URL: %s", opts.arg); |
| 2396 |
|
| 2397 |
r = free_and_strdup_warn(&arg_data_url, opts.arg); |
| 2398 |
if (r < 0) |
| 2399 |
return r; |
| 2400 |
break; |
| 2401 |
|
| 2402 |
OPTION_LONG("data-url-suffix", "STRING", "Suffix to append to data URL"): |
| 2403 |
if (isempty(opts.arg)) { |
| 2404 |
arg_data_url_suffix = mfree(arg_data_url_suffix); |
| 2405 |
break; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if (!ascii_is_valid(opts.arg) || string_has_cc(opts.arg, /* ok= */ NULL)) |
| 2409 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid URL suffix: %s", opts.arg); |
| 2410 |
|
| 2411 |
r = free_and_strdup_warn(&arg_data_url_suffix, opts.arg); |
| 2412 |
if (r < 0) |
| 2413 |
return r; |
| 2414 |
break; |
| 2415 |
|
| 2416 |
OPTION_LONG("token-header-name", "NAME", "Header name for passing token string"): |
| 2417 |
if (isempty(opts.arg)) { |
| 2418 |
arg_token_header_name = mfree(arg_token_header_name); |
| 2419 |
break; |
| 2420 |
} |
| 2421 |
|
| 2422 |
if (!http_header_name_valid(opts.arg)) |
| 2423 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid HTTP header name: %s", opts.arg); |
| 2424 |
|
| 2425 |
r = free_and_strdup_warn(&arg_token_header_name, opts.arg); |
| 2426 |
if (r < 0) |
| 2427 |
return r; |
| 2428 |
break; |
| 2429 |
|
| 2430 |
OPTION_LONG("extra-header", "NAME: VALUE", "Additional header to pass to data transfer"): |
| 2431 |
if (isempty(opts.arg)) { |
| 2432 |
arg_extra_header = strv_free(arg_extra_header); |
| 2433 |
break; |
| 2434 |
} |
| 2435 |
|
| 2436 |
if (!http_header_valid(opts.arg)) |
| 2437 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid HTTP header: %s", opts.arg); |
| 2438 |
|
| 2439 |
if (strv_extend(&arg_extra_header, opts.arg) < 0) |
| 2440 |
return log_oom(); |
| 2441 |
break; |
| 2442 |
|
| 2443 |
OPTION_LONG("address-ipv4", "ADDRESS", "Configure IPv4 address of the IMDS server"): { |
| 2444 |
if (isempty(opts.arg)) { |
| 2445 |
arg_address_ipv4 = (struct in_addr) {}; |
| 2446 |
break; |
| 2447 |
} |
| 2448 |
|
| 2449 |
union in_addr_union u; |
| 2450 |
r = in_addr_from_string(AF_INET, opts.arg, &u); |
| 2451 |
if (r < 0) |
| 2452 |
return log_error_errno(r, "Failed to parse IPv4 address: %s", opts.arg); |
| 2453 |
arg_address_ipv4 = u.in; |
| 2454 |
break; |
| 2455 |
} |
| 2456 |
|
| 2457 |
OPTION_LONG("address-ipv6", "ADDRESS", "Configure IPv6 address of the IMDS server"): { |
| 2458 |
if (isempty(opts.arg)) { |
| 2459 |
arg_address_ipv6 = (struct in6_addr) {}; |
| 2460 |
break; |
| 2461 |
} |
| 2462 |
|
| 2463 |
union in_addr_union u; |
| 2464 |
r = in_addr_from_string(AF_INET6, opts.arg, &u); |
| 2465 |
if (r < 0) |
| 2466 |
return log_error_errno(r, "Failed to parse IPv6 address: %s", opts.arg); |
| 2467 |
arg_address_ipv6 = u.in6; |
| 2468 |
break; |
| 2469 |
} |
| 2470 |
|
| 2471 |
OPTION_LONG("well-known-key", "NAME:KEY", "Configure the location of well-known keys"): { |
| 2472 |
if (isempty(opts.arg)) { |
| 2473 |
for (ImdsWellKnown wk = 0; wk < _IMDS_WELL_KNOWN_MAX; wk++) |
| 2474 |
arg_well_known_key[wk] = mfree(arg_well_known_key[wk]); |
| 2475 |
break; |
| 2476 |
} |
| 2477 |
|
| 2478 |
const char *e = strchr(opts.arg, ':'); |
| 2479 |
if (!e) |
| 2480 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--well-known-key= expects colon separated name and key pairs."); |
| 2481 |
|
| 2482 |
_cleanup_free_ char *name = strndup(opts.arg, e - opts.arg); |
| 2483 |
if (!name) |
| 2484 |
return log_oom(); |
| 2485 |
|
| 2486 |
ImdsWellKnown wk = imds_well_known_from_string(name); |
| 2487 |
if (wk < 0) |
| 2488 |
return log_error_errno(wk, "Failed to parse --well-known-key= argument: %m"); |
| 2489 |
|
| 2490 |
e++; |
| 2491 |
if (!imds_key_is_valid(e)) |
| 2492 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Well known key '%s' is not valid.", e); |
| 2493 |
|
| 2494 |
r = free_and_strdup_warn(arg_well_known_key + wk, e); |
| 2495 |
if (r < 0) |
| 2496 |
return r; |
| 2497 |
break; |
| 2498 |
} |
| 2499 |
} |
| 2500 |
|
| 2501 |
if (arg_vendor || arg_token_url || arg_refresh_header_name || arg_data_url || arg_data_url_suffix || arg_token_header_name || arg_extra_header) |
| 2502 |
arg_endpoint_source = ENDPOINT_USER; |
| 2503 |
|
| 2504 |
r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT); |
| 2505 |
if (r < 0) |
| 2506 |
return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m"); |
| 2507 |
|
| 2508 |
arg_varlink = r; |
| 2509 |
|
| 2510 |
if (!arg_varlink) { |
| 2511 |
char **args = option_parser_get_args(&opts); |
| 2512 |
size_t n_args = option_parser_get_n_args(&opts); |
| 2513 |
|
| 2514 |
if (arg_setup_network) { |
| 2515 |
if (n_args != 0) |
| 2516 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No argument expected."); |
| 2517 |
} else { |
| 2518 |
if (arg_well_known < 0) { |
| 2519 |
/* if no --well-known= parameter was specified we require an argument */ |
| 2520 |
if (n_args != 1) |
| 2521 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "A single argument expected."); |
| 2522 |
} else if (n_args > 1) /* if not, then the additional parameter is optional */ |
| 2523 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "At most a single argument expected."); |
| 2524 |
|
| 2525 |
if (n_args > 0) { |
| 2526 |
if (!imds_key_is_valid(args[0])) |
| 2527 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified IMDS key is not valid, refusing: %s", args[0]); |
| 2528 |
|
| 2529 |
r = free_and_strdup_warn(&arg_key, args[0]); |
| 2530 |
if (r < 0) |
| 2531 |
return r; |
| 2532 |
} |
| 2533 |
} |
| 2534 |
} |
| 2535 |
|
| 2536 |
return 1; |
| 2537 |
} |
| 2538 |
|
| 2539 |
static int device_get_property_ip_address( |
| 2540 |
sd_device *d, |
| 2541 |
const char *name, |
| 2542 |
int family, |
| 2543 |
union in_addr_union *ret) { |
| 2544 |
|
| 2545 |
int r; |
| 2546 |
|
| 2547 |
/* Parses an IP address stored in the udev database for a device */ |
| 2548 |
|
| 2549 |
assert(d); |
| 2550 |
assert(name); |
| 2551 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 2552 |
|
| 2553 |
const char *v = NULL; |
| 2554 |
r = sd_device_get_property_value(d, name, &v); |
| 2555 |
if (r < 0) |
| 2556 |
return r; |
| 2557 |
|
| 2558 |
return in_addr_from_string(family, v, ret); |
| 2559 |
} |
| 2560 |
|
| 2561 |
static const char * const imds_well_known_udev_table[_IMDS_WELL_KNOWN_MAX] = { |
| 2562 |
[IMDS_HOSTNAME] = "IMDS_KEY_HOSTNAME", |
| 2563 |
[IMDS_REGION] = "IMDS_KEY_REGION", |
| 2564 |
[IMDS_ZONE] = "IMDS_KEY_ZONE", |
| 2565 |
[IMDS_IPV4_PUBLIC] = "IMDS_KEY_IPV4_PUBLIC", |
| 2566 |
[IMDS_IPV6_PUBLIC] = "IMDS_KEY_IPV6_PUBLIC", |
| 2567 |
[IMDS_SSH_KEY] = "IMDS_KEY_SSH_KEY", |
| 2568 |
[IMDS_USERDATA] = "IMDS_KEY_USERDATA", |
| 2569 |
[IMDS_USERDATA_BASE] = "IMDS_KEY_USERDATA_BASE", |
| 2570 |
[IMDS_USERDATA_BASE64] = "IMDS_KEY_USERDATA_BASE64", |
| 2571 |
}; |
| 2572 |
|
| 2573 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(imds_well_known_udev, ImdsWellKnown); |
| 2574 |
|
| 2575 |
static int smbios_server_info(void) { |
| 2576 |
int r; |
| 2577 |
|
| 2578 |
/* Acquires IMDS server information from udev/hwdb */ |
| 2579 |
|
| 2580 |
if (arg_endpoint_source >= 0) |
| 2581 |
return 0; |
| 2582 |
|
| 2583 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 2584 |
r = sd_device_new_from_syspath(&d, "/sys/class/dmi/id/"); |
| 2585 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 2586 |
log_debug_errno(r, "Failed to open /sys/class/dmi/id/ device, ignoring: %m"); |
| 2587 |
return 0; |
| 2588 |
} |
| 2589 |
if (r < 0) |
| 2590 |
return log_error_errno(r, "Failed to open /sys/class/dmi/id/ device: %m"); |
| 2591 |
|
| 2592 |
const char *vendor; |
| 2593 |
r = sd_device_get_property_value(d, "IMDS_VENDOR", &vendor); |
| 2594 |
if (r == -ENOENT) { |
| 2595 |
log_debug_errno(r, "IMDS_VENDOR= property not set on DMI device, skipping."); |
| 2596 |
return 0; |
| 2597 |
} |
| 2598 |
if (r < 0) |
| 2599 |
return log_error_errno(r, "Failed to read IMDS_VENDOR= property of DMI device: %m"); |
| 2600 |
|
| 2601 |
log_debug("Detected IMDS vendor support '%s'.", vendor); |
| 2602 |
|
| 2603 |
r = free_and_strdup_warn(&arg_vendor, vendor); |
| 2604 |
if (r < 0) |
| 2605 |
return r; |
| 2606 |
|
| 2607 |
struct { |
| 2608 |
const char *property; |
| 2609 |
char **variable; |
| 2610 |
} table[] = { |
| 2611 |
{ "IMDS_TOKEN_URL", &arg_token_url }, |
| 2612 |
{ "IMDS_REFRESH_HEADER_NAME", &arg_refresh_header_name }, |
| 2613 |
{ "IMDS_DATA_URL", &arg_data_url }, |
| 2614 |
{ "IMDS_DATA_URL_SUFFIX", &arg_data_url_suffix }, |
| 2615 |
{ "IMDS_TOKEN_HEADER_NAME", &arg_token_header_name }, |
| 2616 |
}; |
| 2617 |
|
| 2618 |
FOREACH_ELEMENT(i, table) { |
| 2619 |
const char *v = NULL; |
| 2620 |
|
| 2621 |
r = sd_device_get_property_value(d, i->property, &v); |
| 2622 |
if (r < 0 && r != -ENOENT) |
| 2623 |
return log_error_errno(r, "Failed to read property '%s' of DMI: %m", i->property); |
| 2624 |
|
| 2625 |
r = free_and_strdup_warn(i->variable, v); |
| 2626 |
if (r < 0) |
| 2627 |
return r; |
| 2628 |
} |
| 2629 |
|
| 2630 |
for (size_t i = 0; i < 64U; i++) { |
| 2631 |
_cleanup_free_ char *property = NULL; |
| 2632 |
const char *p = NULL; |
| 2633 |
if (i > 0) { |
| 2634 |
if (asprintf(&property, "IMDS_EXTRA_HEADER%zu", i + 1) < 0) |
| 2635 |
return log_oom(); |
| 2636 |
p = property; |
| 2637 |
} else |
| 2638 |
p = "IMDS_EXTRA_HEADER"; |
| 2639 |
|
| 2640 |
const char *v = NULL; |
| 2641 |
r = sd_device_get_property_value(d, p, &v); |
| 2642 |
if (r < 0 && r != -ENOENT) |
| 2643 |
return log_error_errno(r, "Failed to read property '%s' of DMI: %m", p); |
| 2644 |
|
| 2645 |
if (v) |
| 2646 |
if (strv_extend(&arg_extra_header, v) < 0) |
| 2647 |
return log_oom(); |
| 2648 |
} |
| 2649 |
|
| 2650 |
union in_addr_union u; |
| 2651 |
r = device_get_property_ip_address(d, "IMDS_ADDRESS_IPV4", AF_INET, &u); |
| 2652 |
if (r < 0 && r != -ENOENT) |
| 2653 |
return log_error_errno(r, "Failed to read property 'IMDS_ADDRESS_IPV4' of DMI: %m"); |
| 2654 |
else if (r >= 0) |
| 2655 |
arg_address_ipv4 = u.in; |
| 2656 |
|
| 2657 |
r = device_get_property_ip_address(d, "IMDS_ADDRESS_IPV6", AF_INET6, &u); |
| 2658 |
if (r < 0 && r != -ENOENT) |
| 2659 |
return log_error_errno(r, "Failed to read property 'IMDS_ADDRESS_IPV6' of DMI: %m"); |
| 2660 |
else if (r >= 0) |
| 2661 |
arg_address_ipv6 = u.in6; |
| 2662 |
|
| 2663 |
for (ImdsWellKnown k = 0; k < _IMDS_WELL_KNOWN_MAX; k++) { |
| 2664 |
const char *p = imds_well_known_udev_to_string(k); |
| 2665 |
if (!p) |
| 2666 |
continue; |
| 2667 |
|
| 2668 |
const char *v = NULL; |
| 2669 |
r = sd_device_get_property_value(d, p, &v); |
| 2670 |
if (r < 0 && r != -ENOENT) |
| 2671 |
return log_error_errno(r, "Failed to read property '%s' of DMI: %m", p); |
| 2672 |
|
| 2673 |
r = free_and_strdup_warn(arg_well_known_key + k, v); |
| 2674 |
if (r < 0) |
| 2675 |
return r; |
| 2676 |
} |
| 2677 |
|
| 2678 |
log_debug("IMDS endpoint data set from SMBIOS device."); |
| 2679 |
arg_endpoint_source = ENDPOINT_UDEV; |
| 2680 |
return 0; |
| 2681 |
} |
| 2682 |
|
| 2683 |
static int secure_getenv_ip_address( |
| 2684 |
const char *name, |
| 2685 |
int family, |
| 2686 |
union in_addr_union *ret) { |
| 2687 |
|
| 2688 |
assert(name); |
| 2689 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 2690 |
|
| 2691 |
/* Parses an IP address specified in an environment variable */ |
| 2692 |
|
| 2693 |
const char *e = secure_getenv(name); |
| 2694 |
if (!e) |
| 2695 |
return -ENXIO; |
| 2696 |
|
| 2697 |
return in_addr_from_string(family, e, ret); |
| 2698 |
} |
| 2699 |
|
| 2700 |
static const char * const imds_well_known_environment_table[_IMDS_WELL_KNOWN_MAX] = { |
| 2701 |
[IMDS_HOSTNAME] = "SYSTEMD_IMDS_KEY_HOSTNAME", |
| 2702 |
[IMDS_REGION] = "SYSTEMD_IMDS_KEY_REGION", |
| 2703 |
[IMDS_ZONE] = "SYSTEMD_IMDS_KEY_ZONE", |
| 2704 |
[IMDS_IPV4_PUBLIC] = "SYSTEMD_IMDS_KEY_IPV4_PUBLIC", |
| 2705 |
[IMDS_IPV6_PUBLIC] = "SYSTEMD_IMDS_KEY_IPV6_PUBLIC", |
| 2706 |
[IMDS_SSH_KEY] = "SYSTEMD_IMDS_KEY_SSH_KEY", |
| 2707 |
[IMDS_USERDATA] = "SYSTEMD_IMDS_KEY_USERDATA", |
| 2708 |
[IMDS_USERDATA_BASE] = "SYSTEMD_IMDS_KEY_USERDATA_BASE", |
| 2709 |
[IMDS_USERDATA_BASE64] = "SYSTEMD_IMDS_KEY_USERDATA_BASE64", |
| 2710 |
}; |
| 2711 |
|
| 2712 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(imds_well_known_environment, ImdsWellKnown); |
| 2713 |
|
| 2714 |
static int environment_server_info(void) { |
| 2715 |
int r; |
| 2716 |
|
| 2717 |
/* Acquires IMDS endpoint info from environment variables */ |
| 2718 |
|
| 2719 |
if (arg_endpoint_source >= 0) |
| 2720 |
return 0; |
| 2721 |
|
| 2722 |
static const struct { |
| 2723 |
const char *name; |
| 2724 |
char **variable; |
| 2725 |
} table[] = { |
| 2726 |
{ "SYSTEMD_IMDS_VENDOR", &arg_vendor }, |
| 2727 |
{ "SYSTEMD_IMDS_TOKEN_URL", &arg_token_url }, |
| 2728 |
{ "SYSTEMD_IMDS_REFRESH_HEADER_NAME", &arg_refresh_header_name }, |
| 2729 |
{ "SYSTEMD_IMDS_DATA_URL", &arg_data_url }, |
| 2730 |
{ "SYSTEMD_IMDS_DATA_URL_SUFFIX", &arg_data_url_suffix }, |
| 2731 |
{ "SYSTEMD_IMDS_TOKEN_HEADER_NAME", &arg_token_header_name }, |
| 2732 |
}; |
| 2733 |
|
| 2734 |
FOREACH_ELEMENT(i, table) { |
| 2735 |
const char *e = secure_getenv(i->name); |
| 2736 |
if (!e) |
| 2737 |
continue; |
| 2738 |
|
| 2739 |
r = free_and_strdup_warn(i->variable, e); |
| 2740 |
if (r < 0) |
| 2741 |
return r; |
| 2742 |
|
| 2743 |
arg_endpoint_source = ENDPOINT_ENVIRONMENT; |
| 2744 |
} |
| 2745 |
|
| 2746 |
for (unsigned u = 1; u < 64; u++) { |
| 2747 |
_cleanup_free_ char *name = NULL; |
| 2748 |
|
| 2749 |
if (u > 1 && asprintf(&name, "SYSTEMD_IMDS_EXTRA_HEADER%u", u) < 0) |
| 2750 |
return log_oom(); |
| 2751 |
|
| 2752 |
const char *e = secure_getenv(name ?: "SYSTEMD_IMDS_EXTRA_HEADER"); |
| 2753 |
if (!e) |
| 2754 |
break; |
| 2755 |
|
| 2756 |
if (strv_extend(&arg_extra_header, e) < 0) |
| 2757 |
return log_oom(); |
| 2758 |
|
| 2759 |
arg_endpoint_source = ENDPOINT_ENVIRONMENT; |
| 2760 |
} |
| 2761 |
|
| 2762 |
union in_addr_union u; |
| 2763 |
r = secure_getenv_ip_address("SYSTEMD_IMDS_ADDRESS_IPV4", AF_INET, &u); |
| 2764 |
if (r < 0 && r != -ENXIO) |
| 2765 |
return log_error_errno(r, "Failed read IPv4 address from environment variable 'SYSTEMD_IMDS_ADDRESS_IPV4': %m"); |
| 2766 |
if (r >= 0) { |
| 2767 |
arg_address_ipv4 = u.in; |
| 2768 |
arg_endpoint_source = ENDPOINT_ENVIRONMENT; |
| 2769 |
} |
| 2770 |
|
| 2771 |
r = secure_getenv_ip_address("SYSTEMD_IMDS_ADDRESS_IPV6", AF_INET6, &u); |
| 2772 |
if (r < 0 && r != -ENXIO) |
| 2773 |
return log_error_errno(r, "Failed read IPv6 address from environment variable 'SYSTEMD_IMDS_ADDRESS_IPV6': %m"); |
| 2774 |
if (r >= 0) { |
| 2775 |
arg_address_ipv6 = u.in6; |
| 2776 |
arg_endpoint_source = ENDPOINT_ENVIRONMENT; |
| 2777 |
} |
| 2778 |
|
| 2779 |
for (ImdsWellKnown k = 0; k < _IMDS_WELL_KNOWN_MAX; k++) { |
| 2780 |
const char *n = imds_well_known_environment_to_string(k); |
| 2781 |
if (!n) |
| 2782 |
continue; |
| 2783 |
|
| 2784 |
const char *e = secure_getenv(n); |
| 2785 |
if (!e) |
| 2786 |
continue; |
| 2787 |
|
| 2788 |
r = free_and_strdup_warn(arg_well_known_key + k, e); |
| 2789 |
if (r < 0) |
| 2790 |
return r; |
| 2791 |
|
| 2792 |
arg_endpoint_source = ENDPOINT_ENVIRONMENT; |
| 2793 |
} |
| 2794 |
|
| 2795 |
if (arg_endpoint_source >= 0) |
| 2796 |
log_debug("IMDS endpoint data set from environment."); |
| 2797 |
|
| 2798 |
return 0; |
| 2799 |
} |
| 2800 |
|
| 2801 |
static int read_credential_ip_address( |
| 2802 |
const char *name, |
| 2803 |
int family, |
| 2804 |
union in_addr_union *ret) { |
| 2805 |
|
| 2806 |
int r; |
| 2807 |
|
| 2808 |
assert(name); |
| 2809 |
assert(IN_SET(family, AF_INET, AF_INET6)); |
| 2810 |
|
| 2811 |
/* Parses an IP address specified in a credential */ |
| 2812 |
|
| 2813 |
_cleanup_free_ char *s = NULL; |
| 2814 |
r = read_credential(name, (void**) &s, /* ret_size= */ NULL); |
| 2815 |
if (r < 0) |
| 2816 |
return r; |
| 2817 |
|
| 2818 |
return in_addr_from_string(family, s, ret); |
| 2819 |
} |
| 2820 |
|
| 2821 |
static const char * const imds_well_known_credential_table[_IMDS_WELL_KNOWN_MAX] = { |
| 2822 |
[IMDS_HOSTNAME] = "imds.key_hostname", |
| 2823 |
[IMDS_REGION] = "imds.key_region", |
| 2824 |
[IMDS_ZONE] = "imds.key_zone", |
| 2825 |
[IMDS_IPV4_PUBLIC] = "imds.key_ipv4_public", |
| 2826 |
[IMDS_IPV6_PUBLIC] = "imds.key_ipv6_public", |
| 2827 |
[IMDS_SSH_KEY] = "imds.key_ssh_key", |
| 2828 |
[IMDS_USERDATA] = "imds.key_userdata", |
| 2829 |
[IMDS_USERDATA_BASE] = "imds.key_userdata_base", |
| 2830 |
[IMDS_USERDATA_BASE64] = "imds.key_userdata_base64", |
| 2831 |
}; |
| 2832 |
|
| 2833 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(imds_well_known_credential, ImdsWellKnown); |
| 2834 |
|
| 2835 |
static int credential_server_info(void) { |
| 2836 |
int r; |
| 2837 |
|
| 2838 |
/* Acquires IMDS endpoint info from credentials */ |
| 2839 |
|
| 2840 |
if (arg_endpoint_source >= 0) |
| 2841 |
return 0; |
| 2842 |
|
| 2843 |
static const struct { |
| 2844 |
const char *name; |
| 2845 |
char **variable; |
| 2846 |
} table[] = { |
| 2847 |
{ "imds.vendor", &arg_vendor }, |
| 2848 |
{ "imds.vendor_token", &arg_token_url }, |
| 2849 |
{ "imds.refresh_header_name", &arg_refresh_header_name }, |
| 2850 |
{ "imds.data_url", &arg_data_url }, |
| 2851 |
{ "imds.data_url_suffix", &arg_data_url_suffix }, |
| 2852 |
{ "imds.token_header_name", &arg_token_header_name }, |
| 2853 |
}; |
| 2854 |
|
| 2855 |
FOREACH_ELEMENT(i, table) { |
| 2856 |
_cleanup_free_ char *s = NULL; |
| 2857 |
|
| 2858 |
r = read_credential(i->name, (void**) &s, /* ret_size= */ NULL); |
| 2859 |
if (IN_SET(r, -ENOENT, -ENXIO)) |
| 2860 |
continue; |
| 2861 |
if (r < 0) { |
| 2862 |
log_warning_errno(r, "Failed to read credential '%s', ignoring: %m", i->name); |
| 2863 |
continue; |
| 2864 |
} |
| 2865 |
|
| 2866 |
r = free_and_strdup_warn(i->variable, s); |
| 2867 |
if (r < 0) |
| 2868 |
return r; |
| 2869 |
|
| 2870 |
arg_endpoint_source = ENDPOINT_CREDENTIALS; |
| 2871 |
} |
| 2872 |
|
| 2873 |
for (unsigned u = 1; u < 64; u++) { |
| 2874 |
_cleanup_free_ char *name = NULL; |
| 2875 |
if (u > 1 && asprintf(&name, "imds.extra_header%u", u) < 0) |
| 2876 |
return log_oom(); |
| 2877 |
|
| 2878 |
const char *n = name ?: "imds.extra_header"; |
| 2879 |
|
| 2880 |
_cleanup_free_ char *s = NULL; |
| 2881 |
r = read_credential(n, (void**) &s, /* ret_size= */ NULL); |
| 2882 |
if (IN_SET(r, -ENOENT, -ENXIO)) |
| 2883 |
continue; |
| 2884 |
if (r < 0) { |
| 2885 |
log_warning_errno(r, "Failed to read credential '%s', ignoring: %m", n); |
| 2886 |
continue; |
| 2887 |
} |
| 2888 |
|
| 2889 |
if (strv_extend(&arg_extra_header, s) < 0) |
| 2890 |
return log_oom(); |
| 2891 |
|
| 2892 |
arg_endpoint_source = ENDPOINT_CREDENTIALS; |
| 2893 |
} |
| 2894 |
|
| 2895 |
union in_addr_union u; |
| 2896 |
r = read_credential_ip_address("imds.address_ipv4", AF_INET, &u); |
| 2897 |
if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO)) |
| 2898 |
log_warning_errno(r, "Failed read IPv4 address from credential 'imds.address_ipv4', ignoring: %m"); |
| 2899 |
if (r >= 0) { |
| 2900 |
arg_address_ipv4 = u.in; |
| 2901 |
arg_endpoint_source = ENDPOINT_CREDENTIALS; |
| 2902 |
} |
| 2903 |
|
| 2904 |
r = read_credential_ip_address("imds.address_ipv6", AF_INET6, &u); |
| 2905 |
if (r < 0 && !IN_SET(r, -ENOENT, -ENXIO)) |
| 2906 |
log_warning_errno(r, "Failed read IPv6 address from credential 'imds.address_ipv6', ignoring: %m"); |
| 2907 |
if (r >= 0) { |
| 2908 |
arg_address_ipv6 = u.in6; |
| 2909 |
arg_endpoint_source = ENDPOINT_CREDENTIALS; |
| 2910 |
} |
| 2911 |
|
| 2912 |
for (ImdsWellKnown k = 0; k < _IMDS_WELL_KNOWN_MAX; k++) { |
| 2913 |
const char *n = imds_well_known_credential_to_string(k); |
| 2914 |
if (!n) |
| 2915 |
continue; |
| 2916 |
|
| 2917 |
_cleanup_free_ char *s = NULL; |
| 2918 |
r = read_credential(n, (void**) &s, /* ret_size= */ NULL); |
| 2919 |
if (IN_SET(r, -ENOENT, -ENXIO)) |
| 2920 |
continue; |
| 2921 |
if (r < 0) { |
| 2922 |
log_warning_errno(r, "Failed to read credential '%s', ignoring: %m", n); |
| 2923 |
continue; |
| 2924 |
} |
| 2925 |
|
| 2926 |
free_and_replace(arg_well_known_key[k], s); |
| 2927 |
arg_endpoint_source = ENDPOINT_CREDENTIALS; |
| 2928 |
} |
| 2929 |
|
| 2930 |
if (arg_endpoint_source >= 0) |
| 2931 |
log_debug("IMDS endpoint data set from credentials."); |
| 2932 |
|
| 2933 |
return 0; |
| 2934 |
} |
| 2935 |
|
| 2936 |
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
| 2937 |
int r; |
| 2938 |
|
| 2939 |
assert(key); |
| 2940 |
|
| 2941 |
/* Called for each kernel command line option. */ |
| 2942 |
|
| 2943 |
if (proc_cmdline_key_streq(key, "systemd.imds.network")) { |
| 2944 |
if (proc_cmdline_value_missing(key, value)) |
| 2945 |
return 0; |
| 2946 |
|
| 2947 |
ImdsNetworkMode m = imds_network_mode_from_string(value); |
| 2948 |
if (m < 0) |
| 2949 |
return log_warning_errno(m, "Failed to parse systemd.imds.network= value: %m"); |
| 2950 |
|
| 2951 |
arg_network_mode = m; |
| 2952 |
return 0; |
| 2953 |
} |
| 2954 |
|
| 2955 |
/* The other kernel command line options configured IMDS endpoint data. We'll only check it if no |
| 2956 |
* other configuration source for it has been used */ |
| 2957 |
if (arg_endpoint_source >= 0 && arg_endpoint_source != ENDPOINT_PROC_CMDLINE) |
| 2958 |
return 0; |
| 2959 |
|
| 2960 |
static const struct { |
| 2961 |
const char *key; |
| 2962 |
char **variable; |
| 2963 |
} table[] = { |
| 2964 |
{ "systemd.imds.vendor", &arg_vendor }, |
| 2965 |
{ "systemd.imds.token_url", &arg_token_url }, |
| 2966 |
{ "systemd.imds.refresh_header_name", &arg_refresh_header_name }, |
| 2967 |
{ "systemd.imds.data_url", &arg_data_url }, |
| 2968 |
{ "systemd.imds.data_url_suffix", &arg_data_url_suffix }, |
| 2969 |
{ "systemd.imds.token_header_name", &arg_token_header_name }, |
| 2970 |
}; |
| 2971 |
|
| 2972 |
FOREACH_ELEMENT(i, table) { |
| 2973 |
if (!proc_cmdline_key_streq(key, i->key)) |
| 2974 |
continue; |
| 2975 |
|
| 2976 |
if (proc_cmdline_value_missing(key, value)) |
| 2977 |
return 0; |
| 2978 |
|
| 2979 |
r = free_and_strdup_warn(i->variable, value); |
| 2980 |
if (r < 0) |
| 2981 |
return r; |
| 2982 |
|
| 2983 |
arg_endpoint_source = ENDPOINT_PROC_CMDLINE; |
| 2984 |
return 0; |
| 2985 |
} |
| 2986 |
|
| 2987 |
if (proc_cmdline_key_streq(key, "systemd.imds.extra_header")) { |
| 2988 |
if (proc_cmdline_value_missing(key, value)) |
| 2989 |
return 0; |
| 2990 |
|
| 2991 |
if (isempty(value)) |
| 2992 |
arg_extra_header = strv_free(arg_extra_header); |
| 2993 |
else if (strv_extend(&arg_extra_header, value) < 0) |
| 2994 |
return log_oom(); |
| 2995 |
|
| 2996 |
arg_endpoint_source = ENDPOINT_PROC_CMDLINE; |
| 2997 |
return 0; |
| 2998 |
} |
| 2999 |
|
| 3000 |
if (proc_cmdline_key_streq(key, "systemd.imds.address_ipv4")) { |
| 3001 |
if (proc_cmdline_value_missing(key, value)) |
| 3002 |
return 0; |
| 3003 |
|
| 3004 |
union in_addr_union u; |
| 3005 |
r = in_addr_from_string(AF_INET, value, &u); |
| 3006 |
if (r < 0) |
| 3007 |
return log_error_errno(r, "Failed to parse 'systemd.imds.address_ipv4=' parameter: %s", value); |
| 3008 |
|
| 3009 |
arg_address_ipv4 = u.in; |
| 3010 |
arg_endpoint_source = ENDPOINT_PROC_CMDLINE; |
| 3011 |
return 0; |
| 3012 |
} |
| 3013 |
|
| 3014 |
if (proc_cmdline_key_streq(key, "systemd.imds.address_ipv6")) { |
| 3015 |
if (proc_cmdline_value_missing(key, value)) |
| 3016 |
return 0; |
| 3017 |
|
| 3018 |
union in_addr_union u; |
| 3019 |
r = in_addr_from_string(AF_INET6, value, &u); |
| 3020 |
if (r < 0) |
| 3021 |
return log_error_errno(r, "Failed to parse 'systemd.imds.address_ipv6=' parameter: %s", value); |
| 3022 |
|
| 3023 |
arg_address_ipv6 = u.in6; |
| 3024 |
arg_endpoint_source = ENDPOINT_PROC_CMDLINE; |
| 3025 |
return 0; |
| 3026 |
} |
| 3027 |
|
| 3028 |
static const char * const well_known_table[_IMDS_WELL_KNOWN_MAX] = { |
| 3029 |
[IMDS_HOSTNAME] = "systemd.imds.key.hostname", |
| 3030 |
[IMDS_REGION] = "systemd.imds.key.region", |
| 3031 |
[IMDS_ZONE] = "systemd.imds.key.zone", |
| 3032 |
[IMDS_IPV4_PUBLIC] = "systemd.imds.key.ipv4_public", |
| 3033 |
[IMDS_IPV6_PUBLIC] = "systemd.imds.key.ipv6_public", |
| 3034 |
[IMDS_SSH_KEY] = "systemd.imds.key.ssh_key", |
| 3035 |
[IMDS_USERDATA] = "systemd.imds.key.userdata", |
| 3036 |
[IMDS_USERDATA_BASE] = "systemd.imds.key.userdata_base", |
| 3037 |
[IMDS_USERDATA_BASE64] = "systemd.imds.key.userdata_base64", |
| 3038 |
}; |
| 3039 |
|
| 3040 |
for (ImdsWellKnown wk = 0; wk < _IMDS_WELL_KNOWN_MAX; wk++) { |
| 3041 |
const char *k = well_known_table[wk]; |
| 3042 |
if (!k) |
| 3043 |
continue; |
| 3044 |
|
| 3045 |
if (!proc_cmdline_key_streq(key, k)) |
| 3046 |
continue; |
| 3047 |
|
| 3048 |
r = free_and_strdup_warn(arg_well_known_key + wk, value); |
| 3049 |
if (r < 0) |
| 3050 |
return r; |
| 3051 |
|
| 3052 |
arg_endpoint_source = ENDPOINT_PROC_CMDLINE; |
| 3053 |
return 0; |
| 3054 |
} |
| 3055 |
|
| 3056 |
return 0; |
| 3057 |
} |
| 3058 |
|
| 3059 |
static int run(int argc, char* argv[]) { |
| 3060 |
int r; |
| 3061 |
|
| 3062 |
log_setup(); |
| 3063 |
|
| 3064 |
r = parse_argv(argc, argv); |
| 3065 |
if (r <= 0) |
| 3066 |
return r; |
| 3067 |
|
| 3068 |
r = DLOPEN_CURL(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 3069 |
if (r < 0) |
| 3070 |
return r; |
| 3071 |
|
| 3072 |
r = environment_server_info(); |
| 3073 |
if (r < 0) |
| 3074 |
return r; |
| 3075 |
|
| 3076 |
r = proc_cmdline_parse(parse_proc_cmdline_item, /* userdata= */ NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
| 3077 |
if (r < 0) |
| 3078 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 3079 |
|
| 3080 |
r = credential_server_info(); |
| 3081 |
if (r < 0) |
| 3082 |
return r; |
| 3083 |
|
| 3084 |
r = smbios_server_info(); |
| 3085 |
if (r < 0) |
| 3086 |
return r; |
| 3087 |
|
| 3088 |
if (arg_varlink) |
| 3089 |
return vl_server(); |
| 3090 |
|
| 3091 |
return cmdline_run(); |
| 3092 |
} |
| 3093 |
|
| 3094 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |