xref: /freebsd/usr.bin/ypcat/ypcat.c (revision e5d258c9e599d2b2fe642e678091cac5da8a10d1)
1c1f70e37SMarcelo Araujo /*	$OpenBSD: ypcat.c,v 1.16 2015/02/08 23:40:35 deraadt Exp $ */
2c1f70e37SMarcelo Araujo 
31de7b4b8SPedro F. Giffuni /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni  *
6c1f70e37SMarcelo Araujo  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
773407b34SGarrett Wollman  * All rights reserved.
873407b34SGarrett Wollman  *
973407b34SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
1073407b34SGarrett Wollman  * modification, are permitted provided that the following conditions
1173407b34SGarrett Wollman  * are met:
1273407b34SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
1373407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1473407b34SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1573407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1673407b34SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1773407b34SGarrett Wollman  *
1873407b34SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1973407b34SGarrett Wollman  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2073407b34SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2173407b34SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2273407b34SGarrett Wollman  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2373407b34SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2473407b34SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2573407b34SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2673407b34SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2773407b34SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2873407b34SGarrett Wollman  * SUCH DAMAGE.
2973407b34SGarrett Wollman  */
3073407b34SGarrett Wollman 
3173407b34SGarrett Wollman 
3273407b34SGarrett Wollman #include <sys/param.h>
3373407b34SGarrett Wollman #include <sys/types.h>
3473407b34SGarrett Wollman #include <sys/socket.h>
35c52bab11SMarcelo Araujo 
36c1f70e37SMarcelo Araujo #include <ctype.h>
37c1f70e37SMarcelo Araujo #include <err.h>
38c52bab11SMarcelo Araujo #include <stdio.h>
39c52bab11SMarcelo Araujo #include <stdlib.h>
40c52bab11SMarcelo Araujo #include <string.h>
41c52bab11SMarcelo Araujo #include <unistd.h>
42082d8262SMark Murray 
43082d8262SMark Murray #include <rpc/rpc.h>
44082d8262SMark Murray #include <rpc/xdr.h>
45c1f70e37SMarcelo Araujo #include <rpcsvc/yp.h>
46082d8262SMark Murray #include <rpcsvc/ypclnt.h>
47082d8262SMark Murray 
48006a388bSMarcelo Araujo static const struct ypalias {
49ba7887c7SPeter Wemm 	char *alias, *name;
50006a388bSMarcelo Araujo } ypaliases[] = {
5173407b34SGarrett Wollman 	{ "passwd", "passwd.byname" },
52d228e65cSPeter Wemm 	{ "master.passwd", "master.passwd.byname" },
5353c40578SBrian Somers 	{ "shadow", "shadow.byname" },
5473407b34SGarrett Wollman 	{ "group", "group.byname" },
5573407b34SGarrett Wollman 	{ "networks", "networks.byaddr" },
5673407b34SGarrett Wollman 	{ "hosts", "hosts.byaddr" },
5773407b34SGarrett Wollman 	{ "protocols", "protocols.bynumber" },
5873407b34SGarrett Wollman 	{ "services", "services.byname" },
5973407b34SGarrett Wollman 	{ "aliases", "mail.aliases" },
6073407b34SGarrett Wollman 	{ "ethers", "ethers.byname" },
6173407b34SGarrett Wollman };
6273407b34SGarrett Wollman 
63033af09dSMarcelo Araujo static int key;
6473407b34SGarrett Wollman 
65c52bab11SMarcelo Araujo static void
usage(void)66082d8262SMark Murray usage(void)
6773407b34SGarrett Wollman {
68c52bab11SMarcelo Araujo 	fprintf(stderr, "%s\n%s\n",
69c52bab11SMarcelo Araujo 	    "usage: ypcat [-kt] [-d domainname] mapname",
70c52bab11SMarcelo Araujo 	    "       ypcat -x");
7173407b34SGarrett Wollman 	exit(1);
7273407b34SGarrett Wollman }
7373407b34SGarrett Wollman 
74c52bab11SMarcelo Araujo static int
printit(u_long instatus,char * inkey,int inkeylen,char * inval,int invallen,void * indata)75c1f70e37SMarcelo Araujo printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
76c1f70e37SMarcelo Araujo     void *indata)
7773407b34SGarrett Wollman {
7873407b34SGarrett Wollman 	if (instatus != YP_TRUE)
79ed4d1c46SDag-Erling Smørgrav 		return (instatus);
8073407b34SGarrett Wollman 	if (key)
8173407b34SGarrett Wollman 		printf("%*.*s ", inkeylen, inkeylen, inkey);
8273407b34SGarrett Wollman 	printf("%*.*s\n", invallen, invallen, inval);
83ed4d1c46SDag-Erling Smørgrav 	return (0);
8473407b34SGarrett Wollman }
8573407b34SGarrett Wollman 
8673407b34SGarrett Wollman int
main(int argc,char * argv[])87082d8262SMark Murray main(int argc, char *argv[])
8873407b34SGarrett Wollman {
89c1f70e37SMarcelo Araujo 	char *domain = NULL, *inmap;
9073407b34SGarrett Wollman 	struct ypall_callback ypcb;
91c52bab11SMarcelo Araujo 	int c, notrans, r;
92082d8262SMark Murray 	u_int i;
9373407b34SGarrett Wollman 
9473407b34SGarrett Wollman 	notrans = key = 0;
9573407b34SGarrett Wollman 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
9673407b34SGarrett Wollman 		switch (c) {
9773407b34SGarrett Wollman 		case 'x':
9807c1d44fSMarcelo Araujo 			for (i = 0; i < nitems(ypaliases); i++)
9973407b34SGarrett Wollman 				printf("Use \"%s\" for \"%s\"\n",
100c1f70e37SMarcelo Araujo 				    ypaliases[i].alias, ypaliases[i].name);
10173407b34SGarrett Wollman 			exit(0);
10273407b34SGarrett Wollman 		case 'd':
103c1f70e37SMarcelo Araujo 			domain = optarg;
10473407b34SGarrett Wollman 			break;
10573407b34SGarrett Wollman 		case 't':
106c1f70e37SMarcelo Araujo 			notrans = 1;
10773407b34SGarrett Wollman 			break;
10873407b34SGarrett Wollman 		case 'k':
109c1f70e37SMarcelo Araujo 			key = 1;
11073407b34SGarrett Wollman 			break;
11173407b34SGarrett Wollman 		default:
11273407b34SGarrett Wollman 			usage();
11373407b34SGarrett Wollman 		}
11473407b34SGarrett Wollman 
11573407b34SGarrett Wollman 	if (optind + 1 != argc)
11673407b34SGarrett Wollman 		usage();
11773407b34SGarrett Wollman 
118c52bab11SMarcelo Araujo 	if (domain == NULL)
119c1f70e37SMarcelo Araujo 		yp_get_default_domain(&domain);
120c1f84cc1SPhilippe Charnier 
12173407b34SGarrett Wollman 	inmap = argv[optind];
12207c1d44fSMarcelo Araujo 	if (notrans == 0) {
12307c1d44fSMarcelo Araujo 		for (i = 0; i < nitems(ypaliases); i++)
12473407b34SGarrett Wollman 			if (strcmp(inmap, ypaliases[i].alias) == 0)
12573407b34SGarrett Wollman 				inmap = ypaliases[i].name;
126c1f70e37SMarcelo Araujo 	}
12773407b34SGarrett Wollman 	ypcb.foreach = printit;
12873407b34SGarrett Wollman 	ypcb.data = NULL;
12973407b34SGarrett Wollman 
130c1f70e37SMarcelo Araujo 	r = yp_all(domain, inmap, &ypcb);
13173407b34SGarrett Wollman 	switch (r) {
13273407b34SGarrett Wollman 	case 0:
13373407b34SGarrett Wollman 		break;
13473407b34SGarrett Wollman 	case YPERR_YPBIND:
135c52bab11SMarcelo Araujo 		errx(1, "not running ypbind");
13673407b34SGarrett Wollman 	default:
137c52bab11SMarcelo Araujo 		errx(1, "no such map %s. Reason: %s",
138c1f70e37SMarcelo Araujo 		    inmap, yperr_string(r));
13973407b34SGarrett Wollman 	}
14073407b34SGarrett Wollman 	exit(0);
14173407b34SGarrett Wollman }
142