1 1.18 matt /* $NetBSD: ypcat.c,v 1.18 2012/03/02 18:55:16 matt Exp $ */ 2 1.6 thorpej 3 1.2 deraadt /* 4 1.3 deraadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca> 5 1.2 deraadt * All rights reserved. 6 1.2 deraadt * 7 1.2 deraadt * Redistribution and use in source and binary forms, with or without 8 1.2 deraadt * modification, are permitted provided that the following conditions 9 1.2 deraadt * are met: 10 1.2 deraadt * 1. Redistributions of source code must retain the above copyright 11 1.2 deraadt * notice, this list of conditions and the following disclaimer. 12 1.2 deraadt * 2. Redistributions in binary form must reproduce the above copyright 13 1.2 deraadt * notice, this list of conditions and the following disclaimer in the 14 1.2 deraadt * documentation and/or other materials provided with the distribution. 15 1.2 deraadt * 16 1.2 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 1.2 deraadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 1.2 deraadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.2 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 1.2 deraadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.2 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.2 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.2 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.2 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.2 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.2 deraadt * SUCH DAMAGE. 27 1.2 deraadt */ 28 1.2 deraadt 29 1.7 thorpej #include <sys/cdefs.h> 30 1.7 thorpej #ifndef lint 31 1.18 matt __RCSID("$NetBSD: ypcat.c,v 1.18 2012/03/02 18:55:16 matt Exp $"); 32 1.2 deraadt #endif 33 1.2 deraadt 34 1.1 deraadt #include <sys/param.h> 35 1.1 deraadt #include <sys/types.h> 36 1.1 deraadt #include <sys/socket.h> 37 1.13 christos #include <sys/errno.h> 38 1.7 thorpej #include <ctype.h> 39 1.7 thorpej #include <err.h> 40 1.1 deraadt #include <stdio.h> 41 1.9 matt #include <stdlib.h> 42 1.9 matt #include <string.h> 43 1.7 thorpej #include <unistd.h> 44 1.1 deraadt 45 1.1 deraadt #include <rpc/rpc.h> 46 1.1 deraadt #include <rpc/xdr.h> 47 1.1 deraadt #include <rpcsvc/yp_prot.h> 48 1.1 deraadt #include <rpcsvc/ypclnt.h> 49 1.1 deraadt 50 1.13 christos #include "ypalias_init.h" 51 1.13 christos 52 1.13 christos static int printit(int, char *, int, char *, int, char *); 53 1.13 christos static void usage(void) __attribute__((__noreturn__)); 54 1.7 thorpej 55 1.15 christos static int compressspace; 56 1.15 christos 57 1.1 deraadt 58 1.1 deraadt int 59 1.13 christos main(int argc, char *argv[]) 60 1.1 deraadt { 61 1.18 matt char *domainname, *b_retry_cnt; 62 1.1 deraadt struct ypall_callback ypcb; 63 1.12 lukem const char *inmap; 64 1.1 deraadt int notrans; 65 1.12 lukem int c, r; 66 1.12 lukem size_t i; 67 1.13 christos const struct ypalias *ypaliases; 68 1.13 christos int key; 69 1.1 deraadt 70 1.13 christos setprogname(*argv); 71 1.18 matt domainname = b_retry_cnt = NULL; 72 1.1 deraadt notrans = key = 0; 73 1.13 christos ypaliases = ypalias_init(); 74 1.17 christos while((c = getopt(argc, argv, "bd:kstx")) != -1) { 75 1.7 thorpej switch (c) { 76 1.17 christos case 'b': 77 1.17 christos b_retry_cnt = optarg; 78 1.17 christos break; 79 1.17 christos 80 1.15 christos case 'd': 81 1.15 christos domainname = optarg; 82 1.15 christos break; 83 1.15 christos 84 1.15 christos case 'k': 85 1.15 christos key++; 86 1.15 christos break; 87 1.15 christos 88 1.15 christos case 's': 89 1.15 christos compressspace++; 90 1.15 christos break; 91 1.15 christos 92 1.1 deraadt case 'x': 93 1.13 christos for (i = 0; ypaliases[i].alias; i++) 94 1.1 deraadt printf("Use \"%s\" for \"%s\"\n", 95 1.13 christos ypaliases[i].alias, 96 1.13 christos ypaliases[i].name); 97 1.13 christos return 0; 98 1.7 thorpej 99 1.1 deraadt case 't': 100 1.1 deraadt notrans++; 101 1.1 deraadt break; 102 1.7 thorpej 103 1.1 deraadt default: 104 1.1 deraadt usage(); 105 1.1 deraadt } 106 1.7 thorpej } 107 1.7 thorpej 108 1.7 thorpej argc -= optind; 109 1.7 thorpej argv += optind; 110 1.1 deraadt 111 1.7 thorpej if (argc != 1) 112 1.1 deraadt usage(); 113 1.5 jtc 114 1.17 christos if (b_retry_cnt != NULL) { 115 1.17 christos char *s; 116 1.17 christos unsigned long l; 117 1.17 christos 118 1.17 christos l = strtoul(b_retry_cnt, &s, 10); 119 1.17 christos if (*s != '\0' || l > 0xffff) usage(); 120 1.17 christos yp_setbindtries((int)l); 121 1.17 christos } 122 1.17 christos 123 1.7 thorpej if (domainname == NULL) 124 1.5 jtc yp_get_default_domain(&domainname); 125 1.1 deraadt 126 1.7 thorpej inmap = argv[0]; 127 1.7 thorpej if (notrans == 0) { 128 1.13 christos for (i = 0; ypaliases[i].alias; i++) 129 1.7 thorpej if (strcmp(inmap, ypaliases[i].alias) == 0) 130 1.4 jtc inmap = ypaliases[i].name; 131 1.4 jtc } 132 1.7 thorpej 133 1.1 deraadt ypcb.foreach = printit; 134 1.13 christos ypcb.data = key ? (void *)&key : NULL; 135 1.1 deraadt 136 1.1 deraadt r = yp_all(domainname, inmap, &ypcb); 137 1.7 thorpej switch (r) { 138 1.1 deraadt case 0: 139 1.1 deraadt break; 140 1.7 thorpej 141 1.1 deraadt case YPERR_YPBIND: 142 1.7 thorpej errx(1, "not running ypbind"); 143 1.7 thorpej 144 1.1 deraadt default: 145 1.7 thorpej errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r)); 146 1.1 deraadt } 147 1.13 christos return 0; 148 1.7 thorpej } 149 1.7 thorpej 150 1.13 christos static int 151 1.13 christos printit(int instatus, char *inkey, int inkeylen, char *inval, 152 1.13 christos int invallen, char *indata) 153 1.7 thorpej { 154 1.7 thorpej 155 1.7 thorpej if (instatus != YP_TRUE) 156 1.7 thorpej return instatus; 157 1.13 christos if (indata) 158 1.13 christos (void)printf("%*.*s", inkeylen, inkeylen, inkey); 159 1.15 christos if (invallen) { 160 1.15 christos if (indata) 161 1.15 christos (void)putc(' ', stdout); 162 1.15 christos if (compressspace) { 163 1.15 christos int i; 164 1.15 christos int hadspace = 0; 165 1.15 christos 166 1.15 christos for (i = 0; i < invallen; i++) { 167 1.15 christos if (isspace((unsigned char)inval[i])) { 168 1.15 christos if (hadspace) 169 1.15 christos continue; 170 1.15 christos hadspace = 1; 171 1.15 christos (void)putc(' ', stdout); 172 1.15 christos } else { 173 1.15 christos hadspace = 0; 174 1.15 christos (void)putc(inval[i], stdout); 175 1.15 christos } 176 1.15 christos } 177 1.15 christos } else 178 1.15 christos (void)printf("%*.*s", invallen, invallen, inval); 179 1.15 christos } 180 1.15 christos (void)putc('\n', stdout); 181 1.7 thorpej return 0; 182 1.7 thorpej } 183 1.7 thorpej 184 1.13 christos static void 185 1.13 christos usage(void) 186 1.7 thorpej { 187 1.7 thorpej 188 1.17 christos (void)fprintf(stderr, "Usage: %s [-kst] [-b <num-retry> " 189 1.17 christos "[-d <domainname>] <mapname>\n", getprogname()); 190 1.13 christos (void)fprintf(stderr, " %s -x\n", getprogname()); 191 1.7 thorpej exit(1); 192 1.1 deraadt } 193