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