xref: /onnv-gate/usr/src/cmd/ypcmd/ypcat.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  *
22*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * This is a user command which dumps each entry in a yp data base.  It gets
390Sstevel@tonic-gate  * the stuff using the normal ypclnt package; the user doesn't get to choose
400Sstevel@tonic-gate  * which server gives him the input.  Usage is:
410Sstevel@tonic-gate  *	ypcat [-k] [-d domain] [-t] map
420Sstevel@tonic-gate  *	ypcat -x
430Sstevel@tonic-gate  * where the -k switch will dump keys followed by a single blank space
440Sstevel@tonic-gate  * before the value, and the -d switch can be used to specify a domain other
450Sstevel@tonic-gate  * than the default domain. -t switch inhibits nickname translation of map
460Sstevel@tonic-gate  * names. -x is to dump the nickname translation table from file /var/yp/
470Sstevel@tonic-gate  * nicknames.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate #ifdef NULL
510Sstevel@tonic-gate #undef NULL
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate #define	NULL 0
540Sstevel@tonic-gate #include <stdio.h>
550Sstevel@tonic-gate #include <rpc/rpc.h>
560Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
570Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
580Sstevel@tonic-gate #include <string.h>
590Sstevel@tonic-gate #include <unistd.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static int translate = TRUE;
630Sstevel@tonic-gate static int dodump = FALSE;
640Sstevel@tonic-gate static int dumpkeys = FALSE;
650Sstevel@tonic-gate static char *domain = NULL;
660Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
670Sstevel@tonic-gate static char nm[YPMAXMAP+1];
680Sstevel@tonic-gate static char *map = NULL;
690Sstevel@tonic-gate static char nullstring[] = "";
700Sstevel@tonic-gate static char err_usage[] =
710Sstevel@tonic-gate "Usage:\n\
720Sstevel@tonic-gate 	ypcat [-k] [-d domainname] [-t] mapname\n\
730Sstevel@tonic-gate 	ypcat -x\n\
740Sstevel@tonic-gate where\n\
750Sstevel@tonic-gate 	mapname may be either a mapname or a nickname for a map.\n\
760Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
770Sstevel@tonic-gate 	-k prints keys as well as values.\n\
780Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
790Sstevel@tonic-gate static char err_bad_args[] =
800Sstevel@tonic-gate 	"ypcat:  %s argument is bad.\n";
810Sstevel@tonic-gate static char err_cant_get_kname[] =
820Sstevel@tonic-gate 	"ypcat:  can't get %s back from system call.\n";
830Sstevel@tonic-gate static char err_null_kname[] =
840Sstevel@tonic-gate 	"ypcat:  the %s hasn't been set on this machine.\n";
850Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
860Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
870Sstevel@tonic-gate static char err_first_failed[] =
880Sstevel@tonic-gate 	"ypcat:  can't get first record from yp.  Reason:  %s.\n";
890Sstevel@tonic-gate static char err_next_failed[] =
900Sstevel@tonic-gate 	"ypcat:  can't get next record from yp.  Reason:  %s.\n";
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static void get_command_line_args();
930Sstevel@tonic-gate static int callback();
940Sstevel@tonic-gate static void one_by_one_all();
950Sstevel@tonic-gate extern void maketable();
960Sstevel@tonic-gate extern int getmapname();
970Sstevel@tonic-gate static void getdomain();
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * This is the mainline for the ypcat process.  It pulls whatever arguments
1010Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
104*702Sth160488 int
main(int argc,char ** argv)105*702Sth160488 main(int argc, char ** argv)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	int err;
1080Sstevel@tonic-gate 	int fail = 0;
1090Sstevel@tonic-gate 	struct ypall_callback cbinfo;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (dodump) {
1140Sstevel@tonic-gate 		maketable(dodump);
1150Sstevel@tonic-gate 		exit(0);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (!domain) {
1190Sstevel@tonic-gate 		getdomain();
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (translate && (strchr(map, '.') == NULL) &&
1230Sstevel@tonic-gate 		(getmapname(map, nm))) {
1240Sstevel@tonic-gate 		map = nm;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	cbinfo.foreach = callback;
128*702Sth160488 	cbinfo.data = (char *)&fail;
1290Sstevel@tonic-gate 	err = __yp_all_rsvdport(domain, map, &cbinfo);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (err == YPERR_VERS) {
1320Sstevel@tonic-gate 		one_by_one_all(domain, map);
1330Sstevel@tonic-gate 	} else if (err) {
1340Sstevel@tonic-gate 		fail = TRUE;
135*702Sth160488 		fprintf(stderr, "%s\n", yperr_string(err));
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	exit(fail);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * This does the command line argument processing.
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1450Sstevel@tonic-gate get_command_line_args(argc, argv)
1460Sstevel@tonic-gate 	int argc;
1470Sstevel@tonic-gate 	char **argv;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	argv++;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	while (--argc > 0 && (*argv)[0] == '-') {
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		switch ((*argv)[1]) {
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		case 't':
1580Sstevel@tonic-gate 			translate = FALSE;
1590Sstevel@tonic-gate 			break;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		case 'k':
1620Sstevel@tonic-gate 			dumpkeys = TRUE;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		case 'x':
1660Sstevel@tonic-gate 			dodump = TRUE;
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		case 'd':
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			if (argc > 1) {
1720Sstevel@tonic-gate 				argv++;
1730Sstevel@tonic-gate 				argc--;
1740Sstevel@tonic-gate 				domain = *argv;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 				if ((int)strlen(domain) > YPMAXDOMAIN) {
1770Sstevel@tonic-gate 					(void) fprintf(stderr, err_bad_args,
1780Sstevel@tonic-gate 					    err_bad_domainname);
1790Sstevel@tonic-gate 					exit(1);
1800Sstevel@tonic-gate 				}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 			} else {
1830Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
1840Sstevel@tonic-gate 				exit(1);
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 			break;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		default:
1900Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
1910Sstevel@tonic-gate 			exit(1);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 		argv++;
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	if (!dodump) {
1970Sstevel@tonic-gate 		map = *argv;
1980Sstevel@tonic-gate 		if (argc < 1) {
1990Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
2000Sstevel@tonic-gate 			exit(1);
2010Sstevel@tonic-gate 		}
202*702Sth160488 		if ((int)strlen(map) > YPMAXMAP) {
2030Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args, err_bad_mapname);
2040Sstevel@tonic-gate 			exit(1);
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * This dumps out the value, optionally the key, and perhaps an error message.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate static int
callback(status,key,kl,val,vl,fail)2130Sstevel@tonic-gate callback(status, key, kl, val, vl, fail)
2140Sstevel@tonic-gate 	int status;
2150Sstevel@tonic-gate 	char *key;
2160Sstevel@tonic-gate 	int kl;
2170Sstevel@tonic-gate 	char *val;
2180Sstevel@tonic-gate 	int vl;
2190Sstevel@tonic-gate 	int *fail;
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	int e;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (status == YP_TRUE) {
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		if (dumpkeys)
2260Sstevel@tonic-gate 			(void) printf("%.*s ", kl, key);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		(void) printf("%.*s\n", vl, val);
2290Sstevel@tonic-gate 		return (FALSE);
2300Sstevel@tonic-gate 	} else {
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		e = ypprot_err(status);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		if (e != YPERR_NOMORE) {
2350Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", yperr_string(e));
2360Sstevel@tonic-gate 			*fail = TRUE;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		return (TRUE);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * This cats the map out by using the old one-by-one enumeration interface.
2450Sstevel@tonic-gate  * As such, it is prey to the old-style problems of rebinding to different
2460Sstevel@tonic-gate  * servers during the enumeration.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate static void
one_by_one_all(domain,map)2490Sstevel@tonic-gate one_by_one_all(domain, map)
2500Sstevel@tonic-gate char *domain;
2510Sstevel@tonic-gate char *map;
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	char *key;
2540Sstevel@tonic-gate 	int keylen;
2550Sstevel@tonic-gate 	char *outkey;
2560Sstevel@tonic-gate 	int outkeylen;
2570Sstevel@tonic-gate 	char *val;
2580Sstevel@tonic-gate 	int vallen;
2590Sstevel@tonic-gate 	int err;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	key = nullstring;
2620Sstevel@tonic-gate 	keylen = 0;
2630Sstevel@tonic-gate 	val = nullstring;
2640Sstevel@tonic-gate 	vallen = 0;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (err = yp_first(domain, map, &outkey, &outkeylen, &val, &vallen)) {
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		if (err == YPERR_NOMORE) {
2690Sstevel@tonic-gate 			exit(0);
2700Sstevel@tonic-gate 		} else {
2710Sstevel@tonic-gate 			(void) fprintf(stderr, err_first_failed,
2720Sstevel@tonic-gate 			    yperr_string(err));
2730Sstevel@tonic-gate 			exit(1);
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	for (;;) {
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if (dumpkeys) {
2800Sstevel@tonic-gate 			(void) printf("%.*s ", outkeylen, outkey);
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		(void) printf("%.*s\n", vallen, val);
2840Sstevel@tonic-gate 		free(val);
2850Sstevel@tonic-gate 		key = outkey;
2860Sstevel@tonic-gate 		keylen = outkeylen;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		if (err = yp_next(domain, map, key, keylen, &outkey, &outkeylen,
2890Sstevel@tonic-gate 		    &val, &vallen)) {
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 			if (err == YPERR_NOMORE) {
2920Sstevel@tonic-gate 				break;
2930Sstevel@tonic-gate 			} else {
2940Sstevel@tonic-gate 				(void) fprintf(stderr, err_next_failed,
2950Sstevel@tonic-gate 				    yperr_string(err));
2960Sstevel@tonic-gate 				exit(1);
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		free(key);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
3060Sstevel@tonic-gate  * to something reasonable.  domain is set here.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate static void
getdomain()3090Sstevel@tonic-gate getdomain()
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
3120Sstevel@tonic-gate 		domain = default_domain_name;
3130Sstevel@tonic-gate 	} else {
3140Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
3150Sstevel@tonic-gate 		exit(1);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
318*702Sth160488 	if ((int)strlen(domain) == 0) {
3190Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
3200Sstevel@tonic-gate 		exit(1);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate }
323