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