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