]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / resolve / resolved.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-daemon.h"
4 #include "sd-event.h"
5
6 #include "capability-util.h"
7 #include "daemon-util.h"
8 #include "main-func.h"
9 #include "mkdir.h"
10 #include "resolved-conf.h"
11 #include "resolved-manager.h"
12 #include "resolved-resolv-conf.h"
13 #include "selinux-util.h"
14 #include "signal-util.h"
15 #include "user-util.h"
16
17 static int run(int argc, char *argv[]) {
18 _cleanup_(notify_on_cleanup) const char *notify_stop = NULL;
19 _cleanup_(manager_freep) Manager *m = NULL;
20 const char *user = "systemd-resolve";
21 uid_t uid;
22 gid_t gid;
23 int r;
24
25 log_setup_service();
26
27 if (argc != 1)
28 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
29
30 umask(0022);
31
32 r = mac_selinux_init();
33 if (r < 0)
34 return log_error_errno(r, "SELinux setup failed: %m");
35
36 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
37 if (r < 0)
38 return log_error_errno(r, "Cannot resolve user name %s: %m", user);
39
40 /* Always create the directory where resolv.conf will live */
41 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE);
42 if (r < 0)
43 return log_error_errno(r, "Could not create runtime directory: %m");
44
45 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume most
46 * privileges are already dropped. */
47 if (getuid() == 0) {
48
49 /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */
50 r = drop_privileges(uid, gid,
51 (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */
52 (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */
53 (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */);
54 if (r < 0)
55 return log_error_errno(r, "Failed to drop privileges: %m");
56 }
57
58 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
59
60 r = manager_new(&m);
61 if (r < 0)
62 return log_error_errno(r, "Could not create manager: %m");
63
64 r = manager_start(m);
65 if (r < 0)
66 return log_error_errno(r, "Failed to start manager: %m");
67
68 /* Write finish default resolv.conf to avoid a dangling symlink */
69 (void) manager_write_resolv_conf(m);
70
71 (void) manager_check_resolv_conf(m);
72
73 /* Let's drop the remaining caps now */
74 r = capability_bounding_set_drop(0, true);
75 if (r < 0)
76 return log_error_errno(r, "Failed to drop remaining caps: %m");
77
78 notify_stop = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
79
80 r = sd_event_loop(m->event);
81 if (r < 0)
82 return log_error_errno(r, "Event loop failed: %m");
83
84 return 0;
85 }
86
87 DEFINE_MAIN_FUNCTION(run);