1 /* $OpenBSD: ypcat.c,v 1.16 2015/02/08 23:40:35 deraadt Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5 *
6 * Copyright (c) 1992, 1993, 1996 Theo de Raadt <[email protected]>
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 #include <sys/cdefs.h>
32
33 __FBSDID("$FreeBSD$");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <rpc/rpc.h>
47 #include <rpc/xdr.h>
48 #include <rpcsvc/yp.h>
49 #include <rpcsvc/ypclnt.h>
50
51 static const struct ypalias {
52 char *alias, *name;
53 } ypaliases[] = {
54 { "passwd", "passwd.byname" },
55 { "master.passwd", "master.passwd.byname" },
56 { "shadow", "shadow.byname" },
57 { "group", "group.byname" },
58 { "networks", "networks.byaddr" },
59 { "hosts", "hosts.byaddr" },
60 { "protocols", "protocols.bynumber" },
61 { "services", "services.byname" },
62 { "aliases", "mail.aliases" },
63 { "ethers", "ethers.byname" },
64 };
65
66 static int key;
67
68 static void
usage(void)69 usage(void)
70 {
71 fprintf(stderr, "%s\n%s\n",
72 "usage: ypcat [-kt] [-d domainname] mapname",
73 " ypcat -x");
74 exit(1);
75 }
76
77 static int
printit(u_long instatus,char * inkey,int inkeylen,char * inval,int invallen,void * indata)78 printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
79 void *indata)
80 {
81 if (instatus != YP_TRUE)
82 return (instatus);
83 if (key)
84 printf("%*.*s ", inkeylen, inkeylen, inkey);
85 printf("%*.*s\n", invallen, invallen, inval);
86 return (0);
87 }
88
89 int
main(int argc,char * argv[])90 main(int argc, char *argv[])
91 {
92 char *domain = NULL, *inmap;
93 struct ypall_callback ypcb;
94 int c, notrans, r;
95 u_int i;
96
97 notrans = key = 0;
98 while ((c = getopt(argc, argv, "xd:kt")) != -1)
99 switch (c) {
100 case 'x':
101 for (i = 0; i < nitems(ypaliases); i++)
102 printf("Use \"%s\" for \"%s\"\n",
103 ypaliases[i].alias, ypaliases[i].name);
104 exit(0);
105 case 'd':
106 domain = optarg;
107 break;
108 case 't':
109 notrans = 1;
110 break;
111 case 'k':
112 key = 1;
113 break;
114 default:
115 usage();
116 }
117
118 if (optind + 1 != argc)
119 usage();
120
121 if (domain == NULL)
122 yp_get_default_domain(&domain);
123
124 inmap = argv[optind];
125 if (notrans == 0) {
126 for (i = 0; i < nitems(ypaliases); i++)
127 if (strcmp(inmap, ypaliases[i].alias) == 0)
128 inmap = ypaliases[i].name;
129 }
130 ypcb.foreach = printit;
131 ypcb.data = NULL;
132
133 r = yp_all(domain, inmap, &ypcb);
134 switch (r) {
135 case 0:
136 break;
137 case YPERR_YPBIND:
138 errx(1, "not running ypbind");
139 default:
140 errx(1, "no such map %s. Reason: %s",
141 inmap, yperr_string(r));
142 }
143 exit(0);
144 }
145