blob: 5ec795208050f4c251a546c65a468e2a3a1a1b8c [file] [log] [blame]
Eric Andersen6ab22022000-08-21 23:04:00 +00001/* vi: set sw=4 ts=4: */
2/*
3 * The Rdate command will ask a time server for the RFC 868 time
Denys Vlasenko93c32f22012-06-11 02:06:11 +02004 * and optionally set the system time.
Eric Andersen6ab22022000-08-21 23:04:00 +00005 *
6 * by Sterling Huxley <sterling@europa.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkodd898c92016-11-23 11:46:32 +01009 */
10//config:config RDATE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "rdate (6 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//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 Vlasenkodd898c92016-11-23 11:46:32 +010018
19//applet:IF_RDATE(APPLET(rdate, BB_DIR_USR_SBIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_RDATE) += rdate.o
Eric Andersen6ab22022000-08-21 23:04:00 +000022
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define rdate_trivial_usage
Denys Vlasenko179e88b2017-01-20 16:03:48 +010024//usage: "[-s/-p] HOST"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define rdate_full_usage "\n\n"
Denys Vlasenko179e88b2017-01-20 16:03:48 +010026//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 Orga5bc8c002011-04-11 03:29:49 +020029
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000031
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000032enum { RFC_868_BIAS = 2208988800UL };
Eric Andersen6ab22022000-08-21 23:04:00 +000033
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000034static void socket_timeout(int sig UNUSED_PARAM)
Eric Andersene15138a2003-09-12 05:50:51 +000035{
Eric Andersen7f2935b2003-09-12 08:32:24 +000036 bb_error_msg_and_die("timeout connecting to time server");
Eric Andersene15138a2003-09-12 05:50:51 +000037}
38
Eric Andersen40eaa9f2001-03-19 19:41:54 +000039static time_t askremotedate(const char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000040{
Denis Vlasenko8e9ccba2007-01-11 16:50:23 +000041 uint32_t nett;
Eric Andersen6ab22022000-08-21 23:04:00 +000042 int fd;
43
Denys Vlasenkoe9a5a692017-07-27 14:31:59 +020044 /* Timeout for dead or inaccessible servers */
Eric Andersene15138a2003-09-12 05:50:51 +000045 alarm(10);
46 signal(SIGALRM, socket_timeout);
47
Denys Vlasenko2aeb2012018-04-17 12:43:54 +020048 fd = create_and_connect_stream_or_die(host, bb_lookup_std_port("time", "tcp", 37));
Eric Andersen40eaa9f2001-03-19 19:41:54 +000049
Denys Vlasenko93c32f22012-06-11 02:06:11 +020050 if (safe_read(fd, &nett, 4) != 4) /* read time from server */
Denys Vlasenko54e95852015-02-18 13:47:27 +010051 bb_error_msg_and_die("%s: %s", host, "short read");
Denys Vlasenko93c32f22012-06-11 02:06:11 +020052 if (ENABLE_FEATURE_CLEAN_UP)
53 close(fd);
Eric Andersen6ab22022000-08-21 23:04:00 +000054
Denys Vlasenko93c32f22012-06-11 02:06:11 +020055 /* Convert from network byte order to local byte order.
Denys Vlasenkoe9a5a692017-07-27 14:31:59 +020056 * 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 Vlasenko93c32f22012-06-11 02:06:11 +020058 * Subtract the RFC 868 time to get Linux epoch.
Eric Andersen6ab22022000-08-21 23:04:00 +000059 */
Denys Vlasenko179e88b2017-01-20 16:03:48 +010060 nett = ntohl(nett) - RFC_868_BIAS;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000061
Denys Vlasenko179e88b2017-01-20 16:03:48 +010062 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 Vlasenkoe9a5a692017-07-27 14:31:59 +020068 * Need to adjust our time by (int32_t)(nett - cur).
Denys Vlasenko179e88b2017-01-20 16:03:48 +010069 */
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 Andersen6ab22022000-08-21 23:04:00 +000076}
77
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000078int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000079int rdate_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen6ab22022000-08-21 23:04:00 +000080{
Eric Andersen1ca20a72001-03-21 07:34:27 +000081 time_t remote_time;
Denys Vlasenko93c32f22012-06-11 02:06:11 +020082 unsigned flags;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Denys Vlasenko22542ec2017-08-08 21:55:02 +020084 flags = getopt32(argv, "^" "sp" "\0" "-1");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000085
Eric Andersen1ca20a72001-03-21 07:34:27 +000086 remote_time = askremotedate(argv[optind]);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000087
Denys Vlasenko179e88b2017-01-20 16:03:48 +010088 /* 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 Vlasenko39f82d42012-06-11 14:57:29 +020095 if (!(flags & 2)) { /* no -p (-s may be present) */
Denys Vlasenko6e511392017-01-20 16:07:14 +010096 if (time(NULL) == remote_time)
Denis Vlasenko000b9ba2006-10-05 23:12:49 +000097 bb_error_msg("current time matches remote time");
Eric Andersen89f10bc2003-12-19 11:29:29 +000098 else
99 if (stime(&remote_time) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100100 bb_perror_msg_and_die("can't set time of day");
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +0000101 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000102
Denys Vlasenko39f82d42012-06-11 14:57:29 +0200103 if (flags != 1) /* not lone -s */
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +0000104 printf("%s", ctime(&remote_time));
Eric Andersen6ab22022000-08-21 23:04:00 +0000105
Matt Kraai3e856ce2000-12-01 02:55:13 +0000106 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +0000107}