| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "bus-message-util.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "copy.h" |
| 11 |
#include "in-addr-util.h" |
| 12 |
#include "resolve-util.h" |
| 13 |
#include "set.h" |
| 14 |
#include "socket-netlink.h" |
| 15 |
|
| 16 |
int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) { |
| 17 |
const void *a; |
| 18 |
size_t sz; |
| 19 |
int r; |
| 20 |
|
| 21 |
assert(m); |
| 22 |
|
| 23 |
r = sd_bus_message_read_array(m, 'y', &a, &sz); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
|
| 27 |
switch (sz) { |
| 28 |
|
| 29 |
case 0: |
| 30 |
if (ret) |
| 31 |
*ret = SD_ID128_NULL; |
| 32 |
return 0; |
| 33 |
|
| 34 |
case sizeof(sd_id128_t): |
| 35 |
if (ret) |
| 36 |
memcpy(ret, a, sz); |
| 37 |
return !memeqzero(a, sz); /* This mimics sd_id128_is_null(), but ret may be NULL, |
| 38 |
* and a may be misaligned, so use memeqzero() here. */ |
| 39 |
|
| 40 |
default: |
| 41 |
return -EINVAL; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
int bus_message_read_ifindex(sd_bus_message *message, sd_bus_error *reterr_error, int *ret) { |
| 46 |
int ifindex, r; |
| 47 |
|
| 48 |
assert(message); |
| 49 |
assert(ret); |
| 50 |
|
| 51 |
assert_cc(sizeof(int) == sizeof(int32_t)); |
| 52 |
|
| 53 |
r = sd_bus_message_read(message, "i", &ifindex); |
| 54 |
if (r < 0) |
| 55 |
return r; |
| 56 |
|
| 57 |
if (ifindex <= 0) |
| 58 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid interface index"); |
| 59 |
|
| 60 |
*ret = ifindex; |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
int bus_message_read_family(sd_bus_message *message, sd_bus_error *reterr_error, int *ret) { |
| 66 |
int family, r; |
| 67 |
|
| 68 |
assert(message); |
| 69 |
assert(ret); |
| 70 |
|
| 71 |
assert_cc(sizeof(int) == sizeof(int32_t)); |
| 72 |
|
| 73 |
r = sd_bus_message_read(message, "i", &family); |
| 74 |
if (r < 0) |
| 75 |
return r; |
| 76 |
|
| 77 |
if (!IN_SET(family, AF_INET, AF_INET6)) |
| 78 |
return sd_bus_error_setf(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Unknown address family %i", family); |
| 79 |
|
| 80 |
*ret = family; |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int bus_message_read_in_addr_auto(sd_bus_message *message, sd_bus_error *reterr_error, int *ret_family, union in_addr_union *ret_addr) { |
| 85 |
int family, r; |
| 86 |
const void *d; |
| 87 |
size_t sz; |
| 88 |
|
| 89 |
assert(message); |
| 90 |
|
| 91 |
r = bus_message_read_family(message, reterr_error, &family); |
| 92 |
if (r < 0) |
| 93 |
return r; |
| 94 |
|
| 95 |
r = sd_bus_message_read_array(message, 'y', &d, &sz); |
| 96 |
if (r < 0) |
| 97 |
return r; |
| 98 |
|
| 99 |
if (sz != FAMILY_ADDRESS_SIZE(family)) |
| 100 |
return sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid address size"); |
| 101 |
|
| 102 |
if (ret_family) |
| 103 |
*ret_family = family; |
| 104 |
if (ret_addr) |
| 105 |
memcpy(ret_addr, d, sz); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
static int bus_message_read_dns_one( |
| 110 |
sd_bus_message *message, |
| 111 |
sd_bus_error *reterr_error, |
| 112 |
bool extended, |
| 113 |
int *ret_family, |
| 114 |
union in_addr_union *ret_address, |
| 115 |
uint16_t *ret_port, |
| 116 |
const char **ret_server_name) { |
| 117 |
const char *server_name = NULL; |
| 118 |
union in_addr_union a; |
| 119 |
uint16_t port = 0; |
| 120 |
int family, r; |
| 121 |
|
| 122 |
assert(message); |
| 123 |
assert(ret_family); |
| 124 |
assert(ret_address); |
| 125 |
assert(ret_port); |
| 126 |
assert(ret_server_name); |
| 127 |
|
| 128 |
r = sd_bus_message_enter_container(message, 'r', extended ? "iayqs" : "iay"); |
| 129 |
if (r <= 0) |
| 130 |
return r; |
| 131 |
|
| 132 |
r = bus_message_read_in_addr_auto(message, reterr_error, &family, &a); |
| 133 |
if (r < 0) |
| 134 |
return r; |
| 135 |
|
| 136 |
if (!dns_server_address_valid(family, &a)) { |
| 137 |
r = sd_bus_error_set(reterr_error, SD_BUS_ERROR_INVALID_ARGS, "Invalid DNS server address"); |
| 138 |
assert(r < 0); |
| 139 |
return r; |
| 140 |
} |
| 141 |
|
| 142 |
if (extended) { |
| 143 |
r = sd_bus_message_read(message, "q", &port); |
| 144 |
if (r < 0) |
| 145 |
return r; |
| 146 |
|
| 147 |
if (IN_SET(port, 53, 853)) |
| 148 |
port = 0; |
| 149 |
|
| 150 |
r = sd_bus_message_read(message, "s", &server_name); |
| 151 |
if (r < 0) |
| 152 |
return r; |
| 153 |
} |
| 154 |
|
| 155 |
r = sd_bus_message_exit_container(message); |
| 156 |
if (r < 0) |
| 157 |
return r; |
| 158 |
|
| 159 |
*ret_family = family; |
| 160 |
*ret_address = a; |
| 161 |
*ret_port = port; |
| 162 |
*ret_server_name = server_name; |
| 163 |
|
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
int bus_message_read_dns_servers( |
| 168 |
sd_bus_message *message, |
| 169 |
sd_bus_error *reterr_error, |
| 170 |
bool extended, |
| 171 |
struct in_addr_full ***ret_dns, |
| 172 |
size_t *ret_n_dns) { |
| 173 |
|
| 174 |
struct in_addr_full **dns = NULL; |
| 175 |
size_t n = 0; |
| 176 |
int r; |
| 177 |
|
| 178 |
assert(message); |
| 179 |
assert(ret_dns); |
| 180 |
assert(ret_n_dns); |
| 181 |
|
| 182 |
r = sd_bus_message_enter_container(message, 'a', extended ? "(iayqs)" : "(iay)"); |
| 183 |
if (r < 0) |
| 184 |
return r; |
| 185 |
|
| 186 |
for (;;) { |
| 187 |
const char *server_name; |
| 188 |
union in_addr_union a; |
| 189 |
uint16_t port; |
| 190 |
int family; |
| 191 |
|
| 192 |
r = bus_message_read_dns_one(message, reterr_error, extended, &family, &a, &port, &server_name); |
| 193 |
if (r < 0) |
| 194 |
goto clear; |
| 195 |
if (r == 0) |
| 196 |
break; |
| 197 |
|
| 198 |
if (!GREEDY_REALLOC(dns, n+1)) { |
| 199 |
r = -ENOMEM; |
| 200 |
goto clear; |
| 201 |
} |
| 202 |
|
| 203 |
r = in_addr_full_new(family, &a, port, 0, server_name, dns + n); |
| 204 |
if (r < 0) |
| 205 |
goto clear; |
| 206 |
|
| 207 |
n++; |
| 208 |
} |
| 209 |
|
| 210 |
*ret_dns = TAKE_PTR(dns); |
| 211 |
*ret_n_dns = n; |
| 212 |
return 0; |
| 213 |
|
| 214 |
clear: |
| 215 |
for (size_t i = 0; i < n; i++) |
| 216 |
in_addr_full_free(dns[i]); |
| 217 |
free(dns); |
| 218 |
|
| 219 |
return r; |
| 220 |
} |
| 221 |
|
| 222 |
int bus_message_append_string_set(sd_bus_message *m, const Set *set) { |
| 223 |
int r; |
| 224 |
|
| 225 |
assert(m); |
| 226 |
|
| 227 |
r = sd_bus_message_open_container(m, 'a', "s"); |
| 228 |
if (r < 0) |
| 229 |
return r; |
| 230 |
|
| 231 |
const char *s; |
| 232 |
SET_FOREACH(s, set) { |
| 233 |
r = sd_bus_message_append(m, "s", s); |
| 234 |
if (r < 0) |
| 235 |
return r; |
| 236 |
} |
| 237 |
|
| 238 |
return sd_bus_message_close_container(m); |
| 239 |
} |
| 240 |
|
| 241 |
int bus_message_dump_string(sd_bus_message *message) { |
| 242 |
const char *s; |
| 243 |
int r; |
| 244 |
|
| 245 |
assert(message); |
| 246 |
|
| 247 |
r = sd_bus_message_read(message, "s", &s); |
| 248 |
if (r < 0) |
| 249 |
return bus_log_parse_error(r); |
| 250 |
|
| 251 |
fputs(s, stdout); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
int bus_message_dump_fd(sd_bus_message *message) { |
| 256 |
int fd, r; |
| 257 |
|
| 258 |
assert(message); |
| 259 |
|
| 260 |
r = sd_bus_message_read(message, "h", &fd); |
| 261 |
if (r < 0) |
| 262 |
return bus_log_parse_error(r); |
| 263 |
|
| 264 |
fflush(stdout); |
| 265 |
r = copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0); |
| 266 |
if (r < 0) |
| 267 |
return log_error_errno(r, "Failed to dump contents in received file descriptor: %m"); |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(bus_message_hash_ops, |
| 273 |
void, trivial_hash_func, trivial_compare_func, |
| 274 |
sd_bus_message, sd_bus_message_unref); |