ypmatch.c revision 1.10 1 1.10 thorpej /* $NetBSD: ypmatch.c,v 1.10 1997/07/18 07:05:36 thorpej Exp $ */
2 1.5 cgd
3 1.2 deraadt /*
4 1.4 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.4 deraadt * 3. All advertising materials mentioning features or use of this software
16 1.4 deraadt * must display the following acknowledgement:
17 1.4 deraadt * This product includes software developed by Theo de Raadt.
18 1.4 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.10 thorpej #include <sys/cdefs.h>
36 1.10 thorpej #ifndef lint
37 1.10 thorpej __RCSID("$NetBSD: ypmatch.c,v 1.10 1997/07/18 07:05:36 thorpej 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.10 thorpej #include <ctype.h>
44 1.10 thorpej #include <err.h>
45 1.1 deraadt #include <stdio.h>
46 1.5 cgd #include <string.h>
47 1.10 thorpej #include <unistd.h>
48 1.1 deraadt
49 1.1 deraadt #include <rpc/rpc.h>
50 1.1 deraadt #include <rpc/xdr.h>
51 1.1 deraadt #include <rpcsvc/yp_prot.h>
52 1.1 deraadt #include <rpcsvc/ypclnt.h>
53 1.1 deraadt
54 1.10 thorpej const struct ypalias {
55 1.1 deraadt char *alias, *name;
56 1.1 deraadt } ypaliases[] = {
57 1.1 deraadt { "passwd", "passwd.byname" },
58 1.1 deraadt { "group", "group.byname" },
59 1.1 deraadt { "networks", "networks.byaddr" },
60 1.3 deraadt { "hosts", "hosts.byname" },
61 1.1 deraadt { "protocols", "protocols.bynumber" },
62 1.1 deraadt { "services", "services.byname" },
63 1.1 deraadt { "aliases", "mail.aliases" },
64 1.1 deraadt { "ethers", "ethers.byname" },
65 1.1 deraadt };
66 1.1 deraadt
67 1.10 thorpej int main __P((int, char *[]));
68 1.10 thorpej void usage __P((void));
69 1.10 thorpej
70 1.10 thorpej extern char *__progname;
71 1.1 deraadt
72 1.1 deraadt int
73 1.1 deraadt main(argc, argv)
74 1.10 thorpej int argc;
75 1.10 thorpej char *argv[];
76 1.1 deraadt {
77 1.1 deraadt char *domainname;
78 1.1 deraadt char *inkey, *inmap, *outbuf;
79 1.1 deraadt extern char *optarg;
80 1.1 deraadt extern int optind;
81 1.1 deraadt int outbuflen, key, notrans;
82 1.1 deraadt int c, r, i;
83 1.8 jtc int rval;
84 1.1 deraadt
85 1.9 jtc domainname = NULL;
86 1.1 deraadt notrans = key = 0;
87 1.10 thorpej while ((c = getopt(argc, argv, "xd:kt")) != -1) {
88 1.10 thorpej switch (c) {
89 1.1 deraadt case 'x':
90 1.10 thorpej for(i = 0;
91 1.10 thorpej i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
92 1.1 deraadt printf("Use \"%s\" for \"%s\"\n",
93 1.1 deraadt ypaliases[i].alias,
94 1.1 deraadt ypaliases[i].name);
95 1.1 deraadt exit(0);
96 1.10 thorpej
97 1.1 deraadt case 'd':
98 1.1 deraadt domainname = optarg;
99 1.1 deraadt break;
100 1.10 thorpej
101 1.1 deraadt case 't':
102 1.1 deraadt notrans++;
103 1.1 deraadt break;
104 1.10 thorpej
105 1.1 deraadt case 'k':
106 1.1 deraadt key++;
107 1.1 deraadt break;
108 1.10 thorpej
109 1.1 deraadt default:
110 1.1 deraadt usage();
111 1.1 deraadt }
112 1.10 thorpej }
113 1.10 thorpej
114 1.10 thorpej argc -= optind;
115 1.10 thorpej argv += optind;
116 1.1 deraadt
117 1.10 thorpej if (argc < 2)
118 1.1 deraadt usage();
119 1.9 jtc
120 1.10 thorpej if (domainname == NULL)
121 1.9 jtc yp_get_default_domain(&domainname);
122 1.1 deraadt
123 1.10 thorpej inmap = argv[argc - 1];
124 1.10 thorpej if (notrans == 0) {
125 1.10 thorpej for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
126 1.10 thorpej if (strcmp(inmap, ypaliases[i].alias) == 0)
127 1.6 jtc inmap = ypaliases[i].name;
128 1.6 jtc }
129 1.8 jtc
130 1.8 jtc rval = 0;
131 1.10 thorpej for(i = 0; i < (argc - 1); i++) {
132 1.10 thorpej inkey = argv[i];
133 1.1 deraadt
134 1.10 thorpej r = yp_match(domainname, inmap, inkey, strlen(inkey),
135 1.10 thorpej &outbuf, &outbuflen);
136 1.10 thorpej switch (r) {
137 1.1 deraadt case 0:
138 1.10 thorpej if (key)
139 1.7 jtc printf("%s: ", inkey);
140 1.1 deraadt printf("%*.*s\n", outbuflen, outbuflen, outbuf);
141 1.1 deraadt break;
142 1.10 thorpej
143 1.1 deraadt case YPERR_YPBIND:
144 1.10 thorpej errx(1, "not running ypbind");
145 1.10 thorpej
146 1.1 deraadt default:
147 1.10 thorpej warnx("can't match key %s in map %s. Reason: %s",
148 1.10 thorpej inkey, inmap, yperr_string(r));
149 1.8 jtc rval = 1;
150 1.1 deraadt break;
151 1.1 deraadt }
152 1.1 deraadt }
153 1.8 jtc
154 1.8 jtc exit(rval);
155 1.10 thorpej }
156 1.10 thorpej
157 1.10 thorpej void
158 1.10 thorpej usage()
159 1.10 thorpej {
160 1.10 thorpej
161 1.10 thorpej fprintf(stderr, "usage: %s [-d domain] [-t] [-k] key [key ...] "
162 1.10 thorpej "mapname\n", __progname);
163 1.10 thorpej fprintf(stderr, " %s -x\n", __progname);
164 1.10 thorpej exit(1);
165 1.1 deraadt }
166