xref: /freebsd/usr.bin/ypmatch/ypmatch.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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
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 
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <ctype.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <rpc/rpc.h>
45 #include <rpc/xdr.h>
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 
49 static const struct ypalias {
50 	char *alias, *name;
51 } ypaliases[] = {
52 	{ "passwd", "passwd.byname" },
53 	{ "master.passwd", "master.passwd.byname" },
54 	{ "shadow", "shadow.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 static void
65 usage(void)
66 {
67 	fprintf(stderr, "%s\n%s\n",
68 	    "usage: ypmatch [-kt] [-d domainname] key ... mapname",
69 	    "       ypmatch -x");
70 	exit(1);
71 }
72 
73 int
74 main(int argc, char *argv[])
75 {
76 	char *domainname, *inkey, *inmap, *outbuf;
77 	int outbuflen, key, notrans, rval;
78 	int c, r;
79 	u_int i;
80 
81 	domainname = NULL;
82 	notrans = key = 0;
83 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
84 		switch (c) {
85 		case 'x':
86 			for (i = 0; i < nitems(ypaliases); i++)
87 				printf("Use \"%s\" for \"%s\"\n",
88 					ypaliases[i].alias,
89 					ypaliases[i].name);
90 			exit(0);
91 		case 'd':
92 			domainname = optarg;
93 			break;
94 		case 't':
95 			notrans = 1;
96 			break;
97 		case 'k':
98 			key = 1;
99 			break;
100 		default:
101 			usage();
102 		}
103 
104 	if (argc - optind < 2)
105 		usage();
106 
107 	if (domainname == NULL)
108 		yp_get_default_domain(&domainname);
109 
110 	inmap = argv[argc-1];
111 	if (notrans == 0) {
112 		for (i = 0; i < nitems(ypaliases); i++)
113 			if (strcmp(inmap, ypaliases[i].alias) == 0)
114 				inmap = ypaliases[i].name;
115 	}
116 
117 	rval = 0;
118 	for (; optind < argc - 1; optind++) {
119 		inkey = argv[optind];
120 
121 		r = yp_match(domainname, inmap, inkey,
122 			strlen(inkey), &outbuf, &outbuflen);
123 		switch (r) {
124 		case 0:
125 			if (key)
126 				printf("%s: ", inkey);
127 			printf("%*.*s\n", outbuflen, outbuflen, outbuf);
128 			break;
129 		case YPERR_YPBIND:
130 			errx(1, "not running ypbind");
131 		default:
132 			errx(1, "can't match key %s in map %s. reason: %s",
133 			    inkey, inmap, yperr_string(r));
134 			rval = 1;
135 			break;
136 		}
137 	}
138 	exit(rval);
139 }
140