xref: /openbsd/usr.sbin/rdate/rdate.c (revision 73471bf0)
1 /*	$OpenBSD: rdate.c,v 1.36 2021/11/15 15:14:24 millert Exp $	*/
2 /*	$NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $	*/
3 
4 /*
5  * Copyright (c) 1994 Christos Zoulas
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Christos Zoulas.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * rdate.c: Set the date from the specified host
36  *
37  *	Time is returned as the number of seconds since
38  *	midnight January 1st 1900.
39  */
40 
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/wait.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <time.h>
52 
53 /* there are systems without libutil; for portability */
54 #ifndef NO_UTIL
55 #include <util.h>
56 #else
57 #define logwtmp(a,b,c)
58 #endif
59 
60 void rfc868time_client(const char *, int, struct timeval *, struct timeval *, int);
61 void ntp_client(const char *, int, struct timeval *, struct timeval *, int);
62 
63 extern char    *__progname;
64 __dead void	usage(void);
65 
66 struct {
67 	char message[2048];
68 	struct timeval new;
69 	struct timeval adjust;
70 } pdata;
71 
72 __dead void
73 usage(void)
74 {
75 	(void) fprintf(stderr, "usage: %s [-46acnopsv] host\n", __progname);
76 	exit(1);
77 }
78 
79 int
80 main(int argc, char **argv)
81 {
82 	int             pr = 0, silent = 0, ntp = 1, verbose = 0;
83 	int		slidetime = 0, corrleaps = 0;
84 	char           *hname;
85 	int             c, p[2], pid;
86 	int		family = PF_UNSPEC;
87 
88 	while ((c = getopt(argc, argv, "46psanocv")) != -1) {
89 		switch (c) {
90 		case '4':
91 			family = PF_INET;
92 			break;
93 
94 		case '6':
95 			family = PF_INET6;
96 			break;
97 
98 		case 'p':
99 			pr = 1;
100 			break;
101 
102 		case 's':
103 			silent = 1;
104 			break;
105 
106 		case 'a':
107 			slidetime = 1;
108 			break;
109 
110 		case 'n':
111 			ntp = 1;
112 			break;
113 
114 		case 'o':
115 			ntp = 0;
116 			break;
117 
118 		case 'c':
119 			corrleaps = 1;
120 			break;
121 
122 		case 'v':
123 			verbose = 1;
124 			break;
125 
126 		default:
127 			usage();
128 		}
129 	}
130 	if (argc - 1 != optind)
131 		usage();
132 	hname = argv[optind];
133 
134 	/*
135 	 * Privilege separation increases safety, with a slight reduction
136 	 * in precision because the time values have to return over a pipe.
137 	 */
138 	if (pipe(p) == -1)
139 		err(1, "pipe");
140 	switch ((pid = fork())) {
141 	case -1:
142 		err(1, "fork");
143 		break;
144 	case 0:
145 		if (pledge("stdio inet dns", NULL) == -1)
146 			err(1, "pledge");
147 
148 		close(p[0]);	/* read side of pipe */
149 		dup2(p[1], STDIN_FILENO);
150 		if (p[1] != STDIN_FILENO)
151 			close(p[1]);
152 		dup2(STDIN_FILENO, STDOUT_FILENO);
153 		dup2(STDOUT_FILENO, STDERR_FILENO);
154 		setvbuf(stdout, NULL, _IOFBF, 0);
155 		setvbuf(stderr, NULL, _IOFBF, 0);
156 
157 		if (ntp)
158 			ntp_client(hname, family, &pdata.new,
159 			    &pdata.adjust, corrleaps);
160 		else
161 			rfc868time_client(hname, family, &pdata.new,
162 			    &pdata.adjust, corrleaps);
163 
164 		if (write(STDOUT_FILENO, &pdata, sizeof pdata) != sizeof pdata)
165 			exit(1);
166 		exit(0);
167 	}
168 
169 	if (pledge("stdio rpath wpath settime", NULL) == -1)
170 		err(1, "pledge");
171 
172 	close(p[1]);	/* write side of pipe */
173 	if (read(p[0], &pdata, sizeof pdata) < 1)
174 		err(1, "child did not collect time");
175 	if (waitpid(pid, NULL, 0) == -1)
176 		err(1, "waitpid");
177 
178 	/*
179 	 * A viable timestamp from the child contains no message.
180 	 */
181 	if (pdata.message[0]) {
182 		pdata.message[sizeof(pdata.message)- 1] = '\0';
183 		write(STDERR_FILENO, pdata.message, strlen(pdata.message));
184 		exit(1);
185 	}
186 
187 	if (!pr) {
188 		if (!slidetime) {
189 			logwtmp("|", "date", "");
190 			if (settimeofday(&pdata.new, NULL) == -1)
191 				err(1, "Could not set time of day");
192 			logwtmp("{", "date", "");
193 		} else {
194 			if (adjtime(&pdata.adjust, NULL) == -1)
195 				err(1, "Could not adjust time of day");
196 		}
197 	}
198 
199 	if (pledge("stdio rpath", NULL) == -1)
200 		err(1, "pledge");
201 
202 	if (!silent) {
203 		struct tm      *ltm;
204 		char		buf[80];
205 		time_t		tim = pdata.new.tv_sec;
206 		double		adjsec;
207 
208 		ltm = localtime(&tim);
209 		(void) strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y\n", ltm);
210 		(void) fputs(buf, stdout);
211 
212 		adjsec  = pdata.adjust.tv_sec + pdata.adjust.tv_usec / 1.0e6;
213 
214 		if (slidetime || verbose) {
215 			if (ntp)
216 				(void) fprintf(stdout,
217 				   "%s: adjust local clock by %.6f seconds\n",
218 				   __progname, adjsec);
219 			else
220 				(void) fprintf(stdout,
221 				   "%s: adjust local clock by %lld seconds\n",
222 				   __progname, (long long)pdata.adjust.tv_sec);
223 		}
224 	}
225 
226 	return 0;
227 }
228