HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/imds/imdsd.c

121,943 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: d4e5b23937d568a82c995f2f37217f1f28097dc8

Lines 1–1000 of 3,099

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <net/if.h>
#include <sys/xattr.h>
#include <unistd.h>
 
#include "sd-bus.h"
#include "sd-device.h"
#include "sd-event.h"
#include "sd-json.h"
#include "sd-netlink.h"
 
#include "alloc-util.h"
#include "build-path.h"
#include "build.h"
#include "bus-polkit.h"
#include "chase.h"
#include "copy.h"
#include "creds-util.h"
#include "curl-util.h"
#include "device-private.h"
#include "dlopen-note.h"
#include "dns-rr.h"
#include "errno-util.h"
#include "escape.h"
#include "event-util.h"
#include "exit-status.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-ifname.h"
#include "hash-funcs.h"
#include "hashmap.h"
#include "imds-util.h"
#include "in-addr-util.h"
#include "io-util.h"
#include "iovec-util.h"
#include "json-util.h"
#include "log.h"
#include "main-func.h"
#include "netlink-util.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
#include "socket-util.h"
#include "string-util.h"
#include "strv.h"
#include "sysctl-util.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "utf8.h"
#include "varlink-io.systemd.InstanceMetadata.h"
#include "varlink-util.h"
#include "verbs.h"
#include "web-util.h"
#include "xattr-util.h"
 
/* This implements a client to the AWS' and Azure's "Instance Metadata Service", as well as GCP's "VM
 * Metadata", i.e.:
 *
 * https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
 * https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service
 * https://docs.cloud.google.com/compute/docs/metadata/overview
 * https://docs.hetzner.cloud/reference/cloud#description/server-metadata
 *
 * Some notes:
 *   - IMDS service are heavily rate-limited, and hence we want to centralize requests in one place and cache
 *   - In order to isolate IMDS access this expects that traffic to the IMDS address 169.254.169.254 is
 *     generally prohibited (via a prohibit route), but our service uses fwmark 0x7FFF0815, which (via source
 *     routing) can bypass this route.
 *   - To be robust to situations with multiple interfaces, if we have no hint which interface we shall use,
 *     we'll fork our own binary off, once for each interface, and communicate to it via Varlink.
 *   - This is supposed to run under its own UID, but with CAP_NET_ADMIN held (since we want to use
 *     IP_UNICAST_IF + SO_MARK)
 *   - This daemon either be invoked manually from the command line, to do a single request, mostly for
 *     debugging purposes. Or it can be invoked as a Varlink service, which is the primary intended mode of
 *     operation.
 */
 
#define TOKEN_SIZE_MAX (4096U)
#define DATA_SIZE_MAX (4*1024*1024U)
#define FWMARK_DEFAULT UINT32_C(0x7FFF0815)
#define REFRESH_USEC_DEFAULT (15U * USEC_PER_MINUTE)
#define REFRESH_USEC_MIN (1U * USEC_PER_SEC)
#define DIRECT_OVERALL_TIMEOUT_USEC (40U * USEC_PER_SEC) /* a bit shorter than the default D-Bus/Varlink method call time-out) */
#define INDIRECT_OVERALL_TIMEOUT_USEC (DIRECT_OVERALL_TIMEOUT_USEC + 5U * USEC_PER_SEC)
#define RETRY_MIN_USEC (20U * USEC_PER_MSEC)
#define RETRY_MAX_USEC (3U * USEC_PER_SEC)
#define RETRY_MAX 10U
 
/* Which endpoint configuration source has been used, in order of preference */
typedef enum EndpointSource {
        ENDPOINT_USER,           /* Explicit command line options */
        ENDPOINT_ENVIRONMENT,    /* Fallback environment variables */
        ENDPOINT_PROC_CMDLINE,   /* Acquired via kernel command line */
        ENDPOINT_CREDENTIALS,    /* Acquired via system credentials */
        ENDPOINT_UDEV,           /* Acquired via udev SMBIOS object */
        _ENDPOINT_SOURCE_MAX,
        _ENDPOINT_SOURCE_INVALID = -EINVAL,
} EndpointSource;
 
static char *arg_ifname = NULL;
static usec_t arg_refresh_usec = REFRESH_USEC_DEFAULT;
static uint32_t arg_fwmark = FWMARK_DEFAULT;
static bool arg_fwmark_set = true;
static ImdsWellKnown arg_well_known = _IMDS_WELL_KNOWN_INVALID;
static char* arg_key = NULL;
static bool arg_cache = true;
static bool arg_wait = false;
static bool arg_varlink = false;
static ImdsNetworkMode arg_network_mode = IMDS_NETWORK_DEFAULT;
static bool arg_setup_network = false;
 
