Home | History | Annotate | Line # | Download | only in ypcat
ypcat.c revision 1.6
      1 /*	$NetBSD: ypcat.c,v 1.6 1996/05/13 02:43:38 thorpej 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: ypcat.c,v 1.6 1996/05/13 02:43:38 thorpej 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 <ctype.h>
     44 
     45 #include <rpc/rpc.h>
     46 #include <rpc/xdr.h>
     47 #include <rpcsvc/yp_prot.h>
     48 #include <rpcsvc/ypclnt.h>
     49 
     50 struct ypalias {
     51 	char *alias, *name;
     52 } ypaliases[] = {
     53 	{ "passwd", "passwd.byname" },
     54 	{ "group", "group.byname" },
     55 	{ "networks", "networks.byaddr" },
     56 	{ "hosts", "hosts.byaddr" },
     57 	{ "protocols", "protocols.bynumber" },
     58 	{ "services", "services.byname" },
     59 	{ "aliases", "mail.aliases" },
     60 	{ "ethers", "ethers.byname" },
     61 };
     62 
     63 int key;
     64 
     65 usage()
     66 {
     67 	fprintf(stderr, "Usage:\n");
     68 	fprintf(stderr, "\typcat [-k] [-d domainname] [-t] mapname\n");
     69 	fprintf(stderr, "\typcat -x\n");
     70 	exit(1);
     71 }
     72 
     73 printit(instatus, inkey, inkeylen, inval, invallen, indata)
     74 int instatus;
     75 char *inkey;
     76 int inkeylen;
     77 char *inval;
     78 int invallen;
     79 char *indata;
     80 {
     81 	if(instatus != YP_TRUE)
     82 		return instatus;
     83 	if(key)
     84 		printf("%*.*s ", inkeylen, inkeylen, inkey);
     85 	printf("%*.*s\n", invallen, invallen, inval);
     86 	return 0;
     87 }
     88 
     89 int
     90 main(argc, argv)
     91 char **argv;
     92 {
     93 	char *domainname;
     94 	struct ypall_callback ypcb;
     95 	char *inmap;
     96 	extern char *optarg;
     97 	extern int optind;
     98 	int notrans;
     99 	int c, r, i;
    100 
    101 	domainname = NULL;
    102 	notrans = key = 0;
    103 	while( (c=getopt(argc, argv, "xd:kt")) != -1)
    104 		switch(c) {
    105 		case 'x':
    106 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
    107 				printf("Use \"%s\" for \"%s\"\n",
    108 					ypaliases[i].alias,
    109 					ypaliases[i].name);
    110 			exit(0);
    111 		case 'd':
    112 			domainname = optarg;
    113 			break;
    114 		case 't':
    115 			notrans++;
    116 			break;
    117 		case 'k':
    118 			key++;
    119 			break;
    120 		default:
    121 			usage();
    122 		}
    123 
    124 	if(optind + 1 != argc )
    125 		usage();
    126 
    127 	if (!domainname) {
    128 		yp_get_default_domain(&domainname);
    129 	}
    130 
    131 	inmap = argv[optind];
    132 	if (!notrans) {
    133 		for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
    134 			if( strcmp(inmap, ypaliases[i].alias) == 0)
    135 				inmap = ypaliases[i].name;
    136 	}
    137 	ypcb.foreach = printit;
    138 	ypcb.data = NULL;
    139 
    140 	r = yp_all(domainname, inmap, &ypcb);
    141 	switch(r) {
    142 	case 0:
    143 		break;
    144 	case YPERR_YPBIND:
    145 		fprintf(stderr, "ypcat: not running ypbind\n");
    146 		exit(1);
    147 	default:
    148 		fprintf(stderr, "No such map %s. Reason: %s\n",
    149 			inmap, yperr_string(r));
    150 		exit(1);
    151 	}
    152 	exit(0);
    153 }
    154