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