11930Swollman/* 21930Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 31930Swollman * All rights reserved. 41930Swollman * 51930Swollman * Redistribution and use in source and binary forms, with or without 61930Swollman * modification, are permitted provided that the following conditions 71930Swollman * are met: 81930Swollman * 1. Redistributions of source code must retain the above copyright 91930Swollman * notice, this list of conditions and the following disclaimer. 101930Swollman * 2. Redistributions in binary form must reproduce the above copyright 111930Swollman * notice, this list of conditions and the following disclaimer in the 121930Swollman * documentation and/or other materials provided with the distribution. 131930Swollman * 3. The name of the author may not be used to endorse or promote 141930Swollman * products derived from this software without specific prior written 151930Swollman * permission. 161930Swollman * 171930Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 181930Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 191930Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201930Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 211930Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221930Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231930Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241930Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251930Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261930Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271930Swollman * SUCH DAMAGE. 281930Swollman */ 291930Swollman 3087672Smarkm#include <sys/cdefs.h> 311930Swollman 3287672Smarkm__FBSDID("$FreeBSD$"); 3387672Smarkm 341930Swollman#include <sys/param.h> 351930Swollman#include <sys/types.h> 361930Swollman#include <sys/socket.h> 3787672Smarkm 3887672Smarkm#include <rpc/rpc.h> 3987672Smarkm#include <rpc/xdr.h> 4087672Smarkm#include <rpcsvc/yp_prot.h> 4187672Smarkm#include <rpcsvc/ypclnt.h> 4287672Smarkm 43200462Sdelphij#include <ctype.h> 4428894Scharnier#include <err.h> 451930Swollman#include <stdio.h> 4678718Sdd#include <stdlib.h> 4733645Sjb#include <string.h> 4828894Scharnier#include <unistd.h> 491930Swollman 501930Swollmanstruct ypalias { 51121550Speter char *alias, *name; 521930Swollman} ypaliases[] = { 531930Swollman { "passwd", "passwd.byname" }, 5427345Speter { "master.passwd", "master.passwd.byname" }, 55194968Sbrian { "shadow", "shadow.byname" }, 561930Swollman { "group", "group.byname" }, 571930Swollman { "networks", "networks.byaddr" }, 587377Swpaul { "hosts", "hosts.byname" }, 591930Swollman { "protocols", "protocols.bynumber" }, 601930Swollman { "services", "services.byname" }, 611930Swollman { "aliases", "mail.aliases" }, 621930Swollman { "ethers", "ethers.byname" }, 631930Swollman}; 641930Swollman 6528894Scharnierstatic void 6687672Smarkmusage(void) 671930Swollman{ 6828894Scharnier fprintf(stderr, "%s\n%s\n", 69146466Sru "usage: ypmatch [-kt] [-d domainname] key ... mapname", 7028894Scharnier " ypmatch -x"); 711930Swollman exit(1); 721930Swollman} 731930Swollman 741930Swollmanint 7587672Smarkmmain(int argc, char *argv[]) 761930Swollman{ 7728894Scharnier char *domainname = NULL; 781930Swollman char *inkey, *inmap, *outbuf; 791930Swollman int outbuflen, key, notrans; 8087672Smarkm int c, r; 8187672Smarkm u_int i; 821930Swollman 831930Swollman notrans = key = 0; 841930Swollman 8590297Sdes while ((c = getopt(argc, argv, "xd:kt")) != -1) 8690297Sdes switch (c) { 871930Swollman case 'x': 8890297Sdes for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 891930Swollman printf("Use \"%s\" for \"%s\"\n", 901930Swollman ypaliases[i].alias, 911930Swollman ypaliases[i].name); 921930Swollman exit(0); 931930Swollman case 'd': 941930Swollman domainname = optarg; 951930Swollman break; 961930Swollman case 't': 971930Swollman notrans++; 981930Swollman break; 991930Swollman case 'k': 1001930Swollman key++; 1011930Swollman break; 1021930Swollman default: 1031930Swollman usage(); 1041930Swollman } 1051930Swollman 10690297Sdes if ((argc-optind) < 2) 1071930Swollman usage(); 1081930Swollman 10928894Scharnier if (!domainname) 11028894Scharnier yp_get_default_domain(&domainname); 11128894Scharnier 1121930Swollman inmap = argv[argc-1]; 11390297Sdes for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 11490297Sdes if (strcmp(inmap, ypaliases[i].alias) == 0) 1151930Swollman inmap = ypaliases[i].name; 11690297Sdes for (; optind < argc-1; optind++) { 1171930Swollman inkey = argv[optind]; 1181930Swollman 1191930Swollman r = yp_match(domainname, inmap, inkey, 1201930Swollman strlen(inkey), &outbuf, &outbuflen); 12190297Sdes switch (r) { 1221930Swollman case 0: 12390297Sdes if (key) 1241930Swollman printf("%s ", inkey); 1251930Swollman printf("%*.*s\n", outbuflen, outbuflen, outbuf); 1261930Swollman break; 1271930Swollman case YPERR_YPBIND: 12828894Scharnier errx(1, "not running ypbind"); 1291930Swollman default: 13028894Scharnier errx(1, "can't match key %s in map %s. reason: %s", 1311930Swollman inkey, inmap, yperr_string(r)); 1321930Swollman } 1331930Swollman } 1341930Swollman exit(0); 1351930Swollman} 136