ypmatch.c revision 1.21 1 1.21 matt /* $NetBSD: ypmatch.c,v 1.21 2012/03/02 18:57:27 matt 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.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.10 thorpej #include <sys/cdefs.h>
30 1.10 thorpej #ifndef lint
31 1.21 matt __RCSID("$NetBSD: ypmatch.c,v 1.21 2012/03/02 18:57:27 matt 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.10 thorpej #include <ctype.h>
38 1.10 thorpej #include <err.h>
39 1.1 deraadt #include <stdio.h>
40 1.12 matt #include <stdlib.h>
41 1.5 cgd #include <string.h>
42 1.10 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.18 christos #include "ypalias_init.h"
50 1.1 deraadt
51 1.18 christos static void usage(void) __attribute__((__noreturn__));
52 1.10 thorpej
53 1.1 deraadt int
54 1.18 christos main(int argc, char *argv[])
55 1.1 deraadt {
56 1.20 christos char *domainname, *b_retry_cnt;
57 1.17 lukem char *inkey, *outbuf;
58 1.17 lukem const char *inmap;
59 1.14 christos int outbuflen, key, null, notrans;
60 1.17 lukem int c, r, len;
61 1.17 lukem size_t i;
62 1.8 jtc int rval;
63 1.18 christos const struct ypalias *ypaliases;
64 1.1 deraadt
65 1.18 christos setprogname(*argv);
66 1.20 christos domainname = b_retry_cnt = NULL;
67 1.14 christos notrans = key = null = 0;
68 1.18 christos ypaliases = ypalias_init();
69 1.20 christos while ((c = getopt(argc, argv, "bd:ktxz")) != -1) {
70 1.10 thorpej switch (c) {
71 1.1 deraadt case 'x':
72 1.18 christos for(i = 0; ypaliases[i].alias; i++)
73 1.1 deraadt printf("Use \"%s\" for \"%s\"\n",
74 1.1 deraadt ypaliases[i].alias,
75 1.1 deraadt ypaliases[i].name);
76 1.18 christos return 0;
77 1.10 thorpej
78 1.20 christos case 'b':
79 1.20 christos b_retry_cnt = optarg;
80 1.20 christos break;
81 1.20 christos
82 1.1 deraadt case 'd':
83 1.1 deraadt domainname = optarg;
84 1.1 deraadt break;
85 1.10 thorpej
86 1.1 deraadt case 't':
87 1.1 deraadt notrans++;
88 1.1 deraadt break;
89 1.10 thorpej
90 1.1 deraadt case 'k':
91 1.1 deraadt key++;
92 1.1 deraadt break;
93 1.10 thorpej
94 1.14 christos case 'z':
95 1.14 christos null++;
96 1.14 christos break;
97 1.14 christos
98 1.1 deraadt default:
99 1.1 deraadt usage();
100 1.1 deraadt }
101 1.10 thorpej }
102 1.10 thorpej
103 1.10 thorpej argc -= optind;
104 1.10 thorpej argv += optind;
105 1.1 deraadt
106 1.10 thorpej if (argc < 2)
107 1.1 deraadt usage();
108 1.9 jtc
109 1.20 christos if (b_retry_cnt != NULL) {
110 1.20 christos char *s;
111 1.20 christos unsigned long l;
112 1.20 christos
113 1.20 christos l = strtoul(b_retry_cnt, &s, 10);
114 1.20 christos if (*s != '\0' || l > 0xffff) usage();
115 1.20 christos yp_setbindtries((int)l);
116 1.20 christos }
117 1.20 christos
118 1.10 thorpej if (domainname == NULL)
119 1.9 jtc yp_get_default_domain(&domainname);
120 1.1 deraadt
121 1.10 thorpej inmap = argv[argc - 1];
122 1.10 thorpej if (notrans == 0) {
123 1.18 christos for (i = 0; ypaliases[i].alias; i++)
124 1.10 thorpej if (strcmp(inmap, ypaliases[i].alias) == 0)
125 1.6 jtc inmap = ypaliases[i].name;
126 1.6 jtc }
127 1.8 jtc
128 1.8 jtc rval = 0;
129 1.17 lukem for(c = 0; c < (argc - 1); c++) {
130 1.17 lukem inkey = argv[c];
131 1.1 deraadt
132 1.14 christos len = strlen(inkey);
133 1.14 christos if (null)
134 1.14 christos len++;
135 1.14 christos r = yp_match(domainname, inmap, inkey, len,
136 1.10 thorpej &outbuf, &outbuflen);
137 1.10 thorpej switch (r) {
138 1.1 deraadt case 0:
139 1.10 thorpej if (key)
140 1.7 jtc printf("%s: ", inkey);
141 1.14 christos fwrite(outbuf, outbuflen, 1, stdout);
142 1.14 christos putc('\n', stdout);
143 1.1 deraadt break;
144 1.10 thorpej
145 1.1 deraadt case YPERR_YPBIND:
146 1.10 thorpej errx(1, "not running ypbind");
147 1.10 thorpej
148 1.1 deraadt default:
149 1.10 thorpej warnx("can't match key %s in map %s. Reason: %s",
150 1.10 thorpej inkey, inmap, yperr_string(r));
151 1.8 jtc rval = 1;
152 1.1 deraadt break;
153 1.1 deraadt }
154 1.1 deraadt }
155 1.8 jtc
156 1.18 christos return rval;
157 1.10 thorpej }
158 1.10 thorpej
159 1.18 christos static void
160 1.18 christos usage(void)
161 1.10 thorpej {
162 1.10 thorpej
163 1.20 christos (void)fprintf(stderr, "Usage: %s [-ktz] [-b <num-retry>] "
164 1.21 matt "[-d <domainname>] <key> ... <mapname>\n", getprogname());
165 1.18 christos (void)fprintf(stderr, " %s -x\n", getprogname());
166 1.10 thorpej exit(1);
167 1.1 deraadt }
168