xref: /onnv-gate/usr/src/cmd/ypcmd/ypmatch.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 looks up the value of a key in a map
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * Usage is:
410Sstevel@tonic-gate  *	ypmatch [-d domain] [-t] [-k] key [key ...] mname
420Sstevel@tonic-gate  *  ypmatch -x
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * where:  the -d switch can be used to specify a domain other than the
450Sstevel@tonic-gate  * default domain.  mname may be either a mapname, or a nickname which
460Sstevel@tonic-gate  * will be translated into a mapname according this translation.  The
470Sstevel@tonic-gate  * -k switch prints keys as well as values. The -x switch may be used
480Sstevel@tonic-gate  * to dump the translation table.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <rpc/rpc.h>
530Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
540Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
550Sstevel@tonic-gate #include <string.h>
560Sstevel@tonic-gate #include <unistd.h>
570Sstevel@tonic-gate #include <stdlib.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static void get_command_line_args();
600Sstevel@tonic-gate static void getdomain();
610Sstevel@tonic-gate static bool match_list();
620Sstevel@tonic-gate static bool match_one();
630Sstevel@tonic-gate static void print_one();
640Sstevel@tonic-gate extern void maketable();
650Sstevel@tonic-gate extern int getmapname();
66*702Sth160488 extern int yp_match_rsvdport();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int translate = TRUE;
690Sstevel@tonic-gate static int dodump = FALSE;
700Sstevel@tonic-gate static int printkeys = FALSE;
710Sstevel@tonic-gate static char *domain = NULL;
720Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN];
730Sstevel@tonic-gate static char *map = NULL;
740Sstevel@tonic-gate static char nm[YPMAXMAP+1];
750Sstevel@tonic-gate static char **keys = NULL;
760Sstevel@tonic-gate static int nkeys;
770Sstevel@tonic-gate static char err_usage[] =
780Sstevel@tonic-gate "Usage:\n\
790Sstevel@tonic-gate 	ypmatch [-d domain] [-t] [-k] key [key ...] mname\n\
800Sstevel@tonic-gate 	ypmatch -x\n\
810Sstevel@tonic-gate where\n\
820Sstevel@tonic-gate 	mname may be either a mapname or a nickname for a map\n\
830Sstevel@tonic-gate 	-t inhibits map nickname translation\n\
840Sstevel@tonic-gate 	-k prints keys as well as values.\n\
850Sstevel@tonic-gate 	-x dumps the map nickname translation table.\n";
860Sstevel@tonic-gate static char err_bad_args[] =
870Sstevel@tonic-gate 	"ypmatch:  %s argument is bad.\n";
880Sstevel@tonic-gate static char err_cant_get_kname[] =
890Sstevel@tonic-gate 	"ypmatch:  can't get %s back from system call.\n";
900Sstevel@tonic-gate static char err_null_kname[] =
910Sstevel@tonic-gate 	"ypmatch:  the %s hasn't been set on this machine.\n";
920Sstevel@tonic-gate static char err_bad_mapname[] = "mapname";
930Sstevel@tonic-gate static char err_bad_domainname[] = "domainname";
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * This is the main line for the ypmatch process.
970Sstevel@tonic-gate  */
98*702Sth160488 int
main(argc,argv)990Sstevel@tonic-gate main(argc, argv)
1000Sstevel@tonic-gate 	char **argv;
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	get_command_line_args(argc, argv);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (dodump) {
1050Sstevel@tonic-gate 		maketable(dodump);
1060Sstevel@tonic-gate 		exit(0);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (!domain) {
1100Sstevel@tonic-gate 		getdomain();
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (translate && (strchr(map, '.') == NULL) &&
1140Sstevel@tonic-gate 		(getmapname(map, nm))) {
1150Sstevel@tonic-gate 		map = nm;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (!match_list())
1190Sstevel@tonic-gate 		return (1);
1200Sstevel@tonic-gate 	return (0);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * This does the command line argument processing.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate static void
get_command_line_args(argc,argv)1270Sstevel@tonic-gate get_command_line_args(argc, argv)
1280Sstevel@tonic-gate 	int argc;
1290Sstevel@tonic-gate 	char **argv;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (argc < 2) {
1340Sstevel@tonic-gate 		(void) fprintf(stderr, err_usage);
1350Sstevel@tonic-gate 		exit(1);
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	argv++;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	while (--argc > 0 && (*argv)[0] == '-') {
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		switch ((*argv)[1]) {
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		case 't':
1440Sstevel@tonic-gate 			translate = FALSE;
1450Sstevel@tonic-gate 			break;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		case 'k':
1480Sstevel@tonic-gate 			printkeys = TRUE;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		case 'x':
1520Sstevel@tonic-gate 			dodump = TRUE;
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		case 'd':
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 			if (argc > 1) {
1580Sstevel@tonic-gate 				argv++;
1590Sstevel@tonic-gate 				argc--;
1600Sstevel@tonic-gate 				domain = *argv;
1610Sstevel@tonic-gate 
162*702Sth160488 				if ((int)strlen(domain) > YPMAXDOMAIN) {
1630Sstevel@tonic-gate 					(void) fprintf(stderr, err_bad_args,
1640Sstevel@tonic-gate 					    err_bad_domainname);
1650Sstevel@tonic-gate 					exit(1);
1660Sstevel@tonic-gate 				}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 			} else {
1690Sstevel@tonic-gate 				(void) fprintf(stderr, err_usage);
1700Sstevel@tonic-gate 				exit(1);
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		default:
1760Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
1770Sstevel@tonic-gate 			exit(1);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 		argv++;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (!dodump) {
1840Sstevel@tonic-gate 		if (argc < 2) {
1850Sstevel@tonic-gate 			(void) fprintf(stderr, err_usage);
1860Sstevel@tonic-gate 			exit(1);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		keys = argv;
1900Sstevel@tonic-gate 		nkeys = argc -1;
1910Sstevel@tonic-gate 		map = argv[argc -1];
1920Sstevel@tonic-gate 
193*702Sth160488 		if ((int)strlen(map) > YPMAXMAP) {
1940Sstevel@tonic-gate 			(void) fprintf(stderr, err_bad_args, err_bad_mapname);
1950Sstevel@tonic-gate 			exit(1);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * This gets the local default domainname, and makes sure that it's set
2020Sstevel@tonic-gate  * to something reasonable.  domain is set here.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate static void
getdomain()2050Sstevel@tonic-gate getdomain()
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
2080Sstevel@tonic-gate 		domain = default_domain_name;
2090Sstevel@tonic-gate 	} else {
2100Sstevel@tonic-gate 		(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
2110Sstevel@tonic-gate 		exit(1);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
214*702Sth160488 	if ((int)strlen(domain) == 0) {
2150Sstevel@tonic-gate 		(void) fprintf(stderr, err_null_kname, err_bad_domainname);
2160Sstevel@tonic-gate 		exit(1);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * This traverses the list of argument keys.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate static bool
match_list()2240Sstevel@tonic-gate match_list()
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	bool error;
2270Sstevel@tonic-gate 	bool errors = FALSE;
2280Sstevel@tonic-gate 	char *val;
2290Sstevel@tonic-gate 	int len;
2300Sstevel@tonic-gate 	int n = 0;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	while (n < nkeys) {
2330Sstevel@tonic-gate 		error = match_one(keys[n], &val, &len);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		if (!error) {
2360Sstevel@tonic-gate 			print_one(keys[n], val, len);
2370Sstevel@tonic-gate 			free(val);
2380Sstevel@tonic-gate 		} else {
2390Sstevel@tonic-gate 			errors = TRUE;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		n++;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	return (!errors);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * This fires off a "match" request to any old yp server, using the vanilla
2500Sstevel@tonic-gate  * yp client interface.  To cover the case in which trailing NULLs are included
2510Sstevel@tonic-gate  * in the keys, this retrys the match request including the NULL if the key
2520Sstevel@tonic-gate  * isn't in the map.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static bool
match_one(key,val,len)2550Sstevel@tonic-gate match_one(key, val, len)
2560Sstevel@tonic-gate 	char *key;
2570Sstevel@tonic-gate 	char **val;
2580Sstevel@tonic-gate 	int *len;
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	int err;
2610Sstevel@tonic-gate 	bool error = FALSE;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	*val = NULL;
2640Sstevel@tonic-gate 	*len = 0;
265*702Sth160488 	err = yp_match_rsvdport(domain, map, key, (int)strlen(key), val, len);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (err == YPERR_KEY) {
269*702Sth160488 		err = yp_match_rsvdport(domain, map, key,
270*702Sth160488 					((int)strlen(key) + 1),
2710Sstevel@tonic-gate 		    val, len);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (err) {
2750Sstevel@tonic-gate 		(void) fprintf(stderr,
2760Sstevel@tonic-gate 		    "Can't match key %s in map %s.  Reason: %s.\n", key, map,
2770Sstevel@tonic-gate 		    yperr_string(err));
2780Sstevel@tonic-gate 		error = TRUE;
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	return (error);
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * This prints the value, (and optionally, the key) after first checking that
2860Sstevel@tonic-gate  * the last char in the value isn't a NULL.  If the last char is a NULL, the
2870Sstevel@tonic-gate  * \n\0 sequence which the yp client layer has given to us is shuffled back
2880Sstevel@tonic-gate  * one byte.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static void
print_one(key,val,len)2910Sstevel@tonic-gate print_one(key, val, len)
2920Sstevel@tonic-gate 	char *key;
2930Sstevel@tonic-gate 	char *val;
2940Sstevel@tonic-gate 	int len;
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	if (printkeys) {
2970Sstevel@tonic-gate 		(void) printf("%s: ", key);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	(void) printf("%.*s\n", len, val);
3010Sstevel@tonic-gate }
302