/* The follow configure the IMDS service endpoint details */
static EndpointSource arg_endpoint_source = _ENDPOINT_SOURCE_INVALID;
static char *arg_vendor = NULL;
static char *arg_token_url = NULL;
static char *arg_refresh_header_name = NULL;
static char *arg_data_url = NULL;
static char *arg_data_url_suffix = NULL;
static char *arg_token_header_name = NULL;
static char **arg_extra_header = NULL;
static struct in_addr arg_address_ipv4 = {};
static struct in6_addr arg_address_ipv6 = {};
static char *arg_well_known_key[_IMDS_WELL_KNOWN_MAX] = {};
 
static void imds_well_known_key_free(typeof(arg_well_known_key) *array) {
        free_many_charp(*array, _IMDS_WELL_KNOWN_MAX);
}
 
STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep);
STATIC_DESTRUCTOR_REGISTER(arg_key, freep);
STATIC_DESTRUCTOR_REGISTER(arg_vendor, freep);
STATIC_DESTRUCTOR_REGISTER(arg_token_url, freep);
STATIC_DESTRUCTOR_REGISTER(arg_refresh_header_name, freep);
STATIC_DESTRUCTOR_REGISTER(arg_data_url, freep);
STATIC_DESTRUCTOR_REGISTER(arg_data_url_suffix, freep);
STATIC_DESTRUCTOR_REGISTER(arg_token_header_name, freep);
STATIC_DESTRUCTOR_REGISTER(arg_extra_header, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_well_known_key, imds_well_known_key_free);
 
COMMAND(
        "systemd-imdsd\0",
        "Low-level IMDS data acquisition.",
        .argspec = "KEY\0",
        .man_pages = "systemd-imdsd@.service(8)\0",
);
 
typedef struct Context Context;
 
typedef struct ChildData {
        /* If there are multiple network interfaces, and we are not sure where to look for things, we'll fork
         * additional instances of ourselves, one for each interface. */
        Context *context;
        int ifindex;
        sd_varlink *link;  /* outgoing varlink connection towards the child */
        bool retry;        /* If true then new information came to light and we should restart the request */
} ChildData;
 
struct Context {
        /* Fields shared between requests (these remain allocated between Varlink requests) */
        sd_event *event;
        sd_netlink *rtnl;
        bool rtnl_attached;
        sd_bus *system_bus;  /* for polkit */
        CurlGlue *glue;
        struct iovec token;  /* token in binary */
        char *token_string;  /* token as string, once complete and validated */
        int cache_dir_fd;
        Hashmap *polkit_registry;
 
        /* Request-specific fields (these get reset whenever we start processing a new Varlink call) */
        int ifindex;
        usec_t timestamp; /* CLOCK_BOOTTIME */
        int cache_fd;
        char *cache_filename, *cache_temporary_filename;
        uint64_t data_size;
        usec_t refresh_usec;
        char *key;
        ImdsWellKnown well_known;
        bool write_stdout;
        struct iovec write_iovec;
        bool cache;
        bool wait;
        sd_varlink *current_link; /* incoming varlink connection we are processing */
        uint32_t fwmark;
        bool fwmark_set;
        sd_event_source *overall_timeout_source;
 
        /* Mode 1 "direct": we go directly to the network (this is done if we know the interface index to
         * use) */
        CurlSlot *slot_token;
        CurlSlot *slot_data;
        struct curl_slist *request_header_token, *request_header_data;
        sd_event_source *retry_source;
        unsigned n_retry;
 
        /* Mode 2 "indirect": we fork off a number of children which go to the network on behalf of us,
         * because we have multiple network interfaces to deal with. */
        Hashmap *child_data;
        sd_netlink_slot *address_change_slot;
};
 
#define CONTEXT_NULL                                    \
        (Context) {                                     \
                .cache_dir_fd = -EBADF,                 \
                .cache_fd = -EBADF,                     \
                .well_known = _IMDS_WELL_KNOWN_INVALID, \
        }
 
