xref: /freebsd/sbin/routed/rtquery/rtquery.c (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
1 /*-
2  * Copyright (c) 1982, 1986, 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  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <netinet/in.h>
38 #define RIPVERSION RIPv2
39 #include <protocols/routed.h>
40 #include <arpa/inet.h>
41 #include <netdb.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef sgi
48 #include <strings.h>
49 #include <bstring.h>
50 #endif
51 
52 #define UNUSED __attribute__((unused))
53 #ifndef __RCSID
54 #define __RCSID(_s) static const char rcsid[] UNUSED = _s
55 #endif
56 #ifndef __COPYRIGHT
57 #define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s
58 #endif
59 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n"
60 	    "The Regents of the University of California."
61 	    "  All rights reserved.\n");
62 #ifdef __NetBSD__
63 __RCSID("$NetBSD$");
64 #elif defined(__FreeBSD__)
65 __RCSID("$FreeBSD$");
66 #else
67 __RCSID("$Revision: 2.26 $");
68 #ident "$Revision: 2.26 $"
69 #endif
70 
71 #ifndef sgi
72 #define _HAVE_SIN_LEN
73 #endif
74 
75 #ifdef __NetBSD__
76 #include <md5.h>
77 #else
78 #define MD5_DIGEST_LEN 16
79 typedef struct {
80 	u_int32_t state[4];		/* state (ABCD) */
81 	u_int32_t count[2];		/* # of bits, modulo 2^64 (LSB 1st) */
82 	unsigned char buffer[64];	/* input buffer */
83 } MD5_CTX;
84 extern void MD5Init(MD5_CTX*);
85 extern void MD5Update(MD5_CTX*, u_char*, u_int);
86 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
87 #endif
88 
89 
90 #define	WTIME	15		/* Time to wait for all responses */
91 #define	STIME	(250*1000)	/* usec to wait for another response */
92 
93 int	soc;
94 
95 const char *pgmname;
96 
97 union {
98 	struct rip rip;
99 	char	packet[MAXPACKETSIZE+MAXPATHLEN];
100 } omsg_buf;
101 #define OMSG omsg_buf.rip
102 int omsg_len = sizeof(struct rip);
103 
104 union {
105 	struct	rip rip;
106 	char	packet[MAXPACKETSIZE+1024];
107 	} imsg_buf;
108 #define IMSG imsg_buf.rip
109 
110 int	nflag;				/* numbers, no names */
111 int	pflag;				/* play the `gated` game */
112 int	ripv2 = 1;			/* use RIP version 2 */
113 int	wtime = WTIME;
114 int	rflag;				/* 1=ask about a particular route */
115 int	trace, not_trace;		/* send trace command or not */
116 int	auth_type = RIP_AUTH_NONE;
117 char	passwd[RIP_AUTH_PW_LEN];
118 u_long	keyid;
119 
120 struct timeval sent;			/* when query sent */
121 
122 static char localhost_str[] = "localhost";
123 static char *default_argv[] = {localhost_str, 0};
124 
125 static void rip_input(struct sockaddr_in*, int);
126 static int out(const char *);
127 static void trace_loop(char *argv[]) __attribute((__noreturn__));
128 static void query_loop(char *argv[], int) __attribute((__noreturn__));
129 static int getnet(char *, struct netinfo *);
130 static u_int std_mask(u_int);
131 static int parse_quote(char **, const char *, char *, char *, int);
132 static void usage(void);
133 
134 
135 int
136 main(int argc,
137      char *argv[])
138 {
139 	int ch, bsize;
140 	char *p, *options, *value, delim;
141 	const char *result;
142 
143 	OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
144 	OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
145 	OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
146 
147 	pgmname = argv[0];
148 	while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
149 		switch (ch) {
150 		case 'n':
151 			not_trace = 1;
152 			nflag = 1;
153 			break;
154 
155 		case 'p':
156 			not_trace = 1;
157 			pflag = 1;
158 			break;
159 
160 		case '1':
161 			ripv2 = 0;
162 			break;
163 
164 		case 'w':
165 			not_trace = 1;
166 			wtime = (int)strtoul(optarg, &p, 0);
167 			if (*p != '\0'
168 			    || wtime <= 0)
169 				usage();
170 			break;
171 
172 		case 'r':
173 			not_trace = 1;
174 			if (rflag)
175 				usage();
176 			rflag = getnet(optarg, &OMSG.rip_nets[0]);
177 			if (!rflag) {
178 				struct hostent *hp = gethostbyname(optarg);
179 				if (hp == 0) {
180 					fprintf(stderr, "%s: %s:",
181 						pgmname, optarg);
182 					herror(0);
183 					exit(1);
184 				}
185 				memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
186 				       sizeof(OMSG.rip_nets[0].n_dst));
187 				OMSG.rip_nets[0].n_family = RIP_AF_INET;
188 				OMSG.rip_nets[0].n_mask = -1;
189 				rflag = 1;
190 			}
191 			break;
192 
193 		case 't':
194 			trace = 1;
195 			options = optarg;
196 			while (*options != '\0') {
197 				/* messy complications to make -W -Wall happy */
198 				static char on_str[] = "on";
199 				static char more_str[] = "more";
200 				static char off_str[] = "off";
201 				static char dump_str[] = "dump";
202 				static char *traceopts[] = {
203 #				    define TRACE_ON	0
204 					on_str,
205 #				    define TRACE_MORE	1
206 					more_str,
207 #				    define TRACE_OFF	2
208 					off_str,
209 #				    define TRACE_DUMP	3
210 					dump_str,
211 					0
212 				};
213 				result = "";
214 				switch (getsubopt(&options,traceopts,&value)) {
215 				case TRACE_ON:
216 					OMSG.rip_cmd = RIPCMD_TRACEON;
217 					if (!value
218 					    || strlen(value) > MAXPATHLEN)
219 					    usage();
220 					result = value;
221 					break;
222 				case TRACE_MORE:
223 					if (value)
224 					    usage();
225 					OMSG.rip_cmd = RIPCMD_TRACEON;
226 					break;
227 				case TRACE_OFF:
228 					if (value)
229 					    usage();
230 					OMSG.rip_cmd = RIPCMD_TRACEOFF;
231 					break;
232 				case TRACE_DUMP:
233 					if (value)
234 					    usage();
235 					OMSG.rip_cmd = RIPCMD_TRACEON;
236 					result = "dump/../table";
237 					break;
238 				default:
239 					usage();
240 				}
241 				strcpy((char*)OMSG.rip_tracefile, result);
242 				omsg_len += strlen(result) - sizeof(OMSG.ripun);
243 			}
244 			break;
245 
246 		case 'a':
247 			not_trace = 1;
248 			p = strchr(optarg,'=');
249 			if (!p)
250 				usage();
251 			*p++ = '\0';
252 			if (!strcasecmp("passwd",optarg))
253 				auth_type = RIP_AUTH_PW;
254 			else if (!strcasecmp("md5_passwd",optarg))
255 				auth_type = RIP_AUTH_MD5;
256 			else
257 				usage();
258 			if (0 > parse_quote(&p,"|",&delim,
259 					    passwd, sizeof(passwd)))
260 				usage();
261 			if (auth_type == RIP_AUTH_MD5
262 			    && delim == '|') {
263 				keyid = strtoul(p+1,&p,0);
264 				if (keyid > 255 || *p != '\0')
265 					usage();
266 			} else if (delim != '\0') {
267 				usage();
268 			}
269 			break;
270 
271 		default:
272 			usage();
273 	}
274 	argv += optind;
275 	argc -= optind;
276 	if (not_trace && trace)
277 		usage();
278 	if (argc == 0) {
279 		argc = 1;
280 		argv = default_argv;
281 	}
282 
283 	soc = socket(AF_INET, SOCK_DGRAM, 0);
284 	if (soc < 0) {
285 		perror("socket");
286 		exit(2);
287 	}
288 
289 	/* be prepared to receive a lot of routes */
290 	for (bsize = 127*1024; ; bsize -= 1024) {
291 		if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
292 			       &bsize, sizeof(bsize)) == 0)
293 			break;
294 		if (bsize <= 4*1024) {
295 			perror("setsockopt SO_RCVBUF");
296 			break;
297 		}
298 	}
299 
300 	if (trace)
301 		trace_loop(argv);
302 	else
303 		query_loop(argv, argc);
304 	/* NOTREACHED */
305 	return 0;
306 }
307 
308 
309 static void
310 usage(void)
311 {
312 	fprintf(stderr,
313 		"usage:  rtquery [-np1] [-r tgt_rt] [-w wtime]"
314 		" [-a type=passwd] host1 [host2 ...]\n"
315 		"\trtquery -t {on=filename|more|off|dump}"
316 				" host1 [host2 ...]\n");
317 	exit(1);
318 }
319 
320 
321 /* tell the target hosts about tracing
322  */
323 static void
324 trace_loop(char *argv[])
325 {
326 	struct sockaddr_in myaddr;
327 	int res;
328 
329 	if (geteuid() != 0) {
330 		(void)fprintf(stderr, "-t requires UID 0\n");
331 		exit(1);
332 	}
333 
334 	if (ripv2) {
335 		OMSG.rip_vers = RIPv2;
336 	} else {
337 		OMSG.rip_vers = RIPv1;
338 	}
339 
340 	memset(&myaddr, 0, sizeof(myaddr));
341 	myaddr.sin_family = AF_INET;
342 #ifdef _HAVE_SIN_LEN
343 	myaddr.sin_len = sizeof(myaddr);
344 #endif
345 	myaddr.sin_port = htons(IPPORT_RESERVED-1);
346 	while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
347 		if (errno != EADDRINUSE
348 		    || myaddr.sin_port == 0) {
349 			perror("bind");
350 			exit(2);
351 		}
352 		myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
353 	}
354 
355 	res = 1;
356 	while (*argv != 0) {
357 		if (out(*argv++) <= 0)
358 			res = 0;
359 	}
360 	exit(res);
361 }
362 
363 
364 /* query all of the listed hosts
365  */
366 static void
367 query_loop(char *argv[], int argc)
368 {
369 #	define NA0 (OMSG.rip_auths[0])
370 #	define NA2 (OMSG.rip_auths[2])
371 	struct seen {
372 		struct seen *next;
373 		struct in_addr addr;
374 	} *seen, *sp;
375 	int answered = 0;
376 	int cc;
377 	fd_set bits;
378 	struct timeval now, delay;
379 	struct sockaddr_in from;
380 	int fromlen;
381 	MD5_CTX md5_ctx;
382 
383 
384 	OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
385 	if (ripv2) {
386 		OMSG.rip_vers = RIPv2;
387 		if (auth_type == RIP_AUTH_PW) {
388 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
389 			NA0.a_family = RIP_AF_AUTH;
390 			NA0.a_type = RIP_AUTH_PW;
391 			memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
392 			omsg_len += sizeof(OMSG.rip_nets[0]);
393 
394 		} else if (auth_type == RIP_AUTH_MD5) {
395 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
396 			NA0.a_family = RIP_AF_AUTH;
397 			NA0.a_type = RIP_AUTH_MD5;
398 			NA0.au.a_md5.md5_keyid = (int8_t)keyid;
399 			NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN;
400 			NA0.au.a_md5.md5_seqno = 0;
401 			cc = (char *)&NA2-(char *)&OMSG;
402 			NA0.au.a_md5.md5_pkt_len = htons(cc);
403 			NA2.a_family = RIP_AF_AUTH;
404 			NA2.a_type = htons(1);
405 			MD5Init(&md5_ctx);
406 			MD5Update(&md5_ctx,
407 				  (u_char *)&OMSG, cc);
408 			MD5Update(&md5_ctx,
409 				  (u_char *)passwd, RIP_AUTH_MD5_HASH_LEN);
410 			MD5Final(NA2.au.au_pw, &md5_ctx);
411 			omsg_len += 2*sizeof(OMSG.rip_nets[0]);
412 		}
413 
414 	} else {
415 		OMSG.rip_vers = RIPv1;
416 		OMSG.rip_nets[0].n_mask = 0;
417 	}
418 
419 	/* ask the first (valid) host */
420 	seen = 0;
421 	while (0 > out(*argv++)) {
422 		if (*argv == 0)
423 			exit(1);
424 		answered++;
425 	}
426 
427 	FD_ZERO(&bits);
428 	for (;;) {
429 		FD_SET(soc, &bits);
430 		delay.tv_sec = 0;
431 		delay.tv_usec = STIME;
432 		cc = select(soc+1, &bits, 0,0, &delay);
433 		if (cc > 0) {
434 			fromlen = sizeof(from);
435 			cc = recvfrom(soc, imsg_buf.packet,
436 				      sizeof(imsg_buf.packet), 0,
437 				      (struct sockaddr *)&from, &fromlen);
438 			if (cc < 0) {
439 				perror("recvfrom");
440 				exit(1);
441 			}
442 			/* count the distinct responding hosts.
443 			 * You cannot match responding hosts with
444 			 * addresses to which queries were transmitted,
445 			 * because a router might respond with a
446 			 * different source address.
447 			 */
448 			for (sp = seen; sp != 0; sp = sp->next) {
449 				if (sp->addr.s_addr == from.sin_addr.s_addr)
450 					break;
451 			}
452 			if (sp == 0) {
453 				sp = malloc(sizeof(*sp));
454 				if (sp == 0) {
455 					fprintf(stderr,
456 						"rtquery: malloc failed\n");
457 					exit(1);
458 				}
459 				sp->addr = from.sin_addr;
460 				sp->next = seen;
461 				seen = sp;
462 				answered++;
463 			}
464 
465 			rip_input(&from, cc);
466 			continue;
467 		}
468 
469 		if (cc < 0) {
470 			if (errno == EINTR)
471 				continue;
472 			perror("select");
473 			exit(1);
474 		}
475 
476 		/* After a pause in responses, probe another host.
477 		 * This reduces the intermingling of answers.
478 		 */
479 		while (*argv != 0 && 0 > out(*argv++))
480 			answered++;
481 
482 		/* continue until no more packets arrive
483 		 * or we have heard from all hosts
484 		 */
485 		if (answered >= argc)
486 			break;
487 
488 		/* or until we have waited a long time
489 		 */
490 		if (gettimeofday(&now, 0) < 0) {
491 			perror("gettimeofday(now)");
492 			exit(1);
493 		}
494 		if (sent.tv_sec + wtime <= now.tv_sec)
495 			break;
496 	}
497 
498 	/* fail if there was no answer */
499 	exit (answered >= argc ? 0 : 1);
500 }
501 
502 
503 /* send to one host
504  */
505 static int
506 out(const char *host)
507 {
508 	struct sockaddr_in router;
509 	struct hostent *hp;
510 
511 	if (gettimeofday(&sent, 0) < 0) {
512 		perror("gettimeofday(sent)");
513 		return -1;
514 	}
515 
516 	memset(&router, 0, sizeof(router));
517 	router.sin_family = AF_INET;
518 #ifdef _HAVE_SIN_LEN
519 	router.sin_len = sizeof(router);
520 #endif
521 	if (!inet_aton(host, &router.sin_addr)) {
522 		hp = gethostbyname(host);
523 		if (hp == 0) {
524 			herror(host);
525 			return -1;
526 		}
527 		memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
528 	}
529 	router.sin_port = htons(RIP_PORT);
530 
531 	if (sendto(soc, &omsg_buf, omsg_len, 0,
532 		   (struct sockaddr *)&router, sizeof(router)) < 0) {
533 		perror(host);
534 		return -1;
535 	}
536 
537 	return 0;
538 }
539 
540 
541 /*
542  * Convert string to printable characters
543  */
544 static char *
545 qstring(u_char *s, int len)
546 {
547 	static char buf[8*20+1];
548 	char *p;
549 	u_char *s2, c;
550 
551 
552 	for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
553 		c = *s++;
554 		if (c == '\0') {
555 			for (s2 = s+1; s2 < &s[len]; s2++) {
556 				if (*s2 != '\0')
557 					break;
558 			}
559 			if (s2 >= &s[len])
560 			    goto exit;
561 		}
562 
563 		if (c >= ' ' && c < 0x7f && c != '\\') {
564 			*p++ = c;
565 			continue;
566 		}
567 		*p++ = '\\';
568 		switch (c) {
569 		case '\\':
570 			*p++ = '\\';
571 			break;
572 		case '\n':
573 			*p++= 'n';
574 			break;
575 		case '\r':
576 			*p++= 'r';
577 			break;
578 		case '\t':
579 			*p++ = 't';
580 			break;
581 		case '\b':
582 			*p++ = 'b';
583 			break;
584 		default:
585 			p += sprintf(p,"%o",c);
586 			break;
587 		}
588 	}
589 exit:
590 	*p = '\0';
591 	return buf;
592 }
593 
594 
595 /*
596  * Handle an incoming RIP packet.
597  */
598 static void
599 rip_input(struct sockaddr_in *from,
600 	  int size)
601 {
602 	struct netinfo *n, *lim;
603 	struct in_addr in;
604 	const char *name;
605 	char net_buf[80];
606 	u_char hash[RIP_AUTH_MD5_KEY_LEN];
607 	MD5_CTX md5_ctx;
608 	u_char md5_authed = 0;
609 	u_int mask, dmask;
610 	char *sp;
611 	int i;
612 	struct hostent *hp;
613 	struct netent *np;
614 	struct netauth *na;
615 
616 
617 	if (nflag) {
618 		printf("%s:", inet_ntoa(from->sin_addr));
619 	} else {
620 		hp = gethostbyaddr((char*)&from->sin_addr,
621 				   sizeof(struct in_addr), AF_INET);
622 		if (hp == 0) {
623 			printf("%s:",
624 			       inet_ntoa(from->sin_addr));
625 		} else {
626 			printf("%s (%s):", hp->h_name,
627 			       inet_ntoa(from->sin_addr));
628 		}
629 	}
630 	if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
631 		printf("\n    unexpected response type %d\n", IMSG.rip_cmd);
632 		return;
633 	}
634 	printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
635 	       (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
636 	       size);
637 	if (size > MAXPACKETSIZE) {
638 		if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
639 			printf("       at least %d bytes too long\n",
640 			       size-MAXPACKETSIZE);
641 			size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
642 		} else {
643 			printf("       %d bytes too long\n",
644 			       size-MAXPACKETSIZE);
645 		}
646 	} else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
647 		printf("    response of bad length=%d\n", size);
648 	}
649 
650 	n = IMSG.rip_nets;
651 	lim = (struct netinfo *)((char*)n + size) - 1;
652 	for (; n <= lim; n++) {
653 		name = "";
654 		if (n->n_family == RIP_AF_INET) {
655 			in.s_addr = n->n_dst;
656 			(void)strcpy(net_buf, inet_ntoa(in));
657 
658 			mask = ntohl(n->n_mask);
659 			dmask = mask & -mask;
660 			if (mask != 0) {
661 				sp = &net_buf[strlen(net_buf)];
662 				if (IMSG.rip_vers == RIPv1) {
663 					(void)sprintf(sp," mask=%#x ? ",mask);
664 					mask = 0;
665 				} else if (mask + dmask == 0) {
666 					for (i = 0;
667 					     (i != 32
668 					      && ((1<<i)&mask) == 0);
669 					     i++)
670 						continue;
671 					(void)sprintf(sp, "/%d",32-i);
672 				} else {
673 					(void)sprintf(sp," (mask %#x)", mask);
674 				}
675 			}
676 
677 			if (!nflag) {
678 				if (mask == 0) {
679 					mask = std_mask(in.s_addr);
680 					if ((ntohl(in.s_addr) & ~mask) != 0)
681 						mask = 0;
682 				}
683 				/* Without a netmask, do not worry about
684 				 * whether the destination is a host or a
685 				 * network. Try both and use the first name
686 				 * we get.
687 				 *
688 				 * If we have a netmask we can make a
689 				 * good guess.
690 				 */
691 				if ((in.s_addr & ~mask) == 0) {
692 					np = getnetbyaddr((long)in.s_addr,
693 							  AF_INET);
694 					if (np != 0)
695 						name = np->n_name;
696 					else if (in.s_addr == 0)
697 						name = "default";
698 				}
699 				if (name[0] == '\0'
700 				    && ((in.s_addr & ~mask) != 0
701 					|| mask == 0xffffffff)) {
702 					hp = gethostbyaddr((char*)&in,
703 							   sizeof(in),
704 							   AF_INET);
705 					if (hp != 0)
706 						name = hp->h_name;
707 				}
708 			}
709 
710 		} else if (n->n_family == RIP_AF_AUTH) {
711 			na = (struct netauth*)n;
712 			if (na->a_type == RIP_AUTH_PW
713 			    && n == IMSG.rip_nets) {
714 				(void)printf("  Password Authentication:"
715 					     " \"%s\"\n",
716 					     qstring(na->au.au_pw,
717 						     RIP_AUTH_PW_LEN));
718 				continue;
719 			}
720 
721 			if (na->a_type == RIP_AUTH_MD5
722 			    && n == IMSG.rip_nets) {
723 				(void)printf("  MD5 Auth"
724 					     " len=%d KeyID=%d"
725 					     " auth_len=%d"
726 					     " seqno=%#x"
727 					     " rsvd=%#x,%#x\n",
728 					     ntohs(na->au.a_md5.md5_pkt_len),
729 					     na->au.a_md5.md5_keyid,
730 					     na->au.a_md5.md5_auth_len,
731 					     (int)ntohl(na->au.a_md5.md5_seqno),
732 					     na->au.a_md5.rsvd[0],
733 					     na->au.a_md5.rsvd[1]);
734 				md5_authed = 1;
735 				continue;
736 			}
737 			(void)printf("  Authentication type %d: ",
738 				     ntohs(na->a_type));
739 			for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
740 				(void)printf("%02x ", na->au.au_pw[i]);
741 			putc('\n', stdout);
742 			if (md5_authed && n+1 > lim
743 			    && na->a_type == ntohs(1)) {
744 				MD5Init(&md5_ctx);
745 				MD5Update(&md5_ctx, (u_char *)&IMSG,
746 					  (char *)na-(char *)&IMSG
747 					  +RIP_AUTH_MD5_HASH_XTRA);
748 				MD5Update(&md5_ctx, (u_char *)passwd,
749 					  RIP_AUTH_MD5_KEY_LEN);
750 				MD5Final(hash, &md5_ctx);
751 				(void)printf("    %s hash\n",
752 					     memcmp(hash, na->au.au_pw,
753 						    sizeof(hash))
754 					     ? "WRONG" : "correct");
755 			}
756 			continue;
757 
758 		} else {
759 			(void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
760 				      ntohs(n->n_family),
761 				      (u_char)(n->n_dst >> 24),
762 				      (u_char)(n->n_dst >> 16),
763 				      (u_char)(n->n_dst >> 8),
764 				      (u_char)n->n_dst);
765 		}
766 
767 		(void)printf("  %-18s metric %2d %-10s",
768 			     net_buf, (int)ntohl(n->n_metric), name);
769 
770 		if (n->n_nhop != 0) {
771 			in.s_addr = n->n_nhop;
772 			if (nflag)
773 				hp = 0;
774 			else
775 				hp = gethostbyaddr((char*)&in, sizeof(in),
776 						   AF_INET);
777 			(void)printf(" nhop=%-15s%s",
778 				     (hp != 0) ? hp->h_name : inet_ntoa(in),
779 				     (IMSG.rip_vers == RIPv1) ? " ?" : "");
780 		}
781 		if (n->n_tag != 0)
782 			(void)printf(" tag=%#x%s", n->n_tag,
783 				     (IMSG.rip_vers == RIPv1) ? " ?" : "");
784 		putc('\n', stdout);
785 	}
786 }
787 
788 
789 /* Return the classical netmask for an IP address.
790  */
791 static u_int
792 std_mask(u_int addr)			/* in network order */
793 {
794 	addr = ntohl(addr);		/* was a host, not a network */
795 
796 	if (addr == 0)			/* default route has mask 0 */
797 		return 0;
798 	if (IN_CLASSA(addr))
799 		return IN_CLASSA_NET;
800 	if (IN_CLASSB(addr))
801 		return IN_CLASSB_NET;
802 	return IN_CLASSC_NET;
803 }
804 
805 
806 /* get a network number as a name or a number, with an optional "/xx"
807  * netmask.
808  */
809 static int				/* 0=bad */
810 getnet(char *name,
811        struct netinfo *rt)
812 {
813 	int i;
814 	struct netent *nentp;
815 	u_int mask;
816 	struct in_addr in;
817 	char hname[MAXHOSTNAMELEN+1];
818 	char *mname, *p;
819 
820 
821 	/* Detect and separate "1.2.3.4/24"
822 	 */
823 	if (0 != (mname = strrchr(name,'/'))) {
824 		i = (int)(mname - name);
825 		if (i > (int)sizeof(hname)-1)	/* name too long */
826 			return 0;
827 		memmove(hname, name, i);
828 		hname[i] = '\0';
829 		mname++;
830 		name = hname;
831 	}
832 
833 	nentp = getnetbyname(name);
834 	if (nentp != 0) {
835 		in.s_addr = nentp->n_net;
836 	} else if (inet_aton(name, &in) == 1) {
837 		in.s_addr = ntohl(in.s_addr);
838 	} else {
839 		return 0;
840 	}
841 
842 	if (mname == 0) {
843 		mask = std_mask(in.s_addr);
844 		if ((~mask & in.s_addr) != 0)
845 			mask = 0xffffffff;
846 	} else {
847 		mask = (u_int)strtoul(mname, &p, 0);
848 		if (*p != '\0' || mask > 32)
849 			return 0;
850 		mask = 0xffffffff << (32-mask);
851 	}
852 
853 	rt->n_dst = htonl(in.s_addr);
854 	rt->n_family = RIP_AF_INET;
855 	rt->n_mask = htonl(mask);
856 	return 1;
857 }
858 
859 
860 /* strtok(), but honoring backslash
861  */
862 static int				/* -1=bad */
863 parse_quote(char **linep,
864 	    const char *delims,
865 	    char *delimp,
866 	    char *buf,
867 	    int	lim)
868 {
869 	char c, *pc;
870 	const char *p;
871 
872 
873 	pc = *linep;
874 	if (*pc == '\0')
875 		return -1;
876 
877 	for (;;) {
878 		if (lim == 0)
879 			return -1;
880 		c = *pc++;
881 		if (c == '\0')
882 			break;
883 
884 		if (c == '\\' && *pc != '\0') {
885 			if ((c = *pc++) == 'n') {
886 				c = '\n';
887 			} else if (c == 'r') {
888 				c = '\r';
889 			} else if (c == 't') {
890 				c = '\t';
891 			} else if (c == 'b') {
892 				c = '\b';
893 			} else if (c >= '0' && c <= '7') {
894 				c -= '0';
895 				if (*pc >= '0' && *pc <= '7') {
896 					c = (c<<3)+(*pc++ - '0');
897 					if (*pc >= '0' && *pc <= '7')
898 					    c = (c<<3)+(*pc++ - '0');
899 				}
900 			}
901 
902 		} else {
903 			for (p = delims; *p != '\0'; ++p) {
904 				if (*p == c)
905 					goto exit;
906 			}
907 		}
908 
909 		*buf++ = c;
910 		--lim;
911 	}
912 exit:
913 	if (delimp != 0)
914 		*delimp = c;
915 	*linep = pc-1;
916 	if (lim != 0)
917 		*buf = '\0';
918 	return 0;
919 }
920