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