| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
#include "sd-varlink.h" |
| 5 |
|
| 6 |
#include "bus-polkit.h" |
| 7 |
#include "copy.h" |
| 8 |
#include "errno-util.h" |
| 9 |
#include "fd-util.h" |
| 10 |
#include "format-util.h" |
| 11 |
#include "hashmap.h" |
| 12 |
#include "hostname-util.h" |
| 13 |
#include "json-util.h" |
| 14 |
#include "machine.h" |
| 15 |
#include "machine-varlink.h" |
| 16 |
#include "machined.h" |
| 17 |
#include "mount-util.h" |
| 18 |
#include "namespace-util.h" |
| 19 |
#include "operation.h" |
| 20 |
#include "pidref.h" |
| 21 |
#include "socket-util.h" |
| 22 |
#include "string-table.h" |
| 23 |
#include "string-util.h" |
| 24 |
#include "user-util.h" |
| 25 |
#include "varlink-util.h" |
| 26 |
|
| 27 |
static JSON_DISPATCH_ENUM_DEFINE(dispatch_machine_class, MachineClass, machine_class_from_string); |
| 28 |
|
| 29 |
static int machine_name(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 30 |
char **m = ASSERT_PTR(userdata); |
| 31 |
const char *hostname; |
| 32 |
int r; |
| 33 |
|
| 34 |
assert(variant); |
| 35 |
|
| 36 |
if (!sd_json_variant_is_string(variant)) |
| 37 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a string.", strna(name)); |
| 38 |
|
| 39 |
hostname = sd_json_variant_string(variant); |
| 40 |
if (!hostname_is_valid(hostname, /* flags= */ 0)) |
| 41 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid machine name"); |
| 42 |
|
| 43 |
r = free_and_strdup(m, hostname); |
| 44 |
if (r < 0) |
| 45 |
return json_log_oom(variant, flags); |
| 46 |
|
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static int machine_pidref(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 51 |
PidRef *pidref = ASSERT_PTR(userdata); |
| 52 |
_cleanup_(pidref_done) PidRef temp = PIDREF_NULL; |
| 53 |
int r; |
| 54 |
|
| 55 |
r = json_dispatch_pidref(name, variant, flags, &temp); |
| 56 |
if (r < 0) |
| 57 |
return r; |
| 58 |
|
| 59 |
if (temp.pid == 1) /* refuse PID 1 */ |
| 60 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a valid PID.", strna(name)); |
| 61 |
|
| 62 |
/* When both leader and leaderProcessId are specified, they must be consistent with each other. */ |
| 63 |
if (pidref_is_set(pidref) && !pidref_equal(pidref, &temp)) |
| 64 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' conflicts with already dispatched PID.", strna(name)); |
| 65 |
|
| 66 |
pidref_done(pidref); |
| 67 |
*pidref = TAKE_PIDREF(temp); |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static int machine_ifindices(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 73 |
Machine *m = ASSERT_PTR(userdata); |
| 74 |
_cleanup_free_ int *netif = NULL; |
| 75 |
size_t n_netif, k = 0; |
| 76 |
|
| 77 |
assert(variant); |
| 78 |
|
| 79 |
if (!sd_json_variant_is_array(variant)) |
| 80 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not an array.", strna(name)); |
| 81 |
|
| 82 |
n_netif = sd_json_variant_elements(variant); |
| 83 |
|
| 84 |
netif = new(int, n_netif); |
| 85 |
if (!netif) |
| 86 |
return json_log_oom(variant, flags); |
| 87 |
|
| 88 |
sd_json_variant *i; |
| 89 |
JSON_VARIANT_ARRAY_FOREACH(i, variant) { |
| 90 |
uint64_t b; |
| 91 |
|
| 92 |
if (!sd_json_variant_is_unsigned(i)) |
| 93 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Element %zu of JSON field '%s' is not an unsigned integer.", k, strna(name)); |
| 94 |
|
| 95 |
b = sd_json_variant_unsigned(i); |
| 96 |
if (b > INT_MAX || b <= 0) |
| 97 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Invalid network interface index %"PRIu64, b); |
| 98 |
|
| 99 |
netif[k++] = (int) b; |
| 100 |
} |
| 101 |
assert(k == n_netif); |
| 102 |
|
| 103 |
free_and_replace(m->netif, netif); |
| 104 |
m->n_netif = n_netif; |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
static int machine_cid(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) { |
| 110 |
unsigned cid, *c = ASSERT_PTR(userdata); |
| 111 |
int r; |
| 112 |
|
| 113 |
assert(variant); |
| 114 |
|
| 115 |
r = sd_json_dispatch_uint(name, variant, flags, &cid); |
| 116 |
if (r < 0) |
| 117 |
return r; |
| 118 |
|
| 119 |
if (!VSOCK_CID_IS_REGULAR(cid)) |
| 120 |
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "JSON field '%s' is not a regular VSOCK CID.", strna(name)); |
| 121 |
|
| 122 |
*c = cid; |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
int vl_method_register(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 127 |
Manager *manager = ASSERT_PTR(userdata); |
| 128 |
_cleanup_(machine_freep) Machine *machine = NULL; |
| 129 |
int r; |
| 130 |
|
| 131 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 132 |
{ "name", SD_JSON_VARIANT_STRING, machine_name, offsetof(Machine, name), SD_JSON_MANDATORY }, |
| 133 |
{ "id", SD_JSON_VARIANT_STRING, sd_json_dispatch_id128, offsetof(Machine, id), 0 }, |
| 134 |
{ "service", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, service), 0 }, |
| 135 |
{ "class", SD_JSON_VARIANT_STRING, dispatch_machine_class, offsetof(Machine, class), SD_JSON_MANDATORY }, |
| 136 |
{ "leader", _SD_JSON_VARIANT_TYPE_INVALID, machine_pidref, offsetof(Machine, leader), SD_JSON_STRICT }, |
| 137 |
{ "leaderProcessId", SD_JSON_VARIANT_OBJECT, machine_pidref, offsetof(Machine, leader), SD_JSON_STRICT }, |
| 138 |
{ "supervisor", _SD_JSON_VARIANT_TYPE_INVALID, machine_pidref, offsetof(Machine, supervisor), SD_JSON_STRICT }, |
| 139 |
{ "supervisorProcessId", SD_JSON_VARIANT_OBJECT, machine_pidref, offsetof(Machine, supervisor), SD_JSON_STRICT }, |
| 140 |
{ "rootDirectory", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, root_directory), SD_JSON_STRICT }, |
| 141 |
{ "ifIndices", SD_JSON_VARIANT_ARRAY, machine_ifindices, 0, 0 }, |
| 142 |
{ "vSockCid", _SD_JSON_VARIANT_TYPE_INVALID, machine_cid, offsetof(Machine, vsock_cid), 0 }, |
| 143 |
{ "sshAddress", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Machine, ssh_address), SD_JSON_STRICT }, |
| 144 |
{ "sshPrivateKeyPath", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, ssh_private_key_path), 0 }, |
| 145 |
{ "controlAddress", SD_JSON_VARIANT_STRING, json_dispatch_path, offsetof(Machine, control_address), SD_JSON_STRICT }, |
| 146 |
{ "allocateUnit", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Machine, allocate_unit), 0 }, |
| 147 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 148 |
{} |
| 149 |
}; |
| 150 |
|
| 151 |
r = machine_new(_MACHINE_CLASS_INVALID, NULL, &machine); |
| 152 |
if (r < 0) |
| 153 |
return r; |
| 154 |
|
| 155 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, machine); |
| 156 |
if (r != 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
if (!MACHINE_CLASS_CAN_REGISTER(machine->class)) |
| 160 |
return sd_varlink_error_invalid_parameter_name(link, "class"); |
| 161 |
|
| 162 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 163 |
r = varlink_verify_polkit_async( |
| 164 |
link, |
| 165 |
manager->system_bus, |
| 166 |
machine->allocate_unit ? "org.freedesktop.machine1.create-machine" : "org.freedesktop.machine1.register-machine", |
| 167 |
(const char**) STRV_MAKE("name", machine->name, |
| 168 |
"class", machine_class_to_string(machine->class)), |
| 169 |
&manager->polkit_registry); |
| 170 |
if (r <= 0) |
| 171 |
return r; |
| 172 |
} |
| 173 |
|
| 174 |
if (!pidref_is_set(&machine->leader)) { |
| 175 |
r = varlink_get_peer_pidref(link, &machine->leader); |
| 176 |
if (r < 0) |
| 177 |
return r; |
| 178 |
} |
| 179 |
|
| 180 |
if (!pidref_is_set(&machine->supervisor)) { |
| 181 |
_cleanup_(pidref_done) PidRef client_pidref = PIDREF_NULL; |
| 182 |
|
| 183 |
r = varlink_get_peer_pidref(link, &client_pidref); |
| 184 |
if (r < 0) |
| 185 |
return r; |
| 186 |
|
| 187 |
/* If the client process is not the leader, then make it the supervisor */ |
| 188 |
if (!pidref_equal(&client_pidref, &machine->leader)) |
| 189 |
machine->supervisor = TAKE_PIDREF(client_pidref); |
| 190 |
} |
| 191 |
|
| 192 |
r = sd_varlink_get_peer_uid(link, &machine->uid); |
| 193 |
if (r < 0) |
| 194 |
return r; |
| 195 |
|
| 196 |
/* In system scope, ensure an unprivileged user cannot claim any process they don't |
| 197 |
* control as their own machine. In user scope the varlink socket is already |
| 198 |
* protected by $XDG_RUNTIME_DIR permissions. */ |
| 199 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER && machine->uid != 0) { |
| 200 |
r = process_is_owned_by_uid(&machine->leader, machine->uid); |
| 201 |
if (r < 0) |
| 202 |
return r; |
| 203 |
if (r == 0) |
| 204 |
return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL); |
| 205 |
} |
| 206 |
|
| 207 |
r = machine_link(manager, machine); |
| 208 |
if (r == -EEXIST) |
| 209 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_EXISTS, NULL); |
| 210 |
if (r < 0) |
| 211 |
return r; |
| 212 |
|
| 213 |
if (!machine->allocate_unit) { |
| 214 |
r = cg_pidref_get_unit_full(&machine->leader, &machine->unit, &machine->subgroup); |
| 215 |
if (r < 0) |
| 216 |
return r; |
| 217 |
} |
| 218 |
|
| 219 |
r = machine_start(machine, /* properties= */ NULL, /* error= */ NULL); |
| 220 |
if (r < 0) |
| 221 |
return r; |
| 222 |
|
| 223 |
/* the manager will free this machine */ |
| 224 |
TAKE_PTR(machine); |
| 225 |
|
| 226 |
return sd_varlink_reply(link, NULL); |
| 227 |
} |
| 228 |
|
| 229 |
static int lookup_machine_by_name(sd_varlink *link, Manager *manager, const char *machine_name, Machine **ret_machine) { |
| 230 |
assert(link); |
| 231 |
assert(manager); |
| 232 |
assert(ret_machine); |
| 233 |
|
| 234 |
if (!machine_name) |
| 235 |
return -EINVAL; |
| 236 |
|
| 237 |
if (!hostname_is_valid(machine_name, /* flags= */ VALID_HOSTNAME_DOT_HOST)) |
| 238 |
return -EINVAL; |
| 239 |
|
| 240 |
Machine *machine = hashmap_get(manager->machines, machine_name); |
| 241 |
if (!machine) |
| 242 |
return -ESRCH; |
| 243 |
|
| 244 |
*ret_machine = machine; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
static int lookup_machine_by_pidref(sd_varlink *link, Manager *manager, const PidRef *pidref, Machine **ret_machine) { |
| 249 |
_cleanup_(pidref_done) PidRef peer = PIDREF_NULL; |
| 250 |
Machine *machine; |
| 251 |
int r; |
| 252 |
|
| 253 |
assert(link); |
| 254 |
assert(manager); |
| 255 |
assert(ret_machine); |
| 256 |
|
| 257 |
if (pidref_is_automatic(pidref)) { |
| 258 |
r = varlink_get_peer_pidref(link, &peer); |
| 259 |
if (r < 0) |
| 260 |
return log_debug_errno(r, "Failed to get peer pidref: %m"); |
| 261 |
|
| 262 |
pidref = &peer; |
| 263 |
} else if (!pidref_is_set(pidref)) |
| 264 |
return -EINVAL; |
| 265 |
|
| 266 |
r = manager_get_machine_by_pidref(manager, pidref, &machine); |
| 267 |
if (r < 0) |
| 268 |
return r; |
| 269 |
if (!machine) |
| 270 |
return -ESRCH; |
| 271 |
|
| 272 |
*ret_machine = machine; |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
int lookup_machine_by_name_or_pidref(sd_varlink *link, Manager *manager, const char *machine_name, const PidRef *pidref, Machine **ret_machine) { |
| 277 |
Machine *machine = NULL, *pid_machine = NULL; |
| 278 |
int r; |
| 279 |
|
| 280 |
assert(link); |
| 281 |
assert(manager); |
| 282 |
assert(ret_machine); |
| 283 |
|
| 284 |
if (machine_name) { |
| 285 |
r = lookup_machine_by_name(link, manager, machine_name, &machine); |
| 286 |
if (r == -EINVAL) |
| 287 |
return sd_varlink_error_invalid_parameter_name(link, "name"); |
| 288 |
if (r < 0) |
| 289 |
return r; |
| 290 |
} |
| 291 |
|
| 292 |
if (pidref_is_set(pidref) || pidref_is_automatic(pidref)) { |
| 293 |
r = lookup_machine_by_pidref(link, manager, pidref, &pid_machine); |
| 294 |
if (r == -EINVAL) |
| 295 |
return sd_varlink_error_invalid_parameter_name(link, "pid"); |
| 296 |
if (r < 0) |
| 297 |
return r; |
| 298 |
} |
| 299 |
|
| 300 |
if (machine && pid_machine && machine != pid_machine) |
| 301 |
return log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Search by machine name '%s' and pid " PID_FMT " resulted in two different machines", machine_name, pidref->pid); |
| 302 |
if (machine) |
| 303 |
*ret_machine = machine; |
| 304 |
else if (pid_machine) |
| 305 |
*ret_machine = pid_machine; |
| 306 |
else |
| 307 |
return -ESRCH; |
| 308 |
|
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
int vl_method_unregister_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 313 |
Machine *machine = ASSERT_PTR(userdata); |
| 314 |
Manager *manager = ASSERT_PTR(machine->manager); |
| 315 |
int r; |
| 316 |
|
| 317 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 318 |
r = varlink_verify_polkit_async_full( |
| 319 |
link, |
| 320 |
manager->system_bus, |
| 321 |
"org.freedesktop.machine1.manage-machines", |
| 322 |
(const char**) STRV_MAKE("name", machine->name, |
| 323 |
"verb", "unregister"), |
| 324 |
machine->uid, |
| 325 |
/* flags= */ 0, |
| 326 |
&manager->polkit_registry); |
| 327 |
if (r <= 0) |
| 328 |
return r; |
| 329 |
} |
| 330 |
|
| 331 |
r = machine_finalize(machine); |
| 332 |
if (r < 0) |
| 333 |
return log_debug_errno(r, "Failed to finalize machine: %m"); |
| 334 |
|
| 335 |
return sd_varlink_reply(link, NULL); |
| 336 |
} |
| 337 |
|
| 338 |
int vl_method_terminate_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 339 |
Machine *machine = ASSERT_PTR(userdata); |
| 340 |
Manager *manager = ASSERT_PTR(machine->manager); |
| 341 |
int r; |
| 342 |
|
| 343 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 344 |
r = varlink_verify_polkit_async_full( |
| 345 |
link, |
| 346 |
manager->system_bus, |
| 347 |
"org.freedesktop.machine1.manage-machines", |
| 348 |
(const char**) STRV_MAKE("name", machine->name, |
| 349 |
"verb", "terminate"), |
| 350 |
machine->uid, |
| 351 |
/* flags= */ 0, |
| 352 |
&manager->polkit_registry); |
| 353 |
if (r <= 0) |
| 354 |
return r; |
| 355 |
} |
| 356 |
|
| 357 |
r = machine_stop(machine); |
| 358 |
if (r < 0) |
| 359 |
return log_debug_errno(r, "Failed to stop machine: %m"); |
| 360 |
|
| 361 |
return sd_varlink_reply(link, NULL); |
| 362 |
} |
| 363 |
|
| 364 |
static JSON_DISPATCH_ENUM_DEFINE(dispatch_kill_whom, KillWhom, kill_whom_from_string); |
| 365 |
|
| 366 |
typedef struct MachineKillParameters { |
| 367 |
const char *name; |
| 368 |
PidRef pidref; |
| 369 |
KillWhom whom; |
| 370 |
int32_t signo; |
| 371 |
} MachineKillParameters; |
| 372 |
|
| 373 |
static void machine_kill_paramaters_done(MachineKillParameters *p) { |
| 374 |
assert(p); |
| 375 |
|
| 376 |
pidref_done(&p->pidref); |
| 377 |
} |
| 378 |
|
| 379 |
int vl_method_kill(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 380 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 381 |
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineKillParameters), |
| 382 |
{ "whom", SD_JSON_VARIANT_STRING, dispatch_kill_whom, offsetof(MachineKillParameters, whom), 0 }, |
| 383 |
{ "signal", _SD_JSON_VARIANT_TYPE_INVALID , sd_json_dispatch_signal, offsetof(MachineKillParameters, signo), SD_JSON_MANDATORY }, |
| 384 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 385 |
{} |
| 386 |
}; |
| 387 |
|
| 388 |
Manager *manager = ASSERT_PTR(userdata); |
| 389 |
_cleanup_(machine_kill_paramaters_done) MachineKillParameters p = { |
| 390 |
.pidref = PIDREF_NULL, |
| 391 |
.whom = _KILL_WHOM_INVALID, |
| 392 |
}; |
| 393 |
int r; |
| 394 |
|
| 395 |
assert(link); |
| 396 |
assert(parameters); |
| 397 |
|
| 398 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 399 |
if (r != 0) |
| 400 |
return r; |
| 401 |
|
| 402 |
Machine *machine; |
| 403 |
r = lookup_machine_by_name_or_pidref(link, manager, p.name, &p.pidref, &machine); |
| 404 |
if (r == -ESRCH) |
| 405 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 406 |
if (r < 0) |
| 407 |
return r; |
| 408 |
|
| 409 |
KillWhom whom = p.whom >= 0 ? p.whom : KILL_ALL; |
| 410 |
|
| 411 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 412 |
r = varlink_verify_polkit_async_full( |
| 413 |
link, |
| 414 |
manager->system_bus, |
| 415 |
"org.freedesktop.machine1.manage-machines", |
| 416 |
(const char**) STRV_MAKE("name", machine->name, |
| 417 |
"verb", "kill"), |
| 418 |
machine->uid, |
| 419 |
/* flags= */ 0, |
| 420 |
&manager->polkit_registry); |
| 421 |
if (r <= 0) |
| 422 |
return r; |
| 423 |
} |
| 424 |
|
| 425 |
r = machine_kill(machine, whom, p.signo); |
| 426 |
if (r < 0) |
| 427 |
return log_debug_errno(r, "Failed to send signal to machine: %m"); |
| 428 |
|
| 429 |
return sd_varlink_reply(link, NULL); |
| 430 |
} |
| 431 |
|
| 432 |
typedef enum MachineOpenMode { |
| 433 |
MACHINE_OPEN_MODE_TTY, |
| 434 |
MACHINE_OPEN_MODE_LOGIN, |
| 435 |
MACHINE_OPEN_MODE_SHELL, |
| 436 |
_MACHINE_OPEN_MODE_MAX, |
| 437 |
_MACHINE_OPEN_MODE_INVALID = -EINVAL, |
| 438 |
} MachineOpenMode; |
| 439 |
|
| 440 |
static const char* const machine_open_mode_table[_MACHINE_OPEN_MODE_MAX] = { |
| 441 |
[MACHINE_OPEN_MODE_TTY] = "tty", |
| 442 |
[MACHINE_OPEN_MODE_LOGIN] = "login", |
| 443 |
[MACHINE_OPEN_MODE_SHELL] = "shell", |
| 444 |
}; |
| 445 |
|
| 446 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(machine_open_mode, MachineOpenMode); |
| 447 |
static JSON_DISPATCH_ENUM_DEFINE(json_dispatch_machine_open_mode, MachineOpenMode, machine_open_mode_from_string); |
| 448 |
|
| 449 |
typedef struct MachineOpenParameters { |
| 450 |
const char *name; |
| 451 |
const char *user; |
| 452 |
PidRef pidref; |
| 453 |
MachineOpenMode mode; |
| 454 |
const char *path; |
| 455 |
char **args; |
| 456 |
char **env; |
| 457 |
} MachineOpenParameters; |
| 458 |
|
| 459 |
static void machine_open_paramaters_done(MachineOpenParameters *p) { |
| 460 |
assert(p); |
| 461 |
pidref_done(&p->pidref); |
| 462 |
strv_free(p->args); |
| 463 |
strv_free(p->env); |
| 464 |
} |
| 465 |
|
| 466 |
inline static const char* machine_open_polkit_action(MachineOpenMode mode, MachineClass class) { |
| 467 |
switch (mode) { |
| 468 |
case MACHINE_OPEN_MODE_TTY: |
| 469 |
return class == MACHINE_HOST ? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty"; |
| 470 |
case MACHINE_OPEN_MODE_LOGIN: |
| 471 |
return class == MACHINE_HOST ? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login"; |
| 472 |
case MACHINE_OPEN_MODE_SHELL: |
| 473 |
return class == MACHINE_HOST ? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell"; |
| 474 |
default: |
| 475 |
assert_not_reached(); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
inline static char** machine_open_polkit_details(MachineOpenMode mode, const char *machine_name, const char *user, const char *path, const char *command_line) { |
| 480 |
assert(machine_name); |
| 481 |
|
| 482 |
switch (mode) { |
| 483 |
case MACHINE_OPEN_MODE_TTY: |
| 484 |
return strv_new("machine", machine_name); |
| 485 |
case MACHINE_OPEN_MODE_LOGIN: |
| 486 |
return strv_new("machine", machine_name, "verb", "login"); |
| 487 |
case MACHINE_OPEN_MODE_SHELL: |
| 488 |
assert(user); |
| 489 |
assert(path); |
| 490 |
assert(command_line); |
| 491 |
return strv_new( |
| 492 |
"machine", machine_name, |
| 493 |
"verb", "shell", |
| 494 |
"user", user, |
| 495 |
"program", path, |
| 496 |
"command_line", command_line); |
| 497 |
default: |
| 498 |
assert_not_reached(); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
int vl_method_open(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 503 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 504 |
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineOpenParameters), |
| 505 |
{ "mode", SD_JSON_VARIANT_STRING, json_dispatch_machine_open_mode, offsetof(MachineOpenParameters, mode), SD_JSON_MANDATORY }, |
| 506 |
{ "user", SD_JSON_VARIANT_STRING, json_dispatch_const_user_group_name, offsetof(MachineOpenParameters, user), SD_JSON_RELAX }, |
| 507 |
{ "path", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(MachineOpenParameters, path), 0 }, |
| 508 |
{ "args", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(MachineOpenParameters, args), 0 }, |
| 509 |
{ "environment", SD_JSON_VARIANT_ARRAY, json_dispatch_strv_environment, offsetof(MachineOpenParameters, env), 0 }, |
| 510 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 511 |
{} |
| 512 |
}; |
| 513 |
|
| 514 |
Manager *manager = ASSERT_PTR(userdata); |
| 515 |
_cleanup_close_ int ptmx_fd = -EBADF; |
| 516 |
_cleanup_(machine_open_paramaters_done) MachineOpenParameters p = { |
| 517 |
.pidref = PIDREF_NULL, |
| 518 |
.mode = _MACHINE_OPEN_MODE_INVALID, |
| 519 |
}; |
| 520 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 521 |
_cleanup_free_ char *ptmx_name = NULL, *command_line = NULL; |
| 522 |
const char *user = NULL, *path = NULL; /* gcc complains about uninitialized variables */ |
| 523 |
_cleanup_strv_free_ char **args = NULL; |
| 524 |
Machine *machine; |
| 525 |
int r, ptmx_fd_idx; |
| 526 |
|
| 527 |
assert(link); |
| 528 |
assert(parameters); |
| 529 |
|
| 530 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 531 |
if (r != 0) |
| 532 |
return r; |
| 533 |
|
| 534 |
if (p.mode == MACHINE_OPEN_MODE_SHELL) { |
| 535 |
/* json_dispatch_const_user_group_name() does valid_user_group_name(p.user) */ |
| 536 |
/* json_dispatch_const_path() does path_is_absolute(p.path) */ |
| 537 |
/* json_dispatch_strv_environment() does validation of p.env */ |
| 538 |
|
| 539 |
user = p.user ?: "root"; |
| 540 |
path = p.path ?: machine_default_shell_path(); |
| 541 |
args = !p.path ? machine_default_shell_args(user) : strv_isempty(p.args) ? strv_new(path) : TAKE_PTR(p.args); |
| 542 |
if (!args) |
| 543 |
return -ENOMEM; |
| 544 |
|
| 545 |
command_line = strv_join(args, " "); |
| 546 |
if (!command_line) |
| 547 |
return -ENOMEM; |
| 548 |
} |
| 549 |
|
| 550 |
r = lookup_machine_by_name_or_pidref(link, manager, p.name, &p.pidref, &machine); |
| 551 |
if (r == -ESRCH) |
| 552 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 553 |
if (r < 0) |
| 554 |
return r; |
| 555 |
|
| 556 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 557 |
/* Ensure only root can shell into the root namespace. This is to avoid unprivileged users |
| 558 |
* registering a process they own in the root user namespace, and then shelling in as root |
| 559 |
* or another user. Note that the shell operation is privileged and requires 'auth_admin', so we |
| 560 |
* do not need to check the caller's uid, as that will be checked by polkit, and if the machine's |
| 561 |
* and the caller's do not match, authorization will be required. It's only the case where the |
| 562 |
* caller owns the machine that will be shortcut and needs to be checked here. */ |
| 563 |
if (machine->uid != 0) { |
| 564 |
assert(machine->class != MACHINE_HOST); |
| 565 |
|
| 566 |
r = pidref_in_same_namespace(&PIDREF_MAKE_FROM_PID(1), &machine->leader, NAMESPACE_USER); |
| 567 |
if (r < 0) |
| 568 |
return log_debug_errno( |
| 569 |
r, |
| 570 |
"Failed to check if machine '%s' is running in the root user namespace: %m", |
| 571 |
machine->name); |
| 572 |
if (r > 0) |
| 573 |
return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL); |
| 574 |
} |
| 575 |
|
| 576 |
_cleanup_strv_free_ char **polkit_details = NULL; |
| 577 |
|
| 578 |
polkit_details = machine_open_polkit_details(p.mode, machine->name, user, path, command_line); |
| 579 |
r = varlink_verify_polkit_async_full( |
| 580 |
link, |
| 581 |
manager->system_bus, |
| 582 |
machine_open_polkit_action(p.mode, machine->class), |
| 583 |
(const char**) polkit_details, |
| 584 |
machine->uid, |
| 585 |
/* flags= */ 0, |
| 586 |
&manager->polkit_registry); |
| 587 |
if (r <= 0) |
| 588 |
return r; |
| 589 |
} |
| 590 |
|
| 591 |
ptmx_fd = machine_openpt(machine, O_RDWR|O_NOCTTY|O_CLOEXEC, &ptmx_name); |
| 592 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(ptmx_fd)) |
| 593 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 594 |
if (ptmx_fd < 0) |
| 595 |
return log_debug_errno(ptmx_fd, "Failed to open pseudo terminal: %m"); |
| 596 |
|
| 597 |
switch (p.mode) { |
| 598 |
case MACHINE_OPEN_MODE_TTY: |
| 599 |
/* noop */ |
| 600 |
break; |
| 601 |
|
| 602 |
case MACHINE_OPEN_MODE_LOGIN: |
| 603 |
r = machine_start_getty(machine, ptmx_name, /* error= */ NULL); |
| 604 |
if (r == -ENOENT) |
| 605 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_IPC, NULL); |
| 606 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 607 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 608 |
if (r < 0) |
| 609 |
return log_debug_errno(r, "Failed to start getty for machine '%s': %m", machine->name); |
| 610 |
|
| 611 |
break; |
| 612 |
|
| 613 |
case MACHINE_OPEN_MODE_SHELL: { |
| 614 |
assert(user && path && args); /* to avoid gcc complaining about possible uninitialized variables */ |
| 615 |
r = machine_start_shell(machine, ptmx_fd, ptmx_name, user, path, args, p.env, /* error= */ NULL); |
| 616 |
if (r == -ENOENT) |
| 617 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_IPC, NULL); |
| 618 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 619 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 620 |
if (r < 0) |
| 621 |
return log_debug_errno(r, "Failed to start shell for machine '%s': %m", machine->name); |
| 622 |
|
| 623 |
break; |
| 624 |
} |
| 625 |
|
| 626 |
default: |
| 627 |
assert_not_reached(); |
| 628 |
} |
| 629 |
|
| 630 |
ptmx_fd_idx = sd_varlink_push_fd(link, ptmx_fd); |
| 631 |
if (ERRNO_IS_PRIVILEGE(ptmx_fd_idx)) |
| 632 |
return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL); |
| 633 |
if (ptmx_fd_idx < 0) |
| 634 |
return log_debug_errno(ptmx_fd_idx, "Failed to push file descriptor over varlink: %m"); |
| 635 |
|
| 636 |
TAKE_FD(ptmx_fd); |
| 637 |
|
| 638 |
r = sd_json_buildo( |
| 639 |
&v, |
| 640 |
SD_JSON_BUILD_PAIR_INTEGER("ptyFileDescriptor", ptmx_fd_idx), |
| 641 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("ptyPath", ptmx_name)); |
| 642 |
if (r < 0) |
| 643 |
return r; |
| 644 |
|
| 645 |
return sd_varlink_reply(link, v); |
| 646 |
} |
| 647 |
|
| 648 |
typedef struct MachineMapParameters { |
| 649 |
const char *name; |
| 650 |
PidRef pidref; |
| 651 |
uid_t uid; |
| 652 |
gid_t gid; |
| 653 |
} MachineMapParameters; |
| 654 |
|
| 655 |
static void machine_map_paramaters_done(MachineMapParameters *p) { |
| 656 |
assert(p); |
| 657 |
pidref_done(&p->pidref); |
| 658 |
} |
| 659 |
|
| 660 |
int vl_method_map_from(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 661 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 662 |
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineMapParameters), |
| 663 |
{ "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(MachineMapParameters, uid), 0 }, |
| 664 |
{ "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(MachineMapParameters, gid), 0 }, |
| 665 |
{} |
| 666 |
}; |
| 667 |
|
| 668 |
Manager *manager = ASSERT_PTR(userdata); |
| 669 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 670 |
_cleanup_(machine_map_paramaters_done) MachineMapParameters p = { |
| 671 |
.pidref = PIDREF_NULL, |
| 672 |
.uid = UID_INVALID, |
| 673 |
.gid = GID_INVALID, |
| 674 |
}; |
| 675 |
uid_t converted_uid = UID_INVALID; |
| 676 |
gid_t converted_gid = GID_INVALID; |
| 677 |
Machine *machine; |
| 678 |
int r; |
| 679 |
|
| 680 |
assert(link); |
| 681 |
assert(parameters); |
| 682 |
|
| 683 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 684 |
if (r != 0) |
| 685 |
return r; |
| 686 |
|
| 687 |
if (p.uid != UID_INVALID && !uid_is_valid(p.uid)) |
| 688 |
return sd_varlink_error_invalid_parameter_name(link, "uid"); |
| 689 |
|
| 690 |
if (p.gid != GID_INVALID && !gid_is_valid(p.gid)) |
| 691 |
return sd_varlink_error_invalid_parameter_name(link, "gid"); |
| 692 |
|
| 693 |
r = lookup_machine_by_name_or_pidref(link, manager, p.name, &p.pidref, &machine); |
| 694 |
if (r == -ESRCH) |
| 695 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 696 |
if (r < 0) |
| 697 |
return r; |
| 698 |
|
| 699 |
if (machine->class != MACHINE_CONTAINER) |
| 700 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 701 |
|
| 702 |
if (p.uid != UID_INVALID) { |
| 703 |
r = machine_translate_uid(machine, p.uid, &converted_uid); |
| 704 |
if (r == -ESRCH) |
| 705 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_USER, NULL); |
| 706 |
if (r < 0) |
| 707 |
return log_debug_errno(r, "Failed to map uid=%u for machine '%s': %m", p.uid, machine->name); |
| 708 |
} |
| 709 |
|
| 710 |
if (p.gid != UID_INVALID) { |
| 711 |
r = machine_translate_gid(machine, p.gid, &converted_gid); |
| 712 |
if (r == -ESRCH) |
| 713 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_GROUP, NULL); |
| 714 |
if (r < 0) |
| 715 |
return log_debug_errno(r, "Failed to map gid=%u for machine '%s': %m", p.gid, machine->name); |
| 716 |
} |
| 717 |
|
| 718 |
r = sd_json_buildo(&v, |
| 719 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("uid", converted_uid, UID_INVALID), |
| 720 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("gid", converted_gid, GID_INVALID)); |
| 721 |
if (r < 0) |
| 722 |
return r; |
| 723 |
|
| 724 |
return sd_varlink_reply(link, v); |
| 725 |
} |
| 726 |
|
| 727 |
int vl_method_map_to(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 728 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 729 |
{ "uid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(MachineMapParameters, uid), 0 }, |
| 730 |
{ "gid", SD_JSON_VARIANT_UNSIGNED, sd_json_dispatch_uid_gid, offsetof(MachineMapParameters, gid), 0 }, |
| 731 |
{} |
| 732 |
}; |
| 733 |
|
| 734 |
Manager *manager = ASSERT_PTR(userdata); |
| 735 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 736 |
_cleanup_(machine_map_paramaters_done) MachineMapParameters p = { |
| 737 |
.pidref = PIDREF_NULL, |
| 738 |
.uid = UID_INVALID, |
| 739 |
.gid = GID_INVALID, |
| 740 |
}; |
| 741 |
Machine *machine_by_uid = NULL, *machine_by_gid = NULL; |
| 742 |
uid_t converted_uid = UID_INVALID; |
| 743 |
gid_t converted_gid = GID_INVALID; |
| 744 |
const char *machine_name = NULL; |
| 745 |
int r; |
| 746 |
|
| 747 |
assert(link); |
| 748 |
assert(parameters); |
| 749 |
|
| 750 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 751 |
if (r != 0) |
| 752 |
return r; |
| 753 |
|
| 754 |
if (p.uid != UID_INVALID) { |
| 755 |
if (!uid_is_valid(p.uid)) |
| 756 |
return sd_varlink_error_invalid_parameter_name(link, "uid"); |
| 757 |
if (p.uid < 0x10000) |
| 758 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_USER_IN_HOST_RANGE, NULL); |
| 759 |
} |
| 760 |
|
| 761 |
if (p.gid != GID_INVALID) { |
| 762 |
if (!gid_is_valid(p.gid)) |
| 763 |
return sd_varlink_error_invalid_parameter_name(link, "gid"); |
| 764 |
if (p.gid < 0x10000) |
| 765 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_GROUP_IN_HOST_RANGE, NULL); |
| 766 |
} |
| 767 |
|
| 768 |
if (p.uid != UID_INVALID) { |
| 769 |
r = manager_find_machine_for_uid(manager, p.uid, &machine_by_uid, &converted_uid); |
| 770 |
if (r < 0) |
| 771 |
return log_debug_errno(r, "Failed to find machine for uid=%u: %m", p.uid); |
| 772 |
if (!r) |
| 773 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_USER, NULL); |
| 774 |
} |
| 775 |
|
| 776 |
if (p.gid != GID_INVALID) { |
| 777 |
r = manager_find_machine_for_gid(manager, p.gid, &machine_by_gid, &converted_gid); |
| 778 |
if (r < 0) |
| 779 |
return log_debug_errno(r, "Failed to find machine for gid=%u: %m", p.gid); |
| 780 |
if (!r) |
| 781 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_GROUP, NULL); |
| 782 |
} |
| 783 |
|
| 784 |
if (machine_by_uid && machine_by_gid && machine_by_uid != machine_by_gid) { |
| 785 |
log_debug_errno(SYNTHETIC_ERRNO(ESRCH), "Mapping of UID %u and GID %u resulted in two different machines", p.uid, p.gid); |
| 786 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 787 |
} |
| 788 |
|
| 789 |
if (machine_by_uid) |
| 790 |
machine_name = machine_by_uid->name; |
| 791 |
else if (machine_by_gid) |
| 792 |
machine_name = machine_by_gid->name; |
| 793 |
else |
| 794 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 795 |
|
| 796 |
r = sd_json_buildo(&v, |
| 797 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("uid", converted_uid, UID_INVALID), |
| 798 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("gid", converted_gid, GID_INVALID), |
| 799 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("machineName", machine_name)); |
| 800 |
if (r < 0) |
| 801 |
return r; |
| 802 |
|
| 803 |
return sd_varlink_reply(link, v); |
| 804 |
} |
| 805 |
|
| 806 |
typedef struct MachineMountParameters { |
| 807 |
const char *name; |
| 808 |
PidRef pidref; |
| 809 |
const char *src; |
| 810 |
const char *dest; |
| 811 |
bool read_only; |
| 812 |
bool mkdir; |
| 813 |
} MachineMountParameters; |
| 814 |
|
| 815 |
static void machine_mount_paramaters_done(MachineMountParameters *p) { |
| 816 |
assert(p); |
| 817 |
|
| 818 |
pidref_done(&p->pidref); |
| 819 |
} |
| 820 |
|
| 821 |
int vl_method_bind_mount(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 822 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 823 |
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineMountParameters), |
| 824 |
{ "source", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(MachineMountParameters, src), SD_JSON_MANDATORY|SD_JSON_STRICT }, |
| 825 |
{ "destination", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(MachineMountParameters, dest), SD_JSON_STRICT }, |
| 826 |
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(MachineMountParameters, read_only), 0 }, |
| 827 |
{ "mkdir", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(MachineMountParameters, mkdir), 0 }, |
| 828 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 829 |
{} |
| 830 |
}; |
| 831 |
|
| 832 |
Manager *manager = ASSERT_PTR(userdata); |
| 833 |
_cleanup_(machine_mount_paramaters_done) MachineMountParameters p = { |
| 834 |
.pidref = PIDREF_NULL, |
| 835 |
}; |
| 836 |
MountInNamespaceFlags mount_flags = 0; |
| 837 |
uid_t uid_shift; |
| 838 |
int r; |
| 839 |
|
| 840 |
assert(link); |
| 841 |
assert(parameters); |
| 842 |
|
| 843 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 844 |
if (r != 0) |
| 845 |
return r; |
| 846 |
|
| 847 |
/* There is no need for extra validation since json_dispatch_const_path() with SD_JSON_STRICT does path_is_normalized() and path_is_absolute(). */ |
| 848 |
const char *dest = p.dest ?: p.src; |
| 849 |
|
| 850 |
Machine *machine; |
| 851 |
r = lookup_machine_by_name_or_pidref(link, manager, p.name, &p.pidref, &machine); |
| 852 |
if (r == -ESRCH) |
| 853 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 854 |
if (r != 0) |
| 855 |
return r; |
| 856 |
|
| 857 |
if (machine->class != MACHINE_CONTAINER) |
| 858 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 859 |
|
| 860 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 861 |
/* NB: For now not opened up to owner of machine without auth */ |
| 862 |
r = varlink_verify_polkit_async( |
| 863 |
link, |
| 864 |
manager->system_bus, |
| 865 |
"org.freedesktop.machine1.manage-machines", |
| 866 |
(const char**) STRV_MAKE("name", machine->name, |
| 867 |
"verb", "bind", |
| 868 |
"src", p.src, |
| 869 |
"dest", dest), |
| 870 |
&manager->polkit_registry); |
| 871 |
if (r <= 0) |
| 872 |
return r; |
| 873 |
} |
| 874 |
|
| 875 |
r = machine_get_uid_shift(machine, &uid_shift); |
| 876 |
if (r < 0) |
| 877 |
return log_debug_errno(r, "Failed to get machine UID shift: %m"); |
| 878 |
if (uid_shift != 0) { |
| 879 |
log_debug("Can't bind mount on container '%s' with user namespacing applied", machine->name); |
| 880 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 881 |
} |
| 882 |
|
| 883 |
if (p.read_only) |
| 884 |
mount_flags |= MOUNT_IN_NAMESPACE_READ_ONLY; |
| 885 |
if (p.mkdir) |
| 886 |
mount_flags |= MOUNT_IN_NAMESPACE_MAKE_FILE_OR_DIRECTORY; |
| 887 |
|
| 888 |
const char *propagate_directory = strjoina("/run/systemd/nspawn/propagate/", machine->name); |
| 889 |
|
| 890 |
r = bind_mount_in_namespace( |
| 891 |
&machine->leader, |
| 892 |
propagate_directory, |
| 893 |
"/run/host/incoming/", |
| 894 |
p.src, |
| 895 |
dest, |
| 896 |
mount_flags); |
| 897 |
if (r < 0) |
| 898 |
return log_debug_errno(r, "Failed to mount %s on %s in the namespace of machine '%s': %m", p.src, dest, machine->name); |
| 899 |
|
| 900 |
return sd_varlink_reply(link, NULL); |
| 901 |
} |
| 902 |
|
| 903 |
typedef struct MachineCopyParameters { |
| 904 |
const char *name; |
| 905 |
PidRef pidref; |
| 906 |
const char *src; |
| 907 |
const char *dest; |
| 908 |
bool replace; |
| 909 |
} MachineCopyParameters; |
| 910 |
|
| 911 |
static void machine_copy_paramaters_done(MachineCopyParameters *p) { |
| 912 |
assert(p); |
| 913 |
|
| 914 |
pidref_done(&p->pidref); |
| 915 |
} |
| 916 |
|
| 917 |
static int copy_done(Operation *operation, int ret, sd_bus_error *error) { |
| 918 |
assert(operation); |
| 919 |
assert(operation->link); |
| 920 |
|
| 921 |
if (ERRNO_IS_PRIVILEGE(ret)) |
| 922 |
return sd_varlink_error(operation->link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL); |
| 923 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(ret)) |
| 924 |
return sd_varlink_error(operation->link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 925 |
if (ret < 0) |
| 926 |
return sd_varlink_error_errno(operation->link, ret); |
| 927 |
|
| 928 |
return sd_varlink_reply(operation->link, NULL); |
| 929 |
} |
| 930 |
|
| 931 |
int vl_method_copy_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata, bool copy_from) { |
| 932 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 933 |
VARLINK_DISPATCH_MACHINE_LOOKUP_FIELDS(MachineCopyParameters), |
| 934 |
{ "source", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(MachineCopyParameters, src), SD_JSON_MANDATORY|SD_JSON_STRICT }, |
| 935 |
{ "destination", SD_JSON_VARIANT_STRING, json_dispatch_const_path, offsetof(MachineCopyParameters, dest), SD_JSON_STRICT }, |
| 936 |
{ "replace", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(MachineCopyParameters, replace), 0 }, |
| 937 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 938 |
{} |
| 939 |
}; |
| 940 |
|
| 941 |
int r; |
| 942 |
Manager *manager = ASSERT_PTR(userdata); |
| 943 |
_cleanup_(machine_copy_paramaters_done) MachineCopyParameters p = { |
| 944 |
.pidref = PIDREF_NULL |
| 945 |
}; |
| 946 |
|
| 947 |
assert(link); |
| 948 |
assert(parameters); |
| 949 |
|
| 950 |
if (manager->n_operations >= OPERATIONS_MAX) |
| 951 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_TOO_MANY_OPERATIONS, NULL); |
| 952 |
|
| 953 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 954 |
if (r != 0) |
| 955 |
return r; |
| 956 |
|
| 957 |
/* There is no need for extra validation since json_dispatch_const_path() with SD_JSON_STRICT does path_is_normalized() and path_is_absolute(). */ |
| 958 |
const char *dest = p.dest ?: p.src; |
| 959 |
const char *container_path = copy_from ? p.src : dest; |
| 960 |
const char *host_path = copy_from ? dest : p.src; |
| 961 |
CopyFlags copy_flags = COPY_REFLINK|COPY_MERGE|COPY_HARDLINKS; |
| 962 |
copy_flags |= p.replace ? COPY_REPLACE : 0; |
| 963 |
|
| 964 |
Machine *machine; |
| 965 |
r = lookup_machine_by_name_or_pidref(link, manager, p.name, &p.pidref, &machine); |
| 966 |
if (r == -ESRCH) |
| 967 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NO_SUCH_MACHINE, NULL); |
| 968 |
if (r != 0) |
| 969 |
return r; |
| 970 |
|
| 971 |
if (machine->class != MACHINE_CONTAINER) |
| 972 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 973 |
|
| 974 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 975 |
/* NB: For now not opened up to owner of machine without auth */ |
| 976 |
r = varlink_verify_polkit_async( |
| 977 |
link, |
| 978 |
manager->system_bus, |
| 979 |
"org.freedesktop.machine1.manage-machines", |
| 980 |
(const char**) STRV_MAKE("name", machine->name, |
| 981 |
"verb", "copy", |
| 982 |
"src", p.src, |
| 983 |
"dest", dest), |
| 984 |
&manager->polkit_registry); |
| 985 |
if (r <= 0) |
| 986 |
return r; |
| 987 |
} |
| 988 |
|
| 989 |
Operation *op; |
| 990 |
r = machine_copy_from_to_operation(manager, machine, host_path, container_path, copy_from, copy_flags, &op); |
| 991 |
if (r < 0) |
| 992 |
return r; |
| 993 |
|
| 994 |
operation_attach_varlink_reply(op, link); |
| 995 |
op->done = copy_done; |
| 996 |
return 1; |
| 997 |
} |
| 998 |
|
| 999 |
int vl_method_open_root_directory_internal(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 1000 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1001 |
_cleanup_close_ int fd = -EBADF; |
| 1002 |
Machine *machine = ASSERT_PTR(userdata); |
| 1003 |
Manager *manager = ASSERT_PTR(machine->manager); |
| 1004 |
int r; |
| 1005 |
|
| 1006 |
/* NB: For now not opened up to owner of machine without auth */ |
| 1007 |
if (manager->runtime_scope != RUNTIME_SCOPE_USER) { |
| 1008 |
r = varlink_verify_polkit_async( |
| 1009 |
link, |
| 1010 |
manager->system_bus, |
| 1011 |
"org.freedesktop.machine1.manage-machines", |
| 1012 |
(const char**) STRV_MAKE("name", machine->name, |
| 1013 |
"verb", "open_root_directory"), |
| 1014 |
&manager->polkit_registry); |
| 1015 |
if (r <= 0) |
| 1016 |
return r; |
| 1017 |
} |
| 1018 |
|
| 1019 |
fd = machine_open_root_directory(machine); |
| 1020 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(fd)) |
| 1021 |
return sd_varlink_error(link, VARLINK_ERROR_MACHINE_NOT_SUPPORTED, NULL); |
| 1022 |
if (fd < 0) |
| 1023 |
return log_debug_errno(fd, "Failed to open root directory of machine '%s': %m", machine->name); |
| 1024 |
|
| 1025 |
int fd_idx = sd_varlink_push_fd(link, fd); |
| 1026 |
if (ERRNO_IS_PRIVILEGE(fd_idx)) |
| 1027 |
return sd_varlink_error(link, SD_VARLINK_ERROR_PERMISSION_DENIED, NULL); |
| 1028 |
if (fd_idx < 0) |
| 1029 |
return log_debug_errno(fd_idx, "Failed to push file descriptor over varlink: %m"); |
| 1030 |
|
| 1031 |
TAKE_FD(fd); |
| 1032 |
|
| 1033 |
r = sd_json_buildo(&v, SD_JSON_BUILD_PAIR_INTEGER("fileDescriptor", fd_idx)); |
| 1034 |
if (r < 0) |
| 1035 |
return r; |
| 1036 |
|
| 1037 |
return sd_varlink_reply(link, v); |
| 1038 |
} |