ypbind.c revision 1.36 1 1.36 mrg /* $NetBSD: ypbind.c,v 1.36 1998/07/13 12:01:03 mrg Exp $ */
2 1.20 cgd
3 1.2 deraadt /*
4 1.7 deraadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
5 1.2 deraadt * All rights reserved.
6 1.2 deraadt *
7 1.2 deraadt * Redistribution and use in source and binary forms, with or without
8 1.2 deraadt * modification, are permitted provided that the following conditions
9 1.2 deraadt * are met:
10 1.2 deraadt * 1. Redistributions of source code must retain the above copyright
11 1.2 deraadt * notice, this list of conditions and the following disclaimer.
12 1.2 deraadt * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 deraadt * notice, this list of conditions and the following disclaimer in the
14 1.2 deraadt * documentation and/or other materials provided with the distribution.
15 1.7 deraadt * 3. All advertising materials mentioning features or use of this software
16 1.7 deraadt * must display the following acknowledgement:
17 1.7 deraadt * This product includes software developed by Theo de Raadt.
18 1.7 deraadt * 4. The name of the author may not be used to endorse or promote
19 1.2 deraadt * products derived from this software without specific prior written
20 1.2 deraadt * permission.
21 1.2 deraadt *
22 1.2 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23 1.2 deraadt * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 1.2 deraadt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 1.2 deraadt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2 deraadt * SUCH DAMAGE.
33 1.2 deraadt */
34 1.2 deraadt
35 1.30 lukem #include <sys/cdefs.h>
36 1.2 deraadt #ifndef LINT
37 1.36 mrg __RCSID("$NetBSD: ypbind.c,v 1.36 1998/07/13 12:01:03 mrg Exp $");
38 1.2 deraadt #endif
39 1.2 deraadt
40 1.1 deraadt #include <sys/param.h>
41 1.1 deraadt #include <sys/types.h>
42 1.1 deraadt #include <sys/ioctl.h>
43 1.1 deraadt #include <sys/signal.h>
44 1.1 deraadt #include <sys/socket.h>
45 1.1 deraadt #include <sys/file.h>
46 1.1 deraadt #include <sys/fcntl.h>
47 1.6 deraadt #include <sys/uio.h>
48 1.1 deraadt #include <sys/syslog.h>
49 1.24 christos #include <sys/stat.h>
50 1.27 thorpej #include <limits.h>
51 1.1 deraadt #include <stdio.h>
52 1.14 cgd #include <stdlib.h>
53 1.1 deraadt #include <errno.h>
54 1.1 deraadt #include <ctype.h>
55 1.1 deraadt #include <dirent.h>
56 1.1 deraadt #include <netdb.h>
57 1.1 deraadt #include <string.h>
58 1.24 christos #include <err.h>
59 1.1 deraadt #include <rpc/rpc.h>
60 1.1 deraadt #include <rpc/xdr.h>
61 1.1 deraadt #include <net/if.h>
62 1.1 deraadt #include <arpa/inet.h>
63 1.1 deraadt #include <rpc/pmap_clnt.h>
64 1.1 deraadt #include <rpc/pmap_prot.h>
65 1.1 deraadt #include <rpc/pmap_rmt.h>
66 1.1 deraadt #include <unistd.h>
67 1.1 deraadt #include <rpcsvc/yp_prot.h>
68 1.1 deraadt #include <rpcsvc/ypclnt.h>
69 1.1 deraadt
70 1.27 thorpej #include "pathnames.h"
71 1.27 thorpej
72 1.24 christos #ifndef O_SHLOCK
73 1.24 christos #define O_SHLOCK 0
74 1.24 christos #endif
75 1.24 christos
76 1.24 christos #define BUFSIZE 1400
77 1.27 thorpej
78 1.29 thorpej #define YPSERVERSSUFF ".ypservers"
79 1.27 thorpej #define BINDINGDIR __CONCAT(_PATH_VAR_YP, "binding")
80 1.1 deraadt
81 1.1 deraadt struct _dom_binding {
82 1.1 deraadt struct _dom_binding *dom_pnext;
83 1.1 deraadt char dom_domain[YPMAXDOMAIN + 1];
84 1.1 deraadt struct sockaddr_in dom_server_addr;
85 1.35 lukem unsigned short int dom_server_port;
86 1.1 deraadt int dom_socket;
87 1.1 deraadt CLIENT *dom_client;
88 1.35 lukem long dom_vers;
89 1.1 deraadt time_t dom_check_t;
90 1.9 deraadt time_t dom_ask_t;
91 1.1 deraadt int dom_lockfd;
92 1.1 deraadt int dom_alive;
93 1.30 lukem u_int32_t dom_xid;
94 1.1 deraadt };
95 1.1 deraadt
96 1.24 christos static char *domainname;
97 1.1 deraadt
98 1.24 christos static struct _dom_binding *ypbindlist;
99 1.24 christos static int check;
100 1.1 deraadt
101 1.27 thorpej typedef enum {
102 1.27 thorpej YPBIND_DIRECT, YPBIND_BROADCAST, YPBIND_SETLOCAL, YPBIND_SETALL
103 1.27 thorpej } ypbind_mode_t;
104 1.27 thorpej
105 1.27 thorpej ypbind_mode_t ypbindmode;
106 1.27 thorpej
107 1.27 thorpej /*
108 1.27 thorpej * If ypbindmode is YPBIND_SETLOCAL or YPBIND_SETALL, this indicates
109 1.27 thorpej * whether or not we've been "ypset". If we haven't, we behave like
110 1.27 thorpej * YPBIND_BROADCAST. If we have, we behave like YPBIND_DIRECT.
111 1.27 thorpej */
112 1.27 thorpej int been_ypset;
113 1.27 thorpej
114 1.24 christos #ifdef DEBUG
115 1.24 christos static int debug;
116 1.24 christos #endif
117 1.24 christos
118 1.30 lukem static int insecure;
119 1.24 christos static int rpcsock, pingsock;
120 1.24 christos static struct rmtcallargs rmtca;
121 1.24 christos static struct rmtcallres rmtcr;
122 1.26 ws static bool_t rmtcr_outval;
123 1.35 lukem static u_long rmtcr_port;
124 1.24 christos static SVCXPRT *udptransp, *tcptransp;
125 1.24 christos
126 1.30 lukem int _yp_invalid_domain __P((const char *)); /* from libc */
127 1.31 thorpej int main __P((int, char *[]));
128 1.24 christos
129 1.24 christos static void usage __P((void));
130 1.24 christos static struct _dom_binding *makebinding __P((const char *));
131 1.24 christos static int makelock __P((struct _dom_binding *));
132 1.24 christos static void removelock __P((struct _dom_binding *));
133 1.24 christos static void *ypbindproc_null_2 __P((SVCXPRT *, void *));
134 1.24 christos static void *ypbindproc_domain_2 __P((SVCXPRT *, void *));
135 1.24 christos static void *ypbindproc_setdom_2 __P((SVCXPRT *, void *));
136 1.24 christos static void ypbindprog_2 __P((struct svc_req *, SVCXPRT *));
137 1.24 christos static void checkwork __P((void));
138 1.24 christos static int ping __P((struct _dom_binding *));
139 1.27 thorpej static int nag_servers __P((struct _dom_binding *));
140 1.24 christos static enum clnt_stat handle_replies __P((void));
141 1.24 christos static enum clnt_stat handle_ping __P((void));
142 1.24 christos static void rpc_received __P((char *, struct sockaddr_in *, int));
143 1.30 lukem static struct _dom_binding *xid2ypdb __P((u_int32_t));
144 1.30 lukem static u_int32_t unique_xid __P((struct _dom_binding *));
145 1.27 thorpej static int broadcast __P((char *, int));
146 1.27 thorpej static int direct __P((char *, int));
147 1.27 thorpej static int direct_set __P((char *, int, struct _dom_binding *));
148 1.24 christos
149 1.24 christos static void
150 1.24 christos usage()
151 1.24 christos {
152 1.24 christos extern char *__progname;
153 1.24 christos char *opt = "";
154 1.24 christos #ifdef DEBUG
155 1.24 christos opt = " [-d]";
156 1.24 christos #endif
157 1.36 mrg
158 1.36 mrg (void)fprintf(stderr,
159 1.30 lukem "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme] %s\n",
160 1.24 christos __progname, opt);
161 1.24 christos exit(1);
162 1.24 christos }
163 1.24 christos
164 1.24 christos static struct _dom_binding *
165 1.24 christos makebinding(dm)
166 1.24 christos const char *dm;
167 1.24 christos {
168 1.24 christos struct _dom_binding *ypdb;
169 1.1 deraadt
170 1.24 christos if ((ypdb = (struct _dom_binding *)malloc(sizeof *ypdb)) == NULL)
171 1.24 christos err(1, "makebinding");
172 1.1 deraadt
173 1.36 mrg (void)memset(ypdb, 0, sizeof *ypdb);
174 1.36 mrg (void)strncpy(ypdb->dom_domain, dm, sizeof ypdb->dom_domain);
175 1.24 christos ypdb->dom_domain[sizeof(ypdb->dom_domain) - 1] = '\0';
176 1.24 christos return ypdb;
177 1.24 christos }
178 1.24 christos
179 1.24 christos static int
180 1.24 christos makelock(ypdb)
181 1.24 christos struct _dom_binding *ypdb;
182 1.24 christos {
183 1.24 christos int fd;
184 1.24 christos char path[MAXPATHLEN];
185 1.20 cgd
186 1.36 mrg (void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
187 1.24 christos ypdb->dom_domain, ypdb->dom_vers);
188 1.24 christos
189 1.24 christos if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
190 1.36 mrg (void)mkdir(BINDINGDIR, 0755);
191 1.24 christos if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
192 1.24 christos return -1;
193 1.24 christos }
194 1.24 christos
195 1.24 christos #if O_SHLOCK == 0
196 1.36 mrg (void)flock(fd, LOCK_SH);
197 1.24 christos #endif
198 1.24 christos return fd;
199 1.24 christos }
200 1.24 christos
201 1.24 christos static void
202 1.24 christos removelock(ypdb)
203 1.24 christos struct _dom_binding *ypdb;
204 1.24 christos {
205 1.24 christos char path[MAXPATHLEN];
206 1.24 christos
207 1.36 mrg (void)snprintf(path, sizeof(path), "%s/%s.%ld",
208 1.24 christos BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
209 1.36 mrg (void)unlink(path);
210 1.24 christos }
211 1.24 christos
212 1.24 christos static void *
213 1.24 christos ypbindproc_null_2(transp, argp)
214 1.17 mycroft SVCXPRT *transp;
215 1.17 mycroft void *argp;
216 1.1 deraadt {
217 1.1 deraadt static char res;
218 1.1 deraadt
219 1.24 christos #ifdef DEBUG
220 1.24 christos if (debug)
221 1.24 christos printf("ypbindproc_null_2\n");
222 1.24 christos #endif
223 1.36 mrg (void)memset(&res, 0, sizeof(res));
224 1.1 deraadt return (void *)&res;
225 1.1 deraadt }
226 1.1 deraadt
227 1.24 christos static void *
228 1.24 christos ypbindproc_domain_2(transp, argp)
229 1.17 mycroft SVCXPRT *transp;
230 1.24 christos void *argp;
231 1.1 deraadt {
232 1.1 deraadt static struct ypbind_resp res;
233 1.1 deraadt struct _dom_binding *ypdb;
234 1.24 christos char *arg = *(char **) argp;
235 1.9 deraadt time_t now;
236 1.30 lukem int count;
237 1.1 deraadt
238 1.24 christos #ifdef DEBUG
239 1.24 christos if (debug)
240 1.24 christos printf("ypbindproc_domain_2 %s\n", arg);
241 1.24 christos #endif
242 1.30 lukem if (_yp_invalid_domain(arg))
243 1.30 lukem return NULL;
244 1.30 lukem
245 1.36 mrg (void)memset(&res, 0, sizeof res);
246 1.1 deraadt res.ypbind_status = YPBIND_FAIL_VAL;
247 1.1 deraadt
248 1.30 lukem for (count = 0, ypdb = ypbindlist;
249 1.30 lukem ypdb != NULL;
250 1.30 lukem ypdb = ypdb->dom_pnext, count++) {
251 1.30 lukem if (count > 100)
252 1.30 lukem return NULL; /* prevent denial of service */
253 1.22 thorpej if (!strcmp(ypdb->dom_domain, arg))
254 1.1 deraadt break;
255 1.30 lukem }
256 1.1 deraadt
257 1.17 mycroft if (ypdb == NULL) {
258 1.24 christos ypdb = makebinding(arg);
259 1.1 deraadt ypdb->dom_vers = YPVERS;
260 1.1 deraadt ypdb->dom_alive = 0;
261 1.1 deraadt ypdb->dom_lockfd = -1;
262 1.24 christos removelock(ypdb);
263 1.20 cgd ypdb->dom_xid = unique_xid(ypdb);
264 1.1 deraadt ypdb->dom_pnext = ypbindlist;
265 1.1 deraadt ypbindlist = ypdb;
266 1.1 deraadt check++;
267 1.24 christos #ifdef DEBUG
268 1.24 christos if (debug)
269 1.24 christos printf("unknown domain %s\n", arg);
270 1.24 christos #endif
271 1.1 deraadt return NULL;
272 1.1 deraadt }
273 1.1 deraadt
274 1.24 christos if (ypdb->dom_alive == 0) {
275 1.24 christos #ifdef DEBUG
276 1.24 christos if (debug)
277 1.24 christos printf("dead domain %s\n", arg);
278 1.24 christos #endif
279 1.1 deraadt return NULL;
280 1.24 christos }
281 1.1 deraadt
282 1.9 deraadt #ifdef HEURISTIC
283 1.9 deraadt time(&now);
284 1.9 deraadt if (now < ypdb->dom_ask_t + 5) {
285 1.1 deraadt /*
286 1.9 deraadt * Hmm. More than 2 requests in 5 seconds have indicated
287 1.9 deraadt * that my binding is possibly incorrect.
288 1.9 deraadt * Ok, do an immediate poll of the server.
289 1.1 deraadt */
290 1.9 deraadt if (ypdb->dom_check_t >= now) {
291 1.9 deraadt /* don't flood it */
292 1.9 deraadt ypdb->dom_check_t = 0;
293 1.9 deraadt check++;
294 1.9 deraadt }
295 1.1 deraadt }
296 1.9 deraadt ypdb->dom_ask_t = now;
297 1.1 deraadt #endif
298 1.1 deraadt
299 1.1 deraadt res.ypbind_status = YPBIND_SUCC_VAL;
300 1.1 deraadt res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
301 1.1 deraadt ypdb->dom_server_addr.sin_addr.s_addr;
302 1.1 deraadt res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
303 1.1 deraadt ypdb->dom_server_port;
304 1.24 christos #ifdef DEBUG
305 1.24 christos if (debug)
306 1.24 christos printf("domain %s at %s/%d\n", ypdb->dom_domain,
307 1.24 christos inet_ntoa(ypdb->dom_server_addr.sin_addr),
308 1.24 christos ntohs(ypdb->dom_server_addr.sin_port));
309 1.24 christos #endif
310 1.1 deraadt return &res;
311 1.1 deraadt }
312 1.1 deraadt
313 1.24 christos static void *
314 1.24 christos ypbindproc_setdom_2(transp, argp)
315 1.17 mycroft SVCXPRT *transp;
316 1.24 christos void *argp;
317 1.1 deraadt {
318 1.24 christos struct ypbind_setdom *sd = argp;
319 1.1 deraadt struct sockaddr_in *fromsin, bindsin;
320 1.13 deraadt static bool_t res;
321 1.1 deraadt
322 1.24 christos #ifdef DEBUG
323 1.24 christos if (debug)
324 1.24 christos printf("ypbindproc_setdom_2 %s\n", inet_ntoa(bindsin.sin_addr));
325 1.24 christos #endif
326 1.36 mrg (void)memset(&res, 0, sizeof(res));
327 1.1 deraadt fromsin = svc_getcaller(transp);
328 1.1 deraadt
329 1.27 thorpej switch (ypbindmode) {
330 1.27 thorpej case YPBIND_SETLOCAL:
331 1.24 christos if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
332 1.24 christos #ifdef DEBUG
333 1.24 christos if (debug)
334 1.24 christos printf("ypset from %s denied\n",
335 1.24 christos inet_ntoa(fromsin->sin_addr));
336 1.24 christos #endif
337 1.24 christos return NULL;
338 1.24 christos }
339 1.27 thorpej /* FALLTHROUGH */
340 1.27 thorpej
341 1.27 thorpej case YPBIND_SETALL:
342 1.27 thorpej been_ypset = 1;
343 1.1 deraadt break;
344 1.27 thorpej
345 1.27 thorpej case YPBIND_DIRECT:
346 1.27 thorpej case YPBIND_BROADCAST:
347 1.1 deraadt default:
348 1.24 christos #ifdef DEBUG
349 1.24 christos if (debug)
350 1.24 christos printf("ypset denied\n");
351 1.24 christos #endif
352 1.24 christos return NULL;
353 1.1 deraadt }
354 1.4 deraadt
355 1.24 christos if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
356 1.24 christos #ifdef DEBUG
357 1.24 christos if (debug)
358 1.24 christos printf("ypset from unpriviledged port denied\n");
359 1.24 christos #endif
360 1.13 deraadt return &res;
361 1.24 christos }
362 1.1 deraadt
363 1.24 christos if (sd->ypsetdom_vers != YPVERS) {
364 1.24 christos #ifdef DEBUG
365 1.24 christos if (debug)
366 1.24 christos printf("ypset with wrong version denied\n");
367 1.24 christos #endif
368 1.13 deraadt return &res;
369 1.24 christos }
370 1.1 deraadt
371 1.36 mrg (void)memset(&bindsin, 0, sizeof bindsin);
372 1.1 deraadt bindsin.sin_family = AF_INET;
373 1.17 mycroft bindsin.sin_len = sizeof(bindsin);
374 1.24 christos bindsin.sin_addr = sd->ypsetdom_addr;
375 1.24 christos bindsin.sin_port = sd->ypsetdom_port;
376 1.24 christos rpc_received(sd->ypsetdom_domain, &bindsin, 1);
377 1.24 christos
378 1.24 christos #ifdef DEBUG
379 1.24 christos if (debug)
380 1.24 christos printf("ypset to %s succeeded\n", inet_ntoa(bindsin.sin_addr));
381 1.24 christos #endif
382 1.1 deraadt res = 1;
383 1.13 deraadt return &res;
384 1.1 deraadt }
385 1.1 deraadt
386 1.1 deraadt static void
387 1.1 deraadt ypbindprog_2(rqstp, transp)
388 1.17 mycroft struct svc_req *rqstp;
389 1.17 mycroft register SVCXPRT *transp;
390 1.1 deraadt {
391 1.1 deraadt union {
392 1.22 thorpej char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
393 1.1 deraadt struct ypbind_setdom ypbindproc_setdom_2_arg;
394 1.1 deraadt } argument;
395 1.1 deraadt struct authunix_parms *creds;
396 1.1 deraadt char *result;
397 1.24 christos xdrproc_t xdr_argument, xdr_result;
398 1.24 christos void *(*local) __P((SVCXPRT *, void *));
399 1.1 deraadt
400 1.1 deraadt switch (rqstp->rq_proc) {
401 1.1 deraadt case YPBINDPROC_NULL:
402 1.1 deraadt xdr_argument = xdr_void;
403 1.1 deraadt xdr_result = xdr_void;
404 1.24 christos local = ypbindproc_null_2;
405 1.1 deraadt break;
406 1.1 deraadt
407 1.1 deraadt case YPBINDPROC_DOMAIN:
408 1.21 thorpej xdr_argument = xdr_ypdomain_wrap_string;
409 1.1 deraadt xdr_result = xdr_ypbind_resp;
410 1.24 christos local = ypbindproc_domain_2;
411 1.1 deraadt break;
412 1.1 deraadt
413 1.1 deraadt case YPBINDPROC_SETDOM:
414 1.17 mycroft switch (rqstp->rq_cred.oa_flavor) {
415 1.1 deraadt case AUTH_UNIX:
416 1.1 deraadt creds = (struct authunix_parms *)rqstp->rq_clntcred;
417 1.17 mycroft if (creds->aup_uid != 0) {
418 1.1 deraadt svcerr_auth(transp, AUTH_BADCRED);
419 1.1 deraadt return;
420 1.1 deraadt }
421 1.1 deraadt break;
422 1.1 deraadt default:
423 1.1 deraadt svcerr_auth(transp, AUTH_TOOWEAK);
424 1.1 deraadt return;
425 1.1 deraadt }
426 1.1 deraadt
427 1.1 deraadt xdr_argument = xdr_ypbind_setdom;
428 1.1 deraadt xdr_result = xdr_void;
429 1.24 christos local = ypbindproc_setdom_2;
430 1.1 deraadt break;
431 1.1 deraadt
432 1.1 deraadt default:
433 1.1 deraadt svcerr_noproc(transp);
434 1.1 deraadt return;
435 1.1 deraadt }
436 1.36 mrg (void)memset(&argument, 0, sizeof(argument));
437 1.14 cgd if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
438 1.1 deraadt svcerr_decode(transp);
439 1.1 deraadt return;
440 1.1 deraadt }
441 1.24 christos result = (*local)(transp, &argument);
442 1.1 deraadt if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
443 1.1 deraadt svcerr_systemerr(transp);
444 1.1 deraadt }
445 1.1 deraadt return;
446 1.1 deraadt }
447 1.1 deraadt
448 1.24 christos int
449 1.1 deraadt main(argc, argv)
450 1.17 mycroft int argc;
451 1.17 mycroft char *argv[];
452 1.1 deraadt {
453 1.1 deraadt struct timeval tv;
454 1.1 deraadt fd_set fdsr;
455 1.13 deraadt int width, lockfd;
456 1.18 mycroft int evil = 0, one;
457 1.27 thorpej char pathname[MAXPATHLEN];
458 1.27 thorpej struct stat st;
459 1.1 deraadt
460 1.1 deraadt yp_get_default_domain(&domainname);
461 1.24 christos if (domainname[0] == '\0')
462 1.24 christos errx(1, "Domainname not set. Aborting.");
463 1.1 deraadt
464 1.27 thorpej /*
465 1.27 thorpej * Per traditional ypbind(8) semantics, if a ypservers
466 1.27 thorpej * file does not exist, we default to broadcast mode.
467 1.27 thorpej * If the file does exist, we default to direct mode.
468 1.27 thorpej * Note that we can still override direct mode by passing
469 1.27 thorpej * the -broadcast flag.
470 1.27 thorpej */
471 1.29 thorpej snprintf(pathname, sizeof(pathname), "%s/%s%s", BINDINGDIR,
472 1.29 thorpej domainname, YPSERVERSSUFF);
473 1.27 thorpej if (stat(pathname, &st) < 0) {
474 1.27 thorpej #ifdef DEBUG
475 1.27 thorpej if (debug)
476 1.27 thorpej fprintf(stderr,
477 1.27 thorpej "%s does not exist, defaulting to broadcast\n",
478 1.27 thorpej pathname);
479 1.27 thorpej #endif
480 1.27 thorpej ypbindmode = YPBIND_BROADCAST;
481 1.27 thorpej } else
482 1.27 thorpej ypbindmode = YPBIND_DIRECT;
483 1.27 thorpej
484 1.18 mycroft while (--argc) {
485 1.18 mycroft ++argv;
486 1.30 lukem if (!strcmp("-insecure", *argv))
487 1.30 lukem insecure = 1;
488 1.30 lukem else if (!strcmp("-ypset", *argv))
489 1.27 thorpej ypbindmode = YPBIND_SETALL;
490 1.18 mycroft else if (!strcmp("-ypsetme", *argv))
491 1.27 thorpej ypbindmode = YPBIND_SETLOCAL;
492 1.27 thorpej else if (!strcmp("-broadcast", *argv))
493 1.27 thorpej ypbindmode = YPBIND_BROADCAST;
494 1.24 christos #ifdef DEBUG
495 1.24 christos else if (!strcmp("-d", *argv))
496 1.24 christos debug++;
497 1.24 christos #endif
498 1.24 christos else
499 1.24 christos usage();
500 1.1 deraadt }
501 1.1 deraadt
502 1.1 deraadt /* blow away everything in BINDINGDIR */
503 1.1 deraadt
504 1.27 thorpej lockfd = open(_PATH_YPBIND_LOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644);
505 1.24 christos if (lockfd == -1)
506 1.27 thorpej err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
507 1.24 christos
508 1.24 christos #if O_SHLOCK == 0
509 1.36 mrg (void)flock(lockfd, LOCK_SH);
510 1.13 deraadt #endif
511 1.1 deraadt
512 1.36 mrg (void)pmap_unset(YPBINDPROG, YPBINDVERS);
513 1.1 deraadt
514 1.6 deraadt udptransp = svcudp_create(RPC_ANYSOCK);
515 1.24 christos if (udptransp == NULL)
516 1.24 christos errx(1, "Cannot create udp service.");
517 1.24 christos
518 1.6 deraadt if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
519 1.24 christos IPPROTO_UDP))
520 1.24 christos errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
521 1.1 deraadt
522 1.6 deraadt tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
523 1.24 christos if (tcptransp == NULL)
524 1.24 christos errx(1, "Cannot create tcp service.");
525 1.24 christos
526 1.6 deraadt if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
527 1.24 christos IPPROTO_TCP))
528 1.24 christos errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
529 1.1 deraadt
530 1.27 thorpej /* XXX use SOCK_STREAM for direct queries? */
531 1.24 christos if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
532 1.24 christos err(1, "rpc socket");
533 1.24 christos if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
534 1.24 christos err(1, "ping socket");
535 1.6 deraadt
536 1.36 mrg (void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
537 1.36 mrg (void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
538 1.24 christos
539 1.18 mycroft one = 1;
540 1.36 mrg (void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
541 1.1 deraadt rmtca.prog = YPPROG;
542 1.1 deraadt rmtca.vers = YPVERS;
543 1.1 deraadt rmtca.proc = YPPROC_DOMAIN_NONACK;
544 1.1 deraadt rmtca.xdr_args = NULL; /* set at call time */
545 1.1 deraadt rmtca.args_ptr = NULL; /* set at call time */
546 1.1 deraadt rmtcr.port_ptr = &rmtcr_port;
547 1.1 deraadt rmtcr.xdr_results = xdr_bool;
548 1.1 deraadt rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
549 1.1 deraadt
550 1.30 lukem if (_yp_invalid_domain(domainname))
551 1.30 lukem errx(1, "bad domainname: %s", domainname);
552 1.30 lukem
553 1.1 deraadt /* build initial domain binding, make it "unsuccessful" */
554 1.24 christos ypbindlist = makebinding(domainname);
555 1.1 deraadt ypbindlist->dom_vers = YPVERS;
556 1.1 deraadt ypbindlist->dom_alive = 0;
557 1.1 deraadt ypbindlist->dom_lockfd = -1;
558 1.24 christos removelock(ypbindlist);
559 1.12 deraadt
560 1.12 deraadt checkwork();
561 1.1 deraadt
562 1.27 thorpej width = svc_maxfd;
563 1.27 thorpej if (rpcsock > width)
564 1.27 thorpej width = rpcsock;
565 1.27 thorpej if (pingsock > width)
566 1.27 thorpej width = pingsock;
567 1.27 thorpej width++;
568 1.17 mycroft
569 1.27 thorpej for (;;) {
570 1.1 deraadt fdsr = svc_fdset;
571 1.1 deraadt FD_SET(rpcsock, &fdsr);
572 1.10 deraadt FD_SET(pingsock, &fdsr);
573 1.1 deraadt tv.tv_sec = 1;
574 1.1 deraadt tv.tv_usec = 0;
575 1.1 deraadt
576 1.17 mycroft switch (select(width, &fdsr, NULL, NULL, &tv)) {
577 1.1 deraadt case 0:
578 1.1 deraadt checkwork();
579 1.1 deraadt break;
580 1.1 deraadt case -1:
581 1.24 christos warn("select");
582 1.1 deraadt break;
583 1.1 deraadt default:
584 1.15 mycroft if (FD_ISSET(rpcsock, &fdsr))
585 1.1 deraadt handle_replies();
586 1.15 mycroft if (FD_ISSET(pingsock, &fdsr))
587 1.10 deraadt handle_ping();
588 1.1 deraadt svc_getreqset(&fdsr);
589 1.15 mycroft if (check)
590 1.1 deraadt checkwork();
591 1.1 deraadt break;
592 1.18 mycroft }
593 1.18 mycroft
594 1.18 mycroft if (!evil && ypbindlist->dom_alive) {
595 1.18 mycroft evil = 1;
596 1.24 christos #ifdef DEBUG
597 1.24 christos if (!debug)
598 1.25 thorpej #endif
599 1.24 christos daemon(0, 0);
600 1.1 deraadt }
601 1.1 deraadt }
602 1.1 deraadt }
603 1.1 deraadt
604 1.1 deraadt /*
605 1.9 deraadt * State transition is done like this:
606 1.9 deraadt *
607 1.11 ws * STATE EVENT ACTION NEWSTATE TIMEOUT
608 1.11 ws * no binding timeout broadcast no binding 5 sec
609 1.11 ws * no binding answer -- binding 60 sec
610 1.11 ws * binding timeout ping server checking 5 sec
611 1.11 ws * checking timeout ping server + broadcast checking 5 sec
612 1.11 ws * checking answer -- binding 60 sec
613 1.1 deraadt */
614 1.24 christos void
615 1.1 deraadt checkwork()
616 1.1 deraadt {
617 1.1 deraadt struct _dom_binding *ypdb;
618 1.1 deraadt time_t t;
619 1.1 deraadt
620 1.1 deraadt check = 0;
621 1.1 deraadt
622 1.1 deraadt time(&t);
623 1.17 mycroft for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
624 1.17 mycroft if (ypdb->dom_check_t < t) {
625 1.10 deraadt if (ypdb->dom_alive == 1)
626 1.10 deraadt ping(ypdb);
627 1.10 deraadt else
628 1.27 thorpej nag_servers(ypdb);
629 1.1 deraadt time(&t);
630 1.1 deraadt ypdb->dom_check_t = t + 5;
631 1.1 deraadt }
632 1.1 deraadt }
633 1.1 deraadt }
634 1.1 deraadt
635 1.24 christos int
636 1.8 deraadt ping(ypdb)
637 1.8 deraadt struct _dom_binding *ypdb;
638 1.1 deraadt {
639 1.8 deraadt char *dom = ypdb->dom_domain;
640 1.17 mycroft struct rpc_msg msg;
641 1.24 christos char buf[BUFSIZE];
642 1.10 deraadt enum clnt_stat st;
643 1.10 deraadt int outlen;
644 1.10 deraadt AUTH *rpcua;
645 1.17 mycroft XDR xdr;
646 1.10 deraadt
647 1.36 mrg (void)memset(&xdr, 0, sizeof xdr);
648 1.36 mrg (void)memset(&msg, 0, sizeof msg);
649 1.10 deraadt
650 1.10 deraadt rpcua = authunix_create_default();
651 1.24 christos if (rpcua == NULL) {
652 1.24 christos #ifdef DEBUG
653 1.24 christos if (debug)
654 1.24 christos printf("cannot get unix auth\n");
655 1.24 christos #endif
656 1.10 deraadt return RPC_SYSTEMERROR;
657 1.10 deraadt }
658 1.24 christos
659 1.17 mycroft msg.rm_direction = CALL;
660 1.17 mycroft msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
661 1.17 mycroft msg.rm_call.cb_prog = YPPROG;
662 1.17 mycroft msg.rm_call.cb_vers = YPVERS;
663 1.17 mycroft msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
664 1.17 mycroft msg.rm_call.cb_cred = rpcua->ah_cred;
665 1.17 mycroft msg.rm_call.cb_verf = rpcua->ah_verf;
666 1.17 mycroft
667 1.20 cgd msg.rm_xid = ypdb->dom_xid;
668 1.17 mycroft xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
669 1.17 mycroft if (!xdr_callmsg(&xdr, &msg)) {
670 1.10 deraadt st = RPC_CANTENCODEARGS;
671 1.10 deraadt AUTH_DESTROY(rpcua);
672 1.10 deraadt return st;
673 1.10 deraadt }
674 1.23 thorpej if (!xdr_ypdomain_wrap_string(&xdr, &dom)) {
675 1.10 deraadt st = RPC_CANTENCODEARGS;
676 1.10 deraadt AUTH_DESTROY(rpcua);
677 1.10 deraadt return st;
678 1.10 deraadt }
679 1.17 mycroft outlen = (int)xdr_getpos(&xdr);
680 1.17 mycroft xdr_destroy(&xdr);
681 1.17 mycroft if (outlen < 1) {
682 1.10 deraadt st = RPC_CANTENCODEARGS;
683 1.10 deraadt AUTH_DESTROY(rpcua);
684 1.10 deraadt return st;
685 1.10 deraadt }
686 1.10 deraadt AUTH_DESTROY(rpcua);
687 1.10 deraadt
688 1.10 deraadt ypdb->dom_alive = 2;
689 1.10 deraadt if (sendto(pingsock, buf, outlen, 0,
690 1.10 deraadt (struct sockaddr *)&ypdb->dom_server_addr,
691 1.24 christos sizeof ypdb->dom_server_addr) == -1)
692 1.24 christos warn("ping: sendto");
693 1.10 deraadt return 0;
694 1.10 deraadt
695 1.10 deraadt }
696 1.10 deraadt
697 1.27 thorpej static int
698 1.27 thorpej nag_servers(ypdb)
699 1.10 deraadt struct _dom_binding *ypdb;
700 1.10 deraadt {
701 1.10 deraadt char *dom = ypdb->dom_domain;
702 1.17 mycroft struct rpc_msg msg;
703 1.27 thorpej char buf[BUFSIZE];
704 1.1 deraadt enum clnt_stat st;
705 1.27 thorpej int outlen;
706 1.1 deraadt AUTH *rpcua;
707 1.17 mycroft XDR xdr;
708 1.1 deraadt
709 1.21 thorpej rmtca.xdr_args = xdr_ypdomain_wrap_string;
710 1.21 thorpej rmtca.args_ptr = (char *)&dom;
711 1.1 deraadt
712 1.36 mrg (void)memset(&xdr, 0, sizeof xdr);
713 1.36 mrg (void)memset(&msg, 0, sizeof msg);
714 1.1 deraadt
715 1.1 deraadt rpcua = authunix_create_default();
716 1.24 christos if (rpcua == NULL) {
717 1.24 christos #ifdef DEBUG
718 1.24 christos if (debug)
719 1.24 christos printf("cannot get unix auth\n");
720 1.24 christos #endif
721 1.1 deraadt return RPC_SYSTEMERROR;
722 1.1 deraadt }
723 1.17 mycroft msg.rm_direction = CALL;
724 1.17 mycroft msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
725 1.17 mycroft msg.rm_call.cb_prog = PMAPPROG;
726 1.17 mycroft msg.rm_call.cb_vers = PMAPVERS;
727 1.17 mycroft msg.rm_call.cb_proc = PMAPPROC_CALLIT;
728 1.17 mycroft msg.rm_call.cb_cred = rpcua->ah_cred;
729 1.17 mycroft msg.rm_call.cb_verf = rpcua->ah_verf;
730 1.17 mycroft
731 1.20 cgd msg.rm_xid = ypdb->dom_xid;
732 1.17 mycroft xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
733 1.17 mycroft if (!xdr_callmsg(&xdr, &msg)) {
734 1.1 deraadt st = RPC_CANTENCODEARGS;
735 1.1 deraadt AUTH_DESTROY(rpcua);
736 1.1 deraadt return st;
737 1.1 deraadt }
738 1.17 mycroft if (!xdr_rmtcall_args(&xdr, &rmtca)) {
739 1.1 deraadt st = RPC_CANTENCODEARGS;
740 1.1 deraadt AUTH_DESTROY(rpcua);
741 1.1 deraadt return st;
742 1.1 deraadt }
743 1.17 mycroft outlen = (int)xdr_getpos(&xdr);
744 1.17 mycroft xdr_destroy(&xdr);
745 1.17 mycroft if (outlen < 1) {
746 1.10 deraadt st = RPC_CANTENCODEARGS;
747 1.1 deraadt AUTH_DESTROY(rpcua);
748 1.1 deraadt return st;
749 1.1 deraadt }
750 1.1 deraadt AUTH_DESTROY(rpcua);
751 1.1 deraadt
752 1.17 mycroft if (ypdb->dom_lockfd != -1) {
753 1.36 mrg (void)close(ypdb->dom_lockfd);
754 1.9 deraadt ypdb->dom_lockfd = -1;
755 1.24 christos removelock(ypdb);
756 1.9 deraadt }
757 1.8 deraadt
758 1.11 ws if (ypdb->dom_alive == 2) {
759 1.11 ws /*
760 1.11 ws * This resolves the following situation:
761 1.11 ws * ypserver on other subnet was once bound,
762 1.11 ws * but rebooted and is now using a different port
763 1.11 ws */
764 1.27 thorpej struct sockaddr_in bindsin;
765 1.27 thorpej
766 1.27 thorpej memset(&bindsin, 0, sizeof bindsin);
767 1.27 thorpej bindsin.sin_family = AF_INET;
768 1.27 thorpej bindsin.sin_len = sizeof(bindsin);
769 1.27 thorpej bindsin.sin_port = htons(PMAPPORT);
770 1.17 mycroft bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
771 1.27 thorpej
772 1.17 mycroft if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
773 1.24 christos sizeof bindsin) == -1)
774 1.24 christos warn("broadcast: sendto");
775 1.11 ws }
776 1.27 thorpej
777 1.27 thorpej switch (ypbindmode) {
778 1.27 thorpej case YPBIND_SETALL:
779 1.27 thorpej case YPBIND_SETLOCAL:
780 1.27 thorpej if (been_ypset)
781 1.27 thorpej return direct_set(buf, outlen, ypdb);
782 1.27 thorpej /* FALLTHROUGH */
783 1.27 thorpej
784 1.27 thorpej case YPBIND_BROADCAST:
785 1.27 thorpej return broadcast(buf, outlen);
786 1.27 thorpej
787 1.27 thorpej case YPBIND_DIRECT:
788 1.27 thorpej return direct(buf, outlen);
789 1.27 thorpej }
790 1.27 thorpej
791 1.27 thorpej return -1;
792 1.27 thorpej }
793 1.27 thorpej
794 1.27 thorpej static int
795 1.27 thorpej broadcast(buf, outlen)
796 1.27 thorpej char *buf;
797 1.27 thorpej int outlen;
798 1.27 thorpej {
799 1.27 thorpej struct ifconf ifc;
800 1.27 thorpej struct ifreq ifreq, *ifr;
801 1.27 thorpej struct in_addr in;
802 1.27 thorpej int i, sock, len;
803 1.27 thorpej char inbuf[8192];
804 1.27 thorpej struct sockaddr_in bindsin;
805 1.27 thorpej
806 1.1 deraadt /* find all networks and send the RPC packet out them all */
807 1.24 christos if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
808 1.24 christos warn("broadcast: socket");
809 1.1 deraadt return -1;
810 1.1 deraadt }
811 1.27 thorpej
812 1.27 thorpej memset(&bindsin, 0, sizeof bindsin);
813 1.27 thorpej bindsin.sin_family = AF_INET;
814 1.27 thorpej bindsin.sin_len = sizeof(bindsin);
815 1.27 thorpej bindsin.sin_port = htons(PMAPPORT);
816 1.27 thorpej
817 1.1 deraadt ifc.ifc_len = sizeof inbuf;
818 1.1 deraadt ifc.ifc_buf = inbuf;
819 1.17 mycroft if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
820 1.36 mrg (void)close(sock);
821 1.24 christos warn("broadcast: ioctl(SIOCGIFCONF)");
822 1.1 deraadt return -1;
823 1.1 deraadt }
824 1.1 deraadt ifr = ifc.ifc_req;
825 1.1 deraadt ifreq.ifr_name[0] = '\0';
826 1.17 mycroft for (i = 0; i < ifc.ifc_len; i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
827 1.1 deraadt #if defined(BSD) && BSD >= 199103
828 1.1 deraadt len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
829 1.1 deraadt #else
830 1.1 deraadt len = sizeof ifc.ifc_len / sizeof(struct ifreq);
831 1.1 deraadt #endif
832 1.1 deraadt ifreq = *ifr;
833 1.17 mycroft if (ifreq.ifr_addr.sa_family != AF_INET)
834 1.1 deraadt continue;
835 1.17 mycroft if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
836 1.24 christos warn("broadcast: ioctl(SIOCGIFFLAGS)");
837 1.1 deraadt continue;
838 1.1 deraadt }
839 1.17 mycroft if ((ifreq.ifr_flags & IFF_UP) == 0)
840 1.1 deraadt continue;
841 1.1 deraadt
842 1.1 deraadt ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
843 1.17 mycroft if (ifreq.ifr_flags == IFF_BROADCAST) {
844 1.17 mycroft if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
845 1.24 christos warn("broadcast: ioctl(SIOCGIFBRDADDR)");
846 1.1 deraadt continue;
847 1.1 deraadt }
848 1.17 mycroft } else if (ifreq.ifr_flags == IFF_LOOPBACK) {
849 1.17 mycroft if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
850 1.24 christos warn("broadcast: ioctl(SIOCGIFADDR)");
851 1.1 deraadt continue;
852 1.1 deraadt }
853 1.1 deraadt } else
854 1.1 deraadt continue;
855 1.1 deraadt
856 1.1 deraadt in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
857 1.17 mycroft bindsin.sin_addr = in;
858 1.17 mycroft if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
859 1.24 christos sizeof bindsin) == -1)
860 1.24 christos warn("broadcast: sendto");
861 1.1 deraadt }
862 1.36 mrg (void)close(sock);
863 1.27 thorpej return 0;
864 1.27 thorpej }
865 1.27 thorpej
866 1.27 thorpej static int
867 1.27 thorpej direct(buf, outlen)
868 1.27 thorpej char *buf;
869 1.27 thorpej int outlen;
870 1.27 thorpej {
871 1.27 thorpej static FILE *df;
872 1.27 thorpej static char ypservers_path[MAXPATHLEN];
873 1.27 thorpej char line[_POSIX2_LINE_MAX];
874 1.27 thorpej char *p;
875 1.27 thorpej struct hostent *hp;
876 1.27 thorpej struct sockaddr_in bindsin;
877 1.27 thorpej int i, count = 0;
878 1.27 thorpej
879 1.27 thorpej if (df)
880 1.27 thorpej rewind(df);
881 1.27 thorpej else {
882 1.27 thorpej snprintf(ypservers_path, sizeof(ypservers_path),
883 1.29 thorpej "%s/%s%s", BINDINGDIR, domainname, YPSERVERSSUFF);
884 1.27 thorpej df = fopen(ypservers_path, "r");
885 1.27 thorpej if (df == NULL)
886 1.27 thorpej err(1, ypservers_path);
887 1.27 thorpej }
888 1.27 thorpej
889 1.27 thorpej memset(&bindsin, 0, sizeof bindsin);
890 1.27 thorpej bindsin.sin_family = AF_INET;
891 1.27 thorpej bindsin.sin_len = sizeof(bindsin);
892 1.27 thorpej bindsin.sin_port = htons(PMAPPORT);
893 1.27 thorpej
894 1.27 thorpej while(fgets(line, sizeof(line), df) != NULL) {
895 1.27 thorpej /* skip lines that are too big */
896 1.27 thorpej p = strchr(line, '\n');
897 1.27 thorpej if (p == NULL) {
898 1.27 thorpej int c;
899 1.27 thorpej
900 1.27 thorpej while ((c = getc(df)) != '\n' && c != EOF)
901 1.27 thorpej ;
902 1.27 thorpej continue;
903 1.27 thorpej }
904 1.27 thorpej *p = '\0';
905 1.27 thorpej p = line;
906 1.27 thorpej while (isspace(*p))
907 1.27 thorpej p++;
908 1.27 thorpej if (*p == '#')
909 1.27 thorpej continue;
910 1.27 thorpej hp = gethostbyname(p);
911 1.27 thorpej if (!hp) {
912 1.27 thorpej herror(p);
913 1.27 thorpej continue;
914 1.27 thorpej }
915 1.27 thorpej /* step through all addresses in case first is unavailable */
916 1.27 thorpej for (i = 0; hp->h_addr_list[i]; i++) {
917 1.27 thorpej memmove(&bindsin.sin_addr, hp->h_addr_list[0],
918 1.27 thorpej hp->h_length);
919 1.27 thorpej if (sendto(rpcsock, buf, outlen, 0,
920 1.27 thorpej (struct sockaddr *)&bindsin, sizeof bindsin) < 0) {
921 1.27 thorpej warn("direct: sendto");
922 1.27 thorpej continue;
923 1.27 thorpej } else
924 1.27 thorpej count++;
925 1.27 thorpej }
926 1.27 thorpej }
927 1.27 thorpej if (!count)
928 1.27 thorpej errx(1, "no contactable servers found in %s",
929 1.27 thorpej ypservers_path);
930 1.27 thorpej
931 1.27 thorpej return 0;
932 1.27 thorpej }
933 1.27 thorpej
934 1.27 thorpej static int
935 1.27 thorpej direct_set(buf, outlen, ypdb)
936 1.27 thorpej char *buf;
937 1.27 thorpej int outlen;
938 1.27 thorpej struct _dom_binding *ypdb;
939 1.27 thorpej {
940 1.27 thorpej struct sockaddr_in bindsin;
941 1.27 thorpej char path[MAXPATHLEN];
942 1.27 thorpej struct iovec iov[2];
943 1.27 thorpej struct ypbind_resp ybr;
944 1.27 thorpej SVCXPRT dummy_svc;
945 1.27 thorpej int fd, bytes;
946 1.27 thorpej
947 1.27 thorpej /*
948 1.27 thorpej * Gack, we lose if binding file went away. We reset
949 1.27 thorpej * "been_set" if this happens, otherwise we'll never
950 1.27 thorpej * bind again.
951 1.27 thorpej */
952 1.35 lukem snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
953 1.27 thorpej ypdb->dom_domain, ypdb->dom_vers);
954 1.27 thorpej
955 1.27 thorpej if ((fd = open(path, O_SHLOCK|O_RDONLY, 0644)) == -1) {
956 1.27 thorpej warn(path);
957 1.27 thorpej been_ypset = 0;
958 1.27 thorpej return -1;
959 1.27 thorpej }
960 1.27 thorpej
961 1.27 thorpej #if O_SHLOCK == 0
962 1.36 mrg (void)flock(fd, LOCK_SH);
963 1.27 thorpej #endif
964 1.27 thorpej
965 1.27 thorpej /* Read the binding file... */
966 1.27 thorpej iov[0].iov_base = (caddr_t)&(dummy_svc.xp_port);
967 1.27 thorpej iov[0].iov_len = sizeof(dummy_svc.xp_port);
968 1.27 thorpej iov[1].iov_base = (caddr_t)&ybr;
969 1.27 thorpej iov[1].iov_len = sizeof(ybr);
970 1.27 thorpej bytes = readv(fd, iov, 2);
971 1.27 thorpej (void)close(fd);
972 1.27 thorpej if (bytes != (iov[0].iov_len + iov[1].iov_len)) {
973 1.27 thorpej /* Binding file corrupt? */
974 1.27 thorpej warn(path);
975 1.27 thorpej been_ypset = 0;
976 1.27 thorpej return -1;
977 1.27 thorpej }
978 1.27 thorpej
979 1.27 thorpej bindsin.sin_addr =
980 1.27 thorpej ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
981 1.27 thorpej
982 1.27 thorpej if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
983 1.27 thorpej sizeof(bindsin)) < 0) {
984 1.27 thorpej warn("direct_set: sendto");
985 1.27 thorpej return -1;
986 1.27 thorpej }
987 1.27 thorpej
988 1.1 deraadt return 0;
989 1.1 deraadt }
990 1.1 deraadt
991 1.24 christos static enum clnt_stat
992 1.1 deraadt handle_replies()
993 1.1 deraadt {
994 1.24 christos char buf[BUFSIZE];
995 1.1 deraadt int fromlen, inlen;
996 1.20 cgd struct _dom_binding *ypdb;
997 1.1 deraadt struct sockaddr_in raddr;
998 1.1 deraadt struct rpc_msg msg;
999 1.1 deraadt XDR xdr;
1000 1.1 deraadt
1001 1.1 deraadt recv_again:
1002 1.36 mrg (void)memset(&xdr, 0, sizeof(xdr));
1003 1.36 mrg (void)memset(&msg, 0, sizeof(msg));
1004 1.1 deraadt msg.acpted_rply.ar_verf = _null_auth;
1005 1.1 deraadt msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
1006 1.1 deraadt msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
1007 1.1 deraadt
1008 1.1 deraadt try_again:
1009 1.36 mrg fromlen = sizeof(struct sockaddr);
1010 1.1 deraadt inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
1011 1.1 deraadt (struct sockaddr *)&raddr, &fromlen);
1012 1.17 mycroft if (inlen < 0) {
1013 1.17 mycroft if (errno == EINTR)
1014 1.1 deraadt goto try_again;
1015 1.1 deraadt return RPC_CANTRECV;
1016 1.1 deraadt }
1017 1.17 mycroft if (inlen < sizeof(u_int32_t))
1018 1.1 deraadt goto recv_again;
1019 1.1 deraadt
1020 1.1 deraadt /*
1021 1.1 deraadt * see if reply transaction id matches sent id.
1022 1.1 deraadt * If so, decode the results.
1023 1.1 deraadt */
1024 1.1 deraadt xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
1025 1.17 mycroft if (xdr_replymsg(&xdr, &msg)) {
1026 1.17 mycroft if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
1027 1.17 mycroft (msg.acpted_rply.ar_stat == SUCCESS)) {
1028 1.35 lukem raddr.sin_port = htons((u_short)rmtcr_port);
1029 1.20 cgd ypdb = xid2ypdb(msg.rm_xid);
1030 1.20 cgd if (ypdb != NULL)
1031 1.20 cgd rpc_received(ypdb->dom_domain, &raddr, 0);
1032 1.1 deraadt }
1033 1.1 deraadt }
1034 1.1 deraadt xdr.x_op = XDR_FREE;
1035 1.1 deraadt msg.acpted_rply.ar_results.proc = xdr_void;
1036 1.1 deraadt xdr_destroy(&xdr);
1037 1.1 deraadt
1038 1.1 deraadt return RPC_SUCCESS;
1039 1.1 deraadt }
1040 1.1 deraadt
1041 1.24 christos static enum clnt_stat
1042 1.10 deraadt handle_ping()
1043 1.10 deraadt {
1044 1.24 christos char buf[BUFSIZE];
1045 1.10 deraadt int fromlen, inlen;
1046 1.20 cgd struct _dom_binding *ypdb;
1047 1.10 deraadt struct sockaddr_in raddr;
1048 1.10 deraadt struct rpc_msg msg;
1049 1.10 deraadt XDR xdr;
1050 1.10 deraadt bool_t res;
1051 1.10 deraadt
1052 1.10 deraadt recv_again:
1053 1.36 mrg (void)memset(&xdr, 0, sizeof(xdr));
1054 1.36 mrg (void)memset(&msg, 0, sizeof(msg));
1055 1.10 deraadt msg.acpted_rply.ar_verf = _null_auth;
1056 1.10 deraadt msg.acpted_rply.ar_results.where = (caddr_t)&res;
1057 1.10 deraadt msg.acpted_rply.ar_results.proc = xdr_bool;
1058 1.10 deraadt
1059 1.10 deraadt try_again:
1060 1.10 deraadt fromlen = sizeof (struct sockaddr);
1061 1.10 deraadt inlen = recvfrom(pingsock, buf, sizeof buf, 0,
1062 1.10 deraadt (struct sockaddr *)&raddr, &fromlen);
1063 1.17 mycroft if (inlen < 0) {
1064 1.17 mycroft if (errno == EINTR)
1065 1.10 deraadt goto try_again;
1066 1.10 deraadt return RPC_CANTRECV;
1067 1.10 deraadt }
1068 1.17 mycroft if (inlen < sizeof(u_int32_t))
1069 1.10 deraadt goto recv_again;
1070 1.10 deraadt
1071 1.10 deraadt /*
1072 1.10 deraadt * see if reply transaction id matches sent id.
1073 1.10 deraadt * If so, decode the results.
1074 1.10 deraadt */
1075 1.10 deraadt xdrmem_create(&xdr, buf, (u_int)inlen, XDR_DECODE);
1076 1.17 mycroft if (xdr_replymsg(&xdr, &msg)) {
1077 1.17 mycroft if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
1078 1.17 mycroft (msg.acpted_rply.ar_stat == SUCCESS)) {
1079 1.20 cgd ypdb = xid2ypdb(msg.rm_xid);
1080 1.20 cgd if (ypdb != NULL)
1081 1.20 cgd rpc_received(ypdb->dom_domain, &raddr, 0);
1082 1.10 deraadt }
1083 1.10 deraadt }
1084 1.10 deraadt xdr.x_op = XDR_FREE;
1085 1.10 deraadt msg.acpted_rply.ar_results.proc = xdr_void;
1086 1.10 deraadt xdr_destroy(&xdr);
1087 1.10 deraadt
1088 1.10 deraadt return RPC_SUCCESS;
1089 1.10 deraadt }
1090 1.10 deraadt
1091 1.1 deraadt /*
1092 1.1 deraadt * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
1093 1.1 deraadt */
1094 1.24 christos void
1095 1.1 deraadt rpc_received(dom, raddrp, force)
1096 1.24 christos char *dom;
1097 1.24 christos struct sockaddr_in *raddrp;
1098 1.24 christos int force;
1099 1.1 deraadt {
1100 1.1 deraadt struct _dom_binding *ypdb;
1101 1.6 deraadt struct iovec iov[2];
1102 1.6 deraadt struct ypbind_resp ybr;
1103 1.1 deraadt int fd;
1104 1.14 cgd
1105 1.24 christos #ifdef DEBUG
1106 1.24 christos if (debug)
1107 1.24 christos printf("returned from %s about %s\n",
1108 1.24 christos inet_ntoa(raddrp->sin_addr), dom);
1109 1.24 christos #endif
1110 1.19 cgd
1111 1.17 mycroft if (dom == NULL)
1112 1.1 deraadt return;
1113 1.1 deraadt
1114 1.30 lukem if (_yp_invalid_domain(dom))
1115 1.30 lukem return;
1116 1.30 lukem
1117 1.30 lukem /* don't support insecure servers by default */
1118 1.30 lukem if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
1119 1.30 lukem return;
1120 1.30 lukem
1121 1.17 mycroft for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
1122 1.17 mycroft if (!strcmp(ypdb->dom_domain, dom))
1123 1.1 deraadt break;
1124 1.1 deraadt
1125 1.17 mycroft if (ypdb == NULL) {
1126 1.17 mycroft if (force == 0)
1127 1.1 deraadt return;
1128 1.24 christos ypdb = makebinding(dom);
1129 1.1 deraadt ypdb->dom_lockfd = -1;
1130 1.1 deraadt ypdb->dom_pnext = ypbindlist;
1131 1.1 deraadt ypbindlist = ypdb;
1132 1.1 deraadt }
1133 1.1 deraadt
1134 1.8 deraadt /* soft update, alive */
1135 1.17 mycroft if (ypdb->dom_alive == 1 && force == 0) {
1136 1.17 mycroft if (!memcmp(&ypdb->dom_server_addr, raddrp,
1137 1.17 mycroft sizeof ypdb->dom_server_addr)) {
1138 1.8 deraadt ypdb->dom_alive = 1;
1139 1.24 christos /* recheck binding in 60 sec */
1140 1.24 christos ypdb->dom_check_t = time(NULL) + 60;
1141 1.8 deraadt }
1142 1.1 deraadt return;
1143 1.8 deraadt }
1144 1.8 deraadt
1145 1.36 mrg (void)memcpy(&ypdb->dom_server_addr, raddrp,
1146 1.24 christos sizeof ypdb->dom_server_addr);
1147 1.24 christos /* recheck binding in 60 seconds */
1148 1.24 christos ypdb->dom_check_t = time(NULL) + 60;
1149 1.1 deraadt ypdb->dom_vers = YPVERS;
1150 1.1 deraadt ypdb->dom_alive = 1;
1151 1.1 deraadt
1152 1.17 mycroft if (ypdb->dom_lockfd != -1)
1153 1.36 mrg (void)close(ypdb->dom_lockfd);
1154 1.1 deraadt
1155 1.24 christos if ((fd = makelock(ypdb)) == -1)
1156 1.24 christos return;
1157 1.1 deraadt
1158 1.1 deraadt /*
1159 1.1 deraadt * ok, if BINDINGDIR exists, and we can create the binding file,
1160 1.1 deraadt * then write to it..
1161 1.1 deraadt */
1162 1.1 deraadt ypdb->dom_lockfd = fd;
1163 1.6 deraadt
1164 1.6 deraadt iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
1165 1.6 deraadt iov[0].iov_len = sizeof udptransp->xp_port;
1166 1.6 deraadt iov[1].iov_base = (caddr_t)&ybr;
1167 1.6 deraadt iov[1].iov_len = sizeof ybr;
1168 1.6 deraadt
1169 1.36 mrg (void)memset(&ybr, 0, sizeof ybr);
1170 1.6 deraadt ybr.ypbind_status = YPBIND_SUCC_VAL;
1171 1.24 christos ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
1172 1.24 christos raddrp->sin_addr;
1173 1.24 christos ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
1174 1.24 christos raddrp->sin_port;
1175 1.24 christos
1176 1.24 christos if (writev(ypdb->dom_lockfd, iov, 2) !=
1177 1.24 christos iov[0].iov_len + iov[1].iov_len) {
1178 1.24 christos warnx("writev");
1179 1.36 mrg (void)close(ypdb->dom_lockfd);
1180 1.24 christos removelock(ypdb);
1181 1.1 deraadt ypdb->dom_lockfd = -1;
1182 1.1 deraadt }
1183 1.20 cgd }
1184 1.20 cgd
1185 1.24 christos static struct _dom_binding *
1186 1.20 cgd xid2ypdb(xid)
1187 1.30 lukem u_int32_t xid;
1188 1.20 cgd {
1189 1.20 cgd struct _dom_binding *ypdb;
1190 1.20 cgd
1191 1.20 cgd for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
1192 1.20 cgd if (ypdb->dom_xid == xid)
1193 1.20 cgd break;
1194 1.20 cgd return (ypdb);
1195 1.20 cgd }
1196 1.20 cgd
1197 1.30 lukem static u_int32_t
1198 1.20 cgd unique_xid(ypdb)
1199 1.20 cgd struct _dom_binding *ypdb;
1200 1.20 cgd {
1201 1.30 lukem u_int32_t tmp_xid;
1202 1.20 cgd
1203 1.35 lukem tmp_xid = (u_int32_t)(((u_long)ypdb) & 0xffffffff);
1204 1.20 cgd while (xid2ypdb(tmp_xid) != NULL)
1205 1.20 cgd tmp_xid++;
1206 1.20 cgd
1207 1.20 cgd return tmp_xid;
1208 1.1 deraadt }
1209