void dbus_announce (void) { char *argv[] = { TLSDATE_DBUS_ANNOUNCE, NULL }; pid_t pid = fork(); if (!pid) { drop_privs_to (DBUS_USER, DBUS_GROUP); pid = fork(); if (!pid) exit(execve(argv[0], argv, NULL)); else exit(pid < 0); } else if (pid > 0) { wait(NULL); } }
int main(int argc, char **argv) { uint32_t *time_map; struct tlsdate_time start_time, end_time, warp_time; int status; pid_t ssl_child; long long rt_time_ms; uint32_t server_time_s; int setclock; int showtime; int showtime_raw; int timewarp; int leap; int http; if (argc != 13) return 1; host = argv[1]; hostname_to_verify = argv[1]; port = argv[2]; protocol = argv[3]; ca_cert_container = argv[6]; ca_racket = (0 != strcmp ("unchecked", argv[4])); verbose = (0 != strcmp ("quiet", argv[5])); verbose_debug = (0 != strcmp ("verbose", argv[5])); setclock = (0 == strcmp ("setclock", argv[7])); showtime = (0 == strcmp ("showtime", argv[8])); showtime_raw = (0 == strcmp ("showtime=raw", argv[8])); timewarp = (0 == strcmp ("timewarp", argv[9])); leap = (0 == strcmp ("leapaway", argv[10])); proxy = (0 == strcmp ("none", argv[11]) ? NULL : argv[11]); http = (0 == (strcmp("http", argv[12]))); /* Initalize warp_time with RECENT_COMPILE_DATE */ clock_init_time(&warp_time, RECENT_COMPILE_DATE, 0); verb ("V: RECENT_COMPILE_DATE is %lu.%06lu", (unsigned long) CLOCK_SEC(&warp_time), (unsigned long) CLOCK_USEC(&warp_time)); if (1 != timewarp) { verb ("V: we'll do the time warp another time - we're not setting clock"); } /* We are not going to set the clock, thus no need to stay root */ if (0 == setclock && 0 == timewarp) { verb ("V: attemping to drop administrator privileges"); drop_privs_to (UNPRIV_USER, UNPRIV_GROUP); } // We cast the mmap value to remove this error when compiling with g++: // src/tlsdate-helper.c: In function ‘int main(int, char**)’: // src/tlsdate-helper.c:822:41: error: invalid conversion from ‘void*’ to ‘uint32_t time_map = (uint32_t *) mmap (NULL, sizeof (uint32_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); if (MAP_FAILED == time_map) { fprintf (stderr, "mmap failed: %s", strerror (errno)); return 1; } /* Get the current time from the system clock. */ if (0 != clock_get_real_time(&start_time)) { die ("Failed to read current time of day: %s", strerror (errno)); } verb ("V: time is currently %lu.%06lu", (unsigned long) CLOCK_SEC(&start_time), (unsigned long) CLOCK_NSEC(&start_time)); if (((unsigned long) CLOCK_SEC(&start_time)) < ((unsigned long) CLOCK_SEC(&warp_time))) { verb ("V: local clock time is less than RECENT_COMPILE_DATE"); if (timewarp) { verb ("V: Attempting to warp local clock into the future"); if (0 != clock_set_real_time(&warp_time)) { die ("setting time failed: %s (Attempted to set clock to %lu.%06lu)", strerror (errno), (unsigned long) CLOCK_SEC(&warp_time), (unsigned long) CLOCK_SEC(&warp_time)); } if (0 != clock_get_real_time(&start_time)) { die ("Failed to read current time of day: %s", strerror (errno)); } verb ("V: time is currently %lu.%06lu", (unsigned long) CLOCK_SEC(&start_time), (unsigned long) CLOCK_NSEC(&start_time)); verb ("V: It's just a step to the left..."); } } else { verb ("V: time is greater than RECENT_COMPILE_DATE"); } /* initialize to bogus value, just to be on the safe side */ *time_map = 0; /* Run SSL interaction in separate process (and not as 'root') */ ssl_child = fork (); if (-1 == ssl_child) die ("fork failed: %s", strerror (errno)); if (0 == ssl_child) { drop_privs_to (UNPRIV_USER, UNPRIV_GROUP); run_ssl (time_map, leap, http); (void) munmap (time_map, sizeof (uint32_t)); _exit (0); } if (ssl_child != platform->process_wait (ssl_child, &status, 1)) die ("waitpid failed: %s", strerror (errno)); if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status)) )) die ("child process failed in SSL handshake"); if (0 != clock_get_real_time(&end_time)) die ("Failed to read current time of day: %s", strerror (errno)); /* calculate RTT */ rt_time_ms = (CLOCK_SEC(&end_time) - CLOCK_SEC(&start_time)) * 1000 + (CLOCK_USEC(&end_time) - CLOCK_USEC(&start_time)) / 1000; if (rt_time_ms < 0) rt_time_ms = 0; /* non-linear time... */ #ifdef USE_POLARSSL server_time_s = *time_map; #else server_time_s = ntohl (*time_map); #endif // We should never have a time_map of zero here; // It either stayed zero or we have a false ticker. if ( 0 == server_time_s ) die ("child process failed to update time map; weird platform issues?"); munmap (time_map, sizeof (uint32_t)); verb ("V: server time %u (difference is about %d s) was fetched in %lld ms", (unsigned int) server_time_s, CLOCK_SEC(&start_time) - server_time_s, rt_time_ms); /* warning if the handshake took too long */ if (rt_time_ms > TLS_RTT_UNREASONABLE) { die ("the TLS handshake took more than %d msecs - consider using a different " \ "server or run it again", TLS_RTT_UNREASONABLE); } if (rt_time_ms > TLS_RTT_THRESHOLD) { verb ("V: the TLS handshake took more than %d msecs - consider using a different " \ "server or run it again", TLS_RTT_THRESHOLD); } if (showtime_raw) { fwrite(&server_time_s, sizeof(server_time_s), 1, stdout); } if (showtime) { struct tm ltm; time_t tim = server_time_s; char buf[256]; localtime_r(&tim, <m); if (0 == strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y", <m)) { die ("strftime returned 0"); } fprintf(stdout, "%s\n", buf); } /* finally, actually set the time */ if (setclock) { struct tlsdate_time server_time; clock_init_time(&server_time, server_time_s + (rt_time_ms / 2 / 1000), (rt_time_ms / 2) % 1000); // We should never receive a time that is before the time we were last // compiled; we subscribe to the linear theory of time for this program // and this program alone! if (CLOCK_SEC(&server_time) >= MAX_REASONABLE_TIME) die("remote server is a false ticker from the future!"); if (CLOCK_SEC(&server_time) <= RECENT_COMPILE_DATE) die ("remote server is a false ticker!"); if (0 != clock_set_real_time(&server_time)) die ("setting time failed: %s (Difference from server is about %d s)", strerror (errno), CLOCK_SEC(&start_time) - server_time_s); verb ("V: setting time succeeded"); } return 0; }
int API main (int argc, char *argv[], char *envp[]) { initalize_syslog (); struct state state; /* TODO(wad) EVENT_BASE_FLAG_PRECISE_TIMER | EVENT_BASE_FLAG_PRECISE_TIMER */ struct event_base *base = event_base_new(); if (!base) { fatal ("could not allocated new event base"); } /* Add three priority levels: * 0 - time saving. Must be done before any other events are handled. * 1 - network synchronization events * 2 - any other events (wake, platform, etc) */ event_base_priority_init (base, MAX_EVENT_PRIORITIES); memset (&state, 0, sizeof (state)); set_conf_defaults (&state.opts); parse_argv (&state.opts, argc, argv); check_conf (&state); load_conf (&state.opts); check_conf (&state); if (!state.opts.sources) add_source_to_conf (&state.opts, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_PROXY); state.base = base; state.envp = envp; state.backoff = state.opts.wait_between_tries; /* TODO(wad) move this into setup_time_setter */ /* grab a handle to /dev/rtc for time-setter. */ if (state.opts.should_sync_hwclock && platform->rtc_open(&state.hwclock)) { pinfo ("can't open hwclock fd"); state.opts.should_sync_hwclock = 0; } /* install the SIGCHLD handler for the setter and tlsdate */ if (setup_sigchld_event (&state, 1)) { error ("Failed to setup SIGCHLD event"); goto out; } /* fork off the privileged helper */ info ("spawning time setting helper . . ."); if (setup_time_setter (&state)) { error ("could not fork privileged coprocess"); goto out; } /* release the hwclock now that the time-setter is running. */ if (state.opts.should_sync_hwclock) { platform->rtc_close (&state.hwclock); } /* drop privileges before touching any untrusted data */ drop_privs_to (state.opts.user, state.opts.group); /* register a signal handler to save time at shutdown */ if (state.opts.should_save_disk) { struct event *event = event_new (base, SIGTERM, EV_SIGNAL|EV_PERSIST, action_sigterm, &state); if (!event) fatal ("Failed to create SIGTERM event"); event_priority_set (event, PRI_SAVE); event_add (event, NULL); } if (state.opts.should_dbus && init_dbus (&state)) { error ("Failed to initialize DBus"); goto out; } /* Register the tlsdate event before any listeners could show up. */ state.events[E_TLSDATE] = event_new (base, -1, EV_TIMEOUT, action_run_tlsdate, &state); if (!state.events[E_TLSDATE]) { error ("Failed to create tlsdate event"); goto out; } event_priority_set (state.events[E_TLSDATE], PRI_NET); /* The timeout and fd will be filled in per-call. */ if (setup_tlsdate_status (&state)) { error ("Failed to create tlsdate status event"); goto out; } /* TODO(wad) Could use a timeout on this to catch setter death? */ /* EV_READ is for truncation/EPIPE notification */ state.events[E_SAVE] = event_new (base, state.setter_save_fd, EV_READ|EV_WRITE, action_sync_and_save, &state); if (!state.events[E_SAVE]) { error ("Failed to create sync & save event"); goto out; } event_priority_set (state.events[E_SAVE], PRI_SAVE); /* Start by grabbing the system time. */ state.last_sync_type = SYNC_TYPE_RTC; state.last_time = time (NULL); /* If possible, grab disk time and check the two. */ if (state.opts.should_load_disk) { time_t disk_time = state.last_time; if (!load_disk_timestamp (state.timestamp_path, &disk_time)) { info ("disk timestamp available: yes"); if (!is_sane_time (state.last_time) || state.last_time < disk_time) { state.last_sync_type = SYNC_TYPE_DISK; state.last_time = disk_time; } } else { info ("disk timestamp available: no"); } } if (!is_sane_time (state.last_time)) { state.last_sync_type = SYNC_TYPE_BUILD; state.last_time = RECENT_COMPILE_DATE + 1; } /* Save and announce the initial time source. */ trigger_event (&state, E_SAVE, -1); info ("initial time sync type: %s", sync_type_str (state.last_sync_type)); /* Initialize platform specific loop behavior */ if (platform_init_cros (&state)) { error ("Failed to initialize platform code"); goto out; } if (setup_event_route_up (&state)) { error ("Failed to setup route up monitoring"); goto out; } if (setup_event_timer_sync (&state)) { error ("Failed to setup a timer event"); goto out; } if (setup_event_timer_continuity (&state)) { error ("Failed to setup continuity timer"); goto out; } /* Add a forced sync event to the event list. */ action_kickoff_time_sync (-1, EV_TIMEOUT, &state); info ("Entering dispatch . . ."); event_base_dispatch (base); info ("tlsdated terminating gracefully"); out: return cleanup_main (&state); }