importd: validate local image names with the right helper

A while back we introduced image_name_is_valid() for validating image
file names. It's more liberal than hostname_is_valid() in many ways (and
allows version suffixes and such). Since importd deals in offline images
(as opposed to machined otherwise which deals in running machines),
let's hence use the right helper to validate the identifiers.
This commit is contained in:
Lennart Poettering 2024-02-22 12:31:03 +01:00
parent 420b8ce798
commit 8f20b498bd
8 changed files with 38 additions and 35 deletions

View file

@ -63,12 +63,13 @@ static int export_tar(int argc, char *argv[], void *userdata) {
_cleanup_close_ int open_fd = -EBADF;
int r, fd;
if (hostname_is_valid(argv[1], 0)) {
r = image_find(arg_class, argv[1], NULL, &image);
local = argv[1];
if (image_name_is_valid(local)) {
r = image_find(arg_class, local, NULL, &image);
if (r == -ENOENT)
return log_error_errno(r, "Machine image %s not found.", argv[1]);
return log_error_errno(r, "Image %s not found.", local);
if (r < 0)
return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
return log_error_errno(r, "Failed to look for image %s: %m", local);
local = image->path;
} else
@ -135,12 +136,13 @@ static int export_raw(int argc, char *argv[], void *userdata) {
_cleanup_close_ int open_fd = -EBADF;
int r, fd;
if (hostname_is_valid(argv[1], 0)) {
r = image_find(arg_class, argv[1], NULL, &image);
local = argv[1];
if (image_name_is_valid(local)) {
r = image_find(arg_class, local, NULL, &image);
if (r == -ENOENT)
return log_error_errno(r, "Machine image %s not found.", argv[1]);
return log_error_errno(r, "Image %s not found.", local);
if (r < 0)
return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
return log_error_errno(r, "Failed to look for image %s: %m", local);
local = image->path;
} else

View file

@ -276,7 +276,7 @@ bool import_validate_local(const char *name, ImportFlags flags) {
if (FLAGS_SET(flags, IMPORT_DIRECT))
return path_is_valid(name);
return hostname_is_valid(name, 0);
return image_name_is_valid(name);
}
static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {

View file

@ -133,7 +133,7 @@ static int import_fs(int argc, char *argv[], void *userdata) {
"Local path name '%s' is not valid.", final_path);
} else {
if (local) {
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.", local);
} else

View file

@ -54,7 +54,7 @@ static int normalize_local(const char *local, char **ret) {
"Local path name '%s' is not valid.", local);
} else {
if (local) {
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.",
local);

View file

@ -220,9 +220,9 @@ static int import_tar(int argc, char *argv[], void *userdata) {
local = ll;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.",
"Local name %s is not a suitable image name.",
local);
if (path) {
@ -299,9 +299,9 @@ static int import_raw(int argc, char *argv[], void *userdata) {
local = ll;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.",
"Local name %s is not a suitable image name.",
local);
if (path) {
@ -369,9 +369,9 @@ static int import_fs(int argc, char *argv[], void *userdata) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Need either path or local name.");
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.",
"Local name %s is not a suitable image name.",
local);
if (path) {
@ -439,9 +439,9 @@ static int export_tar(int argc, char *argv[], void *userdata) {
return r;
local = argv[1];
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Machine name %s is not valid.", local);
"Image name %s is not valid.", local);
if (argc >= 3)
path = argv[2];
@ -498,9 +498,9 @@ static int export_raw(int argc, char *argv[], void *userdata) {
return r;
local = argv[1];
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Machine name %s is not valid.", local);
"Image name %s is not valid.", local);
if (argc >= 3)
path = argv[2];
@ -580,9 +580,9 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
local = ll;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.",
"Local name %s is not a suitable image name.",
local);
}
@ -653,9 +653,9 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
local = ll;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local name %s is not a suitable machine name.",
"Local name %s is not a suitable image name.",
local);
}

View file

@ -771,9 +771,9 @@ static int method_import_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode))
return -EINVAL;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local);
"Local image name %s is invalid", local);
if (class == IMAGE_MACHINE) {
r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota);
@ -870,9 +870,9 @@ static int method_import_fs(sd_bus_message *msg, void *userdata, sd_bus_error *e
if (r < 0)
return r;
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local);
"Local image name %s is invalid", local);
if (class == IMAGE_MACHINE) {
r = setup_machine_directory(error, m->use_btrfs_subvol, m->use_btrfs_quota);
@ -955,9 +955,9 @@ static int method_export_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_
flags = 0;
}
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local);
"Local image name %s is invalid", local);
r = fd_verify_safe_flags(fd);
if (r < 0)
@ -1063,9 +1063,9 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
if (isempty(local))
local = NULL;
else if (!hostname_is_valid(local, 0))
else if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local name %s is invalid", local);
"Local image name %s is invalid", local);
if (isempty(verify))
v = IMPORT_VERIFY_SIGNATURE;

View file

@ -7,6 +7,7 @@
#include "capability-util.h"
#include "copy.h"
#include "dirent-util.h"
#include "discover-image.h"
#include "escape.h"
#include "fd-util.h"
#include "hostname-util.h"
@ -644,7 +645,7 @@ bool pull_validate_local(const char *name, ImportFlags flags) {
if (FLAGS_SET(flags, IMPORT_DIRECT))
return path_is_valid(name);
return hostname_is_valid(name, 0);
return image_name_is_valid(name);
}
int pull_url_needs_checksum(const char *url) {

View file

@ -59,7 +59,7 @@ static int normalize_local(const char *local, const char *url, char **ret) {
} else if (local) {
if (!hostname_is_valid(local, 0))
if (!image_name_is_valid(local))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Local image name '%s' is not valid.",
local);