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