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