xref: /freebsd/usr.bin/ypcat/ypcat.c (revision a0ee8cc6)
1 /*	$OpenBSD: ypcat.c,v 1.16 2015/02/08 23:40:35 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <ctype.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <rpc/rpc.h>
45 #include <rpc/xdr.h>
46 #include <rpcsvc/yp.h>
47 #include <rpcsvc/ypclnt.h>
48 
49 static const struct ypalias {
50 	char *alias, *name;
51 } ypaliases[] = {
52 	{ "passwd", "passwd.byname" },
53 	{ "master.passwd", "master.passwd.byname" },
54 	{ "shadow", "shadow.byname" },
55 	{ "group", "group.byname" },
56 	{ "networks", "networks.byaddr" },
57 	{ "hosts", "hosts.byaddr" },
58 	{ "protocols", "protocols.bynumber" },
59 	{ "services", "services.byname" },
60 	{ "aliases", "mail.aliases" },
61 	{ "ethers", "ethers.byname" },
62 };
63 
64 static int key;
65 
66 static void
67 usage(void)
68 {
69 	fprintf(stderr, "%s\n%s\n",
70 	    "usage: ypcat [-kt] [-d domainname] mapname",
71 	    "       ypcat -x");
72 	exit(1);
73 }
74 
75 static int
76 printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
77     void *indata)
78 {
79 	if (instatus != YP_TRUE)
80 		return (instatus);
81 	if (key)
82 		printf("%*.*s ", inkeylen, inkeylen, inkey);
83 	printf("%*.*s\n", invallen, invallen, inval);
84 	return (0);
85 }
86 
87 int
88 main(int argc, char *argv[])
89 {
90 	char *domain = NULL, *inmap;
91 	struct ypall_callback ypcb;
92 	int c, notrans, r;
93 	u_int i;
94 
95 	notrans = key = 0;
96 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
97 		switch (c) {
98 		case 'x':
99 			for (i = 0; i < nitems(ypaliases); i++)
100 				printf("Use \"%s\" for \"%s\"\n",
101 				    ypaliases[i].alias, ypaliases[i].name);
102 			exit(0);
103 		case 'd':
104 			domain = optarg;
105 			break;
106 		case 't':
107 			notrans = 1;
108 			break;
109 		case 'k':
110 			key = 1;
111 			break;
112 		default:
113 			usage();
114 		}
115 
116 	if (optind + 1 != argc)
117 		usage();
118 
119 	if (domain == NULL)
120 		yp_get_default_domain(&domain);
121 
122 	inmap = argv[optind];
123 	if (notrans == 0) {
124 		for (i = 0; i < nitems(ypaliases); i++)
125 			if (strcmp(inmap, ypaliases[i].alias) == 0)
126 				inmap = ypaliases[i].name;
127 	}
128 	ypcb.foreach = printit;
129 	ypcb.data = NULL;
130 
131 	r = yp_all(domain, inmap, &ypcb);
132 	switch (r) {
133 	case 0:
134 		break;
135 	case YPERR_YPBIND:
136 		errx(1, "not running ypbind");
137 	default:
138 		errx(1, "no such map %s. Reason: %s",
139 		    inmap, yperr_string(r));
140 	}
141 	exit(0);
142 }
143