ypmatch.c revision 121550
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: head/usr.bin/ypmatch/ypmatch.c 121550 2003-10-26 05:28:01Z peter $"); 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 4328894Scharnier#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" }, 551930Swollman { "group", "group.byname" }, 561930Swollman { "networks", "networks.byaddr" }, 577377Swpaul { "hosts", "hosts.byname" }, 581930Swollman { "protocols", "protocols.bynumber" }, 591930Swollman { "services", "services.byname" }, 601930Swollman { "aliases", "mail.aliases" }, 611930Swollman { "ethers", "ethers.byname" }, 621930Swollman}; 631930Swollman 6428894Scharnierstatic void 6587672Smarkmusage(void) 661930Swollman{ 6728894Scharnier fprintf(stderr, "%s\n%s\n", 6828894Scharnier "usage: ypmatch [-d domain] [-t] [-k] key [key ...] mname", 6928894Scharnier " ypmatch -x"); 701930Swollman exit(1); 711930Swollman} 721930Swollman 731930Swollmanint 7487672Smarkmmain(int argc, char *argv[]) 751930Swollman{ 7628894Scharnier char *domainname = NULL; 771930Swollman char *inkey, *inmap, *outbuf; 781930Swollman int outbuflen, key, notrans; 7987672Smarkm int c, r; 8087672Smarkm u_int i; 811930Swollman 821930Swollman notrans = key = 0; 831930Swollman 8490297Sdes while ((c = getopt(argc, argv, "xd:kt")) != -1) 8590297Sdes switch (c) { 861930Swollman case 'x': 8790297Sdes for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 881930Swollman printf("Use \"%s\" for \"%s\"\n", 891930Swollman ypaliases[i].alias, 901930Swollman ypaliases[i].name); 911930Swollman exit(0); 921930Swollman case 'd': 931930Swollman domainname = optarg; 941930Swollman break; 951930Swollman case 't': 961930Swollman notrans++; 971930Swollman break; 981930Swollman case 'k': 991930Swollman key++; 1001930Swollman break; 1011930Swollman default: 1021930Swollman usage(); 1031930Swollman } 1041930Swollman 10590297Sdes if ((argc-optind) < 2) 1061930Swollman usage(); 1071930Swollman 10828894Scharnier if (!domainname) 10928894Scharnier yp_get_default_domain(&domainname); 11028894Scharnier 1111930Swollman inmap = argv[argc-1]; 11290297Sdes for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 11390297Sdes if (strcmp(inmap, ypaliases[i].alias) == 0) 1141930Swollman inmap = ypaliases[i].name; 11590297Sdes for (; optind < argc-1; optind++) { 1161930Swollman inkey = argv[optind]; 1171930Swollman 1181930Swollman r = yp_match(domainname, inmap, inkey, 1191930Swollman strlen(inkey), &outbuf, &outbuflen); 12090297Sdes switch (r) { 1211930Swollman case 0: 12290297Sdes if (key) 1231930Swollman printf("%s ", inkey); 1241930Swollman printf("%*.*s\n", outbuflen, outbuflen, outbuf); 1251930Swollman break; 1261930Swollman case YPERR_YPBIND: 12728894Scharnier errx(1, "not running ypbind"); 1281930Swollman default: 12928894Scharnier errx(1, "can't match key %s in map %s. reason: %s", 1301930Swollman inkey, inmap, yperr_string(r)); 1311930Swollman } 1321930Swollman } 1331930Swollman exit(0); 1341930Swollman} 135