1 /* $OpenBSD: ypmatch.c,v 1.14 2009/10/27 23:59:50 deraadt 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/param.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <ctype.h> 38 39 #include <rpc/rpc.h> 40 #include <rpc/xdr.h> 41 #include <rpcsvc/yp_prot.h> 42 #include <rpcsvc/ypclnt.h> 43 44 void usage(void); 45 46 struct ypalias { 47 char *alias, *name; 48 } ypaliases[] = { 49 { "passwd", "passwd.byname" }, 50 { "group", "group.byname" }, 51 { "networks", "networks.byaddr" }, 52 { "hosts", "hosts.byname" }, 53 { "protocols", "protocols.bynumber" }, 54 { "services", "services.byname" }, 55 { "aliases", "mail.aliases" }, 56 { "ethers", "ethers.byname" }, 57 }; 58 59 void 60 usage(void) 61 { 62 fprintf(stderr, 63 "usage: ypmatch [-kt] [-d domain] key ... mapname\n" 64 " ypmatch -x\n"); 65 fprintf(stderr, 66 "where\n" 67 "\tmapname may be either a mapname or a nickname for a map.\n" 68 "\t-k prints keys as well as values.\n" 69 "\t-t inhibits map nickname translation.\n" 70 "\t-x dumps the map nickname translation table.\n"); 71 exit(1); 72 } 73 74 int 75 main(int argc, char *argv[]) 76 { 77 char *domainname, *inkey, *inmap, *outbuf; 78 extern char *optarg; 79 extern int optind; 80 int outbuflen, key, notrans, rval; 81 int c, r, i; 82 83 domainname = NULL; 84 notrans = key = 0; 85 while ((c=getopt(argc, argv, "xd:kt")) != -1) 86 switch (c) { 87 case 'x': 88 for(i=0; 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 case 'd': 94 domainname = optarg; 95 break; 96 case 't': 97 notrans++; 98 break; 99 case 'k': 100 key++; 101 break; 102 default: 103 usage(); 104 } 105 106 if ((argc-optind) < 2 ) 107 usage(); 108 109 if (!domainname) { 110 yp_get_default_domain(&domainname); 111 } 112 113 inmap = argv[argc-1]; 114 if (!notrans) { 115 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; 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 fprintf(stderr, "yp_match: not running ypbind\n"); 134 exit(1); 135 default: 136 fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n", 137 inkey, inmap, yperr_string(r)); 138 rval = 1; 139 break; 140 } 141 } 142 exit(rval); 143 } 144