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