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. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 11 | //usage:#define rdate_trivial_usage |
| 12 | //usage: "[-sp] HOST" |
| 13 | //usage:#define rdate_full_usage "\n\n" |
Denys Vlasenko | 54e9585 | 2015-02-18 13:47:27 +0100 | [diff] [blame] | 14 | //usage: "Get and possibly set system time from a remote HOST\n" |
| 15 | //usage: "\n -s Set system time (default)" |
| 16 | //usage: "\n -p Print time" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 17 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 20 | enum { RFC_868_BIAS = 2208988800UL }; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 22 | static void socket_timeout(int sig UNUSED_PARAM) |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 23 | { |
Eric Andersen | 7f2935b | 2003-09-12 08:32:24 +0000 | [diff] [blame] | 24 | bb_error_msg_and_die("timeout connecting to time server"); |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 27 | static time_t askremotedate(const char *host) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 28 | { |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 29 | uint32_t nett; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 30 | int fd; |
| 31 | |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 32 | /* Add a timeout for dead or inaccessible servers */ |
Eric Andersen | e15138a | 2003-09-12 05:50:51 +0000 | [diff] [blame] | 33 | alarm(10); |
| 34 | signal(SIGALRM, socket_timeout); |
| 35 | |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 36 | fd = create_and_connect_stream_or_die(host, bb_lookup_port("time", "tcp", 37)); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 37 | |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 38 | if (safe_read(fd, &nett, 4) != 4) /* read time from server */ |
Denys Vlasenko | 54e9585 | 2015-02-18 13:47:27 +0100 | [diff] [blame] | 39 | bb_error_msg_and_die("%s: %s", host, "short read"); |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 40 | if (ENABLE_FEATURE_CLEAN_UP) |
| 41 | close(fd); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 43 | /* Convert from network byte order to local byte order. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 44 | * RFC 868 time is the number of seconds |
Denis Vlasenko | 8e9ccba | 2007-01-11 16:50:23 +0000 | [diff] [blame] | 45 | * since 00:00 (midnight) 1 January 1900 GMT |
| 46 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 47 | * Subtract the RFC 868 time to get Linux epoch. |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 48 | */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 50 | return ntohl(nett) - RFC_868_BIAS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 54 | int rdate_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 55 | { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 56 | time_t remote_time; |
Denys Vlasenko | 93c32f2 | 2012-06-11 02:06:11 +0200 | [diff] [blame] | 57 | unsigned flags; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 59 | opt_complementary = "-1"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 60 | flags = getopt32(argv, "sp"); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 61 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 62 | remote_time = askremotedate(argv[optind]); |
Eric Andersen | 40eaa9f | 2001-03-19 19:41:54 +0000 | [diff] [blame] | 63 | |
Denys Vlasenko | 39f82d4 | 2012-06-11 14:57:29 +0200 | [diff] [blame] | 64 | if (!(flags & 2)) { /* no -p (-s may be present) */ |
Eric Andersen | 89f10bc | 2003-12-19 11:29:29 +0000 | [diff] [blame] | 65 | time_t current_time; |
| 66 | |
| 67 | time(¤t_time); |
| 68 | if (current_time == remote_time) |
Denis Vlasenko | 000b9ba | 2006-10-05 23:12:49 +0000 | [diff] [blame] | 69 | bb_error_msg("current time matches remote time"); |
Eric Andersen | 89f10bc | 2003-12-19 11:29:29 +0000 | [diff] [blame] | 70 | else |
| 71 | if (stime(&remote_time) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 72 | bb_perror_msg_and_die("can't set time of day"); |
Bernhard Reutner-Fischer | 5c0ae06 | 2006-06-03 10:24:20 +0000 | [diff] [blame] | 73 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 74 | |
Denys Vlasenko | 39f82d4 | 2012-06-11 14:57:29 +0200 | [diff] [blame] | 75 | if (flags != 1) /* not lone -s */ |
Bernhard Reutner-Fischer | 5c0ae06 | 2006-06-03 10:24:20 +0000 | [diff] [blame] | 76 | printf("%s", ctime(&remote_time)); |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 77 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 78 | return EXIT_SUCCESS; |
Eric Andersen | 6ab2202 | 2000-08-21 23:04:00 +0000 | [diff] [blame] | 79 | } |