| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "bus-log-control-api.h" |
| 9 |
#include "bus-object.h" |
| 10 |
#include "capability-util.h" |
| 11 |
#include "daemon-util.h" |
| 12 |
#include "main-func.h" |
| 13 |
#include "mkdir.h" |
| 14 |
#include "networkd-conf.h" |
| 15 |
#include "networkd-manager.h" |
| 16 |
#include "networkd-manager-bus.h" |
| 17 |
#include "networkd-serialize.h" |
| 18 |
#include "service-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "user-util.h" |
| 21 |
|
| 22 |
static int run(int argc, char *argv[]) { |
| 23 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 24 |
_unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL; |
| 25 |
int r; |
| 26 |
|
| 27 |
log_setup(); |
| 28 |
|
| 29 |
r = service_parse_argv("systemd-networkd.service", |
| 30 |
"Manage and configure network devices, create virtual network devices", |
| 31 |
BUS_IMPLEMENTATIONS(&manager_object, &log_control_object), |
| 32 |
/* runtime_scope= */ NULL, |
| 33 |
argc, argv); |
| 34 |
if (r <= 0) |
| 35 |
return r; |
| 36 |
|
| 37 |
umask(0022); |
| 38 |
|
| 39 |
if (argc != 1) |
| 40 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments."); |
| 41 |
|
| 42 |
/* Drop privileges, but only if we have been started as root. If we are not running as root we assume all |
| 43 |
* privileges are already dropped and we can't create our runtime directory. */ |
| 44 |
if (geteuid() == 0) { |
| 45 |
uid_t uid; |
| 46 |
gid_t gid; |
| 47 |
|
| 48 |
r = get_user_creds("systemd-network", /* flags= */ 0, NULL, &uid, &gid, NULL, NULL); |
| 49 |
if (r < 0) |
| 50 |
return log_error_errno(r, "Cannot resolve user name %s: %m", "systemd-network"); |
| 51 |
|
| 52 |
/* Create runtime directory. This is not necessary when networkd is |
| 53 |
* started with "RuntimeDirectory=systemd/netif", or after |
| 54 |
* systemd-tmpfiles-setup.service. */ |
| 55 |
r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid, MKDIR_WARN_MODE); |
| 56 |
if (r < 0) |
| 57 |
log_warning_errno(r, "Could not create runtime directory: %m"); |
| 58 |
|
| 59 |
r = drop_privileges(uid, gid, |
| 60 |
(1ULL << CAP_NET_ADMIN) | |
| 61 |
(1ULL << CAP_NET_BIND_SERVICE) | |
| 62 |
(1ULL << CAP_NET_BROADCAST) | |
| 63 |
(1ULL << CAP_NET_RAW) | |
| 64 |
(1ULL << CAP_SYS_ADMIN) | |
| 65 |
(1ULL << CAP_BPF)); |
| 66 |
if (r < 0) |
| 67 |
return log_error_errno(r, "Failed to drop privileges: %m"); |
| 68 |
} |
| 69 |
|
| 70 |
/* Always create the directories people can create inotify watches in. It is necessary to create the |
| 71 |
* following subdirectories after drop_privileges() to make them owned by systemd-network. */ |
| 72 |
FOREACH_STRING(p, |
| 73 |
"/run/systemd/netif/dhcp-server-lease/", |
| 74 |
"/run/systemd/netif/leases/", |
| 75 |
"/run/systemd/netif/links/") { |
| 76 |
r = mkdir_safe_label(p, 0755, UID_INVALID, GID_INVALID, MKDIR_WARN_MODE); |
| 77 |
if (r < 0) |
| 78 |
log_warning_errno(r, "Could not create directory '%s': %m", p); |
| 79 |
} |
| 80 |
|
| 81 |
r = manager_new(&m, /* test_mode= */ false); |
| 82 |
if (r < 0) |
| 83 |
return log_error_errno(r, "Could not create manager: %m"); |
| 84 |
|
| 85 |
r = manager_setup(m); |
| 86 |
if (r < 0) |
| 87 |
return log_error_errno(r, "Could not set up manager: %m"); |
| 88 |
|
| 89 |
r = manager_parse_config_file(m); |
| 90 |
if (r < 0) |
| 91 |
log_warning_errno(r, "Failed to parse configuration file: %m"); |
| 92 |
|
| 93 |
r = manager_load_config(m); |
| 94 |
if (r < 0) |
| 95 |
return log_error_errno(r, "Could not load configuration files: %m"); |
| 96 |
|
| 97 |
r = manager_enumerate(m); |
| 98 |
if (r < 0) |
| 99 |
return r; |
| 100 |
|
| 101 |
r = manager_deserialize(m); |
| 102 |
if (r < 0) |
| 103 |
log_warning_errno(r, "Failed to deserialize the previous invocation, ignoring: %m"); |
| 104 |
|
| 105 |
r = manager_start(m); |
| 106 |
if (r < 0) |
| 107 |
return log_error_errno(r, "Could not start manager: %m"); |
| 108 |
|
| 109 |
notify_message = notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE); |
| 110 |
|
| 111 |
r = sd_event_loop(m->event); |
| 112 |
if (r < 0) |
| 113 |
return log_error_errno(r, "Event loop failed: %m"); |
| 114 |
|
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
DEFINE_MAIN_FUNCTION(run); |