Annotation of src/usr.bin/ypmatch/ypmatch.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: ypmatch.c,v 1.13 2008/06/01 21:45:08 sobrado 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/param.h>
31: #include <sys/types.h>
32: #include <sys/socket.h>
33: #include <stdio.h>
1.11 david 34: #include <stdlib.h>
1.1 deraadt 35: #include <string.h>
1.6 deraadt 36: #include <unistd.h>
1.1 deraadt 37: #include <ctype.h>
38:
39: #include <rpc/rpc.h>
40: #include <rpc/xdr.h>
41: #include <rpcsvc/yp_prot.h>
42: #include <rpcsvc/ypclnt.h>
1.10 deraadt 43:
44: void usage(void);
1.1 deraadt 45:
46: struct ypalias {
47: char *alias, *name;
48: } ypaliases[] = {
49: { "passwd", "passwd.byname" },
50: { "group", "group.byname" },
51: { "networks", "networks.byaddr" },
52: { "hosts", "hosts.byname" },
53: { "protocols", "protocols.bynumber" },
54: { "services", "services.byname" },
55: { "aliases", "mail.aliases" },
56: { "ethers", "ethers.byname" },
57: };
58:
1.5 deraadt 59: void
1.8 deraadt 60: usage(void)
1.1 deraadt 61: {
1.13 sobrado 62: fprintf(stderr,
63: "usage: ypmatch [-kt] [-d domain] key ... mapname\n"
64: " ypmatch -x\n");
65: fprintf(stderr,
66: "where\n"
67: "\tmapname may be either a mapname or a nickname for a map.\n"
68: "\t-k prints keys as well as values.\n"
69: "\t-t inhibits map nickname translation.\n"
70: "\t-x dumps the map nickname translation table.\n");
1.1 deraadt 71: exit(1);
72: }
73:
74: int
1.8 deraadt 75: main(int argc, char *argv[])
1.1 deraadt 76: {
1.8 deraadt 77: char *domainname, *inkey, *inmap, *outbuf;
1.1 deraadt 78: extern char *optarg;
79: extern int optind;
1.8 deraadt 80: int outbuflen, key, notrans, rval;
1.1 deraadt 81: int c, r, i;
82:
1.5 deraadt 83: domainname = NULL;
1.1 deraadt 84: notrans = key = 0;
1.8 deraadt 85: while ((c=getopt(argc, argv, "xd:kt")) != -1)
1.7 deraadt 86: switch (c) {
1.1 deraadt 87: case 'x':
88: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
89: printf("Use \"%s\" for \"%s\"\n",
90: ypaliases[i].alias,
91: ypaliases[i].name);
92: exit(0);
93: case 'd':
94: domainname = optarg;
95: break;
96: case 't':
97: notrans++;
98: break;
99: case 'k':
100: key++;
101: break;
102: default:
103: usage();
104: }
105:
1.8 deraadt 106: if ((argc-optind) < 2 )
1.1 deraadt 107: usage();
1.5 deraadt 108:
109: if (!domainname) {
110: yp_get_default_domain(&domainname);
111: }
1.1 deraadt 112:
113: inmap = argv[argc-1];
1.3 deraadt 114: if (!notrans) {
115: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.8 deraadt 116: if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3 deraadt 117: inmap = ypaliases[i].name;
118: }
1.4 deraadt 119:
120: rval = 0;
1.1 deraadt 121: for(; optind < argc-1; optind++) {
122: inkey = argv[optind];
123:
124: r = yp_match(domainname, inmap, inkey,
125: strlen(inkey), &outbuf, &outbuflen);
1.7 deraadt 126: switch (r) {
1.1 deraadt 127: case 0:
1.8 deraadt 128: if (key)
1.4 deraadt 129: printf("%s: ", inkey);
1.1 deraadt 130: printf("%*.*s\n", outbuflen, outbuflen, outbuf);
131: break;
132: case YPERR_YPBIND:
133: fprintf(stderr, "yp_match: not running ypbind\n");
134: exit(1);
135: default:
136: fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
1.8 deraadt 137: inkey, inmap, yperr_string(r));
1.4 deraadt 138: rval = 1;
139: break;
1.1 deraadt 140: }
141: }
1.4 deraadt 142: exit(rval);
1.1 deraadt 143: }