Annotation of src/usr.bin/ypmatch/ypmatch.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: ypmatch.c,v 1.14 2009/10/27 23:59:50 deraadt Exp $ */
1.4 deraadt 2: /* $NetBSD: ypmatch.c,v 1.8 1996/05/07 01:24:52 jtc Exp $ */
1.1 deraadt 3:
4: /*
1.3 deraadt 5: * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
1.1 deraadt 6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: #include <sys/types.h>
31: #include <sys/socket.h>
32: #include <stdio.h>
1.11 david 33: #include <stdlib.h>
1.1 deraadt 34: #include <string.h>
1.6 deraadt 35: #include <unistd.h>
1.1 deraadt 36: #include <ctype.h>
37:
38: #include <rpc/rpc.h>
39: #include <rpc/xdr.h>
40: #include <rpcsvc/yp_prot.h>
41: #include <rpcsvc/ypclnt.h>
1.10 deraadt 42:
43: void usage(void);
1.1 deraadt 44:
45: struct ypalias {
46: char *alias, *name;
47: } ypaliases[] = {
48: { "passwd", "passwd.byname" },
49: { "group", "group.byname" },
50: { "networks", "networks.byaddr" },
51: { "hosts", "hosts.byname" },
52: { "protocols", "protocols.bynumber" },
53: { "services", "services.byname" },
54: { "aliases", "mail.aliases" },
55: { "ethers", "ethers.byname" },
56: };
57:
1.5 deraadt 58: void
1.8 deraadt 59: usage(void)
1.1 deraadt 60: {
1.13 sobrado 61: fprintf(stderr,
62: "usage: ypmatch [-kt] [-d domain] key ... mapname\n"
63: " ypmatch -x\n");
64: fprintf(stderr,
65: "where\n"
66: "\tmapname may be either a mapname or a nickname for a map.\n"
67: "\t-k prints keys as well as values.\n"
68: "\t-t inhibits map nickname translation.\n"
69: "\t-x dumps the map nickname translation table.\n");
1.1 deraadt 70: exit(1);
71: }
72:
73: int
1.8 deraadt 74: main(int argc, char *argv[])
1.1 deraadt 75: {
1.8 deraadt 76: char *domainname, *inkey, *inmap, *outbuf;
1.1 deraadt 77: extern char *optarg;
78: extern int optind;
1.8 deraadt 79: int outbuflen, key, notrans, rval;
1.1 deraadt 80: int c, r, i;
81:
1.5 deraadt 82: domainname = NULL;
1.1 deraadt 83: notrans = key = 0;
1.8 deraadt 84: while ((c=getopt(argc, argv, "xd:kt")) != -1)
1.7 deraadt 85: switch (c) {
1.1 deraadt 86: case 'x':
87: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
88: printf("Use \"%s\" for \"%s\"\n",
89: ypaliases[i].alias,
90: ypaliases[i].name);
91: exit(0);
92: case 'd':
93: domainname = optarg;
94: break;
95: case 't':
96: notrans++;
97: break;
98: case 'k':
99: key++;
100: break;
101: default:
102: usage();
103: }
104:
1.8 deraadt 105: if ((argc-optind) < 2 )
1.1 deraadt 106: usage();
1.5 deraadt 107:
108: if (!domainname) {
109: yp_get_default_domain(&domainname);
110: }
1.1 deraadt 111:
112: inmap = argv[argc-1];
1.3 deraadt 113: if (!notrans) {
114: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.8 deraadt 115: if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3 deraadt 116: inmap = ypaliases[i].name;
117: }
1.4 deraadt 118:
119: rval = 0;
1.1 deraadt 120: for(; optind < argc-1; optind++) {
121: inkey = argv[optind];
122:
123: r = yp_match(domainname, inmap, inkey,
124: strlen(inkey), &outbuf, &outbuflen);
1.7 deraadt 125: switch (r) {
1.1 deraadt 126: case 0:
1.8 deraadt 127: if (key)
1.4 deraadt 128: printf("%s: ", inkey);
1.1 deraadt 129: printf("%*.*s\n", outbuflen, outbuflen, outbuf);
130: break;
131: case YPERR_YPBIND:
132: fprintf(stderr, "yp_match: not running ypbind\n");
133: exit(1);
134: default:
135: fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
1.8 deraadt 136: inkey, inmap, yperr_string(r));
1.4 deraadt 137: rval = 1;
138: break;
1.1 deraadt 139: }
140: }
1.4 deraadt 141: exit(rval);
1.1 deraadt 142: }