| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "bus-get-properties.h" |
| 9 |
#include "bus-label.h" |
| 10 |
#include "bus-object.h" |
| 11 |
#include "bus-polkit.h" |
| 12 |
#include "bus-util.h" |
| 13 |
#include "discover-image.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "hashmap.h" |
| 16 |
#include "image-dbus.h" |
| 17 |
#include "image-policy.h" |
| 18 |
#include "io-util.h" |
| 19 |
#include "machined.h" |
| 20 |
#include "operation.h" |
| 21 |
#include "os-util.h" |
| 22 |
#include "process-util.h" |
| 23 |
#include "strv.h" |
| 24 |
|
| 25 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, image_type, ImageType); |
| 26 |
|
| 27 |
int bus_image_method_remove( |
| 28 |
sd_bus_message *message, |
| 29 |
void *userdata, |
| 30 |
sd_bus_error *error) { |
| 31 |
|
| 32 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 33 |
Image *image = ASSERT_PTR(userdata); |
| 34 |
Manager *m = image->userdata; |
| 35 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 36 |
int r; |
| 37 |
|
| 38 |
assert(message); |
| 39 |
|
| 40 |
if (m->n_operations >= OPERATIONS_MAX) |
| 41 |
return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); |
| 42 |
|
| 43 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 44 |
const char *details[] = { |
| 45 |
"image", image->name, |
| 46 |
"verb", "remove", |
| 47 |
NULL |
| 48 |
}; |
| 49 |
|
| 50 |
r = bus_verify_polkit_async( |
| 51 |
message, |
| 52 |
"org.freedesktop.machine1.manage-images", |
| 53 |
details, |
| 54 |
&m->polkit_registry, |
| 55 |
error); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
if (r == 0) |
| 59 |
return 1; /* Will call us back */ |
| 60 |
} |
| 61 |
|
| 62 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 63 |
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); |
| 64 |
|
| 65 |
r = pidref_safe_fork("(sd-imgrm)", FORK_RESET_SIGNALS, &child); |
| 66 |
if (r < 0) |
| 67 |
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); |
| 68 |
if (r == 0) { |
| 69 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 70 |
r = image_remove(image, m->runtime_scope); |
| 71 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 72 |
} |
| 73 |
|
| 74 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 75 |
|
| 76 |
r = operation_new_with_bus_reply(m, /* machine= */ NULL, &child, message, errno_pipe_fd[0], /* ret= */ NULL); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
|
| 80 |
TAKE_PIDREF(child); |
| 81 |
|
| 82 |
errno_pipe_fd[0] = -EBADF; |
| 83 |
|
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
int bus_image_method_rename( |
| 88 |
sd_bus_message *message, |
| 89 |
void *userdata, |
| 90 |
sd_bus_error *error) { |
| 91 |
|
| 92 |
Image *image = ASSERT_PTR(userdata); |
| 93 |
Manager *m = image->userdata; |
| 94 |
const char *new_name; |
| 95 |
int r; |
| 96 |
|
| 97 |
assert(message); |
| 98 |
|
| 99 |
r = sd_bus_message_read(message, "s", &new_name); |
| 100 |
if (r < 0) |
| 101 |
return r; |
| 102 |
|
| 103 |
if (!image_name_is_valid(new_name)) |
| 104 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", new_name); |
| 105 |
|
| 106 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 107 |
const char *details[] = { |
| 108 |
"image", image->name, |
| 109 |
"verb", "rename", |
| 110 |
"new_name", new_name, |
| 111 |
NULL |
| 112 |
}; |
| 113 |
|
| 114 |
r = bus_verify_polkit_async( |
| 115 |
message, |
| 116 |
"org.freedesktop.machine1.manage-images", |
| 117 |
details, |
| 118 |
&m->polkit_registry, |
| 119 |
error); |
| 120 |
if (r < 0) |
| 121 |
return r; |
| 122 |
if (r == 0) |
| 123 |
return 1; /* Will call us back */ |
| 124 |
} |
| 125 |
|
| 126 |
r = rename_image_and_update_cache(m, image, new_name); |
| 127 |
if (r < 0) |
| 128 |
return sd_bus_error_set_errnof(error, r, "Failed to rename image: %m"); |
| 129 |
|
| 130 |
return sd_bus_reply_method_return(message, NULL); |
| 131 |
} |
| 132 |
|
| 133 |
int bus_image_method_clone( |
| 134 |
sd_bus_message *message, |
| 135 |
void *userdata, |
| 136 |
sd_bus_error *error) { |
| 137 |
|
| 138 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 139 |
Image *image = ASSERT_PTR(userdata); |
| 140 |
Manager *m = ASSERT_PTR(image->userdata); |
| 141 |
const char *new_name; |
| 142 |
int r, read_only; |
| 143 |
_cleanup_(pidref_done_sigkill_wait) PidRef child = PIDREF_NULL; |
| 144 |
|
| 145 |
assert(message); |
| 146 |
|
| 147 |
if (m->n_operations >= OPERATIONS_MAX) |
| 148 |
return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); |
| 149 |
|
| 150 |
r = sd_bus_message_read(message, "sb", &new_name, &read_only); |
| 151 |
if (r < 0) |
| 152 |
return r; |
| 153 |
|
| 154 |
if (!image_name_is_valid(new_name)) |
| 155 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", new_name); |
| 156 |
|
| 157 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 158 |
const char *details[] = { |
| 159 |
"image", image->name, |
| 160 |
"verb", "clone", |
| 161 |
"new_name", new_name, |
| 162 |
NULL |
| 163 |
}; |
| 164 |
|
| 165 |
r = bus_verify_polkit_async( |
| 166 |
message, |
| 167 |
"org.freedesktop.machine1.manage-images", |
| 168 |
details, |
| 169 |
&m->polkit_registry, |
| 170 |
error); |
| 171 |
if (r < 0) |
| 172 |
return r; |
| 173 |
if (r == 0) |
| 174 |
return 1; /* Will call us back */ |
| 175 |
} |
| 176 |
|
| 177 |
if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
| 178 |
return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); |
| 179 |
|
| 180 |
r = pidref_safe_fork("(sd-imgclone)", FORK_RESET_SIGNALS, &child); |
| 181 |
if (r < 0) |
| 182 |
return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); |
| 183 |
if (r == 0) { |
| 184 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 185 |
r = image_clone(image, new_name, read_only, m->runtime_scope); |
| 186 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 187 |
} |
| 188 |
|
| 189 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 190 |
|
| 191 |
r = operation_new_with_bus_reply(m, /* machine= */ NULL, &child, message, errno_pipe_fd[0], /* ret= */ NULL); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
TAKE_PIDREF(child); |
| 196 |
|
| 197 |
errno_pipe_fd[0] = -EBADF; |
| 198 |
|
| 199 |
return 1; |
| 200 |
} |
| 201 |
|
| 202 |
int bus_image_method_mark_read_only( |
| 203 |
sd_bus_message *message, |
| 204 |
void *userdata, |
| 205 |
sd_bus_error *error) { |
| 206 |
|
| 207 |
Image *image = userdata; |
| 208 |
Manager *m = ASSERT_PTR(image->userdata); |
| 209 |
int read_only, r; |
| 210 |
|
| 211 |
assert(message); |
| 212 |
|
| 213 |
r = sd_bus_message_read(message, "b", &read_only); |
| 214 |
if (r < 0) |
| 215 |
return r; |
| 216 |
|
| 217 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 218 |
const char *details[] = { |
| 219 |
"image", image->name, |
| 220 |
"verb", "mark_read_only", |
| 221 |
"read_only", one_zero(read_only), |
| 222 |
NULL |
| 223 |
}; |
| 224 |
|
| 225 |
r = bus_verify_polkit_async( |
| 226 |
message, |
| 227 |
"org.freedesktop.machine1.manage-images", |
| 228 |
details, |
| 229 |
&m->polkit_registry, |
| 230 |
error); |
| 231 |
if (r < 0) |
| 232 |
return r; |
| 233 |
if (r == 0) |
| 234 |
return 1; /* Will call us back */ |
| 235 |
} |
| 236 |
|
| 237 |
r = image_read_only(image, read_only, m->runtime_scope); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
return sd_bus_reply_method_return(message, NULL); |
| 242 |
} |
| 243 |
|
| 244 |
int bus_image_method_set_limit( |
| 245 |
sd_bus_message *message, |
| 246 |
void *userdata, |
| 247 |
sd_bus_error *error) { |
| 248 |
|
| 249 |
Image *image = userdata; |
| 250 |
Manager *m = image->userdata; |
| 251 |
uint64_t limit; |
| 252 |
int r; |
| 253 |
|
| 254 |
assert(message); |
| 255 |
|
| 256 |
r = sd_bus_message_read(message, "t", &limit); |
| 257 |
if (r < 0) |
| 258 |
return r; |
| 259 |
if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
| 260 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); |
| 261 |
|
| 262 |
if (m->runtime_scope != RUNTIME_SCOPE_USER) { |
| 263 |
const char *details[] = { |
| 264 |
"machine", image->name, |
| 265 |
"verb", "set_limit", |
| 266 |
NULL |
| 267 |
}; |
| 268 |
|
| 269 |
r = bus_verify_polkit_async( |
| 270 |
message, |
| 271 |
"org.freedesktop.machine1.manage-images", |
| 272 |
details, |
| 273 |
&m->polkit_registry, |
| 274 |
error); |
| 275 |
if (r < 0) |
| 276 |
return r; |
| 277 |
if (r == 0) |
| 278 |
return 1; /* Will call us back */ |
| 279 |
} |
| 280 |
|
| 281 |
r = image_set_limit(image, limit); |
| 282 |
if (r < 0) |
| 283 |
return r; |
| 284 |
|
| 285 |
return sd_bus_reply_method_return(message, NULL); |
| 286 |
} |
| 287 |
|
| 288 |
int bus_image_method_get_hostname( |
| 289 |
sd_bus_message *message, |
| 290 |
void *userdata, |
| 291 |
sd_bus_error *error) { |
| 292 |
|
| 293 |
Image *image = ASSERT_PTR(userdata); |
| 294 |
Manager *m = ASSERT_PTR(image->userdata); |
| 295 |
int r; |
| 296 |
|
| 297 |
if (!image->metadata_valid) { |
| 298 |
r = image_read_metadata(image, /* root= */ NULL, &image_policy_container, m->runtime_scope); |
| 299 |
if (r < 0) |
| 300 |
return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m"); |
| 301 |
} |
| 302 |
|
| 303 |
return sd_bus_reply_method_return(message, "s", image->hostname); |
| 304 |
} |
| 305 |
|
| 306 |
int bus_image_method_get_machine_id( |
| 307 |
sd_bus_message *message, |
| 308 |
void *userdata, |
| 309 |
sd_bus_error *error) { |
| 310 |
|
| 311 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 312 |
Image *image = ASSERT_PTR(userdata); |
| 313 |
Manager *m = ASSERT_PTR(image->userdata); |
| 314 |
int r; |
| 315 |
|
| 316 |
if (!image->metadata_valid) { |
| 317 |
r = image_read_metadata(image, /* root= */ NULL, &image_policy_container, m->runtime_scope); |
| 318 |
if (r < 0) |
| 319 |
return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m"); |
| 320 |
} |
| 321 |
|
| 322 |
r = sd_bus_message_new_method_return(message, &reply); |
| 323 |
if (r < 0) |
| 324 |
return r; |
| 325 |
|
| 326 |
if (sd_id128_is_null(image->machine_id)) /* Add an empty array if the ID is zero */ |
| 327 |
r = sd_bus_message_append(reply, "ay", 0); |
| 328 |
else |
| 329 |
r = sd_bus_message_append_array(reply, 'y', image->machine_id.bytes, 16); |
| 330 |
if (r < 0) |
| 331 |
return r; |
| 332 |
|
| 333 |
return sd_bus_message_send(reply); |
| 334 |
} |
| 335 |
|
| 336 |
int bus_image_method_get_machine_info( |
| 337 |
sd_bus_message *message, |
| 338 |
void *userdata, |
| 339 |
sd_bus_error *error) { |
| 340 |
|
| 341 |
Image *image = ASSERT_PTR(userdata); |
| 342 |
Manager *m = ASSERT_PTR(image->userdata); |
| 343 |
int r; |
| 344 |
|
| 345 |
if (!image->metadata_valid) { |
| 346 |
r = image_read_metadata(image, /* root= */ NULL, &image_policy_container, m->runtime_scope); |
| 347 |
if (r < 0) |
| 348 |
return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m"); |
| 349 |
} |
| 350 |
|
| 351 |
return bus_reply_pair_array(message, image->machine_info); |
| 352 |
} |
| 353 |
|
| 354 |
int bus_image_method_get_os_release( |
| 355 |
sd_bus_message *message, |
| 356 |
void *userdata, |
| 357 |
sd_bus_error *error) { |
| 358 |
|
| 359 |
Image *image = ASSERT_PTR(userdata); |
| 360 |
Manager *m = ASSERT_PTR(image->userdata); |
| 361 |
int r; |
| 362 |
|
| 363 |
if (!image->metadata_valid) { |
| 364 |
r = image_read_metadata(image, /* root= */ NULL, &image_policy_container, m->runtime_scope); |
| 365 |
if (r < 0) |
| 366 |
return sd_bus_error_set_errnof(error, r, "Failed to read image metadata: %m"); |
| 367 |
} |
| 368 |
|
| 369 |
return bus_reply_pair_array(message, image->os_release); |
| 370 |
} |
| 371 |
|
| 372 |
static int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
| 373 |
_cleanup_free_ char *e = NULL; |
| 374 |
Manager *m = userdata; |
| 375 |
Image *image; |
| 376 |
const char *p; |
| 377 |
int r; |
| 378 |
|
| 379 |
assert(bus); |
| 380 |
assert(path); |
| 381 |
assert(interface); |
| 382 |
assert(found); |
| 383 |
|
| 384 |
p = startswith(path, "/org/freedesktop/machine1/image/"); |
| 385 |
if (!p) |
| 386 |
return 0; |
| 387 |
|
| 388 |
e = bus_label_unescape(p); |
| 389 |
if (!e) |
| 390 |
return -ENOMEM; |
| 391 |
|
| 392 |
r = manager_acquire_image(m, e, &image); |
| 393 |
if (r == -ENOENT) |
| 394 |
return 0; |
| 395 |
if (r < 0) |
| 396 |
return r; |
| 397 |
|
| 398 |
*found = image; |
| 399 |
return 1; |
| 400 |
} |
| 401 |
|
| 402 |
char* image_bus_path(const char *name) { |
| 403 |
_cleanup_free_ char *e = NULL; |
| 404 |
|
| 405 |
assert(name); |
| 406 |
|
| 407 |
e = bus_label_escape(name); |
| 408 |
if (!e) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
return strjoin("/org/freedesktop/machine1/image/", e); |
| 412 |
} |
| 413 |
|
| 414 |
static int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
| 415 |
Manager *m = ASSERT_PTR(userdata); |
| 416 |
int r; |
| 417 |
|
| 418 |
assert(bus); |
| 419 |
assert(path); |
| 420 |
assert(nodes); |
| 421 |
|
| 422 |
_cleanup_hashmap_free_ Hashmap *images = NULL; |
| 423 |
r = image_discover(m->runtime_scope, IMAGE_MACHINE, NULL, &images); |
| 424 |
if (r < 0) |
| 425 |
return r; |
| 426 |
|
| 427 |
_cleanup_strv_free_ char **l = NULL; |
| 428 |
Image *image; |
| 429 |
HASHMAP_FOREACH(image, images) { |
| 430 |
char *p; |
| 431 |
|
| 432 |
p = image_bus_path(image->name); |
| 433 |
if (!p) |
| 434 |
return -ENOMEM; |
| 435 |
|
| 436 |
r = strv_consume(&l, p); |
| 437 |
if (r < 0) |
| 438 |
return r; |
| 439 |
} |
| 440 |
|
| 441 |
*nodes = TAKE_PTR(l); |
| 442 |
|
| 443 |
return 1; |
| 444 |
} |
| 445 |
|
| 446 |
static const sd_bus_vtable image_vtable[] = { |
| 447 |
SD_BUS_VTABLE_START(0), |
| 448 |
SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Image, name), 0), |
| 449 |
SD_BUS_PROPERTY("Path", "s", NULL, offsetof(Image, path), 0), |
| 450 |
SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Image, type), 0), |
| 451 |
SD_BUS_PROPERTY("ReadOnly", "b", bus_property_get_image_is_read_only, 0, 0), |
| 452 |
SD_BUS_PROPERTY("CreationTimestamp", "t", NULL, offsetof(Image, crtime), 0), |
| 453 |
SD_BUS_PROPERTY("ModificationTimestamp", "t", NULL, offsetof(Image, mtime), 0), |
| 454 |
SD_BUS_PROPERTY("Usage", "t", NULL, offsetof(Image, usage), 0), |
| 455 |
SD_BUS_PROPERTY("Limit", "t", NULL, offsetof(Image, limit), 0), |
| 456 |
SD_BUS_PROPERTY("UsageExclusive", "t", NULL, offsetof(Image, usage_exclusive), 0), |
| 457 |
SD_BUS_PROPERTY("LimitExclusive", "t", NULL, offsetof(Image, limit_exclusive), 0), |
| 458 |
SD_BUS_METHOD("Remove", NULL, NULL, bus_image_method_remove, SD_BUS_VTABLE_UNPRIVILEGED), |
| 459 |
SD_BUS_METHOD("Rename", "s", NULL, bus_image_method_rename, SD_BUS_VTABLE_UNPRIVILEGED), |
| 460 |
SD_BUS_METHOD("Clone", "sb", NULL, bus_image_method_clone, SD_BUS_VTABLE_UNPRIVILEGED), |
| 461 |
SD_BUS_METHOD("MarkReadOnly", "b", NULL, bus_image_method_mark_read_only, SD_BUS_VTABLE_UNPRIVILEGED), |
| 462 |
SD_BUS_METHOD("SetLimit", "t", NULL, bus_image_method_set_limit, SD_BUS_VTABLE_UNPRIVILEGED), |
| 463 |
SD_BUS_METHOD("GetHostname", NULL, "s", bus_image_method_get_hostname, SD_BUS_VTABLE_UNPRIVILEGED), |
| 464 |
SD_BUS_METHOD("GetMachineID", NULL, "ay", bus_image_method_get_machine_id, SD_BUS_VTABLE_UNPRIVILEGED), |
| 465 |
SD_BUS_METHOD("GetMachineInfo", NULL, "a{ss}", bus_image_method_get_machine_info, SD_BUS_VTABLE_UNPRIVILEGED), |
| 466 |
SD_BUS_METHOD("GetOSRelease", NULL, "a{ss}", bus_image_method_get_os_release, SD_BUS_VTABLE_UNPRIVILEGED), |
| 467 |
SD_BUS_VTABLE_END |
| 468 |
}; |
| 469 |
|
| 470 |
const BusObjectImplementation image_object = { |
| 471 |
"/org/freedesktop/machine1/image", |
| 472 |
"org.freedesktop.machine1.Image", |
| 473 |
.fallback_vtables = BUS_FALLBACK_VTABLES({image_vtable, image_object_find}), |
| 474 |
.node_enumerator = image_node_enumerator, |
| 475 |
}; |