xref: /netbsd-src/usr.sbin/rdate/rdate.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*
2  * Copyright (c) 1994 Christos Zoulas
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Christos Zoulas.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *	$Id: rdate.c,v 1.1 1994/06/02 22:55:07 deraadt Exp $
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
41 static char rcsid[] = "$Id: rdate.c,v 1.1 1994/06/02 22:55:07 deraadt 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 
61 int
62 main(argc, argv)
63 	int             argc;
64 	char           *argv[];
65 {
66 	int             pr = 0, silent = 0, s;
67 	time_t          tim;
68 	char           *hname;
69 	struct hostent *hp;
70 	struct protoent *pp, ppp;
71 	struct servent *sp, ssp;
72 	struct sockaddr_in sa;
73 	extern char    *__progname;
74 	extern int      optind;
75 	int             c;
76 
77 	while ((c = getopt(argc, argv, "ps")) != -1)
78 		switch (c) {
79 		case 'p':
80 			pr++;
81 			break;
82 
83 		case 's':
84 			silent++;
85 			break;
86 
87 		default:
88 			goto usage;
89 		}
90 
91 	if (argc - 1 != optind) {
92 usage:
93 		(void) fprintf(stderr, "Usage: %s [-ps] host\n", __progname);
94 		return (1);
95 	}
96 	hname = argv[optind];
97 
98 	if ((hp = gethostbyname(hname)) == NULL) {
99 		fprintf(stderr, "%s: ", __progname);
100 		herror(hname);
101 		exit(1);
102 	}
103 
104 	if ((sp = getservbyname("time", "tcp")) == NULL) {
105 		sp = &ssp;
106 		sp->s_port = 37;
107 		sp->s_proto = "tcp";
108 	}
109 	if ((pp = getprotobyname(sp->s_proto)) == NULL) {
110 		pp = &ppp;
111 		pp->p_proto = 6;
112 	}
113 	if ((s = socket(AF_INET, SOCK_STREAM, pp->p_proto)) == -1)
114 		err(1, "Could not create socket");
115 
116 	bzero(&sa, sizeof sa);
117 	sa.sin_family = AF_INET;
118 	sa.sin_port = sp->s_port;
119 
120 	memcpy(&(sa.sin_addr.s_addr), hp->h_addr, hp->h_length);
121 
122 	if (connect(s, (struct sockaddr *) & sa, sizeof(sa)) == -1)
123 		err(1, "Could not connect socket");
124 
125 	if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
126 		err(1, "Could not read data");
127 
128 	(void) close(s);
129 	tim = ntohl(tim) - DIFFERENCE;
130 
131 	if (!pr) {
132 		struct timeval  tv;
133 		tv.tv_sec = tim;
134 		tv.tv_usec = 0;
135 		if (settimeofday(&tv, NULL) == -1)
136 			err(1, "Could not set time of day");
137 	}
138 	if (!silent)
139 		(void) fputs(ctime(&tim), stdout);
140 	return 0;
141 }
142