rdate.c revision 1.5
1/*	$NetBSD: rdate.c,v 1.5 1996/12/08 14:06:38 mycroft 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#ifndef lint
41static char rcsid[] = "$NetBSD: rdate.c,v 1.5 1996/12/08 14:06:38 mycroft Exp $";
42#endif/* lint */
43
44#include <sys/types.h>
45#include <sys/param.h>
46#include <stdio.h>
47#include <ctype.h>
48#include <string.h>
49#include <sys/time.h>
50#include <sys/socket.h>
51#include <netdb.h>
52#include <netinet/in.h>
53
54/* seconds from midnight Jan 1900 - 1970 */
55#if __STDC__
56#define DIFFERENCE 2208988800UL
57#else
58#define DIFFERENCE 2208988800
59#endif
60
61extern char    *__progname;
62
63static void
64usage()
65{
66	(void) fprintf(stderr, "Usage: %s [-psa] host\n", __progname);
67	(void) fprintf(stderr, "  -p: just print, don't set\n");
68	(void) fprintf(stderr, "  -s: just set, don't print\n");
69	(void) fprintf(stderr, "  -a: use adjtime instead of instant change\n");
70}
71
72int
73main(argc, argv)
74	int             argc;
75	char           *argv[];
76{
77	int             pr = 0, silent = 0, s;
78	int		slidetime = 0;
79	int		adjustment;
80	time_t          tim;
81	char           *hname;
82	struct hostent *hp;
83	struct protoent *pp, ppp;
84	struct servent *sp, ssp;
85	struct sockaddr_in sa;
86	extern int      optind;
87	int             c;
88
89	while ((c = getopt(argc, argv, "psa")) != -1)
90		switch (c) {
91		case 'p':
92			pr++;
93			break;
94
95		case 's':
96			silent++;
97			break;
98
99		case 'a':
100			slidetime++;
101			break;
102
103		default:
104			usage();
105			return 1;
106		}
107
108	if (argc - 1 != optind) {
109		usage();
110		return 1;
111	}
112	hname = argv[optind];
113
114	if ((hp = gethostbyname(hname)) == NULL) {
115		warnx("%s: %s\n", hname, hstrerror(h_errno));
116		return 1;
117	}
118
119	if ((sp = getservbyname("time", "tcp")) == NULL) {
120		sp = &ssp;
121		sp->s_port = 37;
122		sp->s_proto = "tcp";
123	}
124	if ((pp = getprotobyname(sp->s_proto)) == NULL) {
125		pp = &ppp;
126		pp->p_proto = 6;
127	}
128	if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1)
129		err(1, "Could not create socket");
130
131	bzero(&sa, sizeof sa);
132	sa.sin_family = AF_INET;
133	sa.sin_port = sp->s_port;
134
135	memcpy(&(sa.sin_addr.s_addr), hp->h_addr, hp->h_length);
136
137	if (connect(s, (struct sockaddr *) & sa, sizeof(sa)) == -1)
138		err(1, "Could not connect socket");
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		    tv.tv_sec = tim;
150		    tv.tv_usec = 0;
151		    if (settimeofday(&tv, NULL) == -1)
152			    err(1, "Could not set time of day");
153	    } else {
154		    struct timeval tv_current;
155		    if (gettimeofday(&tv_current, NULL) == -1)
156			    err(1, "Could not get local time of day");
157		    adjustment = tv.tv_sec = tim - tv_current.tv_sec;
158		    tv.tv_usec = 0;
159		    if (adjtime(&tv, NULL) == -1)
160			    err(1, "Could not adjust time of day");
161	    }
162	}
163
164	if (!silent) {
165		(void) fputs(ctime(&tim), stdout);
166		if (slidetime)
167		    (void) fprintf(stdout,
168				   "%s: adjust local clock by %d seconds\n",
169				   __progname, adjustment);
170	}
171	return 0;
172}
173