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