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 * 22*a506a34cSth160488 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 317c478bd9Sstevel@tonic-gate * under license from the Regents of the University of 327c478bd9Sstevel@tonic-gate * California. 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * This is a user command which looks up the value of a key in a map 397c478bd9Sstevel@tonic-gate * 407c478bd9Sstevel@tonic-gate * Usage is: 417c478bd9Sstevel@tonic-gate * ypmatch [-d domain] [-t] [-k] key [key ...] mname 427c478bd9Sstevel@tonic-gate * ypmatch -x 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * where: the -d switch can be used to specify a domain other than the 457c478bd9Sstevel@tonic-gate * default domain. mname may be either a mapname, or a nickname which 467c478bd9Sstevel@tonic-gate * will be translated into a mapname according this translation. The 477c478bd9Sstevel@tonic-gate * -k switch prints keys as well as values. The -x switch may be used 487c478bd9Sstevel@tonic-gate * to dump the translation table. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #include <stdio.h> 527c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 537c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h> 547c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h> 557c478bd9Sstevel@tonic-gate #include <string.h> 567c478bd9Sstevel@tonic-gate #include <unistd.h> 577c478bd9Sstevel@tonic-gate #include <stdlib.h> 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static void get_command_line_args(); 607c478bd9Sstevel@tonic-gate static void getdomain(); 617c478bd9Sstevel@tonic-gate static bool match_list(); 627c478bd9Sstevel@tonic-gate static bool match_one(); 637c478bd9Sstevel@tonic-gate static void print_one(); 647c478bd9Sstevel@tonic-gate extern void maketable(); 657c478bd9Sstevel@tonic-gate extern int getmapname(); 667c478bd9Sstevel@tonic-gate extern int yp_match_rsvdport(); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static int translate = TRUE; 697c478bd9Sstevel@tonic-gate static int dodump = FALSE; 707c478bd9Sstevel@tonic-gate static int printkeys = FALSE; 717c478bd9Sstevel@tonic-gate static char *domain = NULL; 727c478bd9Sstevel@tonic-gate static char default_domain_name[YPMAXDOMAIN]; 737c478bd9Sstevel@tonic-gate static char *map = NULL; 747c478bd9Sstevel@tonic-gate static char nm[YPMAXMAP+1]; 757c478bd9Sstevel@tonic-gate static char **keys = NULL; 767c478bd9Sstevel@tonic-gate static int nkeys; 777c478bd9Sstevel@tonic-gate static char err_usage[] = 787c478bd9Sstevel@tonic-gate "Usage:\n\ 797c478bd9Sstevel@tonic-gate ypmatch [-d domain] [-t] [-k] key [key ...] mname\n\ 807c478bd9Sstevel@tonic-gate ypmatch -x\n\ 817c478bd9Sstevel@tonic-gate where\n\ 827c478bd9Sstevel@tonic-gate mname may be either a mapname or a nickname for a map\n\ 837c478bd9Sstevel@tonic-gate -t inhibits map nickname translation\n\ 847c478bd9Sstevel@tonic-gate -k prints keys as well as values.\n\ 857c478bd9Sstevel@tonic-gate -x dumps the map nickname translation table.\n"; 867c478bd9Sstevel@tonic-gate static char err_bad_args[] = 877c478bd9Sstevel@tonic-gate "ypmatch: %s argument is bad.\n"; 887c478bd9Sstevel@tonic-gate static char err_cant_get_kname[] = 897c478bd9Sstevel@tonic-gate "ypmatch: can't get %s back from system call.\n"; 907c478bd9Sstevel@tonic-gate static char err_null_kname[] = 917c478bd9Sstevel@tonic-gate "ypmatch: the %s hasn't been set on this machine.\n"; 927c478bd9Sstevel@tonic-gate static char err_bad_mapname[] = "mapname"; 937c478bd9Sstevel@tonic-gate static char err_bad_domainname[] = "domainname"; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * This is the main line for the ypmatch process. 977c478bd9Sstevel@tonic-gate */ 98*a506a34cSth160488 int 997c478bd9Sstevel@tonic-gate main(argc, argv) 1007c478bd9Sstevel@tonic-gate char **argv; 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv); 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if (dodump) { 1057c478bd9Sstevel@tonic-gate maketable(dodump); 1067c478bd9Sstevel@tonic-gate exit(0); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate if (!domain) { 1107c478bd9Sstevel@tonic-gate getdomain(); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (translate && (strchr(map, '.') == NULL) && 1147c478bd9Sstevel@tonic-gate (getmapname(map, nm))) { 1157c478bd9Sstevel@tonic-gate map = nm; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if (!match_list()) 1197c478bd9Sstevel@tonic-gate return (1); 1207c478bd9Sstevel@tonic-gate return (0); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate /* 1247c478bd9Sstevel@tonic-gate * This does the command line argument processing. 1257c478bd9Sstevel@tonic-gate */ 1267c478bd9Sstevel@tonic-gate static void 1277c478bd9Sstevel@tonic-gate get_command_line_args(argc, argv) 1287c478bd9Sstevel@tonic-gate int argc; 1297c478bd9Sstevel@tonic-gate char **argv; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (argc < 2) { 1347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 1357c478bd9Sstevel@tonic-gate exit(1); 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate argv++; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate while (--argc > 0 && (*argv)[0] == '-') { 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate switch ((*argv)[1]) { 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate case 't': 1447c478bd9Sstevel@tonic-gate translate = FALSE; 1457c478bd9Sstevel@tonic-gate break; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate case 'k': 1487c478bd9Sstevel@tonic-gate printkeys = TRUE; 1497c478bd9Sstevel@tonic-gate break; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate case 'x': 1527c478bd9Sstevel@tonic-gate dodump = TRUE; 1537c478bd9Sstevel@tonic-gate break; 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate case 'd': 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (argc > 1) { 1587c478bd9Sstevel@tonic-gate argv++; 1597c478bd9Sstevel@tonic-gate argc--; 1607c478bd9Sstevel@tonic-gate domain = *argv; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate if ((int)strlen(domain) > YPMAXDOMAIN) { 1637c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_bad_args, 1647c478bd9Sstevel@tonic-gate err_bad_domainname); 1657c478bd9Sstevel@tonic-gate exit(1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate } else { 1697c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 1707c478bd9Sstevel@tonic-gate exit(1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate break; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate default: 1767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 1777c478bd9Sstevel@tonic-gate exit(1); 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate argv++; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (!dodump) { 1847c478bd9Sstevel@tonic-gate if (argc < 2) { 1857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_usage); 1867c478bd9Sstevel@tonic-gate exit(1); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate keys = argv; 1907c478bd9Sstevel@tonic-gate nkeys = argc -1; 1917c478bd9Sstevel@tonic-gate map = argv[argc -1]; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if ((int)strlen(map) > YPMAXMAP) { 1947c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_bad_args, err_bad_mapname); 1957c478bd9Sstevel@tonic-gate exit(1); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * This gets the local default domainname, and makes sure that it's set 2027c478bd9Sstevel@tonic-gate * to something reasonable. domain is set here. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate static void 2057c478bd9Sstevel@tonic-gate getdomain() 2067c478bd9Sstevel@tonic-gate { 2077c478bd9Sstevel@tonic-gate if (!getdomainname(default_domain_name, YPMAXDOMAIN)) { 2087c478bd9Sstevel@tonic-gate domain = default_domain_name; 2097c478bd9Sstevel@tonic-gate } else { 2107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_cant_get_kname, err_bad_domainname); 2117c478bd9Sstevel@tonic-gate exit(1); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if ((int)strlen(domain) == 0) { 2157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, err_null_kname, err_bad_domainname); 2167c478bd9Sstevel@tonic-gate exit(1); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * This traverses the list of argument keys. 2227c478bd9Sstevel@tonic-gate */ 2237c478bd9Sstevel@tonic-gate static bool 2247c478bd9Sstevel@tonic-gate match_list() 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate bool error; 2277c478bd9Sstevel@tonic-gate bool errors = FALSE; 2287c478bd9Sstevel@tonic-gate char *val; 2297c478bd9Sstevel@tonic-gate int len; 2307c478bd9Sstevel@tonic-gate int n = 0; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate while (n < nkeys) { 2337c478bd9Sstevel@tonic-gate error = match_one(keys[n], &val, &len); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate if (!error) { 2367c478bd9Sstevel@tonic-gate print_one(keys[n], val, len); 2377c478bd9Sstevel@tonic-gate free(val); 2387c478bd9Sstevel@tonic-gate } else { 2397c478bd9Sstevel@tonic-gate errors = TRUE; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate n++; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate return (!errors); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * This fires off a "match" request to any old yp server, using the vanilla 2507c478bd9Sstevel@tonic-gate * yp client interface. To cover the case in which trailing NULLs are included 2517c478bd9Sstevel@tonic-gate * in the keys, this retrys the match request including the NULL if the key 2527c478bd9Sstevel@tonic-gate * isn't in the map. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate static bool 2557c478bd9Sstevel@tonic-gate match_one(key, val, len) 2567c478bd9Sstevel@tonic-gate char *key; 2577c478bd9Sstevel@tonic-gate char **val; 2587c478bd9Sstevel@tonic-gate int *len; 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate int err; 2617c478bd9Sstevel@tonic-gate bool error = FALSE; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate *val = NULL; 2647c478bd9Sstevel@tonic-gate *len = 0; 2657c478bd9Sstevel@tonic-gate err = yp_match_rsvdport(domain, map, key, (int)strlen(key), val, len); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate if (err == YPERR_KEY) { 269*a506a34cSth160488 err = yp_match_rsvdport(domain, map, key, 270*a506a34cSth160488 ((int)strlen(key) + 1), 2717c478bd9Sstevel@tonic-gate val, len); 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (err) { 2757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2767c478bd9Sstevel@tonic-gate "Can't match key %s in map %s. Reason: %s.\n", key, map, 2777c478bd9Sstevel@tonic-gate yperr_string(err)); 2787c478bd9Sstevel@tonic-gate error = TRUE; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate return (error); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * This prints the value, (and optionally, the key) after first checking that 2867c478bd9Sstevel@tonic-gate * the last char in the value isn't a NULL. If the last char is a NULL, the 2877c478bd9Sstevel@tonic-gate * \n\0 sequence which the yp client layer has given to us is shuffled back 2887c478bd9Sstevel@tonic-gate * one byte. 2897c478bd9Sstevel@tonic-gate */ 2907c478bd9Sstevel@tonic-gate static void 2917c478bd9Sstevel@tonic-gate print_one(key, val, len) 2927c478bd9Sstevel@tonic-gate char *key; 2937c478bd9Sstevel@tonic-gate char *val; 2947c478bd9Sstevel@tonic-gate int len; 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate if (printkeys) { 2977c478bd9Sstevel@tonic-gate (void) printf("%s: ", key); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate (void) printf("%.*s\n", len, val); 3017c478bd9Sstevel@tonic-gate } 302