1 /* $NetBSD: ypmatch.c,v 1.14 2003/04/12 09:13:29 christos 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.14 2003/04/12 09:13:29 christos 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, null, notrans; 79 int c, r, i, len; 80 int rval; 81 82 domainname = NULL; 83 notrans = key = null = 0; 84 while ((c = getopt(argc, argv, "xd:ktz")) != -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 case 'z': 107 null++; 108 break; 109 110 default: 111 usage(); 112 } 113 } 114 115 argc -= optind; 116 argv += optind; 117 118 if (argc < 2) 119 usage(); 120 121 if (domainname == NULL) 122 yp_get_default_domain(&domainname); 123 124 inmap = argv[argc - 1]; 125 if (notrans == 0) { 126 for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++) 127 if (strcmp(inmap, ypaliases[i].alias) == 0) 128 inmap = ypaliases[i].name; 129 } 130 131 rval = 0; 132 for(i = 0; i < (argc - 1); i++) { 133 inkey = argv[i]; 134 135 len = strlen(inkey); 136 if (null) 137 len++; 138 r = yp_match(domainname, inmap, inkey, len, 139 &outbuf, &outbuflen); 140 switch (r) { 141 case 0: 142 if (key) 143 printf("%s: ", inkey); 144 fwrite(outbuf, outbuflen, 1, stdout); 145 putc('\n', stdout); 146 break; 147 148 case YPERR_YPBIND: 149 errx(1, "not running ypbind"); 150 151 default: 152 warnx("can't match key %s in map %s. Reason: %s", 153 inkey, inmap, yperr_string(r)); 154 rval = 1; 155 break; 156 } 157 } 158 159 exit(rval); 160 } 161 162 void 163 usage() 164 { 165 166 fprintf(stderr, "Usage: %s [-d domain] [-tkz] key [key ...] " 167 "mapname\n", getprogname()); 168 fprintf(stderr, " %s -x\n", getprogname()); 169 exit(1); 170 } 171