| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fnmatch.h> |
| 4 |
|
| 5 |
#include "sd-device.h" |
| 6 |
#include "sd-json.h" |
| 7 |
#include "sd-varlink.h" |
| 8 |
|
| 9 |
#include "blockdev-list.h" |
| 10 |
#include "build.h" |
| 11 |
#include "bus-polkit.h" |
| 12 |
#include "device-private.h" |
| 13 |
#include "device-util.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "format-table.h" |
| 17 |
#include "hashmap.h" |
| 18 |
#include "help-util.h" |
| 19 |
#include "json-util.h" |
| 20 |
#include "log.h" |
| 21 |
#include "main-func.h" |
| 22 |
#include "options.h" |
| 23 |
#include "path-util.h" |
| 24 |
#include "storage-util.h" |
| 25 |
#include "strv.h" |
| 26 |
#include "varlink-io.systemd.StorageProvider.h" |
| 27 |
#include "varlink-util.h" |
| 28 |
|
| 29 |
static int block_device_pick_name( |
| 30 |
const BlockDevice *d, |
| 31 |
const char **ret_name, |
| 32 |
char ***ret_aliases) { |
| 33 |
|
| 34 |
int r; |
| 35 |
|
| 36 |
assert(d); |
| 37 |
assert(d->node); |
| 38 |
assert(ret_name); |
| 39 |
assert(ret_aliases); |
| 40 |
|
| 41 |
static const char *const prefixes[] = { |
| 42 |
/* The list of preferred prefixes, in order of preference. Note: for security reasons we only |
| 43 |
* use identifiers that do not depend on the *contents* of the device, i.e. we restrict |
| 44 |
* ourselves to IDs whose fields are either chosen by whoever created the kernel device or are |
| 45 |
* hardware properties, but not names generated from superblock metainformation or similar. */ |
| 46 |
"/dev/mapper", |
| 47 |
"/dev/disk/by-loop-ref", |
| 48 |
"/dev/disk/by-id", |
| 49 |
"/dev/disk/by-path", |
| 50 |
}; |
| 51 |
|
| 52 |
const char* found[ELEMENTSOF(prefixes)] = {}; |
| 53 |
_cleanup_strv_free_ char **aliases = NULL; |
| 54 |
size_t best = SIZE_MAX; |
| 55 |
STRV_FOREACH(sl, d->symlinks) { |
| 56 |
bool matched = false; |
| 57 |
for (size_t i = 0; i < ELEMENTSOF(prefixes); i++) { |
| 58 |
if (!path_startswith(*sl, prefixes[i])) |
| 59 |
continue; |
| 60 |
|
| 61 |
if (found[i]) { |
| 62 |
/* Two symlinks with the same prefix? Then keep the lower one. */ |
| 63 |
if (path_compare(*sl, found[i]) > 0) |
| 64 |
continue; |
| 65 |
|
| 66 |
r = strv_extend(&aliases, found[i]); |
| 67 |
if (r < 0) |
| 68 |
return r; |
| 69 |
} |
| 70 |
|
| 71 |
found[i] = *sl; |
| 72 |
if (i < best) |
| 73 |
best = i; |
| 74 |
matched = true; |
| 75 |
} |
| 76 |
|
| 77 |
if (!matched) { |
| 78 |
r = strv_extend(&aliases, *sl); |
| 79 |
if (r < 0) |
| 80 |
return r; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if (best == SIZE_MAX) /* No preferred prefix found, use the kernel device name */ |
| 85 |
*ret_name = d->node; |
| 86 |
else { |
| 87 |
/* We found a preferred prefix, add the kernel device name to the aliases then. */ |
| 88 |
r = strv_extend(&aliases, d->node); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
/* If there are any less preferred prefixes also add them to the aliases array */ |
| 93 |
for (size_t i = best + 1; i < ELEMENTSOF(prefixes); i++) { |
| 94 |
if (!found[i]) |
| 95 |
continue; |
| 96 |
|
| 97 |
r = strv_extend(&aliases, found[i]); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
} |
| 101 |
|
| 102 |
*ret_name = found[best]; |
| 103 |
} |
| 104 |
|
| 105 |
strv_sort(aliases); |
| 106 |
*ret_aliases = TAKE_PTR(aliases); |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
static bool block_device_match(const BlockDevice *d, const char *match) { |
| 112 |
assert(d); |
| 113 |
assert(d->node); |
| 114 |
|
| 115 |
if (!match) |
| 116 |
return true; |
| 117 |
|
| 118 |
if (fnmatch(match, d->node, FNM_NOESCAPE) == 0) |
| 119 |
return true; |
| 120 |
|
| 121 |
STRV_FOREACH(sl, d->symlinks) |
| 122 |
if (fnmatch(match, *sl, FNM_NOESCAPE) == 0) |
| 123 |
return true; |
| 124 |
|
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
static int vl_method_list_volumes( |
| 129 |
sd_varlink *link, |
| 130 |
sd_json_variant *parameters, |
| 131 |
sd_varlink_method_flags_t flags, |
| 132 |
void *userdata) { |
| 133 |
|
| 134 |
int r; |
| 135 |
|
| 136 |
assert(link); |
| 137 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 138 |
|
| 139 |
struct { |
| 140 |
const char *match_name; |
| 141 |
} p = {}; |
| 142 |
|
| 143 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 144 |
{ "matchName", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, match_name), 0 }, |
| 145 |
{} |
| 146 |
}; |
| 147 |
|
| 148 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 149 |
if (r != 0) |
| 150 |
return r; |
| 151 |
|
| 152 |
BlockDevice *l = NULL; |
| 153 |
size_t n = 0; |
| 154 |
CLEANUP_ARRAY(l, n, block_device_array_free); |
| 155 |
|
| 156 |
r = blockdev_list( |
| 157 |
BLOCKDEV_LIST_SHOW_SYMLINKS| |
| 158 |
BLOCKDEV_LIST_IGNORE_ROOT| |
| 159 |
BLOCKDEV_LIST_IGNORE_EMPTY| |
| 160 |
BLOCKDEV_LIST_METADATA, |
| 161 |
&l, |
| 162 |
&n); |
| 163 |
if (r < 0) |
| 164 |
return r; |
| 165 |
|
| 166 |
r = sd_varlink_set_sentinel(link, "io.systemd.StorageProvider.NoSuchVolume"); |
| 167 |
if (r < 0) |
| 168 |
return r; |
| 169 |
|
| 170 |
FOREACH_ARRAY(d, l, n) { |
| 171 |
const char *name = NULL; |
| 172 |
_cleanup_strv_free_ char **aliases = NULL; |
| 173 |
|
| 174 |
if (!block_device_match(d, p.match_name)) |
| 175 |
continue; |
| 176 |
|
| 177 |
r = block_device_pick_name(d, &name, &aliases); |
| 178 |
if (r < 0) |
| 179 |
return r; |
| 180 |
|
| 181 |
r = sd_varlink_replybo( |
| 182 |
link, |
| 183 |
SD_JSON_BUILD_PAIR_STRING("name", name), |
| 184 |
JSON_BUILD_PAIR_STRV_NON_EMPTY("aliases", aliases), |
| 185 |
SD_JSON_BUILD_PAIR_STRING("type", "blk"), |
| 186 |
SD_JSON_BUILD_PAIR_CONDITION(d->read_only >= 0, "readOnly", SD_JSON_BUILD_BOOLEAN(d->read_only)), |
| 187 |
JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("sizeBytes", d->size, UINT64_MAX)); |
| 188 |
if (r < 0) |
| 189 |
return r; |
| 190 |
} |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
static int vl_method_list_templates( |
| 196 |
sd_varlink *link, |
| 197 |
sd_json_variant *parameters, |
| 198 |
sd_varlink_method_flags_t flags, |
| 199 |
void *userdata) { |
| 200 |
|
| 201 |
/* This storage provider does not support templates */ |
| 202 |
assert(link); |
| 203 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 204 |
|
| 205 |
return sd_varlink_error(link, "io.systemd.StorageProvider.NoSuchTemplate", NULL); |
| 206 |
} |
| 207 |
|
| 208 |
static int device_open_disk_auto_rw(sd_device *d, int *read_only) { |
| 209 |
assert(d); |
| 210 |
assert(read_only); |
| 211 |
|
| 212 |
int fd = sd_device_open(d, *read_only > 0 ? O_RDONLY : O_RDWR); |
| 213 |
if (fd < 0) { |
| 214 |
if (!ERRNO_IS_NEG_FS_WRITE_REFUSED(fd) || *read_only >= 0) |
| 215 |
return log_device_debug_errno(d, fd, "Failed to open device in %s mode: %m", *read_only > 0 ? "read-only" : "read-write"); |
| 216 |
|
| 217 |
/* Try again in read-only mode */ |
| 218 |
fd = sd_device_open(d, O_RDONLY); |
| 219 |
if (fd < 0) |
| 220 |
return log_device_debug_errno(d, fd, "Failed to open device in read-only mode, too: %m"); |
| 221 |
|
| 222 |
*read_only = true; |
| 223 |
} else |
| 224 |
*read_only = *read_only > 0; |
| 225 |
|
| 226 |
return fd; |
| 227 |
} |
| 228 |
|
| 229 |
static int vl_method_acquire( |
| 230 |
sd_varlink *link, |
| 231 |
sd_json_variant *parameters, |
| 232 |
sd_varlink_method_flags_t flags, |
| 233 |
void *userdata) { |
| 234 |
|
| 235 |
Hashmap **polkit_registry = ASSERT_PTR(userdata); |
| 236 |
int r; |
| 237 |
|
| 238 |
assert(link); |
| 239 |
|
| 240 |
struct { |
| 241 |
const char *name; |
| 242 |
CreateMode create_mode; |
| 243 |
const char *template; |
| 244 |
int read_only; |
| 245 |
VolumeType request_as; |
| 246 |
uint64_t create_size; |
| 247 |
} p = { |
| 248 |
.create_mode = CREATE_ANY, |
| 249 |
.read_only = -1, |
| 250 |
.request_as = _VOLUME_TYPE_INVALID, |
| 251 |
.create_size = UINT64_MAX, /* never actually used here, just validated; we don't allow creation of block devices here */ |
| 252 |
}; |
| 253 |
|
| 254 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 255 |
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, name), SD_JSON_MANDATORY }, |
| 256 |
{ "createMode", SD_JSON_VARIANT_STRING, json_dispatch_create_mode, voffsetof(p, create_mode), 0 }, |
| 257 |
{ "template", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, voffsetof(p, template), 0 }, |
| 258 |
{ "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, voffsetof(p, read_only), 0 }, |
| 259 |
{ "requestAs", SD_JSON_VARIANT_STRING, json_dispatch_volume_type, voffsetof(p, request_as), 0 }, |
| 260 |
{ "createSizeBytes", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, voffsetof(p, create_size), 0 }, |
| 261 |
VARLINK_DISPATCH_POLKIT_FIELD, |
| 262 |
{} |
| 263 |
}; |
| 264 |
|
| 265 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 266 |
if (r != 0) |
| 267 |
return r; |
| 268 |
|
| 269 |
if (!storage_volume_name_is_valid(p.name)) |
| 270 |
return sd_varlink_error_invalid_parameter_name(link, "name"); |
| 271 |
if (!path_startswith(p.name, "/dev") || !path_is_normalized(p.name)) |
| 272 |
return sd_varlink_error(link, "io.systemd.StorageProvider.NoSuchVolume", NULL); |
| 273 |
|
| 274 |
if (!IN_SET(p.create_mode, CREATE_ANY, CREATE_OPEN)) |
| 275 |
return sd_varlink_error(link, "io.systemd.StorageProvider.CreateNotSupported", NULL); |
| 276 |
|
| 277 |
/* off_t is signed, hence refuse overly long requests */ |
| 278 |
if (p.create_size != UINT64_MAX && p.create_size > INT64_MAX) |
| 279 |
return sd_varlink_error_invalid_parameter_name(link, "createSizeBytes"); |
| 280 |
|
| 281 |
if (!isempty(p.template)) { |
| 282 |
if (!storage_template_name_is_valid(p.template)) |
| 283 |
return sd_varlink_error_invalid_parameter_name(link, "template"); |
| 284 |
|
| 285 |
return sd_varlink_error(link, "io.systemd.StorageProvider.NoSuchTemplate", NULL); |
| 286 |
} |
| 287 |
|
| 288 |
if (p.request_as >= 0 && p.request_as != VOLUME_BLK) |
| 289 |
return sd_varlink_error(link, "io.systemd.StorageProvider.TypeNotSupported", NULL); |
| 290 |
|
| 291 |
const char *details[] = { |
| 292 |
"name", p.name, |
| 293 |
NULL |
| 294 |
}; |
| 295 |
|
| 296 |
r = varlink_verify_polkit_async( |
| 297 |
link, |
| 298 |
/* bus= */ NULL, |
| 299 |
"io.systemd.storage.block.acquire", |
| 300 |
details, |
| 301 |
polkit_registry); |
| 302 |
if (r <= 0) |
| 303 |
return r; |
| 304 |
|
| 305 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 306 |
r = sd_device_new_from_devname(&d, p.name); |
| 307 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 308 |
return sd_varlink_error(link, "io.systemd.StorageProvider.NoSuchVolume", NULL); |
| 309 |
if (r < 0) |
| 310 |
return r; |
| 311 |
|
| 312 |
if (!device_in_subsystem(d, "block")) |
| 313 |
return sd_varlink_error(link, "io.systemd.StorageProvider.NoSuchVolume", NULL); |
| 314 |
|
| 315 |
/* The error returns are sometimes a bit inconclusive (i.e. read-only media might appear as |
| 316 |
* inaccessible due to a permission issue), hence let's do an explicit check first, to give good |
| 317 |
* answers */ |
| 318 |
if (p.read_only <= 0) { |
| 319 |
r = device_get_sysattr_bool(d, "ro"); |
| 320 |
if (r < 0) |
| 321 |
log_device_debug_errno(d, r, "Failed to acquire read-only flag of device '%s', ignoring: %m", p.name); |
| 322 |
else if (r > 0) { |
| 323 |
if (p.read_only == 0) |
| 324 |
return sd_varlink_error(link, "io.systemd.StorageProvider.ReadOnlyVolume", NULL); |
| 325 |
|
| 326 |
p.read_only = true; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
_cleanup_close_ int fd = device_open_disk_auto_rw(d, &p.read_only); |
| 331 |
if (ERRNO_IS_NEG_FS_WRITE_REFUSED(fd)) |
| 332 |
return sd_varlink_error(link, "io.systemd.StorageProvider.ReadOnlyVolume", NULL); |
| 333 |
if (fd < 0) |
| 334 |
return fd; |
| 335 |
|
| 336 |
assert(p.read_only >= 0); /* flag is now definitely initialized to either true or false, not negative anymore */ |
| 337 |
|
| 338 |
int idx = sd_varlink_push_fd(link, fd); |
| 339 |
if (idx < 0) |
| 340 |
return idx; |
| 341 |
|
| 342 |
TAKE_FD(fd); |
| 343 |
|
| 344 |
return sd_varlink_replybo( |
| 345 |
link, |
| 346 |
SD_JSON_BUILD_PAIR_INTEGER("fileDescriptorIndex", idx), |
| 347 |
SD_JSON_BUILD_PAIR_STRING("type", "blk"), |
| 348 |
SD_JSON_BUILD_PAIR_BOOLEAN("readOnly", p.read_only)); |
| 349 |
} |
| 350 |
|
| 351 |
static int vl_server(void) { |
| 352 |
int r; |
| 353 |
|
| 354 |
_cleanup_(hashmap_freep) Hashmap *polkit_registry = NULL; |
| 355 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL; |
| 356 |
r = varlink_server_new( |
| 357 |
&varlink_server, |
| 358 |
SD_VARLINK_SERVER_HANDLE_SIGINT| |
| 359 |
SD_VARLINK_SERVER_HANDLE_SIGTERM| |
| 360 |
SD_VARLINK_SERVER_ALLOW_FD_PASSING_OUTPUT| |
| 361 |
SD_VARLINK_SERVER_INHERIT_USERDATA, |
| 362 |
&polkit_registry); |
| 363 |
if (r < 0) |
| 364 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 365 |
|
| 366 |
r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_StorageProvider); |
| 367 |
if (r < 0) |
| 368 |
return log_error_errno(r, "Failed to add Varlink interface: %m"); |
| 369 |
|
| 370 |
r = sd_varlink_server_bind_method_many( |
| 371 |
varlink_server, |
| 372 |
"io.systemd.StorageProvider.Acquire", vl_method_acquire, |
| 373 |
"io.systemd.StorageProvider.ListVolumes", vl_method_list_volumes, |
| 374 |
"io.systemd.StorageProvider.ListTemplates", vl_method_list_templates); |
| 375 |
if (r < 0) |
| 376 |
return log_error_errno(r, "Failed to bind Varlink methods: %m"); |
| 377 |
|
| 378 |
r = sd_varlink_server_loop_auto(varlink_server); |
| 379 |
if (r < 0) |
| 380 |
return log_error_errno(r, "Failed to run Varlink event loop: %m"); |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
static int help(void) { |
| 386 |
int r; |
| 387 |
|
| 388 |
help_cmdline("[OPTIONS...]"); |
| 389 |
help_abstract("Simple block device backed storage provider"); |
| 390 |
|
| 391 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 392 |
r = option_parser_get_help_table(&options); |
| 393 |
if (r < 0) |
| 394 |
return r; |
| 395 |
|
| 396 |
help_section("Options"); |
| 397 |
|
| 398 |
r = table_print_or_warn(options); |
| 399 |
if (r < 0) |
| 400 |
return r; |
| 401 |
|
| 402 |
help_man_page_reference("systemd-storage-block", "8"); |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
static int parse_argv(int argc, char *argv[]) { |
| 407 |
assert(argc >= 0); |
| 408 |
assert(argv); |
| 409 |
|
| 410 |
OptionParser opts = { argc, argv }; |
| 411 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 412 |
switch (c) { |
| 413 |
|
| 414 |
OPTION_COMMON_HELP: |
| 415 |
return help(); |
| 416 |
|
| 417 |
OPTION_COMMON_VERSION: |
| 418 |
return version(); |
| 419 |
} |
| 420 |
|
| 421 |
if (option_parser_get_n_args(&opts) > 0) |
| 422 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments."); |
| 423 |
|
| 424 |
return 1; |
| 425 |
} |
| 426 |
|
| 427 |
static int run(int argc, char* argv[]) { |
| 428 |
int r; |
| 429 |
|
| 430 |
log_setup(); |
| 431 |
|
| 432 |
r = parse_argv(argc, argv); |
| 433 |
if (r <= 0) |
| 434 |
return r; |
| 435 |
|
| 436 |
return vl_server(); |
| 437 |
} |
| 438 |
|
| 439 |
DEFINE_MAIN_FUNCTION(run); |