xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/rdate.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  *  rdate - get date from remote machine
44*0Sstevel@tonic-gate  *
45*0Sstevel@tonic-gate  *	sets time, obtaining value from host
46*0Sstevel@tonic-gate  *	on the tcp/time socket.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include <signal.h>
50*0Sstevel@tonic-gate #include <sys/types.h>
51*0Sstevel@tonic-gate #include <sys/time.h>
52*0Sstevel@tonic-gate #include <sys/socket.h>
53*0Sstevel@tonic-gate #include <netinet/tcp.h>
54*0Sstevel@tonic-gate #include <strings.h>
55*0Sstevel@tonic-gate #include <unistd.h>
56*0Sstevel@tonic-gate #include <stdlib.h>
57*0Sstevel@tonic-gate #include <netdb.h>
58*0Sstevel@tonic-gate #include <stdio.h>
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * The timeserver returns with time of day in seconds since
62*0Sstevel@tonic-gate  * Jan 1, 1900. We must subtract 86400(365*70 + 17) to get time
63*0Sstevel@tonic-gate  * since Jan 1, 1970, which is what get/settimeofday uses.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #define	TOFFSET	((unsigned int)86400*(365*70 + 17))
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * Before setting the system time, the value returned by the
70*0Sstevel@tonic-gate  * timeserver is checked for plausibility. If the returned date
71*0Sstevel@tonic-gate  * is before the time this program was written it cannot be
72*0Sstevel@tonic-gate  * correct.
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #define	WRITTEN 440199955		/* 22:45:55 13/Dec/1983 */
76*0Sstevel@tonic-gate #define	SECONDS_TO_MS	1000
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static void timeout(int);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int
main(int argc,char ** argv)81*0Sstevel@tonic-gate main(int argc, char **argv)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate 	int s, i;
84*0Sstevel@tonic-gate 	uint32_t time;
85*0Sstevel@tonic-gate 	struct timeval timestruct;
86*0Sstevel@tonic-gate 	unsigned int connect_timeout;
87*0Sstevel@tonic-gate 	/* number of seconds to wait for something to happen. */
88*0Sstevel@tonic-gate 	unsigned int rdate_timeout = 30;	/* seconds */
89*0Sstevel@tonic-gate 	struct addrinfo hints;
90*0Sstevel@tonic-gate 	struct addrinfo *res;
91*0Sstevel@tonic-gate 	int rc;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	if (argc != 2) {
94*0Sstevel@tonic-gate 		(void) fputs("usage: rdate host\n", stderr);
95*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
99*0Sstevel@tonic-gate 	hints.ai_protocol = IPPROTO_TCP;
100*0Sstevel@tonic-gate 	res = NULL;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	/*
103*0Sstevel@tonic-gate 	 * getaddrinfo() may take a long time, because it can involve
104*0Sstevel@tonic-gate 	 * NIS, DNS, or LDAP lookups. Set an alarm timer. Note that this
105*0Sstevel@tonic-gate 	 * may still fail, depending on how SIGALRM is handled by the
106*0Sstevel@tonic-gate 	 * functions invoked by getaddrinfo().
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	(void) signal(SIGALRM, timeout);
110*0Sstevel@tonic-gate 	(void) alarm(rdate_timeout);
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * Note: memory not freed due to short lifetime of program.
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	rc = getaddrinfo(argv[1], "time", &hints, &res);
117*0Sstevel@tonic-gate 	(void) alarm(0);
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	if (rc != 0) {
120*0Sstevel@tonic-gate 		(void) fprintf(stderr, "Host name %s not found: %s\n", argv[1],
121*0Sstevel@tonic-gate 		    gai_strerror(rc));
122*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	connect_timeout = rdate_timeout * SECONDS_TO_MS;
126*0Sstevel@tonic-gate 	for (; res != NULL; res = res->ai_next) {
127*0Sstevel@tonic-gate 		s = socket(res->ai_addr->sa_family, res->ai_socktype,
128*0Sstevel@tonic-gate 		    res->ai_protocol);
129*0Sstevel@tonic-gate 		if (s < 0) {
130*0Sstevel@tonic-gate 			perror("rdate: socket");
131*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		if (setsockopt(s, IPPROTO_TCP, TCP_CONN_ABORT_THRESHOLD,
135*0Sstevel@tonic-gate 		    (char *)&connect_timeout, sizeof (connect_timeout)) == -1) {
136*0Sstevel@tonic-gate 			perror("setsockopt TCP_CONN_ABORT_THRESHOLD");
137*0Sstevel@tonic-gate 		}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 		if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
140*0Sstevel@tonic-gate 			break;
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 		if (res->ai_next == NULL) {
143*0Sstevel@tonic-gate 			perror("rdate: connect");
144*0Sstevel@tonic-gate 			(void) close(s);
145*0Sstevel@tonic-gate 			exit(EXIT_FAILURE);
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		(void) close(s);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	(void) alarm(rdate_timeout);
152*0Sstevel@tonic-gate 	if (read(s, (char *)&time, sizeof (time)) != sizeof (time)) {
153*0Sstevel@tonic-gate 		perror("rdate: read");
154*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate 	(void) alarm(0);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	time = ntohl(time) - TOFFSET;
159*0Sstevel@tonic-gate 	/* date must be later than when program was written */
160*0Sstevel@tonic-gate 	if (time < WRITTEN) {
161*0Sstevel@tonic-gate 		(void) fprintf(stderr, "didn't get plausible time from %s\n",
162*0Sstevel@tonic-gate 		    argv[1]);
163*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 	timestruct.tv_usec = 0;
166*0Sstevel@tonic-gate 	timestruct.tv_sec = time;
167*0Sstevel@tonic-gate 	i = settimeofday(&timestruct, 0);
168*0Sstevel@tonic-gate 	if (i == -1) {
169*0Sstevel@tonic-gate 		perror("couldn't set time of day");
170*0Sstevel@tonic-gate 		exit(EXIT_FAILURE);
171*0Sstevel@tonic-gate 	} else {
172*0Sstevel@tonic-gate 		(void) printf("%s", ctime(&timestruct.tv_sec));
173*0Sstevel@tonic-gate #if defined(i386)
174*0Sstevel@tonic-gate 		(void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
175*0Sstevel@tonic-gate #endif
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 	return (EXIT_SUCCESS);
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate /*ARGSUSED*/
181*0Sstevel@tonic-gate static void
timeout(int sig)182*0Sstevel@tonic-gate timeout(int sig)
183*0Sstevel@tonic-gate {
184*0Sstevel@tonic-gate 	(void) fputs("couldn't contact time server\n", stderr);
185*0Sstevel@tonic-gate 	exit(EXIT_FAILURE);
186*0Sstevel@tonic-gate 	/* NOTREACHED */
187*0Sstevel@tonic-gate }
188