ypcat.c revision 1.14 1 1.14 wiz /* $NetBSD: ypcat.c,v 1.14 2009/06/21 14:58:16 wiz 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.14 wiz __RCSID("$NetBSD: ypcat.c,v 1.14 2009/06/21 14:58:16 wiz 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.1 deraadt
56 1.1 deraadt int
57 1.13 christos main(int argc, char *argv[])
58 1.1 deraadt {
59 1.1 deraadt char *domainname;
60 1.1 deraadt struct ypall_callback ypcb;
61 1.12 lukem const char *inmap;
62 1.1 deraadt int notrans;
63 1.12 lukem int c, r;
64 1.12 lukem size_t i;
65 1.13 christos const struct ypalias *ypaliases;
66 1.13 christos int key;
67 1.1 deraadt
68 1.13 christos setprogname(*argv);
69 1.5 jtc domainname = NULL;
70 1.1 deraadt notrans = key = 0;
71 1.13 christos ypaliases = ypalias_init();
72 1.7 thorpej while((c = getopt(argc, argv, "xd:kt")) != -1) {
73 1.7 thorpej switch (c) {
74 1.1 deraadt case 'x':
75 1.13 christos for (i = 0; ypaliases[i].alias; i++)
76 1.1 deraadt printf("Use \"%s\" for \"%s\"\n",
77 1.13 christos ypaliases[i].alias,
78 1.13 christos ypaliases[i].name);
79 1.13 christos return 0;
80 1.7 thorpej
81 1.1 deraadt case 'd':
82 1.1 deraadt domainname = optarg;
83 1.1 deraadt break;
84 1.7 thorpej
85 1.1 deraadt case 't':
86 1.1 deraadt notrans++;
87 1.1 deraadt break;
88 1.7 thorpej
89 1.1 deraadt case 'k':
90 1.1 deraadt key++;
91 1.1 deraadt break;
92 1.7 thorpej
93 1.1 deraadt default:
94 1.1 deraadt usage();
95 1.1 deraadt }
96 1.7 thorpej }
97 1.7 thorpej
98 1.7 thorpej argc -= optind;
99 1.7 thorpej argv += optind;
100 1.1 deraadt
101 1.7 thorpej if (argc != 1)
102 1.1 deraadt usage();
103 1.5 jtc
104 1.7 thorpej if (domainname == NULL)
105 1.5 jtc yp_get_default_domain(&domainname);
106 1.1 deraadt
107 1.7 thorpej inmap = argv[0];
108 1.7 thorpej if (notrans == 0) {
109 1.13 christos for (i = 0; ypaliases[i].alias; i++)
110 1.7 thorpej if (strcmp(inmap, ypaliases[i].alias) == 0)
111 1.4 jtc inmap = ypaliases[i].name;
112 1.4 jtc }
113 1.7 thorpej
114 1.1 deraadt ypcb.foreach = printit;
115 1.13 christos ypcb.data = key ? (void *)&key : NULL;
116 1.1 deraadt
117 1.1 deraadt r = yp_all(domainname, inmap, &ypcb);
118 1.7 thorpej switch (r) {
119 1.1 deraadt case 0:
120 1.1 deraadt break;
121 1.7 thorpej
122 1.1 deraadt case YPERR_YPBIND:
123 1.7 thorpej errx(1, "not running ypbind");
124 1.7 thorpej
125 1.1 deraadt default:
126 1.7 thorpej errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r));
127 1.1 deraadt }
128 1.13 christos return 0;
129 1.7 thorpej }
130 1.7 thorpej
131 1.13 christos static int
132 1.13 christos printit(int instatus, char *inkey, int inkeylen, char *inval,
133 1.13 christos int invallen, char *indata)
134 1.7 thorpej {
135 1.7 thorpej
136 1.7 thorpej if (instatus != YP_TRUE)
137 1.7 thorpej return instatus;
138 1.13 christos if (indata)
139 1.13 christos (void)printf("%*.*s", inkeylen, inkeylen, inkey);
140 1.8 lukem if (invallen)
141 1.13 christos (void)printf("%s%*.*s", (indata ? " " : ""), invallen, invallen,
142 1.13 christos inval);
143 1.13 christos (void)printf("\n");
144 1.7 thorpej return 0;
145 1.7 thorpej }
146 1.7 thorpej
147 1.13 christos static void
148 1.13 christos usage(void)
149 1.7 thorpej {
150 1.7 thorpej
151 1.14 wiz (void)fprintf(stderr, "Usage: %s [-kt] [-d domainname] mapname\n",
152 1.10 cgd getprogname());
153 1.13 christos (void)fprintf(stderr, " %s -x\n", getprogname());
154 1.7 thorpej exit(1);
155 1.1 deraadt }
156