HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/import/importd.c
Git blob: 00d52b3f46be75346d5aeccc6ecd0ec4a0bec147
Lines 1001–2000 of 2,145
if (class < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Image class '%s' not known", sclass);
if (flags != 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Flags 0x%" PRIx64 " invalid", flags);
} else {
r = sd_bus_message_read(msg, "shs", &local, &fd, &format);
if (r < 0)
return r;
class = IMAGE_MACHINE;
flags = 0;
}
if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local image name %s is invalid", local);
r = fd_verify_safe_flags(fd);
if (r < 0)
return r;
if (fstat(fd, &st) < 0)
return -errno;
if (!S_ISREG(st.st_mode) && !S_ISFIFO(st.st_mode))
return -EINVAL;
type = startswith(sd_bus_message_get_member(msg), "ExportTar") ?
TRANSFER_EXPORT_TAR : TRANSFER_EXPORT_RAW;
r = transfer_new(m, &t);
if (r < 0)
return r;
t->type = type;
t->class = class;
t->flags = flags;
if (!isempty(format)) {
t->format = strdup(format);
if (!t->format)
return -ENOMEM;
}
t->local = strdup(local);
if (!t->local)
return -ENOMEM;
t->stdout_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
if (t->stdout_fd < 0)
return -errno;
r = transfer_start(t);
if (r < 0)
return r;
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path);
if (r < 0)
return r;
TAKE_PTR(t);
return 1;
}
static int method_pull_tar_or_raw_or_oci(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
_cleanup_(transfer_unrefp) Transfer *t = NULL;
ImageClass class = _IMAGE_CLASS_INVALID;
const char *remote, *local, *verify;
Manager *m = ASSERT_PTR(userdata);
TransferType type;
uint64_t flags;
ImportVerify v;
int r;
assert(msg);
if (m->runtime_scope != RUNTIME_SCOPE_USER) {
r = bus_verify_polkit_async(
msg,
"org.freedesktop.import1.pull",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* Will call us back */
}
if (streq(sd_bus_message_get_member(msg), "PullOci")) {
const char *sclass;
r = sd_bus_message_read(msg, "ssst", &remote, &local, &sclass, &flags);
if (r < 0)
return r;
class = image_class_from_string(sclass);
if (class < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Image class '%s' not known", sclass);
if (flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Flags 0x%" PRIx64 " invalid", flags);
verify = NULL;
} else if (endswith(sd_bus_message_get_member(msg), "Ex")) {
const char *sclass;
r = sd_bus_message_read(msg, "sssst", &remote, &local, &sclass, &verify, &flags);
if (r < 0)
return r;
class = image_class_from_string(sclass);
if (class < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Image class '%s' not known", sclass);
if (flags & ~(IMPORT_FORCE|IMPORT_READ_ONLY|IMPORT_PULL_KEEP_DOWNLOAD))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Flags 0x%" PRIx64 " invalid", flags);
} else {
int force;
r = sd_bus_message_read(msg, "sssb", &remote, &local, &verify, &force);
if (r < 0)
return r;
class = IMAGE_MACHINE;
flags = 0;
SET_FLAG(flags, IMPORT_FORCE, force);
}
type = startswith(sd_bus_message_get_member(msg), "PullTar") ? TRANSFER_PULL_TAR :
startswith(sd_bus_message_get_member(msg), "PullRaw") ? TRANSFER_PULL_RAW :
streq(sd_bus_message_get_member(msg), "PullOci") ? TRANSFER_PULL_OCI : _TRANSFER_TYPE_INVALID;
assert(type >= 0);
if (type == TRANSFER_PULL_OCI) {
r = oci_ref_valid(remote);
if (r < 0)
return r;
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Reference '%s' is invalid", remote);
} else if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"URL '%s' is invalid", remote);
if (isempty(local))
local = NULL;
else if (!image_name_is_valid(local))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Local image name %s is invalid", local);
if (type == TRANSFER_PULL_OCI)
v = _IMPORT_VERIFY_INVALID;
else if (isempty(verify))
v = IMPORT_VERIFY_SIGNATURE;
else {
v = import_verify_from_string(verify);
if (v < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Unknown verification mode %s", verify);
}
if (class == IMAGE_MACHINE) {
r = image_setup_pool(m->runtime_scope, class, m->use_btrfs_subvol, m->use_btrfs_quota);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to set up machine pool: %m");
}
if (manager_find(m, type, remote))
return sd_bus_error_setf(error, BUS_ERROR_TRANSFER_IN_PROGRESS,
"Transfer for %s already in progress.", remote);
r = transfer_new(m, &t);
if (r < 0)
return r;
t->type = type;
t->verify = v;
t->flags = flags;
t->class = class;
t->remote = strdup(remote);
if (!t->remote)
return -ENOMEM;
if (local) {
t->local = strdup(local);
if (!t->local)
return -ENOMEM;
}
r = transfer_start(t);
if (r < 0)
return r;
r = sd_bus_reply_method_return(msg, "uo", t->id, t->object_path);
if (r < 0)
return r;
TAKE_PTR(t);
return 1;
}
static int method_list_transfers(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
Manager *m = ASSERT_PTR(userdata);
ImageClass class = _IMAGE_CLASS_INVALID;
Transfer *t;
int r;
assert(msg);
bool ex = endswith(sd_bus_message_get_member(msg), "Ex");
if (ex) {
const char *sclass;
uint64_t flags;
r = sd_bus_message_read(msg, "st", &sclass, &flags);
if (r < 0)
return bus_log_parse_error(r);
if (!isempty(sclass)) {
class = image_class_from_string(sclass);
if (class < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Image class '%s' not known", sclass);
}
if (flags != 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Flags 0x%" PRIx64 " invalid", flags);
}
r = sd_bus_message_new_method_return(msg, &reply);
if (r < 0)
return r;
if (ex)
r = sd_bus_message_open_container(reply, 'a', "(ussssdo)");
else
r = sd_bus_message_open_container(reply, 'a', "(usssdo)");
if (r < 0)
return r;
HASHMAP_FOREACH(t, m->transfers) {
if (class >= 0 && class != t->class)
continue;
if (ex)
r = sd_bus_message_append(
reply,
"(ussssdo)",
t->id,
transfer_type_to_string(t->type),
t->remote,
t->local,
image_class_to_string(t->class),
transfer_percent_as_double(t),
t->object_path);
else
r = sd_bus_message_append(
reply,
"(usssdo)",
t->id,
transfer_type_to_string(t->type),
t->remote,
t->local,
transfer_percent_as_double(t),
t->object_path);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_cancel(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
Transfer *t = ASSERT_PTR(userdata);
int r;
assert(msg);
r = bus_verify_polkit_async(
msg,
"org.freedesktop.import1.cancel",
/* details= */ NULL,
&t->manager->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* Will call us back */
r = transfer_cancel(t);
if (r < 0)
return r;
return sd_bus_reply_method_return(msg, NULL);
}
static int method_cancel_transfer(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
Transfer *t;
uint32_t id;
int r;
assert(msg);
r = bus_verify_polkit_async(
msg,
"org.freedesktop.import1.cancel",
/* details= */ NULL,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* Will call us back */
r = sd_bus_message_read(msg, "u", &id);
if (r < 0)
return r;
if (id <= 0)
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid transfer id");
t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
if (!t)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_TRANSFER, "No transfer by id %" PRIu32, id);
r = transfer_cancel(t);
if (r < 0)
return r;
return sd_bus_reply_method_return(msg, NULL);
}
static int method_list_images(sd_bus_message *msg, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
ImageClass class = _IMAGE_CLASS_INVALID;
Manager *m = ASSERT_PTR(userdata);
int r;
assert(msg);
const char *sclass;
uint64_t flags;
r = sd_bus_message_read(msg, "st", &sclass, &flags);
if (r < 0)
return r;
if (!isempty(sclass)) {
class = image_class_from_string(sclass);
if (class < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Image class '%s' not known", sclass);
}
if (flags != 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Flags 0x%" PRIx64 " invalid", flags);
r = sd_bus_message_new_method_return(msg, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, 'a', "(ssssbtttttt)");
if (r < 0)
return r;
for (ImageClass c = class < 0 ? 0 : class;
class < 0 ? (c < _IMAGE_CLASS_MAX) : (c == class);
c++) {
_cleanup_hashmap_free_ Hashmap *images = NULL;
r = image_discover(m->runtime_scope, c, /* root= */ NULL, &images);
if (r < 0) {
if (class >= 0)
return r;
log_warning_errno(r, "Failed to discover images of type %s: %m", image_class_to_string(c));
continue;
}
Image *i;
HASHMAP_FOREACH(i, images) {
r = sd_bus_message_append(
reply,
"(ssssbtttttt)",
image_class_to_string(i->class),
i->name,
image_type_to_string(i->type),
i->path,
image_is_read_only(i),
i->crtime,
i->mtime,
i->usage,
i->usage_exclusive,
i->limit,
i->limit_exclusive);
if (r < 0)
return r;
}
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int property_get_progress(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Transfer *t = ASSERT_PTR(userdata);
assert(bus);
assert(reply);
return sd_bus_message_append(reply, "d", transfer_percent_as_double(t));
}
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, transfer_type, TransferType);
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_verify, import_verify, ImportVerify);
static int transfer_object_find(
sd_bus *bus,
const char *path,
const char *interface,
void *userdata,
void **found,
sd_bus_error *error) {
Manager *m = ASSERT_PTR(userdata);
Transfer *t;
const char *p;
uint32_t id;
int r;
assert(bus);
assert(path);
assert(interface);
assert(found);
p = startswith(path, "/org/freedesktop/import1/transfer/_");
if (!p)
return 0;
r = safe_atou32(p, &id);
if (r < 0 || id == 0)
return 0;
t = hashmap_get(m->transfers, UINT32_TO_PTR(id));
if (!t)
return 0;
*found = t;
return 1;
}
static int transfer_node_enumerator(
sd_bus *bus,
const char *path,
void *userdata,
char ***nodes,
sd_bus_error *error) {
_cleanup_strv_free_ char **l = NULL;
Manager *m = ASSERT_PTR(userdata);
Transfer *t;
unsigned k = 0;
assert(nodes);
l = new0(char*, hashmap_size(m->transfers) + 1);
if (!l)
return -ENOMEM;
HASHMAP_FOREACH(t, m->transfers) {
l[k] = strdup(t->object_path);
if (!l[k])
return -ENOMEM;
k++;
}
*nodes = TAKE_PTR(l);
return 1;
}
static const sd_bus_vtable transfer_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Id", "u", NULL, offsetof(Transfer, id), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Local", "s", NULL, offsetof(Transfer, local), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Remote", "s", NULL, offsetof(Transfer, remote), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Transfer, type), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Verify", "s", property_get_verify, offsetof(Transfer, verify), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0),
SD_BUS_METHOD("Cancel", NULL, NULL, method_cancel, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_SIGNAL_WITH_NAMES("LogMessage",
"us",
SD_BUS_PARAM(priority)
SD_BUS_PARAM(line),
0),
SD_BUS_SIGNAL_WITH_NAMES("ProgressUpdate",
"d",
SD_BUS_PARAM(progress),
0),
SD_BUS_VTABLE_END,
};
static const BusObjectImplementation transfer_object = {
"/org/freedesktop/import1/transfer",
"org.freedesktop.import1.Transfer",
.fallback_vtables = BUS_FALLBACK_VTABLES({transfer_vtable, transfer_object_find}),
.node_enumerator = transfer_node_enumerator,
};
static const sd_bus_vtable manager_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_METHOD_WITH_NAMES("ImportTar",
"hsbb",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(force)
SD_BUS_PARAM(read_only),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ImportTarEx",
"hsst",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(class)
SD_BUS_PARAM(flags),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ImportRaw",
"hsbb",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(force)
SD_BUS_PARAM(read_only),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ImportRawEx",
"hsst",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(class)
SD_BUS_PARAM(flags),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ImportFileSystem",
"hsbb",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(force)
SD_BUS_PARAM(read_only),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_fs,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ImportFileSystemEx",
"hsst",
SD_BUS_PARAM(fd)
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(class)
SD_BUS_PARAM(flags),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_import_fs,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ExportTar",
"shs",
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(fd)
SD_BUS_PARAM(format),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_export_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ExportTarEx",
"sshst",
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(class)
SD_BUS_PARAM(fd)
SD_BUS_PARAM(format)
SD_BUS_PARAM(flags),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_export_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ExportRaw",
"shs",
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(fd)
SD_BUS_PARAM(format),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_export_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ExportRawEx",
"sshst",
SD_BUS_PARAM(local_name)
SD_BUS_PARAM(class)
SD_BUS_PARAM(fd)
SD_BUS_PARAM(format)
SD_BUS_PARAM(flags),
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
method_export_tar_or_raw,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("PullTar",
SD_BUS_ARGS("s", url,
"s", local_name,
"s", verify_mode,
"b", force),
SD_BUS_RESULT("u", transfer_id,
"o", transfer_path),
method_pull_tar_or_raw_or_oci,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("PullTarEx",
SD_BUS_ARGS("s", url,
"s", local_name,
"s", class,
"s", verify_mode,
"t", flags),
SD_BUS_RESULT("u", transfer_id,
"o", transfer_path),
method_pull_tar_or_raw_or_oci,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("PullRaw",
SD_BUS_ARGS("s", url,
"s", local_name,
"s", verify_mode,
"b", force),
SD_BUS_RESULT("u", transfer_id,
"o", transfer_path),
method_pull_tar_or_raw_or_oci,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("PullRawEx",
SD_BUS_ARGS("s", url,
"s", local_name,
"s", class,
"s", verify_mode,
"t", flags),
SD_BUS_RESULT("u", transfer_id,
"o", transfer_path),
method_pull_tar_or_raw_or_oci,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("PullOci",
SD_BUS_ARGS("s", ref,
"s", local_name,
"s", class,
"t", flags),
SD_BUS_RESULT("u", transfer_id,
"o", transfer_path),
method_pull_tar_or_raw_or_oci,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ListTransfers",
NULL,,
"a(usssdo)",
SD_BUS_PARAM(transfers),
method_list_transfers,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ListTransfersEx",
"st",
SD_BUS_PARAM(class)
SD_BUS_PARAM(flags),
"a(ussssdo)",
SD_BUS_PARAM(transfers),
method_list_transfers,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("CancelTransfer",
"u",
SD_BUS_PARAM(transfer_id),
NULL,,
method_cancel_transfer,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_NAMES("ListImages",
"st",
SD_BUS_PARAM(class)
SD_BUS_PARAM(flags),
"a(ssssbtttttt)",
SD_BUS_PARAM(images),
method_list_images,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_SIGNAL_WITH_NAMES("TransferNew",
"uo",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path),
0),
SD_BUS_SIGNAL_WITH_NAMES("TransferRemoved",
"uos",
SD_BUS_PARAM(transfer_id)
SD_BUS_PARAM(transfer_path)
SD_BUS_PARAM(result),
0),
SD_BUS_VTABLE_END,
};
static const BusObjectImplementation manager_object = {
"/org/freedesktop/import1",
"org.freedesktop.import1.Manager",
.vtables = BUS_VTABLES(manager_vtable),
.children = BUS_IMPLEMENTATIONS(&transfer_object),
};
static int manager_connect_bus(Manager *m) {
int r;
assert(m);
assert(m->event);
assert(!m->system_bus);
assert(!m->api_bus);
r = bus_open_system_watch_bind(&m->system_bus);
if (r < 0)
return log_error_errno(r, "Failed to get system bus connection: %m");
r = sd_bus_attach_event(m->system_bus, m->event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach system bus to event loop: %m");
if (m->runtime_scope == RUNTIME_SCOPE_SYSTEM)
m->api_bus = sd_bus_ref(m->system_bus);
else {
assert(m->runtime_scope == RUNTIME_SCOPE_USER);
r = sd_bus_default_user(&m->api_bus);
if (r < 0)
return log_error_errno(r, "Failed to get user bus connection: %m");
r = sd_bus_attach_event(m->api_bus, m->event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach user bus to event loop: %m");
}
r = bus_add_implementation(m->api_bus, &manager_object, m);
if (r < 0)
return r;
r = bus_log_control_api_register(m->api_bus);
if (r < 0)
return r;
r = sd_bus_request_name_async(m->api_bus, NULL, "org.freedesktop.import1", 0, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to request name: %m");
return 0;
}
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_image_class, ImageClass, image_class_from_string);
static int make_transfer_json(Transfer *t, sd_json_variant **ret) {
int r;
assert(t);
r = sd_json_buildo(ret,
SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id),
JSON_BUILD_PAIR_ENUM("type", transfer_type_to_string(t->type)),
SD_JSON_BUILD_PAIR_STRING("remote", t->remote),
SD_JSON_BUILD_PAIR_STRING("local", t->local),
JSON_BUILD_PAIR_ENUM("class", image_class_to_string(t->class)),
SD_JSON_BUILD_PAIR_REAL("percent", transfer_percent_as_double(t)));
if (r < 0)
return log_error_errno(r, "Failed to build transfer JSON data: %m");
return 0;
}
static int vl_method_list_transfers(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
struct p {
ImageClass class;
} p = {
.class = _IMAGE_CLASS_INVALID,
};
static const sd_json_dispatch_field dispatch_table[] = {
{ "class", SD_JSON_VARIANT_STRING, json_dispatch_image_class, offsetof(struct p, class), 0 },
{},
};
Manager *m = ASSERT_PTR(userdata);
int r;
assert(link);
assert(parameters);
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE));
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
if (r != 0)
return r;
r = sd_varlink_set_sentinel(link, "io.systemd.Import.NoTransfers");
if (r < 0)
return r;
Transfer *t;
HASHMAP_FOREACH(t, m->transfers) {
if (p.class >= 0 && p.class != t->class)
continue;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
r = make_transfer_json(t, &v);
if (r < 0)
return r;
r = sd_varlink_reply(link, v);
if (r < 0)
return r;
}
return 0;
}
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_import_verify, ImportVerify, import_verify_from_string);
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_import_type, ImportType, import_type_from_string);
static int vl_method_pull(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
struct p {
const char *remote, *local;
ImageClass class;
ImportType type;
ImportVerify verify;
bool force;
bool read_only;
bool keep_download;
const char *image_root;
} p = {
.class = _IMAGE_CLASS_INVALID,
.verify = IMPORT_VERIFY_SIGNATURE,
};
static const sd_json_dispatch_field dispatch_table[] = {
{ "remote", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct p, remote), SD_JSON_MANDATORY },
{ "local", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(struct p, local), 0 },
{ "class", SD_JSON_VARIANT_STRING, json_dispatch_image_class, offsetof(struct p, class), SD_JSON_MANDATORY },
{ "type", SD_JSON_VARIANT_STRING, json_dispatch_import_type, offsetof(struct p, type), SD_JSON_MANDATORY },
{ "verify", SD_JSON_VARIANT_STRING, json_dispatch_import_verify, offsetof(struct p, verify), SD_JSON_STRICT },
{ "force", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, force), 0 },
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, read_only), 0 },
{ "keepDownload", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(struct p, keep_download), 0 },
{ "imageRoot", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(struct p, image_root), SD_JSON_STRICT },
VARLINK_DISPATCH_POLKIT_FIELD,
{},
};
Manager *m = ASSERT_PTR(userdata);
int r;
assert(link);
assert(parameters);
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p);
if (r != 0)
return r;
if (p.type == IMPORT_OCI) {
r = oci_ref_valid(p.remote);
if (r < 0)
return r;
if (r == 0)
return sd_varlink_error_invalid_parameter_name(link, "remote");
} else if (!http_url_is_valid(p.remote) && !file_url_is_valid(p.remote))
return sd_varlink_error_invalid_parameter_name(link, "remote");
if (p.local && !image_name_is_valid(p.local))
return sd_varlink_error_invalid_parameter_name(link, "local");
uint64_t transfer_flags = (p.force * IMPORT_FORCE) | (p.read_only * IMPORT_READ_ONLY) | (p.keep_download * IMPORT_PULL_KEEP_DOWNLOAD);
TransferType tt =
p.type == IMPORT_TAR ? TRANSFER_PULL_TAR :
p.type == IMPORT_RAW ? TRANSFER_PULL_RAW :
p.type == IMPORT_OCI ? TRANSFER_PULL_OCI : _TRANSFER_TYPE_INVALID;
assert(tt >= 0);
if (manager_find(m, tt, p.remote))
return sd_varlink_errorbo(link, "io.systemd.Import.AlreadyInProgress", SD_JSON_BUILD_PAIR_STRING("remote", p.remote));
if (m->runtime_scope != RUNTIME_SCOPE_USER) {
r = varlink_verify_polkit_async(
link,
m->system_bus,
"org.freedesktop.import1.pull",
(const char**) STRV_MAKE(
"remote", p.remote,
"local", p.local,
"class", image_class_to_string(p.class),
"type", import_type_to_string(p.type),
"verify", import_verify_to_string(p.verify)),
&m->polkit_registry);
if (r <= 0)
return r;
}
_cleanup_(transfer_unrefp) Transfer *t = NULL;
r = transfer_new(m, &t);
if (r < 0)
return r;
t->type = tt;
t->verify = p.verify;
t->flags = transfer_flags;
t->class = p.class;
t->remote = strdup(p.remote);
if (!t->remote)
return -ENOMEM;
if (p.local) {
t->local = strdup(p.local);
if (!t->local)
return -ENOMEM;
}
if (p.image_root) {
t->image_root = strdup(p.image_root);
if (!t->image_root)
return -ENOMEM;
}
r = transfer_start(t);
if (r < 0)
return r;
/* If more was not set, just return the download id, and be done with it */
if (!FLAGS_SET(flags, SD_VARLINK_METHOD_MORE))
return sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id));
/* Otherwise add this connection to the set of subscriptions, return the id, but keep the thing running */
r = set_ensure_put(&t->varlink_subscribed, &varlink_hash_ops, link);
if (r < 0)
return r;
sd_varlink_ref(link);
r = sd_varlink_notifybo(link, SD_JSON_BUILD_PAIR_UNSIGNED("id", t->id));
if (r < 0)
return r;
TAKE_PTR(t);
return 0;
}
static int manager_connect_varlink(Manager *m) {