1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 
6 #include "sd-daemon.h"
7 #include "sd-event.h"
8 
9 #include "capability-util.h"
10 #include "clock-util.h"
11 #include "daemon-util.h"
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "main-func.h"
15 #include "mkdir-label.h"
16 #include "network-util.h"
17 #include "process-util.h"
18 #include "signal-util.h"
19 #include "timesyncd-bus.h"
20 #include "timesyncd-conf.h"
21 #include "timesyncd-manager.h"
22 #include "user-util.h"
23 
load_clock_timestamp(uid_t uid,gid_t gid)24 static int load_clock_timestamp(uid_t uid, gid_t gid) {
25         usec_t min = TIME_EPOCH * USEC_PER_SEC, ct;
26         _cleanup_close_ int fd = -1;
27         int r;
28 
29         /* Let's try to make sure that the clock is always
30          * monotonically increasing, by saving the clock whenever we
31          * have a new NTP time, or when we shut down, and restoring it
32          * when we start again. This is particularly helpful on
33          * systems lacking a battery backed RTC. We also will adjust
34          * the time to at least the build time of systemd. */
35 
36         fd = open(CLOCK_FILE, O_RDWR|O_CLOEXEC, 0644);
37         if (fd >= 0) {
38                 struct stat st;
39                 usec_t stamp;
40 
41                 /* check if the recorded time is later than the compiled-in one */
42                 if (fstat(fd, &st) >= 0) {
43                         stamp = timespec_load(&st.st_mtim);
44                         if (stamp > min)
45                                 min = stamp;
46                 }
47 
48                 if (geteuid() == 0) {
49                         /* Try to fix the access mode, so that we can still
50                            touch the file after dropping privileges */
51                         r = fchmod_and_chown(fd, 0644, uid, gid);
52                         if (r < 0)
53                                 log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
54                 }
55 
56         } else {
57                 r = mkdir_safe_label(STATE_DIR, 0755, uid, gid,
58                                      MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
59                 if (r < 0) {
60                         log_debug_errno(r, "Failed to create state directory, ignoring: %m");
61                         goto settime;
62                 }
63 
64                 /* create stamp file with the compiled-in date */
65                 r = touch_file(CLOCK_FILE, /* parents= */ false, min, uid, gid, 0644);
66                 if (r < 0)
67                         log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE);
68         }
69 
70 settime:
71         ct = now(CLOCK_REALTIME);
72         if (ct < min) {
73                 char date[FORMAT_TIMESTAMP_MAX];
74 
75                 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
76                          format_timestamp(date, sizeof(date), min));
77 
78                 if (clock_settime(CLOCK_REALTIME, TIMESPEC_STORE(min)) < 0)
79                         log_error_errno(errno, "Failed to restore system clock, ignoring: %m");
80         }
81 
82         return 0;
83 }
84 
run(int argc,char * argv[])85 static int run(int argc, char *argv[]) {
86         _cleanup_(manager_freep) Manager *m = NULL;
87         _unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
88         const char *user = "systemd-timesync";
89         uid_t uid, uid_current;
90         gid_t gid;
91         int r;
92 
93         log_set_facility(LOG_CRON);
94         log_setup();
95 
96         umask(0022);
97 
98         if (argc != 1)
99                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program does not take arguments.");
100 
101         uid = uid_current = geteuid();
102         gid = getegid();
103 
104         if (uid_current == 0) {
105                 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
106                 if (r < 0)
107                         return log_error_errno(r, "Cannot resolve user name %s: %m", user);
108         }
109 
110         r = load_clock_timestamp(uid, gid);
111         if (r < 0)
112                 return r;
113 
114         /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
115          * privileges are already dropped. */
116         if (uid_current == 0) {
117                 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
118                 if (r < 0)
119                         return log_error_errno(r, "Failed to drop privileges: %m");
120         }
121 
122         assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
123 
124         r = manager_new(&m);
125         if (r < 0)
126                 return log_error_errno(r, "Failed to allocate manager: %m");
127 
128         r = manager_connect_bus(m);
129         if (r < 0)
130                 return log_error_errno(r, "Could not connect to bus: %m");
131 
132         if (clock_is_localtime(NULL) > 0) {
133                 log_info("The system is configured to read the RTC time in the local time zone. "
134                          "This mode cannot be fully supported. All system time to RTC updates are disabled.");
135                 m->rtc_local_time = true;
136         }
137 
138         r = manager_parse_config_file(m);
139         if (r < 0)
140                 log_warning_errno(r, "Failed to parse configuration file: %m");
141 
142         r = manager_parse_fallback_string(m, NTP_SERVERS);
143         if (r < 0)
144                 return log_error_errno(r, "Failed to parse fallback server strings: %m");
145 
146         log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
147 
148         notify_message = notify_start("READY=1\n"
149                                       "STATUS=Daemon is running",
150                                       NOTIFY_STOPPING);
151 
152         r = manager_setup_save_time_event(m);
153         if (r < 0)
154                 return r;
155 
156         if (network_is_online()) {
157                 r = manager_connect(m);
158                 if (r < 0)
159                         return r;
160         }
161 
162         r = sd_event_loop(m->event);
163         if (r < 0)
164                 return log_error_errno(r, "Failed to run event loop: %m");
165 
166         /* if we got an authoritative time, store it in the file system */
167         if (m->save_on_exit) {
168                 r = touch(CLOCK_FILE);
169                 if (r < 0)
170                         log_debug_errno(r, "Failed to touch " CLOCK_FILE ", ignoring: %m");
171         }
172 
173         return 0;
174 }
175 
176 DEFINE_MAIN_FUNCTION(run);
177