| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-daemon.h" |
| 6 |
#include "sd-device.h" |
| 7 |
#include "sd-netlink.h" |
| 8 |
#include "sd-network.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "conf-files.h" |
| 12 |
#include "edit-util.h" |
| 13 |
#include "errno-util.h" |
| 14 |
#include "extract-word.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "mkdir.h" |
| 18 |
#include "netlink-util.h" |
| 19 |
#include "network-util.h" |
| 20 |
#include "networkctl.h" |
| 21 |
#include "networkctl-config-file.h" |
| 22 |
#include "networkctl-util.h" |
| 23 |
#include "pager.h" |
| 24 |
#include "path-util.h" |
| 25 |
#include "pretty-print.h" |
| 26 |
#include "selinux-util.h" |
| 27 |
#include "socket-util.h" |
| 28 |
#include "stat-util.h" |
| 29 |
#include "string-table.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "strv.h" |
| 32 |
#include "terminal-util.h" |
| 33 |
#include "virt.h" |
| 34 |
|
| 35 |
typedef enum ReloadFlags { |
| 36 |
RELOAD_NETWORKD = 1 << 0, |
| 37 |
RELOAD_UDEVD = 1 << 1, |
| 38 |
} ReloadFlags; |
| 39 |
|
| 40 |
typedef enum LinkConfigType { |
| 41 |
CONFIG_NETWORK, |
| 42 |
CONFIG_LINK, |
| 43 |
CONFIG_NETDEV, |
| 44 |
_CONFIG_MAX, |
| 45 |
_CONFIG_INVALID = -EINVAL, |
| 46 |
} LinkConfigType; |
| 47 |
|
| 48 |
static const char* const link_config_type_table[_CONFIG_MAX] = { |
| 49 |
[CONFIG_NETWORK] = "network", |
| 50 |
[CONFIG_LINK] = "link", |
| 51 |
[CONFIG_NETDEV] = "netdev", |
| 52 |
}; |
| 53 |
|
| 54 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(link_config_type, LinkConfigType); |
| 55 |
|
| 56 |
static int get_config_files_by_name( |
| 57 |
const char *name, |
| 58 |
bool allow_masked, |
| 59 |
char **ret_path, |
| 60 |
char ***ret_dropins) { |
| 61 |
|
| 62 |
_cleanup_free_ char *path = NULL; |
| 63 |
int r; |
| 64 |
|
| 65 |
assert(filename_is_valid(name)); |
| 66 |
assert(ret_path); |
| 67 |
|
| 68 |
STRV_FOREACH(i, NETWORK_DIRS) { |
| 69 |
_cleanup_free_ char *p = NULL; |
| 70 |
|
| 71 |
p = path_join(*i, name); |
| 72 |
if (!p) |
| 73 |
return -ENOMEM; |
| 74 |
|
| 75 |
r = RET_NERRNO(access(p, F_OK)); |
| 76 |
if (r >= 0) { |
| 77 |
if (!allow_masked) { |
| 78 |
r = null_or_empty_path(p); |
| 79 |
if (r < 0) |
| 80 |
return log_debug_errno(r, |
| 81 |
"Failed to check if network config '%s' is masked: %m", |
| 82 |
name); |
| 83 |
if (r > 0) |
| 84 |
return -ERFKILL; |
| 85 |
} |
| 86 |
|
| 87 |
path = TAKE_PTR(p); |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
if (r != -ENOENT) |
| 92 |
log_debug_errno(r, "Failed to determine whether '%s' exists, ignoring: %m", p); |
| 93 |
} |
| 94 |
|
| 95 |
if (!path) |
| 96 |
return -ENOENT; |
| 97 |
|
| 98 |
if (ret_dropins) { |
| 99 |
_cleanup_free_ char *dropin_dirname = NULL; |
| 100 |
|
| 101 |
dropin_dirname = strjoin(name, ".d"); |
| 102 |
if (!dropin_dirname) |
| 103 |
return -ENOMEM; |
| 104 |
|
| 105 |
r = conf_files_list_dropins( |
| 106 |
ret_dropins, |
| 107 |
dropin_dirname, |
| 108 |
/* root= */ NULL, |
| 109 |
/* root_fd= */ XAT_FDROOT, |
| 110 |
CONF_FILES_WARN, |
| 111 |
NETWORK_DIRS); |
| 112 |
if (r < 0) |
| 113 |
return r; |
| 114 |
} |
| 115 |
|
| 116 |
*ret_path = TAKE_PTR(path); |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static int get_dropin_by_name( |
| 122 |
const char *name, |
| 123 |
char * const *dropins, |
| 124 |
char **ret) { |
| 125 |
|
| 126 |
assert(name); |
| 127 |
assert(ret); |
| 128 |
|
| 129 |
STRV_FOREACH(i, dropins) |
| 130 |
if (path_equal_filename(*i, name)) { |
| 131 |
_cleanup_free_ char *d = NULL; |
| 132 |
|
| 133 |
d = strdup(*i); |
| 134 |
if (!d) |
| 135 |
return -ENOMEM; |
| 136 |
|
| 137 |
*ret = TAKE_PTR(d); |
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
*ret = NULL; |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int get_network_files_by_link( |
| 146 |
const char *link, |
| 147 |
int ifindex, |
| 148 |
bool ignore_missing, |
| 149 |
char **ret_path, |
| 150 |
char ***ret_dropins) { |
| 151 |
|
| 152 |
_cleanup_strv_free_ char **dropins = NULL; |
| 153 |
_cleanup_free_ char *path = NULL; |
| 154 |
int r; |
| 155 |
|
| 156 |
assert(link); |
| 157 |
assert(ifindex > 0); |
| 158 |
assert(ret_path); |
| 159 |
assert(ret_dropins); |
| 160 |
|
| 161 |
r = sd_network_link_get_network_file(ifindex, &path); |
| 162 |
if (r == -ENODATA) |
| 163 |
return log_full_errno(ignore_missing ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(ENOENT), |
| 164 |
"Link '%s' has no associated network file.", link); |
| 165 |
if (r < 0) |
| 166 |
return log_error_errno(r, "Failed to get network file for link '%s': %m", link); |
| 167 |
|
| 168 |
r = sd_network_link_get_network_file_dropins(ifindex, &dropins); |
| 169 |
if (r < 0 && r != -ENODATA) |
| 170 |
return log_error_errno(r, "Failed to get network drop-ins for link '%s': %m", link); |
| 171 |
|
| 172 |
*ret_path = TAKE_PTR(path); |
| 173 |
*ret_dropins = TAKE_PTR(dropins); |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static int get_netdev_files_by_link( |
| 179 |
const char *link, |
| 180 |
int ifindex, |
| 181 |
bool ignore_missing, |
| 182 |
char **ret_path, |
| 183 |
char ***ret_dropins) { |
| 184 |
|
| 185 |
_cleanup_strv_free_ char **dropins = NULL; |
| 186 |
_cleanup_free_ char *path = NULL; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(link); |
| 190 |
assert(ifindex > 0); |
| 191 |
assert(ret_path); |
| 192 |
assert(ret_dropins); |
| 193 |
|
| 194 |
r = sd_network_link_get_netdev_file(ifindex, &path); |
| 195 |
if (r == -ENODATA) |
| 196 |
return log_full_errno(ignore_missing ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(ENOENT), |
| 197 |
"Link '%s' has no associated netdev file.", link); |
| 198 |
if (r < 0) |
| 199 |
return log_error_errno(r, "Failed to get netdev file for link '%s': %m", link); |
| 200 |
|
| 201 |
r = sd_network_link_get_netdev_file_dropins(ifindex, &dropins); |
| 202 |
if (r < 0 && r != -ENODATA) |
| 203 |
return log_error_errno(r, "Failed to get netdev drop-ins for link '%s': %m", link); |
| 204 |
|
| 205 |
*ret_path = TAKE_PTR(path); |
| 206 |
*ret_dropins = TAKE_PTR(dropins); |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
static int get_link_files_by_link(const char *link, bool ignore_missing, char **ret_path, char ***ret_dropins) { |
| 212 |
_cleanup_(sd_device_unrefp) sd_device *device = NULL; |
| 213 |
_cleanup_strv_free_ char **dropins_split = NULL; |
| 214 |
_cleanup_free_ char *p = NULL; |
| 215 |
const char *path, *dropins; |
| 216 |
int r; |
| 217 |
|
| 218 |
assert(link); |
| 219 |
assert(ret_path); |
| 220 |
assert(ret_dropins); |
| 221 |
|
| 222 |
r = sd_device_new_from_ifname(&device, link); |
| 223 |
if (r < 0) |
| 224 |
return log_error_errno(r, "Failed to create sd-device object for link '%s': %m", link); |
| 225 |
|
| 226 |
r = sd_device_get_property_value(device, "ID_NET_LINK_FILE", &path); |
| 227 |
if (r == -ENOENT) |
| 228 |
return log_full_errno(ignore_missing ? LOG_DEBUG : LOG_ERR, r, |
| 229 |
"Link '%s' has no associated link file.", link); |
| 230 |
if (r < 0) |
| 231 |
return log_error_errno(r, "Failed to get link file for link '%s': %m", link); |
| 232 |
|
| 233 |
r = sd_device_get_property_value(device, "ID_NET_LINK_FILE_DROPINS", &dropins); |
| 234 |
if (r < 0 && r != -ENOENT) |
| 235 |
return log_error_errno(r, "Failed to get link drop-ins for link '%s': %m", link); |
| 236 |
if (r >= 0) { |
| 237 |
r = strv_split_full(&dropins_split, dropins, ":", EXTRACT_CUNESCAPE); |
| 238 |
if (r < 0) |
| 239 |
return log_error_errno(r, "Failed to parse link drop-ins for link '%s': %m", link); |
| 240 |
} |
| 241 |
|
| 242 |
p = strdup(path); |
| 243 |
if (!p) |
| 244 |
return log_oom(); |
| 245 |
|
| 246 |
*ret_path = TAKE_PTR(p); |
| 247 |
*ret_dropins = TAKE_PTR(dropins_split); |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
static int get_config_files_by_link_config( |
| 253 |
const char *ifname, |
| 254 |
LinkConfigType type, |
| 255 |
bool ignore_missing, |
| 256 |
sd_netlink **rtnl, |
| 257 |
char **ret_path, |
| 258 |
char ***ret_dropins) { |
| 259 |
|
| 260 |
int r; |
| 261 |
|
| 262 |
assert(ifname); |
| 263 |
assert(type >= 0 && type < _CONFIG_MAX); |
| 264 |
assert(rtnl); |
| 265 |
assert(ret_path); |
| 266 |
assert(ret_dropins); |
| 267 |
|
| 268 |
if (type == CONFIG_LINK) |
| 269 |
return get_link_files_by_link(ifname, ignore_missing, ret_path, ret_dropins); |
| 270 |
|
| 271 |
if (!networkd_is_running()) |
| 272 |
return log_full_errno(ignore_missing ? LOG_DEBUG : LOG_ERR, SYNTHETIC_ERRNO(ESRCH), |
| 273 |
"Cannot get network/netdev file for link if systemd-networkd is not running."); |
| 274 |
|
| 275 |
int ifindex = rtnl_resolve_interface_or_warn(rtnl, ifname); |
| 276 |
if (ifindex < 0) |
| 277 |
return ifindex; |
| 278 |
|
| 279 |
if (type == CONFIG_NETWORK) |
| 280 |
r = get_network_files_by_link(ifname, ifindex, ignore_missing, ret_path, ret_dropins); |
| 281 |
else if (type == CONFIG_NETDEV) |
| 282 |
r = get_netdev_files_by_link(ifname, ifindex, ignore_missing, ret_path, ret_dropins); |
| 283 |
else |
| 284 |
assert_not_reached(); |
| 285 |
|
| 286 |
return r; |
| 287 |
} |
| 288 |
|
| 289 |
static int parse_link_config(const char *link_config, char **ret_ifname, LinkConfigType *ret_type) { |
| 290 |
const char *p = ASSERT_PTR(link_config); |
| 291 |
_cleanup_free_ char *ifname = NULL; |
| 292 |
int r; |
| 293 |
|
| 294 |
assert(ret_ifname); |
| 295 |
assert(ret_type); |
| 296 |
|
| 297 |
r = extract_first_word(&p, &ifname, ":", EXTRACT_DONT_COALESCE_SEPARATORS); |
| 298 |
if (r <= 0) |
| 299 |
return log_error_errno(r < 0 ? r : SYNTHETIC_ERRNO(EINVAL), |
| 300 |
"Failed to extract link name from '%s': %m", link_config); |
| 301 |
|
| 302 |
if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC)) |
| 303 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid link name: %s", ifname); |
| 304 |
|
| 305 |
LinkConfigType t; |
| 306 |
|
| 307 |
if (isempty(p)) |
| 308 |
t = CONFIG_NETWORK; |
| 309 |
else if (streq(p, "all")) |
| 310 |
t = _CONFIG_MAX; |
| 311 |
else { |
| 312 |
t = link_config_type_from_string(p); |
| 313 |
if (t < 0) |
| 314 |
return log_error_errno(t, "Invalid config type '%s' for link '%s'.", p, ifname); |
| 315 |
} |
| 316 |
|
| 317 |
*ret_ifname = TAKE_PTR(ifname); |
| 318 |
*ret_type = t; |
| 319 |
|
| 320 |
return 0; |
| 321 |
} |
| 322 |
|
| 323 |
static int add_config_to_edit( |
| 324 |
EditFileContext *context, |
| 325 |
const char *path, |
| 326 |
char * const *dropins) { |
| 327 |
|
| 328 |
_cleanup_free_ char *new_path = NULL, *dropin_path = NULL, *old_dropin = NULL; |
| 329 |
_cleanup_strv_free_ char **comment_paths = NULL; |
| 330 |
int r; |
| 331 |
|
| 332 |
assert(context); |
| 333 |
assert(path); |
| 334 |
|
| 335 |
/* If we're supposed to edit main config file in /run/, but a config with the same name is present |
| 336 |
* under /etc/, we bail out since the one in /etc/ always overrides that in /run/. */ |
| 337 |
if (arg_runtime && !arg_drop_in && path_startswith(path, "/etc")) |
| 338 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 339 |
"Cannot edit runtime config file: overridden by %s", path); |
| 340 |
|
| 341 |
if (path_startswith(path, "/usr") || arg_runtime != !!path_startswith(path, "/run")) { |
| 342 |
_cleanup_free_ char *name = NULL; |
| 343 |
|
| 344 |
r = path_extract_filename(path, &name); |
| 345 |
if (r < 0) |
| 346 |
return log_error_errno(r, "Failed to extract filename from '%s': %m", path); |
| 347 |
|
| 348 |
new_path = path_join(NETWORK_DIRS[arg_runtime ? 1 : 0], name); |
| 349 |
if (!new_path) |
| 350 |
return log_oom(); |
| 351 |
} |
| 352 |
|
| 353 |
if (!arg_drop_in) |
| 354 |
return edit_files_add(context, new_path ?: path, path, NULL); |
| 355 |
|
| 356 |
bool need_new_dropin; |
| 357 |
|
| 358 |
r = get_dropin_by_name(arg_drop_in, dropins, &old_dropin); |
| 359 |
if (r < 0) |
| 360 |
return log_error_errno(r, "Failed to acquire drop-in '%s': %m", arg_drop_in); |
| 361 |
if (r > 0) { |
| 362 |
/* See the explanation above */ |
| 363 |
if (arg_runtime && path_startswith(old_dropin, "/etc")) |
| 364 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 365 |
"Cannot edit runtime config file: overridden by %s", old_dropin); |
| 366 |
|
| 367 |
need_new_dropin = path_startswith(old_dropin, "/usr") || arg_runtime != !!path_startswith(old_dropin, "/run"); |
| 368 |
} else |
| 369 |
need_new_dropin = true; |
| 370 |
|
| 371 |
if (!need_new_dropin) |
| 372 |
/* An existing drop-in is found in the correct scope. Let's edit it directly. */ |
| 373 |
dropin_path = TAKE_PTR(old_dropin); |
| 374 |
else { |
| 375 |
/* No drop-in was found or an existing drop-in is in a different scope. Let's create a new |
| 376 |
* drop-in file. */ |
| 377 |
dropin_path = strjoin(new_path ?: path, ".d/", arg_drop_in); |
| 378 |
if (!dropin_path) |
| 379 |
return log_oom(); |
| 380 |
} |
| 381 |
|
| 382 |
comment_paths = strv_new(path); |
| 383 |
if (!comment_paths) |
| 384 |
return log_oom(); |
| 385 |
|
| 386 |
r = strv_extend_strv(&comment_paths, dropins, /* filter_duplicates= */ false); |
| 387 |
if (r < 0) |
| 388 |
return log_oom(); |
| 389 |
|
| 390 |
return edit_files_add(context, dropin_path, old_dropin, comment_paths); |
| 391 |
} |
| 392 |
|
| 393 |
static int reload_daemons(ReloadFlags flags) { |
| 394 |
int ret = 1; |
| 395 |
|
| 396 |
if (arg_no_reload) |
| 397 |
return 0; |
| 398 |
|
| 399 |
if (flags == 0) |
| 400 |
return 0; |
| 401 |
|
| 402 |
if (!sd_booted() || running_in_chroot() > 0) { |
| 403 |
log_debug("System is not booted with systemd or is running in chroot, skipping reload."); |
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
if (FLAGS_SET(flags, RELOAD_UDEVD)) |
| 408 |
RET_GATHER(ret, reload_udevd()); |
| 409 |
|
| 410 |
if (FLAGS_SET(flags, RELOAD_NETWORKD)) { |
| 411 |
if (!networkd_is_running()) |
| 412 |
log_debug("systemd-networkd is not running, skipping reload."); |
| 413 |
else |
| 414 |
RET_GATHER(ret, reload_networkd()); |
| 415 |
} |
| 416 |
|
| 417 |
return ret; |
| 418 |
} |
| 419 |
|
| 420 |
int verb_edit(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 421 |
char **args = ASSERT_PTR(strv_skip(argv, 1)); |
| 422 |
_cleanup_(edit_file_context_done) EditFileContext context = { |
| 423 |
.marker_start = DROPIN_MARKER_START, |
| 424 |
.marker_end = DROPIN_MARKER_END, |
| 425 |
.remove_parent = !!arg_drop_in, |
| 426 |
.read_from_stdin = arg_stdin, |
| 427 |
}; |
| 428 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 429 |
ReloadFlags reload = 0; |
| 430 |
int r; |
| 431 |
|
| 432 |
if (!on_tty() && !arg_stdin) |
| 433 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot edit network config files interactively if not on a tty."); |
| 434 |
|
| 435 |
/* Duplicating main configs makes no sense. This also mimics the behavior of systemctl. */ |
| 436 |
if (arg_stdin && !arg_drop_in && strv_length(args) != 1) |
| 437 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 438 |
"When 'edit --stdin' without '--drop-in=', exactly one config file for editing must be specified."); |
| 439 |
|
| 440 |
r = mac_selinux_init(); |
| 441 |
if (r < 0) |
| 442 |
return r; |
| 443 |
|
| 444 |
STRV_FOREACH(name, args) { |
| 445 |
_cleanup_strv_free_ char **dropins = NULL; |
| 446 |
_cleanup_free_ char *path = NULL; |
| 447 |
const char *link_config; |
| 448 |
|
| 449 |
link_config = startswith(*name, "@"); |
| 450 |
if (link_config) { |
| 451 |
_cleanup_free_ char *ifname = NULL; |
| 452 |
LinkConfigType type; |
| 453 |
|
| 454 |
r = parse_link_config(link_config, &ifname, &type); |
| 455 |
if (r < 0) |
| 456 |
return r; |
| 457 |
if (type == _CONFIG_MAX) |
| 458 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 459 |
"Config type 'all' cannot be used with 'edit'."); |
| 460 |
|
| 461 |
r = get_config_files_by_link_config(ifname, type, |
| 462 |
/* ignore_missing= */ false, |
| 463 |
&rtnl, |
| 464 |
&path, &dropins); |
| 465 |
if (r < 0) |
| 466 |
return r; |
| 467 |
|
| 468 |
r = add_config_to_edit(&context, path, dropins); |
| 469 |
if (r < 0) |
| 470 |
return r; |
| 471 |
|
| 472 |
reload |= type == CONFIG_LINK ? RELOAD_UDEVD : RELOAD_NETWORKD; |
| 473 |
|
| 474 |
continue; |
| 475 |
} |
| 476 |
|
| 477 |
if (!filename_is_valid(*name)) |
| 478 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid config filename: %s", *name); |
| 479 |
|
| 480 |
if (ENDSWITH_SET(*name, ".network", ".netdev")) |
| 481 |
reload |= RELOAD_NETWORKD; |
| 482 |
else if (endswith(*name, ".link")) |
| 483 |
reload |= RELOAD_UDEVD; |
| 484 |
else |
| 485 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Network config of unknown type: %s", *name); |
| 486 |
|
| 487 |
r = get_config_files_by_name(*name, /* allow_masked= */ false, &path, &dropins); |
| 488 |
if (r == -ERFKILL) |
| 489 |
return log_error_errno(r, "Network config '%s' is masked.", *name); |
| 490 |
if (r == -ENOENT) { |
| 491 |
if (arg_drop_in) |
| 492 |
return log_error_errno(r, "Cannot find network config '%s'.", *name); |
| 493 |
|
| 494 |
log_debug("No existing network config '%s' found, creating a new file.", *name); |
| 495 |
|
| 496 |
path = path_join(NETWORK_DIRS[arg_runtime ? 1 : 0], *name); |
| 497 |
if (!path) |
| 498 |
return log_oom(); |
| 499 |
|
| 500 |
r = edit_files_add(&context, path, NULL, NULL); |
| 501 |
if (r < 0) |
| 502 |
return r; |
| 503 |
continue; |
| 504 |
} |
| 505 |
if (r < 0) |
| 506 |
return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name); |
| 507 |
|
| 508 |
r = add_config_to_edit(&context, path, dropins); |
| 509 |
if (r < 0) |
| 510 |
return r; |
| 511 |
} |
| 512 |
|
| 513 |
r = do_edit_files_and_install(&context); |
| 514 |
if (r < 0) |
| 515 |
return r; |
| 516 |
|
| 517 |
return reload_daemons(reload); |
| 518 |
} |
| 519 |
|
| 520 |
static int cat_files_by_link_one( |
| 521 |
const char *ifname, |
| 522 |
LinkConfigType type, |
| 523 |
sd_netlink **rtnl, |
| 524 |
bool ignore_missing, |
| 525 |
bool *first) { |
| 526 |
|
| 527 |
_cleanup_strv_free_ char **dropins = NULL; |
| 528 |
_cleanup_free_ char *path = NULL; |
| 529 |
int r; |
| 530 |
|
| 531 |
assert(ifname); |
| 532 |
assert(type >= 0 && type < _CONFIG_MAX); |
| 533 |
assert(rtnl); |
| 534 |
assert(first); |
| 535 |
|
| 536 |
r = get_config_files_by_link_config(ifname, type, ignore_missing, rtnl, &path, &dropins); |
| 537 |
if (ignore_missing && IN_SET(r, -ENOENT, -ESRCH)) |
| 538 |
return 0; |
| 539 |
if (r < 0) |
| 540 |
return r; |
| 541 |
|
| 542 |
if (!*first) |
| 543 |
putchar('\n'); |
| 544 |
|
| 545 |
r = cat_files(path, dropins, /* flags= */ CAT_FORMAT_HAS_SECTIONS); |
| 546 |
if (r < 0) |
| 547 |
return r; |
| 548 |
|
| 549 |
*first = false; |
| 550 |
|
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
static int cat_files_by_link_config(const char *link_config, sd_netlink **rtnl, bool *first) { |
| 555 |
_cleanup_free_ char *ifname = NULL; |
| 556 |
LinkConfigType type; |
| 557 |
int r; |
| 558 |
|
| 559 |
assert(link_config); |
| 560 |
assert(rtnl); |
| 561 |
assert(first); |
| 562 |
|
| 563 |
r = parse_link_config(link_config, &ifname, &type); |
| 564 |
if (r < 0) |
| 565 |
return r; |
| 566 |
|
| 567 |
if (type == _CONFIG_MAX) { |
| 568 |
for (LinkConfigType i = 0; i < _CONFIG_MAX; i++) { |
| 569 |
r = cat_files_by_link_one(ifname, i, rtnl, /* ignore_missing= */ true, first); |
| 570 |
if (r < 0) |
| 571 |
return r; |
| 572 |
} |
| 573 |
|
| 574 |
return 0; |
| 575 |
} |
| 576 |
|
| 577 |
return cat_files_by_link_one(ifname, type, rtnl, /* ignore_missing= */ false, first); |
| 578 |
} |
| 579 |
|
| 580 |
int verb_cat(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 581 |
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 582 |
char **args = strv_skip(argv, 1); |
| 583 |
int r, ret = 0; |
| 584 |
|
| 585 |
pager_open(arg_pager_flags); |
| 586 |
|
| 587 |
if (strv_isempty(args)) |
| 588 |
return conf_files_cat(NULL, "systemd/networkd.conf", CAT_FORMAT_HAS_SECTIONS); |
| 589 |
|
| 590 |
bool first = true; |
| 591 |
STRV_FOREACH(name, args) { |
| 592 |
const char *link_config; |
| 593 |
|
| 594 |
link_config = startswith(*name, "@"); |
| 595 |
if (link_config) { |
| 596 |
r = cat_files_by_link_config(link_config, &rtnl, &first); |
| 597 |
if (r < 0) |
| 598 |
return r; |
| 599 |
continue; |
| 600 |
} |
| 601 |
|
| 602 |
if (!filename_is_valid(*name) || !ENDSWITH_SET(*name, ".network", ".netdev", ".link")) |
| 603 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid network config name: %s", *name); |
| 604 |
|
| 605 |
_cleanup_strv_free_ char **dropins = NULL; |
| 606 |
_cleanup_free_ char *path = NULL; |
| 607 |
|
| 608 |
r = get_config_files_by_name(*name, /* allow_masked= */ false, &path, &dropins); |
| 609 |
if (r == -ENOENT) { |
| 610 |
RET_GATHER(ret, log_error_errno(r, "Cannot find network config file '%s'.", *name)); |
| 611 |
continue; |
| 612 |
} |
| 613 |
if (r == -ERFKILL) { |
| 614 |
RET_GATHER(ret, log_debug_errno(r, "Network config '%s' is masked, ignoring.", *name)); |
| 615 |
continue; |
| 616 |
} |
| 617 |
if (r < 0) |
| 618 |
return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name); |
| 619 |
|
| 620 |
if (!first) |
| 621 |
putchar('\n'); |
| 622 |
|
| 623 |
r = cat_files(path, dropins, /* flags= */ CAT_FORMAT_HAS_SECTIONS); |
| 624 |
if (r < 0) |
| 625 |
return r; |
| 626 |
|
| 627 |
first = false; |
| 628 |
} |
| 629 |
|
| 630 |
return ret; |
| 631 |
} |
| 632 |
|
| 633 |
int verb_mask(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 634 |
ReloadFlags flags = 0; |
| 635 |
int r; |
| 636 |
|
| 637 |
r = mac_selinux_init(); |
| 638 |
if (r < 0) |
| 639 |
return r; |
| 640 |
|
| 641 |
STRV_FOREACH(name, strv_skip(argv, 1)) { |
| 642 |
_cleanup_free_ char *config_path = NULL, *symlink_path = NULL; |
| 643 |
ReloadFlags reload; |
| 644 |
|
| 645 |
if (!filename_is_valid(*name)) |
| 646 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid config filename: %s", *name); |
| 647 |
|
| 648 |
/* We update the real 'flags' at last, since the operation can be skipped. */ |
| 649 |
if (ENDSWITH_SET(*name, ".network", ".netdev")) |
| 650 |
reload = RELOAD_NETWORKD; |
| 651 |
else if (endswith(*name, ".link")) |
| 652 |
reload = RELOAD_UDEVD; |
| 653 |
else |
| 654 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Network config of unknown type: %s", *name); |
| 655 |
|
| 656 |
r = get_config_files_by_name(*name, /* allow_masked= */ true, &config_path, /* ret_dropins= */ NULL); |
| 657 |
if (r == -ENOENT) |
| 658 |
log_warning("No existing network config '%s' found, proceeding anyway.", *name); |
| 659 |
else if (r < 0) |
| 660 |
return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name); |
| 661 |
else if (!path_startswith(config_path, "/usr")) { |
| 662 |
r = null_or_empty_path(config_path); |
| 663 |
if (r < 0) |
| 664 |
return log_error_errno(r, |
| 665 |
"Failed to check if '%s' is masked: %m", config_path); |
| 666 |
if (r > 0) { |
| 667 |
log_debug("%s is already masked, skipping.", config_path); |
| 668 |
continue; |
| 669 |
} |
| 670 |
|
| 671 |
/* At this point, we have found a config under mutable dir (/run/ or /etc/), |
| 672 |
* so masking through /run/ (--runtime) is not possible. If it's under /etc/, |
| 673 |
* then it doesn't work without --runtime either. */ |
| 674 |
if (arg_runtime || path_startswith(config_path, "/etc")) |
| 675 |
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), |
| 676 |
"Cannot mask network config %s: %s exists", |
| 677 |
*name, config_path); |
| 678 |
} |
| 679 |
|
| 680 |
symlink_path = path_join(NETWORK_DIRS[arg_runtime ? 1 : 0], *name); |
| 681 |
if (!symlink_path) |
| 682 |
return log_oom(); |
| 683 |
|
| 684 |
(void) mkdir_parents_label(symlink_path, 0755); |
| 685 |
|
| 686 |
if (symlink("/dev/null", symlink_path) < 0) |
| 687 |
return log_error_errno(errno, |
| 688 |
"Failed to create symlink '%s' to /dev/null: %m", symlink_path); |
| 689 |
|
| 690 |
flags |= reload; |
| 691 |
log_info("Successfully created symlink '%s' to /dev/null.", symlink_path); |
| 692 |
} |
| 693 |
|
| 694 |
return reload_daemons(flags); |
| 695 |
} |
| 696 |
|
| 697 |
int verb_unmask(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 698 |
ReloadFlags flags = 0; |
| 699 |
int r; |
| 700 |
|
| 701 |
STRV_FOREACH(name, strv_skip(argv, 1)) { |
| 702 |
_cleanup_free_ char *path = NULL; |
| 703 |
ReloadFlags reload; |
| 704 |
|
| 705 |
if (!filename_is_valid(*name)) |
| 706 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid config filename: %s", *name); |
| 707 |
|
| 708 |
if (ENDSWITH_SET(*name, ".network", ".netdev")) |
| 709 |
reload = RELOAD_NETWORKD; |
| 710 |
else if (endswith(*name, ".link")) |
| 711 |
reload = RELOAD_UDEVD; |
| 712 |
else |
| 713 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Network config of unknown type: %s", *name); |
| 714 |
|
| 715 |
r = get_config_files_by_name(*name, /* allow_masked= */ true, &path, /* ret_dropins= */ NULL); |
| 716 |
if (r == -ENOENT) { |
| 717 |
log_debug_errno(r, "Network configuration '%s' doesn't exist, skipping.", *name); |
| 718 |
continue; |
| 719 |
} |
| 720 |
if (r < 0) |
| 721 |
return log_error_errno(r, "Failed to get the path of network config '%s': %m", *name); |
| 722 |
|
| 723 |
r = null_or_empty_path(path); |
| 724 |
if (r < 0) |
| 725 |
return log_error_errno(r, "Failed to check if '%s' is masked: %m", path); |
| 726 |
if (r == 0) |
| 727 |
continue; |
| 728 |
|
| 729 |
if (path_startswith(path, "/usr")) |
| 730 |
return log_error_errno(r, "Cannot unmask network config under /usr/: %s", path); |
| 731 |
|
| 732 |
if (unlink(path) < 0) { |
| 733 |
if (errno == ENOENT) |
| 734 |
continue; |
| 735 |
|
| 736 |
return log_error_errno(errno, "Failed to remove '%s': %m", path); |
| 737 |
} |
| 738 |
|
| 739 |
flags |= reload; |
| 740 |
log_info("Successfully removed masked network config '%s'.", path); |
| 741 |
} |
| 742 |
|
| 743 |
return reload_daemons(flags); |
| 744 |
} |