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