]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
091a364c TG |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2014 Tom Gundersen <teg@jklm.no> | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
091a364c | 21 | #include "sd-daemon.h" |
b1d4f8e1 LP |
22 | #include "sd-event.h" |
23 | ||
430f0182 | 24 | #include "capability-util.h" |
b1d4f8e1 LP |
25 | #include "mkdir.h" |
26 | #include "resolved-conf.h" | |
27 | #include "resolved-manager.h" | |
f8dc7e34 | 28 | #include "resolved-resolv-conf.h" |
d7b8eec7 | 29 | #include "selinux-util.h" |
24882e06 | 30 | #include "signal-util.h" |
b1d4f8e1 | 31 | #include "user-util.h" |
4e945a6f | 32 | |
091a364c | 33 | int main(int argc, char *argv[]) { |
74b2466e | 34 | _cleanup_(manager_freep) Manager *m = NULL; |
682265d5 TG |
35 | const char *user = "systemd-resolve"; |
36 | uid_t uid; | |
37 | gid_t gid; | |
091a364c TG |
38 | int r; |
39 | ||
40 | log_set_target(LOG_TARGET_AUTO); | |
41 | log_parse_environment(); | |
42 | log_open(); | |
43 | ||
091a364c TG |
44 | if (argc != 1) { |
45 | log_error("This program takes no arguments."); | |
46 | r = -EINVAL; | |
74b2466e | 47 | goto finish; |
091a364c TG |
48 | } |
49 | ||
a5a807e6 ZJS |
50 | umask(0022); |
51 | ||
c3dacc8b | 52 | r = mac_selinux_init(); |
a5a807e6 | 53 | if (r < 0) { |
da927ba9 | 54 | log_error_errno(r, "SELinux setup failed: %m"); |
a5a807e6 ZJS |
55 | goto finish; |
56 | } | |
57 | ||
682265d5 TG |
58 | r = get_user_creds(&user, &uid, &gid, NULL, NULL); |
59 | if (r < 0) { | |
da927ba9 | 60 | log_error_errno(r, "Cannot resolve user name %s: %m", user); |
74b2466e | 61 | goto finish; |
682265d5 TG |
62 | } |
63 | ||
091a364c | 64 | /* Always create the directory where resolv.conf will live */ |
37c1d5e9 | 65 | r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE); |
682265d5 | 66 | if (r < 0) { |
da927ba9 | 67 | log_error_errno(r, "Could not create runtime directory: %m"); |
74b2466e | 68 | goto finish; |
682265d5 TG |
69 | } |
70 | ||
635f3df5 LP |
71 | /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all |
72 | * privileges are already dropped. */ | |
73 | if (getuid() == 0) { | |
74 | ||
75 | /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */ | |
76 | r = drop_privileges(uid, gid, | |
77 | (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */ | |
78 | (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */ | |
79 | (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */); | |
80 | if (r < 0) | |
81 | goto finish; | |
82 | } | |
091a364c | 83 | |
d55b0463 | 84 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0); |
b9e7a9d8 | 85 | |
091a364c TG |
86 | r = manager_new(&m); |
87 | if (r < 0) { | |
da927ba9 | 88 | log_error_errno(r, "Could not create manager: %m"); |
74b2466e | 89 | goto finish; |
091a364c TG |
90 | } |
91 | ||
edc501d4 LP |
92 | r = manager_start(m); |
93 | if (r < 0) { | |
da927ba9 | 94 | log_error_errno(r, "Failed to start manager: %m"); |
edc501d4 LP |
95 | goto finish; |
96 | } | |
97 | ||
7207052d LP |
98 | /* Write finish default resolv.conf to avoid a dangling symlink */ |
99 | (void) manager_write_resolv_conf(m); | |
091a364c | 100 | |
b30bf55d LP |
101 | /* Let's drop the remaining caps now */ |
102 | r = capability_bounding_set_drop(0, true); | |
103 | if (r < 0) { | |
104 | log_error_errno(r, "Failed to drop remaining caps: %m"); | |
105 | goto finish; | |
106 | } | |
107 | ||
091a364c TG |
108 | sd_notify(false, |
109 | "READY=1\n" | |
110 | "STATUS=Processing requests..."); | |
111 | ||
112 | r = sd_event_loop(m->event); | |
113 | if (r < 0) { | |
da927ba9 | 114 | log_error_errno(r, "Event loop failed: %m"); |
74b2466e | 115 | goto finish; |
091a364c TG |
116 | } |
117 | ||
96e6e394 LP |
118 | sd_event_get_exit_code(m->event, &r); |
119 | ||
74b2466e | 120 | finish: |
af4ec430 | 121 | sd_notify(false, |
b37d45c9 | 122 | "STOPPING=1\n" |
af4ec430 | 123 | "STATUS=Shutting down..."); |
091a364c TG |
124 | |
125 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
126 | } |