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