ypmatch.c revision 50477
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 31static const char rcsid[] = 32 "$FreeBSD: head/usr.bin/ypmatch/ypmatch.c 50477 1999-08-28 01:08:13Z peter $"; 33#endif /* not lint */ 34 35#include <sys/param.h> 36#include <sys/types.h> 37#include <sys/socket.h> 38#include <ctype.h> 39#include <err.h> 40#include <stdio.h> 41#include <string.h> 42#include <unistd.h> 43 44#include <rpc/rpc.h> 45#include <rpc/xdr.h> 46#include <rpcsvc/yp_prot.h> 47#include <rpcsvc/ypclnt.h> 48 49struct ypalias { 50 char *alias, *name; 51} ypaliases[] = { 52 { "passwd", "passwd.byname" }, 53 { "master.passwd", "master.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 63static void 64usage() 65{ 66 fprintf(stderr, "%s\n%s\n", 67 "usage: ypmatch [-d domain] [-t] [-k] key [key ...] mname", 68 " ypmatch -x"); 69 exit(1); 70} 71 72int 73main(argc, argv) 74char **argv; 75{ 76 char *domainname = NULL; 77 char *inkey, *inmap, *outbuf; 78 int outbuflen, key, notrans; 79 int c, r, i; 80 81 notrans = key = 0; 82 83 while( (c=getopt(argc, argv, "xd:kt")) != -1) 84 switch(c) { 85 case 'x': 86 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 87 printf("Use \"%s\" for \"%s\"\n", 88 ypaliases[i].alias, 89 ypaliases[i].name); 90 exit(0); 91 case 'd': 92 domainname = optarg; 93 break; 94 case 't': 95 notrans++; 96 break; 97 case 'k': 98 key++; 99 break; 100 default: 101 usage(); 102 } 103 104 if( (argc-optind) < 2 ) 105 usage(); 106 107 if (!domainname) 108 yp_get_default_domain(&domainname); 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 errx(1, "not running ypbind"); 127 default: 128 errx(1, "can't match key %s in map %s. reason: %s", 129 inkey, inmap, yperr_string(r)); 130 } 131 } 132 exit(0); 133} 134