xref: /titanic_53/usr/src/cmd/ypcmd/ypcat.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
31*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
32*7c478bd9Sstevel@tonic-gate  * California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * This is a user command which dumps each entry in a yp data base.  It gets
39*7c478bd9Sstevel@tonic-gate  * the stuff using the normal ypclnt package; the user doesn't get to choose
40*7c478bd9Sstevel@tonic-gate  * which server gives him the input.  Usage is:
41*7c478bd9Sstevel@tonic-gate  *	ypcat [-k] [-d domain] [-t] map
42*7c478bd9Sstevel@tonic-gate  *	ypcat -x
43*7c478bd9Sstevel@tonic-gate  * where the -k switch will dump keys followed by a single blank space
44*7c478bd9Sstevel@tonic-gate  * before the value, and the -d switch can be used to specify a domain other
45*7c478bd9Sstevel@tonic-gate  * than the default domain. -t switch inhibits nickname translation of map
46*7c478bd9Sstevel@tonic-gate  * names. -x is to dump the nickname translation table from file /var/yp/
47*7c478bd9Sstevel@tonic-gate  * nicknames.
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate #ifdef NULL
51*7c478bd9Sstevel@tonic-gate #undef NULL
52*7c478bd9Sstevel@tonic-gate #endif
53*7c478bd9Sstevel@tonic-gate #define	NULL 0
54*7c478bd9Sstevel@tonic-gate #include <stdio.h>
55*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
56*7c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
57*7c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
58*7c478bd9Sstevel@tonic-gate #include <string.h>
59*7c478bd9Sstevel@tonic-gate #include <unistd.h>
60*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int translate = TRUE;
63*7c478bd9Sstevel@tonic-gate static int dodump = FALSE;
64*7c478bd9Sstevel@tonic-gate static int dumpkeys = FALSE;
65*7c478bd9Sstevel@tonic-gate static char *domain = NULL;
66*7c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
67*7c478bd9Sstevel@tonic-gate static char nm[YPMAXMAP+1];
68*7c478bd9Sstevel@tonic-gate static char *map = NULL;
69*7c478bd9Sstevel@tonic-gate static char nullstring[] = "";
70*7c478bd9Sstevel@tonic-gate static char err_usage[] =
71*7c478bd9Sstevel@tonic-gate "Usage:\n\
72*7c478bd9Sstevel@tonic-gate 	ypcat [-k] [-d domainname] [-t] mapname\n\
73*7c478bd9Sstevel@tonic-gate 	ypcat -x\n\
74*7c478bd9Sstevel@tonic-gate where\n\
75*7c478bd9Sstevel@tonic-gate 	mapname may be either a mapname or a nickname for a map.\n\
76*7c478bd9Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
77*7c478bd9Sstevel@tonic-gate 	-k prints keys as well as values.\n\
78*7c478bd9Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
79*7c478bd9Sstevel@tonic-gate static char err_bad_args[] =
80*7c478bd9Sstevel@tonic-gate 	"ypcat:  %s argument is bad.\n";
81*7c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
82*7c478bd9Sstevel@tonic-gate 	"ypcat:  can't get %s back from system call.\n";
83*7c478bd9Sstevel@tonic-gate static char err_null_kname[] =
84*7c478bd9Sstevel@tonic-gate 	"ypcat:  the %s hasn't been set on this machine.\n";
85*7c478bd9Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
86*7c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
87*7c478bd9Sstevel@tonic-gate static char err_first_failed[] =
88*7c478bd9Sstevel@tonic-gate 	"ypcat:  can't get first record from yp.  Reason:  %s.\n";
89*7c478bd9Sstevel@tonic-gate static char err_next_failed[] =
90*7c478bd9Sstevel@tonic-gate 	"ypcat:  can't get next record from yp.  Reason:  %s.\n";
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate static void get_command_line_args();
93*7c478bd9Sstevel@tonic-gate static int callback();
94*7c478bd9Sstevel@tonic-gate static void one_by_one_all();
95*7c478bd9Sstevel@tonic-gate extern void maketable();
96*7c478bd9Sstevel@tonic-gate extern int getmapname();
97*7c478bd9Sstevel@tonic-gate static void getdomain();
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * This is the mainline for the ypcat process.  It pulls whatever arguments
101*7c478bd9Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate main (argc, argv)
105*7c478bd9Sstevel@tonic-gate 	int argc;
106*7c478bd9Sstevel@tonic-gate 	char **argv;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate {
109*7c478bd9Sstevel@tonic-gate 	int err;
110*7c478bd9Sstevel@tonic-gate 	int fail = 0;
111*7c478bd9Sstevel@tonic-gate 	struct ypall_callback cbinfo;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	if (dodump) {
116*7c478bd9Sstevel@tonic-gate 		maketable(dodump);
117*7c478bd9Sstevel@tonic-gate 		exit(0);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 	if (!domain) {
121*7c478bd9Sstevel@tonic-gate 		getdomain();
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (translate && (strchr(map, '.') == NULL) &&
125*7c478bd9Sstevel@tonic-gate 		(getmapname(map, nm))) {
126*7c478bd9Sstevel@tonic-gate 		map = nm;
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	cbinfo.foreach = callback;
130*7c478bd9Sstevel@tonic-gate 	cbinfo.data = (char *) &fail;
131*7c478bd9Sstevel@tonic-gate 	err = __yp_all_rsvdport(domain, map, &cbinfo);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	if (err == YPERR_VERS) {
134*7c478bd9Sstevel@tonic-gate 		one_by_one_all(domain, map);
135*7c478bd9Sstevel@tonic-gate 	} else if (err) {
136*7c478bd9Sstevel@tonic-gate 		fail = TRUE;
137*7c478bd9Sstevel@tonic-gate 		fprintf (stderr, "%s\n", yperr_string(err));
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	exit(fail);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate static void
147*7c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
148*7c478bd9Sstevel@tonic-gate 	int argc;
149*7c478bd9Sstevel@tonic-gate 	char **argv;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate {
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	argv++;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	while (--argc > 0 && (*argv)[0] == '-') {
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 		switch ((*argv)[1]) {
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		case 't':
160*7c478bd9Sstevel@tonic-gate 			translate = FALSE;
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		case 'k':
164*7c478bd9Sstevel@tonic-gate 			dumpkeys = TRUE;
165*7c478bd9Sstevel@tonic-gate 			break;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		case 'x':
168*7c478bd9Sstevel@tonic-gate 			dodump = TRUE;
169*7c478bd9Sstevel@tonic-gate 			break;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		case 'd':
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 			if (argc > 1) {
174*7c478bd9Sstevel@tonic-gate 				argv++;
175*7c478bd9Sstevel@tonic-gate 				argc--;
176*7c478bd9Sstevel@tonic-gate 				domain = *argv;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 				if ((int)strlen(domain) > YPMAXDOMAIN) {
179*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_bad_args,
180*7c478bd9Sstevel@tonic-gate 					    err_bad_domainname);
181*7c478bd9Sstevel@tonic-gate 					exit(1);
182*7c478bd9Sstevel@tonic-gate 				}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 			} else {
185*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
186*7c478bd9Sstevel@tonic-gate 				exit(1);
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 			break;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 		default:
192*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
193*7c478bd9Sstevel@tonic-gate 			exit(1);
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 		argv++;
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (!dodump) {
199*7c478bd9Sstevel@tonic-gate 		map = *argv;
200*7c478bd9Sstevel@tonic-gate 		if (argc < 1) {
201*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
202*7c478bd9Sstevel@tonic-gate 			exit(1);
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 		if ((int) strlen(map) > YPMAXMAP) {
205*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args, err_bad_mapname);
206*7c478bd9Sstevel@tonic-gate 			exit(1);
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * This dumps out the value, optionally the key, and perhaps an error message.
213*7c478bd9Sstevel@tonic-gate  */
214*7c478bd9Sstevel@tonic-gate static int
215*7c478bd9Sstevel@tonic-gate callback(status, key, kl, val, vl, fail)
216*7c478bd9Sstevel@tonic-gate 	int status;
217*7c478bd9Sstevel@tonic-gate 	char *key;
218*7c478bd9Sstevel@tonic-gate 	int kl;
219*7c478bd9Sstevel@tonic-gate 	char *val;
220*7c478bd9Sstevel@tonic-gate 	int vl;
221*7c478bd9Sstevel@tonic-gate 	int *fail;
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	int e;
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	if (status == YP_TRUE) {
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		if (dumpkeys)
228*7c478bd9Sstevel@tonic-gate 			(void) printf("%.*s ", kl, key);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		(void) printf("%.*s\n", vl, val);
231*7c478bd9Sstevel@tonic-gate 		return (FALSE);
232*7c478bd9Sstevel@tonic-gate 	} else {
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 		e = ypprot_err(status);
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		if (e != YPERR_NOMORE) {
237*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", yperr_string(e));
238*7c478bd9Sstevel@tonic-gate 			*fail = TRUE;
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		return (TRUE);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate /*
246*7c478bd9Sstevel@tonic-gate  * This cats the map out by using the old one-by-one enumeration interface.
247*7c478bd9Sstevel@tonic-gate  * As such, it is prey to the old-style problems of rebinding to different
248*7c478bd9Sstevel@tonic-gate  * servers during the enumeration.
249*7c478bd9Sstevel@tonic-gate  */
250*7c478bd9Sstevel@tonic-gate static void
251*7c478bd9Sstevel@tonic-gate one_by_one_all(domain, map)
252*7c478bd9Sstevel@tonic-gate char *domain;
253*7c478bd9Sstevel@tonic-gate char *map;
254*7c478bd9Sstevel@tonic-gate {
255*7c478bd9Sstevel@tonic-gate 	char *key;
256*7c478bd9Sstevel@tonic-gate 	int keylen;
257*7c478bd9Sstevel@tonic-gate 	char *outkey;
258*7c478bd9Sstevel@tonic-gate 	int outkeylen;
259*7c478bd9Sstevel@tonic-gate 	char *val;
260*7c478bd9Sstevel@tonic-gate 	int vallen;
261*7c478bd9Sstevel@tonic-gate 	int err;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	key = nullstring;
264*7c478bd9Sstevel@tonic-gate 	keylen = 0;
265*7c478bd9Sstevel@tonic-gate 	val = nullstring;
266*7c478bd9Sstevel@tonic-gate 	vallen = 0;
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 	if (err = yp_first(domain, map, &outkey, &outkeylen, &val, &vallen)) {
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 		if (err == YPERR_NOMORE) {
271*7c478bd9Sstevel@tonic-gate 			exit(0);
272*7c478bd9Sstevel@tonic-gate 		} else {
273*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_first_failed,
274*7c478bd9Sstevel@tonic-gate 			    yperr_string(err));
275*7c478bd9Sstevel@tonic-gate 			exit(1);
276*7c478bd9Sstevel@tonic-gate 		}
277*7c478bd9Sstevel@tonic-gate 	}
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	for (;;) {
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 		if (dumpkeys) {
282*7c478bd9Sstevel@tonic-gate 			(void) printf("%.*s ", outkeylen, outkey);
283*7c478bd9Sstevel@tonic-gate 		}
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 		(void) printf("%.*s\n", vallen, val);
286*7c478bd9Sstevel@tonic-gate 		free(val);
287*7c478bd9Sstevel@tonic-gate 		key = outkey;
288*7c478bd9Sstevel@tonic-gate 		keylen = outkeylen;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 		if (err = yp_next(domain, map, key, keylen, &outkey, &outkeylen,
291*7c478bd9Sstevel@tonic-gate 		    &val, &vallen)) {
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 			if (err == YPERR_NOMORE) {
294*7c478bd9Sstevel@tonic-gate 				break;
295*7c478bd9Sstevel@tonic-gate 			} else {
296*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_next_failed,
297*7c478bd9Sstevel@tonic-gate 				    yperr_string(err));
298*7c478bd9Sstevel@tonic-gate 				exit(1);
299*7c478bd9Sstevel@tonic-gate 			}
300*7c478bd9Sstevel@tonic-gate 		}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 		free(key);
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate /*
307*7c478bd9Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
308*7c478bd9Sstevel@tonic-gate  * to something reasonable.  domain is set here.
309*7c478bd9Sstevel@tonic-gate  */
310*7c478bd9Sstevel@tonic-gate static void
311*7c478bd9Sstevel@tonic-gate getdomain()
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
314*7c478bd9Sstevel@tonic-gate 		domain = default_domain_name;
315*7c478bd9Sstevel@tonic-gate 	} else {
316*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
317*7c478bd9Sstevel@tonic-gate 		exit(1);
318*7c478bd9Sstevel@tonic-gate 	}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	if ((int) strlen(domain) == 0) {
321*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
322*7c478bd9Sstevel@tonic-gate 		exit(1);
323*7c478bd9Sstevel@tonic-gate 	}
324*7c478bd9Sstevel@tonic-gate }
325