]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved.c
fix(journal-gatewayd): use relative urls (not starting with '/')
[thirdparty/systemd.git] / src / resolve / resolved.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6
7 #include "sd-daemon.h"
8 #include "sd-event.h"
9
10 #include "capability-util.h"
11 #include "daemon-util.h"
12 #include "main-func.h"
13 #include "mkdir.h"
14 #include "resolved-conf.h"
15 #include "resolved-manager.h"
16 #include "resolved-resolv-conf.h"
17 #include "selinux-util.h"
18 #include "signal-util.h"
19 #include "user-util.h"
20
21 static int run(int argc, char *argv[]) {
22 _cleanup_(notify_on_cleanup) const char *notify_stop = NULL;
23 _cleanup_(manager_freep) Manager *m = NULL;
24 const char *user = "systemd-resolve";
25 uid_t uid;
26 gid_t gid;
27 int r;
28
29 log_setup_service();
30
31 if (argc != 1)
32 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
33
34 umask(0022);
35
36 r = mac_selinux_init();
37 if (r < 0)
38 return log_error_errno(r, "SELinux setup failed: %m");
39
40 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
41 if (r < 0)
42 return log_error_errno(r, "Cannot resolve user name %s: %m", user);
43
44 /* Always create the directory where resolv.conf will live */
45 r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE);
46 if (r < 0)
47 return log_error_errno(r, "Could not create runtime directory: %m");
48
49 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume most
50 * privileges are already dropped. */
51 if (getuid() == 0) {
52
53 /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */
54 r = drop_privileges(uid, gid,
55 (UINT64_C(1) << CAP_NET_RAW)| /* needed for SO_BINDTODEVICE */
56 (UINT64_C(1) << CAP_NET_BIND_SERVICE)| /* needed to bind on port 53 */
57 (UINT64_C(1) << CAP_SETPCAP) /* needed in order to drop the caps later */);
58 if (r < 0)
59 return log_error_errno(r, "Failed to drop privileges: %m");
60 }
61
62 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0);
63
64 r = manager_new(&m);
65 if (r < 0)
66 return log_error_errno(r, "Could not create manager: %m");
67
68 r = manager_start(m);
69 if (r < 0)
70 return log_error_errno(r, "Failed to start manager: %m");
71
72 /* Write finish default resolv.conf to avoid a dangling symlink */
73 (void) manager_write_resolv_conf(m);
74
75 (void) manager_check_resolv_conf(m);
76
77 /* Let's drop the remaining caps now */
78 r = capability_bounding_set_drop(0, true);
79 if (r < 0)
80 return log_error_errno(r, "Failed to drop remaining caps: %m");
81
82 notify_stop = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
83
84 r = sd_event_loop(m->event);
85 if (r < 0)
86 return log_error_errno(r, "Event loop failed: %m");
87
88 return 0;
89 }
90
91 DEFINE_MAIN_FUNCTION(run);