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