1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 1985-1999 by Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SMI4.1 1.7 */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdio.h> 30*0Sstevel@tonic-gate #include <ndbm.h> 31*0Sstevel@tonic-gate #include <netdb.h> 32*0Sstevel@tonic-gate #include <sys/types.h> 33*0Sstevel@tonic-gate #include <sys/socket.h> 34*0Sstevel@tonic-gate #include <netinet/in.h> 35*0Sstevel@tonic-gate #include <arpa/inet.h> 36*0Sstevel@tonic-gate #include <string.h> 37*0Sstevel@tonic-gate #include <ctype.h> 38*0Sstevel@tonic-gate #include <errno.h> 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate /* 41*0Sstevel@tonic-gate * Filter to convert both IPv4 and IPv6 addresses from /etc/hosts or 42*0Sstevel@tonic-gate * /etc/inet/ipnodes files. 43*0Sstevel@tonic-gate */ 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * Size of buffer for input lines. Add two bytes on input for newline 47*0Sstevel@tonic-gate * and terminating NULL. Note that the practical limit for data 48*0Sstevel@tonic-gate * storage in ndbm is (PBLKSIZ - 3 * sizeof (short)). Though this 49*0Sstevel@tonic-gate * differs from spec 1170 the common industry implementation does 50*0Sstevel@tonic-gate * conform to this slightly lower limit. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #define OUTPUTSIZ (PBLKSIZ - 3 * sizeof (short)) 54*0Sstevel@tonic-gate #define INPUTSIZ (OUTPUTSIZ + 2) 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate static int ipv4 = -1; 57*0Sstevel@tonic-gate static char *cmd; 58*0Sstevel@tonic-gate int warn = 0; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate static void verify_and_output(const char *key, char *value, int lineno); 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate void 63*0Sstevel@tonic-gate usage() 64*0Sstevel@tonic-gate { 65*0Sstevel@tonic-gate fprintf(stderr, "stdhosts [-w] [-n] [in-file]\n"); 66*0Sstevel@tonic-gate fprintf(stderr, "\t-w\tprint malformed warning messages.\n"); 67*0Sstevel@tonic-gate exit(1); 68*0Sstevel@tonic-gate } 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate main(argc, argv) 71*0Sstevel@tonic-gate char **argv; 72*0Sstevel@tonic-gate { 73*0Sstevel@tonic-gate char line[INPUTSIZ]; 74*0Sstevel@tonic-gate char adr[INPUTSIZ]; 75*0Sstevel@tonic-gate char nadr[INET6_ADDRSTRLEN]; /* Contains normalised address */ 76*0Sstevel@tonic-gate const char *nadrp; /* Pointer to the normalised address */ 77*0Sstevel@tonic-gate char *trailer; 78*0Sstevel@tonic-gate char *commentp; /* Pointer to comment character '#' */ 79*0Sstevel@tonic-gate int c; 80*0Sstevel@tonic-gate FILE *fp; 81*0Sstevel@tonic-gate int lineno = 0; /* Input line counter */ 82*0Sstevel@tonic-gate struct in_addr in; /* Used for normalising the IPv4 address */ 83*0Sstevel@tonic-gate struct in6_addr in6; /* Used for normalising the IPv6 address */ 84*0Sstevel@tonic-gate char *fgetsp; /* Holds return value for fgets() calls */ 85*0Sstevel@tonic-gate int endoffile = 0; /* Set when end of file reached */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate if (cmd = strrchr(argv[0], '/')) 88*0Sstevel@tonic-gate ++cmd; 89*0Sstevel@tonic-gate else 90*0Sstevel@tonic-gate cmd = argv[0]; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "v:wn")) != -1) { 93*0Sstevel@tonic-gate switch (c) { 94*0Sstevel@tonic-gate case 'w': /* Send warning messages to stderr */ 95*0Sstevel@tonic-gate warn = 1; 96*0Sstevel@tonic-gate break; 97*0Sstevel@tonic-gate case 'n': 98*0Sstevel@tonic-gate ipv4 = 0; 99*0Sstevel@tonic-gate break; 100*0Sstevel@tonic-gate default: 101*0Sstevel@tonic-gate usage(); 102*0Sstevel@tonic-gate exit(1); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate if (optind < argc) { 107*0Sstevel@tonic-gate fp = fopen(argv[optind], "r"); 108*0Sstevel@tonic-gate if (fp == NULL) { 109*0Sstevel@tonic-gate fprintf(stderr, "%s: can't open %s\n", 110*0Sstevel@tonic-gate cmd, argv[optind]); 111*0Sstevel@tonic-gate exit(1); 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate } else 114*0Sstevel@tonic-gate fp = stdin; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate while (!endoffile && 117*0Sstevel@tonic-gate (fgetsp = fgets(line, sizeof (line), fp)) != NULL) { 118*0Sstevel@tonic-gate lineno++; 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate /* Check for comments */ 121*0Sstevel@tonic-gate if ((commentp = strchr(line, '#')) != NULL) { 122*0Sstevel@tonic-gate if ((line[strlen(line) - 1] != '\n') && 123*0Sstevel@tonic-gate (strlen(line) >= (sizeof (line) - 1))) { 124*0Sstevel@tonic-gate /* 125*0Sstevel@tonic-gate * Discard the remainder of the line 126*0Sstevel@tonic-gate * until the newline or EOF, then 127*0Sstevel@tonic-gate * continue to parse the line. Use 128*0Sstevel@tonic-gate * adr[] rather then line[] to 129*0Sstevel@tonic-gate * preserve the contents of line[]. 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate while ((fgetsp = fgets(adr, sizeof (adr), 132*0Sstevel@tonic-gate fp)) != NULL) { 133*0Sstevel@tonic-gate if (adr[strlen(adr) - 1] == '\n') 134*0Sstevel@tonic-gate break; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate if (fgetsp == NULL) 137*0Sstevel@tonic-gate endoffile = 1; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate /* Terminate line[] at the comment character */ 140*0Sstevel@tonic-gate *commentp = '\0'; 141*0Sstevel@tonic-gate } else if ((line[strlen(line) - 1] != '\n') && 142*0Sstevel@tonic-gate (strlen(line) >= (sizeof (line) - 1))) { 143*0Sstevel@tonic-gate /* 144*0Sstevel@tonic-gate * Catch long lines but not if this is a short 145*0Sstevel@tonic-gate * line with no '\n' at the end of the input. 146*0Sstevel@tonic-gate */ 147*0Sstevel@tonic-gate if (warn) 148*0Sstevel@tonic-gate fprintf(stderr, 149*0Sstevel@tonic-gate "%s: Warning: more than %d " 150*0Sstevel@tonic-gate "bytes on line %d, ignored\n", 151*0Sstevel@tonic-gate cmd, sizeof (line) - 2, lineno); 152*0Sstevel@tonic-gate /* 153*0Sstevel@tonic-gate * Discard the remaining lines until the 154*0Sstevel@tonic-gate * newline or EOF. 155*0Sstevel@tonic-gate */ 156*0Sstevel@tonic-gate while ((fgetsp = fgets(line, sizeof (line), 157*0Sstevel@tonic-gate fp)) != NULL) 158*0Sstevel@tonic-gate if (line[strlen(line) - 1] == '\n') 159*0Sstevel@tonic-gate break; 160*0Sstevel@tonic-gate if (fgetsp == NULL) 161*0Sstevel@tonic-gate endoffile = 1; 162*0Sstevel@tonic-gate continue; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate if (sscanf(line, "%s", adr) != 1) { /* Blank line, ignore */ 166*0Sstevel@tonic-gate continue; 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate if ((trailer = strpbrk(line, " \t")) == NULL) { 170*0Sstevel@tonic-gate if (warn) 171*0Sstevel@tonic-gate fprintf(stderr, 172*0Sstevel@tonic-gate "%s: Warning: no host names on line %d, " 173*0Sstevel@tonic-gate "ignored\n", cmd, lineno); 174*0Sstevel@tonic-gate continue; 175*0Sstevel@tonic-gate } 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate /* 178*0Sstevel@tonic-gate * check for valid addresses 179*0Sstevel@tonic-gate * 180*0Sstevel@tonic-gate * Attempt an ipv4 conversion, this accepts all valid 181*0Sstevel@tonic-gate * ipv4 addresses including: 182*0Sstevel@tonic-gate * d 183*0Sstevel@tonic-gate * d.d 184*0Sstevel@tonic-gate * d.d.d 185*0Sstevel@tonic-gate * Unfortunately inet_pton() doesn't recognise these. 186*0Sstevel@tonic-gate */ 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate in.s_addr = inet_addr(adr); 189*0Sstevel@tonic-gate if (-1 != (int)in.s_addr) { 190*0Sstevel@tonic-gate /* 191*0Sstevel@tonic-gate * It's safe not to check return of NULL as 192*0Sstevel@tonic-gate * nadrp is checked for validity later. 193*0Sstevel@tonic-gate */ 194*0Sstevel@tonic-gate nadrp = inet_ntop(AF_INET, &in, nadr, sizeof (nadr)); 195*0Sstevel@tonic-gate } else { 196*0Sstevel@tonic-gate nadrp = NULL; /* Not a valid IPv4 address */ 197*0Sstevel@tonic-gate } 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate if (ipv4) { 200*0Sstevel@tonic-gate if (nadrp == NULL) { 201*0Sstevel@tonic-gate if (warn) 202*0Sstevel@tonic-gate fprintf(stderr, 203*0Sstevel@tonic-gate "%s: Warning: malformed address on" 204*0Sstevel@tonic-gate " line %d, ignored\n", 205*0Sstevel@tonic-gate cmd, lineno); 206*0Sstevel@tonic-gate continue; 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate } else { /* v4 or v6 for ipnodes */ 209*0Sstevel@tonic-gate if (nadrp == NULL) { 210*0Sstevel@tonic-gate if (inet_pton(AF_INET6, adr, &in6) == 1) { 211*0Sstevel@tonic-gate nadrp = inet_ntop(AF_INET6, &in6, 212*0Sstevel@tonic-gate nadr, sizeof (nadr)); 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate if (nadrp == NULL) { /* Invalid IPv6 too */ 215*0Sstevel@tonic-gate if (warn) 216*0Sstevel@tonic-gate fprintf(stderr, 217*0Sstevel@tonic-gate "%s: Warning: malformed" 218*0Sstevel@tonic-gate " address on" 219*0Sstevel@tonic-gate " line %d, ignored\n", 220*0Sstevel@tonic-gate cmd, lineno); 221*0Sstevel@tonic-gate continue; 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate verify_and_output(nadrp, trailer, lineno); 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate } /* while */ 229*0Sstevel@tonic-gate exit(0); 230*0Sstevel@tonic-gate /* NOTREACHED */ 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate /* 234*0Sstevel@tonic-gate * verify_and_output 235*0Sstevel@tonic-gate * 236*0Sstevel@tonic-gate * Builds and verifies the output key and value string 237*0Sstevel@tonic-gate * 238*0Sstevel@tonic-gate * It makes sure these rules are followed: 239*0Sstevel@tonic-gate * key + separator + value <= OUTPUTSIZ (for ndbm) 240*0Sstevel@tonic-gate * names <= MAXALIASES + 1, ie one canonical name + MAXALIASES aliases 241*0Sstevel@tonic-gate * It will also ignore everything after a '#' comment character 242*0Sstevel@tonic-gate */ 243*0Sstevel@tonic-gate static void 244*0Sstevel@tonic-gate verify_and_output(const char *key, char *value, int lineno) 245*0Sstevel@tonic-gate { 246*0Sstevel@tonic-gate char *p; /* General char pointer */ 247*0Sstevel@tonic-gate char *endp; /* Points to the NULL at the end */ 248*0Sstevel@tonic-gate char *namep; /* First character of a name */ 249*0Sstevel@tonic-gate char tmpbuf[OUTPUTSIZ+1]; /* Buffer before writing out */ 250*0Sstevel@tonic-gate char *tmpbufp = tmpbuf; /* Current point in output string */ 251*0Sstevel@tonic-gate int n = 0; /* Length of output */ 252*0Sstevel@tonic-gate int names = 0; /* Number of names found */ 253*0Sstevel@tonic-gate int namelen; /* Length of the name */ 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate if (key) { /* Just in case key is NULL */ 256*0Sstevel@tonic-gate n = strlen(key); 257*0Sstevel@tonic-gate if (n > OUTPUTSIZ) { 258*0Sstevel@tonic-gate if (warn) 259*0Sstevel@tonic-gate fprintf(stderr, 260*0Sstevel@tonic-gate "%s: address too long on " 261*0Sstevel@tonic-gate "line %d, line discarded\n", 262*0Sstevel@tonic-gate cmd, lineno); 263*0Sstevel@tonic-gate return; 264*0Sstevel@tonic-gate } 265*0Sstevel@tonic-gate memcpy(tmpbufp, key, n+1); /* Plus the '\0' */ 266*0Sstevel@tonic-gate tmpbufp += n; 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate if (value) { /* Just in case value is NULL */ 270*0Sstevel@tonic-gate p = value; 271*0Sstevel@tonic-gate if ((endp = strchr(value, '#')) == 0) /* Ignore # comments */ 272*0Sstevel@tonic-gate endp = p + strlen(p); /* Or endp = EOL */ 273*0Sstevel@tonic-gate do { 274*0Sstevel@tonic-gate /* 275*0Sstevel@tonic-gate * Skip white space. Type conversion is 276*0Sstevel@tonic-gate * necessary to avoid unfortunate effects of 277*0Sstevel@tonic-gate * 8-bit characters appearing negative. 278*0Sstevel@tonic-gate */ 279*0Sstevel@tonic-gate while ((p < endp) && isspace((unsigned char)*p)) 280*0Sstevel@tonic-gate p++; 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate if (p == endp) /* End of the string */ 283*0Sstevel@tonic-gate break; 284*0Sstevel@tonic-gate 285*0Sstevel@tonic-gate names++; 286*0Sstevel@tonic-gate if (names > (MAXALIASES+1)) { /* cname + MAXALIASES */ 287*0Sstevel@tonic-gate if (warn) 288*0Sstevel@tonic-gate fprintf(stderr, 289*0Sstevel@tonic-gate "%s: Warning: too many " 290*0Sstevel@tonic-gate "host names on line %d, " 291*0Sstevel@tonic-gate "truncating\n", 292*0Sstevel@tonic-gate cmd, lineno); 293*0Sstevel@tonic-gate break; 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate namep = p; 297*0Sstevel@tonic-gate while ((p < endp) && !isspace((unsigned char)*p)) 298*0Sstevel@tonic-gate p++; 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate namelen = p - namep; 301*0Sstevel@tonic-gate n += namelen + 1; /* single white space + name */ 302*0Sstevel@tonic-gate *p = '\0'; /* Terminate the name string */ 303*0Sstevel@tonic-gate if (n > OUTPUTSIZ) { 304*0Sstevel@tonic-gate if (warn) 305*0Sstevel@tonic-gate fprintf(stderr, 306*0Sstevel@tonic-gate "%s: Warning: %d byte ndbm limit " 307*0Sstevel@tonic-gate "reached on line %d, truncating\n", 308*0Sstevel@tonic-gate cmd, OUTPUTSIZ, lineno); 309*0Sstevel@tonic-gate break; 310*0Sstevel@tonic-gate } 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate if (names == 1) /* First space is a '\t' */ 313*0Sstevel@tonic-gate *tmpbufp++ = '\t'; 314*0Sstevel@tonic-gate else 315*0Sstevel@tonic-gate *tmpbufp++ = ' '; 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate memcpy(tmpbufp, namep, namelen+1); /* Plus the '\0' */ 318*0Sstevel@tonic-gate tmpbufp += namelen; 319*0Sstevel@tonic-gate 320*0Sstevel@tonic-gate if (p < endp) 321*0Sstevel@tonic-gate p++; /* Skip the added NULL */ 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate } while (p < endp); 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate if (names > 0) { 327*0Sstevel@tonic-gate fputs(tmpbuf, stdout); 328*0Sstevel@tonic-gate fputc('\n', stdout); 329*0Sstevel@tonic-gate } else { 330*0Sstevel@tonic-gate if (warn) 331*0Sstevel@tonic-gate fprintf(stderr, 332*0Sstevel@tonic-gate "%s: Warning: no host names on line %d, " 333*0Sstevel@tonic-gate "ignored\n", cmd, lineno); 334*0Sstevel@tonic-gate } 335*0Sstevel@tonic-gate } 336