1 /* $NetBSD: ypcat.c,v 1.10 2001/02/19 23:03:54 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Theo de Raadt. 18 * 4. The name of the author may not be used to endorse or promote 19 * products derived from this software without specific prior written 20 * permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 23 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 26 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __RCSID("$NetBSD: ypcat.c,v 1.10 2001/02/19 23:03:54 cgd Exp $"); 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 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 #include <rpc/rpc.h> 51 #include <rpc/xdr.h> 52 #include <rpcsvc/yp_prot.h> 53 #include <rpcsvc/ypclnt.h> 54 55 const struct ypalias { 56 char *alias, *name; 57 } ypaliases[] = { 58 { "passwd", "passwd.byname" }, 59 { "group", "group.byname" }, 60 { "networks", "networks.byaddr" }, 61 { "hosts", "hosts.byaddr" }, 62 { "protocols", "protocols.bynumber" }, 63 { "services", "services.byname" }, 64 { "aliases", "mail.aliases" }, 65 { "ethers", "ethers.byname" }, 66 }; 67 68 int main __P((int, char *[])); 69 int printit __P((int, char *, int, char *, int, char *)); 70 void usage __P((void)); 71 72 int key; 73 74 int 75 main(argc, argv) 76 int argc; 77 char *argv[]; 78 { 79 char *domainname; 80 struct ypall_callback ypcb; 81 char *inmap; 82 int notrans; 83 int c, r, i; 84 85 domainname = NULL; 86 notrans = key = 0; 87 while((c = getopt(argc, argv, "xd:kt")) != -1) { 88 switch (c) { 89 case 'x': 90 for (i = 0; 91 i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++) 92 printf("Use \"%s\" for \"%s\"\n", 93 ypaliases[i].alias, 94 ypaliases[i].name); 95 exit(0); 96 97 case 'd': 98 domainname = optarg; 99 break; 100 101 case 't': 102 notrans++; 103 break; 104 105 case 'k': 106 key++; 107 break; 108 109 default: 110 usage(); 111 } 112 } 113 114 argc -= optind; 115 argv += optind; 116 117 if (argc != 1) 118 usage(); 119 120 if (domainname == NULL) 121 yp_get_default_domain(&domainname); 122 123 inmap = argv[0]; 124 if (notrans == 0) { 125 for (i = 0; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++) 126 if (strcmp(inmap, ypaliases[i].alias) == 0) 127 inmap = ypaliases[i].name; 128 } 129 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 138 case YPERR_YPBIND: 139 errx(1, "not running ypbind"); 140 141 default: 142 errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r)); 143 } 144 exit(0); 145 } 146 147 int 148 printit(instatus, inkey, inkeylen, inval, invallen, indata) 149 int instatus; 150 char *inkey; 151 int inkeylen; 152 char *inval; 153 int invallen; 154 char *indata; 155 { 156 157 if (instatus != YP_TRUE) 158 return instatus; 159 if (key) 160 printf("%*.*s", inkeylen, inkeylen, inkey); 161 if (invallen) 162 printf("%s%*.*s", (key ? " " : ""), invallen, invallen, inval); 163 printf("\n"); 164 return 0; 165 } 166 167 void 168 usage() 169 { 170 171 fprintf(stderr, "usage: %s [-k] [-d domainname] [-t] mapname\n", 172 getprogname()); 173 fprintf(stderr, " %s -x\n", getprogname()); 174 exit(1); 175 } 176