/* Log helpers that cap at debug logging if we are operating on behalf of a Varlink client */
#define context_log_errno(c, level, r, fmt, ...)                        \
        log_full_errno((c)->current_link ? LOG_DEBUG : (level), r, fmt, ##__VA_ARGS__)
#define context_log(c, level, fmt, ...)                                 \
        log_full((c)->current_link ? LOG_DEBUG : (level), fmt, ##__VA_ARGS__)
#define context_log_oom(c)                                              \
        (c)->current_link ? log_oom_debug() : log_oom()
 
static int context_acquire_data(Context *c);
static int context_acquire_token(Context *c);
static int context_spawn_child(Context *c, int ifindex, sd_varlink **ret);
 
static ChildData* child_data_free(ChildData *cd) {
        if (!cd)
                return NULL;
 
        if (cd->context)
                hashmap_remove(cd->context->child_data, INT_TO_PTR(cd->ifindex));
 
        sd_varlink_close_unref(cd->link);
        return mfree(cd);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(ChildData*, child_data_free);
 
static void context_reset_token(Context *c) {
        assert(c);
 
        iovec_done(&c->token);
        c->token_string = mfree(c->token_string);
}
 
static void context_flush_token(Context *c) {
 
        if (c->cache_dir_fd >= 0)
                (void) unlinkat(c->cache_dir_fd, "token", /* flags= */ 0);
 
        context_reset_token(c);
}
 
static void context_reset_for_refresh(Context *c) {
        assert(c);
 
        /* Flush out all fields, up to the point we can restart the current request */
 
        c->slot_token = curl_slot_unref(c->slot_token);
        c->slot_data = curl_slot_unref(c->slot_data);
 
        sym_curl_slist_free_all(c->request_header_token);
        c->request_header_token = NULL;
        sym_curl_slist_free_all(c->request_header_data);
        c->request_header_data = NULL;
 
        c->cache_fd = safe_close(c->cache_fd);
        c->cache_filename = mfree(c->cache_filename);
 
        if (c->cache_temporary_filename && c->cache_dir_fd >= 0)
                (void) unlinkat(c->cache_dir_fd, c->cache_temporary_filename, /* flags= */ 0);
 
        c->cache_temporary_filename = mfree(c->cache_temporary_filename);
 
        iovec_done(&c->write_iovec);
 
        c->child_data = hashmap_free(c->child_data);
        c->data_size = 0;
 
        (void) sd_event_source_set_enabled(c->retry_source, SD_EVENT_OFF);
}
 
static void context_reset_full(Context *c) {
        assert(c);
 
        /* Flush out all fields relevant to the current request, comprehensively */
 
        context_reset_for_refresh(c);
        c->key = mfree(c->key);
        c->well_known = _IMDS_WELL_KNOWN_INVALID;
        c->current_link = sd_varlink_unref(c->current_link);
        c->address_change_slot = sd_netlink_slot_unref(c->address_change_slot);
        c->retry_source = sd_event_source_unref(c->retry_source);
        c->overall_timeout_source = sd_event_source_unref(c->overall_timeout_source);
        c->cache_dir_fd = safe_close(c->cache_dir_fd);
}
 
static void context_new_request(Context *c) {
        assert(c);
 
        /* Flush everything out from the previous request */
        context_reset_full(c);
 
        /* Reinitialize settings from defaults. */
        c->ifindex = 0;
        c->timestamp = now(CLOCK_BOOTTIME);
        c->refresh_usec = arg_refresh_usec;
        c->cache = arg_cache;
        c->wait = arg_wait;
        c->fwmark = arg_fwmark;
        c->fwmark_set = arg_fwmark_set;
        c->n_retry = 0;
}
 
static void context_done(Context *c) {
        assert(c);
 
        /* Flush out everything specific to the current request first */
        context_reset_full(c);
        context_reset_token(c);
 
        /* And then also flush out everything shared between requests */
        c->glue = curl_glue_unref(c->glue);
        c->rtnl = sd_netlink_unref(c->rtnl);
        c->event = sd_event_unref(c->event);
        c->polkit_registry = hashmap_free(c->polkit_registry);
        c->system_bus = sd_bus_flush_close_unref(c->system_bus);
}
 
static int context_fail_full(Context *c, int r, const char *varlink_error) {
        assert(c);
        assert(r != 0);
 
        /* Called whenever the current retrieval fails asynchronously. Returns 0 so callers in
         * int-returning paths can `return context_fail_full(...)` directly. */
 
        r = -abs(r);
 
        if (varlink_error)
                context_log_errno(c, LOG_ERR, r, "Operation failed (%s).", varlink_error);
        else
                context_log_errno(c, LOG_ERR, r, "Operation failed (%m).");
 
        /* If we are running in Varlink mode, return the error on the connection */
        if (c->current_link) {
                if (varlink_error)
                        (void) sd_varlink_error(c->current_link, varlink_error, NULL);
                else
                        (void) sd_varlink_error_errno(c->current_link, r);
        } else
                /* Otherwise terminate the whole process. */
                sd_event_exit(c->event, r);
 
        context_reset_full(c);
        return 0;
}
 
static int context_fail(Context *c, int r) {
        return context_fail_full(c, r, /* varlink_error= */ NULL);
}
 
static void context_success(Context *c) {
        int r;
 
        assert(c);
 
        /* Called whenever the current retrieval succeeds asynchronously */
 
        context_log(c, LOG_DEBUG, "Operation succeeded.");
 
        if (c->current_link) {
                r = sd_varlink_replybo(
                                c->current_link,
                                JSON_BUILD_PAIR_IOVEC_BASE64("data", &c->write_iovec),
                                SD_JSON_BUILD_PAIR_CONDITION(c->ifindex > 0, "interface", SD_JSON_BUILD_INTEGER(c->ifindex)));
                if (r < 0)
                        context_log_errno(c, LOG_WARNING, r, "Failed to reply to Varlink call, ignoring: %m");
        } else
                sd_event_exit(c->event, 0);
 
        context_reset_full(c);
}
 
static int setsockopt_callback(void *userdata, curl_socket_t curlfd, curlsocktype purpose) {
        Context *c = ASSERT_PTR(userdata);
        int r;
 
        assert(curlfd >= 0);
 
        if (purpose != CURLSOCKTYPE_IPCXN)
                return CURL_SOCKOPT_OK;
 
        r = socket_set_unicast_if(curlfd, AF_UNSPEC, c->ifindex);
        if (r < 0) {
                context_log_errno(c, LOG_ERR, r, "Failed to bind HTTP socket to interface: %m");
                return CURL_SOCKOPT_ERROR;
        }
 
        if (c->fwmark_set &&
            setsockopt(curlfd, SOL_SOCKET, SO_MARK, &c->fwmark, sizeof(c->fwmark)) < 0) {
                context_log_errno(c, LOG_ERR, errno, "Failed to set firewall mark on HTTP socket: %m");
                return CURL_SOCKOPT_ERROR;
        }
 
        return CURL_SOCKOPT_OK;
}
 
static int context_combine_key(Context *c, char **ret) {
        assert(ret);
 
        /* Combines the well known key with the explicitly configured key */
 
        char *s;
        if (c->well_known < 0 || c->well_known == IMDS_BASE) {
                if (!c->key)
                        return -ENODATA;
 
                s = strdup(c->key);
        } else {
                const char *wk = arg_well_known_key[c->well_known];
                if (!wk)
                        return -ENODATA;
                if (c->key)
                        s = strjoin(wk, c->key);
                else
                        s = strdup(wk);
        }
        if (!s)
                return -ENOMEM;
 
        *ret = TAKE_PTR(s);
        return 0;
}
 
static const char *context_get_runtime_directory(Context *c) {
        assert(c);
 
        /* Returns the discovered runtime directory, but only if caching is enabled. */
 
        if (!c->cache) {
                context_log(c, LOG_DEBUG, "Cache disabled.");
                return NULL;
        }
 
        const char *e = secure_getenv("RUNTIME_DIRECTORY");
        if (!e) {
                context_log(c, LOG_DEBUG, "Not using cache as $RUNTIME_DIRECTORY is not set.");
                return NULL;
        }
 
        return e;
}
 
static int context_save_ifname(Context *c) {
        int r;
 
        assert(c);
 
        /* Saves the used interface name for later retrievals, so that we don't have to wildcard search on
         * all interfaces anymore. */
 
        if (c->ifindex <= 0)
                return 0;
 
        const char *d = context_get_runtime_directory(c);
        if (!d)
                return 0;
 
        _cleanup_close_ int dirfd = open(d, O_PATH|O_CLOEXEC);
        if (dirfd < 0)
                return context_log_errno(c, LOG_ERR, errno, "Failed to open runtime directory: %m");
 
        _cleanup_free_ char *ifname = NULL;
        r = rtnl_get_ifname_full(&c->rtnl, c->ifindex, &ifname, /* ret_altnames= */ NULL);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to resolve interface index %i: %m", c->ifindex);
 
        r = write_string_file_at(dirfd, "ifname", ifname, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to write 'ifname' file: %m");
 
        return 1;
}
 
typedef enum CacheResult {
        CACHE_RESULT_DISABLED,             /* caching is disabled */
        CACHE_RESULT_HIT,                  /* found a positive entry */
        CACHE_RESULT_MISS,                 /* did not find an entry */
        CACHE_RESULT_KEY_NOT_FOUND,        /* found a negative entry */
        CACHE_RESULT_NOT_CACHEABLE,        /* not suitable for caching */
        _CACHE_RESULT_MAX,
        _CACHE_RESULT_INVALID = -EINVAL,
        _CACHE_RESULT_ERRNO_MAX = -ERRNO_MAX,
} CacheResult;
 
static CacheResult context_process_cache(Context *c) {
        int r;
 
        assert(c);
 
        assert(c->key || c->well_known >= 0);
        assert(c->cache_fd < 0);
        assert(!c->cache_filename);
        assert(!c->cache_temporary_filename);
 
        /* Checks the local cache – if we have one – for the current request */
 
        if (c->cache_dir_fd < 0) {
                const char *e = context_get_runtime_directory(c);
                if (!e)
                        return CACHE_RESULT_DISABLED;
 
                char ifname[IF_NAMESIZE];
                r = format_ifname(c->ifindex, ifname);
                if (r < 0)
                        return context_log_errno(c, LOG_ERR, r, "Failed to format interface name: %m");
 
                if (!filename_is_valid(ifname))
                        return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EINVAL), "Network interface name '%s' is not a valid filename, refusing.", ifname);
 
                _cleanup_free_ char *cache_dir = path_join("cache", ifname);
                if (!cache_dir)
                        return context_log_oom(c);
 
                r = chase(cache_dir,
                          e,
                          CHASE_PROHIBIT_SYMLINKS|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY|CHASE_PREFIX_ROOT,
                          /* ret_path= */ NULL,
                          &c->cache_dir_fd);
                if (r < 0)
                        return context_log_errno(c, LOG_ERR, r, "Failed to open cache directory: %m");
        }
 
        _cleanup_free_ char *k = NULL;
        r = context_combine_key(c, &k);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to combine IMDS key: %m");
 
        _cleanup_free_ char *escaped = xescape(k, "/.");
        if (!escaped)
                return context_log_oom(c);
 
        _cleanup_free_ char *fn = strjoin("key-", escaped);
        if (!fn)
                return context_log_oom(c);
 
        if (!filename_is_valid(fn)) {
                context_log(c, LOG_WARNING, "Cache filename for '%s' is not valid, not caching.", fn);
                return CACHE_RESULT_NOT_CACHEABLE;
        }
 
        c->cache_filename = TAKE_PTR(fn);
 
        _cleanup_close_ int fd = openat(c->cache_dir_fd, c->cache_filename, O_RDONLY|O_CLOEXEC);
        if (fd < 0) {
                if (errno != ENOENT)
                        return context_log_errno(c, LOG_ERR, errno, "Failed to open cache file '%s': %m", c->cache_filename);
        } else {
                _cleanup_free_ char *d = NULL;
                size_t l;
 
                context_log(c, LOG_DEBUG, "Found cached file '%s'.", c->cache_filename);
 
                r = fgetxattr_malloc(fd, "user.imds.timestamp", &d, &l);
                if (r < 0)
                        return context_log_errno(c, LOG_ERR, r, "Failed to read timestamp from cache file: %m");
                if (l != sizeof(usec_t))
                        return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EBADMSG), "Invalid timestamp xattr on cache file '%s': %m", c->cache_filename);
 
                usec_t *u = (usec_t*) d;
                if (usec_add(*u, c->refresh_usec) > c->timestamp) {
                        _cleanup_free_ char *result = NULL;
                        r = fgetxattr_malloc(fd, "user.imds.result", &result, /* ret_size= */ NULL);
                        if (r == -ENODATA) {
                                /* No user.imds.result xattr means: hit! */
                                if (c->write_stdout) {
                                        r = copy_bytes(fd, STDOUT_FILENO, /* max_bytes= */ UINT64_MAX, /* copy_flags= */ 0);
                                        if (r < 0)
                                                return context_log_errno(c, LOG_ERR, r, "Failed to write cached data to standard output: %m");
                                } else {
                                        assert(!iovec_is_set(&c->write_iovec));
                                        r = read_full_file_at(fd, /* filename= */ NULL, (char**) &c->write_iovec.iov_base, &c->write_iovec.iov_len);
                                        if (r < 0)
                                                return context_log_errno(c, LOG_ERR, r, "Failed to read cache data: %m");
                                }
 
                                return CACHE_RESULT_HIT;
                        }
                        if (r < 0)
                                return context_log_errno(c, LOG_ERR, r, "Failed to read 'user.imds.result' extended attribute: %m");
 
                        if (streq(result, "key-not-found"))
                                return CACHE_RESULT_KEY_NOT_FOUND;
 
                        context_log(c, LOG_WARNING, "Unexpected 'user.imds.result' extended attribute value, ignoring: %s", result);
                        (void) unlinkat(c->cache_dir_fd, c->cache_filename, /* flags= */ 0);
                } else {
                        context_log(c, LOG_DEBUG, "Cached data is older than '%s', ignoring.", FORMAT_TIMESPAN(c->refresh_usec, 0));
                        (void) unlinkat(c->cache_dir_fd, c->cache_filename, /* flags= */ 0);
                }
        }
 
        /* So the above was not conclusive, let's then at least try to reuse the token */
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
        r = sd_json_parse_file_at(/* f= */ NULL, c->cache_dir_fd, "token", /* flags= */ 0, &j, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
        if (r == -ENOENT) {
                context_log_errno(c, LOG_DEBUG, r, "No cached token");
                return CACHE_RESULT_MISS;
        }
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to read cached token: %m");
 
        struct {
                const char *token;
                uint64_t until;
        } d = {};
 
        static const sd_json_dispatch_field table[] = {
                { "token",          SD_JSON_VARIANT_STRING,        sd_json_dispatch_const_string, voffsetof(d, token), SD_JSON_MANDATORY },
                { "validUntilUSec", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64,       voffsetof(d, until), SD_JSON_MANDATORY },
                {}
        };
 
        r = sd_json_dispatch(j, table, SD_JSON_ALLOW_EXTENSIONS, &d);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to decode cached token data: %m");
 
        if (d.until > c->timestamp) {
                /* On retries we might still have a token */
                if (free_and_strdup(&c->token_string, d.token) < 0)
                        return context_log_oom(c);
 
                context_log(c, LOG_INFO, "Reusing cached token.");
        } else
                context_log(c, LOG_DEBUG, "Cached token is stale, not using.");
 
        return CACHE_RESULT_MISS;
}
 
static int context_fail_cached_key_not_found(Context *c) {
        int r;
 
        assert(c);
 
        /* Called when the cache has a negative entry for the current request */
 
        r = context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Cache reports: key not found");
        context_fail_full(c, r, "io.systemd.InstanceMetadata.KeyNotFound");
        return r;
}
 
static int context_restart(Context *c) {
        int r;
 
        assert(c);
 
        /* Restarts the current request after context_reset_for_refresh(). Failures are reported via
         * context_fail(), which returns 0, hence this always returns 0. */
 
        /* Maybe some other instance was successful in the meantime and already found something? */
        CacheResult cr = context_process_cache(c);
        if (cr < 0)
                return context_fail(c, cr);
        if (cr == CACHE_RESULT_HIT) {
                context_success(c);
                return 0;
        }
        if (cr == CACHE_RESULT_KEY_NOT_FOUND) {
                (void) context_fail_cached_key_not_found(c);
                return 0;
        }
 
        r = context_acquire_token(c);
        if (r < 0)
                return context_fail(c, r);
 
        r = context_acquire_data(c);
        if (r < 0)
                return context_fail(c, r);
 
        return 0;
}
 
static int on_retry(sd_event_source *s, uint64_t usec, void *userdata) {
        Context *c = ASSERT_PTR(userdata);
 
        assert(s);
 
        /* Invoked whenever the retry timer event elapses and we need to retry again */
 
        context_log(c, LOG_DEBUG, "Retrying...");
 
        return context_restart(c);
}
 
static int context_schedule_retry(Context *c) {
        int r;
 
        assert(c);
 
        /* Schedules a new retry via a timer event */
 
        if (c->n_retry >= RETRY_MAX)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EUCLEAN), "Retry limits reached, refusing.");
 
        /* Exponential backoff, counting all retries of this request, including token refreshes */
        usec_t interval = MIN(RETRY_MIN_USEC << c->n_retry, RETRY_MAX_USEC);
 
        c->n_retry++;
        context_log(c, LOG_DEBUG, "Retry attempt #%u in %s...", c->n_retry, FORMAT_TIMESPAN(interval, USEC_PER_MSEC));
 
        context_reset_for_refresh(c);
 
        r = event_reset_time_relative(
                        c->event,
                        &c->retry_source,
                        CLOCK_BOOTTIME,
                        interval,
                        /* accuracy= */ 0,
                        on_retry,
                        c,
                        /* priority= */ 0,
                        "imds-retry",
                        /* force_reset= */ true);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to reset retry timer event source: %m");
 
        return 0;
}
 
