blob: c24465dc3bc95b20875428194e16d41d1c5bbb83 [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
4 * and optionally set the system time.
5 *
6 * by Sterling Huxley <sterling@europa.com>
7 *
Rob Landley1b751c82005-10-28 09:24:33 +00008 * Licensed under GPL v2 or later, see file License for details.
Eric Andersen6ab22022000-08-21 23:04:00 +00009*/
10
Eric Andersen6ab22022000-08-21 23:04:00 +000011#include <sys/types.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
14#include <netdb.h>
15#include <stdio.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000016#include <string.h>
17#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000018#include <stdlib.h>
19#include <unistd.h>
Eric Andersene15138a2003-09-12 05:50:51 +000020#include <signal.h>
Glenn L McGratha55d72b2003-10-09 11:38:45 +000021
Eric Andersencbe31da2001-02-20 06:14:08 +000022#include "busybox.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000023
24
Mark Whitley59ab0252001-01-23 22:30:04 +000025static const int RFC_868_BIAS = 2208988800UL;
Eric Andersen6ab22022000-08-21 23:04:00 +000026
Glenn L McGratha55d72b2003-10-09 11:38:45 +000027static void socket_timeout(int sig)
Eric Andersene15138a2003-09-12 05:50:51 +000028{
Eric Andersen7f2935b2003-09-12 08:32:24 +000029 bb_error_msg_and_die("timeout connecting to time server");
Eric Andersene15138a2003-09-12 05:50:51 +000030}
31
Eric Andersen40eaa9f2001-03-19 19:41:54 +000032static time_t askremotedate(const char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000033{
Rob Landley1b751c82005-10-28 09:24:33 +000034 unsigned long nett;
Eric Andersen04d055f2003-11-03 21:20:18 +000035 struct sockaddr_in s_in;
Eric Andersen6ab22022000-08-21 23:04:00 +000036 int fd;
37
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000038 bb_lookup_host(&s_in, host);
Eric Andersen15eb39c2004-01-18 18:18:33 +000039 s_in.sin_port = bb_lookup_port("time", "tcp", 37);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000040
Rob Landley1b751c82005-10-28 09:24:33 +000041 /* Add a timeout for dead or inaccessible servers */
Eric Andersene15138a2003-09-12 05:50:51 +000042 alarm(10);
43 signal(SIGALRM, socket_timeout);
44
Eric Andersen04d055f2003-11-03 21:20:18 +000045 fd = xconnect(&s_in);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000046
Eric Andersen725db192003-07-22 08:26:05 +000047 if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_error_msg_and_die("%s did not send the complete time", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000049 close(fd);
50
51 /* convert from network byte order to local byte order.
52 * RFC 868 time is the number of seconds
53 * since 00:00 (midnight) 1 January 1900 GMT
54 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
55 * Subtract the RFC 868 time to get Linux epoch
56 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000057
Rob Landley1b751c82005-10-28 09:24:33 +000058 return(ntohl(nett) - RFC_868_BIAS);
Eric Andersen6ab22022000-08-21 23:04:00 +000059}
60
61int rdate_main(int argc, char **argv)
62{
Eric Andersen1ca20a72001-03-21 07:34:27 +000063 time_t remote_time;
"Vladimir N. Oleynik"ea972822005-10-28 15:43:41 +000064 unsigned long flags;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000065
Denis Vlasenko67b23e62006-10-03 21:00:06 +000066 opt_complementary = "-1";
67 flags = getopt32(argc, argv, "sp");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000068
Eric Andersen1ca20a72001-03-21 07:34:27 +000069 remote_time = askremotedate(argv[optind]);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000070
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000071 if ((flags & 2) == 0) {
Eric Andersen89f10bc2003-12-19 11:29:29 +000072 time_t current_time;
73
74 time(&current_time);
75 if (current_time == remote_time)
76 bb_error_msg("Current time matches remote time.");
77 else
78 if (stime(&remote_time) < 0)
79 bb_perror_msg_and_die("Could not set time of day");
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000080 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Bernhard Reutner-Fischer5c0ae062006-06-03 10:24:20 +000082 if ((flags & 1) == 0)
83 printf("%s", ctime(&remote_time));
Eric Andersen6ab22022000-08-21 23:04:00 +000084
Matt Kraai3e856ce2000-12-01 02:55:13 +000085 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +000086}