Debian Codegraph @

src/shared/storage-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-varlink.h"
4
5 #include "alloc-util.h"
6 #include "fd-util.h"
7 #include "json-util.h"
8 #include "machine-util.h"
9 #include "path-lookup.h"
10 #include "path-util.h"
11 #include "runtime-scope.h"
12 #include "string-table.h"
13 #include "storage-util.h"
14
15 static const char *volume_type_table[_VOLUME_TYPE_MAX] = {
16 [VOLUME_BLK] = "blk",
17 [VOLUME_REG] = "reg",
18 [VOLUME_DIR] = "dir",
19 };
20
21 static const char *create_mode_table[_CREATE_MODE_MAX] = {
22 [CREATE_ANY] = "any",
23 [CREATE_NEW] = "new",
24 [CREATE_OPEN] = "open",
25 };
26
27 DEFINE_STRING_TABLE_LOOKUP(volume_type, VolumeType);
28 DEFINE_STRING_TABLE_LOOKUP(create_mode, CreateMode);
29
30 JSON_DISPATCH_ENUM_DEFINE(json_dispatch_volume_type, VolumeType, volume_type_from_string);
31 JSON_DISPATCH_ENUM_DEFINE(json_dispatch_create_mode, CreateMode, create_mode_from_string);
32
33 void storage_acquire_reply_done(StorageAcquireReply *reply) {
34 if (!reply)
35 return;
36
37 reply->fd = safe_close(reply->fd);
38 }
39
40 int storage_acquire_volume(
41 RuntimeScope scope,
42 const BindVolume *bv,
43 bool allow_interactive_auth,
44 char **reterr_error_id,
45 StorageAcquireReply *ret) {
46
47 int r;
48
49 assert(bv);
50 assert(bv->provider);
51 assert(bv->volume);
52 assert(ret);
53
54 /* Defense-in-depth: this is a libshared helper that may grow new callers; reject
55 * provider names that could escape the StorageProvider runtime directory. */
56 if (!storage_provider_name_is_valid(bv->provider))
57 return -EINVAL;
58
59 _cleanup_free_ char *socket_path = NULL;
60 r = runtime_directory_generic(scope, "systemd/io.systemd.StorageProvider", &socket_path);
61 if (r < 0)
62 return r;
63
64 if (!path_extend(&socket_path, bv->provider))
65 return -ENOMEM;
66
67 _cleanup_(sd_varlink_unrefp) sd_varlink *link = NULL;
68 r = sd_varlink_connect_address(&link, socket_path);
69 if (r < 0)
70 return r;
71
72 r = sd_varlink_set_allow_fd_passing_input(link, true);
73 if (r < 0)
74 return r;
75
76 sd_json_variant *mreply = NULL;
77 const char *merror_id = NULL;
78 r = sd_varlink_callbo(
79 link,
80 "io.systemd.StorageProvider.Acquire",
81 &mreply,
82 &merror_id,
83 SD_JSON_BUILD_PAIR_STRING("name", bv->volume),
84 JSON_BUILD_PAIR_CONDITION_STRING(bv->create_mode >= 0, "createMode", create_mode_to_string(bv->create_mode)),
85 JSON_BUILD_PAIR_STRING_NON_EMPTY("template", bv->template),
86 JSON_BUILD_PAIR_TRISTATE_NON_NULL("readOnly", read_only_mode_to_tristate(bv->read_only)),
87 JSON_BUILD_PAIR_CONDITION_STRING(bv->request_as >= 0, "requestAs", volume_type_to_string(bv->request_as)),
88 JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("createSizeBytes", bv->create_size_bytes, UINT64_MAX),
89 SD_JSON_BUILD_PAIR_BOOLEAN("allowInteractiveAuthentication", allow_interactive_auth));
90 if (r < 0)
91 return r;
92
93 if (merror_id) {
94 if (reterr_error_id) {
95 char *copy = strdup(merror_id);
96 if (!copy)
97 return -ENOMEM;
98 *reterr_error_id = copy;
99 }
100
101 r = sd_varlink_error_to_errno(merror_id, mreply);
102 return r == -EBADR ? -EPROTO : r;
103 }
104
105 /* tmp.fd holds the JSON fd index until sd_varlink_take_fd() swaps it for the real fd. */
106 StorageAcquireReply tmp = STORAGE_ACQUIRE_REPLY_INIT;
107
108 static const sd_json_dispatch_field dispatch_table[] = {
109 { "fileDescriptorIndex", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_int, voffsetof(StorageAcquireReply, fd), SD_JSON_MANDATORY },
110 { "readOnly", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, voffsetof(StorageAcquireReply, read_only), 0 },
111 { "type", SD_JSON_VARIANT_STRING, json_dispatch_volume_type, voffsetof(StorageAcquireReply, type), SD_JSON_MANDATORY },
112 { "baseUID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, voffsetof(StorageAcquireReply, base_uid), 0 },
113 { "baseGID", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, voffsetof(StorageAcquireReply, base_gid), 0 },
114 {}
115 };
116
117 r = sd_json_dispatch(mreply, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &tmp);
118 if (r < 0)
119 return r;
120 if (tmp.fd < 0)
121 return -EBADMSG;
122
123 _cleanup_close_ int fd = sd_varlink_take_fd(link, tmp.fd);
124 if (fd < 0)
125 return fd;
126
127 tmp.fd = TAKE_FD(fd);
128 *ret = tmp;
129 return 0;
130 }