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