1 /* $OpenBSD: ypcat.c,v 1.6 1997/07/21 19:21:14 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com> 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 #ifndef LINT 36 static char rcsid[] = "$OpenBSD: ypcat.c,v 1.6 1997/07/21 19:21:14 deraadt Exp $"; 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/socket.h> 42 #include <unistd.h> 43 #include <string.h> 44 #include <stdio.h> 45 #include <ctype.h> 46 47 #include <rpc/rpc.h> 48 #include <rpc/xdr.h> 49 #include <rpcsvc/yp.h> 50 #include <rpcsvc/ypclnt.h> 51 52 struct ypalias { 53 char *alias, *name; 54 } ypaliases[] = { 55 { "passwd", "passwd.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 int key; 66 67 void 68 usage() 69 { 70 fprintf(stderr, "Usage:\n"); 71 fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n"); 72 fprintf(stderr, "\typcat -x\n"); 73 exit(1); 74 } 75 76 int 77 printit(instatus, inkey, inkeylen, inval, invallen, indata) 78 int instatus; 79 char *inkey; 80 int inkeylen; 81 char *inval; 82 int invallen; 83 char *indata; 84 { 85 if (instatus != YP_TRUE) 86 return instatus; 87 if (key) 88 printf("%*.*s ", inkeylen, inkeylen, inkey); 89 printf("%*.*s\n", invallen, invallen, inval); 90 return 0; 91 } 92 93 int 94 main(argc, argv) 95 char **argv; 96 { 97 char *domain = NULL; 98 struct ypall_callback ypcb; 99 char *inmap; 100 extern char *optarg; 101 extern int optind; 102 int notrans; 103 int c, r, i; 104 105 notrans = key = 0; 106 while ((c=getopt(argc, argv, "xd:kt")) != -1) 107 switch (c) { 108 case 'x': 109 for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 110 printf("Use \"%s\" for \"%s\"\n", 111 ypaliases[i].alias, ypaliases[i].name); 112 exit(0); 113 case 'd': 114 domain = optarg; 115 break; 116 case 't': 117 notrans++; 118 break; 119 case 'k': 120 key++; 121 break; 122 default: 123 usage(); 124 } 125 126 if (optind + 1 != argc ) 127 usage(); 128 129 if (!domain) 130 yp_get_default_domain(&domain); 131 132 inmap = argv[optind]; 133 if (!notrans) { 134 for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 135 if (strcmp(inmap, ypaliases[i].alias) == 0) 136 inmap = ypaliases[i].name; 137 } 138 ypcb.foreach = printit; 139 ypcb.data = NULL; 140 141 r = yp_all(domain, inmap, &ypcb); 142 switch (r) { 143 case 0: 144 break; 145 case YPERR_YPBIND: 146 fprintf(stderr, "ypcat: not running ypbind\n"); 147 exit(1); 148 default: 149 fprintf(stderr, "No such map %s. Reason: %s\n", 150 inmap, yperr_string(r)); 151 exit(1); 152 } 153 exit(0); 154 } 155