Home | History | Annotate | Line # | Download | only in ypmatch
ypmatch.c revision 1.15
      1 /*	$NetBSD: ypmatch.c,v 1.15 2003/12/10 12:06:26 agc 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __RCSID("$NetBSD: ypmatch.c,v 1.15 2003/12/10 12:06:26 agc Exp $");
     32 #endif
     33 
     34 #include <sys/param.h>
     35 #include <sys/types.h>
     36 #include <sys/socket.h>
     37 #include <ctype.h>
     38 #include <err.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 
     44 #include <rpc/rpc.h>
     45 #include <rpc/xdr.h>
     46 #include <rpcsvc/yp_prot.h>
     47 #include <rpcsvc/ypclnt.h>
     48 
     49 const struct ypalias {
     50 	char *alias, *name;
     51 } ypaliases[] = {
     52 	{ "passwd", "passwd.byname" },
     53 	{ "group", "group.byname" },
     54 	{ "networks", "networks.byaddr" },
     55 	{ "hosts", "hosts.byname" },
     56 	{ "protocols", "protocols.bynumber" },
     57 	{ "services", "services.byname" },
     58 	{ "aliases", "mail.aliases" },
     59 	{ "ethers", "ethers.byname" },
     60 };
     61 
     62 int	main __P((int, char *[]));
     63 void	usage __P((void));
     64 
     65 int
     66 main(argc, argv)
     67 	int argc;
     68 	char *argv[];
     69 {
     70 	char *domainname;
     71 	char *inkey, *inmap, *outbuf;
     72 	int outbuflen, key, null, notrans;
     73 	int c, r, i, len;
     74 	int rval;
     75 
     76 	domainname = NULL;
     77 	notrans = key = null = 0;
     78 	while ((c = getopt(argc, argv, "xd:ktz")) != -1) {
     79 		switch (c) {
     80 		case 'x':
     81 			for(i = 0;
     82 			    i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
     83 				printf("Use \"%s\" for \"%s\"\n",
     84 					ypaliases[i].alias,
     85 					ypaliases[i].name);
     86 			exit(0);
     87 
     88 		case 'd':
     89 			domainname = optarg;
     90 			break;
     91 
     92 		case 't':
     93 			notrans++;
     94 			break;
     95 
     96 		case 'k':
     97 			key++;
     98 			break;
     99 
    100 		case 'z':
    101 			null++;
    102 			break;
    103 
    104 		default:
    105 			usage();
    106 		}
    107 	}
    108 
    109 	argc -= optind;
    110 	argv += optind;
    111 
    112 	if (argc < 2)
    113 		usage();
    114 
    115 	if (domainname == NULL)
    116 		yp_get_default_domain(&domainname);
    117 
    118 	inmap = argv[argc - 1];
    119 	if (notrans == 0) {
    120 		for (i = 0 ; i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
    121 			if (strcmp(inmap, ypaliases[i].alias) == 0)
    122 				inmap = ypaliases[i].name;
    123 	}
    124 
    125 	rval = 0;
    126 	for(i = 0; i < (argc - 1); i++) {
    127 		inkey = argv[i];
    128 
    129 		len = strlen(inkey);
    130 		if (null)
    131 			len++;
    132 		r = yp_match(domainname, inmap, inkey, len,
    133 		    &outbuf, &outbuflen);
    134 		switch (r) {
    135 		case 0:
    136 			if (key)
    137 				printf("%s: ", inkey);
    138 			fwrite(outbuf, outbuflen, 1, stdout);
    139 			putc('\n', stdout);
    140 			break;
    141 
    142 		case YPERR_YPBIND:
    143 			errx(1, "not running ypbind");
    144 
    145 		default:
    146 			warnx("can't match key %s in map %s.  Reason: %s",
    147 			    inkey, inmap, yperr_string(r));
    148 			rval = 1;
    149 			break;
    150 		}
    151 	}
    152 
    153 	exit(rval);
    154 }
    155 
    156 void
    157 usage()
    158 {
    159 
    160 	fprintf(stderr, "Usage: %s [-d domain] [-tkz] key [key ...] "
    161 	    "mapname\n", getprogname());
    162 	fprintf(stderr, "       %s -x\n", getprogname());
    163 	exit(1);
    164 }
    165