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