| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <string.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "architecture.h" |
| 9 |
#include "constants.h" |
| 10 |
#include "dns-domain.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "fileio.h" |
| 13 |
#include "hexdecoct.h" |
| 14 |
#include "log.h" |
| 15 |
#include "oci-util.h" |
| 16 |
#include "parse-util.h" |
| 17 |
#include "string-table.h" |
| 18 |
#include "string-util.h" |
| 19 |
|
| 20 |
bool oci_image_is_valid(const char *n) { |
| 21 |
bool slash = true; |
| 22 |
|
| 23 |
/* The OCI spec suggests validating this regex: |
| 24 |
* |
| 25 |
* [a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)* |
| 26 |
* |
| 27 |
* We implement a generalization of this, i.e. do not insist on the single ".", "_", "__", "-", "+" |
| 28 |
* separator, but allow any number of them. And we refuse leading dots, since if used in the fs this |
| 29 |
* would make the files hidden, and we probably don't want that. |
| 30 |
*/ |
| 31 |
|
| 32 |
for (const char *p = n; *p; p++) { |
| 33 |
if (*p == '/') { |
| 34 |
if (slash) |
| 35 |
return false; |
| 36 |
|
| 37 |
slash = true; |
| 38 |
continue; |
| 39 |
} |
| 40 |
|
| 41 |
if (!strchr(slash ? LOWERCASE_LETTERS DIGITS "_-+" : |
| 42 |
"." LOWERCASE_LETTERS DIGITS "_-+", *p)) |
| 43 |
return false; |
| 44 |
|
| 45 |
slash = false; |
| 46 |
} |
| 47 |
|
| 48 |
return !slash; |
| 49 |
} |
| 50 |
|
| 51 |
int oci_registry_is_valid(const char *n) { |
| 52 |
int r; |
| 53 |
|
| 54 |
if (!n) |
| 55 |
return false; |
| 56 |
|
| 57 |
const char *colon = strchr(n, ':'); |
| 58 |
if (!colon) |
| 59 |
return dns_name_is_valid(n); |
| 60 |
|
| 61 |
_cleanup_free_ char *s = strndup(n, colon - n); |
| 62 |
if (!s) |
| 63 |
return -ENOMEM; |
| 64 |
|
| 65 |
r = dns_name_is_valid(s); |
| 66 |
if (r <= 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
uint16_t port; |
| 70 |
return safe_atou16(s, &port) >= 0 && port != 0; |
| 71 |
} |
| 72 |
|
| 73 |
bool oci_tag_is_valid(const char *n) { |
| 74 |
if (!n) |
| 75 |
return false; |
| 76 |
|
| 77 |
/* As per https://github.com/opencontainers/distribution-spec/blob/main/spec.md, accept the following regex: |
| 78 |
* |
| 79 |
* [a-zA-Z0-9_][a-zA-Z0-9._-]{0,127} |
| 80 |
*/ |
| 81 |
|
| 82 |
if (!strchr(ALPHANUMERICAL "_", n[0])) |
| 83 |
return false; |
| 84 |
|
| 85 |
size_t l = strspn(n + 1, ALPHANUMERICAL "._-"); |
| 86 |
if (l > 126) |
| 87 |
return false; |
| 88 |
if (n[1+l] != 0) |
| 89 |
return false; |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
int oci_ref_parse( |
| 95 |
const char *ref, |
| 96 |
char **ret_registry, |
| 97 |
char **ret_image, |
| 98 |
char **ret_tag) { |
| 99 |
|
| 100 |
int r; |
| 101 |
|
| 102 |
assert(ref); |
| 103 |
|
| 104 |
_cleanup_free_ char *without_tag = NULL, *tag = NULL; |
| 105 |
const char *t = strrchr(ref, ':'); |
| 106 |
if (t) { |
| 107 |
tag = strdup(t + 1); |
| 108 |
if (!tag) |
| 109 |
return -ENOMEM; |
| 110 |
if (!oci_tag_is_valid(tag)) |
| 111 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "OCI tag specification '%s' is not valid.", tag); |
| 112 |
|
| 113 |
without_tag = strndup(ref, t - ref); |
| 114 |
if (!without_tag) |
| 115 |
return -ENOMEM; |
| 116 |
|
| 117 |
ref = without_tag; |
| 118 |
} |
| 119 |
|
| 120 |
_cleanup_free_ char *image = NULL, *registry = NULL; |
| 121 |
t = strchr(ref, '/'); |
| 122 |
if (t) { |
| 123 |
registry = strndup(ref, t - ref); |
| 124 |
if (!registry) |
| 125 |
return -ENOMEM; |
| 126 |
|
| 127 |
r = oci_registry_is_valid(registry); |
| 128 |
if (r < 0) |
| 129 |
return r; |
| 130 |
if (r == 0) |
| 131 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "OCI registry specification '%s' is not valid.", registry); |
| 132 |
|
| 133 |
image = strdup(t + 1); |
| 134 |
} else |
| 135 |
image = strdup(ref); |
| 136 |
if (!image) |
| 137 |
return -ENOMEM; |
| 138 |
if (!oci_image_is_valid(image)) |
| 139 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "OCI image specification '%s' is not valid.", registry); |
| 140 |
|
| 141 |
if (ret_registry) |
| 142 |
*ret_registry = TAKE_PTR(registry); |
| 143 |
if (ret_image) |
| 144 |
*ret_image = TAKE_PTR(image); |
| 145 |
if (ret_tag) |
| 146 |
*ret_tag = TAKE_PTR(tag); |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
int oci_ref_normalize(char **protocol, char **registry, char **image, char **tag) { |
| 152 |
int r; |
| 153 |
|
| 154 |
assert(protocol); |
| 155 |
assert(registry); |
| 156 |
assert(image); |
| 157 |
assert(*image); |
| 158 |
assert(tag); |
| 159 |
|
| 160 |
/* OCI container reference are supposed to have the form <registry>/<name>:<tag>. Except that it's |
| 161 |
* all super messy, and for some registries the server name differs from the name people use in the |
| 162 |
* references, and there are special rules for "short" container names (i.e. those which do not |
| 163 |
* contain a "/"), and more. To deal with this, we devise a relatively simple scheme, to normalize |
| 164 |
* such names. Specifically: |
| 165 |
* |
| 166 |
* If a registry is specified we look for |
| 167 |
* /usr/lib/systemd/oci-registry/registry.<registry>.oci-registry for registry-specific rules, to |
| 168 |
* enforce on the reference. If no registry is specified, we look for an |
| 169 |
* /usr/lib/systemd/oci-registry/image.<name>.oci-registry file, which contains image-specific rules |
| 170 |
* instead. If this is not found we load /usr/lib/systemd/oci-registry/default.oci-registry |
| 171 |
* instead. The files are encoded in JSON. |
| 172 |
* |
| 173 |
* The rules we apply are relatively simple: |
| 174 |
* |
| 175 |
* • defaultProtocol controls which protocol to use if none is known. This should always be https |
| 176 |
* (since OCI images are authenticated purely via HTTPS), but for testing purposes "file" might be |
| 177 |
* useful too. |
| 178 |
* |
| 179 |
* • overrideRegistry encodes which registry server to actually use, overriding what might have been |
| 180 |
* specified. |
| 181 |
* |
| 182 |
* • overrideImage encodes which image name to actually use, overriding what might have been specified. |
| 183 |
* |
| 184 |
* • shortImagePrefix encodes a name prefix to prepend to "short" container names. This has no effect |
| 185 |
* if overrideImage is set too. |
| 186 |
* |
| 187 |
* • defaultTag contains a tag to use as default, if none is specified. If not configured this |
| 188 |
* defaults to "latest". |
| 189 |
*/ |
| 190 |
|
| 191 |
_cleanup_free_ char *fn = NULL; |
| 192 |
if (*registry) { |
| 193 |
/* If a registry is specified, we'll always respect it, and use it as only search key */ |
| 194 |
_cleanup_free_ char *e = urlescape(*registry); |
| 195 |
if (!e) |
| 196 |
return -ENOMEM; |
| 197 |
|
| 198 |
fn = strjoin("registry.", e, ".oci-registry"); |
| 199 |
} else { |
| 200 |
/* If no registry is specified, let's go by image name */ |
| 201 |
_cleanup_free_ char *e = urlescape(*image); |
| 202 |
if (!e) |
| 203 |
return -ENOMEM; |
| 204 |
|
| 205 |
fn = strjoin("image.", e, ".oci-registry"); |
| 206 |
} |
| 207 |
if (!fn) |
| 208 |
return -ENOMEM; |
| 209 |
|
| 210 |
_cleanup_fclose_ FILE *f = NULL; |
| 211 |
_cleanup_free_ char *path = NULL; |
| 212 |
r = search_and_fopen_nulstr(fn, "re", /* root= */ NULL, CONF_PATHS_NULSTR("systemd/oci-registry"), &f, &path); |
| 213 |
if (r == -ENOENT) |
| 214 |
r = search_and_fopen_nulstr("default.oci-registry", "re", /* root= */ NULL, CONF_PATHS_NULSTR("systemd/oci-registry"), &f, &path); |
| 215 |
if (r < 0 && r != -ENOENT) |
| 216 |
return log_debug_errno(r, "Failed to find suitable OCI registry file: %m"); |
| 217 |
|
| 218 |
/* if ENOENT is seen, we use the defaults below! */ |
| 219 |
|
| 220 |
struct { |
| 221 |
const char *default_protocol; |
| 222 |
const char *override_registry; |
| 223 |
const char *default_registry; |
| 224 |
const char *override_image; |
| 225 |
const char *short_image_prefix; |
| 226 |
const char *default_tag; |
| 227 |
} data = { |
| 228 |
.default_protocol = "https", |
| 229 |
.default_tag = "latest", |
| 230 |
}; |
| 231 |
|
| 232 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 233 |
if (f) { |
| 234 |
unsigned line = 0, column = 0; |
| 235 |
r = sd_json_parse_file(f, path, /* flags= */ 0, &v, &line, &column); |
| 236 |
if (r < 0) |
| 237 |
return log_debug_errno(r, "Parse failure at %s:%u:%u: %m", path, line, column); |
| 238 |
|
| 239 |
static const sd_json_dispatch_field table[] = { |
| 240 |
{ "defaultProtocol", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, default_protocol), 0 }, |
| 241 |
{ "overrideRegistry", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, override_registry), 0 }, |
| 242 |
{ "defaultRegistry", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, default_registry), 0 }, |
| 243 |
{ "overrideImage", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, override_image), 0 }, |
| 244 |
{ "shortImagePrefix", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, short_image_prefix), 0 }, |
| 245 |
{ "defaultTag", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(data, default_tag), 0 }, |
| 246 |
{}, |
| 247 |
}; |
| 248 |
r = sd_json_dispatch(v, table, SD_JSON_ALLOW_EXTENSIONS, &data); |
| 249 |
if (r < 0) |
| 250 |
return r; |
| 251 |
} |
| 252 |
|
| 253 |
_cleanup_free_ char *new_protocol = NULL; |
| 254 |
if (data.default_protocol && isempty(*protocol)) { |
| 255 |
new_protocol = strdup(data.default_protocol); |
| 256 |
if (!new_protocol) |
| 257 |
return -ENOMEM; |
| 258 |
} |
| 259 |
|
| 260 |
_cleanup_free_ char *new_registry = NULL; |
| 261 |
if (data.override_registry) { |
| 262 |
if (!isempty(*registry)) |
| 263 |
log_debug("Overriding registry to '%s' (was '%s') based on OCI registry database.", data.override_registry, *registry); |
| 264 |
|
| 265 |
new_registry = strdup(data.override_registry); |
| 266 |
if (!new_registry) |
| 267 |
return -ENOMEM; |
| 268 |
} else if (data.default_registry && isempty(*registry)) { |
| 269 |
new_registry = strdup(data.default_registry); |
| 270 |
if (!new_registry) |
| 271 |
return -ENOMEM; |
| 272 |
} |
| 273 |
|
| 274 |
_cleanup_free_ char *new_image = NULL; |
| 275 |
if (data.override_image) { |
| 276 |
log_debug("Overriding image to '%s' (was '%s') based on OCI registry database.", data.override_registry, *image); |
| 277 |
|
| 278 |
new_image = strdup(data.override_image); |
| 279 |
if (!new_image) |
| 280 |
return -ENOMEM; |
| 281 |
} else if (data.short_image_prefix && !strchr(*image, '/')) { |
| 282 |
new_image = strjoin(data.short_image_prefix, *image); |
| 283 |
if (!new_image) |
| 284 |
return -ENOMEM; |
| 285 |
} |
| 286 |
|
| 287 |
_cleanup_free_ char *new_tag = NULL; |
| 288 |
if (data.default_tag && isempty(*tag)) { |
| 289 |
new_tag = strdup(data.default_tag); |
| 290 |
if (!new_tag) |
| 291 |
return -ENOMEM; |
| 292 |
} |
| 293 |
|
| 294 |
if (!new_registry && isempty(*registry)) |
| 295 |
return log_debug_errno(SYNTHETIC_ERRNO(ENODATA), "No suitable registry found."); |
| 296 |
|
| 297 |
if (new_protocol) |
| 298 |
free_and_replace(*protocol, new_protocol); |
| 299 |
if (new_registry) |
| 300 |
free_and_replace(*registry, new_registry); |
| 301 |
if (new_image) |
| 302 |
free_and_replace(*image, new_image); |
| 303 |
if (new_tag) |
| 304 |
free_and_replace(*tag, new_tag); |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
char* oci_digest_string(const struct iovec *iovec) { |
| 310 |
assert(iovec); |
| 311 |
|
| 312 |
_cleanup_free_ char *h = hexmem(iovec->iov_base, iovec->iov_len); |
| 313 |
if (!h) |
| 314 |
return NULL; |
| 315 |
|
| 316 |
return strjoin("sha256:", h); |
| 317 |
} |
| 318 |
|
| 319 |
int oci_make_manifest_url( |
| 320 |
const char *protocol, |
| 321 |
const char *repository, |
| 322 |
const char *image, |
| 323 |
const char *tag, |
| 324 |
char **ret) { |
| 325 |
|
| 326 |
assert(protocol); |
| 327 |
assert(repository); |
| 328 |
assert(image); |
| 329 |
assert(tag); |
| 330 |
assert(ret); |
| 331 |
|
| 332 |
_cleanup_free_ char *url = strjoin(protocol, "://", repository, "/v2/", image, "/manifests/", tag); |
| 333 |
if (!url) |
| 334 |
return -ENOMEM; |
| 335 |
|
| 336 |
*ret = TAKE_PTR(url); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
int oci_make_blob_url( |
| 341 |
const char *protocol, |
| 342 |
const char *repository, |
| 343 |
const char *image, |
| 344 |
const struct iovec *digest, |
| 345 |
char **ret) { |
| 346 |
|
| 347 |
assert(protocol); |
| 348 |
assert(repository); |
| 349 |
assert(image); |
| 350 |
assert(digest); |
| 351 |
assert(ret); |
| 352 |
|
| 353 |
_cleanup_free_ char *d = oci_digest_string(digest); |
| 354 |
if (!d) |
| 355 |
return -ENOMEM; |
| 356 |
|
| 357 |
_cleanup_free_ char *url = strjoin(protocol, "://", repository, "/v2/", image, "/blobs/", d); |
| 358 |
if (!url) |
| 359 |
return -ENOMEM; |
| 360 |
|
| 361 |
*ret = TAKE_PTR(url); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
/* OCI uses the Go architecture IDs */ |
| 366 |
static const char *const go_arch_table[_ARCHITECTURE_MAX] = { |
| 367 |
[ARCHITECTURE_ARM] = "arm", |
| 368 |
[ARCHITECTURE_ARM64] = "arm64", |
| 369 |
[ARCHITECTURE_MIPS] = "mips", |
| 370 |
[ARCHITECTURE_MIPS64] = "mips64", |
| 371 |
[ARCHITECTURE_MIPS64_LE] = "mips64le", |
| 372 |
[ARCHITECTURE_MIPS_LE] = "mipsle", |
| 373 |
[ARCHITECTURE_PPC64] = "ppc64", |
| 374 |
[ARCHITECTURE_PPC64_LE] = "ppc64le", |
| 375 |
[ARCHITECTURE_S390X] = "s390x", |
| 376 |
[ARCHITECTURE_X86] = "386", |
| 377 |
[ARCHITECTURE_X86_64] = "amd64", |
| 378 |
}; |
| 379 |
|
| 380 |
DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(go_arch, Architecture); |
| 381 |
|
| 382 |
char* urlescape(const char *s) { |
| 383 |
POINTER_MAY_BE_NULL(s); |
| 384 |
size_t l = strlen_ptr(s); |
| 385 |
|
| 386 |
_cleanup_free_ char *t = new(char, l * 3 + 1); |
| 387 |
if (!t) |
| 388 |
return NULL; |
| 389 |
|
| 390 |
char *p = t; |
| 391 |
for (; s && *s; s++) { |
| 392 |
if (strchr(ALPHANUMERICAL ".-_", *s)) |
| 393 |
*(p++) = *s; |
| 394 |
else { |
| 395 |
*(p++) = '%'; |
| 396 |
*(p++) = hexchar((uint8_t) *s >> 4); |
| 397 |
*(p++) = hexchar((uint8_t) *s & 15); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
*p = 0; |
| 402 |
return TAKE_PTR(t); |
| 403 |
} |