rdate.c revision 1.14
1/*	$NetBSD: rdate.c,v 1.14 2002/07/14 01:01:10 wiz 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.14 2002/07/14 01:01:10 wiz 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 <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58#include <util.h>
59
60/* seconds from midnight Jan 1900 - 1970 */
61#define DIFFERENCE 2208988800UL
62
63	int	main(int, char **);
64static	void	usage(void);
65
66static void
67usage(void)
68{
69	(void) fprintf(stderr, "Usage: %s [-psa] host\n", getprogname());
70	(void) fprintf(stderr, "  -p: just print, don't set\n");
71	(void) fprintf(stderr, "  -s: just set, don't print\n");
72	(void) fprintf(stderr, "  -a: use adjtime instead of instant change\n");
73}
74
75int
76main(int argc, char *argv[])
77{
78	int             pr = 0, silent = 0, s;
79	int		slidetime = 0;
80	int		adjustment;
81	time_t          tim;
82	char           *hname, *emsg;
83	struct addrinfo	hints, *res, *res0;
84	int             c;
85	int		error;
86
87	adjustment = 0;
88	while ((c = getopt(argc, argv, "psa")) != -1)
89		switch (c) {
90		case 'p':
91			pr++;
92			break;
93
94		case 's':
95			silent++;
96			break;
97
98		case 'a':
99			slidetime++;
100			break;
101
102		default:
103			usage();
104			return 1;
105		}
106
107	if (argc - 1 != optind) {
108		usage();
109		return 1;
110	}
111	hname = argv[optind];
112
113	memset(&hints, 0, sizeof (hints));
114	hints.ai_family = PF_UNSPEC;
115	hints.ai_socktype = SOCK_STREAM;
116	hints.ai_flags = AI_CANONNAME;
117	error = getaddrinfo(hname, "time", &hints, &res0);
118	if (error)
119		errx(1, "%s: %s", gai_strerror(error), hname);
120
121	for (res = res0, s = -1; res != NULL; res = res->ai_next) {
122		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
123		if (s < 0) {
124			emsg = "socket";
125			continue;
126		}
127
128		if (connect(s, res->ai_addr, res->ai_addrlen)) {
129			close(s);
130			s = -1;
131			emsg = "connect";
132			continue;
133		}
134
135		break;
136	}
137	if (s < 0)
138		err(1, "%s", emsg);
139
140	if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
141		err(1, "Could not read data");
142
143	(void) close(s);
144	tim = ntohl(tim) - DIFFERENCE;
145
146	if (!pr) {
147	    struct timeval  tv;
148	    if (!slidetime) {
149		    logwtmp("|", "date", "");
150		    tv.tv_sec = tim;
151		    tv.tv_usec = 0;
152		    if (settimeofday(&tv, NULL) == -1)
153			    err(1, "Could not set time of day");
154		    logwtmp("{", "date", "");
155	    } else {
156		    struct timeval tv_current;
157		    if (gettimeofday(&tv_current, NULL) == -1)
158			    err(1, "Could not get local time of day");
159		    adjustment = tv.tv_sec = tim - tv_current.tv_sec;
160		    tv.tv_usec = 0;
161		    if (adjtime(&tv, NULL) == -1)
162			    err(1, "Could not adjust time of day");
163	    }
164	}
165
166	if (!silent) {
167		(void) fputs(ctime(&tim), stdout);
168		if (slidetime)
169		    (void) fprintf(stdout,
170				   "%s: adjust local clock by %d seconds\n",
171				   getprogname(), adjustment);
172	}
173	return 0;
174}
175