| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "creds-util.h" |
| 5 |
#include "escape.h" |
| 6 |
#include "extract-word.h" |
| 7 |
#include "fileio.h" |
| 8 |
#include "iovec-util.h" |
| 9 |
#include "log.h" |
| 10 |
#include "machine-credential.h" |
| 11 |
#include "memory-util.h" |
| 12 |
#include "path-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
|
| 15 |
static void machine_credential_done(MachineCredential *cred) { |
| 16 |
assert(cred); |
| 17 |
|
| 18 |
cred->id = mfree(cred->id); |
| 19 |
cred->data = erase_and_free(cred->data); |
| 20 |
cred->size = 0; |
| 21 |
} |
| 22 |
|
| 23 |
void machine_credential_context_done(MachineCredentialContext *ctx) { |
| 24 |
assert(ctx); |
| 25 |
|
| 26 |
FOREACH_ARRAY(cred, ctx->credentials, ctx->n_credentials) |
| 27 |
machine_credential_done(cred); |
| 28 |
|
| 29 |
free(ctx->credentials); |
| 30 |
} |
| 31 |
|
| 32 |
MachineCredential* machine_credential_find(MachineCredentialContext *ctx, const char *id) { |
| 33 |
assert(ctx); |
| 34 |
assert(id); |
| 35 |
|
| 36 |
FOREACH_ARRAY(cred, ctx->credentials, ctx->n_credentials) |
| 37 |
if (streq(cred->id, id)) |
| 38 |
return cred; |
| 39 |
|
| 40 |
return NULL; |
| 41 |
} |
| 42 |
|
| 43 |
int machine_credential_add( |
| 44 |
MachineCredentialContext *ctx, |
| 45 |
const char *id, |
| 46 |
const char *value, |
| 47 |
size_t size) { |
| 48 |
|
| 49 |
assert(ctx); |
| 50 |
assert(id); |
| 51 |
assert(value || size == 0); |
| 52 |
|
| 53 |
if (!credential_name_valid(id)) |
| 54 |
return -EINVAL; |
| 55 |
|
| 56 |
if (machine_credential_find(ctx, id)) |
| 57 |
return -EEXIST; |
| 58 |
|
| 59 |
if (size == SIZE_MAX) |
| 60 |
size = strlen_ptr(value); |
| 61 |
|
| 62 |
_cleanup_(machine_credential_done) MachineCredential cred = {}; |
| 63 |
cred.id = strdup(id); |
| 64 |
if (!cred.id) |
| 65 |
return -ENOMEM; |
| 66 |
|
| 67 |
cred.data = memdup(value, size); |
| 68 |
if (!cred.data) |
| 69 |
return -ENOMEM; |
| 70 |
|
| 71 |
cred.size = size; |
| 72 |
|
| 73 |
if (!GREEDY_REALLOC(ctx->credentials, ctx->n_credentials + 1)) |
| 74 |
return -ENOMEM; |
| 75 |
|
| 76 |
ctx->credentials[ctx->n_credentials++] = TAKE_STRUCT(cred); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int machine_credential_add_and_log( |
| 81 |
MachineCredentialContext *ctx, |
| 82 |
const char *id, |
| 83 |
const char *value, |
| 84 |
size_t size) { |
| 85 |
|
| 86 |
int r; |
| 87 |
|
| 88 |
assert(ctx); |
| 89 |
assert(id); |
| 90 |
assert(value || size == 0); |
| 91 |
|
| 92 |
r = machine_credential_add(ctx, id, value, size); |
| 93 |
if (r == -EEXIST) |
| 94 |
return log_error_errno(r, "Duplicated credential '%s', refusing.", id); |
| 95 |
if (r == -EINVAL) |
| 96 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Credential name is not valid: %s", id); |
| 97 |
if (r == -ENOMEM) |
| 98 |
return log_oom(); |
| 99 |
if (r < 0) |
| 100 |
return log_error_errno(r, "Failed to add credential '%s': %m", id); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
int machine_credential_set(MachineCredentialContext *ctx, const char *cred_str) { |
| 107 |
int r; |
| 108 |
|
| 109 |
assert(ctx); |
| 110 |
|
| 111 |
const char *p = ASSERT_PTR(cred_str); |
| 112 |
_cleanup_free_ char *id = NULL; |
| 113 |
r = extract_first_word(&p, &id, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 114 |
if (r < 0) |
| 115 |
return log_error_errno(r, "Failed to parse --set-credential= parameter: %m"); |
| 116 |
if (r == 0 || !p) |
| 117 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 118 |
"Missing value for --set-credential=: %s", cred_str); |
| 119 |
|
| 120 |
_cleanup_free_ char *data = NULL; |
| 121 |
ssize_t l; |
| 122 |
l = cunescape(p, UNESCAPE_ACCEPT_NUL, &data); |
| 123 |
if (l < 0) |
| 124 |
return log_error_errno(l, "Failed to unescape credential data: %s", p); |
| 125 |
|
| 126 |
return machine_credential_add_and_log(ctx, id, data, l); |
| 127 |
} |
| 128 |
|
| 129 |
int machine_credential_load(MachineCredentialContext *ctx, const char *cred_path) { |
| 130 |
int r; |
| 131 |
|
| 132 |
assert(ctx); |
| 133 |
|
| 134 |
const char *p = ASSERT_PTR(cred_path); |
| 135 |
_cleanup_free_ char *id = NULL; |
| 136 |
r = extract_first_word(&p, &id, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 137 |
if (r < 0) |
| 138 |
return log_error_errno(r, "Failed to parse --load-credential= parameter: %m"); |
| 139 |
if (r == 0 || !p) |
| 140 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Missing value for --load-credential=: %s", cred_path); |
| 141 |
|
| 142 |
ReadFullFileFlags flags = READ_FULL_FILE_SECURE; |
| 143 |
_cleanup_free_ char *path_alloc = NULL; |
| 144 |
if (is_path(p) && path_is_valid(p)) |
| 145 |
flags |= READ_FULL_FILE_CONNECT_SOCKET; |
| 146 |
else if (credential_name_valid(p)) { |
| 147 |
const char *e; |
| 148 |
|
| 149 |
r = get_credentials_dir(&e); |
| 150 |
if (r < 0) |
| 151 |
return log_error_errno(r, "Credential not available (no credentials passed at all): %s", p); |
| 152 |
|
| 153 |
path_alloc = path_join(e, p); |
| 154 |
if (!path_alloc) |
| 155 |
return log_oom(); |
| 156 |
|
| 157 |
p = path_alloc; |
| 158 |
} else |
| 159 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 160 |
"Credential source appears to be neither a valid path nor a credential name: %s", p); |
| 161 |
|
| 162 |
_cleanup_(iovec_done_erase) struct iovec iov = {}; |
| 163 |
r = read_full_file_full( |
| 164 |
AT_FDCWD, p, |
| 165 |
/* offset= */ UINT64_MAX, |
| 166 |
/* size= */ SIZE_MAX, |
| 167 |
flags, |
| 168 |
/* bind_name= */ NULL, |
| 169 |
(char**) &iov.iov_base, &iov.iov_len); |
| 170 |
if (r < 0) |
| 171 |
return log_error_errno(r, "Failed to read credential '%s': %m", p); |
| 172 |
|
| 173 |
return machine_credential_add_and_log(ctx, id, iov.iov_base, iov.iov_len); |
| 174 |
} |