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