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