static int context_acquire_http_status(Context *c, CURL *curl, long *ret_status) {
        assert(c);
        assert(ret_status);
 
        /* Acquires the HTTP status code, and does some generic validation that applies to both the token and
         * the data transfer.
         *
         * Error handling as per:
         *     https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html#instance-metadata-returns
         *     https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service#rate-limiting
         */
 
        long status;
        CURLcode code = sym_curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
        if (code != CURLE_OK)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", sym_curl_easy_strerror(code));
 
        context_log(c, LOG_DEBUG, "Got HTTP error code %li.", status);
 
        if (status == 403)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EADDRNOTAVAIL), "IMDS is not available");
 
        /* Automatically retry on some transient errors from HTTP */
        if (IN_SET(status,
                   503, /* AWS + GCP */
                   429  /* Azure + GCP */)) {
                *ret_status = 0;
                return 0; /* no immediate answer, please schedule retry */
        }
 
        if (status < 200 || status > 600)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request finished with unexpected code %li.", status);
 
        *ret_status = status;
        return 1; /* valid answer */
}
 
static int context_validate_token_http_status(Context *c, long status) {
        assert(c);
 
        /* Specific HTTP status checks for the token transfer */
 
        if (status >= 300)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request for token finished with unexpected code %li.", status);
 
        return 1; /* all good */
}
 
