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