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