static int context_validate_data_http_status(Context *c, long status) {
        int r;
 
        assert(c);
 
        /* Specific HTTP status checks for the data transfer */
 
        if (status == 401 && arg_token_url) {
                /* We need a new token */
                context_log(c, LOG_DEBUG, "Server requested a new token...");
 
                /* Count token requests as a retry */
                if (c->n_retry >= RETRY_MAX)
                        return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EUCLEAN), "Retry limits reached, refusing.");
                c->n_retry++;
 
                context_flush_token(c);
                context_reset_for_refresh(c);
                return context_restart(c); /* restarted right-away */
        }
 
        if (status == 404) {
                _cleanup_free_ char *key = NULL;
                r = context_combine_key(c, &key);
                if (r < 0)
                        return context_log_errno(c, LOG_ERR, r, "Failed to combine IMDS key: %m");
 
                /* Do negative caching for not found */
                if (c->cache_fd >= 0) {
                        if (fsetxattr(c->cache_fd, "user.imds.result", "key-not-found", STRLEN("key-not-found"), /* flags= */ 0) < 0)
                                context_log_errno(c, LOG_DEBUG, errno, "Failed to set result xattr on '%s', ignoring: %m", c->cache_filename);
                        else {
                                r = link_tmpfile_at(c->cache_fd, c->cache_dir_fd, c->cache_temporary_filename, c->cache_filename, LINK_TMPFILE_REPLACE);
                                if (r < 0)
                                        return context_log_errno(c, LOG_ERR, r, "Failed to move cache file into place: %m");
 
                                c->cache_fd = safe_close(c->cache_fd);
                                c->cache_temporary_filename = mfree(c->cache_temporary_filename);
 
                                context_log(c, LOG_DEBUG, "Cached negative entry for '%s'.", key);
                        }
                }
 
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(ENOENT), "Key '%s' not found.", key);
        }
 
        if (status >= 300)
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EIO), "HTTP request for data finished with unexpected code %li.", status);
 
        return 1; /* all good */
}
 
