1b2845e83SBill Paul /*
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
4b2845e83SBill Paul * Copyright (c) 1996, 1997
5b2845e83SBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6b2845e83SBill Paul *
7b2845e83SBill Paul * Redistribution and use in source and binary forms, with or without
8b2845e83SBill Paul * modification, are permitted provided that the following conditions
9b2845e83SBill Paul * are met:
10b2845e83SBill Paul * 1. Redistributions of source code must retain the above copyright
11b2845e83SBill Paul * notice, this list of conditions and the following disclaimer.
12b2845e83SBill Paul * 2. Redistributions in binary form must reproduce the above copyright
13b2845e83SBill Paul * notice, this list of conditions and the following disclaimer in the
14b2845e83SBill Paul * documentation and/or other materials provided with the distribution.
15b2845e83SBill Paul * 3. All advertising materials mentioning features or use of this software
16b2845e83SBill Paul * must display the following acknowledgement:
17b2845e83SBill Paul * This product includes software developed by Bill Paul.
18b2845e83SBill Paul * 4. Neither the name of the author nor the names of any co-contributors
19b2845e83SBill Paul * may be used to endorse or promote products derived from this software
20b2845e83SBill Paul * without specific prior written permission.
21b2845e83SBill Paul *
22b2845e83SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23b2845e83SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b2845e83SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b2845e83SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26b2845e83SBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27b2845e83SBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28b2845e83SBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29b2845e83SBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30b2845e83SBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31b2845e83SBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32b2845e83SBill Paul * SUCH DAMAGE.
33b2845e83SBill Paul */
34b2845e83SBill Paul
356a67774fSHiroki Sato /*-
366a67774fSHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
376a67774fSHiroki Sato * All rights reserved.
38b2845e83SBill Paul *
396a67774fSHiroki Sato * Redistribution and use in source and binary forms, with or without
406a67774fSHiroki Sato * modification, are permitted provided that the following conditions are met:
416a67774fSHiroki Sato * - Redistributions of source code must retain the above copyright notice,
426a67774fSHiroki Sato * this list of conditions and the following disclaimer.
436a67774fSHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
446a67774fSHiroki Sato * this list of conditions and the following disclaimer in the documentation
456a67774fSHiroki Sato * and/or other materials provided with the distribution.
466a67774fSHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
476a67774fSHiroki Sato * contributors may be used to endorse or promote products derived
486a67774fSHiroki Sato * from this software without specific prior written permission.
49b2845e83SBill Paul *
506a67774fSHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
516a67774fSHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
526a67774fSHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
536a67774fSHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
546a67774fSHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
556a67774fSHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
566a67774fSHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
576a67774fSHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
586a67774fSHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
596a67774fSHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
606a67774fSHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
61b2845e83SBill Paul */
62b2845e83SBill Paul /*
63b2845e83SBill Paul * clnt_udp.c, Implements a UDP/IP based, client side RPC.
64b2845e83SBill Paul *
65b2845e83SBill Paul * Copyright (C) 1984, Sun Microsystems, Inc.
66b2845e83SBill Paul */
67b2845e83SBill Paul
6877cbf0b3SPhilippe Charnier #include <errno.h>
6977cbf0b3SPhilippe Charnier #include <netdb.h>
70b2845e83SBill Paul #include <stdio.h>
71b2845e83SBill Paul #include <stdlib.h>
72b2845e83SBill Paul #include <string.h>
7377cbf0b3SPhilippe Charnier #include <unistd.h>
748de34b25SBill Paul #include <pthread.h>
75b2845e83SBill Paul #include <rpc/rpc.h>
76b2845e83SBill Paul #include <rpc/pmap_clnt.h>
77b2845e83SBill Paul #include <rpc/pmap_prot.h>
78b2845e83SBill Paul #include <rpcsvc/yp.h>
798de34b25SBill Paul #include <sys/types.h>
808de34b25SBill Paul #include <sys/poll.h>
8177cbf0b3SPhilippe Charnier #include <sys/socket.h>
828de34b25SBill Paul #include <sys/signal.h>
8377cbf0b3SPhilippe Charnier #include <sys/ioctl.h>
84fd8e4ebcSMike Barcroft #include <arpa/inet.h>
8577cbf0b3SPhilippe Charnier #include <net/if.h>
868de34b25SBill Paul
87b2845e83SBill Paul #include "yp_ping.h"
88b2845e83SBill Paul
89b2845e83SBill Paul /*
90b2845e83SBill Paul * pmap_getport.c
91b2845e83SBill Paul * Client interface to pmap rpc service.
92b2845e83SBill Paul *
93b2845e83SBill Paul * Copyright (C) 1984, Sun Microsystems, Inc.
94b2845e83SBill Paul */
95b2845e83SBill Paul
96b2845e83SBill Paul
97b2845e83SBill Paul static struct timeval timeout = { 1, 0 };
98b2845e83SBill Paul static struct timeval tottimeout = { 1, 0 };
99b2845e83SBill Paul
100b2845e83SBill Paul /*
101b2845e83SBill Paul * Find the mapped port for program,version.
102b2845e83SBill Paul * Calls the pmap service remotely to do the lookup.
103b2845e83SBill Paul * Returns 0 if no map exists.
104b2845e83SBill Paul */
105b2845e83SBill Paul static u_short
__pmap_getport(struct sockaddr_in * address,u_long program,u_long version,u_int protocol)106dc584ddbSDag-Erling Smørgrav __pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
107dc584ddbSDag-Erling Smørgrav u_int protocol)
108b2845e83SBill Paul {
109b2845e83SBill Paul u_short port = 0;
110b2845e83SBill Paul int sock = -1;
111b2845e83SBill Paul register CLIENT *client;
112b2845e83SBill Paul struct pmap parms;
113b2845e83SBill Paul
114b2845e83SBill Paul address->sin_port = htons(PMAPPORT);
115b2845e83SBill Paul
116b2845e83SBill Paul client = clntudp_bufcreate(address, PMAPPROG,
117b2845e83SBill Paul PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
118b2845e83SBill Paul if (client != (CLIENT *)NULL) {
119b2845e83SBill Paul parms.pm_prog = program;
120b2845e83SBill Paul parms.pm_vers = version;
121b2845e83SBill Paul parms.pm_prot = protocol;
122b2845e83SBill Paul parms.pm_port = 0; /* not needed or used */
123f249dbccSDag-Erling Smørgrav if (CLNT_CALL(client, PMAPPROC_GETPORT,
124f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_pmap, &parms,
125f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_u_short, &port,
126f249dbccSDag-Erling Smørgrav tottimeout) != RPC_SUCCESS){
127b2845e83SBill Paul rpc_createerr.cf_stat = RPC_PMAPFAILURE;
128b2845e83SBill Paul clnt_geterr(client, &rpc_createerr.cf_error);
129b2845e83SBill Paul } else if (port == 0) {
130b2845e83SBill Paul rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
131b2845e83SBill Paul }
132b2845e83SBill Paul CLNT_DESTROY(client);
133b2845e83SBill Paul }
134b2845e83SBill Paul if (sock != -1)
135b2845e83SBill Paul (void)close(sock);
136b2845e83SBill Paul address->sin_port = 0;
137b2845e83SBill Paul return (port);
138b2845e83SBill Paul }
139b2845e83SBill Paul
140b2845e83SBill Paul /*
141b2845e83SBill Paul * Transmit to YPPROC_DOMAIN_NONACK, return immediately.
142b2845e83SBill Paul */
143b2845e83SBill Paul static bool_t *
ypproc_domain_nonack_2_send(domainname * argp,CLIENT * clnt)144b2845e83SBill Paul ypproc_domain_nonack_2_send(domainname *argp, CLIENT *clnt)
145b2845e83SBill Paul {
146b2845e83SBill Paul static bool_t clnt_res;
147b2845e83SBill Paul struct timeval TIMEOUT = { 0, 0 };
148b2845e83SBill Paul
149b2845e83SBill Paul memset((char *)&clnt_res, 0, sizeof (clnt_res));
150b2845e83SBill Paul if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
151b2845e83SBill Paul (xdrproc_t) xdr_domainname, (caddr_t) argp,
152b2845e83SBill Paul (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
153b2845e83SBill Paul TIMEOUT) != RPC_SUCCESS) {
154b2845e83SBill Paul return (NULL);
155b2845e83SBill Paul }
156b2845e83SBill Paul return (&clnt_res);
157b2845e83SBill Paul }
158b2845e83SBill Paul
159b2845e83SBill Paul /*
160b2845e83SBill Paul * Receive response from YPPROC_DOMAIN_NONACK asynchronously.
161b2845e83SBill Paul */
162b2845e83SBill Paul static bool_t *
ypproc_domain_nonack_2_recv(domainname * argp,CLIENT * clnt)163b2845e83SBill Paul ypproc_domain_nonack_2_recv(domainname *argp, CLIENT *clnt)
164b2845e83SBill Paul {
165b2845e83SBill Paul static bool_t clnt_res;
166b2845e83SBill Paul struct timeval TIMEOUT = { 0, 0 };
167b2845e83SBill Paul
168b2845e83SBill Paul memset((char *)&clnt_res, 0, sizeof (clnt_res));
169b2845e83SBill Paul if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
170b2845e83SBill Paul (xdrproc_t) NULL, (caddr_t) argp,
171b2845e83SBill Paul (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
172b2845e83SBill Paul TIMEOUT) != RPC_SUCCESS) {
173b2845e83SBill Paul return (NULL);
174b2845e83SBill Paul }
175b2845e83SBill Paul return (&clnt_res);
176b2845e83SBill Paul }
177b2845e83SBill Paul
178b2845e83SBill Paul /*
179b2845e83SBill Paul * "We have the machine that goes 'ping!'" -- Monty Python
180b2845e83SBill Paul *
181b2845e83SBill Paul * This function blasts packets at the YPPROC_DOMAIN_NONACK procedures
182b2845e83SBill Paul * of the NIS servers listed in restricted_addrs structure.
183b2845e83SBill Paul * Whoever replies the fastest becomes our chosen server.
184b2845e83SBill Paul *
185b2845e83SBill Paul * Note: THIS IS NOT A BROADCAST OPERATION! We could use clnt_broadcast()
186b2845e83SBill Paul * for this, but that has the following problems:
187b2845e83SBill Paul * - We only get the address of the machine that replied in the
188b2845e83SBill Paul * 'eachresult' callback, and on multi-homed machines this can
189b2845e83SBill Paul * lead to confusion.
190b2845e83SBill Paul * - clnt_broadcast() only transmits to local networks, whereas with
191b2845e83SBill Paul * NIS+ you can have a perfectly good server located anywhere on or
192b2845e83SBill Paul * off the local network.
193b2845e83SBill Paul * - clnt_broadcast() blocks for an arbitrary amount of time which the
194b2845e83SBill Paul * caller can't control -- we want to avoid that.
195b2845e83SBill Paul *
196b2845e83SBill Paul * Also note that this has nothing to do with the NIS_PING procedure used
197b2845e83SBill Paul * for replica updates.
198b2845e83SBill Paul */
199b2845e83SBill Paul
200b2845e83SBill Paul struct ping_req {
201b2845e83SBill Paul struct sockaddr_in sin;
202bfe95cccSBill Fenner u_int32_t xid;
203b2845e83SBill Paul };
204b2845e83SBill Paul
205dc584ddbSDag-Erling Smørgrav int
__yp_ping(struct in_addr * restricted_addrs,int cnt,char * dom,short * port)206dc584ddbSDag-Erling Smørgrav __yp_ping(struct in_addr *restricted_addrs, int cnt, char *dom, short *port)
207b2845e83SBill Paul {
208b2845e83SBill Paul struct timeval tv = { 5, 0 };
209b2845e83SBill Paul struct ping_req **reqs;
210b2845e83SBill Paul unsigned long i;
2118de34b25SBill Paul int async;
212844812c4SBill Paul struct sockaddr_in sin, *any = NULL;
2130be84ce8SDoug Rabson struct netbuf addr;
214b2845e83SBill Paul int winner = -1;
215bfe95cccSBill Fenner u_int32_t xid_seed, xid_lookup;
216b2845e83SBill Paul int sock, dontblock = 1;
217b2845e83SBill Paul CLIENT *clnt;
218b2845e83SBill Paul char *foo = dom;
219bfb109dfSBill Paul int validsrvs = 0;
220b2845e83SBill Paul
221b2845e83SBill Paul /* Set up handles. */
22285f10495SPedro F. Giffuni reqs = calloc(cnt, sizeof(struct ping_req *));
223b2845e83SBill Paul xid_seed = time(NULL) ^ getpid();
224b2845e83SBill Paul
225b2845e83SBill Paul for (i = 0; i < cnt; i++) {
226b2845e83SBill Paul bzero((char *)&sin, sizeof(sin));
227b2845e83SBill Paul sin.sin_family = AF_INET;
228b2845e83SBill Paul bcopy((char *)&restricted_addrs[i],
229b2845e83SBill Paul (char *)&sin.sin_addr, sizeof(struct in_addr));
230b2845e83SBill Paul sin.sin_port = htons(__pmap_getport(&sin, YPPROG,
231b2845e83SBill Paul YPVERS, IPPROTO_UDP));
232b2845e83SBill Paul if (sin.sin_port == 0)
233b2845e83SBill Paul continue;
234b2845e83SBill Paul reqs[i] = calloc(1, sizeof(struct ping_req));
235b2845e83SBill Paul bcopy((char *)&sin, (char *)&reqs[i]->sin, sizeof(sin));
236b2845e83SBill Paul any = &reqs[i]->sin;
237b2845e83SBill Paul reqs[i]->xid = xid_seed;
238b2845e83SBill Paul xid_seed++;
239bfb109dfSBill Paul validsrvs++;
240b2845e83SBill Paul }
241b2845e83SBill Paul
242b2845e83SBill Paul /* Make sure at least one server was assigned */
243bfb109dfSBill Paul if (!validsrvs) {
244b2845e83SBill Paul free(reqs);
245b2845e83SBill Paul return(-1);
246b2845e83SBill Paul }
247b2845e83SBill Paul
248b2845e83SBill Paul /* Create RPC handle */
249b2845e83SBill Paul sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
250b2845e83SBill Paul clnt = clntudp_create(any, YPPROG, YPVERS, tv, &sock);
251b2845e83SBill Paul if (clnt == NULL) {
252b2845e83SBill Paul close(sock);
253b2845e83SBill Paul for (i = 0; i < cnt; i++)
254b2845e83SBill Paul if (reqs[i] != NULL)
255b2845e83SBill Paul free(reqs[i]);
256b2845e83SBill Paul free(reqs);
257b2845e83SBill Paul return(-1);
258b2845e83SBill Paul }
259b2845e83SBill Paul clnt->cl_auth = authunix_create_default();
260b2845e83SBill Paul tv.tv_sec = 0;
2618de34b25SBill Paul
2628360efbdSAlfred Perlstein clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
2638de34b25SBill Paul async = TRUE;
2648de34b25SBill Paul clnt_control(clnt, CLSET_ASYNC, (char *)&async);
265b2845e83SBill Paul ioctl(sock, FIONBIO, &dontblock);
266b2845e83SBill Paul
267b2845e83SBill Paul /* Transmit */
268b2845e83SBill Paul for (i = 0; i < cnt; i++) {
269b2845e83SBill Paul if (reqs[i] != NULL) {
2708de34b25SBill Paul clnt_control(clnt, CLSET_XID, (char *)&reqs[i]->xid);
2710be84ce8SDoug Rabson addr.len = sizeof(reqs[i]->sin);
2720be84ce8SDoug Rabson addr.buf = (char *) &reqs[i]->sin;
2730be84ce8SDoug Rabson clnt_control(clnt, CLSET_SVC_ADDR, &addr);
274b2845e83SBill Paul ypproc_domain_nonack_2_send(&foo, clnt);
275b2845e83SBill Paul }
276b2845e83SBill Paul }
277b2845e83SBill Paul
278b2845e83SBill Paul /* Receive reply */
279b2845e83SBill Paul ypproc_domain_nonack_2_recv(&foo, clnt);
280b2845e83SBill Paul
281b2845e83SBill Paul /* Got a winner -- look him up. */
2828de34b25SBill Paul clnt_control(clnt, CLGET_XID, (char *)&xid_lookup);
283b2845e83SBill Paul for (i = 0; i < cnt; i++) {
284b2845e83SBill Paul if (reqs[i] != NULL && reqs[i]->xid == xid_lookup) {
285b2845e83SBill Paul winner = i;
286b2845e83SBill Paul *port = reqs[i]->sin.sin_port;
287b2845e83SBill Paul }
288b2845e83SBill Paul }
289b2845e83SBill Paul
290b2845e83SBill Paul /* Shut everything down */
291b2845e83SBill Paul auth_destroy(clnt->cl_auth);
292b2845e83SBill Paul clnt_destroy(clnt);
293b2845e83SBill Paul close(sock);
294b2845e83SBill Paul
295b2845e83SBill Paul for (i = 0; i < cnt; i++)
296b2845e83SBill Paul if (reqs[i] != NULL)
297b2845e83SBill Paul free(reqs[i]);
298b2845e83SBill Paul free(reqs);
299b2845e83SBill Paul
300b2845e83SBill Paul return(winner);
301b2845e83SBill Paul }
302