xref: /onnv-gate/usr/src/cmd/ypcmd/stdhosts.c (revision 702)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*702Sth160488  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SMI4.1 1.7  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <ndbm.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <arpa/inet.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Filter to convert both IPv4 and IPv6 addresses from /etc/hosts or
420Sstevel@tonic-gate  * /etc/inet/ipnodes files.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Size of buffer for input lines. Add two bytes on input for newline
470Sstevel@tonic-gate  * and terminating NULL. Note that the practical limit for data
480Sstevel@tonic-gate  * storage in ndbm is (PBLKSIZ - 3 * sizeof (short)). Though this
490Sstevel@tonic-gate  * differs from spec 1170 the common industry implementation does
500Sstevel@tonic-gate  * conform to this slightly lower limit.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	OUTPUTSIZ (PBLKSIZ - 3 * sizeof (short))
540Sstevel@tonic-gate #define	INPUTSIZ (OUTPUTSIZ + 2)
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static int ipv4 = -1;
570Sstevel@tonic-gate static char *cmd;
580Sstevel@tonic-gate int warn = 0;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void verify_and_output(const char *key, char *value, int lineno);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate void
630Sstevel@tonic-gate usage()
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	fprintf(stderr, "stdhosts [-w] [-n] [in-file]\n");
660Sstevel@tonic-gate 	fprintf(stderr, "\t-w\tprint malformed warning messages.\n");
670Sstevel@tonic-gate 	exit(1);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate 
70*702Sth160488 int
710Sstevel@tonic-gate main(argc, argv)
720Sstevel@tonic-gate 	char **argv;
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	char line[INPUTSIZ];
750Sstevel@tonic-gate 	char adr[INPUTSIZ];
760Sstevel@tonic-gate 	char nadr[INET6_ADDRSTRLEN]; /* Contains normalised address */
770Sstevel@tonic-gate 	const char *nadrp;	/* Pointer to the normalised address */
780Sstevel@tonic-gate 	char *trailer;
790Sstevel@tonic-gate 	char *commentp;		/* Pointer to comment character '#' */
800Sstevel@tonic-gate 	int c;
810Sstevel@tonic-gate 	FILE *fp;
820Sstevel@tonic-gate 	int lineno = 0;		/* Input line counter */
830Sstevel@tonic-gate 	struct in_addr in;	/* Used for normalising the IPv4 address */
840Sstevel@tonic-gate 	struct in6_addr in6;	/* Used for normalising the IPv6 address */
850Sstevel@tonic-gate 	char *fgetsp;		/* Holds return value for fgets() calls */
860Sstevel@tonic-gate 	int endoffile = 0;	/* Set when end of file reached */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (cmd = strrchr(argv[0], '/'))
890Sstevel@tonic-gate 		++cmd;
900Sstevel@tonic-gate 	else
910Sstevel@tonic-gate 		cmd = argv[0];
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "v:wn")) != -1) {
940Sstevel@tonic-gate 		switch (c) {
950Sstevel@tonic-gate 		case 'w':	/* Send warning messages to stderr */
960Sstevel@tonic-gate 			warn = 1;
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 		case 'n':
990Sstevel@tonic-gate 			ipv4 = 0;
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 		default:
1020Sstevel@tonic-gate 			usage();
1030Sstevel@tonic-gate 			exit(1);
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (optind < argc) {
1080Sstevel@tonic-gate 		fp = fopen(argv[optind], "r");
1090Sstevel@tonic-gate 		if (fp == NULL) {
1100Sstevel@tonic-gate 			fprintf(stderr, "%s: can't open %s\n",
1110Sstevel@tonic-gate 			    cmd, argv[optind]);
1120Sstevel@tonic-gate 			exit(1);
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 	} else
1150Sstevel@tonic-gate 		fp = stdin;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	while (!endoffile &&
1180Sstevel@tonic-gate 	    (fgetsp = fgets(line, sizeof (line), fp)) != NULL) {
1190Sstevel@tonic-gate 		lineno++;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		/* Check for comments */
1220Sstevel@tonic-gate 		if ((commentp = strchr(line, '#')) != NULL) {
1230Sstevel@tonic-gate 			if ((line[strlen(line) - 1] != '\n') &&
1240Sstevel@tonic-gate 			    (strlen(line) >= (sizeof (line) - 1))) {
1250Sstevel@tonic-gate 				/*
1260Sstevel@tonic-gate 				 * Discard the remainder of the line
1270Sstevel@tonic-gate 				 * until the newline or EOF, then
1280Sstevel@tonic-gate 				 * continue to parse the line. Use
1290Sstevel@tonic-gate 				 * adr[] rather then line[] to
1300Sstevel@tonic-gate 				 * preserve the contents of line[].
1310Sstevel@tonic-gate 				 */
1320Sstevel@tonic-gate 				while ((fgetsp = fgets(adr, sizeof (adr),
1330Sstevel@tonic-gate 				    fp)) != NULL) {
1340Sstevel@tonic-gate 					if (adr[strlen(adr) - 1] == '\n')
1350Sstevel@tonic-gate 						break;
1360Sstevel@tonic-gate 				}
1370Sstevel@tonic-gate 				if (fgetsp == NULL)
1380Sstevel@tonic-gate 					endoffile = 1;
1390Sstevel@tonic-gate 			}
1400Sstevel@tonic-gate 			/* Terminate line[] at the comment character */
1410Sstevel@tonic-gate 			*commentp = '\0';
1420Sstevel@tonic-gate 		} else if ((line[strlen(line) - 1] != '\n') &&
1430Sstevel@tonic-gate 		    (strlen(line) >= (sizeof (line) - 1))) {
1440Sstevel@tonic-gate 			/*
1450Sstevel@tonic-gate 			 * Catch long lines but not if this is a short
1460Sstevel@tonic-gate 			 * line with no '\n' at the end of the input.
1470Sstevel@tonic-gate 			 */
1480Sstevel@tonic-gate 			if (warn)
1490Sstevel@tonic-gate 				fprintf(stderr,
1500Sstevel@tonic-gate 				    "%s: Warning: more than %d "
1510Sstevel@tonic-gate 				    "bytes on line %d, ignored\n",
1520Sstevel@tonic-gate 				    cmd, sizeof (line) - 2, lineno);
1530Sstevel@tonic-gate 			/*
1540Sstevel@tonic-gate 			 * Discard the remaining lines until the
1550Sstevel@tonic-gate 			 * newline or EOF.
1560Sstevel@tonic-gate 			 */
1570Sstevel@tonic-gate 			while ((fgetsp = fgets(line, sizeof (line),
1580Sstevel@tonic-gate 			    fp)) != NULL)
1590Sstevel@tonic-gate 				if (line[strlen(line) - 1] == '\n')
1600Sstevel@tonic-gate 					break;
1610Sstevel@tonic-gate 			if (fgetsp == NULL)
1620Sstevel@tonic-gate 				endoffile = 1;
1630Sstevel@tonic-gate 			continue;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 		if (sscanf(line, "%s", adr) != 1) { /* Blank line, ignore */
1670Sstevel@tonic-gate 			continue;
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		if ((trailer = strpbrk(line, " \t")) == NULL) {
1710Sstevel@tonic-gate 			if (warn)
1720Sstevel@tonic-gate 				fprintf(stderr,
1730Sstevel@tonic-gate 				    "%s: Warning: no host names on line %d, "
1740Sstevel@tonic-gate 				    "ignored\n", cmd, lineno);
1750Sstevel@tonic-gate 			continue;
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		/*
1790Sstevel@tonic-gate 		 * check for valid addresses
1800Sstevel@tonic-gate 		 *
1810Sstevel@tonic-gate 		 * Attempt an ipv4 conversion, this accepts all valid
1820Sstevel@tonic-gate 		 * ipv4 addresses including:
1830Sstevel@tonic-gate 		 *	d
1840Sstevel@tonic-gate 		 *	d.d
1850Sstevel@tonic-gate 		 *	d.d.d
1860Sstevel@tonic-gate 		 * Unfortunately inet_pton() doesn't recognise these.
1870Sstevel@tonic-gate 		 */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		in.s_addr = inet_addr(adr);
1900Sstevel@tonic-gate 		if (-1 != (int)in.s_addr) {
1910Sstevel@tonic-gate 			/*
1920Sstevel@tonic-gate 			 * It's safe not to check return of NULL as
1930Sstevel@tonic-gate 			 * nadrp is checked for validity later.
1940Sstevel@tonic-gate 			 */
1950Sstevel@tonic-gate 			nadrp = inet_ntop(AF_INET, &in, nadr, sizeof (nadr));
1960Sstevel@tonic-gate 		} else {
1970Sstevel@tonic-gate 			nadrp = NULL; /* Not a valid IPv4 address */
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		if (ipv4) {
2010Sstevel@tonic-gate 			if (nadrp == NULL) {
2020Sstevel@tonic-gate 				if (warn)
2030Sstevel@tonic-gate 					fprintf(stderr,
2040Sstevel@tonic-gate 					    "%s: Warning: malformed address on"
2050Sstevel@tonic-gate 					    " line %d, ignored\n",
2060Sstevel@tonic-gate 					    cmd, lineno);
2070Sstevel@tonic-gate 				continue;
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 		} else { /* v4 or v6 for ipnodes */
2100Sstevel@tonic-gate 			if (nadrp == NULL) {
2110Sstevel@tonic-gate 				if (inet_pton(AF_INET6, adr, &in6) == 1) {
2120Sstevel@tonic-gate 					nadrp = inet_ntop(AF_INET6, &in6,
2130Sstevel@tonic-gate 					    nadr, sizeof (nadr));
2140Sstevel@tonic-gate 				}
2150Sstevel@tonic-gate 				if (nadrp == NULL) { /* Invalid IPv6 too */
2160Sstevel@tonic-gate 					if (warn)
2170Sstevel@tonic-gate 						fprintf(stderr,
2180Sstevel@tonic-gate 						    "%s: Warning: malformed"
2190Sstevel@tonic-gate 						    " address on"
2200Sstevel@tonic-gate 						    " line %d, ignored\n",
2210Sstevel@tonic-gate 						    cmd, lineno);
2220Sstevel@tonic-gate 					continue;
2230Sstevel@tonic-gate 				}
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		verify_and_output(nadrp, trailer, lineno);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	}	/* while */
230*702Sth160488 	return (0);
2310Sstevel@tonic-gate 	/* NOTREACHED */
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * verify_and_output
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * Builds and verifies the output key and value string
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  * It makes sure these rules are followed:
2400Sstevel@tonic-gate  *	key + separator + value <= OUTPUTSIZ (for ndbm)
2410Sstevel@tonic-gate  *	names <= MAXALIASES + 1, ie one canonical name + MAXALIASES aliases
2420Sstevel@tonic-gate  * It will also ignore everything after a '#' comment character
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate static void
2450Sstevel@tonic-gate verify_and_output(const char *key, char *value, int lineno)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	char *p;			/* General char pointer */
2480Sstevel@tonic-gate 	char *endp;			/* Points to the NULL at the end */
2490Sstevel@tonic-gate 	char *namep;			/* First character of a name */
2500Sstevel@tonic-gate 	char tmpbuf[OUTPUTSIZ+1];	/* Buffer before writing out */
2510Sstevel@tonic-gate 	char *tmpbufp = tmpbuf;		/* Current point in output string */
2520Sstevel@tonic-gate 	int n = 0;			/* Length of output */
2530Sstevel@tonic-gate 	int names = 0;			/* Number of names found */
2540Sstevel@tonic-gate 	int namelen;			/* Length of the name */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (key) {		/* Just in case key is NULL */
2570Sstevel@tonic-gate 		n = strlen(key);
2580Sstevel@tonic-gate 		if (n > OUTPUTSIZ) {
2590Sstevel@tonic-gate 			if (warn)
2600Sstevel@tonic-gate 				fprintf(stderr,
2610Sstevel@tonic-gate 				    "%s: address too long on "
2620Sstevel@tonic-gate 				    "line %d, line discarded\n",
2630Sstevel@tonic-gate 				    cmd, lineno);
2640Sstevel@tonic-gate 			return;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 		memcpy(tmpbufp, key, n+1); /* Plus the '\0' */
2670Sstevel@tonic-gate 		tmpbufp += n;
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (value) {		/* Just in case value is NULL */
2710Sstevel@tonic-gate 		p = value;
2720Sstevel@tonic-gate 		if ((endp = strchr(value, '#')) == 0)	/* Ignore # comments */
2730Sstevel@tonic-gate 			endp = p + strlen(p);		/* Or endp = EOL */
2740Sstevel@tonic-gate 		do {
2750Sstevel@tonic-gate 			/*
2760Sstevel@tonic-gate 			 * Skip white space. Type conversion is
2770Sstevel@tonic-gate 			 * necessary to avoid unfortunate effects of
2780Sstevel@tonic-gate 			 * 8-bit characters appearing negative.
2790Sstevel@tonic-gate 			 */
2800Sstevel@tonic-gate 			while ((p < endp) && isspace((unsigned char)*p))
2810Sstevel@tonic-gate 				p++;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			if (p == endp)	/* End of the string */
2840Sstevel@tonic-gate 				break;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 			names++;
2870Sstevel@tonic-gate 			if (names > (MAXALIASES+1)) { /* cname + MAXALIASES */
2880Sstevel@tonic-gate 				if (warn)
2890Sstevel@tonic-gate 					fprintf(stderr,
2900Sstevel@tonic-gate 					    "%s: Warning: too many "
2910Sstevel@tonic-gate 					    "host names on line %d, "
2920Sstevel@tonic-gate 					    "truncating\n",
2930Sstevel@tonic-gate 					    cmd, lineno);
2940Sstevel@tonic-gate 				break;
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 			namep = p;
2980Sstevel@tonic-gate 			while ((p < endp) && !isspace((unsigned char)*p))
2990Sstevel@tonic-gate 				p++;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			namelen = p - namep;
3020Sstevel@tonic-gate 			n += namelen + 1; /* single white space + name */
3030Sstevel@tonic-gate 			*p = '\0';	   /* Terminate the name string */
3040Sstevel@tonic-gate 			if (n > OUTPUTSIZ) {
3050Sstevel@tonic-gate 				if (warn)
3060Sstevel@tonic-gate 					fprintf(stderr,
3070Sstevel@tonic-gate 					    "%s: Warning: %d byte ndbm limit "
3080Sstevel@tonic-gate 					    "reached on line %d, truncating\n",
3090Sstevel@tonic-gate 					    cmd, OUTPUTSIZ, lineno);
3100Sstevel@tonic-gate 				break;
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 			if (names == 1) /* First space is a '\t' */
3140Sstevel@tonic-gate 				*tmpbufp++ = '\t';
3150Sstevel@tonic-gate 			else
3160Sstevel@tonic-gate 				*tmpbufp++ = ' ';
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			memcpy(tmpbufp, namep, namelen+1); /* Plus the '\0' */
3190Sstevel@tonic-gate 			tmpbufp += namelen;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			if (p < endp)
3220Sstevel@tonic-gate 				p++;	/* Skip the added NULL */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		} while (p < endp);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (names > 0) {
3280Sstevel@tonic-gate 		fputs(tmpbuf, stdout);
3290Sstevel@tonic-gate 		fputc('\n', stdout);
3300Sstevel@tonic-gate 	} else {
3310Sstevel@tonic-gate 		if (warn)
3320Sstevel@tonic-gate 			fprintf(stderr,
3330Sstevel@tonic-gate 			    "%s: Warning: no host names on line %d, "
3340Sstevel@tonic-gate 			    "ignored\n", cmd, lineno);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate }
337