Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * The Rdate command will ask a time server for the RFC 868 time |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 4 | * and optionally set the system time. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * by Sterling Huxley <sterling@europa.com> |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 9 | */ |
| 10 | //config:config RDATE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 11 | //config: bool "rdate (5.6 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: The rdate utility allows you to synchronize the date and time of your |
| 15 | //config: system clock with the date and time of a remote networked system using |
| 16 | //config: the RFC868 protocol, which is built into the inetd daemon on most |
| 17 | //config: systems. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 18 | |
| 19 | //applet:IF_RDATE(APPLET(rdate, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_RDATE) += rdate.o |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define rdate_trivial_usage |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 24 | //usage: "[-s/-p] HOST" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 25 | //usage:#define rdate_full_usage "\n\n" |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 26 | //usage: "Set and print time from HOST using RFC 868\n" |
| 27 | //usage: "\n -s Only set system time" |
| 28 | //usage: "\n -p Only print time" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 29 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 32 | enum { RFC_868_BIAS = 2208988800UL }; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 34 | static void socket_timeout(int sig UNUSED_PARAM) |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 35 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 36 | bb_simple_error_msg_and_die("timeout connecting to time server"); |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 39 | static time_t askremotedate(const char *host) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 40 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 41 | uint32_t nett; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 42 | int fd; |
| 43 | |
Denys Vlasenko | e9a5a69 | 2017-07-27 14:31:59 +0200 | [diff] [blame] | 44 | /* Timeout for dead or inaccessible servers */ |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 45 | alarm(10); |
| 46 | signal(SIGALRM, socket_timeout); |
| 47 | |
Denys Vlasenko | 2aeb201 | 2018-04-17 12:43:54 +0200 | [diff] [blame] | 48 | fd = create_and_connect_stream_or_die(host, bb_lookup_std_port("time", "tcp", 37)); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 49 | |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 50 | if (safe_read(fd, &nett, 4) != 4) /* read time from server */ |
Denys Vlasenko | 54e9585 | 2015-02-18 13:47:27 +0100 | [diff] [blame] | 51 | bb_error_msg_and_die("%s: %s", host, "short read"); |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 52 | if (ENABLE_FEATURE_CLEAN_UP) |
| 53 | close(fd); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 54 | |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 55 | /* Convert from network byte order to local byte order. |
Denys Vlasenko | e9a5a69 | 2017-07-27 14:31:59 +0200 | [diff] [blame] | 56 | * RFC 868 time is seconds since 1900-01-01 00:00 GMT. |
| 57 | * RFC 868 time 2,208,988,800 corresponds to 1970-01-01 00:00 GMT. |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 58 | * Subtract the RFC 868 time to get Linux epoch. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 59 | */ |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 60 | nett = ntohl(nett) - RFC_868_BIAS; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 62 | if (sizeof(time_t) > 4) { |
| 63 | /* Now we have 32-bit lsb of a wider time_t |
| 64 | * Imagine that nett = 0x00000001, |
| 65 | * current time cur = 0x123ffffffff. |
| 66 | * Assuming our time is not some 40 years off, |
| 67 | * remote time must be 0x12400000001. |
Denys Vlasenko | e9a5a69 | 2017-07-27 14:31:59 +0200 | [diff] [blame] | 68 | * Need to adjust our time by (int32_t)(nett - cur). |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 69 | */ |
| 70 | time_t cur = time(NULL); |
| 71 | int32_t adjust = (int32_t)(nett - (uint32_t)cur); |
| 72 | return cur + adjust; |
| 73 | } |
| 74 | /* This is not going to work, but what can we do */ |
| 75 | return (time_t)nett; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 78 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 79 | int rdate_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 80 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 81 | time_t remote_time; |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 82 | unsigned flags; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 84 | flags = getopt32(argv, "^" "sp" "\0" "-1"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 85 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 86 | remote_time = askremotedate(argv[optind]); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 87 | |
Denys Vlasenko | 179e88b | 2017-01-20 16:03:48 +0100 | [diff] [blame] | 88 | /* Manpages of various Unixes are confusing. What happens is: |
| 89 | * (no opts) set and print time |
| 90 | * -s: set time ("do not print the time") |
| 91 | * -p: print time ("do not set, just print the remote time") |
| 92 | * -sp: print time (that's what we do, not sure this is right) |
| 93 | */ |
| 94 | |
Denys Vlasenko | 39f82d4 | 2012-06-11 14:57:29 +0200 | [diff] [blame] | 95 | if (!(flags & 2)) { /* no -p (-s may be present) */ |
Denys Vlasenko | 6e51139 | 2017-01-20 16:07:14 +0100 | [diff] [blame] | 96 | if (time(NULL) == remote_time) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 97 | bb_simple_error_msg("current time matches remote time"); |
Alistair Francis | d3539be | 2019-11-19 13:06:40 +0100 | [diff] [blame] | 98 | else { |
Denys Vlasenko | eb0c2e2 | 2020-12-16 21:36:36 +0100 | [diff] [blame^] | 99 | struct timeval ts; |
Alistair Francis | d3539be | 2019-11-19 13:06:40 +0100 | [diff] [blame] | 100 | ts.tv_sec = remote_time; |
Denys Vlasenko | eb0c2e2 | 2020-12-16 21:36:36 +0100 | [diff] [blame^] | 101 | ts.tv_usec = 0; |
| 102 | xsettimeofday(&ts); |
Alistair Francis | d3539be | 2019-11-19 13:06:40 +0100 | [diff] [blame] | 103 | } |
Bernhard Reutner-Fischer | 5c0ae06 | 2006-06-03 10:24:20 +0000 | [diff] [blame] | 104 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 105 | |
Denys Vlasenko | 39f82d4 | 2012-06-11 14:57:29 +0200 | [diff] [blame] | 106 | if (flags != 1) /* not lone -s */ |
Bernhard Reutner-Fischer | 5c0ae06 | 2006-06-03 10:24:20 +0000 | [diff] [blame] | 107 | printf("%s", ctime(&remote_time)); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 108 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 109 | return EXIT_SUCCESS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 110 | } |