xref: /freebsd/usr.bin/ypmatch/ypmatch.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
1 /*
2  * Copyright (c) 1992/3 Theo de Raadt <deraadt@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. The name of the author may not be used to endorse or promote
14  *    products derived from this software without specific prior written
15  *    permission.
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 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 
38 #include <rpc/rpc.h>
39 #include <rpc/xdr.h>
40 #include <rpcsvc/yp_prot.h>
41 #include <rpcsvc/ypclnt.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 struct ypalias {
51 	char *alias, *name;
52 } ypaliases[] = {
53 	{ "passwd", "passwd.byname" },
54 	{ "master.passwd", "master.passwd.byname" },
55 	{ "shadow", "shadow.byname" },
56 	{ "group", "group.byname" },
57 	{ "networks", "networks.byaddr" },
58 	{ "hosts", "hosts.byname" },
59 	{ "protocols", "protocols.bynumber" },
60 	{ "services", "services.byname" },
61 	{ "aliases", "mail.aliases" },
62 	{ "ethers", "ethers.byname" },
63 };
64 
65 static void
66 usage(void)
67 {
68 	fprintf(stderr, "%s\n%s\n",
69 		"usage: ypmatch [-kt] [-d domainname] key ... mapname",
70 		"       ypmatch -x");
71 	exit(1);
72 }
73 
74 int
75 main(int argc, char *argv[])
76 {
77 	char *domainname = NULL;
78 	char *inkey, *inmap, *outbuf;
79 	int outbuflen, key, notrans;
80 	int c, r;
81 	u_int i;
82 
83 	notrans = key = 0;
84 
85 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
86 		switch (c) {
87 		case 'x':
88 			for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
89 				printf("Use \"%s\" for \"%s\"\n",
90 					ypaliases[i].alias,
91 					ypaliases[i].name);
92 			exit(0);
93 		case 'd':
94 			domainname = optarg;
95 			break;
96 		case 't':
97 			notrans++;
98 			break;
99 		case 'k':
100 			key++;
101 			break;
102 		default:
103 			usage();
104 		}
105 
106 	if ((argc-optind) < 2)
107 		usage();
108 
109 	if (!domainname)
110 		yp_get_default_domain(&domainname);
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 			errx(1, "not running ypbind");
129 		default:
130 			errx(1, "can't match key %s in map %s. reason: %s",
131 				inkey, inmap, yperr_string(r));
132 		}
133 	}
134 	exit(0);
135 }
136