xref: /netbsd-src/usr.sbin/rdate/rdate.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: rdate.c,v 1.10 2000/02/05 22:14:20 kleink Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Christos Zoulas
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Christos Zoulas.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * rdate.c: Set the date from the specified host
35  *
36  * 	Uses the rfc868 time protocol at socket 37.
37  *	Time is returned as the number of seconds since
38  *	midnight January 1st 1900.
39  */
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __RCSID("$NetBSD: rdate.c,v 1.10 2000/02/05 22:14:20 kleink Exp $");
43 #endif/* lint */
44 
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 
49 #include <netinet/in.h>
50 
51 #include <ctype.h>
52 #include <err.h>
53 #include <netdb.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <util.h>
58 
59 /* seconds from midnight Jan 1900 - 1970 */
60 #if __STDC__
61 #define DIFFERENCE 2208988800UL
62 #else
63 #define DIFFERENCE 2208988800
64 #endif
65 
66 extern char    *__progname;
67 
68 	int	main __P((int, char **));
69 static	void	usage __P((void));
70 
71 static void
72 usage()
73 {
74 	(void) fprintf(stderr, "Usage: %s [-psa] host\n", __progname);
75 	(void) fprintf(stderr, "  -p: just print, don't set\n");
76 	(void) fprintf(stderr, "  -s: just set, don't print\n");
77 	(void) fprintf(stderr, "  -a: use adjtime instead of instant change\n");
78 }
79 
80 int
81 main(argc, argv)
82 	int             argc;
83 	char           *argv[];
84 {
85 	int             pr = 0, silent = 0, s;
86 	int		slidetime = 0;
87 	int		adjustment;
88 	time_t          tim;
89 	char           *hname, *emsg;
90 	struct addrinfo	hints, *res, *res0;
91 	int             c;
92 	int		error;
93 
94 	adjustment = 0;
95 	while ((c = getopt(argc, argv, "psa")) != -1)
96 		switch (c) {
97 		case 'p':
98 			pr++;
99 			break;
100 
101 		case 's':
102 			silent++;
103 			break;
104 
105 		case 'a':
106 			slidetime++;
107 			break;
108 
109 		default:
110 			usage();
111 			return 1;
112 		}
113 
114 	if (argc - 1 != optind) {
115 		usage();
116 		return 1;
117 	}
118 	hname = argv[optind];
119 
120 	memset(&hints, 0, sizeof (hints));
121 	hints.ai_family = PF_UNSPEC;
122 	hints.ai_socktype = SOCK_STREAM;
123 	hints.ai_flags = AI_CANONNAME;
124 	error = getaddrinfo(hname, "time", &hints, &res0);
125 	if (error)
126 		errx(1, "%s: %s", gai_strerror(error), hname);
127 
128 	for (res = res0, s = -1; res != NULL; res = res->ai_next) {
129 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
130 		if (s < 0) {
131 			emsg = "socket";
132 			continue;
133 		}
134 
135 		if (connect(s, res->ai_addr, res->ai_addrlen)) {
136 			close(s);
137 			s = -1;
138 			emsg = "connect";
139 			continue;
140 		}
141 
142 		break;
143 	}
144 	if (s < 0)
145 		err(1, emsg);
146 
147 	if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
148 		err(1, "Could not read data");
149 
150 	(void) close(s);
151 	tim = ntohl(tim) - DIFFERENCE;
152 
153 	if (!pr) {
154 	    struct timeval  tv;
155 	    if (!slidetime) {
156 		    logwtmp("|", "date", "");
157 		    tv.tv_sec = tim;
158 		    tv.tv_usec = 0;
159 		    if (settimeofday(&tv, NULL) == -1)
160 			    err(1, "Could not set time of day");
161 		    logwtmp("{", "date", "");
162 	    } else {
163 		    struct timeval tv_current;
164 		    if (gettimeofday(&tv_current, NULL) == -1)
165 			    err(1, "Could not get local time of day");
166 		    adjustment = tv.tv_sec = tim - tv_current.tv_sec;
167 		    tv.tv_usec = 0;
168 		    if (adjtime(&tv, NULL) == -1)
169 			    err(1, "Could not adjust time of day");
170 	    }
171 	}
172 
173 	if (!silent) {
174 		(void) fputs(ctime(&tim), stdout);
175 		if (slidetime)
176 		    (void) fprintf(stdout,
177 				   "%s: adjust local clock by %d seconds\n",
178 				   __progname, adjustment);
179 	}
180 	return 0;
181 }
182