Branch data Line data Source code
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 : 0 : static int run(int argc, char *argv[]) { 22 : 0 : _cleanup_(notify_on_cleanup) const char *notify_stop = NULL; 23 : 0 : _cleanup_(manager_freep) Manager *m = NULL; 24 : 0 : const char *user = "systemd-resolve"; 25 : : uid_t uid; 26 : : gid_t gid; 27 : : int r; 28 : : 29 : 0 : log_setup_service(); 30 : : 31 [ # # ]: 0 : if (argc != 1) 32 [ # # ]: 0 : return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments."); 33 : : 34 : 0 : umask(0022); 35 : : 36 : 0 : r = mac_selinux_init(); 37 [ # # ]: 0 : if (r < 0) 38 [ # # ]: 0 : return log_error_errno(r, "SELinux setup failed: %m"); 39 : : 40 : 0 : r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); 41 [ # # ]: 0 : if (r < 0) 42 [ # # ]: 0 : return log_error_errno(r, "Cannot resolve user name %s: %m", user); 43 : : 44 : : /* Always create the directory where resolv.conf will live */ 45 : 0 : r = mkdir_safe_label("/run/systemd/resolve", 0755, uid, gid, MKDIR_WARN_MODE); 46 [ # # ]: 0 : if (r < 0) 47 [ # # ]: 0 : 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 [ # # ]: 0 : if (getuid() == 0) { 52 : : 53 : : /* Drop privileges, but keep three caps. Note that we drop those too, later on (see below) */ 54 : 0 : 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 [ # # ]: 0 : if (r < 0) 59 [ # # ]: 0 : return log_error_errno(r, "Failed to drop privileges: %m"); 60 : : } 61 : : 62 [ # # ]: 0 : assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGRTMIN+1, -1) >= 0); 63 : : 64 : 0 : r = manager_new(&m); 65 [ # # ]: 0 : if (r < 0) 66 [ # # ]: 0 : return log_error_errno(r, "Could not create manager: %m"); 67 : : 68 : 0 : r = manager_start(m); 69 [ # # ]: 0 : if (r < 0) 70 [ # # ]: 0 : return log_error_errno(r, "Failed to start manager: %m"); 71 : : 72 : : /* Write finish default resolv.conf to avoid a dangling symlink */ 73 : 0 : (void) manager_write_resolv_conf(m); 74 : : 75 : 0 : (void) manager_check_resolv_conf(m); 76 : : 77 : : /* Let's drop the remaining caps now */ 78 : 0 : r = capability_bounding_set_drop(0, true); 79 [ # # ]: 0 : if (r < 0) 80 [ # # ]: 0 : return log_error_errno(r, "Failed to drop remaining caps: %m"); 81 : : 82 : 0 : notify_stop = notify_start(NOTIFY_READY, NOTIFY_STOPPING); 83 : : 84 : 0 : r = sd_event_loop(m->event); 85 [ # # ]: 0 : if (r < 0) 86 [ # # ]: 0 : return log_error_errno(r, "Event loop failed: %m"); 87 : : 88 : 0 : return 0; 89 : : } 90 : : 91 : 0 : DEFINE_MAIN_FUNCTION(run);