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