ypcat.c revision 1.9 1 /* $NetBSD: ypcat.c,v 1.9 2000/07/03 02:51:44 matt Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Theo de Raadt.
18 * 4. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __RCSID("$NetBSD: ypcat.c,v 1.9 2000/07/03 02:51:44 matt Exp $");
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include <rpc/rpc.h>
51 #include <rpc/xdr.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54
55 const struct ypalias {
56 char *alias, *name;
57 } ypaliases[] = {
58 { "passwd", "passwd.byname" },
59 { "group", "group.byname" },
60 { "networks", "networks.byaddr" },
61 { "hosts", "hosts.byaddr" },
62 { "protocols", "protocols.bynumber" },
63 { "services", "services.byname" },
64 { "aliases", "mail.aliases" },
65 { "ethers", "ethers.byname" },
66 };
67
68 int main __P((int, char *[]));
69 int printit __P((int, char *, int, char *, int, char *));
70 void usage __P((void));
71
72 int key;
73
74 extern char *__progname;
75
76 int
77 main(argc, argv)
78 int argc;
79 char *argv[];
80 {
81 char *domainname;
82 struct ypall_callback ypcb;
83 char *inmap;
84 int notrans;
85 int c, r, i;
86
87 domainname = NULL;
88 notrans = key = 0;
89 while((c = getopt(argc, argv, "xd:kt")) != -1) {
90 switch (c) {
91 case 'x':
92 for (i = 0;
93 i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
94 printf("Use \"%s\" for \"%s\"\n",
95 ypaliases[i].alias,
96 ypaliases[i].name);
97 exit(0);
98
99 case 'd':
100 domainname = optarg;
101 break;
102
103 case 't':
104 notrans++;
105 break;
106
107 case 'k':
108 key++;
109 break;
110
111 default:
112 usage();
113 }
114 }
115
116 argc -= optind;
117 argv += optind;
118
119 if (argc != 1)
120 usage();
121
122 if (domainname == NULL)
123 yp_get_default_domain(&domainname);
124
125 inmap = argv[0];
126 if (notrans == 0) {
127 for (i = 0; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
128 if (strcmp(inmap, ypaliases[i].alias) == 0)
129 inmap = ypaliases[i].name;
130 }
131
132 ypcb.foreach = printit;
133 ypcb.data = NULL;
134
135 r = yp_all(domainname, inmap, &ypcb);
136 switch (r) {
137 case 0:
138 break;
139
140 case YPERR_YPBIND:
141 errx(1, "not running ypbind");
142
143 default:
144 errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r));
145 }
146 exit(0);
147 }
148
149 int
150 printit(instatus, inkey, inkeylen, inval, invallen, indata)
151 int instatus;
152 char *inkey;
153 int inkeylen;
154 char *inval;
155 int invallen;
156 char *indata;
157 {
158
159 if (instatus != YP_TRUE)
160 return instatus;
161 if (key)
162 printf("%*.*s", inkeylen, inkeylen, inkey);
163 if (invallen)
164 printf("%s%*.*s", (key ? " " : ""), invallen, invallen, inval);
165 printf("\n");
166 return 0;
167 }
168
169 void
170 usage()
171 {
172
173 fprintf(stderr, "usage: %s [-k] [-d domainname] [-t] mapname\n",
174 __progname);
175 fprintf(stderr, " %s -x\n", __progname);
176 exit(1);
177 }
178