xref: /dragonfly/usr.bin/ypcat/ypcat.c (revision 984263bc)
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 const char rcsid[] =
32   "$FreeBSD: src/usr.bin/ypcat/ypcat.c,v 1.4.2.2 2002/02/15 00:46:56 des Exp $";
33 #endif /* not lint */
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.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 	{ "master.passwd", "master.passwd.byname" },
55 	{ "group", "group.byname" },
56 	{ "networks", "networks.byaddr" },
57 	{ "hosts", "hosts.byaddr" },
58 	{ "protocols", "protocols.bynumber" },
59 	{ "services", "services.byname" },
60 	{ "aliases", "mail.aliases" },
61 	{ "ethers", "ethers.byname" },
62 };
63 
64 int key;
65 
66 static void
67 usage()
68 {
69 	fprintf(stderr, "%s\n%s\n",
70 		"usage: ypcat [-k] [-d domainname] [-t] mapname",
71 		"       ypcat -x");
72 	exit(1);
73 }
74 
75 int
76 printit(instatus, inkey, inkeylen, inval, invallen, indata)
77 int instatus;
78 char *inkey;
79 int inkeylen;
80 char *inval;
81 int invallen;
82 char *indata;
83 {
84 	if (instatus != YP_TRUE)
85 		return (instatus);
86 	if (key)
87 		printf("%*.*s ", inkeylen, inkeylen, inkey);
88 	printf("%*.*s\n", invallen, invallen, inval);
89 	return (0);
90 }
91 
92 int
93 main(argc, argv)
94 char **argv;
95 {
96 	char *domainname = NULL;
97 	struct ypall_callback ypcb;
98 	char *inmap;
99 	int notrans;
100 	int c, r, i;
101 
102 	notrans = key = 0;
103 
104 	while ((c = getopt(argc, argv, "xd:kt")) != -1)
105 		switch (c) {
106 		case 'x':
107 			for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
108 				printf("Use \"%s\" for \"%s\"\n",
109 					ypaliases[i].alias,
110 					ypaliases[i].name);
111 			exit(0);
112 		case 'd':
113 			domainname = optarg;
114 			break;
115 		case 't':
116 			notrans++;
117 			break;
118 		case 'k':
119 			key++;
120 			break;
121 		default:
122 			usage();
123 		}
124 
125 	if (optind + 1 != argc)
126 		usage();
127 
128 	if (!domainname)
129 		yp_get_default_domain(&domainname);
130 
131 	inmap = argv[optind];
132 	for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
133 		if (strcmp(inmap, ypaliases[i].alias) == 0)
134 			inmap = ypaliases[i].name;
135 	ypcb.foreach = printit;
136 	ypcb.data = NULL;
137 
138 	r = yp_all(domainname, inmap, &ypcb);
139 	switch (r) {
140 	case 0:
141 		break;
142 	case YPERR_YPBIND:
143 		errx(1, "not running ypbind");
144 	default:
145 		errx(1, "no such map %s. reason: %s", inmap, yperr_string(r));
146 	}
147 	exit(0);
148 }
149