| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-common-errors.h" |
| 7 |
#include "bus-get-properties.h" |
| 8 |
#include "bus-label.h" |
| 9 |
#include "bus-object.h" |
| 10 |
#include "bus-polkit.h" |
| 11 |
#include "bus-util.h" |
| 12 |
#include "copy.h" |
| 13 |
#include "env-util.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "hashmap.h" |
| 17 |
#include "in-addr-util.h" |
| 18 |
#include "local-addresses.h" |
| 19 |
#include "machine.h" |
| 20 |
#include "machine-dbus.h" |
| 21 |
#include "machined.h" |
| 22 |
#include "mount-util.h" |
| 23 |
#include "namespace-util.h" |
| 24 |
#include "operation.h" |
| 25 |
#include "path-util.h" |
| 26 |
#include "signal-util.h" |
| 27 |
#include "string-util.h" |
| 28 |
#include "strv.h" |
| 29 |
#include "user-util.h" |
| 30 |
|
| 31 |
static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, machine_class, MachineClass); |
| 32 |
static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", Machine, machine_get_state, machine_state_to_string); |
| 33 |
|
| 34 |
static int property_get_netif( |
| 35 |
sd_bus *bus, |
| 36 |
const char *path, |
| 37 |
const char *interface, |
| 38 |
const char *property, |
| 39 |
sd_bus_message *reply, |
| 40 |
void *userdata, |
| 41 |
sd_bus_error *error) { |
| 42 |
|
| 43 |
Machine *m = ASSERT_PTR(userdata); |
| 44 |
|
| 45 |
assert(bus); |
| 46 |
assert(reply); |
| 47 |
|
| 48 |
assert_cc(sizeof(int) == sizeof(int32_t)); |
| 49 |
|
| 50 |
return sd_bus_message_append_array(reply, 'i', m->netif, m->n_netif * sizeof(int)); |
| 51 |
} |
| 52 |
|
| 53 |
int bus_machine_method_unregister(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 54 |
Machine *m = ASSERT_PTR(userdata); |
| 55 |
int r; |
| 56 |
|
| 57 |
assert(message); |
| 58 |
|
| 59 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 60 |
const char *details[] = { |
| 61 |
"machine", m->name, |
| 62 |
"verb", "unregister", |
| 63 |
NULL |
| 64 |
}; |
| 65 |
|
| 66 |
r = bus_verify_polkit_async_full( |
| 67 |
message, |
| 68 |
"org.freedesktop.machine1.manage-machines", |
| 69 |
details, |
| 70 |
m->uid, |
| 71 |
/* flags= */ 0, |
| 72 |
&m->manager->polkit_registry, |
| 73 |
error); |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
if (r == 0) |
| 77 |
return 1; /* Will call us back */ |
| 78 |
} |
| 79 |
|
| 80 |
r = machine_finalize(m); |
| 81 |
if (r < 0) |
| 82 |
return r; |
| 83 |
|
| 84 |
return sd_bus_reply_method_return(message, NULL); |
| 85 |
} |
| 86 |
|
| 87 |
int bus_machine_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 88 |
Machine *m = ASSERT_PTR(userdata); |
| 89 |
int r; |
| 90 |
|
| 91 |
assert(message); |
| 92 |
|
| 93 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 94 |
const char *details[] = { |
| 95 |
"machine", m->name, |
| 96 |
"verb", "terminate", |
| 97 |
NULL |
| 98 |
}; |
| 99 |
|
| 100 |
r = bus_verify_polkit_async_full( |
| 101 |
message, |
| 102 |
"org.freedesktop.machine1.manage-machines", |
| 103 |
details, |
| 104 |
m->uid, |
| 105 |
/* flags= */ 0, |
| 106 |
&m->manager->polkit_registry, |
| 107 |
error); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
if (r == 0) |
| 111 |
return 1; /* Will call us back */ |
| 112 |
} |
| 113 |
|
| 114 |
r = machine_stop(m); |
| 115 |
if (r < 0) |
| 116 |
return r; |
| 117 |
|
| 118 |
return sd_bus_reply_method_return(message, NULL); |
| 119 |
} |
| 120 |
|
| 121 |
int bus_machine_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 122 |
Machine *m = ASSERT_PTR(userdata); |
| 123 |
const char *swho; |
| 124 |
int32_t signo; |
| 125 |
KillWhom whom; |
| 126 |
int r; |
| 127 |
|
| 128 |
assert(message); |
| 129 |
|
| 130 |
r = sd_bus_message_read(message, "si", &swho, &signo); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
|
| 134 |
if (isempty(swho)) |
| 135 |
whom = KILL_ALL; |
| 136 |
else { |
| 137 |
whom = kill_whom_from_string(swho); |
| 138 |
if (whom < 0) |
| 139 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho); |
| 140 |
} |
| 141 |
|
| 142 |
if (!SIGNAL_VALID(signo)) |
| 143 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
| 144 |
|
| 145 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 146 |
const char *details[] = { |
| 147 |
"machine", m->name, |
| 148 |
"verb", "kill", |
| 149 |
NULL |
| 150 |
}; |
| 151 |
|
| 152 |
r = bus_verify_polkit_async_full( |
| 153 |
message, |
| 154 |
"org.freedesktop.machine1.manage-machines", |
| 155 |
details, |
| 156 |
m->uid, |
| 157 |
/* flags= */ 0, |
| 158 |
&m->manager->polkit_registry, |
| 159 |
error); |
| 160 |
if (r < 0) |
| 161 |
return r; |
| 162 |
if (r == 0) |
| 163 |
return 1; /* Will call us back */ |
| 164 |
} |
| 165 |
|
| 166 |
r = machine_kill(m, whom, signo); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
return sd_bus_reply_method_return(message, NULL); |
| 171 |
} |
| 172 |
|
| 173 |
int bus_machine_method_get_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 174 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 175 |
_cleanup_free_ struct local_address *addresses = NULL; |
| 176 |
Machine *m = ASSERT_PTR(userdata); |
| 177 |
int r; |
| 178 |
|
| 179 |
assert(message); |
| 180 |
|
| 181 |
r = sd_bus_message_new_method_return(message, &reply); |
| 182 |
if (r < 0) |
| 183 |
return r; |
| 184 |
|
| 185 |
r = sd_bus_message_open_container(reply, 'a', "(iay)"); |
| 186 |
if (r < 0) |
| 187 |
return r; |
| 188 |
|
| 189 |
int n = machine_get_addresses(m, &addresses); |
| 190 |
if (n == -ENONET) |
| 191 |
return sd_bus_error_setf(error, BUS_ERROR_NO_PRIVATE_NETWORKING, "Machine %s does not use private networking", m->name); |
| 192 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(n)) |
| 193 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Requesting IP address data is only supported on container machines."); |
| 194 |
if (n < 0) |
| 195 |
return sd_bus_error_set_errnof(error, n, "Failed to get addresses: %m"); |
| 196 |
|
| 197 |
for (int i = 0; i < n; i++) { |
| 198 |
r = sd_bus_message_open_container(reply, 'r', "iay"); |
| 199 |
if (r < 0) |
| 200 |
return r; |
| 201 |
|
| 202 |
r = sd_bus_message_append(reply, "i", addresses[i].family); |
| 203 |
if (r < 0) |
| 204 |
return r; |
| 205 |
|
| 206 |
r = sd_bus_message_append_array(reply, 'y', &addresses[i].address, FAMILY_ADDRESS_SIZE(addresses[i].family)); |
| 207 |
if (r < 0) |
| 208 |
return r; |
| 209 |
|
| 210 |
r = sd_bus_message_close_container(reply); |
| 211 |
if (r < 0) |
| 212 |
return r; |
| 213 |
} |
| 214 |
|
| 215 |
r = sd_bus_message_close_container(reply); |
| 216 |
if (r < 0) |
| 217 |
return r; |
| 218 |
|
| 219 |
return sd_bus_message_send(reply); |
| 220 |
} |
| 221 |
|
| 222 |
int bus_machine_method_get_ssh_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 223 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 224 |
Machine *m = ASSERT_PTR(userdata); |
| 225 |
int r; |
| 226 |
|
| 227 |
assert(message); |
| 228 |
|
| 229 |
r = sd_bus_message_new_method_return(message, &reply); |
| 230 |
if (r < 0) |
| 231 |
return r; |
| 232 |
|
| 233 |
if (!m->ssh_address || !m->ssh_private_key_path) |
| 234 |
return -ENOENT; |
| 235 |
|
| 236 |
r = sd_bus_message_append(reply, "ss", m->ssh_address, m->ssh_private_key_path); |
| 237 |
if (r < 0) |
| 238 |
return r; |
| 239 |
|
| 240 |
return sd_bus_message_send(reply); |
| 241 |
} |
| 242 |
|
| 243 |
int bus_machine_method_get_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 244 |
_cleanup_strv_free_ char **l = NULL; |
| 245 |
Machine *m = ASSERT_PTR(userdata); |
| 246 |
int r; |
| 247 |
|
| 248 |
assert(message); |
| 249 |
|
| 250 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 251 |
const char *details[] = { |
| 252 |
"machine", m->name, |
| 253 |
"verb", "get_os_release", |
| 254 |
NULL |
| 255 |
}; |
| 256 |
|
| 257 |
r = bus_verify_polkit_async( |
| 258 |
message, |
| 259 |
"org.freedesktop.machine1.inspect-machines", |
| 260 |
details, |
| 261 |
&m->manager->polkit_registry, |
| 262 |
error); |
| 263 |
if (r < 0) |
| 264 |
return r; |
| 265 |
if (r == 0) |
| 266 |
return 1; /* Will call us back */ |
| 267 |
} |
| 268 |
|
| 269 |
r = machine_get_os_release(m, &l); |
| 270 |
if (r == -ENONET) |
| 271 |
return sd_bus_error_set(error, SD_BUS_ERROR_FAILED, "Machine does not contain OS release information."); |
| 272 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 273 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Requesting OS release data is only supported on container machines."); |
| 274 |
if (r < 0) |
| 275 |
return sd_bus_error_set_errnof(error, r, "Failed to get OS release: %m"); |
| 276 |
|
| 277 |
return bus_reply_pair_array(message, l); |
| 278 |
} |
| 279 |
|
| 280 |
int bus_machine_method_open_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 281 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 282 |
_cleanup_free_ char *pty_name = NULL; |
| 283 |
_cleanup_close_ int master = -EBADF; |
| 284 |
Machine *m = ASSERT_PTR(userdata); |
| 285 |
int r; |
| 286 |
|
| 287 |
assert(message); |
| 288 |
|
| 289 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 290 |
const char *details[] = { |
| 291 |
"machine", m->name, |
| 292 |
NULL |
| 293 |
}; |
| 294 |
|
| 295 |
r = bus_verify_polkit_async_full( |
| 296 |
message, |
| 297 |
m->class == MACHINE_HOST ? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty", |
| 298 |
details, |
| 299 |
m->uid, |
| 300 |
/* flags= */ 0, |
| 301 |
&m->manager->polkit_registry, |
| 302 |
error); |
| 303 |
if (r < 0) |
| 304 |
return r; |
| 305 |
if (r == 0) |
| 306 |
return 1; /* Will call us back */ |
| 307 |
} |
| 308 |
|
| 309 |
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name); |
| 310 |
if (master < 0) |
| 311 |
return master; |
| 312 |
|
| 313 |
r = sd_bus_message_new_method_return(message, &reply); |
| 314 |
if (r < 0) |
| 315 |
return r; |
| 316 |
|
| 317 |
r = sd_bus_message_append(reply, "hs", master, pty_name); |
| 318 |
if (r < 0) |
| 319 |
return r; |
| 320 |
|
| 321 |
return sd_bus_message_send(reply); |
| 322 |
} |
| 323 |
|
| 324 |
int bus_machine_method_open_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 325 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 326 |
_cleanup_free_ char *pty_name = NULL; |
| 327 |
_cleanup_close_ int master = -EBADF; |
| 328 |
Machine *m = ASSERT_PTR(userdata); |
| 329 |
int r; |
| 330 |
|
| 331 |
assert(message); |
| 332 |
|
| 333 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 334 |
const char *details[] = { |
| 335 |
"machine", m->name, |
| 336 |
"verb", "login", |
| 337 |
NULL |
| 338 |
}; |
| 339 |
|
| 340 |
r = bus_verify_polkit_async_full( |
| 341 |
message, |
| 342 |
m->class == MACHINE_HOST ? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login", |
| 343 |
details, |
| 344 |
m->uid, |
| 345 |
/* flags= */ 0, |
| 346 |
&m->manager->polkit_registry, |
| 347 |
error); |
| 348 |
if (r < 0) |
| 349 |
return r; |
| 350 |
if (r == 0) |
| 351 |
return 1; /* Will call us back */ |
| 352 |
} |
| 353 |
|
| 354 |
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name); |
| 355 |
if (master < 0) |
| 356 |
return master; |
| 357 |
|
| 358 |
r = machine_start_getty(m, pty_name, error); |
| 359 |
if (r < 0) |
| 360 |
return r; |
| 361 |
|
| 362 |
r = sd_bus_message_new_method_return(message, &reply); |
| 363 |
if (r < 0) |
| 364 |
return r; |
| 365 |
|
| 366 |
r = sd_bus_message_append(reply, "hs", master, pty_name); |
| 367 |
if (r < 0) |
| 368 |
return r; |
| 369 |
|
| 370 |
return sd_bus_message_send(reply); |
| 371 |
} |
| 372 |
|
| 373 |
int bus_machine_method_open_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 374 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 375 |
_cleanup_free_ char *pty_name = NULL; |
| 376 |
_cleanup_close_ int master = -EBADF; |
| 377 |
_cleanup_strv_free_ char **env = NULL, **args_wire = NULL, **args = NULL; |
| 378 |
Machine *m = ASSERT_PTR(userdata); |
| 379 |
const char *user, *path; |
| 380 |
int r; |
| 381 |
|
| 382 |
assert(message); |
| 383 |
|
| 384 |
r = sd_bus_message_read(message, "ss", &user, &path); |
| 385 |
if (r < 0) |
| 386 |
return r; |
| 387 |
user = isempty(user) ? "root" : user; |
| 388 |
|
| 389 |
if (!valid_user_group_name(user, VALID_USER_RELAX)) |
| 390 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user name '%s'", user); |
| 391 |
|
| 392 |
/* Ensure only root can shell into the root namespace. This is to avoid unprivileged users registering |
| 393 |
* a process they own in the root user namespace, and then shelling in as root or another user. Note that |
| 394 |
* the shell operation is privileged and requires 'auth_admin', so we do not need to check the caller's uid, |
| 395 |
* as that will be checked by polkit, and if the machine's and the caller's do not match, authorization |
| 396 |
* will be required. It's only the case where the caller owns the machine that will be shortcut and needs |
| 397 |
* to be checked here. */ |
| 398 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER && m->uid != 0) { |
| 399 |
assert(m->class != MACHINE_HOST); |
| 400 |
|
| 401 |
r = pidref_in_same_namespace(&PIDREF_MAKE_FROM_PID(1), &m->leader, NAMESPACE_USER); |
| 402 |
if (r < 0) |
| 403 |
return log_debug_errno( |
| 404 |
r, |
| 405 |
"Failed to check if machine '%s' is running in the root user namespace: %m", |
| 406 |
m->name); |
| 407 |
if (r > 0) |
| 408 |
return sd_bus_error_set( |
| 409 |
error, |
| 410 |
SD_BUS_ERROR_ACCESS_DENIED, |
| 411 |
"Only root may shell into the root user namespace"); |
| 412 |
} |
| 413 |
|
| 414 |
r = sd_bus_message_read_strv(message, &args_wire); |
| 415 |
if (r < 0) |
| 416 |
return r; |
| 417 |
if (isempty(path)) { |
| 418 |
path = machine_default_shell_path(); |
| 419 |
args = machine_default_shell_args(user); |
| 420 |
if (!args) |
| 421 |
return -ENOMEM; |
| 422 |
} else { |
| 423 |
if (!path_is_absolute(path)) |
| 424 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified path '%s' is not absolute", path); |
| 425 |
args = TAKE_PTR(args_wire); |
| 426 |
if (strv_isempty(args)) { |
| 427 |
args = strv_free(args); |
| 428 |
|
| 429 |
args = strv_new(path); |
| 430 |
if (!args) |
| 431 |
return -ENOMEM; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
r = sd_bus_message_read_strv(message, &env); |
| 436 |
if (r < 0) |
| 437 |
return r; |
| 438 |
|
| 439 |
if (strv_length(env) > ENVIRONMENT_ASSIGNMENTS_MAX) |
| 440 |
return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
| 441 |
"Too many environment assignments in a single query."); |
| 442 |
if (!strv_env_is_valid(env)) |
| 443 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments"); |
| 444 |
|
| 445 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 446 |
_cleanup_free_ char *command_line = strv_join(args, " "); |
| 447 |
if (!command_line) |
| 448 |
return -ENOMEM; |
| 449 |
|
| 450 |
const char *details[] = { |
| 451 |
"machine", m->name, |
| 452 |
"user", user, |
| 453 |
"program", path, |
| 454 |
"command_line", command_line, |
| 455 |
NULL |
| 456 |
}; |
| 457 |
|
| 458 |
r = bus_verify_polkit_async_full( |
| 459 |
message, |
| 460 |
m->class == MACHINE_HOST ? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell", |
| 461 |
details, |
| 462 |
m->uid, |
| 463 |
/* flags= */ 0, |
| 464 |
&m->manager->polkit_registry, |
| 465 |
error); |
| 466 |
if (r < 0) |
| 467 |
return r; |
| 468 |
if (r == 0) |
| 469 |
return 1; /* Will call us back */ |
| 470 |
} |
| 471 |
|
| 472 |
master = machine_openpt(m, O_RDWR|O_NOCTTY|O_CLOEXEC, &pty_name); |
| 473 |
if (master < 0) |
| 474 |
return master; |
| 475 |
|
| 476 |
r = machine_start_shell(m, master, pty_name, user, path, args, env, error); |
| 477 |
if (r < 0) |
| 478 |
return r; |
| 479 |
|
| 480 |
r = sd_bus_message_new_method_return(message, &reply); |
| 481 |
if (r < 0) |
| 482 |
return r; |
| 483 |
|
| 484 |
r = sd_bus_message_append(reply, "hs", master, pty_name); |
| 485 |
if (r < 0) |
| 486 |
return r; |
| 487 |
|
| 488 |
return sd_bus_message_send(reply); |
| 489 |
} |
| 490 |
|
| 491 |
int bus_machine_method_bind_mount(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 492 |
int read_only, make_file_or_directory; |
| 493 |
const char *dest, *src, *propagate_directory; |
| 494 |
Machine *m = ASSERT_PTR(userdata); |
| 495 |
MountInNamespaceFlags flags = 0; |
| 496 |
uid_t uid; |
| 497 |
int r; |
| 498 |
|
| 499 |
assert(message); |
| 500 |
|
| 501 |
if (m->class != MACHINE_CONTAINER) |
| 502 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Bind mounting is only supported on container machines."); |
| 503 |
|
| 504 |
r = sd_bus_message_read(message, "ssbb", &src, &dest, &read_only, &make_file_or_directory); |
| 505 |
if (r < 0) |
| 506 |
return r; |
| 507 |
|
| 508 |
if (!path_is_absolute(src) || !path_is_normalized(src)) |
| 509 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Source path must be absolute and normalized."); |
| 510 |
|
| 511 |
if (isempty(dest)) |
| 512 |
dest = src; |
| 513 |
else if (!path_is_absolute(dest) || !path_is_normalized(dest)) |
| 514 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path must be absolute and normalized."); |
| 515 |
|
| 516 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 517 |
const char *details[] = { |
| 518 |
"machine", m->name, |
| 519 |
"verb", "bind", |
| 520 |
"src", src, |
| 521 |
"dest", dest, |
| 522 |
NULL |
| 523 |
}; |
| 524 |
|
| 525 |
/* NB: For now not opened up to owner of machine without auth */ |
| 526 |
r = bus_verify_polkit_async( |
| 527 |
message, |
| 528 |
"org.freedesktop.machine1.manage-machines", |
| 529 |
details, |
| 530 |
&m->manager->polkit_registry, |
| 531 |
error); |
| 532 |
if (r < 0) |
| 533 |
return r; |
| 534 |
if (r == 0) |
| 535 |
return 1; /* Will call us back */ |
| 536 |
} |
| 537 |
|
| 538 |
r = machine_get_uid_shift(m, &uid); |
| 539 |
if (r < 0) |
| 540 |
return r; |
| 541 |
if (uid != 0) |
| 542 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Can't bind mount on container with user namespacing applied."); |
| 543 |
|
| 544 |
if (read_only) |
| 545 |
flags |= MOUNT_IN_NAMESPACE_READ_ONLY; |
| 546 |
if (make_file_or_directory) |
| 547 |
flags |= MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY; |
| 548 |
|
| 549 |
propagate_directory = strjoina("/run/systemd/nspawn/propagate/", m->name); |
| 550 |
r = bind_mount_in_namespace( |
| 551 |
&m->leader, |
| 552 |
propagate_directory, |
| 553 |
"/run/host/incoming/", |
| 554 |
src, dest, |
| 555 |
flags); |
| 556 |
if (r < 0) |
| 557 |
return sd_bus_error_set_errnof(error, r, "Failed to mount %s on %s in machine's namespace: %m", src, dest); |
| 558 |
|
| 559 |
return sd_bus_reply_method_return(message, NULL); |
| 560 |
} |
| 561 |
|
| 562 |
int bus_machine_method_copy(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 563 |
const char *src, *dest, *host_path, *container_path; |
| 564 |
CopyFlags copy_flags = COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS; |
| 565 |
Machine *m = ASSERT_PTR(userdata); |
| 566 |
Manager *manager = m->manager; |
| 567 |
bool copy_from; |
| 568 |
int r; |
| 569 |
|
| 570 |
assert(message); |
| 571 |
|
| 572 |
if (m->manager->n_operations >= OPERATIONS_MAX) |
| 573 |
return sd_bus_error_set(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing copies."); |
| 574 |
|
| 575 |
if (m->class != MACHINE_CONTAINER) |
| 576 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Copying files is only supported on container machines."); |
| 577 |
|
| 578 |
r = sd_bus_message_read(message, "ss", &src, &dest); |
| 579 |
if (r < 0) |
| 580 |
return r; |
| 581 |
|
| 582 |
if (endswith(sd_bus_message_get_member(message), "WithFlags")) { |
| 583 |
uint64_t raw_flags; |
| 584 |
|
| 585 |
r = sd_bus_message_read(message, "t", &raw_flags); |
| 586 |
if (r < 0) |
| 587 |
return r; |
| 588 |
|
| 589 |
if ((raw_flags & ~_MACHINE_COPY_FLAGS_MASK_PUBLIC) != 0) |
| 590 |
return -EINVAL; |
| 591 |
|
| 592 |
if (raw_flags & MACHINE_COPY_REPLACE) |
| 593 |
copy_flags |= COPY_REPLACE; |
| 594 |
} |
| 595 |
|
| 596 |
if (!path_is_absolute(src) || !path_is_normalized(src)) |
| 597 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Source path must be absolute and normalized."); |
| 598 |
|
| 599 |
if (isempty(dest)) |
| 600 |
dest = src; |
| 601 |
else if (!path_is_absolute(dest) || !path_is_normalized(dest)) |
| 602 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path must be absolute and normalized."); |
| 603 |
|
| 604 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 605 |
const char *details[] = { |
| 606 |
"machine", m->name, |
| 607 |
"verb", "copy", |
| 608 |
"src", src, |
| 609 |
"dest", dest, |
| 610 |
NULL |
| 611 |
}; |
| 612 |
|
| 613 |
/* NB: For now not opened up to owner of machine without auth */ |
| 614 |
r = bus_verify_polkit_async( |
| 615 |
message, |
| 616 |
"org.freedesktop.machine1.manage-machines", |
| 617 |
details, |
| 618 |
&manager->polkit_registry, |
| 619 |
error); |
| 620 |
if (r < 0) |
| 621 |
return r; |
| 622 |
if (r == 0) |
| 623 |
return 1; /* Will call us back */ |
| 624 |
} |
| 625 |
|
| 626 |
copy_from = strstr(sd_bus_message_get_member(message), "CopyFrom"); |
| 627 |
|
| 628 |
if (copy_from) { |
| 629 |
container_path = src; |
| 630 |
host_path = dest; |
| 631 |
} else { |
| 632 |
host_path = src; |
| 633 |
container_path = dest; |
| 634 |
} |
| 635 |
|
| 636 |
Operation *op; |
| 637 |
r = machine_copy_from_to_operation(manager, m, host_path, container_path, copy_from, copy_flags, &op); |
| 638 |
if (r < 0) |
| 639 |
return sd_bus_error_set_errnof(error, r, "Failed to copy from/to machine '%s': %m", m->name); |
| 640 |
|
| 641 |
operation_attach_bus_reply(op, message); |
| 642 |
return 1; |
| 643 |
} |
| 644 |
|
| 645 |
int bus_machine_method_open_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 646 |
_cleanup_close_ int fd = -EBADF; |
| 647 |
Machine *m = ASSERT_PTR(userdata); |
| 648 |
int r; |
| 649 |
|
| 650 |
assert(message); |
| 651 |
|
| 652 |
if (m->manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 653 |
const char *details[] = { |
| 654 |
"machine", m->name, |
| 655 |
"verb", "open_root_directory", |
| 656 |
NULL |
| 657 |
}; |
| 658 |
|
| 659 |
/* NB: For now not opened up to owner of machine without auth */ |
| 660 |
r = bus_verify_polkit_async( |
| 661 |
message, |
| 662 |
"org.freedesktop.machine1.manage-machines", |
| 663 |
details, |
| 664 |
&m->manager->polkit_registry, |
| 665 |
error); |
| 666 |
if (r < 0) |
| 667 |
return r; |
| 668 |
if (r == 0) |
| 669 |
return 1; /* Will call us back */ |
| 670 |
} |
| 671 |
|
| 672 |
fd = machine_open_root_directory(m); |
| 673 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(fd)) |
| 674 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Opening the root directory is only supported on container machines."); |
| 675 |
if (fd < 0) |
| 676 |
return sd_bus_error_set_errnof(error, fd, "Failed to open root directory of machine '%s': %m", m->name); |
| 677 |
|
| 678 |
return sd_bus_reply_method_return(message, "h", fd); |
| 679 |
} |
| 680 |
|
| 681 |
int bus_machine_method_get_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 682 |
Machine *m = ASSERT_PTR(userdata); |
| 683 |
uid_t shift = 0; |
| 684 |
int r; |
| 685 |
|
| 686 |
assert(message); |
| 687 |
|
| 688 |
/* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but |
| 689 |
* we kinda have to for this. */ |
| 690 |
|
| 691 |
if (m->class == MACHINE_HOST) |
| 692 |
return sd_bus_reply_method_return(message, "u", UINT32_C(0)); |
| 693 |
|
| 694 |
if (m->class != MACHINE_CONTAINER) |
| 695 |
return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "UID/GID shift may only be determined for container machines."); |
| 696 |
|
| 697 |
r = machine_get_uid_shift(m, &shift); |
| 698 |
if (r == -ENXIO) |
| 699 |
return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m->name); |
| 700 |
if (r < 0) |
| 701 |
return r; |
| 702 |
|
| 703 |
return sd_bus_reply_method_return(message, "u", (uint32_t) shift); |
| 704 |
} |
| 705 |
|
| 706 |
static int machine_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
| 707 |
Manager *m = ASSERT_PTR(userdata); |
| 708 |
Machine *machine; |
| 709 |
int r; |
| 710 |
|
| 711 |
assert(bus); |
| 712 |
assert(path); |
| 713 |
assert(interface); |
| 714 |
assert(found); |
| 715 |
|
| 716 |
if (streq(path, "/org/freedesktop/machine1/machine/self")) { |
| 717 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 718 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 719 |
sd_bus_message *message; |
| 720 |
|
| 721 |
message = sd_bus_get_current_message(bus); |
| 722 |
if (!message) |
| 723 |
return 0; |
| 724 |
|
| 725 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID|SD_BUS_CREDS_PIDFD, &creds); |
| 726 |
if (r < 0) |
| 727 |
return r; |
| 728 |
|
| 729 |
r = bus_creds_get_pidref(creds, &pidref); |
| 730 |
if (r < 0) |
| 731 |
return r; |
| 732 |
|
| 733 |
r = manager_get_machine_by_pidref(m, &pidref, &machine); |
| 734 |
if (r <= 0) |
| 735 |
return 0; |
| 736 |
} else { |
| 737 |
_cleanup_free_ char *e = NULL; |
| 738 |
const char *p; |
| 739 |
|
| 740 |
p = startswith(path, "/org/freedesktop/machine1/machine/"); |
| 741 |
if (!p) |
| 742 |
return 0; |
| 743 |
|
| 744 |
e = bus_label_unescape(p); |
| 745 |
if (!e) |
| 746 |
return -ENOMEM; |
| 747 |
|
| 748 |
machine = hashmap_get(m->machines, e); |
| 749 |
if (!machine) |
| 750 |
return 0; |
| 751 |
} |
| 752 |
|
| 753 |
*found = machine; |
| 754 |
return 1; |
| 755 |
} |
| 756 |
|
| 757 |
char* machine_bus_path(Machine *m) { |
| 758 |
_cleanup_free_ char *e = NULL; |
| 759 |
|
| 760 |
assert(m); |
| 761 |
|
| 762 |
e = bus_label_escape(m->name); |
| 763 |
if (!e) |
| 764 |
return NULL; |
| 765 |
|
| 766 |
return strjoin("/org/freedesktop/machine1/machine/", e); |
| 767 |
} |
| 768 |
|
| 769 |
static int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
| 770 |
_cleanup_strv_free_ char **l = NULL; |
| 771 |
Machine *machine = NULL; |
| 772 |
Manager *m = userdata; |
| 773 |
int r; |
| 774 |
|
| 775 |
assert(bus); |
| 776 |
assert(path); |
| 777 |
assert(nodes); |
| 778 |
|
| 779 |
HASHMAP_FOREACH(machine, m->machines) { |
| 780 |
char *p; |
| 781 |
|
| 782 |
p = machine_bus_path(machine); |
| 783 |
if (!p) |
| 784 |
return -ENOMEM; |
| 785 |
|
| 786 |
r = strv_consume(&l, p); |
| 787 |
if (r < 0) |
| 788 |
return r; |
| 789 |
} |
| 790 |
|
| 791 |
*nodes = TAKE_PTR(l); |
| 792 |
|
| 793 |
return 1; |
| 794 |
} |
| 795 |
|
| 796 |
static const sd_bus_vtable machine_vtable[] = { |
| 797 |
SD_BUS_VTABLE_START(0), |
| 798 |
SD_BUS_PROPERTY("Name", "s", NULL, offsetof(Machine, name), SD_BUS_VTABLE_PROPERTY_CONST), |
| 799 |
SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128, offsetof(Machine, id), SD_BUS_VTABLE_PROPERTY_CONST), |
| 800 |
BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine, timestamp), SD_BUS_VTABLE_PROPERTY_CONST), |
| 801 |
SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Machine, service), SD_BUS_VTABLE_PROPERTY_CONST), |
| 802 |
SD_BUS_PROPERTY("Unit", "s", NULL, offsetof(Machine, unit), SD_BUS_VTABLE_PROPERTY_CONST), |
| 803 |
SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Machine, unit), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
| 804 |
SD_BUS_PROPERTY("Subgroup", "s", NULL, offsetof(Machine, subgroup), SD_BUS_VTABLE_PROPERTY_CONST), |
| 805 |
SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Machine, leader.pid), SD_BUS_VTABLE_PROPERTY_CONST), |
| 806 |
SD_BUS_PROPERTY("LeaderPIDFDId", "t", bus_property_get_pidfdid, offsetof(Machine, leader), SD_BUS_VTABLE_PROPERTY_CONST), |
| 807 |
SD_BUS_PROPERTY("Supervisor", "u", bus_property_get_pid, offsetof(Machine, supervisor.pid), SD_BUS_VTABLE_PROPERTY_CONST), |
| 808 |
SD_BUS_PROPERTY("SupervisorPIDFDId", "t", bus_property_get_pidfdid, offsetof(Machine, supervisor), SD_BUS_VTABLE_PROPERTY_CONST), |
| 809 |
SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Machine, class), SD_BUS_VTABLE_PROPERTY_CONST), |
| 810 |
SD_BUS_PROPERTY("RootDirectory", "s", NULL, offsetof(Machine, root_directory), SD_BUS_VTABLE_PROPERTY_CONST), |
| 811 |
SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 812 |
SD_BUS_PROPERTY("VSockCID", "u", NULL, offsetof(Machine, vsock_cid), SD_BUS_VTABLE_PROPERTY_CONST), |
| 813 |
SD_BUS_PROPERTY("SSHAddress", "s", NULL, offsetof(Machine, ssh_address), SD_BUS_VTABLE_PROPERTY_CONST), |
| 814 |
SD_BUS_PROPERTY("SSHPrivateKeyPath", "s", NULL, offsetof(Machine, ssh_private_key_path), SD_BUS_VTABLE_PROPERTY_CONST), |
| 815 |
SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0), |
| 816 |
SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(Machine, uid), SD_BUS_VTABLE_PROPERTY_CONST), |
| 817 |
|
| 818 |
SD_BUS_METHOD("Terminate", |
| 819 |
NULL, |
| 820 |
NULL, |
| 821 |
bus_machine_method_terminate, |
| 822 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 823 |
SD_BUS_METHOD_WITH_ARGS("Kill", |
| 824 |
SD_BUS_ARGS("s", whom, "i", signal), |
| 825 |
SD_BUS_NO_RESULT, |
| 826 |
bus_machine_method_kill, |
| 827 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 828 |
SD_BUS_METHOD_WITH_ARGS("GetAddresses", |
| 829 |
SD_BUS_NO_ARGS, |
| 830 |
SD_BUS_RESULT("a(iay)", addresses), |
| 831 |
bus_machine_method_get_addresses, |
| 832 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 833 |
SD_BUS_METHOD_WITH_ARGS("GetSSHInfo", |
| 834 |
SD_BUS_NO_ARGS, |
| 835 |
SD_BUS_RESULT("s", ssh_address, "s", ssh_private_key_path), |
| 836 |
bus_machine_method_get_ssh_info, |
| 837 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 838 |
SD_BUS_METHOD_WITH_ARGS("GetOSRelease", |
| 839 |
SD_BUS_NO_ARGS, |
| 840 |
SD_BUS_RESULT("a{ss}", fields), |
| 841 |
bus_machine_method_get_os_release, |
| 842 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 843 |
SD_BUS_METHOD_WITH_ARGS("GetUIDShift", |
| 844 |
SD_BUS_NO_ARGS, |
| 845 |
SD_BUS_RESULT("u", shift), |
| 846 |
bus_machine_method_get_uid_shift, |
| 847 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 848 |
SD_BUS_METHOD_WITH_ARGS("OpenPTY", |
| 849 |
SD_BUS_NO_ARGS, |
| 850 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 851 |
bus_machine_method_open_pty, |
| 852 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 853 |
SD_BUS_METHOD_WITH_ARGS("OpenLogin", |
| 854 |
SD_BUS_NO_ARGS, |
| 855 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 856 |
bus_machine_method_open_login, |
| 857 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 858 |
SD_BUS_METHOD_WITH_ARGS("OpenShell", |
| 859 |
SD_BUS_ARGS("s", user, "s", path, "as", args, "as", environment), |
| 860 |
SD_BUS_RESULT("h", pty, "s", pty_path), |
| 861 |
bus_machine_method_open_shell, |
| 862 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 863 |
SD_BUS_METHOD_WITH_ARGS("BindMount", |
| 864 |
SD_BUS_ARGS("s", source, "s", destination, "b", read_only, "b", mkdir), |
| 865 |
SD_BUS_NO_RESULT, |
| 866 |
bus_machine_method_bind_mount, |
| 867 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 868 |
SD_BUS_METHOD_WITH_ARGS("CopyFrom", |
| 869 |
SD_BUS_ARGS("s", source, "s", destination), |
| 870 |
SD_BUS_NO_RESULT, |
| 871 |
bus_machine_method_copy, |
| 872 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 873 |
SD_BUS_METHOD_WITH_ARGS("CopyTo", |
| 874 |
SD_BUS_ARGS("s", source, "s", destination), |
| 875 |
SD_BUS_NO_RESULT, |
| 876 |
bus_machine_method_copy, |
| 877 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 878 |
SD_BUS_METHOD_WITH_ARGS("CopyFromWithFlags", |
| 879 |
SD_BUS_ARGS("s", source, "s", destination, "t", flags), |
| 880 |
SD_BUS_NO_RESULT, |
| 881 |
bus_machine_method_copy, |
| 882 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 883 |
SD_BUS_METHOD_WITH_ARGS("CopyToWithFlags", |
| 884 |
SD_BUS_ARGS("s", source, "s", destination, "t", flags), |
| 885 |
SD_BUS_NO_RESULT, |
| 886 |
bus_machine_method_copy, |
| 887 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 888 |
SD_BUS_METHOD_WITH_ARGS("OpenRootDirectory", |
| 889 |
SD_BUS_NO_ARGS, |
| 890 |
SD_BUS_RESULT("h", fd), |
| 891 |
bus_machine_method_open_root_directory, |
| 892 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 893 |
|
| 894 |
SD_BUS_VTABLE_END |
| 895 |
}; |
| 896 |
|
| 897 |
const BusObjectImplementation machine_object = { |
| 898 |
"/org/freedesktop/machine1/machine", |
| 899 |
"org.freedesktop.machine1.Machine", |
| 900 |
.fallback_vtables = BUS_FALLBACK_VTABLES({machine_vtable, machine_object_find}), |
| 901 |
.node_enumerator = machine_node_enumerator, |
| 902 |
}; |
| 903 |
|
| 904 |
int machine_send_signal(Machine *m, const char *signal_name) { |
| 905 |
assert(m); |
| 906 |
assert(signal_name); |
| 907 |
|
| 908 |
_cleanup_free_ char *p = machine_bus_path(m); |
| 909 |
if (!p) |
| 910 |
return -ENOMEM; |
| 911 |
|
| 912 |
return sd_bus_emit_signal( |
| 913 |
m->manager->api_bus, |
| 914 |
"/org/freedesktop/machine1", |
| 915 |
"org.freedesktop.machine1.Manager", |
| 916 |
signal_name, |
| 917 |
"so", m->name, p); |
| 918 |
} |
| 919 |
|
| 920 |
int machine_send_create_reply(Machine *m, sd_bus_error *error) { |
| 921 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL; |
| 922 |
_cleanup_free_ char *p = NULL; |
| 923 |
|
| 924 |
assert(m); |
| 925 |
|
| 926 |
if (!m->create_message) |
| 927 |
return 0; |
| 928 |
|
| 929 |
c = TAKE_PTR(m->create_message); |
| 930 |
|
| 931 |
if (error) |
| 932 |
return sd_bus_reply_method_error(c, error); |
| 933 |
|
| 934 |
/* Update the machine state file before we notify the client |
| 935 |
* about the result. */ |
| 936 |
machine_save(m); |
| 937 |
|
| 938 |
p = machine_bus_path(m); |
| 939 |
if (!p) |
| 940 |
return -ENOMEM; |
| 941 |
|
| 942 |
return sd_bus_reply_method_return(c, "o", p); |
| 943 |
} |