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