]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 "mkdir.h"
8 #include "resolved-conf.h"
9 #include "resolved-manager.h"
10 #include "resolved-resolv-conf.h"
11 #include "selinux-util.h"
12 #include "signal-util.h"
13 #include "user-util.h"
14
15 int main(int argc, char *argv[]) {
16 _cleanup_(manager_freep) Manager *m = NULL;
17 const char *user = "systemd-resolve";
18 uid_t uid;
19 gid_t gid;
20 int r;
21
22 log_set_target(LOG_TARGET_AUTO);
23 log_parse_environment();
24 log_open();
25
26 if (argc != 1) {
27 log_error("This program takes no arguments.");
28 r = -EINVAL;
29 goto finish;
30 }
31
32 umask(0022);
33
34 r = mac_selinux_init();
35 if (r < 0) {
36 log_error_errno(r, "SELinux setup failed: %m");
37 goto finish;
38 }
39
40 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
41 if (r < 0) {
42 log_error_errno(r, "Cannot resolve user name %s: %m", user);
43 goto finish;
44 }
45
46 /* Always create the directory where resolv.conf will live */
47 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE);
48 if (r < 0) {
49 log_error_errno(r, "Could not create runtime directory: %m");
50 goto finish;
51 }
52
53 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume most
54 * privileges are already dropped. */
55 if (getuid() == 0) {
56
57 /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */
58 r = drop_privileges(uid, gid,
59 (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */
60 (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */
61 (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */);
62 if (r < 0)
63 goto finish;
64 }
65
66 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
67
68 r = manager_new(&m);
69 if (r < 0) {
70 log_error_errno(r, "Could not create manager: %m");
71 goto finish;
72 }
73
74 r = manager_start(m);
75 if (r < 0) {
76 log_error_errno(r, "Failed to start manager: %m");
77 goto finish;
78 }
79
80 /* Write finish default resolv.conf to avoid a dangling symlink */
81 (void) manager_write_resolv_conf(m);
82
83 (void) manager_check_resolv_conf(m);
84
85 /* Let's drop the remaining caps now */
86 r = capability_bounding_set_drop(0, true);
87 if (r < 0) {
88 log_error_errno(r, "Failed to drop remaining caps: %m");
89 goto finish;
90 }
91
92 sd_notify(false,
93 "READY=1\n"
94 "STATUS=Processing requests...");
95
96 r = sd_event_loop(m->event);
97 if (r < 0) {
98 log_error_errno(r, "Event loop failed: %m");
99 goto finish;
100 }
101
102 sd_event_get_exit_code(m->event, &r);
103
104 finish:
105 sd_notify(false,
106 "STOPPING=1\n"
107 "STATUS=Shutting down...");
108
109 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
110 }