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