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