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 #pragma ident "%Z%%M% %I% %E% SMI"
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate * rdate - get date from remote machine
44*7c478bd9Sstevel@tonic-gate *
45*7c478bd9Sstevel@tonic-gate * sets time, obtaining value from host
46*7c478bd9Sstevel@tonic-gate * on the tcp/time socket.
47*7c478bd9Sstevel@tonic-gate */
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate #include <signal.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
53*7c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
54*7c478bd9Sstevel@tonic-gate #include <strings.h>
55*7c478bd9Sstevel@tonic-gate #include <unistd.h>
56*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
57*7c478bd9Sstevel@tonic-gate #include <netdb.h>
58*7c478bd9Sstevel@tonic-gate #include <stdio.h>
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate * The timeserver returns with time of day in seconds since
62*7c478bd9Sstevel@tonic-gate * Jan 1, 1900. We must subtract 86400(365*70 + 17) to get time
63*7c478bd9Sstevel@tonic-gate * since Jan 1, 1970, which is what get/settimeofday uses.
64*7c478bd9Sstevel@tonic-gate */
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate #define TOFFSET ((unsigned int)86400*(365*70 + 17))
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate * Before setting the system time, the value returned by the
70*7c478bd9Sstevel@tonic-gate * timeserver is checked for plausibility. If the returned date
71*7c478bd9Sstevel@tonic-gate * is before the time this program was written it cannot be
72*7c478bd9Sstevel@tonic-gate * correct.
73*7c478bd9Sstevel@tonic-gate */
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate #define WRITTEN 440199955 /* 22:45:55 13/Dec/1983 */
76*7c478bd9Sstevel@tonic-gate #define SECONDS_TO_MS 1000
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate static void timeout(int);
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)81*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate int s, i;
84*7c478bd9Sstevel@tonic-gate uint32_t time;
85*7c478bd9Sstevel@tonic-gate struct timeval timestruct;
86*7c478bd9Sstevel@tonic-gate unsigned int connect_timeout;
87*7c478bd9Sstevel@tonic-gate /* number of seconds to wait for something to happen. */
88*7c478bd9Sstevel@tonic-gate unsigned int rdate_timeout = 30; /* seconds */
89*7c478bd9Sstevel@tonic-gate struct addrinfo hints;
90*7c478bd9Sstevel@tonic-gate struct addrinfo *res;
91*7c478bd9Sstevel@tonic-gate int rc;
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate if (argc != 2) {
94*7c478bd9Sstevel@tonic-gate (void) fputs("usage: rdate host\n", stderr);
95*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate
98*7c478bd9Sstevel@tonic-gate (void) memset(&hints, 0, sizeof (hints));
99*7c478bd9Sstevel@tonic-gate hints.ai_protocol = IPPROTO_TCP;
100*7c478bd9Sstevel@tonic-gate res = NULL;
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate * getaddrinfo() may take a long time, because it can involve
104*7c478bd9Sstevel@tonic-gate * NIS, DNS, or LDAP lookups. Set an alarm timer. Note that this
105*7c478bd9Sstevel@tonic-gate * may still fail, depending on how SIGALRM is handled by the
106*7c478bd9Sstevel@tonic-gate * functions invoked by getaddrinfo().
107*7c478bd9Sstevel@tonic-gate */
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate (void) signal(SIGALRM, timeout);
110*7c478bd9Sstevel@tonic-gate (void) alarm(rdate_timeout);
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate * Note: memory not freed due to short lifetime of program.
114*7c478bd9Sstevel@tonic-gate */
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate rc = getaddrinfo(argv[1], "time", &hints, &res);
117*7c478bd9Sstevel@tonic-gate (void) alarm(0);
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate if (rc != 0) {
120*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Host name %s not found: %s\n", argv[1],
121*7c478bd9Sstevel@tonic-gate gai_strerror(rc));
122*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate connect_timeout = rdate_timeout * SECONDS_TO_MS;
126*7c478bd9Sstevel@tonic-gate for (; res != NULL; res = res->ai_next) {
127*7c478bd9Sstevel@tonic-gate s = socket(res->ai_addr->sa_family, res->ai_socktype,
128*7c478bd9Sstevel@tonic-gate res->ai_protocol);
129*7c478bd9Sstevel@tonic-gate if (s < 0) {
130*7c478bd9Sstevel@tonic-gate perror("rdate: socket");
131*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate if (setsockopt(s, IPPROTO_TCP, TCP_CONN_ABORT_THRESHOLD,
135*7c478bd9Sstevel@tonic-gate (char *)&connect_timeout, sizeof (connect_timeout)) == -1) {
136*7c478bd9Sstevel@tonic-gate perror("setsockopt TCP_CONN_ABORT_THRESHOLD");
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate if (connect(s, res->ai_addr, res->ai_addrlen) >= 0)
140*7c478bd9Sstevel@tonic-gate break;
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate if (res->ai_next == NULL) {
143*7c478bd9Sstevel@tonic-gate perror("rdate: connect");
144*7c478bd9Sstevel@tonic-gate (void) close(s);
145*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate (void) close(s);
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate (void) alarm(rdate_timeout);
152*7c478bd9Sstevel@tonic-gate if (read(s, (char *)&time, sizeof (time)) != sizeof (time)) {
153*7c478bd9Sstevel@tonic-gate perror("rdate: read");
154*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate (void) alarm(0);
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate time = ntohl(time) - TOFFSET;
159*7c478bd9Sstevel@tonic-gate /* date must be later than when program was written */
160*7c478bd9Sstevel@tonic-gate if (time < WRITTEN) {
161*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "didn't get plausible time from %s\n",
162*7c478bd9Sstevel@tonic-gate argv[1]);
163*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate timestruct.tv_usec = 0;
166*7c478bd9Sstevel@tonic-gate timestruct.tv_sec = time;
167*7c478bd9Sstevel@tonic-gate i = settimeofday(×truct, 0);
168*7c478bd9Sstevel@tonic-gate if (i == -1) {
169*7c478bd9Sstevel@tonic-gate perror("couldn't set time of day");
170*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
171*7c478bd9Sstevel@tonic-gate } else {
172*7c478bd9Sstevel@tonic-gate (void) printf("%s", ctime(×truct.tv_sec));
173*7c478bd9Sstevel@tonic-gate #if defined(i386)
174*7c478bd9Sstevel@tonic-gate (void) system("/usr/sbin/rtc -c > /dev/null 2>&1");
175*7c478bd9Sstevel@tonic-gate #endif
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate return (EXIT_SUCCESS);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
181*7c478bd9Sstevel@tonic-gate static void
timeout(int sig)182*7c478bd9Sstevel@tonic-gate timeout(int sig)
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate (void) fputs("couldn't contact time server\n", stderr);
185*7c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
186*7c478bd9Sstevel@tonic-gate /* NOTREACHED */
187*7c478bd9Sstevel@tonic-gate }
188