1 /* $NetBSD: ypmatch.c,v 1.13 2001/02/19 23:03:54 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Theo de Raadt. 18 * 4. The name of the author may not be used to endorse or promote 19 * products derived from this software without specific prior written 20 * permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __RCSID("$NetBSD: ypmatch.c,v 1.13 2001/02/19 23:03:54 cgd Exp $"); 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 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 #include <rpc/rpc.h> 51 #include <rpc/xdr.h> 52 #include <rpcsvc/yp_prot.h> 53 #include <rpcsvc/ypclnt.h> 54 55 const struct ypalias { 56 char *alias, *name; 57 } ypaliases[] = { 58 { "passwd", "passwd.byname" }, 59 { "group", "group.byname" }, 60 { "networks", "networks.byaddr" }, 61 { "hosts", "hosts.byname" }, 62 { "protocols", "protocols.bynumber" }, 63 { "services", "services.byname" }, 64 { "aliases", "mail.aliases" }, 65 { "ethers", "ethers.byname" }, 66 }; 67 68 int main __P((int, char *[])); 69 void usage __P((void)); 70 71 int 72 main(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 char *domainname; 77 char *inkey, *inmap, *outbuf; 78 int outbuflen, key, notrans; 79 int c, r, i; 80 int rval; 81 82 domainname = NULL; 83 notrans = key = 0; 84 while ((c = getopt(argc, argv, "xd:kt")) != -1) { 85 switch (c) { 86 case 'x': 87 for(i = 0; 88 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 94 case 'd': 95 domainname = optarg; 96 break; 97 98 case 't': 99 notrans++; 100 break; 101 102 case 'k': 103 key++; 104 break; 105 106 default: 107 usage(); 108 } 109 } 110 111 argc -= optind; 112 argv += optind; 113 114 if (argc < 2) 115 usage(); 116 117 if (domainname == NULL) 118 yp_get_default_domain(&domainname); 119 120 inmap = argv[argc - 1]; 121 if (notrans == 0) { 122 for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++) 123 if (strcmp(inmap, ypaliases[i].alias) == 0) 124 inmap = ypaliases[i].name; 125 } 126 127 rval = 0; 128 for(i = 0; i < (argc - 1); i++) { 129 inkey = argv[i]; 130 131 r = yp_match(domainname, inmap, inkey, strlen(inkey), 132 &outbuf, &outbuflen); 133 switch (r) { 134 case 0: 135 if (key) 136 printf("%s: ", inkey); 137 printf("%*.*s\n", outbuflen, outbuflen, outbuf); 138 break; 139 140 case YPERR_YPBIND: 141 errx(1, "not running ypbind"); 142 143 default: 144 warnx("can't match key %s in map %s. Reason: %s", 145 inkey, inmap, yperr_string(r)); 146 rval = 1; 147 break; 148 } 149 } 150 151 exit(rval); 152 } 153 154 void 155 usage() 156 { 157 158 fprintf(stderr, "usage: %s [-d domain] [-t] [-k] key [key ...] " 159 "mapname\n", getprogname()); 160 fprintf(stderr, " %s -x\n", getprogname()); 161 exit(1); 162 } 163