Home | History | Annotate | Line # | Download | only in ypmatch
ypmatch.c revision 1.4
      1  1.2  deraadt /*
      2  1.4  deraadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      3  1.2  deraadt  * All rights reserved.
      4  1.2  deraadt  *
      5  1.2  deraadt  * Redistribution and use in source and binary forms, with or without
      6  1.2  deraadt  * modification, are permitted provided that the following conditions
      7  1.2  deraadt  * are met:
      8  1.2  deraadt  * 1. Redistributions of source code must retain the above copyright
      9  1.2  deraadt  *    notice, this list of conditions and the following disclaimer.
     10  1.2  deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.2  deraadt  *    notice, this list of conditions and the following disclaimer in the
     12  1.2  deraadt  *    documentation and/or other materials provided with the distribution.
     13  1.4  deraadt  * 3. All advertising materials mentioning features or use of this software
     14  1.4  deraadt  *    must display the following acknowledgement:
     15  1.4  deraadt  *	This product includes software developed by Theo de Raadt.
     16  1.4  deraadt  * 4. The name of the author may not be used to endorse or promote
     17  1.2  deraadt  *    products derived from this software without specific prior written
     18  1.2  deraadt  *    permission.
     19  1.2  deraadt  *
     20  1.2  deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21  1.2  deraadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  1.2  deraadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.2  deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     24  1.2  deraadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.2  deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.2  deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.2  deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.2  deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.2  deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.2  deraadt  * SUCH DAMAGE.
     31  1.2  deraadt  */
     32  1.2  deraadt 
     33  1.2  deraadt #ifndef LINT
     34  1.4  deraadt static char rcsid[] = "$Id: ypmatch.c,v 1.4 1994/05/25 09:54:56 deraadt Exp $";
     35  1.2  deraadt #endif
     36  1.2  deraadt 
     37  1.1  deraadt #include <sys/param.h>
     38  1.1  deraadt #include <sys/types.h>
     39  1.1  deraadt #include <sys/socket.h>
     40  1.1  deraadt #include <stdio.h>
     41  1.1  deraadt #include <ctype.h>
     42  1.1  deraadt 
     43  1.1  deraadt #include <rpc/rpc.h>
     44  1.1  deraadt #include <rpc/xdr.h>
     45  1.1  deraadt #include <rpcsvc/yp_prot.h>
     46  1.1  deraadt #include <rpcsvc/ypclnt.h>
     47  1.1  deraadt 
     48  1.1  deraadt struct ypalias {
     49  1.1  deraadt 	char *alias, *name;
     50  1.1  deraadt } ypaliases[] = {
     51  1.1  deraadt 	{ "passwd", "passwd.byname" },
     52  1.1  deraadt 	{ "group", "group.byname" },
     53  1.1  deraadt 	{ "networks", "networks.byaddr" },
     54  1.3  deraadt 	{ "hosts", "hosts.byname" },
     55  1.1  deraadt 	{ "protocols", "protocols.bynumber" },
     56  1.1  deraadt 	{ "services", "services.byname" },
     57  1.1  deraadt 	{ "aliases", "mail.aliases" },
     58  1.1  deraadt 	{ "ethers", "ethers.byname" },
     59  1.1  deraadt };
     60  1.1  deraadt 
     61  1.1  deraadt usage()
     62  1.1  deraadt {
     63  1.1  deraadt 	fprintf(stderr, "Usage:\n");
     64  1.1  deraadt 	fprintf(stderr, "\typmatch [-d domain] [-t] [-k] key [key ...] mname\n");
     65  1.1  deraadt 	fprintf(stderr, "\typmatch -x\n");
     66  1.1  deraadt 	fprintf(stderr, "where\n");
     67  1.1  deraadt 	fprintf(stderr, "\tmname may be either a mapname or a nickname for a map\n");
     68  1.1  deraadt 	fprintf(stderr, "\t-t inhibits map nickname translation\n");
     69  1.1  deraadt 	fprintf(stderr, "\t-k prints keys as well as values.\n");
     70  1.1  deraadt 	fprintf(stderr, "\t-x dumps the map nickname translation table.\n");
     71  1.1  deraadt 	exit(1);
     72  1.1  deraadt }
     73  1.1  deraadt 
     74  1.1  deraadt int
     75  1.1  deraadt main(argc, argv)
     76  1.1  deraadt char **argv;
     77  1.1  deraadt {
     78  1.1  deraadt 	char *domainname;
     79  1.1  deraadt 	char *inkey, *inmap, *outbuf;
     80  1.1  deraadt 	extern char *optarg;
     81  1.1  deraadt 	extern int optind;
     82  1.1  deraadt 	int outbuflen, key, notrans;
     83  1.1  deraadt 	int c, r, i;
     84  1.1  deraadt 
     85  1.1  deraadt 	notrans = key = 0;
     86  1.1  deraadt 	yp_get_default_domain(&domainname);
     87  1.1  deraadt 
     88  1.1  deraadt 	while( (c=getopt(argc, argv, "xd:kt")) != -1)
     89  1.1  deraadt 		switch(c) {
     90  1.1  deraadt 		case 'x':
     91  1.1  deraadt 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
     92  1.1  deraadt 				printf("Use \"%s\" for \"%s\"\n",
     93  1.1  deraadt 					ypaliases[i].alias,
     94  1.1  deraadt 					ypaliases[i].name);
     95  1.1  deraadt 			exit(0);
     96  1.1  deraadt 		case 'd':
     97  1.1  deraadt 			domainname = optarg;
     98  1.1  deraadt 			break;
     99  1.1  deraadt 		case 't':
    100  1.1  deraadt 			notrans++;
    101  1.1  deraadt 			break;
    102  1.1  deraadt 		case 'k':
    103  1.1  deraadt 			key++;
    104  1.1  deraadt 			break;
    105  1.1  deraadt 		default:
    106  1.1  deraadt 			usage();
    107  1.1  deraadt 		}
    108  1.1  deraadt 
    109  1.1  deraadt 	if( (argc-optind) < 2 )
    110  1.1  deraadt 		usage();
    111  1.1  deraadt 
    112  1.1  deraadt 	inmap = argv[argc-1];
    113  1.1  deraadt 	for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
    114  1.1  deraadt 		if( strcmp(inmap, ypaliases[i].alias) == 0)
    115  1.1  deraadt 			inmap = ypaliases[i].name;
    116  1.1  deraadt 	for(; optind < argc-1; optind++) {
    117  1.1  deraadt 		inkey = argv[optind];
    118  1.1  deraadt 
    119  1.1  deraadt 		r = yp_match(domainname, inmap, inkey,
    120  1.1  deraadt 			strlen(inkey), &outbuf, &outbuflen);
    121  1.1  deraadt 		switch(r) {
    122  1.1  deraadt 		case 0:
    123  1.1  deraadt 			if(key)
    124  1.1  deraadt 				printf("%s ", inkey);
    125  1.1  deraadt 			printf("%*.*s\n", outbuflen, outbuflen, outbuf);
    126  1.1  deraadt 			break;
    127  1.1  deraadt 		case YPERR_YPBIND:
    128  1.1  deraadt 			fprintf(stderr, "yp_match: not running ypbind\n");
    129  1.1  deraadt 			exit(1);
    130  1.1  deraadt 		default:
    131  1.1  deraadt 			fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
    132  1.1  deraadt 				inkey, inmap, yperr_string(r));
    133  1.1  deraadt 			break;
    134  1.1  deraadt 		}
    135  1.1  deraadt 	}
    136  1.1  deraadt 	exit(0);
    137  1.1  deraadt }
    138