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