blob: 28e87323d4a2139581daaa9a4bd4499ae94ed55f [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22*/
23
Eric Andersen6ab22022000-08-21 23:04:00 +000024#include <sys/time.h>
25#include <sys/types.h>
26#include <sys/socket.h>
27#include <netinet/in.h>
28#include <netdb.h>
29#include <stdio.h>
30#include <getopt.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000031#include <string.h>
32#include <time.h>
Eric Andersened3ef502001-01-27 08:24:39 +000033#include <stdlib.h>
34#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
36#define BB_DECLARE_EXTERN
37#include "messages.c"
Eric Andersen6ab22022000-08-21 23:04:00 +000038
39
Mark Whitley59ab0252001-01-23 22:30:04 +000040static const int RFC_868_BIAS = 2208988800UL;
Eric Andersen6ab22022000-08-21 23:04:00 +000041
Eric Andersen3e6ff902001-03-09 21:24:12 +000042static int setdate= 0;
43static int printdate= 0;
Eric Andersen6ab22022000-08-21 23:04:00 +000044
Eric Andersen3e6ff902001-03-09 21:24:12 +000045static time_t askremotedate(char *host)
Eric Andersen6ab22022000-08-21 23:04:00 +000046{
47 struct hostent *h;
48 struct sockaddr_in sin;
49 struct servent *tserv;
50 unsigned long int nett, localt;
51 int fd;
52
53 if (!(h = gethostbyname(host))) { /* get the IP addr */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000054 perror_msg("%s", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000055 return(-1);
56 }
57 if ((tserv = getservbyname("time", "tcp")) == NULL) { /* find port # */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000058 perror_msg("%s", "time");
Eric Andersen6ab22022000-08-21 23:04:00 +000059 return(-1);
60 }
61 if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* get net connection */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000062 perror_msg("%s", "socket");
Eric Andersen6ab22022000-08-21 23:04:00 +000063 return(-1);
64 }
65
66 memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr));
67 sin.sin_port= tserv->s_port;
68 sin.sin_family = AF_INET;
69
Eric Andersenba35b982000-09-21 04:05:38 +000070 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { /* connect to time server */
Matt Kraai1fa1ade2000-12-18 03:57:16 +000071 perror_msg("%s", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000072 close(fd);
73 return(-1);
74 }
Eric Andersend1ee7e32000-10-26 07:57:27 +000075 if (read(fd, (void *)&nett, 4) != 4) { /* read time from server */
Eric Andersen6ab22022000-08-21 23:04:00 +000076 close(fd);
Matt Kraaidd19c692001-01-31 19:00:21 +000077 error_msg("%s did not send the complete time", host);
Eric Andersen6ab22022000-08-21 23:04:00 +000078 }
79 close(fd);
80
81 /* convert from network byte order to local byte order.
82 * RFC 868 time is the number of seconds
83 * since 00:00 (midnight) 1 January 1900 GMT
84 * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT
85 * Subtract the RFC 868 time to get Linux epoch
86 */
87 localt= ntohl(nett) - RFC_868_BIAS;
88
89 return(localt);
90}
91
92int rdate_main(int argc, char **argv)
93{
94 time_t time;
95 int opt;
96
97 /* Interpret command line args */
98 /* do special-case option parsing */
99 if (argv[1] && (strcmp(argv[1], "--help") == 0))
Eric Andersen67991cf2001-02-14 21:23:06 +0000100 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000101
102 /* do normal option parsing */
103 while ((opt = getopt(argc, argv, "Hsp")) > 0) {
104 switch (opt) {
105 default:
106 case 'H':
Eric Andersen67991cf2001-02-14 21:23:06 +0000107 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000108 break;
109 case 's':
110 setdate++;
111 break;
112 case 'p':
113 printdate++;
114 break;
115 }
116 }
117
118 /* the default action is to set the date */
119 if (printdate==0 && setdate==0) setdate++;
120
121 if (optind == argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +0000122 show_usage();
Eric Andersen6ab22022000-08-21 23:04:00 +0000123 }
124
125 if ((time= askremotedate(argv[optind])) == (time_t)-1) {
Matt Kraai3e856ce2000-12-01 02:55:13 +0000126 return EXIT_FAILURE;
Eric Andersen6ab22022000-08-21 23:04:00 +0000127 }
128 if (setdate) {
129 if (stime(&time) < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000130 perror_msg_and_die("Could not set time of day");
Eric Andersen6ab22022000-08-21 23:04:00 +0000131 }
132 if (printdate) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000133 printf("%s", ctime(&time));
Eric Andersen6ab22022000-08-21 23:04:00 +0000134 }
135
Matt Kraai3e856ce2000-12-01 02:55:13 +0000136 return EXIT_SUCCESS;
Eric Andersen6ab22022000-08-21 23:04:00 +0000137}