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 { "master.passwd", "master.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 usage() 60 { 61 fprintf(stderr, "Usage:\n"); 62 fprintf(stderr, "\typmatch [-d domain] [-t] [-k] key [key ...] mname\n"); 63 fprintf(stderr, "\typmatch -x\n"); 64 fprintf(stderr, "where\n"); 65 fprintf(stderr, "\tmname may be either a mapname or a nickname for a map\n"); 66 fprintf(stderr, "\t-t inhibits map nickname translation\n"); 67 fprintf(stderr, "\t-k prints keys as well as values.\n"); 68 fprintf(stderr, "\t-x dumps the map nickname translation table.\n"); 69 exit(1); 70 } 71 72 int 73 main(argc, argv) 74 char **argv; 75 { 76 char *domainname; 77 char *inkey, *inmap, *outbuf; 78 extern char *optarg; 79 extern int optind; 80 int outbuflen, key, notrans; 81 int c, r, i; 82 83 notrans = key = 0; 84 yp_get_default_domain(&domainname); 85 86 while( (c=getopt(argc, argv, "xd:kt")) != -1) 87 switch(c) { 88 case 'x': 89 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 90 printf("Use \"%s\" for \"%s\"\n", 91 ypaliases[i].alias, 92 ypaliases[i].name); 93 exit(0); 94 case 'd': 95 domainname = optarg; 96 break; 97 case 't': 98 notrans++; 99 break; 100 case 'k': 101 key++; 102 break; 103 default: 104 usage(); 105 } 106 107 if( (argc-optind) < 2 ) 108 usage(); 109 110 inmap = argv[argc-1]; 111 for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 112 if( strcmp(inmap, ypaliases[i].alias) == 0) 113 inmap = ypaliases[i].name; 114 for(; optind < argc-1; optind++) { 115 inkey = argv[optind]; 116 117 r = yp_match(domainname, inmap, inkey, 118 strlen(inkey), &outbuf, &outbuflen); 119 switch(r) { 120 case 0: 121 if(key) 122 printf("%s ", inkey); 123 printf("%*.*s\n", outbuflen, outbuflen, outbuf); 124 break; 125 case YPERR_YPBIND: 126 fprintf(stderr, "yp_match: not running ypbind\n"); 127 exit(1); 128 default: 129 fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n", 130 inkey, inmap, yperr_string(r)); 131 exit(1); 132 break; 133 } 134 } 135 exit(0); 136 } 137