1*edf7bb77Smatt /* $NetBSD: ypmatch.c,v 1.21 2012/03/02 18:57:27 matt Exp $ */
22e1521a2Scgd
377c037edSderaadt /*
43bf2b62fSderaadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
577c037edSderaadt * All rights reserved.
677c037edSderaadt *
777c037edSderaadt * Redistribution and use in source and binary forms, with or without
877c037edSderaadt * modification, are permitted provided that the following conditions
977c037edSderaadt * are met:
1077c037edSderaadt * 1. Redistributions of source code must retain the above copyright
1177c037edSderaadt * notice, this list of conditions and the following disclaimer.
1277c037edSderaadt * 2. Redistributions in binary form must reproduce the above copyright
1377c037edSderaadt * notice, this list of conditions and the following disclaimer in the
1477c037edSderaadt * documentation and/or other materials provided with the distribution.
1577c037edSderaadt *
1677c037edSderaadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1777c037edSderaadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1877c037edSderaadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1977c037edSderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2077c037edSderaadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2177c037edSderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2277c037edSderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2377c037edSderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2477c037edSderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2577c037edSderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2677c037edSderaadt * SUCH DAMAGE.
2777c037edSderaadt */
2877c037edSderaadt
2900c02e3eSthorpej #include <sys/cdefs.h>
3000c02e3eSthorpej #ifndef lint
31*edf7bb77Smatt __RCSID("$NetBSD: ypmatch.c,v 1.21 2012/03/02 18:57:27 matt Exp $");
3277c037edSderaadt #endif
3377c037edSderaadt
34e39dac2fSderaadt #include <sys/param.h>
35e39dac2fSderaadt #include <sys/types.h>
36e39dac2fSderaadt #include <sys/socket.h>
3700c02e3eSthorpej #include <ctype.h>
3800c02e3eSthorpej #include <err.h>
39e39dac2fSderaadt #include <stdio.h>
40fcd0fb11Smatt #include <stdlib.h>
412e1521a2Scgd #include <string.h>
4200c02e3eSthorpej #include <unistd.h>
43e39dac2fSderaadt
44e39dac2fSderaadt #include <rpc/rpc.h>
45e39dac2fSderaadt #include <rpc/xdr.h>
46e39dac2fSderaadt #include <rpcsvc/yp_prot.h>
47e39dac2fSderaadt #include <rpcsvc/ypclnt.h>
48e39dac2fSderaadt
49e5962c58Schristos #include "ypalias_init.h"
50e39dac2fSderaadt
51e5962c58Schristos static void usage(void) __attribute__((__noreturn__));
5200c02e3eSthorpej
53e39dac2fSderaadt int
main(int argc,char * argv[])54e5962c58Schristos main(int argc, char *argv[])
55e39dac2fSderaadt {
562b01a8adSchristos char *domainname, *b_retry_cnt;
5738a78fe6Slukem char *inkey, *outbuf;
5838a78fe6Slukem const char *inmap;
59efc2b2a1Schristos int outbuflen, key, null, notrans;
6038a78fe6Slukem int c, r, len;
6138a78fe6Slukem size_t i;
6216bd82bcSjtc int rval;
63e5962c58Schristos const struct ypalias *ypaliases;
64e39dac2fSderaadt
65e5962c58Schristos setprogname(*argv);
662b01a8adSchristos domainname = b_retry_cnt = NULL;
67efc2b2a1Schristos notrans = key = null = 0;
68e5962c58Schristos ypaliases = ypalias_init();
692b01a8adSchristos while ((c = getopt(argc, argv, "bd:ktxz")) != -1) {
70e39dac2fSderaadt switch (c) {
71e39dac2fSderaadt case 'x':
72e5962c58Schristos for(i = 0; ypaliases[i].alias; i++)
73e39dac2fSderaadt printf("Use \"%s\" for \"%s\"\n",
74e39dac2fSderaadt ypaliases[i].alias,
75e39dac2fSderaadt ypaliases[i].name);
76e5962c58Schristos return 0;
7700c02e3eSthorpej
782b01a8adSchristos case 'b':
792b01a8adSchristos b_retry_cnt = optarg;
802b01a8adSchristos break;
812b01a8adSchristos
82e39dac2fSderaadt case 'd':
83e39dac2fSderaadt domainname = optarg;
84e39dac2fSderaadt break;
8500c02e3eSthorpej
86e39dac2fSderaadt case 't':
87e39dac2fSderaadt notrans++;
88e39dac2fSderaadt break;
8900c02e3eSthorpej
90e39dac2fSderaadt case 'k':
91e39dac2fSderaadt key++;
92e39dac2fSderaadt break;
9300c02e3eSthorpej
94efc2b2a1Schristos case 'z':
95efc2b2a1Schristos null++;
96efc2b2a1Schristos break;
97efc2b2a1Schristos
98e39dac2fSderaadt default:
99e39dac2fSderaadt usage();
100e39dac2fSderaadt }
1014a22fd1cSjtc }
1024a22fd1cSjtc
10300c02e3eSthorpej argc -= optind;
10400c02e3eSthorpej argv += optind;
10500c02e3eSthorpej
10600c02e3eSthorpej if (argc < 2)
10700c02e3eSthorpej usage();
10800c02e3eSthorpej
1092b01a8adSchristos if (b_retry_cnt != NULL) {
1102b01a8adSchristos char *s;
1112b01a8adSchristos unsigned long l;
1122b01a8adSchristos
1132b01a8adSchristos l = strtoul(b_retry_cnt, &s, 10);
1142b01a8adSchristos if (*s != '\0' || l > 0xffff) usage();
1152b01a8adSchristos yp_setbindtries((int)l);
1162b01a8adSchristos }
1172b01a8adSchristos
11800c02e3eSthorpej if (domainname == NULL)
11900c02e3eSthorpej yp_get_default_domain(&domainname);
12000c02e3eSthorpej
121e39dac2fSderaadt inmap = argv[argc - 1];
12200c02e3eSthorpej if (notrans == 0) {
123e5962c58Schristos for (i = 0; ypaliases[i].alias; i++)
124e39dac2fSderaadt if (strcmp(inmap, ypaliases[i].alias) == 0)
125e39dac2fSderaadt inmap = ypaliases[i].name;
1260791e398Sjtc }
12716bd82bcSjtc
12816bd82bcSjtc rval = 0;
12938a78fe6Slukem for(c = 0; c < (argc - 1); c++) {
13038a78fe6Slukem inkey = argv[c];
131e39dac2fSderaadt
132efc2b2a1Schristos len = strlen(inkey);
133efc2b2a1Schristos if (null)
134efc2b2a1Schristos len++;
135efc2b2a1Schristos r = yp_match(domainname, inmap, inkey, len,
13600c02e3eSthorpej &outbuf, &outbuflen);
137e39dac2fSderaadt switch (r) {
138e39dac2fSderaadt case 0:
139e39dac2fSderaadt if (key)
140f5b6798fSjtc printf("%s: ", inkey);
141efc2b2a1Schristos fwrite(outbuf, outbuflen, 1, stdout);
142efc2b2a1Schristos putc('\n', stdout);
143e39dac2fSderaadt break;
14400c02e3eSthorpej
145e39dac2fSderaadt case YPERR_YPBIND:
14600c02e3eSthorpej errx(1, "not running ypbind");
14700c02e3eSthorpej
148e39dac2fSderaadt default:
14900c02e3eSthorpej warnx("can't match key %s in map %s. Reason: %s",
150e39dac2fSderaadt inkey, inmap, yperr_string(r));
15116bd82bcSjtc rval = 1;
152e39dac2fSderaadt break;
153e39dac2fSderaadt }
154e39dac2fSderaadt }
15516bd82bcSjtc
156e5962c58Schristos return rval;
157e39dac2fSderaadt }
15800c02e3eSthorpej
159e5962c58Schristos static void
usage(void)160e5962c58Schristos usage(void)
16100c02e3eSthorpej {
16200c02e3eSthorpej
1632b01a8adSchristos (void)fprintf(stderr, "Usage: %s [-ktz] [-b <num-retry>] "
164*edf7bb77Smatt "[-d <domainname>] <key> ... <mapname>\n", getprogname());
165e5962c58Schristos (void)fprintf(stderr, " %s -x\n", getprogname());
16600c02e3eSthorpej exit(1);
16700c02e3eSthorpej }
168