1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 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[] = "ypmatch.c,v 1.2 1993/05/16 02:49:03 deraadt 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 <ctype.h> 39 40 #include <rpc/rpc.h> 41 #include <rpc/xdr.h> 42 #include <rpcsvc/yp_prot.h> 43 #include <rpcsvc/ypclnt.h> 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 usage() 59 { 60 fprintf(stderr, "Usage:\n"); 61 fprintf(stderr, "\typmatch [-d domain] [-t] [-k] key [key ...] mname\n"); 62 fprintf(stderr, "\typmatch -x\n"); 63 fprintf(stderr, "where\n"); 64 fprintf(stderr, "\tmname may be either a mapname or a nickname for a map\n"); 65 fprintf(stderr, "\t-t inhibits map nickname translation\n"); 66 fprintf(stderr, "\t-k prints keys as well as values.\n"); 67 fprintf(stderr, "\t-x dumps the map nickname translation table.\n"); 68 exit(1); 69 } 70 71 int 72 main(argc, argv) 73 char **argv; 74 { 75 char *domainname; 76 char *inkey, *inmap, *outbuf; 77 extern char *optarg; 78 extern int optind; 79 int outbuflen, key, notrans; 80 int c, r, i; 81 82 notrans = key = 0; 83 yp_get_default_domain(&domainname); 84 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 inmap = argv[argc-1]; 110 for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 111 if( strcmp(inmap, ypaliases[i].alias) == 0) 112 inmap = ypaliases[i].name; 113 for(; optind < argc-1; optind++) { 114 inkey = argv[optind]; 115 116 r = yp_match(domainname, inmap, inkey, 117 strlen(inkey), &outbuf, &outbuflen); 118 switch(r) { 119 case 0: 120 if(key) 121 printf("%s ", inkey); 122 printf("%*.*s\n", outbuflen, outbuflen, outbuf); 123 break; 124 case YPERR_YPBIND: 125 fprintf(stderr, "yp_match: not running ypbind\n"); 126 exit(1); 127 default: 128 fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n", 129 inkey, inmap, yperr_string(r)); 130 break; 131 } 132 } 133 exit(0); 134 } 135