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