xref: /openbsd/usr.sbin/rdate/rfc868time.c (revision 4cfece93)
1 /*	$OpenBSD: rfc868time.c,v 1.12 2019/06/28 13:32:50 deraadt 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  *	Uses the rfc868 time protocol at socket 37 (tcp).
38  *	Time is returned as the number of seconds since
39  *	midnight January 1st 1900.
40  */
41 
42 #include <sys/socket.h>
43 #include <sys/time.h>
44 #include <netinet/in.h>
45 
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <string.h>
50 #include <netdb.h>
51 #include <unistd.h>
52 #include <time.h>
53 
54 /* Obviously it is not just for SNTP clients... */
55 #include "ntpleaps.h"
56 
57 /* seconds from midnight Jan 1900 - 1970 */
58 #define DIFFERENCE 2208988800UL
59 
60 void
61 rfc868time_client(const char *hostname, int family, struct timeval *new,
62     struct timeval *adjust, int leapflag);
63 
64 
65 void
66 rfc868time_client(const char *hostname, int family, struct timeval *new,
67     struct timeval *adjust, int leapflag)
68 {
69 	struct addrinfo hints, *res0, *res;
70 	struct timeval old;
71 	u_int32_t tim;	/* RFC 868 states clearly this is an uint32 */
72 	int s;
73 	int error;
74 	u_int64_t td;
75 
76 	memset(&hints, 0, sizeof(hints));
77 	hints.ai_family = family;
78 	hints.ai_socktype = SOCK_STREAM;
79 	error = getaddrinfo(hostname, "time", &hints, &res0);
80 	if (error) {
81 		errx(1, "%s: %s", hostname, gai_strerror(error));
82 		/*NOTREACHED*/
83 	}
84 
85 	if (pledge("stdio inet", NULL) == -1)
86 		err(1, "pledge");
87 
88 	s = -1;
89 	for (res = res0; res; res = res->ai_next) {
90 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
91 		if (s == -1)
92 			continue;
93 
94 		if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
95 			close(s);
96 			s = -1;
97 			continue;
98 		}
99 
100 		break;
101 	}
102 	if (s == -1)
103 		err(1, "Could not connect socket");
104 	freeaddrinfo(res0);
105 
106 	if (read(s, &tim, sizeof(tim)) != sizeof(tim))
107 		err(1, "Could not read data");
108 
109 	(void) close(s);
110 	tim = ntohl(tim) - DIFFERENCE;
111 
112 	if (gettimeofday(&old, NULL) == -1)
113 		err(1, "Could not get local time of day");
114 
115 	td = SEC_TO_TAI64(old.tv_sec);
116 	if (leapflag)
117 		ntpleaps_sub(&td);
118 
119 	adjust->tv_sec = tim - TAI64_TO_SEC(td);
120 	adjust->tv_usec = 0;
121 
122 	new->tv_sec = old.tv_sec + adjust->tv_sec;
123 	new->tv_usec = 0;
124 }
125