xref: /openbsd/usr.bin/ypmatch/ypmatch.c (revision 4bdff4be)
1 /*	$OpenBSD: ypmatch.c,v 1.17 2021/06/22 20:14:25 jmc 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  *
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/types.h>
31 #include <sys/socket.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <ctype.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 void	usage(void);
44 
45 struct ypalias {
46 	char *alias, *name;
47 } ypaliases[] = {
48 	{ "passwd", "passwd.byname" },
49 	{ "group", "group.byname" },
50 	{ "networks", "networks.byaddr" },
51 	{ "hosts", "hosts.byname" },
52 	{ "protocols", "protocols.bynumber" },
53 	{ "services", "services.byname" },
54 	{ "aliases", "mail.aliases" },
55 	{ "ethers", "ethers.byname" },
56 };
57 
58 void
59 usage(void)
60 {
61 	fprintf(stderr,
62 	    "usage: ypmatch [-kt] [-d domain] key ... mapname\n"
63 	    "       ypmatch -x\n");
64 
65 	exit(1);
66 }
67 
68 int
69 main(int argc, char *argv[])
70 {
71 	char *domainname, *inkey, *inmap, *outbuf;
72 	extern char *optarg;
73 	extern int optind;
74 	int outbuflen, key, notrans, rval;
75 	int c, r, i;
76 
77 	domainname = NULL;
78 	notrans = key = 0;
79 	while ((c=getopt(argc, argv, "xd:kt")) != -1)
80 		switch (c) {
81 		case 'x':
82 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
83 				printf("Use \"%s\" for \"%s\"\n",
84 					ypaliases[i].alias,
85 					ypaliases[i].name);
86 			exit(0);
87 		case 'd':
88 			domainname = optarg;
89 			break;
90 		case 't':
91 			notrans = 1;
92 			break;
93 		case 'k':
94 			key = 1;
95 			break;
96 		default:
97 			usage();
98 		}
99 
100 	if ((argc-optind) < 2 )
101 		usage();
102 
103 	if (!domainname) {
104 		yp_get_default_domain(&domainname);
105 	}
106 
107 	inmap = argv[argc-1];
108 	if (!notrans) {
109 		for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
110 			if (strcmp(inmap, ypaliases[i].alias) == 0)
111 				inmap = ypaliases[i].name;
112 	}
113 
114 	rval = 0;
115 	for(; optind < argc-1; optind++) {
116 		inkey = argv[optind];
117 
118 		r = yp_match(domainname, inmap, inkey,
119 			strlen(inkey), &outbuf, &outbuflen);
120 		switch (r) {
121 		case 0:
122 			if (key)
123 				printf("%s: ", inkey);
124 			printf("%*.*s\n", outbuflen, outbuflen, outbuf);
125 			break;
126 		case YPERR_YPBIND:
127 			fprintf(stderr, "yp_match: not running ypbind\n");
128 			exit(1);
129 		default:
130 			fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
131 			    inkey, inmap, yperr_string(r));
132 			rval = 1;
133 			break;
134 		}
135 	}
136 	exit(rval);
137 }
138