11930Swollman/*
21930Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
31930Swollman * All rights reserved.
41930Swollman *
51930Swollman * Redistribution and use in source and binary forms, with or without
61930Swollman * modification, are permitted provided that the following conditions
71930Swollman * are met:
81930Swollman * 1. Redistributions of source code must retain the above copyright
91930Swollman *    notice, this list of conditions and the following disclaimer.
101930Swollman * 2. Redistributions in binary form must reproduce the above copyright
111930Swollman *    notice, this list of conditions and the following disclaimer in the
121930Swollman *    documentation and/or other materials provided with the distribution.
131930Swollman * 3. The name of the author may not be used to endorse or promote
141930Swollman *    products derived from this software without specific prior written
151930Swollman *    permission.
161930Swollman *
171930Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
181930Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
191930Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201930Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
211930Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221930Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231930Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241930Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251930Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261930Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271930Swollman * SUCH DAMAGE.
281930Swollman */
291930Swollman
3087672Smarkm#include <sys/cdefs.h>
311930Swollman
3287672Smarkm__FBSDID("$FreeBSD$");
3387672Smarkm
341930Swollman#include <sys/param.h>
351930Swollman#include <sys/types.h>
361930Swollman#include <sys/socket.h>
3787672Smarkm
3887672Smarkm#include <rpc/rpc.h>
3987672Smarkm#include <rpc/xdr.h>
4087672Smarkm#include <rpcsvc/yp_prot.h>
4187672Smarkm#include <rpcsvc/ypclnt.h>
4287672Smarkm
43200462Sdelphij#include <ctype.h>
4428893Scharnier#include <err.h>
451930Swollman#include <stdio.h>
4678718Sdd#include <stdlib.h>
4778718Sdd#include <string.h>
4828893Scharnier#include <unistd.h>
491930Swollman
501930Swollmanstruct ypalias {
51121550Speter	char *alias, *name;
521930Swollman} ypaliases[] = {
531930Swollman	{ "passwd", "passwd.byname" },
5427345Speter	{ "master.passwd", "master.passwd.byname" },
55194968Sbrian	{ "shadow", "shadow.byname" },
561930Swollman	{ "group", "group.byname" },
571930Swollman	{ "networks", "networks.byaddr" },
581930Swollman	{ "hosts", "hosts.byaddr" },
591930Swollman	{ "protocols", "protocols.bynumber" },
601930Swollman	{ "services", "services.byname" },
611930Swollman	{ "aliases", "mail.aliases" },
621930Swollman	{ "ethers", "ethers.byname" },
631930Swollman};
641930Swollman
651930Swollmanint key;
661930Swollman
6728893Scharnierstatic void
6887672Smarkmusage(void)
691930Swollman{
7028893Scharnier	fprintf(stderr, "%s\n%s\n",
71146466Sru		"usage: ypcat [-kt] [-d domainname] mapname",
7228893Scharnier		"       ypcat -x");
731930Swollman	exit(1);
741930Swollman}
751930Swollman
7687672Smarkmstatic int
7787672Smarkmprintit(unsigned long instatus, char *inkey, int inkeylen, char *inval, int invallen, void *dummy __unused)
781930Swollman{
7990297Sdes	if (instatus != YP_TRUE)
8090297Sdes		return (instatus);
8190297Sdes	if (key)
821930Swollman		printf("%*.*s ", inkeylen, inkeylen, inkey);
831930Swollman	printf("%*.*s\n", invallen, invallen, inval);
8490297Sdes	return (0);
851930Swollman}
861930Swollman
871930Swollmanint
8887672Smarkmmain(int argc, char *argv[])
891930Swollman{
9028893Scharnier	char *domainname = NULL;
911930Swollman	struct ypall_callback ypcb;
921930Swollman	char *inmap;
931930Swollman	int notrans;
9487672Smarkm	int c, r;
9587672Smarkm	u_int i;
961930Swollman
971930Swollman	notrans = key = 0;
981930Swollman
9990297Sdes	while ((c = getopt(argc, argv, "xd:kt")) != -1)
10090297Sdes		switch (c) {
1011930Swollman		case 'x':
10290297Sdes			for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1031930Swollman				printf("Use \"%s\" for \"%s\"\n",
1041930Swollman					ypaliases[i].alias,
1051930Swollman					ypaliases[i].name);
1061930Swollman			exit(0);
1071930Swollman		case 'd':
1081930Swollman			domainname = optarg;
1091930Swollman			break;
1101930Swollman		case 't':
1111930Swollman			notrans++;
1121930Swollman			break;
1131930Swollman		case 'k':
1141930Swollman			key++;
1151930Swollman			break;
1161930Swollman		default:
1171930Swollman			usage();
1181930Swollman		}
1191930Swollman
12090297Sdes	if (optind + 1 != argc)
1211930Swollman		usage();
1221930Swollman
12328893Scharnier	if (!domainname)
12428893Scharnier		yp_get_default_domain(&domainname);
12528893Scharnier
1261930Swollman	inmap = argv[optind];
12790297Sdes	for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
12890297Sdes		if (strcmp(inmap, ypaliases[i].alias) == 0)
1291930Swollman			inmap = ypaliases[i].name;
1301930Swollman	ypcb.foreach = printit;
1311930Swollman	ypcb.data = NULL;
1321930Swollman
1331930Swollman	r = yp_all(domainname, inmap, &ypcb);
13490297Sdes	switch (r) {
1351930Swollman	case 0:
1361930Swollman		break;
1371930Swollman	case YPERR_YPBIND:
13828893Scharnier		errx(1, "not running ypbind");
1391930Swollman	default:
14028893Scharnier		errx(1, "no such map %s. reason: %s", inmap, yperr_string(r));
1411930Swollman	}
1421930Swollman	exit(0);
1431930Swollman}
144