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