17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
527092493Srs * Common Development and Distribution License (the "License").
627092493Srs * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*d362b749Svk * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23a506a34cSth * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SMI4.1 1.7 */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <ndbm.h>
307c478bd9Sstevel@tonic-gate #include <netdb.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <netinet/in.h>
347c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
4027092493Srs * Filter to convert both IPv4 and IPv6 addresses from /etc/hosts file.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate * Size of buffer for input lines. Add two bytes on input for newline
457c478bd9Sstevel@tonic-gate * and terminating NULL. Note that the practical limit for data
467c478bd9Sstevel@tonic-gate * storage in ndbm is (PBLKSIZ - 3 * sizeof (short)). Though this
477c478bd9Sstevel@tonic-gate * differs from spec 1170 the common industry implementation does
487c478bd9Sstevel@tonic-gate * conform to this slightly lower limit.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #define OUTPUTSIZ (PBLKSIZ - 3 * sizeof (short))
527c478bd9Sstevel@tonic-gate #define INPUTSIZ (OUTPUTSIZ + 2)
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static int ipv4 = -1;
557c478bd9Sstevel@tonic-gate static char *cmd;
56*d362b749Svk int warning = 0;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static void verify_and_output(const char *key, char *value, int lineno);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate void
usage()617c478bd9Sstevel@tonic-gate usage()
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate fprintf(stderr, "stdhosts [-w] [-n] [in-file]\n");
647c478bd9Sstevel@tonic-gate fprintf(stderr, "\t-w\tprint malformed warning messages.\n");
657c478bd9Sstevel@tonic-gate exit(1);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
68a506a34cSth int
main(argc,argv)697c478bd9Sstevel@tonic-gate main(argc, argv)
707c478bd9Sstevel@tonic-gate char **argv;
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate char line[INPUTSIZ];
737c478bd9Sstevel@tonic-gate char adr[INPUTSIZ];
747c478bd9Sstevel@tonic-gate char nadr[INET6_ADDRSTRLEN]; /* Contains normalised address */
757c478bd9Sstevel@tonic-gate const char *nadrp; /* Pointer to the normalised address */
767c478bd9Sstevel@tonic-gate char *trailer;
777c478bd9Sstevel@tonic-gate char *commentp; /* Pointer to comment character '#' */
787c478bd9Sstevel@tonic-gate int c;
797c478bd9Sstevel@tonic-gate FILE *fp;
807c478bd9Sstevel@tonic-gate int lineno = 0; /* Input line counter */
817c478bd9Sstevel@tonic-gate struct in_addr in; /* Used for normalising the IPv4 address */
827c478bd9Sstevel@tonic-gate struct in6_addr in6; /* Used for normalising the IPv6 address */
837c478bd9Sstevel@tonic-gate char *fgetsp; /* Holds return value for fgets() calls */
847c478bd9Sstevel@tonic-gate int endoffile = 0; /* Set when end of file reached */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if (cmd = strrchr(argv[0], '/'))
877c478bd9Sstevel@tonic-gate ++cmd;
887c478bd9Sstevel@tonic-gate else
897c478bd9Sstevel@tonic-gate cmd = argv[0];
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "v:wn")) != -1) {
927c478bd9Sstevel@tonic-gate switch (c) {
937c478bd9Sstevel@tonic-gate case 'w': /* Send warning messages to stderr */
94*d362b749Svk warning = 1;
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate case 'n':
977c478bd9Sstevel@tonic-gate ipv4 = 0;
987c478bd9Sstevel@tonic-gate break;
997c478bd9Sstevel@tonic-gate default:
1007c478bd9Sstevel@tonic-gate usage();
1017c478bd9Sstevel@tonic-gate exit(1);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (optind < argc) {
1067c478bd9Sstevel@tonic-gate fp = fopen(argv[optind], "r");
1077c478bd9Sstevel@tonic-gate if (fp == NULL) {
1087c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: can't open %s\n",
1097c478bd9Sstevel@tonic-gate cmd, argv[optind]);
1107c478bd9Sstevel@tonic-gate exit(1);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate } else
1137c478bd9Sstevel@tonic-gate fp = stdin;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate while (!endoffile &&
1167c478bd9Sstevel@tonic-gate (fgetsp = fgets(line, sizeof (line), fp)) != NULL) {
1177c478bd9Sstevel@tonic-gate lineno++;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /* Check for comments */
1207c478bd9Sstevel@tonic-gate if ((commentp = strchr(line, '#')) != NULL) {
1217c478bd9Sstevel@tonic-gate if ((line[strlen(line) - 1] != '\n') &&
1227c478bd9Sstevel@tonic-gate (strlen(line) >= (sizeof (line) - 1))) {
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Discard the remainder of the line
1257c478bd9Sstevel@tonic-gate * until the newline or EOF, then
1267c478bd9Sstevel@tonic-gate * continue to parse the line. Use
1277c478bd9Sstevel@tonic-gate * adr[] rather then line[] to
1287c478bd9Sstevel@tonic-gate * preserve the contents of line[].
1297c478bd9Sstevel@tonic-gate */
1307c478bd9Sstevel@tonic-gate while ((fgetsp = fgets(adr, sizeof (adr),
1317c478bd9Sstevel@tonic-gate fp)) != NULL) {
1327c478bd9Sstevel@tonic-gate if (adr[strlen(adr) - 1] == '\n')
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate if (fgetsp == NULL)
1367c478bd9Sstevel@tonic-gate endoffile = 1;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate /* Terminate line[] at the comment character */
1397c478bd9Sstevel@tonic-gate *commentp = '\0';
1407c478bd9Sstevel@tonic-gate } else if ((line[strlen(line) - 1] != '\n') &&
1417c478bd9Sstevel@tonic-gate (strlen(line) >= (sizeof (line) - 1))) {
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * Catch long lines but not if this is a short
1447c478bd9Sstevel@tonic-gate * line with no '\n' at the end of the input.
1457c478bd9Sstevel@tonic-gate */
146*d362b749Svk if (warning)
1477c478bd9Sstevel@tonic-gate fprintf(stderr,
1487c478bd9Sstevel@tonic-gate "%s: Warning: more than %d "
1497c478bd9Sstevel@tonic-gate "bytes on line %d, ignored\n",
1507c478bd9Sstevel@tonic-gate cmd, sizeof (line) - 2, lineno);
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Discard the remaining lines until the
1537c478bd9Sstevel@tonic-gate * newline or EOF.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate while ((fgetsp = fgets(line, sizeof (line),
1567c478bd9Sstevel@tonic-gate fp)) != NULL)
1577c478bd9Sstevel@tonic-gate if (line[strlen(line) - 1] == '\n')
1587c478bd9Sstevel@tonic-gate break;
1597c478bd9Sstevel@tonic-gate if (fgetsp == NULL)
1607c478bd9Sstevel@tonic-gate endoffile = 1;
1617c478bd9Sstevel@tonic-gate continue;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate if (sscanf(line, "%s", adr) != 1) { /* Blank line, ignore */
1657c478bd9Sstevel@tonic-gate continue;
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if ((trailer = strpbrk(line, " \t")) == NULL) {
169*d362b749Svk if (warning)
1707c478bd9Sstevel@tonic-gate fprintf(stderr,
1717c478bd9Sstevel@tonic-gate "%s: Warning: no host names on line %d, "
1727c478bd9Sstevel@tonic-gate "ignored\n", cmd, lineno);
1737c478bd9Sstevel@tonic-gate continue;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * check for valid addresses
1787c478bd9Sstevel@tonic-gate *
1797c478bd9Sstevel@tonic-gate * Attempt an ipv4 conversion, this accepts all valid
1807c478bd9Sstevel@tonic-gate * ipv4 addresses including:
1817c478bd9Sstevel@tonic-gate * d
1827c478bd9Sstevel@tonic-gate * d.d
1837c478bd9Sstevel@tonic-gate * d.d.d
1847c478bd9Sstevel@tonic-gate * Unfortunately inet_pton() doesn't recognise these.
1857c478bd9Sstevel@tonic-gate */
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate in.s_addr = inet_addr(adr);
1887c478bd9Sstevel@tonic-gate if (-1 != (int)in.s_addr) {
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * It's safe not to check return of NULL as
1917c478bd9Sstevel@tonic-gate * nadrp is checked for validity later.
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate nadrp = inet_ntop(AF_INET, &in, nadr, sizeof (nadr));
1947c478bd9Sstevel@tonic-gate } else {
1957c478bd9Sstevel@tonic-gate nadrp = NULL; /* Not a valid IPv4 address */
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
19827092493Srs if (nadrp == NULL) {
19927092493Srs if (inet_pton(AF_INET6, adr, &in6) == 1) {
20027092493Srs nadrp = inet_ntop(AF_INET6, &in6,
20127092493Srs nadr, sizeof (nadr));
20227092493Srs }
20327092493Srs if (nadrp == NULL) { /* Invalid IPv6 too */
204*d362b749Svk if (warning)
2057c478bd9Sstevel@tonic-gate fprintf(stderr,
20627092493Srs "%s: Warning: malformed"
20727092493Srs " address on"
2087c478bd9Sstevel@tonic-gate " line %d, ignored\n",
2097c478bd9Sstevel@tonic-gate cmd, lineno);
2107c478bd9Sstevel@tonic-gate continue;
21127092493Srs } else if (ipv4) {
21227092493Srs continue; /* Ignore valid IPv6 */
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate verify_and_output(nadrp, trailer, lineno);
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate } /* while */
219a506a34cSth return (0);
2207c478bd9Sstevel@tonic-gate /* NOTREACHED */
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * verify_and_output
2257c478bd9Sstevel@tonic-gate *
2267c478bd9Sstevel@tonic-gate * Builds and verifies the output key and value string
2277c478bd9Sstevel@tonic-gate *
2287c478bd9Sstevel@tonic-gate * It makes sure these rules are followed:
2297c478bd9Sstevel@tonic-gate * key + separator + value <= OUTPUTSIZ (for ndbm)
2307c478bd9Sstevel@tonic-gate * names <= MAXALIASES + 1, ie one canonical name + MAXALIASES aliases
2317c478bd9Sstevel@tonic-gate * It will also ignore everything after a '#' comment character
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate static void
verify_and_output(const char * key,char * value,int lineno)2347c478bd9Sstevel@tonic-gate verify_and_output(const char *key, char *value, int lineno)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate char *p; /* General char pointer */
2377c478bd9Sstevel@tonic-gate char *endp; /* Points to the NULL at the end */
2387c478bd9Sstevel@tonic-gate char *namep; /* First character of a name */
2397c478bd9Sstevel@tonic-gate char tmpbuf[OUTPUTSIZ+1]; /* Buffer before writing out */
2407c478bd9Sstevel@tonic-gate char *tmpbufp = tmpbuf; /* Current point in output string */
2417c478bd9Sstevel@tonic-gate int n = 0; /* Length of output */
2427c478bd9Sstevel@tonic-gate int names = 0; /* Number of names found */
2437c478bd9Sstevel@tonic-gate int namelen; /* Length of the name */
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if (key) { /* Just in case key is NULL */
2467c478bd9Sstevel@tonic-gate n = strlen(key);
2477c478bd9Sstevel@tonic-gate if (n > OUTPUTSIZ) {
248*d362b749Svk if (warning)
2497c478bd9Sstevel@tonic-gate fprintf(stderr,
2507c478bd9Sstevel@tonic-gate "%s: address too long on "
2517c478bd9Sstevel@tonic-gate "line %d, line discarded\n",
2527c478bd9Sstevel@tonic-gate cmd, lineno);
2537c478bd9Sstevel@tonic-gate return;
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate memcpy(tmpbufp, key, n+1); /* Plus the '\0' */
2567c478bd9Sstevel@tonic-gate tmpbufp += n;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate if (value) { /* Just in case value is NULL */
2607c478bd9Sstevel@tonic-gate p = value;
2617c478bd9Sstevel@tonic-gate if ((endp = strchr(value, '#')) == 0) /* Ignore # comments */
2627c478bd9Sstevel@tonic-gate endp = p + strlen(p); /* Or endp = EOL */
2637c478bd9Sstevel@tonic-gate do {
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * Skip white space. Type conversion is
2667c478bd9Sstevel@tonic-gate * necessary to avoid unfortunate effects of
2677c478bd9Sstevel@tonic-gate * 8-bit characters appearing negative.
2687c478bd9Sstevel@tonic-gate */
2697c478bd9Sstevel@tonic-gate while ((p < endp) && isspace((unsigned char)*p))
2707c478bd9Sstevel@tonic-gate p++;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate if (p == endp) /* End of the string */
2737c478bd9Sstevel@tonic-gate break;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate names++;
2767c478bd9Sstevel@tonic-gate if (names > (MAXALIASES+1)) { /* cname + MAXALIASES */
277*d362b749Svk if (warning)
2787c478bd9Sstevel@tonic-gate fprintf(stderr,
2797c478bd9Sstevel@tonic-gate "%s: Warning: too many "
2807c478bd9Sstevel@tonic-gate "host names on line %d, "
2817c478bd9Sstevel@tonic-gate "truncating\n",
2827c478bd9Sstevel@tonic-gate cmd, lineno);
2837c478bd9Sstevel@tonic-gate break;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate namep = p;
2877c478bd9Sstevel@tonic-gate while ((p < endp) && !isspace((unsigned char)*p))
2887c478bd9Sstevel@tonic-gate p++;
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate namelen = p - namep;
2917c478bd9Sstevel@tonic-gate n += namelen + 1; /* single white space + name */
2927c478bd9Sstevel@tonic-gate *p = '\0'; /* Terminate the name string */
2937c478bd9Sstevel@tonic-gate if (n > OUTPUTSIZ) {
294*d362b749Svk if (warning)
2957c478bd9Sstevel@tonic-gate fprintf(stderr,
2967c478bd9Sstevel@tonic-gate "%s: Warning: %d byte ndbm limit "
2977c478bd9Sstevel@tonic-gate "reached on line %d, truncating\n",
2987c478bd9Sstevel@tonic-gate cmd, OUTPUTSIZ, lineno);
2997c478bd9Sstevel@tonic-gate break;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate if (names == 1) /* First space is a '\t' */
3037c478bd9Sstevel@tonic-gate *tmpbufp++ = '\t';
3047c478bd9Sstevel@tonic-gate else
3057c478bd9Sstevel@tonic-gate *tmpbufp++ = ' ';
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate memcpy(tmpbufp, namep, namelen+1); /* Plus the '\0' */
3087c478bd9Sstevel@tonic-gate tmpbufp += namelen;
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate if (p < endp)
3117c478bd9Sstevel@tonic-gate p++; /* Skip the added NULL */
3127c478bd9Sstevel@tonic-gate
3137c478bd9Sstevel@tonic-gate } while (p < endp);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate if (names > 0) {
3177c478bd9Sstevel@tonic-gate fputs(tmpbuf, stdout);
3187c478bd9Sstevel@tonic-gate fputc('\n', stdout);
3197c478bd9Sstevel@tonic-gate } else {
320*d362b749Svk if (warning)
3217c478bd9Sstevel@tonic-gate fprintf(stderr,
3227c478bd9Sstevel@tonic-gate "%s: Warning: no host names on line %d, "
3237c478bd9Sstevel@tonic-gate "ignored\n", cmd, lineno);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate }
326