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