xref: /netbsd-src/usr.bin/ypcat/ypcat.c (revision aea125ab8186b2576fe85b2e885bf759284eaf77)
1*aea125abSmatt /* $NetBSD: ypcat.c,v 1.18 2012/03/02 18:55:16 matt Exp $	*/
2b2fd6338Sthorpej 
377c037edSderaadt /*
43bf2b62fSderaadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
577c037edSderaadt  * All rights reserved.
677c037edSderaadt  *
777c037edSderaadt  * Redistribution and use in source and binary forms, with or without
877c037edSderaadt  * modification, are permitted provided that the following conditions
977c037edSderaadt  * are met:
1077c037edSderaadt  * 1. Redistributions of source code must retain the above copyright
1177c037edSderaadt  *    notice, this list of conditions and the following disclaimer.
1277c037edSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1377c037edSderaadt  *    notice, this list of conditions and the following disclaimer in the
1477c037edSderaadt  *    documentation and/or other materials provided with the distribution.
1577c037edSderaadt  *
1677c037edSderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1777c037edSderaadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1877c037edSderaadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1977c037edSderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2077c037edSderaadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2177c037edSderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2277c037edSderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2377c037edSderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2477c037edSderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2577c037edSderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2677c037edSderaadt  * SUCH DAMAGE.
2777c037edSderaadt  */
2877c037edSderaadt 
29981a9bdeSthorpej #include <sys/cdefs.h>
30981a9bdeSthorpej #ifndef lint
31*aea125abSmatt __RCSID("$NetBSD: ypcat.c,v 1.18 2012/03/02 18:55:16 matt Exp $");
3277c037edSderaadt #endif
3377c037edSderaadt 
34e39dac2fSderaadt #include <sys/param.h>
35e39dac2fSderaadt #include <sys/types.h>
36e39dac2fSderaadt #include <sys/socket.h>
37e5962c58Schristos #include <sys/errno.h>
38e39dac2fSderaadt #include <ctype.h>
39981a9bdeSthorpej #include <err.h>
40981a9bdeSthorpej #include <stdio.h>
41fcd0fb11Smatt #include <stdlib.h>
42fcd0fb11Smatt #include <string.h>
43981a9bdeSthorpej #include <unistd.h>
44e39dac2fSderaadt 
45e39dac2fSderaadt #include <rpc/rpc.h>
46e39dac2fSderaadt #include <rpc/xdr.h>
47e39dac2fSderaadt #include <rpcsvc/yp_prot.h>
48e39dac2fSderaadt #include <rpcsvc/ypclnt.h>
49e39dac2fSderaadt 
50e5962c58Schristos #include "ypalias_init.h"
51e39dac2fSderaadt 
52e5962c58Schristos static int	printit(int, char *, int, char *, int, char *);
53e5962c58Schristos static void	usage(void) __attribute__((__noreturn__));
54981a9bdeSthorpej 
551a231f46Schristos static int	compressspace;
561a231f46Schristos 
57e39dac2fSderaadt 
58981a9bdeSthorpej int
main(int argc,char * argv[])59e5962c58Schristos main(int argc, char *argv[])
60e39dac2fSderaadt {
61*aea125abSmatt 	char *domainname, *b_retry_cnt;
62981a9bdeSthorpej 	struct ypall_callback ypcb;
6338a78fe6Slukem 	const char *inmap;
64981a9bdeSthorpej 	int notrans;
6538a78fe6Slukem 	int c, r;
6638a78fe6Slukem 	size_t i;
67e5962c58Schristos 	const struct ypalias *ypaliases;
68e5962c58Schristos 	int key;
69981a9bdeSthorpej 
70e5962c58Schristos 	setprogname(*argv);
71*aea125abSmatt 	domainname = b_retry_cnt = NULL;
72981a9bdeSthorpej 	notrans = key = 0;
73e5962c58Schristos 	ypaliases = ypalias_init();
742b01a8adSchristos 	while((c = getopt(argc, argv, "bd:kstx")) != -1) {
75981a9bdeSthorpej 		switch (c) {
762b01a8adSchristos 		case 'b':
772b01a8adSchristos 			b_retry_cnt = optarg;
782b01a8adSchristos 			break;
792b01a8adSchristos 
801a231f46Schristos 		case 'd':
811a231f46Schristos 			domainname = optarg;
821a231f46Schristos 			break;
831a231f46Schristos 
841a231f46Schristos 		case 'k':
851a231f46Schristos 			key++;
861a231f46Schristos 			break;
871a231f46Schristos 
881a231f46Schristos 		case 's':
891a231f46Schristos 			compressspace++;
901a231f46Schristos 			break;
911a231f46Schristos 
92981a9bdeSthorpej 		case 'x':
93e5962c58Schristos 			for (i = 0; ypaliases[i].alias; i++)
94981a9bdeSthorpej 				printf("Use \"%s\" for \"%s\"\n",
95981a9bdeSthorpej 				    ypaliases[i].alias,
96981a9bdeSthorpej 				    ypaliases[i].name);
97e5962c58Schristos 			return 0;
98981a9bdeSthorpej 
99981a9bdeSthorpej 		case 't':
100981a9bdeSthorpej 			notrans++;
101981a9bdeSthorpej 			break;
102981a9bdeSthorpej 
103981a9bdeSthorpej 		default:
104981a9bdeSthorpej 			usage();
105981a9bdeSthorpej 		}
106e39dac2fSderaadt 	}
107e39dac2fSderaadt 
108981a9bdeSthorpej 	argc -= optind;
109981a9bdeSthorpej 	argv += optind;
110981a9bdeSthorpej 
111981a9bdeSthorpej 	if (argc != 1)
112981a9bdeSthorpej 		usage();
113981a9bdeSthorpej 
1142b01a8adSchristos 	if (b_retry_cnt != NULL) {
1152b01a8adSchristos 		char *s;
1162b01a8adSchristos 		unsigned long l;
1172b01a8adSchristos 
1182b01a8adSchristos 		l = strtoul(b_retry_cnt, &s, 10);
1192b01a8adSchristos 		if (*s != '\0' || l > 0xffff) usage();
1202b01a8adSchristos 		yp_setbindtries((int)l);
1212b01a8adSchristos 	}
1222b01a8adSchristos 
123981a9bdeSthorpej 	if (domainname == NULL)
124981a9bdeSthorpej 		yp_get_default_domain(&domainname);
125981a9bdeSthorpej 
126981a9bdeSthorpej 	inmap = argv[0];
127981a9bdeSthorpej 	if (notrans == 0) {
128e5962c58Schristos 		for (i = 0; ypaliases[i].alias; i++)
129981a9bdeSthorpej 			if (strcmp(inmap, ypaliases[i].alias) == 0)
130981a9bdeSthorpej 				inmap = ypaliases[i].name;
131981a9bdeSthorpej 	}
132981a9bdeSthorpej 
133981a9bdeSthorpej 	ypcb.foreach = printit;
134e5962c58Schristos 	ypcb.data = key ? (void *)&key : NULL;
135981a9bdeSthorpej 
136981a9bdeSthorpej 	r = yp_all(domainname, inmap, &ypcb);
137981a9bdeSthorpej 	switch (r) {
138981a9bdeSthorpej 	case 0:
139981a9bdeSthorpej 		break;
140981a9bdeSthorpej 
141981a9bdeSthorpej 	case YPERR_YPBIND:
142981a9bdeSthorpej 		errx(1, "not running ypbind");
143981a9bdeSthorpej 
144981a9bdeSthorpej 	default:
145981a9bdeSthorpej 		errx(1, "no such map %s.  Reason: %s", inmap, yperr_string(r));
146981a9bdeSthorpej 	}
147e5962c58Schristos 	return 0;
148981a9bdeSthorpej }
149981a9bdeSthorpej 
150e5962c58Schristos static int
printit(int instatus,char * inkey,int inkeylen,char * inval,int invallen,char * indata)151e5962c58Schristos printit(int instatus, char *inkey, int inkeylen, char *inval,
152e5962c58Schristos     int invallen, char *indata)
153e39dac2fSderaadt {
154981a9bdeSthorpej 
155e39dac2fSderaadt 	if (instatus != YP_TRUE)
156e39dac2fSderaadt 		return instatus;
157e5962c58Schristos 	if (indata)
158e5962c58Schristos 		(void)printf("%*.*s", inkeylen, inkeylen, inkey);
1591a231f46Schristos 	if (invallen) {
1601a231f46Schristos 		if (indata)
1611a231f46Schristos 			(void)putc(' ', stdout);
1621a231f46Schristos 		if (compressspace) {
1631a231f46Schristos 			int i;
1641a231f46Schristos 			int hadspace = 0;
1651a231f46Schristos 
1661a231f46Schristos 			for (i = 0; i < invallen; i++) {
1671a231f46Schristos 				if (isspace((unsigned char)inval[i])) {
1681a231f46Schristos 					if (hadspace)
1691a231f46Schristos 						continue;
1701a231f46Schristos 					hadspace = 1;
1711a231f46Schristos 					(void)putc(' ', stdout);
1721a231f46Schristos 				} else {
1731a231f46Schristos 					hadspace = 0;
1741a231f46Schristos 					(void)putc(inval[i], stdout);
1751a231f46Schristos 				}
1761a231f46Schristos 			}
1771a231f46Schristos 		} else
1781a231f46Schristos 			(void)printf("%*.*s", invallen, invallen, inval);
1791a231f46Schristos 	}
1801a231f46Schristos 	(void)putc('\n', stdout);
181e39dac2fSderaadt 	return 0;
182e39dac2fSderaadt }
183e39dac2fSderaadt 
184e5962c58Schristos static void
usage(void)185e5962c58Schristos usage(void)
186e39dac2fSderaadt {
187e39dac2fSderaadt 
1882b01a8adSchristos 	(void)fprintf(stderr, "Usage: %s [-kst] [-b <num-retry> "
1892b01a8adSchristos 	    "[-d <domainname>] <mapname>\n", getprogname());
190e5962c58Schristos 	(void)fprintf(stderr, "       %s -x\n", getprogname());
191e39dac2fSderaadt 	exit(1);
192e39dac2fSderaadt }
193