| 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 "label-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "mkdir.h" |
| 16 |
#include "resolved-bus.h" |
| 17 |
#include "resolved-manager.h" |
| 18 |
#include "resolved-resolv-conf.h" |
| 19 |
#include "service-util.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_stop = NULL; |
| 25 |
int r; |
| 26 |
|
| 27 |
log_setup(); |
| 28 |
|
| 29 |
r = service_parse_argv("systemd-resolved.service", |
| 30 |
"Provide name resolution with caching using DNS, mDNS, LLMNR.", |
| 31 |
BUS_IMPLEMENTATIONS(&manager_object, |
| 32 |
&log_control_object), |
| 33 |
/* runtime_scope= */ NULL, |
| 34 |
argc, argv); |
| 35 |
if (r <= 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
umask(0022); |
| 39 |
|
| 40 |
r = mac_init(); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
/* Drop privileges, but only if we have been started as root. If we are not running as root we assume most |
| 45 |
* privileges are already dropped and we can't create our directory. */ |
| 46 |
if (getuid() == 0) { |
| 47 |
uid_t uid; |
| 48 |
gid_t gid; |
| 49 |
|
| 50 |
r = get_user_creds("systemd-resolve", /* flags= */ 0, NULL, &uid, &gid, NULL, NULL); |
| 51 |
if (r < 0) |
| 52 |
return log_error_errno(r, "Cannot resolve user name %s: %m", "systemd-resolve"); |
| 53 |
|
| 54 |
/* As we're root, we can create the directory where resolv.conf will live */ |
| 55 |
r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE); |
| 56 |
if (r < 0) |
| 57 |
return log_error_errno(r, "Could not create runtime directory: %m"); |
| 58 |
|
| 59 |
/* Drop privileges, but keep two caps. */ |
| 60 |
r = drop_privileges(uid, gid, |
| 61 |
(UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */ |
| 62 |
(UINT64_C(1) << CAP_NET_BIND_SERVICE)); /* needed to bind on port 53 */ |
| 63 |
if (r < 0) |
| 64 |
return log_error_errno(r, "Failed to drop privileges: %m"); |
| 65 |
} |
| 66 |
|
| 67 |
r = manager_new(&m); |
| 68 |
if (r < 0) |
| 69 |
return log_error_errno(r, "Could not create manager: %m"); |
| 70 |
|
| 71 |
r = manager_start(m); |
| 72 |
if (r < 0) |
| 73 |
return log_error_errno(r, "Failed to start manager: %m"); |
| 74 |
|
| 75 |
/* Write finish default resolv.conf to avoid a dangling symlink */ |
| 76 |
(void) manager_write_resolv_conf(m); |
| 77 |
|
| 78 |
(void) manager_check_resolv_conf(m); |
| 79 |
|
| 80 |
notify_stop = notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE); |
| 81 |
|
| 82 |
r = sd_event_loop(m->event); |
| 83 |
if (r < 0) |
| 84 |
return log_error_errno(r, "Event loop failed: %m"); |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
DEFINE_MAIN_FUNCTION(run); |