static int context_validate_token(Context *c) {
        int r;
 
        assert(c);
 
        /* Validates that the downloaded token data actually forms a valid string */
 
        _cleanup_free_ char *t = NULL;
        r = make_cstring(
                        c->token.iov_base,
                        c->token.iov_len,
                        MAKE_CSTRING_REFUSE_TRAILING_NUL,
                        &t);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to convert token into C string: %m");
 
        if (string_has_cc(t, NULL) ||
            !utf8_is_valid(t))
                return context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EINVAL), "Token not valid UTF-8 or contains control characters, refusing.");
 
        free_and_replace(c->token_string, t);
        return 1; /* all good */
}
 
static int context_save_token(Context *c) {
        int r;
 
        assert(c);
        assert(c->token_string);
 
        /* Save the acquired token in the cache, so that we can reuse it later */
 
        if (c->cache_dir_fd < 0)
                return 0;
 
        /* Only store half the valid time, to make sure we have ample time to use it */
        usec_t until = usec_add(c->timestamp, c->refresh_usec/2);
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *j = NULL;
        r = sd_json_buildo(
                        &j,
                        SD_JSON_BUILD_PAIR_STRING("token", c->token_string),
                        SD_JSON_BUILD_PAIR_UNSIGNED("validUntilUSec", until));
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to build token JSON: %m");
 
        _cleanup_free_ char *t = NULL;
        r = sd_json_variant_format(j, SD_JSON_FORMAT_NEWLINE, &t);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to format JSON: %m");
 
        r = write_string_file_at(c->cache_dir_fd, "token", t, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MODE_0600);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to write token cache file: %m");
 
        return 0;
}
 
