xref: /netbsd-src/usr.sbin/yppoll/yppoll.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@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 /*
30  * Copyright (c) 1992, 1993 John Brezak
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions
35  * are met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce the above copyright
39  *    notice, this list of conditions and the following disclaimer in the
40  *    documentation and/or other materials provided with the distribution.
41  * 3. The name of the author may not be used to endorse or promote
42  *    products derived from this software without specific prior written
43  *    permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
46  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
49  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 #ifndef lint
60 __RCSID("$NetBSD: yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp $");
61 #endif /* not lint */
62 
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <err.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <time.h>
70 #include <netdb.h>
71 #include <unistd.h>
72 #include <string.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
75 
76 #include <rpc/rpc.h>
77 #include <rpc/xdr.h>
78 #include <rpcsvc/yp_prot.h>
79 #include <rpcsvc/ypclnt.h>
80 
81 int	main __P((int, char *[]));
82 int	get_remote_info __P((char *, char *, char *, int *, char **));
83 void	usage __P((void));
84 
85 int
86 main(argc, argv)
87 	int  argc;
88 	char **argv;
89 {
90 	char *domainname;
91 	char *hostname = NULL;
92 	char *inmap, *master;
93 	int order;
94 	int c, r;
95 
96 	yp_get_default_domain(&domainname);
97 
98 	while ((c = getopt(argc, argv, "h:d:")) != -1) {
99 		switch (c) {
100 		case 'd':
101 			domainname = optarg;
102 			break;
103 
104 		case 'h':
105 			hostname = optarg;
106 			break;
107 
108 		default:
109 			usage();
110 			/*NOTREACHED*/
111 		}
112 	}
113 
114 	if (domainname == NULL)
115 		errx(1, "YP domain name not set");
116 
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc != 1)
121 		usage();
122 
123 	inmap = argv[0];
124 
125 	if (hostname != NULL)
126 		r = get_remote_info(domainname, inmap, hostname,
127 		    &order, &master);
128 	else {
129 		r = yp_order(domainname, inmap, &order);
130 		if (r == 0)
131 			r = yp_master(domainname, inmap, &master);
132 	}
133 
134 	if (r != 0)
135 		errx(1, "no such map %s. Reason: %s",
136 		    inmap, yperr_string(r));
137 
138 	printf("Map %s has order number %d. %s", inmap, order,
139 	    ctime((void *)&order));
140 	printf("The master server is %s.\n", master);
141 	exit(0);
142 }
143 
144 int
145 get_remote_info(indomain, inmap, server, outorder, outname)
146 	char *indomain;
147 	char *inmap;
148 	char *server;
149 	int *outorder;
150 	char **outname;
151 {
152 	struct ypresp_order ypro;
153 	struct ypresp_master yprm;
154 	struct ypreq_nokey yprnk;
155 	struct timeval tv;
156 	int r;
157 	struct sockaddr_in rsrv_sin;
158 	int rsrv_sock;
159 	CLIENT *client;
160 	struct hostent *h;
161 
162 	memset(&rsrv_sin, 0, sizeof(rsrv_sin));
163 	rsrv_sin.sin_len = sizeof rsrv_sin;
164 	rsrv_sin.sin_family = AF_INET;
165 	rsrv_sock = RPC_ANYSOCK;
166 
167 	h = gethostbyname(server);
168 	if (h == NULL) {
169 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
170 			errx(1, "unknown host %s", server);
171 	} else
172 		memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr, h->h_length);
173 
174 	tv.tv_sec = 10;
175 	tv.tv_usec = 0;
176 
177 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
178 	if (client == NULL)
179 		errx(1, "clntudp_create: no contact with host %s.", server);
180 
181 	yprnk.domain = indomain;
182 	yprnk.map = inmap;
183 
184 	memset(&ypro, 0, sizeof(ypro));
185 
186 	r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
187 	    xdr_ypresp_order, &ypro, tv);
188 	if (r != RPC_SUCCESS)
189 		clnt_perror(client, "yp_order: clnt_call");
190 
191 	*outorder = ypro.ordernum;
192 	xdr_free(xdr_ypresp_order, (char *)&ypro);
193 
194 	r = ypprot_err(ypro.status);
195 	if (r == RPC_SUCCESS) {
196 		memset(&yprm, 0, sizeof(yprm));
197 
198 		r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
199 		    &yprnk, xdr_ypresp_master, &yprm, tv);
200 		if (r != RPC_SUCCESS)
201 			clnt_perror(client, "yp_master: clnt_call");
202 		r = ypprot_err(yprm.status);
203 		if (r == 0)
204 			*outname = (char *)strdup(yprm.master);
205 		xdr_free(xdr_ypresp_master, (char *)&yprm);
206 	}
207 	clnt_destroy(client);
208 	return r;
209 }
210 
211 void
212 usage()
213 {
214 
215 	fprintf(stderr, "usage: %s [-h host] [-d domainname] mapname\n",
216 	    getprogname());
217 	exit(1);
218 }
219