xref: /freebsd/usr.bin/ypmatch/ypmatch.c (revision 148a8da8)
1 /*	$OpenBSD: ypmatch.c,v 1.16 2015/02/08 23:40:35 deraadt Exp $ */
2 /*	$NetBSD: ypmatch.c,v 1.8 1996/05/07 01:24:52 jtc Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6  *
7  * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include <rpc/rpc.h>
48 #include <rpc/xdr.h>
49 #include <rpcsvc/yp_prot.h>
50 #include <rpcsvc/ypclnt.h>
51 
52 static const struct ypalias {
53 	char *alias, *name;
54 } ypaliases[] = {
55 	{ "passwd", "passwd.byname" },
56 	{ "master.passwd", "master.passwd.byname" },
57 	{ "shadow", "shadow.byname" },
58 	{ "group", "group.byname" },
59 	{ "networks", "networks.byaddr" },
60 	{ "hosts", "hosts.byname" },
61 	{ "protocols", "protocols.bynumber" },
62 	{ "services", "services.byname" },
63 	{ "aliases", "mail.aliases" },
64 	{ "ethers", "ethers.byname" },
65 };
66 
67 static void
68 usage(void)
69 {
70 	fprintf(stderr, "%s\n%s\n",
71 	    "usage: ypmatch [-kt] [-d domainname] key ... mapname",
72 	    "       ypmatch -x");
73 	exit(1);
74 }
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	char *domainname, *inkey, *inmap, *outbuf;
80 	int outbuflen, key, notrans, rval;
81 	int c, r;
82 	u_int i;
83 
84 	domainname = NULL;
85 	notrans = key = 0;
86 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
87 		switch (c) {
88 		case 'x':
89 			for (i = 0; i < nitems(ypaliases); i++)
90 				printf("Use \"%s\" for \"%s\"\n",
91 					ypaliases[i].alias,
92 					ypaliases[i].name);
93 			exit(0);
94 		case 'd':
95 			domainname = optarg;
96 			break;
97 		case 't':
98 			notrans = 1;
99 			break;
100 		case 'k':
101 			key = 1;
102 			break;
103 		default:
104 			usage();
105 		}
106 
107 	if (argc - optind < 2)
108 		usage();
109 
110 	if (domainname == NULL)
111 		yp_get_default_domain(&domainname);
112 
113 	inmap = argv[argc-1];
114 	if (notrans == 0) {
115 		for (i = 0; i < nitems(ypaliases); i++)
116 			if (strcmp(inmap, ypaliases[i].alias) == 0)
117 				inmap = ypaliases[i].name;
118 	}
119 
120 	rval = 0;
121 	for (; optind < argc - 1; optind++) {
122 		inkey = argv[optind];
123 
124 		r = yp_match(domainname, inmap, inkey,
125 			strlen(inkey), &outbuf, &outbuflen);
126 		switch (r) {
127 		case 0:
128 			if (key)
129 				printf("%s: ", inkey);
130 			printf("%*.*s\n", outbuflen, outbuflen, outbuf);
131 			break;
132 		case YPERR_YPBIND:
133 			errx(1, "not running ypbind");
134 		default:
135 			errx(1, "can't match key %s in map %s. reason: %s",
136 			    inkey, inmap, yperr_string(r));
137 			rval = 1;
138 			break;
139 		}
140 	}
141 	exit(rval);
142 }
143