xref: /illumos-gate/usr/src/cmd/ypcmd/ypcat.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  *
22a506a34cSth  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
337c478bd9Sstevel@tonic-gate  * California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * This is a user command which dumps each entry in a yp data base.  It gets
387c478bd9Sstevel@tonic-gate  * the stuff using the normal ypclnt package; the user doesn't get to choose
39*48bbca81SDaniel Hoffman  * which server gives them the input.  Usage is:
407c478bd9Sstevel@tonic-gate  *	ypcat [-k] [-d domain] [-t] map
417c478bd9Sstevel@tonic-gate  *	ypcat -x
427c478bd9Sstevel@tonic-gate  * where the -k switch will dump keys followed by a single blank space
437c478bd9Sstevel@tonic-gate  * before the value, and the -d switch can be used to specify a domain other
447c478bd9Sstevel@tonic-gate  * than the default domain. -t switch inhibits nickname translation of map
457c478bd9Sstevel@tonic-gate  * names. -x is to dump the nickname translation table from file /var/yp/
467c478bd9Sstevel@tonic-gate  * nicknames.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate #ifdef NULL
507c478bd9Sstevel@tonic-gate #undef NULL
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate #define	NULL 0
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
557c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
567c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
577c478bd9Sstevel@tonic-gate #include <string.h>
587c478bd9Sstevel@tonic-gate #include <unistd.h>
597c478bd9Sstevel@tonic-gate #include <stdlib.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static int translate = TRUE;
627c478bd9Sstevel@tonic-gate static int dodump = FALSE;
637c478bd9Sstevel@tonic-gate static int dumpkeys = FALSE;
647c478bd9Sstevel@tonic-gate static char *domain = NULL;
657c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
667c478bd9Sstevel@tonic-gate static char nm[YPMAXMAP+1];
677c478bd9Sstevel@tonic-gate static char *map = NULL;
687c478bd9Sstevel@tonic-gate static char nullstring[] = "";
697c478bd9Sstevel@tonic-gate static char err_usage[] =
707c478bd9Sstevel@tonic-gate "Usage:\n\
717c478bd9Sstevel@tonic-gate 	ypcat [-k] [-d domainname] [-t] mapname\n\
727c478bd9Sstevel@tonic-gate 	ypcat -x\n\
737c478bd9Sstevel@tonic-gate where\n\
747c478bd9Sstevel@tonic-gate 	mapname may be either a mapname or a nickname for a map.\n\
757c478bd9Sstevel@tonic-gate 	-t inhibits map nickname translation.\n\
767c478bd9Sstevel@tonic-gate 	-k prints keys as well as values.\n\
777c478bd9Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
787c478bd9Sstevel@tonic-gate static char err_bad_args[] =
797c478bd9Sstevel@tonic-gate 	"ypcat:  %s argument is bad.\n";
807c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] =
817c478bd9Sstevel@tonic-gate 	"ypcat:  can't get %s back from system call.\n";
827c478bd9Sstevel@tonic-gate static char err_null_kname[] =
837c478bd9Sstevel@tonic-gate 	"ypcat:  the %s hasn't been set on this machine.\n";
847c478bd9Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
857c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
867c478bd9Sstevel@tonic-gate static char err_first_failed[] =
877c478bd9Sstevel@tonic-gate 	"ypcat:  can't get first record from yp.  Reason:  %s.\n";
887c478bd9Sstevel@tonic-gate static char err_next_failed[] =
897c478bd9Sstevel@tonic-gate 	"ypcat:  can't get next record from yp.  Reason:  %s.\n";
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static void get_command_line_args();
927c478bd9Sstevel@tonic-gate static int callback();
937c478bd9Sstevel@tonic-gate static void one_by_one_all();
947c478bd9Sstevel@tonic-gate extern void maketable();
957c478bd9Sstevel@tonic-gate extern int getmapname();
967c478bd9Sstevel@tonic-gate static void getdomain();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * This is the mainline for the ypcat process.  It pulls whatever arguments
1007c478bd9Sstevel@tonic-gate  * have been passed from the command line, and uses defaults for the rest.
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate 
103a506a34cSth int
main(int argc,char ** argv)104a506a34cSth main(int argc, char ** argv)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	int err;
1077c478bd9Sstevel@tonic-gate 	int fail = 0;
1087c478bd9Sstevel@tonic-gate 	struct ypall_callback cbinfo;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (dodump) {
1137c478bd9Sstevel@tonic-gate 		maketable(dodump);
1147c478bd9Sstevel@tonic-gate 		exit(0);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (!domain) {
1187c478bd9Sstevel@tonic-gate 		getdomain();
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (translate && (strchr(map, '.') == NULL) &&
1227c478bd9Sstevel@tonic-gate 		(getmapname(map, nm))) {
1237c478bd9Sstevel@tonic-gate 		map = nm;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	cbinfo.foreach = callback;
127a506a34cSth 	cbinfo.data = (char *)&fail;
1287c478bd9Sstevel@tonic-gate 	err = __yp_all_rsvdport(domain, map, &cbinfo);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (err == YPERR_VERS) {
1317c478bd9Sstevel@tonic-gate 		one_by_one_all(domain, map);
1327c478bd9Sstevel@tonic-gate 	} else if (err) {
1337c478bd9Sstevel@tonic-gate 		fail = TRUE;
134a506a34cSth 		fprintf(stderr, "%s\n", yperr_string(err));
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	exit(fail);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * This does the command line argument processing.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1447c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv)
1457c478bd9Sstevel@tonic-gate 	int argc;
1467c478bd9Sstevel@tonic-gate 	char **argv;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	argv++;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	while (--argc > 0 && (*argv)[0] == '-') {
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		switch ((*argv)[1]) {
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		case 't':
1577c478bd9Sstevel@tonic-gate 			translate = FALSE;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		case 'k':
1617c478bd9Sstevel@tonic-gate 			dumpkeys = TRUE;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'x':
1657c478bd9Sstevel@tonic-gate 			dodump = TRUE;
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		case 'd':
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 			if (argc > 1) {
1717c478bd9Sstevel@tonic-gate 				argv++;
1727c478bd9Sstevel@tonic-gate 				argc--;
1737c478bd9Sstevel@tonic-gate 				domain = *argv;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 				if ((int)strlen(domain) > YPMAXDOMAIN) {
1767c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, err_bad_args,
1777c478bd9Sstevel@tonic-gate 					    err_bad_domainname);
1787c478bd9Sstevel@tonic-gate 					exit(1);
1797c478bd9Sstevel@tonic-gate 				}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			} else {
1827c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
1837c478bd9Sstevel@tonic-gate 				exit(1);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		default:
1897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
1907c478bd9Sstevel@tonic-gate 			exit(1);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		argv++;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (!dodump) {
1967c478bd9Sstevel@tonic-gate 		map = *argv;
1977c478bd9Sstevel@tonic-gate 		if (argc < 1) {
1987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
1997c478bd9Sstevel@tonic-gate 			exit(1);
2007c478bd9Sstevel@tonic-gate 		}
201a506a34cSth 		if ((int)strlen(map) > YPMAXMAP) {
2027c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args, err_bad_mapname);
2037c478bd9Sstevel@tonic-gate 			exit(1);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * This dumps out the value, optionally the key, and perhaps an error message.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static int
callback(status,key,kl,val,vl,fail)2127c478bd9Sstevel@tonic-gate callback(status, key, kl, val, vl, fail)
2137c478bd9Sstevel@tonic-gate 	int status;
2147c478bd9Sstevel@tonic-gate 	char *key;
2157c478bd9Sstevel@tonic-gate 	int kl;
2167c478bd9Sstevel@tonic-gate 	char *val;
2177c478bd9Sstevel@tonic-gate 	int vl;
2187c478bd9Sstevel@tonic-gate 	int *fail;
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int e;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (status == YP_TRUE) {
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		if (dumpkeys)
2257c478bd9Sstevel@tonic-gate 			(void) printf("%.*s ", kl, key);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		(void) printf("%.*s\n", vl, val);
2287c478bd9Sstevel@tonic-gate 		return (FALSE);
2297c478bd9Sstevel@tonic-gate 	} else {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		e = ypprot_err(status);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (e != YPERR_NOMORE) {
2347c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", yperr_string(e));
2357c478bd9Sstevel@tonic-gate 			*fail = TRUE;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		return (TRUE);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * This cats the map out by using the old one-by-one enumeration interface.
2447c478bd9Sstevel@tonic-gate  * As such, it is prey to the old-style problems of rebinding to different
2457c478bd9Sstevel@tonic-gate  * servers during the enumeration.
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate static void
one_by_one_all(domain,map)2487c478bd9Sstevel@tonic-gate one_by_one_all(domain, map)
2497c478bd9Sstevel@tonic-gate char *domain;
2507c478bd9Sstevel@tonic-gate char *map;
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	char *key;
2537c478bd9Sstevel@tonic-gate 	int keylen;
2547c478bd9Sstevel@tonic-gate 	char *outkey;
2557c478bd9Sstevel@tonic-gate 	int outkeylen;
2567c478bd9Sstevel@tonic-gate 	char *val;
2577c478bd9Sstevel@tonic-gate 	int vallen;
2587c478bd9Sstevel@tonic-gate 	int err;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	key = nullstring;
2617c478bd9Sstevel@tonic-gate 	keylen = 0;
2627c478bd9Sstevel@tonic-gate 	val = nullstring;
2637c478bd9Sstevel@tonic-gate 	vallen = 0;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (err = yp_first(domain, map, &outkey, &outkeylen, &val, &vallen)) {
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 		if (err == YPERR_NOMORE) {
2687c478bd9Sstevel@tonic-gate 			exit(0);
2697c478bd9Sstevel@tonic-gate 		} else {
2707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, err_first_failed,
2717c478bd9Sstevel@tonic-gate 			    yperr_string(err));
2727c478bd9Sstevel@tonic-gate 			exit(1);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	for (;;) {
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		if (dumpkeys) {
2797c478bd9Sstevel@tonic-gate 			(void) printf("%.*s ", outkeylen, outkey);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		(void) printf("%.*s\n", vallen, val);
2837c478bd9Sstevel@tonic-gate 		free(val);
2847c478bd9Sstevel@tonic-gate 		key = outkey;
2857c478bd9Sstevel@tonic-gate 		keylen = outkeylen;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		if (err = yp_next(domain, map, key, keylen, &outkey, &outkeylen,
2887c478bd9Sstevel@tonic-gate 		    &val, &vallen)) {
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 			if (err == YPERR_NOMORE) {
2917c478bd9Sstevel@tonic-gate 				break;
2927c478bd9Sstevel@tonic-gate 			} else {
2937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, err_next_failed,
2947c478bd9Sstevel@tonic-gate 				    yperr_string(err));
2957c478bd9Sstevel@tonic-gate 				exit(1);
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		free(key);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
3057c478bd9Sstevel@tonic-gate  * to something reasonable.  domain is set here.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate static void
getdomain()3087c478bd9Sstevel@tonic-gate getdomain()
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
3117c478bd9Sstevel@tonic-gate 		domain = default_domain_name;
3127c478bd9Sstevel@tonic-gate 	} else {
3137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
3147c478bd9Sstevel@tonic-gate 		exit(1);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
317a506a34cSth 	if ((int)strlen(domain) == 0) {
3187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
3197c478bd9Sstevel@tonic-gate 		exit(1);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate }
322