]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/timed/timedc/cmds.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / timed / timedc / cmds.c
1 /*-
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)cmds.c      8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
34 #endif
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "timedc.h"
40 #include <sys/file.h>
41
42 #include <arpa/inet.h>
43
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_icmp.h>
47
48 #include <err.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #define TSPTYPES
54 #include <protocols/timed.h>
55
56 #define SECHR   (60*60)
57 #define SECDAY  (24*SECHR)
58
59 # define DATE_PROTO "udp"
60 # define DATE_PORT "time"
61
62
63 int sock;
64 int sock_raw;
65 char myname[MAXHOSTNAMELEN];
66 struct hostent *hp;
67 struct sockaddr_in server;
68 struct sockaddr_in dayaddr;
69 extern int measure_delta;
70
71 void bytenetorder(struct tsp *);
72 void bytehostorder(struct tsp *);
73
74
75 #define BU (2208988800UL)       /* seconds before UNIX epoch */
76
77
78 /* compute the difference between our date and another machine
79  */
80 static int                              /* difference in days from our time */
81 daydiff(hostname)
82         char *hostname;
83 {
84         int i;
85         int trials;
86         struct timeval tout, now;
87         fd_set ready;
88         struct sockaddr from;
89         int fromlen;
90         unsigned long sec;
91
92
93         /* wait 2 seconds between 10 tries */
94         tout.tv_sec = 2;
95         tout.tv_usec = 0;
96         for (trials = 0; trials < 10; trials++) {
97                 /* ask for the time */
98                 sec = 0;
99                 if (sendto(sock, &sec, sizeof(sec), 0,
100                            (struct sockaddr*)&dayaddr, sizeof(dayaddr)) < 0) {
101                         warn("sendto(sock)");
102                         return 0;
103                 }
104
105                 for (;;) {
106                         FD_ZERO(&ready);
107                         FD_SET(sock, &ready);
108                         i = select(sock+1, &ready, (fd_set *)0,
109                                    (fd_set *)0, &tout);
110                         if (i < 0) {
111                                 if (errno == EINTR)
112                                         continue;
113                                 warn("select(date read)");
114                                 return 0;
115                         }
116                         if (0 == i)
117                                 break;
118
119                         fromlen = sizeof(from);
120                         if (recvfrom(sock,&sec,sizeof(sec),0,
121                                      &from,&fromlen) < 0) {
122                                 warn("recvfrom(date read)");
123                                 return 0;
124                         }
125
126                         sec = ntohl(sec);
127                         if (sec < BU) {
128                                 warnx("%s says it is before 1970: %lu",
129                                         hostname, sec);
130                                 return 0;
131                         }
132                         sec -= BU;
133
134                         (void)gettimeofday(&now, (struct timezone*)0);
135                         return (sec - now.tv_sec);
136                 }
137         }
138
139         /* if we get here, we tried too many times */
140         warnx("%s will not tell us the date", hostname);
141         return 0;
142 }
143
144
145 /*
146  * Clockdiff computes the difference between the time of the machine on
147  * which it is called and the time of the machines given as argument.
148  * The time differences measured by clockdiff are obtained using a sequence
149  * of ICMP TSTAMP messages which are returned to the sender by the IP module
150  * in the remote machine.
151  * In order to compare clocks of machines in different time zones, the time
152  * is transmitted (as a 32-bit value) in milliseconds since midnight UT.
153  * If a hosts uses a different time format, it should set the high order
154  * bit of the 32-bit quantity it transmits.
155  * However, VMS apparently transmits the time in milliseconds since midnight
156  * local time (rather than GMT) without setting the high order bit.
157  * Furthermore, it does not understand daylight-saving time.  This makes
158  * clockdiff behaving inconsistently with hosts running VMS.
159  *
160  * In order to reduce the sensitivity to the variance of message transmission
161  * time, clockdiff sends a sequence of messages.  Yet, measures between
162  * two `distant' hosts can be affected by a small error. The error can,
163  * however, be reduced by increasing the number of messages sent in each
164  * measurement.
165  */
166 void
167 clockdiff(argc, argv)
168         int argc;
169         char *argv[];
170 {
171         int measure_status;
172         extern int measure(u_long, u_long, char *, struct sockaddr_in*, int);
173         register int avg_cnt;
174         register long avg;
175         struct servent *sp;
176
177         if (argc < 2)  {
178                 printf("usage: timedc clockdiff host ...\n");
179                 return;
180         }
181
182         if (gethostname(myname, sizeof(myname) - 1) < 0)
183                 err(1, "gethostname");
184
185         /* get the address for the date ready */
186         sp = getservbyname(DATE_PORT, DATE_PROTO);
187         if (!sp) {
188                 warnx("%s/%s: unknown service", DATE_PORT, DATE_PROTO);
189                 dayaddr.sin_port = 0;
190         } else {
191                 dayaddr.sin_port = sp->s_port;
192         }
193
194         while (argc > 1) {
195                 argc--; argv++;
196                 hp = gethostbyname(*argv);
197                 if (hp == NULL) {
198                         warnx("%s: %s", *argv, hstrerror(h_errno));
199                         continue;
200                 }
201
202                 server.sin_family = hp->h_addrtype;
203                 bcopy(hp->h_addr, &server.sin_addr.s_addr, hp->h_length);
204                 for (avg_cnt = 0, avg = 0; avg_cnt < 16; avg_cnt++) {
205                         measure_status = measure(10000,100, *argv, &server, 1);
206                         if (measure_status != GOOD)
207                                 break;
208                         avg += measure_delta;
209                 }
210                 if (measure_status == GOOD)
211                         measure_delta = avg/avg_cnt;
212
213                 switch (measure_status) {
214                 case HOSTDOWN:
215                         printf("%s is down\n", hp->h_name);
216                         continue;
217                 case NONSTDTIME:
218                         printf("%s transmits a non-standard time format\n",
219                                hp->h_name);
220                         continue;
221                 case UNREACHABLE:
222                         printf("%s is unreachable\n", hp->h_name);
223                         continue;
224                 }
225
226                 /*
227                  * Try to get the date only after using ICMP timestamps to
228                  * get the time.  This is because the date protocol
229                  * is optional.
230                  */
231                 if (dayaddr.sin_port != 0) {
232                         dayaddr.sin_family = hp->h_addrtype;
233                         bcopy(hp->h_addr, &dayaddr.sin_addr.s_addr,
234                               hp->h_length);
235                         avg = daydiff(*argv);
236                         if (avg > SECDAY) {
237                                 printf("time on %s is %ld days ahead %s\n",
238                                        hp->h_name, avg/SECDAY, myname);
239                                 continue;
240                         } else if (avg < -SECDAY) {
241                                 printf("time on %s is %ld days behind %s\n",
242                                        hp->h_name, -avg/SECDAY, myname);
243                                 continue;
244                         }
245                 }
246
247                 if (measure_delta > 0) {
248                         printf("time on %s is %d ms. ahead of time on %s\n",
249                                hp->h_name, measure_delta, myname);
250                 } else if (measure_delta == 0) {
251                         printf("%s and %s have the same time\n",
252                                hp->h_name, myname);
253                 } else {
254                         printf("time on %s is %d ms. behind time on %s\n",
255                                hp->h_name, -measure_delta, myname);
256                 }
257         }
258         return;
259 }
260
261
262 /*
263  * finds location of master timedaemon
264  */
265 void
266 msite(argc, argv)
267         int argc;
268         char *argv[];
269 {
270         ssize_t cc;
271         fd_set ready;
272         struct sockaddr_in dest;
273         int i, length;
274         struct sockaddr_in from;
275         struct timeval tout;
276         struct tsp msg;
277         struct servent *srvp;
278         char *tgtname;
279
280         if (argc < 1) {
281                 printf("usage: timedc msite [host ...]\n");
282                 return;
283         }
284
285         srvp = getservbyname("timed", "udp");
286         if (srvp == 0) {
287                 warnx("timed/udp: unknown service");
288                 return;
289         }
290         dest.sin_port = srvp->s_port;
291         dest.sin_family = AF_INET;
292
293         if (gethostname(myname, sizeof(myname) - 1) < 0)
294                 err(1, "gethostname");
295         i = 1;
296         do {
297                 tgtname = (i >= argc) ? myname : argv[i];
298                 hp = gethostbyname(tgtname);
299                 if (hp == 0) {
300                         warnx("%s: %s", tgtname, hstrerror(h_errno));
301                         continue;
302                 }
303                 bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
304
305                 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
306                 msg.tsp_type = TSP_MSITE;
307                 msg.tsp_vers = TSPVERSION;
308                 bytenetorder(&msg);
309                 if (sendto(sock, &msg, sizeof(struct tsp), 0,
310                            (struct sockaddr*)&dest,
311                            sizeof(struct sockaddr)) < 0) {
312                         warn("sendto");
313                         continue;
314                 }
315
316                 tout.tv_sec = 15;
317                 tout.tv_usec = 0;
318                 FD_ZERO(&ready);
319                 FD_SET(sock, &ready);
320                 if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0,
321                            &tout)) {
322                         length = sizeof(from);
323                         cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
324                                       (struct sockaddr *)&from, &length);
325                         if (cc < 0) {
326                                 warn("recvfrom");
327                                 continue;
328                         }
329                         /*
330                          * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
331                          * this is still OS-dependent.  Demand that the packet is at
332                          * least long enough to hold a 4.3BSD packet.
333                          */
334                         if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
335                                 fprintf(stderr, 
336                                    "short packet (%zd/%zu bytes) from %s\n",
337                                    cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
338                                    inet_ntoa(from.sin_addr));
339                                 continue;
340                         }
341                         bytehostorder(&msg);
342                         if (msg.tsp_type == TSP_ACK) {
343                                 printf("master timedaemon at %s is %s\n",
344                                        tgtname, msg.tsp_name);
345                         } else {
346                                 if (msg.tsp_type >= TSPTYPENUMBER)
347                                         printf("unknown ack received: %u\n",
348                                                 msg.tsp_type);
349                                 else    
350                                         printf("wrong ack received: %s\n",
351                                                 tsptype[msg.tsp_type]);
352                         }
353                 } else {
354                         printf("communication error with %s\n", tgtname);
355                 }
356         } while (++i < argc);
357 }
358
359 /*
360  * quits timedc
361  */
362 void
363 quit()
364 {
365         exit(0);
366 }
367
368
369 /*
370  * Causes the election timer to expire on the selected hosts
371  * It sends just one udp message per machine, relying on
372  * reliability of communication channel.
373  */
374 void
375 testing(argc, argv)
376         int argc;
377         char *argv[];
378 {
379         struct servent *srvp;
380         struct sockaddr_in sin;
381         struct tsp msg;
382
383         if (argc < 2)  {
384                 printf("usage: timedc election host1 [host2 ...]\n");
385                 return;
386         }
387
388         srvp = getservbyname("timed", "udp");
389         if (srvp == 0) {
390                 warnx("timed/udp: unknown service");
391                 return;
392         }
393
394         while (argc > 1) {
395                 argc--; argv++;
396                 hp = gethostbyname(*argv);
397                 if (hp == NULL) {
398                         warnx("%s: %s", *argv, hstrerror(h_errno));
399                         argc--; argv++;
400                         continue;
401                 }
402                 sin.sin_port = srvp->s_port;
403                 sin.sin_family = hp->h_addrtype;
404                 bcopy(hp->h_addr, &sin.sin_addr.s_addr, hp->h_length);
405
406                 msg.tsp_type = TSP_TEST;
407                 msg.tsp_vers = TSPVERSION;
408                 if (gethostname(myname, sizeof(myname) - 1) < 0)
409                         err(1, "gethostname");
410                 (void)strlcpy(msg.tsp_name, myname, sizeof(msg.tsp_name));
411                 bytenetorder(&msg);
412                 if (sendto(sock, &msg, sizeof(struct tsp), 0,
413                            (struct sockaddr*)&sin,
414                            sizeof(struct sockaddr)) < 0) {
415                         warn("sendto");
416                 }
417         }
418 }
419
420
421 /*
422  * Enables or disables tracing on local timedaemon
423  */
424 void
425 tracing(argc, argv)
426         int argc;
427         char *argv[];
428 {
429         int onflag;
430         int length;
431         ssize_t cc;
432         fd_set ready;
433         struct sockaddr_in dest;
434         struct sockaddr_in from;
435         struct timeval tout;
436         struct tsp msg;
437         struct servent *srvp;
438
439         if (argc != 2) {
440                 printf("usage: timedc trace { on | off }\n");
441                 return;
442         }
443
444         srvp = getservbyname("timed", "udp");
445         if (srvp == 0) {
446                 warnx("timed/udp: unknown service");
447                 return;
448         }
449         dest.sin_port = srvp->s_port;
450         dest.sin_family = AF_INET;
451
452         if (gethostname(myname, sizeof(myname) - 1) < 0)
453                 err(1, "gethostname");
454         hp = gethostbyname(myname);
455         bcopy(hp->h_addr, &dest.sin_addr.s_addr, hp->h_length);
456
457         if (strcmp(argv[1], "on") == 0) {
458                 msg.tsp_type = TSP_TRACEON;
459                 onflag = ON;
460         } else {
461                 msg.tsp_type = TSP_TRACEOFF;
462                 onflag = OFF;
463         }
464
465         (void)strcpy(msg.tsp_name, myname);
466         msg.tsp_vers = TSPVERSION;
467         bytenetorder(&msg);
468         if (sendto(sock, &msg, sizeof(struct tsp), 0,
469                    (struct sockaddr*)&dest, sizeof(struct sockaddr)) < 0) {
470                 warn("sendto");
471                 return;
472         }
473
474         tout.tv_sec = 5;
475         tout.tv_usec = 0;
476         FD_ZERO(&ready);
477         FD_SET(sock, &ready);
478         if (select(FD_SETSIZE, &ready, (fd_set *)0, (fd_set *)0, &tout)) {
479                 length = sizeof(from);
480                 cc = recvfrom(sock, &msg, sizeof(struct tsp), 0,
481                               (struct sockaddr *)&from, &length);
482                 if (cc < 0) {
483                         warn("recvfrom");
484                         return;
485                 }
486                 /*
487                  * The 4.3BSD protocol spec had a 32-byte tsp_name field, and
488                  * this is still OS-dependent.  Demand that the packet is at
489                  * least long enough to hold a 4.3BSD packet.
490                  */
491                 if (cc < (sizeof(struct tsp) - MAXHOSTNAMELEN + 32)) {
492                         fprintf(stderr, "short packet (%zd/%zu bytes) from %s\n",
493                             cc, sizeof(struct tsp) - MAXHOSTNAMELEN + 32,
494                             inet_ntoa(from.sin_addr));
495                         return;
496                 }
497                 bytehostorder(&msg);
498                 if (msg.tsp_type == TSP_ACK)
499                         if (onflag)
500                                 printf("timed tracing enabled\n");
501                         else
502                                 printf("timed tracing disabled\n");
503                 else {
504                         if (msg.tsp_type >= TSPTYPENUMBER)
505                                 printf("unknown ack received: %u\n",
506                                         msg.tsp_type);
507                         else    
508                                 printf("wrong ack received: %s\n",
509                                                 tsptype[msg.tsp_type]);
510                 }
511         } else
512                 printf("communication error\n");
513 }
514
515 int
516 priv_resources()
517 {
518         int port;
519         struct sockaddr_in sin;
520
521         sock = socket(AF_INET, SOCK_DGRAM, 0);
522         if (sock < 0) {
523                 warn("opening socket");
524                 return(-1);
525         }
526
527         sin.sin_family = AF_INET;
528         sin.sin_addr.s_addr = 0;
529         for (port = IPPORT_RESERVED - 1; port > IPPORT_RESERVED / 2; port--) {
530                 sin.sin_port = htons((u_short)port);
531                 if (bind(sock, (struct sockaddr*)&sin, sizeof (sin)) >= 0)
532                         break;
533                 if (errno != EADDRINUSE && errno != EADDRNOTAVAIL) {
534                         warn("bind");
535                         (void) close(sock);
536                         return(-1);
537                 }
538         }
539         if (port == IPPORT_RESERVED / 2) {
540                 warnx("all reserved ports in use");
541                 (void) close(sock);
542                 return(-1);
543         }
544
545         sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
546         if (sock_raw < 0)  {
547                 warn("opening raw socket");
548                 (void) close(sock);
549                 return(-1);
550         }
551         return(1);
552 }