static int context_save_data(Context *c) {
        int r;
 
        assert(c);
 
        /* Finalize saving of the acquired data in the cache */
 
        if (c->cache_fd < 0)
                return 0;
 
        r = link_tmpfile_at(c->cache_fd, c->cache_dir_fd, c->cache_temporary_filename, c->cache_filename, LINK_TMPFILE_REPLACE);
        if (r < 0)
                return context_log_errno(c, LOG_ERR, r, "Failed to move cache file into place: %m");
 
        c->cache_fd = safe_close(c->cache_fd);
        c->cache_temporary_filename = mfree(c->cache_temporary_filename);
 
        context_log(c, LOG_DEBUG, "Cached data.");
        return 0;
}
 
static int curl_on_finished(CurlSlot *slot, CURL *curl, CURLcode result, void *userdata) {
        Context *c = ASSERT_PTR(userdata);
        int r;
 
        /* Called whenever libcurl did its thing and reports a download being complete or having failed */
 
        switch (result) {
 
        case CURLE_OK: /* yay! */
                /* If we managed to get a HTTP reply, this is good enough, let's pin the interface now for
                 * later calls */
                (void) context_save_ifname(c);
                break;
 
        case CURLE_WRITE_ERROR:
                /* CURLE_WRITE_ERROR we'll see if the data callbacks failed already. We'll try to look at the
                 * HTTP status below, and use that ideally. */
                break;
 
        case CURLE_COULDNT_CONNECT:
        case CURLE_OPERATION_TIMEDOUT:
        case CURLE_GOT_NOTHING:
        case CURLE_SEND_ERROR:
        case CURLE_RECV_ERROR:
                context_log(c, LOG_INFO, "Connection error from curl: %s", sym_curl_easy_strerror(result));
 
                /* Automatically retry on some transient errors from curl itself */
                r = context_schedule_retry(c);
                if (r == -EUCLEAN)
                        return context_fail_full(c, r, "io.systemd.InstanceMetadata.Timeout");
                if (r < 0)
                        return context_fail(c, r);
 
                return 0;
 
        default:
                return context_fail_full(
                                c,
                                context_log_errno(c, LOG_ERR, SYNTHETIC_ERRNO(EHOSTDOWN), "Transfer failed: %s", sym_curl_easy_strerror(result)),
                                "io.systemd.InstanceMetadata.CommunicationFailure");
        }
 
        long status;
        r = context_acquire_http_status(c, curl, &status);
        if (r == -EADDRNOTAVAIL)
                return context_fail_full(c, r, "io.systemd.InstanceMetadata.NotAvailable");
        if (r < 0)
                return context_fail(c, r);
        if (r == 0) { /* We shall retry */
                r = context_schedule_retry(c);
                if (r == -EUCLEAN)
                        return context_fail_full(c, r, "io.systemd.InstanceMetadata.Timeout");
                if (r < 0)
                        return context_fail(c, r);
 
                return 0;
        }
        /* For status >= 300 the transfer was aborted on purpose to skip reading the error page. The status
         * checks below then report the actual error. For a successful reply it means a local failure, which
         * the callback logged already. */
        if (result == CURLE_WRITE_ERROR && status < 300)
                return context_fail(c, SYNTHETIC_ERRNO(ENOTRECOVERABLE));
 
        if (slot == c->slot_token) {
                r = context_validate_token_http_status(c, status);
                if (r < 0)
                        return context_fail(c, r);
 
                r = context_validate_token(c);
                if (r < 0)
                        return context_fail(c, r);
 
                context_log(c, LOG_DEBUG, "Token successfully acquired.");
 
                r = context_save_token(c);
                if (r < 0)
                        return context_fail(c, r);
 
                r = context_acquire_data(c);
                if (r < 0)
                        return context_fail(c, r);
 
        } else if (slot == c->slot_data) {
 
                r = context_validate_data_http_status(c, status);
                if (r == -ENOENT)
                        return context_fail_full(c, r, "io.systemd.InstanceMetadata.KeyNotFound");
                if (r == -EUCLEAN)
                        return context_fail_full(c, r, "io.systemd.InstanceMetadata.Timeout");
                if (r < 0)
                        return context_fail(c, r);
                if (r == 0) /* Immediately restarted */
                        return 0;