1/* $NetBSD: ypcat.c,v 1.17 2012/03/02 17:27:49 christos 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 *
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/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: ypcat.c,v 1.17 2012/03/02 17:27:49 christos Exp $");
32#endif
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <sys/errno.h>
38#include <ctype.h>
39#include <err.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
45#include <rpc/rpc.h>
46#include <rpc/xdr.h>
47#include <rpcsvc/yp_prot.h>
48#include <rpcsvc/ypclnt.h>
49
50#include "ypalias_init.h"
51
52static int	printit(int, char *, int, char *, int, char *);
53static void	usage(void) __attribute__((__noreturn__));
54
55static int	compressspace;
56
57
58int
59main(int argc, char *argv[])
60{
61	char *domainname, *b_retry_cnt;
62	struct ypall_callback ypcb;
63	const char *inmap;
64	int notrans;
65	int c, r;
66	size_t i;
67	const struct ypalias *ypaliases;
68	int key;
69
70	setprogname(*argv);
71	domainname = b_retry_cnt = NULL;
72	notrans = key = 0;
73	ypaliases = ypalias_init();
74	while((c = getopt(argc, argv, "bd:kstx")) != -1) {
75		switch (c) {
76		case 'b':
77			b_retry_cnt = optarg;
78			break;
79
80		case 'd':
81			domainname = optarg;
82			break;
83
84		case 'k':
85			key++;
86			break;
87
88		case 's':
89			compressspace++;
90			break;
91
92		case 'x':
93			for (i = 0; ypaliases[i].alias; i++)
94				printf("Use \"%s\" for \"%s\"\n",
95				    ypaliases[i].alias,
96				    ypaliases[i].name);
97			return 0;
98
99		case 't':
100			notrans++;
101			break;
102
103		default:
104			usage();
105		}
106	}
107
108	argc -= optind;
109	argv += optind;
110
111	if (argc != 1)
112		usage();
113
114	if (b_retry_cnt != NULL) {
115		char *s;
116		unsigned long l;
117
118		l = strtoul(b_retry_cnt, &s, 10);
119		if (*s != '\0' || l > 0xffff) usage();
120		yp_setbindtries((int)l);
121	}
122
123	if (domainname == NULL)
124		yp_get_default_domain(&domainname);
125
126	inmap = argv[0];
127	if (notrans == 0) {
128		for (i = 0; ypaliases[i].alias; i++)
129			if (strcmp(inmap, ypaliases[i].alias) == 0)
130				inmap = ypaliases[i].name;
131	}
132
133	ypcb.foreach = printit;
134	ypcb.data = key ? (void *)&key : NULL;
135
136	r = yp_all(domainname, inmap, &ypcb);
137	switch (r) {
138	case 0:
139		break;
140
141	case YPERR_YPBIND:
142		errx(1, "not running ypbind");
143
144	default:
145		errx(1, "no such map %s.  Reason: %s", inmap, yperr_string(r));
146	}
147	return 0;
148}
149
150static int
151printit(int instatus, char *inkey, int inkeylen, char *inval,
152    int invallen, char *indata)
153{
154
155	if (instatus != YP_TRUE)
156		return instatus;
157	if (indata)
158		(void)printf("%*.*s", inkeylen, inkeylen, inkey);
159	if (invallen) {
160		if (indata)
161			(void)putc(' ', stdout);
162		if (compressspace) {
163			int i;
164			int hadspace = 0;
165
166			for (i = 0; i < invallen; i++) {
167				if (isspace((unsigned char)inval[i])) {
168					if (hadspace)
169						continue;
170					hadspace = 1;
171					(void)putc(' ', stdout);
172				} else {
173					hadspace = 0;
174					(void)putc(inval[i], stdout);
175				}
176			}
177		} else
178			(void)printf("%*.*s", invallen, invallen, inval);
179	}
180	(void)putc('\n', stdout);
181	return 0;
182}
183
184static void
185usage(void)
186{
187
188	(void)fprintf(stderr, "Usage: %s [-kst] [-b <num-retry> "
189	    "[-d <domainname>] <mapname>\n", getprogname());
190	(void)fprintf(stderr, "       %s -x\n", getprogname());
191	exit(1);
192}
193