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