xref: /freebsd/usr.bin/ypcat/ypcat.c (revision 082d8262b020d11a8f0ea5c5a4ad4fea18872a56)
173407b34SGarrett Wollman /*
273407b34SGarrett Wollman  * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
373407b34SGarrett Wollman  * All rights reserved.
473407b34SGarrett Wollman  *
573407b34SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
673407b34SGarrett Wollman  * modification, are permitted provided that the following conditions
773407b34SGarrett Wollman  * are met:
873407b34SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
973407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1073407b34SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1173407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1273407b34SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1373407b34SGarrett Wollman  * 3. The name of the author may not be used to endorse or promote
1473407b34SGarrett Wollman  *    products derived from this software without specific prior written
1573407b34SGarrett Wollman  *    permission.
1673407b34SGarrett Wollman  *
1773407b34SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1873407b34SGarrett Wollman  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1973407b34SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2073407b34SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2173407b34SGarrett Wollman  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2273407b34SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2373407b34SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2473407b34SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2573407b34SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2673407b34SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2773407b34SGarrett Wollman  * SUCH DAMAGE.
2873407b34SGarrett Wollman  */
2973407b34SGarrett Wollman 
30082d8262SMark Murray #include <sys/cdefs.h>
31082d8262SMark Murray 
32082d8262SMark Murray __FBSDID("$FreeBSD$");
3373407b34SGarrett Wollman 
3473407b34SGarrett Wollman #include <sys/param.h>
3573407b34SGarrett Wollman #include <sys/types.h>
3673407b34SGarrett Wollman #include <sys/socket.h>
37082d8262SMark Murray 
38082d8262SMark Murray #include <rpc/rpc.h>
39082d8262SMark Murray #include <rpc/xdr.h>
40082d8262SMark Murray #include <rpcsvc/yp_prot.h>
41082d8262SMark Murray #include <rpcsvc/ypclnt.h>
42082d8262SMark Murray 
4373407b34SGarrett Wollman #include <ctype.h>
44c1f84cc1SPhilippe Charnier #include <err.h>
45c1f84cc1SPhilippe Charnier #include <stdio.h>
467a19d1bbSDima Dorfman #include <stdlib.h>
477a19d1bbSDima Dorfman #include <string.h>
48c1f84cc1SPhilippe Charnier #include <unistd.h>
4973407b34SGarrett Wollman 
5073407b34SGarrett Wollman struct ypalias {
51082d8262SMark Murray 	const char *alias, *name;
5273407b34SGarrett Wollman } ypaliases[] = {
5373407b34SGarrett Wollman 	{ "passwd", "passwd.byname" },
54d228e65cSPeter Wemm 	{ "master.passwd", "master.passwd.byname" },
5573407b34SGarrett Wollman 	{ "group", "group.byname" },
5673407b34SGarrett Wollman 	{ "networks", "networks.byaddr" },
5773407b34SGarrett Wollman 	{ "hosts", "hosts.byaddr" },
5873407b34SGarrett Wollman 	{ "protocols", "protocols.bynumber" },
5973407b34SGarrett Wollman 	{ "services", "services.byname" },
6073407b34SGarrett Wollman 	{ "aliases", "mail.aliases" },
6173407b34SGarrett Wollman 	{ "ethers", "ethers.byname" },
6273407b34SGarrett Wollman };
6373407b34SGarrett Wollman 
6473407b34SGarrett Wollman int key;
6573407b34SGarrett Wollman 
66c1f84cc1SPhilippe Charnier static void
67082d8262SMark Murray usage(void)
6873407b34SGarrett Wollman {
69c1f84cc1SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
70c1f84cc1SPhilippe Charnier 		"usage: ypcat [-k] [-d domainname] [-t] mapname",
71c1f84cc1SPhilippe Charnier 		"       ypcat -x");
7273407b34SGarrett Wollman 	exit(1);
7373407b34SGarrett Wollman }
7473407b34SGarrett Wollman 
75082d8262SMark Murray static int
76082d8262SMark Murray printit(unsigned long instatus, char *inkey, int inkeylen, char *inval, int invallen, void *dummy __unused)
7773407b34SGarrett Wollman {
7873407b34SGarrett Wollman 	if(instatus != YP_TRUE)
7973407b34SGarrett Wollman 		return instatus;
8073407b34SGarrett Wollman 	if(key)
8173407b34SGarrett Wollman 		printf("%*.*s ", inkeylen, inkeylen, inkey);
8273407b34SGarrett Wollman 	printf("%*.*s\n", invallen, invallen, inval);
8373407b34SGarrett Wollman 	return 0;
8473407b34SGarrett Wollman }
8573407b34SGarrett Wollman 
8673407b34SGarrett Wollman int
87082d8262SMark Murray main(int argc, char *argv[])
8873407b34SGarrett Wollman {
89c1f84cc1SPhilippe Charnier 	char *domainname = NULL;
9073407b34SGarrett Wollman 	struct ypall_callback ypcb;
9173407b34SGarrett Wollman 	char *inmap;
9273407b34SGarrett Wollman 	int notrans;
93082d8262SMark Murray 	int c, r;
94082d8262SMark Murray 	u_int i;
9573407b34SGarrett Wollman 
9673407b34SGarrett Wollman 	notrans = key = 0;
9773407b34SGarrett Wollman 
9873407b34SGarrett Wollman 	while( (c=getopt(argc, argv, "xd:kt")) != -1)
9973407b34SGarrett Wollman 		switch(c) {
10073407b34SGarrett Wollman 		case 'x':
10173407b34SGarrett Wollman 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
10273407b34SGarrett Wollman 				printf("Use \"%s\" for \"%s\"\n",
10373407b34SGarrett Wollman 					ypaliases[i].alias,
10473407b34SGarrett Wollman 					ypaliases[i].name);
10573407b34SGarrett Wollman 			exit(0);
10673407b34SGarrett Wollman 		case 'd':
10773407b34SGarrett Wollman 			domainname = optarg;
10873407b34SGarrett Wollman 			break;
10973407b34SGarrett Wollman 		case 't':
11073407b34SGarrett Wollman 			notrans++;
11173407b34SGarrett Wollman 			break;
11273407b34SGarrett Wollman 		case 'k':
11373407b34SGarrett Wollman 			key++;
11473407b34SGarrett Wollman 			break;
11573407b34SGarrett Wollman 		default:
11673407b34SGarrett Wollman 			usage();
11773407b34SGarrett Wollman 		}
11873407b34SGarrett Wollman 
11973407b34SGarrett Wollman 	if(optind + 1 != argc )
12073407b34SGarrett Wollman 		usage();
12173407b34SGarrett Wollman 
122c1f84cc1SPhilippe Charnier 	if (!domainname)
123c1f84cc1SPhilippe Charnier 		yp_get_default_domain(&domainname);
124c1f84cc1SPhilippe Charnier 
12573407b34SGarrett Wollman 	inmap = argv[optind];
12673407b34SGarrett Wollman 	for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
12773407b34SGarrett Wollman 		if( strcmp(inmap, ypaliases[i].alias) == 0)
12873407b34SGarrett Wollman 			inmap = ypaliases[i].name;
12973407b34SGarrett Wollman 	ypcb.foreach = printit;
13073407b34SGarrett Wollman 	ypcb.data = NULL;
13173407b34SGarrett Wollman 
13273407b34SGarrett Wollman 	r = yp_all(domainname, inmap, &ypcb);
13373407b34SGarrett Wollman 	switch(r) {
13473407b34SGarrett Wollman 	case 0:
13573407b34SGarrett Wollman 		break;
13673407b34SGarrett Wollman 	case YPERR_YPBIND:
137c1f84cc1SPhilippe Charnier 		errx(1, "not running ypbind");
13873407b34SGarrett Wollman 	default:
139c1f84cc1SPhilippe Charnier 		errx(1, "no such map %s. reason: %s", inmap, yperr_string(r));
14073407b34SGarrett Wollman 	}
14173407b34SGarrett Wollman 	exit(0);
14273407b34SGarrett Wollman }
143