xref: /netbsd-src/usr.sbin/ypserv/stdhosts/stdhosts.c (revision 8583b49ce082ccbe527cf4f10d9b2a51415c1fda)
1*8583b49cSjoerg /*	$NetBSD: stdhosts.c,v 1.20 2011/08/30 21:10:29 joerg Exp $	 */
260aa689cSthorpej 
360aa689cSthorpej /*
460aa689cSthorpej  * Copyright (c) 1994 Mats O Jansson <moj@stacken.kth.se>
560aa689cSthorpej  * All rights reserved.
660aa689cSthorpej  *
760aa689cSthorpej  * Redistribution and use in source and binary forms, with or without
860aa689cSthorpej  * modification, are permitted provided that the following conditions
960aa689cSthorpej  * are met:
1060aa689cSthorpej  * 1. Redistributions of source code must retain the above copyright
1160aa689cSthorpej  *    notice, this list of conditions and the following disclaimer.
1260aa689cSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1360aa689cSthorpej  *    notice, this list of conditions and the following disclaimer in the
1460aa689cSthorpej  *    documentation and/or other materials provided with the distribution.
1560aa689cSthorpej  *
1660aa689cSthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1760aa689cSthorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1860aa689cSthorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1960aa689cSthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2060aa689cSthorpej  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2160aa689cSthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2260aa689cSthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2360aa689cSthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2460aa689cSthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2560aa689cSthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2660aa689cSthorpej  * SUCH DAMAGE.
2760aa689cSthorpej  */
2860aa689cSthorpej 
2964718e58Slukem #include <sys/cdefs.h>
3064718e58Slukem #ifndef lint
31*8583b49cSjoerg __RCSID("$NetBSD: stdhosts.c,v 1.20 2011/08/30 21:10:29 joerg Exp $");
3264718e58Slukem #endif
3364718e58Slukem 
3460aa689cSthorpej #include <sys/types.h>
3560aa689cSthorpej #include <sys/socket.h>
3660aa689cSthorpej #include <netinet/in.h>
3760aa689cSthorpej #include <arpa/inet.h>
3860aa689cSthorpej #include <ctype.h>
3960aa689cSthorpej #include <err.h>
4060aa689cSthorpej #include <limits.h>
4160aa689cSthorpej #include <stdio.h>
42211a72a8Slukem #include <stdlib.h>
4360aa689cSthorpej #include <string.h>
4486384873Sitojun #include <unistd.h>
4586384873Sitojun #include <netdb.h>
4660aa689cSthorpej 
4760aa689cSthorpej #include "protos.h"
4860aa689cSthorpej 
49*8583b49cSjoerg __dead static void	usage(void);
5060aa689cSthorpej 
5160aa689cSthorpej int
main(int argc,char * argv[])52d532b687Swiz main(int argc, char *argv[])
5360aa689cSthorpej {
5460aa689cSthorpej 	struct in_addr host_addr;
55f4fb444bSlukem 	FILE	*data_file;
5641dc2c91Skleink 	size_t	 line_no;
57ade3ce97Sthorpej 	size_t	 len;
58d3d3aa62Slukem 	char	*line, *k, *v, *addr_string;
59d3d3aa62Slukem 	const char *fname;
6086384873Sitojun 	int	 ch;
6186384873Sitojun 	int	 af = 1 << 4;	/*IPv4*/
6286384873Sitojun 	struct addrinfo hints, *res;
6360aa689cSthorpej 
643d8138fcSthorpej 	addr_string = NULL;		/* XXX gcc -Wuninitialized */
653d8138fcSthorpej 
66e3af9d1dSad 	while ((ch = getopt(argc, argv, "n")) != -1) {
6786384873Sitojun 		switch (ch) {
6886384873Sitojun 		case 'n':
6986384873Sitojun 			af |= 1 << 6;	/*IPv6*/
7086384873Sitojun 			break;
7186384873Sitojun 		default:
7286384873Sitojun 			usage();
7386384873Sitojun 			/* NOTREACHED */
7486384873Sitojun 		}
7586384873Sitojun 	}
7686384873Sitojun 	argc -= optind;
7786384873Sitojun 	argv += optind;
7886384873Sitojun 
7986384873Sitojun 	if (argc > 1)
8060aa689cSthorpej 		usage();
8160aa689cSthorpej 
8286384873Sitojun 	if (argc == 1) {
8386384873Sitojun 		fname = argv[0];
8460aa689cSthorpej 		data_file = fopen(fname, "r");
8560aa689cSthorpej 		if (data_file == NULL)
8660aa689cSthorpej 			err(1, "%s", fname);
8760aa689cSthorpej 	} else {
8860aa689cSthorpej 		fname = "<stdin>";
8960aa689cSthorpej 		data_file = stdin;
9060aa689cSthorpej 	}
9160aa689cSthorpej 
92f4fb444bSlukem 	line_no = 0;
93211a72a8Slukem 	for (;
94811d7dcaSlukem 	    (line = fparseln(data_file, &len, &line_no, NULL,
95811d7dcaSlukem 		FPARSELN_UNESCALL));
96811d7dcaSlukem 	    free(line)) {
97211a72a8Slukem 		if (len == 0)
9860aa689cSthorpej 			continue;
9960aa689cSthorpej 
100811d7dcaSlukem 		v = line;
1013cca093eSdsl 		for (k = v; *v && !isspace((unsigned char)*v); v++)
102f4fb444bSlukem 			;
1033cca093eSdsl 		while (*v && isspace((unsigned char)*v))
104c03ad89dSchuck 			*v++ = '\0';
10560aa689cSthorpej 
10686384873Sitojun 		memset(&hints, 0, sizeof(hints));
10786384873Sitojun 		hints.ai_socktype = SOCK_DGRAM;		/*dummy*/
10886384873Sitojun 		hints.ai_flags = AI_NUMERICHOST;
10986384873Sitojun 
11086384873Sitojun 		if ((af & (1 << 4)) != 0 && inet_aton(k, &host_addr) == 1 &&
11186384873Sitojun 		    (addr_string = inet_ntoa(host_addr)) != NULL) {
11286384873Sitojun 			/* IPv4 */
11386384873Sitojun 			printf("%s %s\n", addr_string, v);
11486384873Sitojun 		} else if ((af & (1 << 6)) != 0 &&
11586384873Sitojun 			   getaddrinfo(k, "0", &hints, &res) == 0) {
11686384873Sitojun 			/* IPv6, with scope extension permitted */
11786384873Sitojun 			freeaddrinfo(res);
11886384873Sitojun 			printf("%s %s\n", k, v);
11986384873Sitojun 		} else
12041dc2c91Skleink 			warnx("%s line %lu: syntax error", fname,
12141dc2c91Skleink 			    (unsigned long)line_no);
12260aa689cSthorpej 	}
12360aa689cSthorpej 
12460aa689cSthorpej 	exit(0);
12560aa689cSthorpej }
12660aa689cSthorpej 
127*8583b49cSjoerg static void
usage(void)128d532b687Swiz usage(void)
12960aa689cSthorpej {
13060aa689cSthorpej 
13125bdbb66Scgd 	fprintf(stderr, "usage: %s [-n] [file]\n", getprogname());
13260aa689cSthorpej 	exit(1);
13360aa689cSthorpej }
134