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