1 /* $OpenBSD: ypcat.c,v 1.20 2015/11/11 02:52:46 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <unistd.h> 32 #include <string.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <ctype.h> 36 37 #include <rpc/rpc.h> 38 #include <rpc/xdr.h> 39 #include <rpcsvc/yp.h> 40 #include <rpcsvc/ypclnt.h> 41 42 void usage(void); 43 int printit(u_long, char *, int, char *, int, void *); 44 45 struct ypalias { 46 char *alias, *name; 47 } ypaliases[] = { 48 { "passwd", "passwd.byname" }, 49 { "master.passwd", "master.passwd.byname" }, 50 { "group", "group.byname" }, 51 { "networks", "networks.byaddr" }, 52 { "hosts", "hosts.byaddr" }, 53 { "protocols", "protocols.bynumber" }, 54 { "services", "services.byname" }, 55 { "aliases", "mail.aliases" }, 56 { "ethers", "ethers.byname" }, 57 }; 58 59 int key; 60 61 void 62 usage(void) 63 { 64 fprintf(stderr, 65 "usage: ypcat [-kt] [-d domainname] mapname\n" 66 " ypcat -x\n"); 67 exit(1); 68 } 69 70 int 71 printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen, 72 void *indata) 73 { 74 if (instatus != YP_TRUE) 75 return instatus; 76 if (key) 77 printf("%*.*s ", inkeylen, inkeylen, inkey); 78 printf("%*.*s\n", invallen, invallen, inval); 79 return 0; 80 } 81 82 int 83 main(int argc, char *argv[]) 84 { 85 char *domain = NULL, *inmap; 86 struct ypall_callback ypcb; 87 extern char *optarg; 88 extern int optind; 89 int notrans, c, r, i; 90 91 if (pledge("stdio rpath inet getpw", NULL) == -1) { 92 perror("pledge"); 93 exit(1); 94 } 95 96 notrans = key = 0; 97 while ((c=getopt(argc, argv, "xd:kt")) != -1) 98 switch (c) { 99 case 'x': 100 for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 101 printf("Use \"%s\" for \"%s\"\n", 102 ypaliases[i].alias, ypaliases[i].name); 103 exit(0); 104 case 'd': 105 domain = optarg; 106 break; 107 case 't': 108 notrans = 1; 109 break; 110 case 'k': 111 key = 1; 112 break; 113 default: 114 usage(); 115 } 116 117 if (optind + 1 != argc ) 118 usage(); 119 120 if (!domain) 121 yp_get_default_domain(&domain); 122 123 inmap = argv[optind]; 124 if (!notrans) { 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 ypcb.foreach = printit; 130 ypcb.data = NULL; 131 132 r = yp_all(domain, inmap, &ypcb); 133 switch (r) { 134 case 0: 135 break; 136 case YPERR_YPBIND: 137 fprintf(stderr, "ypcat: not running ypbind\n"); 138 exit(1); 139 default: 140 fprintf(stderr, "No such map %s. Reason: %s\n", 141 inmap, yperr_string(r)); 142 exit(1); 143 } 144 exit(0); 145 } 146