ypmatch.c revision 1.16.42.1 1 /* $NetBSD: ypmatch.c,v 1.16.42.1 2009/05/13 19:20:14 jym 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: ypmatch.c,v 1.16.42.1 2009/05/13 19:20:14 jym 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.byname" },
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 void usage __P((void));
64
65 int
66 main(argc, argv)
67 int argc;
68 char *argv[];
69 {
70 char *domainname;
71 char *inkey, *outbuf;
72 const char *inmap;
73 int outbuflen, key, null, notrans;
74 int c, r, len;
75 size_t i;
76 int rval;
77
78 domainname = NULL;
79 notrans = key = null = 0;
80 while ((c = getopt(argc, argv, "xd:ktz")) != -1) {
81 switch (c) {
82 case 'x':
83 for(i = 0;
84 i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
85 printf("Use \"%s\" for \"%s\"\n",
86 ypaliases[i].alias,
87 ypaliases[i].name);
88 exit(0);
89
90 case 'd':
91 domainname = optarg;
92 break;
93
94 case 't':
95 notrans++;
96 break;
97
98 case 'k':
99 key++;
100 break;
101
102 case 'z':
103 null++;
104 break;
105
106 default:
107 usage();
108 }
109 }
110
111 argc -= optind;
112 argv += optind;
113
114 if (argc < 2)
115 usage();
116
117 if (domainname == NULL)
118 yp_get_default_domain(&domainname);
119
120 inmap = argv[argc - 1];
121 if (notrans == 0) {
122 for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
123 if (strcmp(inmap, ypaliases[i].alias) == 0)
124 inmap = ypaliases[i].name;
125 }
126
127 rval = 0;
128 for(c = 0; c < (argc - 1); c++) {
129 inkey = argv[c];
130
131 len = strlen(inkey);
132 if (null)
133 len++;
134 r = yp_match(domainname, inmap, inkey, len,
135 &outbuf, &outbuflen);
136 switch (r) {
137 case 0:
138 if (key)
139 printf("%s: ", inkey);
140 fwrite(outbuf, outbuflen, 1, stdout);
141 putc('\n', stdout);
142 break;
143
144 case YPERR_YPBIND:
145 errx(1, "not running ypbind");
146
147 default:
148 warnx("can't match key %s in map %s. Reason: %s",
149 inkey, inmap, yperr_string(r));
150 rval = 1;
151 break;
152 }
153 }
154
155 exit(rval);
156 }
157
158 void
159 usage()
160 {
161
162 fprintf(stderr, "usage: %s [-d domain] [-tkz] key [key ...] "
163 "mapname\n", getprogname());
164 fprintf(stderr, " %s -x\n", getprogname());
165 exit(1);
166 }
167