blob: e2824607eb35e68bd27f1bf33fabd0347635fea0 [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/time.h>
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <netdb.h>
16#include <stdio.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000017#include <string.h>
18#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000019#include <stdlib.h>
20#include <unistd.h>
Eric Andersene15138a2003-09-12 05:50:51 +000021#include <signal.h>
Glenn L McGratha55d72b2003-10-09 11:38:45 +000022
Eric Andersencbe31da2001-02-20 06:14:08 +000023#include "busybox.h"
Eric Andersen6ab22022000-08-21 23:04:00 +000024
25
Mark Whitley59ab0252001-01-23 22:30:04 +000026static const int RFC_868_BIAS = 2208988800UL;
Eric Andersen6ab22022000-08-21 23:04:00 +000027
Glenn L McGratha55d72b2003-10-09 11:38:45 +000028static void socket_timeout(int sig)
Eric Andersene15138a2003-09-12 05:50:51 +000029{
Eric Andersen7f2935b2003-09-12 08:32:24 +000030 bb_error_msg_and_die("timeout connecting to time server");
Eric Andersene15138a2003-09-12 05:50:51 +000031}
32
Eric Andersen40eaa9f2001-03-19 19:41:54 +000033static time_t askremotedate(const char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000034{
Rob Landley1b751c82005-10-28 09:24:33 +000035 unsigned long nett;
Eric Andersen04d055f2003-11-03 21:20:18 +000036 struct sockaddr_in s_in;
Eric Andersen6ab22022000-08-21 23:04:00 +000037 int fd;
38
Glenn L McGrathffccf6e2003-12-20 01:47:18 +000039 bb_lookup_host(&s_in, host);
Eric Andersen15eb39c2004-01-18 18:18:33 +000040 s_in.sin_port = bb_lookup_port("time", "tcp", 37);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000041
Rob Landley1b751c82005-10-28 09:24:33 +000042 /* Add a timeout for dead or inaccessible servers */
Eric Andersene15138a2003-09-12 05:50:51 +000043 alarm(10);
44 signal(SIGALRM, socket_timeout);
45
Eric Andersen04d055f2003-11-03 21:20:18 +000046 fd = xconnect(&s_in);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000047
Eric Andersen725db192003-07-22 08:26:05 +000048 if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_error_msg_and_die("%s did not send the complete time", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000050 close(fd);
51
52 /* convert from network byte order to local byte order.
53 * RFC 868 time is the number of seconds
54 * since 00:00 (midnight) 1 January 1900 GMT
55 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
56 * Subtract the RFC 868 time to get Linux epoch
57 */
Rob Landley1b751c82005-10-28 09:24:33 +000058
59 return(ntohl(nett) - RFC_868_BIAS);
Eric Andersen6ab22022000-08-21 23:04:00 +000060}
61
62int rdate_main(int argc, char **argv)
63{
Eric Andersen1ca20a72001-03-21 07:34:27 +000064 time_t remote_time;
Rob Landley1b751c82005-10-28 09:24:33 +000065
66 unsigned long flags = bb_getopt_ulflags(argc, argv, "sp");
67
68 if (!flags || argc == optind)
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_show_usage();
Rob Landley1b751c82005-10-28 09:24:33 +000070
Eric Andersen1ca20a72001-03-21 07:34:27 +000071 remote_time = askremotedate(argv[optind]);
Eric Andersen40eaa9f2001-03-19 19:41:54 +000072
Rob Landley1b751c82005-10-28 09:24:33 +000073 if (flags & 1) {
Eric Andersen89f10bc2003-12-19 11:29:29 +000074 time_t current_time;
75
76 time(&current_time);
77 if (current_time == remote_time)
78 bb_error_msg("Current time matches remote time.");
79 else
80 if (stime(&remote_time) < 0)
81 bb_perror_msg_and_die("Could not set time of day");
Rob Landley1b751c82005-10-28 09:24:33 +000082
83 /* No need to check for the -p flag as it's the only option left */
84
85 } else printf("%s", ctime(&remote_time));
Eric Andersen6ab22022000-08-21 23:04:00 +000086
Matt Kraai3e856ce2000-12-01 02:55:13 +000087 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +000088}