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