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 #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 <rpc/rpc.h> 39 #include <rpc/xdr.h> 40 #include <rpcsvc/yp_prot.h> 41 #include <rpcsvc/ypclnt.h> 42 43 #include <ctype.h> 44 #include <err.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 const struct ypalias { 51 char *alias, *name; 52 } static 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.byaddr" }, 59 { "protocols", "protocols.bynumber" }, 60 { "services", "services.byname" }, 61 { "aliases", "mail.aliases" }, 62 { "ethers", "ethers.byname" }, 63 }; 64 65 static int key; 66 67 static void 68 usage(void) 69 { 70 fprintf(stderr, "%s\n%s\n", 71 "usage: ypcat [-kt] [-d domainname] mapname", 72 " ypcat -x"); 73 exit(1); 74 } 75 76 static int 77 printit(unsigned long instatus, char *inkey, int inkeylen, char *inval, int invallen, void *dummy __unused) 78 { 79 if (instatus != YP_TRUE) 80 return (instatus); 81 if (key) 82 printf("%*.*s ", inkeylen, inkeylen, inkey); 83 printf("%*.*s\n", invallen, invallen, inval); 84 return (0); 85 } 86 87 int 88 main(int argc, char *argv[]) 89 { 90 char *domainname = NULL; 91 struct ypall_callback ypcb; 92 char *inmap; 93 int notrans; 94 int c, r; 95 u_int i; 96 97 notrans = key = 0; 98 99 while ((c = getopt(argc, argv, "xd:kt")) != -1) 100 switch (c) { 101 case 'x': 102 for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 103 printf("Use \"%s\" for \"%s\"\n", 104 ypaliases[i].alias, 105 ypaliases[i].name); 106 exit(0); 107 case 'd': 108 domainname = optarg; 109 break; 110 case 't': 111 notrans++; 112 break; 113 case 'k': 114 key++; 115 break; 116 default: 117 usage(); 118 } 119 120 if (optind + 1 != argc) 121 usage(); 122 123 if (!domainname) 124 yp_get_default_domain(&domainname); 125 126 inmap = argv[optind]; 127 for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 128 if (strcmp(inmap, ypaliases[i].alias) == 0) 129 inmap = ypaliases[i].name; 130 ypcb.foreach = printit; 131 ypcb.data = NULL; 132 133 r = yp_all(domainname, inmap, &ypcb); 134 switch (r) { 135 case 0: 136 break; 137 case YPERR_YPBIND: 138 errx(1, "not running ypbind"); 139 default: 140 errx(1, "no such map %s. reason: %s", inmap, yperr_string(r)); 141 } 142 exit(0); 143 } 144