Annotation of src/usr.bin/ypmatch/ypmatch.c, Revision 1.12
1.12 ! matt 1: /* $NetBSD: ypmatch.c,v 1.11 2000/04/14 06:11:11 simonb Exp $ */
1.5 cgd 2:
1.2 deraadt 3: /*
1.4 deraadt 4: * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
1.2 deraadt 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.
1.4 deraadt 15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by Theo de Raadt.
18: * 4. The name of the author may not be used to endorse or promote
1.2 deraadt 19: * products derived from this software without specific prior written
20: * permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
1.10 thorpej 35: #include <sys/cdefs.h>
36: #ifndef lint
1.12 ! matt 37: __RCSID("$NetBSD: ypmatch.c,v 1.11 2000/04/14 06:11:11 simonb Exp $");
1.2 deraadt 38: #endif
39:
1.1 deraadt 40: #include <sys/param.h>
41: #include <sys/types.h>
42: #include <sys/socket.h>
1.10 thorpej 43: #include <ctype.h>
44: #include <err.h>
1.1 deraadt 45: #include <stdio.h>
1.12 ! matt 46: #include <stdlib.h>
1.5 cgd 47: #include <string.h>
1.10 thorpej 48: #include <unistd.h>
1.1 deraadt 49:
50: #include <rpc/rpc.h>
51: #include <rpc/xdr.h>
52: #include <rpcsvc/yp_prot.h>
53: #include <rpcsvc/ypclnt.h>
54:
1.10 thorpej 55: const struct ypalias {
1.1 deraadt 56: char *alias, *name;
57: } ypaliases[] = {
58: { "passwd", "passwd.byname" },
59: { "group", "group.byname" },
60: { "networks", "networks.byaddr" },
1.3 deraadt 61: { "hosts", "hosts.byname" },
1.1 deraadt 62: { "protocols", "protocols.bynumber" },
63: { "services", "services.byname" },
64: { "aliases", "mail.aliases" },
65: { "ethers", "ethers.byname" },
66: };
67:
1.10 thorpej 68: int main __P((int, char *[]));
69: void usage __P((void));
70:
71: extern char *__progname;
1.1 deraadt 72:
73: int
74: main(argc, argv)
1.10 thorpej 75: int argc;
76: char *argv[];
1.1 deraadt 77: {
78: char *domainname;
79: char *inkey, *inmap, *outbuf;
80: int outbuflen, key, notrans;
81: int c, r, i;
1.8 jtc 82: int rval;
1.1 deraadt 83:
1.9 jtc 84: domainname = NULL;
1.1 deraadt 85: notrans = key = 0;
1.10 thorpej 86: while ((c = getopt(argc, argv, "xd:kt")) != -1) {
87: switch (c) {
1.1 deraadt 88: case 'x':
1.10 thorpej 89: for(i = 0;
90: i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
1.1 deraadt 91: printf("Use \"%s\" for \"%s\"\n",
92: ypaliases[i].alias,
93: ypaliases[i].name);
94: exit(0);
1.10 thorpej 95:
1.1 deraadt 96: case 'd':
97: domainname = optarg;
98: break;
1.10 thorpej 99:
1.1 deraadt 100: case 't':
101: notrans++;
102: break;
1.10 thorpej 103:
1.1 deraadt 104: case 'k':
105: key++;
106: break;
1.10 thorpej 107:
1.1 deraadt 108: default:
109: usage();
110: }
1.10 thorpej 111: }
112:
113: argc -= optind;
114: argv += optind;
1.1 deraadt 115:
1.10 thorpej 116: if (argc < 2)
1.1 deraadt 117: usage();
1.9 jtc 118:
1.10 thorpej 119: if (domainname == NULL)
1.9 jtc 120: yp_get_default_domain(&domainname);
1.1 deraadt 121:
1.10 thorpej 122: inmap = argv[argc - 1];
123: if (notrans == 0) {
124: for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
125: if (strcmp(inmap, ypaliases[i].alias) == 0)
1.6 jtc 126: inmap = ypaliases[i].name;
127: }
1.8 jtc 128:
129: rval = 0;
1.10 thorpej 130: for(i = 0; i < (argc - 1); i++) {
131: inkey = argv[i];
1.1 deraadt 132:
1.10 thorpej 133: r = yp_match(domainname, inmap, inkey, strlen(inkey),
134: &outbuf, &outbuflen);
135: switch (r) {
1.1 deraadt 136: case 0:
1.10 thorpej 137: if (key)
1.7 jtc 138: printf("%s: ", inkey);
1.1 deraadt 139: printf("%*.*s\n", outbuflen, outbuflen, outbuf);
140: break;
1.10 thorpej 141:
1.1 deraadt 142: case YPERR_YPBIND:
1.10 thorpej 143: errx(1, "not running ypbind");
144:
1.1 deraadt 145: default:
1.10 thorpej 146: warnx("can't match key %s in map %s. Reason: %s",
147: inkey, inmap, yperr_string(r));
1.8 jtc 148: rval = 1;
1.1 deraadt 149: break;
150: }
151: }
1.8 jtc 152:
153: exit(rval);
1.10 thorpej 154: }
155:
156: void
157: usage()
158: {
159:
160: fprintf(stderr, "usage: %s [-d domain] [-t] [-k] key [key ...] "
161: "mapname\n", __progname);
162: fprintf(stderr, " %s -x\n", __progname);
163: exit(1);
1.1 deraadt 164: }
CVSweb <webmaster@jp.NetBSD.org>