2014-06-10 21:19:48 +04:00
|
|
|
/* $NetBSD: ypbind.c,v 1.98 2014/06/10 17:19:48 dholland Exp $ */
|
1996-03-31 01:49:08 +03:00
|
|
|
|
1993-05-16 06:47:20 +04:00
|
|
|
/*
|
1994-05-25 13:55:39 +04:00
|
|
|
* Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
|
1993-05-16 06:47:20 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-07 06:27:05 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-05-16 06:47:20 +04:00
|
|
|
#ifndef LINT
|
2014-06-10 21:19:48 +04:00
|
|
|
__RCSID("$NetBSD: ypbind.c,v 1.98 2014/06/10 17:19:48 dholland Exp $");
|
1993-05-16 06:47:20 +04:00
|
|
|
#endif
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <sys/types.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/signal.h>
|
|
|
|
#include <sys/socket.h>
|
1996-06-22 21:59:52 +04:00
|
|
|
#include <sys/stat.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
1999-02-12 18:04:00 +03:00
|
|
|
#include <fcntl.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <ifaddrs.h>
|
1996-10-01 04:22:27 +04:00
|
|
|
#include <limits.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <netdb.h>
|
2014-06-10 21:19:36 +04:00
|
|
|
#include <signal.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <stdarg.h>
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <stdio.h>
|
1994-12-23 19:21:49 +03:00
|
|
|
#include <stdlib.h>
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <string.h>
|
2011-05-23 06:54:53 +04:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/xdr.h>
|
|
|
|
#include <rpc/pmap_clnt.h>
|
|
|
|
#include <rpc/pmap_prot.h>
|
|
|
|
#include <rpc/pmap_rmt.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
|
1996-10-01 04:22:27 +04:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
#define YPSERVERSSUFF ".ypservers"
|
|
|
|
#define BINDINGDIR (_PATH_VAR_YP "binding")
|
|
|
|
|
1996-06-22 21:59:52 +04:00
|
|
|
#ifndef O_SHLOCK
|
|
|
|
#define O_SHLOCK 0
|
|
|
|
#endif
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
int _yp_invalid_domain(const char *); /* XXX libc internal */
|
1996-10-01 04:22:27 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// types and globals
|
|
|
|
|
|
|
|
typedef enum {
|
2011-05-24 11:00:34 +04:00
|
|
|
YPBIND_DIRECT, YPBIND_BROADCAST,
|
2011-05-24 10:56:16 +04:00
|
|
|
} ypbind_mode_t;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
enum domainstates {
|
|
|
|
DOM_NEW, /* not yet bound */
|
|
|
|
DOM_ALIVE, /* bound and healthy */
|
|
|
|
DOM_PINGING, /* ping outstanding */
|
|
|
|
DOM_LOST, /* binding timed out, looking for a new one */
|
|
|
|
DOM_DEAD, /* long-term lost, in exponential backoff */
|
|
|
|
};
|
|
|
|
|
2011-05-24 10:58:42 +04:00
|
|
|
struct domain {
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom_next;
|
2011-05-24 10:58:42 +04:00
|
|
|
|
2011-05-24 10:59:53 +04:00
|
|
|
char dom_name[YPMAXDOMAIN + 1];
|
1993-04-26 12:09:02 +04:00
|
|
|
struct sockaddr_in dom_server_addr;
|
1998-02-12 06:32:51 +03:00
|
|
|
long dom_vers;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
time_t dom_checktime; /* time of next check/contact */
|
|
|
|
time_t dom_asktime; /* time we were last DOMAIN'd */
|
|
|
|
time_t dom_losttime; /* time the binding was lost, or 0 */
|
|
|
|
unsigned dom_backofftime; /* current backoff period, when DEAD */
|
1993-04-26 12:09:02 +04:00
|
|
|
int dom_lockfd;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
enum domainstates dom_state;
|
2011-05-23 06:06:41 +04:00
|
|
|
uint32_t dom_xid;
|
2011-05-24 11:01:40 +04:00
|
|
|
FILE *dom_serversfile; /* /var/yp/binding/foo.ypservers */
|
2011-05-24 11:01:24 +04:00
|
|
|
int dom_been_ypset; /* ypset been done on this domain? */
|
2011-05-24 11:01:40 +04:00
|
|
|
ypbind_mode_t dom_ypbindmode; /* broadcast or direct */
|
1993-04-26 12:09:02 +04:00
|
|
|
};
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
#define BUFSIZE 1400
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* the list of all domains */
|
2011-05-24 10:59:07 +04:00
|
|
|
static struct domain *domains;
|
1996-06-22 21:59:52 +04:00
|
|
|
static int check;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* option settings */
|
2011-05-24 11:01:15 +04:00
|
|
|
static ypbind_mode_t default_ypbindmode;
|
2011-05-24 11:00:34 +04:00
|
|
|
static int allow_local_ypset = 0, allow_any_ypset = 0;
|
1997-07-07 06:27:05 +04:00
|
|
|
static int insecure;
|
2011-05-24 11:02:08 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* the sockets we use to interact with servers */
|
1996-06-22 21:59:52 +04:00
|
|
|
static int rpcsock, pingsock;
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/* stuff used for manually interacting with servers */
|
1996-06-22 21:59:52 +04:00
|
|
|
static struct rmtcallargs rmtca;
|
|
|
|
static struct rmtcallres rmtcr;
|
1996-07-25 22:53:43 +04:00
|
|
|
static bool_t rmtcr_outval;
|
2011-05-23 06:06:41 +04:00
|
|
|
static unsigned long rmtcr_port;
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/* The ypbind service transports */
|
1996-06-22 21:59:52 +04:00
|
|
|
static SVCXPRT *udptransp, *tcptransp;
|
|
|
|
|
2014-06-10 21:19:36 +04:00
|
|
|
/* set if we get SIGHUP */
|
|
|
|
static sig_atomic_t hupped;
|
|
|
|
|
2011-05-24 10:58:07 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// utilities
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Combo of open() and flock().
|
|
|
|
*/
|
2011-05-24 10:58:07 +04:00
|
|
|
static int
|
|
|
|
open_locked(const char *path, int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(path, flags|O_SHLOCK, mode);
|
|
|
|
if (fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#if O_SHLOCK == 0
|
|
|
|
/* dholland 20110522 wouldn't it be better to check this for error? */
|
|
|
|
(void)flock(fd, LOCK_SH);
|
|
|
|
#endif
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
/*
|
|
|
|
* Exponential backoff for pinging servers for a dead domain.
|
|
|
|
*
|
|
|
|
* We go 10 -> 20 -> 40 -> 60 seconds, then 2 -> 4 -> 8 -> 15 -> 30 ->
|
|
|
|
* 60 minutes, and stay at 60 minutes. This is overengineered.
|
|
|
|
*
|
|
|
|
* With a 60 minute max backoff the response time for when things come
|
|
|
|
* back is not awful, but we only try (and log) about 60 times even if
|
|
|
|
* things are down for a whole long weekend. This is an acceptable log
|
|
|
|
* load, I think.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
backoff(unsigned *psecs)
|
|
|
|
{
|
|
|
|
unsigned secs;
|
|
|
|
|
|
|
|
secs = *psecs;
|
|
|
|
if (secs < 60) {
|
|
|
|
secs *= 2;
|
|
|
|
if (secs > 60) {
|
|
|
|
secs = 60;
|
|
|
|
}
|
|
|
|
} else if (secs < 60 * 15) {
|
|
|
|
secs *= 2;
|
|
|
|
if (secs > 60 * 15) {
|
|
|
|
secs = 60 * 15;
|
|
|
|
}
|
|
|
|
} else if (secs < 60 * 60) {
|
|
|
|
secs *= 2;
|
|
|
|
}
|
|
|
|
*psecs = secs;
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// logging
|
|
|
|
|
1996-06-22 21:59:52 +04:00
|
|
|
#ifdef DEBUG
|
2011-05-24 10:56:16 +04:00
|
|
|
#define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
|
|
|
|
static int debug;
|
|
|
|
#else
|
|
|
|
#define DPRINTF(...)
|
1996-06-22 21:59:52 +04:00
|
|
|
#endif
|
1998-07-13 16:01:03 +04:00
|
|
|
|
2011-05-24 10:56:48 +04:00
|
|
|
static void yp_log(int, const char *, ...) __printflike(2, 3);
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Log some stuff, to syslog or stderr depending on the debug setting.
|
|
|
|
*/
|
1998-08-21 13:39:04 +04:00
|
|
|
static void
|
|
|
|
yp_log(int pri, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
2011-05-24 10:57:55 +04:00
|
|
|
if (debug) {
|
2007-07-08 02:33:57 +04:00
|
|
|
(void)vprintf(fmt, ap);
|
2011-05-24 10:57:55 +04:00
|
|
|
(void)printf("\n");
|
|
|
|
} else
|
1998-08-21 13:39:04 +04:00
|
|
|
#endif
|
|
|
|
vsyslog(pri, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:57:30 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// ypservers file
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get pathname for the ypservers file for a given domain
|
|
|
|
* (/var/yp/binding/DOMAIN.ypservers)
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
ypservers_filename(const char *domain)
|
|
|
|
{
|
|
|
|
static char ret[PATH_MAX];
|
|
|
|
|
|
|
|
(void)snprintf(ret, sizeof(ret), "%s/%s%s",
|
|
|
|
BINDINGDIR, domain, YPSERVERSSUFF);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
2011-05-24 10:58:42 +04:00
|
|
|
// struct domain
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* The state transitions of a domain work as follows:
|
2014-06-10 21:19:12 +04:00
|
|
|
*
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* in state NEW:
|
|
|
|
* nag_servers every 5 seconds
|
|
|
|
* upon answer, state is ALIVE
|
|
|
|
*
|
|
|
|
* in state ALIVE:
|
|
|
|
* every 60 seconds, send ping and switch to state PINGING
|
|
|
|
*
|
|
|
|
* in state PINGING:
|
|
|
|
* upon answer, go to state ALIVE
|
|
|
|
* if no answer in 5 seconds, go to state LOST and do nag_servers
|
|
|
|
*
|
|
|
|
* in state LOST:
|
|
|
|
* do nag_servers every 5 seconds
|
|
|
|
* upon answer, go to state ALIVE
|
|
|
|
* if no answer in 60 seconds, go to state DEAD
|
|
|
|
*
|
|
|
|
* in state DEAD
|
|
|
|
* do nag_servers every backofftime seconds (starts at 10)
|
|
|
|
* upon answer go to state ALIVE
|
|
|
|
* backofftime doubles (approximately) each try, with a cap of 1 hour
|
2014-06-10 21:19:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look up a domain by the XID we assigned it.
|
|
|
|
*/
|
2011-05-24 10:58:42 +04:00
|
|
|
static struct domain *
|
2011-05-24 10:58:54 +04:00
|
|
|
domain_find(uint32_t xid)
|
2011-05-24 10:56:16 +04:00
|
|
|
{
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
for (dom = domains; dom != NULL; dom = dom->dom_next)
|
|
|
|
if (dom->dom_xid == xid)
|
2011-05-24 10:56:16 +04:00
|
|
|
break;
|
2011-05-24 10:59:35 +04:00
|
|
|
return dom;
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Pick an XID for a domain.
|
|
|
|
*
|
|
|
|
* XXX: this should just generate a random number.
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
static uint32_t
|
2011-05-24 10:59:35 +04:00
|
|
|
unique_xid(struct domain *dom)
|
2011-05-24 10:56:16 +04:00
|
|
|
{
|
|
|
|
uint32_t tmp_xid;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
tmp_xid = ((uint32_t)(unsigned long)dom) & 0xffffffff;
|
2011-05-24 10:58:54 +04:00
|
|
|
while (domain_find(tmp_xid) != NULL)
|
2011-05-24 10:56:16 +04:00
|
|
|
tmp_xid++;
|
|
|
|
|
|
|
|
return tmp_xid;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Construct a new domain. Adds it to the global linked list of all
|
|
|
|
* domains.
|
|
|
|
*/
|
2011-05-24 10:58:42 +04:00
|
|
|
static struct domain *
|
2011-05-24 10:59:35 +04:00
|
|
|
domain_create(const char *name)
|
1996-06-22 21:59:52 +04:00
|
|
|
{
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
2011-05-24 11:01:15 +04:00
|
|
|
const char *pathname;
|
|
|
|
struct stat st;
|
1996-06-22 21:59:52 +04:00
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
dom = malloc(sizeof *dom);
|
|
|
|
if (dom == NULL) {
|
2011-05-24 10:58:42 +04:00
|
|
|
yp_log(LOG_ERR, "domain_create: Out of memory");
|
1998-08-21 13:39:04 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-06-22 21:59:52 +04:00
|
|
|
|
2011-05-24 11:00:07 +04:00
|
|
|
dom->dom_next = NULL;
|
|
|
|
|
2011-05-24 10:59:53 +04:00
|
|
|
(void)strlcpy(dom->dom_name, name, sizeof(dom->dom_name));
|
2011-05-24 11:00:07 +04:00
|
|
|
(void)memset(&dom->dom_server_addr, 0, sizeof(dom->dom_server_addr));
|
|
|
|
dom->dom_vers = YPVERS;
|
|
|
|
dom->dom_checktime = 0;
|
|
|
|
dom->dom_asktime = 0;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
dom->dom_losttime = 0;
|
|
|
|
dom->dom_backofftime = 10;
|
2011-05-24 11:00:07 +04:00
|
|
|
dom->dom_lockfd = -1;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
dom->dom_state = DOM_NEW;
|
2011-05-24 11:00:07 +04:00
|
|
|
dom->dom_xid = unique_xid(dom);
|
2011-05-24 11:01:40 +04:00
|
|
|
dom->dom_been_ypset = 0;
|
|
|
|
dom->dom_serversfile = NULL;
|
2011-05-24 11:00:07 +04:00
|
|
|
|
2011-05-24 11:01:15 +04:00
|
|
|
/*
|
|
|
|
* Per traditional ypbind(8) semantics, if a ypservers
|
|
|
|
* file does not exist, we revert to broadcast mode.
|
|
|
|
*
|
|
|
|
* The sysadmin can force broadcast mode by passing the
|
|
|
|
* -broadcast flag. There is currently no way to fail and
|
|
|
|
* reject domains for which there is no ypservers file.
|
|
|
|
*/
|
|
|
|
dom->dom_ypbindmode = default_ypbindmode;
|
|
|
|
if (dom->dom_ypbindmode == YPBIND_DIRECT) {
|
2011-05-25 08:33:52 +04:00
|
|
|
pathname = ypservers_filename(dom->dom_name);
|
2011-05-24 11:01:15 +04:00
|
|
|
if (stat(pathname, &st) < 0) {
|
|
|
|
/* XXX syslog a warning here? */
|
|
|
|
DPRINTF("%s does not exist, defaulting to broadcast\n",
|
|
|
|
pathname);
|
|
|
|
dom->dom_ypbindmode = YPBIND_BROADCAST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 11:00:07 +04:00
|
|
|
/* add to global list */
|
|
|
|
dom->dom_next = domains;
|
|
|
|
domains = dom;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
return dom;
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// locks
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Open a new binding file. Does not write the contents out; the
|
|
|
|
* caller (there's only one) does that.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static int
|
2011-05-24 10:59:35 +04:00
|
|
|
makelock(struct domain *dom)
|
1996-06-22 21:59:52 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
|
2011-05-24 10:59:53 +04:00
|
|
|
dom->dom_name, dom->dom_vers);
|
1996-06-22 21:59:52 +04:00
|
|
|
|
2011-05-24 10:58:07 +04:00
|
|
|
fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
|
|
|
|
if (fd == -1) {
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)mkdir(BINDINGDIR, 0755);
|
2011-05-24 10:58:07 +04:00
|
|
|
fd = open_locked(path, O_CREAT|O_RDWR|O_TRUNC, 0644);
|
|
|
|
if (fd == -1) {
|
1996-06-22 21:59:52 +04:00
|
|
|
return -1;
|
2011-05-24 10:58:07 +04:00
|
|
|
}
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Remove a binding file.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static void
|
2011-05-24 10:59:35 +04:00
|
|
|
removelock(struct domain *dom)
|
1996-06-22 21:59:52 +04:00
|
|
|
{
|
|
|
|
char path[MAXPATHLEN];
|
1993-04-26 12:09:02 +04:00
|
|
|
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/%s.%ld",
|
2011-05-24 10:59:53 +04:00
|
|
|
BINDINGDIR, dom->dom_name, dom->dom_vers);
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)unlink(path);
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
1996-03-31 01:49:08 +03:00
|
|
|
|
2009-11-05 22:34:06 +03:00
|
|
|
/*
|
2014-06-10 21:19:12 +04:00
|
|
|
* purge_bindingdir: remove old binding files (i.e. "rm *.[0-9]" in BINDINGDIR)
|
|
|
|
*
|
|
|
|
* The local YP functions [e.g. yp_master()] will fail without even
|
|
|
|
* talking to ypbind if there is a stale (non-flock'd) binding file
|
|
|
|
* present.
|
2009-11-05 22:34:06 +03:00
|
|
|
*
|
2014-06-10 21:19:12 +04:00
|
|
|
* We have to remove all binding files in BINDINGDIR, not just the one
|
|
|
|
* for the default domain.
|
2009-11-05 22:34:06 +03:00
|
|
|
*/
|
|
|
|
static int
|
2009-11-06 18:36:55 +03:00
|
|
|
purge_bindingdir(const char *dirpath)
|
2009-11-06 18:41:58 +03:00
|
|
|
{
|
2009-11-05 22:34:06 +03:00
|
|
|
DIR *dirp;
|
|
|
|
int unlinkedfiles, l;
|
|
|
|
struct dirent *dp;
|
|
|
|
char pathname[MAXPATHLEN];
|
|
|
|
|
|
|
|
if ((dirp = opendir(dirpath)) == NULL)
|
|
|
|
return(-1); /* at this point, shouldn't ever happen */
|
|
|
|
|
|
|
|
do {
|
|
|
|
unlinkedfiles = 0;
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
l = dp->d_namlen;
|
|
|
|
/* 'rm *.[0-9]' */
|
|
|
|
if (l > 2 && dp->d_name[l-2] == '.' &&
|
|
|
|
dp->d_name[l-1] >= '0' && dp->d_name[l-1] <= '9') {
|
|
|
|
(void)snprintf(pathname, sizeof(pathname),
|
|
|
|
"%s/%s", dirpath, dp->d_name);
|
|
|
|
if (unlink(pathname) < 0 && errno != ENOENT)
|
|
|
|
return(-1);
|
|
|
|
unlinkedfiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rescan dir if we removed it */
|
|
|
|
if (unlinkedfiles)
|
|
|
|
rewinddir(dirp);
|
|
|
|
|
|
|
|
} while (unlinkedfiles);
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// sunrpc twaddle
|
|
|
|
|
2014-06-10 21:18:18 +04:00
|
|
|
/*
|
|
|
|
* Check if the info coming in is (at least somewhat) valid.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rpc_is_valid_response(char *name, struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
if (name == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_yp_invalid_domain(name)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't support insecure servers by default */
|
|
|
|
if (!insecure && ntohs(addr->sin_port) >= IPPORT_RESERVED) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
/*
|
2014-06-10 21:19:12 +04:00
|
|
|
* Take note of the fact that we've received a reply from a ypserver.
|
|
|
|
* Or, in the case of being ypset, that we've been ypset, which
|
|
|
|
* functions much the same.
|
|
|
|
*
|
|
|
|
* Note that FORCE is set if and only if IS_YPSET is set.
|
|
|
|
*
|
|
|
|
* This function has also for the past 20+ years carried the annotation
|
|
|
|
*
|
|
|
|
* LOOPBACK IS MORE IMPORTANT: PUT IN HACK
|
|
|
|
*
|
|
|
|
* whose meaning isn't entirely clear.
|
2011-05-24 10:56:16 +04:00
|
|
|
*/
|
2011-05-24 10:56:48 +04:00
|
|
|
static void
|
2011-05-24 11:01:24 +04:00
|
|
|
rpc_received(char *dom_name, struct sockaddr_in *raddrp, int force,
|
|
|
|
int is_ypset)
|
2011-05-24 10:56:16 +04:00
|
|
|
{
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
2011-05-24 10:56:16 +04:00
|
|
|
struct iovec iov[2];
|
|
|
|
struct ypbind_resp ybr;
|
2011-05-24 10:57:55 +04:00
|
|
|
ssize_t result;
|
2011-05-24 10:56:16 +04:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
DPRINTF("returned from %s about %s\n",
|
2011-05-24 10:59:35 +04:00
|
|
|
inet_ntoa(raddrp->sin_addr), dom_name);
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* validate some stuff */
|
2014-06-10 21:18:18 +04:00
|
|
|
if (!rpc_is_valid_response(dom_name, raddrp)) {
|
2011-05-24 10:56:16 +04:00
|
|
|
return;
|
2014-06-10 21:18:18 +04:00
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* look for the domain */
|
2011-05-24 10:59:35 +04:00
|
|
|
for (dom = domains; dom != NULL; dom = dom->dom_next)
|
2011-05-24 10:59:53 +04:00
|
|
|
if (!strcmp(dom->dom_name, dom_name))
|
2011-05-24 10:56:16 +04:00
|
|
|
break;
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* if not found, create it, but only if FORCE; otherwise ignore */
|
2011-05-24 10:59:35 +04:00
|
|
|
if (dom == NULL) {
|
2011-05-24 10:56:16 +04:00
|
|
|
if (force == 0)
|
|
|
|
return;
|
2011-05-24 10:59:35 +04:00
|
|
|
dom = domain_create(dom_name);
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* the domain needs to know if it's been explicitly ypset */
|
2011-05-24 11:01:24 +04:00
|
|
|
if (is_ypset) {
|
|
|
|
dom->dom_been_ypset = 1;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* If the domain is alive and we aren't being called by ypset,
|
|
|
|
* we shouldn't be getting a response at all. Log it, as it
|
|
|
|
* might be hostile.
|
|
|
|
*/
|
|
|
|
if (dom->dom_state == DOM_ALIVE && force == 0) {
|
|
|
|
if (!memcmp(&dom->dom_server_addr, raddrp,
|
|
|
|
sizeof(dom->dom_server_addr))) {
|
|
|
|
yp_log(LOG_WARNING,
|
|
|
|
"Unexpected reply from server %s for domain %s",
|
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr),
|
|
|
|
dom->dom_name);
|
|
|
|
} else {
|
|
|
|
yp_log(LOG_WARNING,
|
|
|
|
"Falsified reply from %s for domain %s",
|
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr),
|
|
|
|
dom->dom_name);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're expected a ping response, and we've got it
|
|
|
|
* (meaning we aren't being called by ypset), we don't need to
|
|
|
|
* do anything.
|
2014-06-10 21:19:12 +04:00
|
|
|
*/
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (dom->dom_state == DOM_PINGING && force == 0) {
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* If the reply came from the server we expect, set
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* dom_state back to ALIVE and ping again in 60
|
|
|
|
* seconds.
|
2014-06-10 21:19:12 +04:00
|
|
|
*
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* If it came from somewhere else, log it.
|
2014-06-10 21:19:12 +04:00
|
|
|
*/
|
2011-05-24 10:59:35 +04:00
|
|
|
if (!memcmp(&dom->dom_server_addr, raddrp,
|
|
|
|
sizeof(dom->dom_server_addr))) {
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
dom->dom_state = DOM_ALIVE;
|
2011-05-24 10:56:16 +04:00
|
|
|
/* recheck binding in 60 sec */
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_checktime = time(NULL) + 60;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
} else {
|
|
|
|
yp_log(LOG_WARNING,
|
|
|
|
"Falsified reply from %s for domain %s",
|
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr),
|
|
|
|
dom->dom_name);
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-06-10 21:19:12 +04:00
|
|
|
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
#ifdef HEURISTIC
|
|
|
|
/*
|
|
|
|
* If transitioning to the alive state from a non-alive state,
|
|
|
|
* clear dom_asktime. This will help prevent any requests that
|
|
|
|
* are still coming in from triggering unnecessary pings via
|
|
|
|
* the HEURISTIC code.
|
|
|
|
*
|
|
|
|
* XXX: this may not be an adequate measure; we may need to
|
|
|
|
* keep more state so we can disable the HEURISTIC code for
|
|
|
|
* the first few seconds after rebinding.
|
|
|
|
*/
|
|
|
|
if (dom->dom_state == DOM_NEW ||
|
|
|
|
dom->dom_state == DOM_LOST ||
|
|
|
|
dom->dom_state == DOM_DEAD) {
|
|
|
|
dom->dom_asktime = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Take the address we got the message from (or in the case of
|
|
|
|
* ypset, the explicit address we were given) as the server
|
|
|
|
* address for this domain, mark the domain alive, and we'll
|
|
|
|
* check it again in 60 seconds.
|
|
|
|
*
|
|
|
|
* XXX: it looks like if we get a random unsolicited reply
|
|
|
|
* from somewhere, we'll silently switch to that server
|
|
|
|
* address, regardless of merit.
|
|
|
|
*
|
|
|
|
* 1. If we have a foo.ypservers file the address should be
|
|
|
|
* checked against it and rejected if it's not one of the
|
|
|
|
* addresses of one of the listed hostnames. Note that it
|
|
|
|
* might not be the same address we sent to; even fairly smart
|
|
|
|
* UDP daemons don't always handle multihomed hosts correctly
|
|
|
|
* and we can't expect sunrpc code to do anything intelligent
|
|
|
|
* at all.
|
|
|
|
*
|
|
|
|
* 2. If we're in broadcast mode the address should be
|
|
|
|
* checked against the local addresses and netmasks so we
|
|
|
|
* don't accept responses from Mars.
|
|
|
|
*
|
|
|
|
* 2a. If we're in broadcast mode and we've been ypset, we
|
|
|
|
* should not accept anything else until we drop the ypset
|
|
|
|
* state for not responding.
|
|
|
|
*
|
|
|
|
* 3. Either way we should not accept a response from an
|
|
|
|
* arbitrary host unless we don't currently have a binding.
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
* (This is now fixed above.)
|
2014-06-10 21:19:12 +04:00
|
|
|
*
|
|
|
|
* Note that for a random unsolicited reply to work it has to
|
|
|
|
* carry the XID of one of the domains we know about; but
|
|
|
|
* those values are predictable.
|
|
|
|
*/
|
2011-05-24 10:59:35 +04:00
|
|
|
(void)memcpy(&dom->dom_server_addr, raddrp,
|
|
|
|
sizeof(dom->dom_server_addr));
|
2011-05-24 10:56:16 +04:00
|
|
|
/* recheck binding in 60 seconds */
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_checktime = time(NULL) + 60;
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
dom->dom_state = DOM_ALIVE;
|
|
|
|
|
|
|
|
/* Clear the dead/backoff state. */
|
|
|
|
dom->dom_losttime = 0;
|
|
|
|
dom->dom_backofftime = 10;
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2014-06-10 21:19:48 +04:00
|
|
|
if (is_ypset == 0) {
|
|
|
|
yp_log(LOG_NOTICE, "Domain %s is alive; server %s",
|
|
|
|
dom->dom_name,
|
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr));
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Generate a new binding file. If this fails, forget about it.
|
|
|
|
* (But we keep the binding and we'll report it to anyone who
|
|
|
|
* asks via the ypbind service.) XXX: this will interact badly,
|
|
|
|
* maybe very badly, with the code in HEURISTIC.
|
|
|
|
*
|
|
|
|
* Note that makelock() doesn't log on failure.
|
|
|
|
*/
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
if (dom->dom_lockfd != -1)
|
|
|
|
(void)close(dom->dom_lockfd);
|
2011-05-24 10:56:16 +04:00
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
if ((fd = makelock(dom)) == -1)
|
2011-05-24 10:56:16 +04:00
|
|
|
return;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_lockfd = fd;
|
2011-05-24 10:56:16 +04:00
|
|
|
|
|
|
|
iov[0].iov_base = &(udptransp->xp_port);
|
|
|
|
iov[0].iov_len = sizeof udptransp->xp_port;
|
|
|
|
iov[1].iov_base = &ybr;
|
|
|
|
iov[1].iov_len = sizeof ybr;
|
|
|
|
|
|
|
|
(void)memset(&ybr, 0, sizeof ybr);
|
|
|
|
ybr.ypbind_status = YPBIND_SUCC_VAL;
|
|
|
|
ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
|
|
|
|
raddrp->sin_addr;
|
|
|
|
ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
|
|
|
|
raddrp->sin_port;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
result = writev(dom->dom_lockfd, iov, 2);
|
2011-05-24 10:57:55 +04:00
|
|
|
if (result < 0 || (size_t)result != iov[0].iov_len + iov[1].iov_len) {
|
|
|
|
if (result < 0)
|
|
|
|
yp_log(LOG_WARNING, "writev: %s", strerror(errno));
|
|
|
|
else
|
|
|
|
yp_log(LOG_WARNING, "writev: short count");
|
2011-05-24 10:59:35 +04:00
|
|
|
(void)close(dom->dom_lockfd);
|
|
|
|
removelock(dom);
|
|
|
|
dom->dom_lockfd = -1;
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* The NULL call: do nothing. This is obliged to exist because of
|
|
|
|
* sunrpc silliness.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static void *
|
2007-07-08 02:33:57 +04:00
|
|
|
/*ARGSUSED*/
|
2002-07-07 01:44:40 +04:00
|
|
|
ypbindproc_null_2(SVCXPRT *transp, void *argp)
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
|
|
|
static char res;
|
|
|
|
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("ypbindproc_null_2\n");
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&res, 0, sizeof(res));
|
1993-04-26 12:09:02 +04:00
|
|
|
return (void *)&res;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* The DOMAIN call: look up the ypserver for a specified domain.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static void *
|
2007-07-08 02:33:57 +04:00
|
|
|
/*ARGSUSED*/
|
2002-07-07 01:44:40 +04:00
|
|
|
ypbindproc_domain_2(SVCXPRT *transp, void *argp)
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
|
|
|
static struct ypbind_resp res;
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
1996-06-22 21:59:52 +04:00
|
|
|
char *arg = *(char **) argp;
|
1994-07-02 10:45:48 +04:00
|
|
|
time_t now;
|
1997-07-07 06:27:05 +04:00
|
|
|
int count;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("ypbindproc_domain_2 %s\n", arg);
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/* Reject invalid domains. */
|
1997-07-07 06:27:05 +04:00
|
|
|
if (_yp_invalid_domain(arg))
|
|
|
|
return NULL;
|
|
|
|
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&res, 0, sizeof res);
|
1993-04-26 12:09:02 +04:00
|
|
|
res.ypbind_status = YPBIND_FAIL_VAL;
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Look for the domain. XXX: Behave erratically if we have
|
|
|
|
* more than 100 domains. The intent here is to avoid allowing
|
|
|
|
* arbitrary incoming requests to create more than 100
|
|
|
|
* domains; but this logic means that if we legitimately have
|
|
|
|
* more than 100 (e.g. via ypset) we'll only actually bind the
|
|
|
|
* first 100 and the rest will fail. The test on 'count' should
|
|
|
|
* be moved further down.
|
|
|
|
*/
|
2011-05-24 10:59:35 +04:00
|
|
|
for (count = 0, dom = domains;
|
|
|
|
dom != NULL;
|
|
|
|
dom = dom->dom_next, count++) {
|
1997-07-07 06:27:05 +04:00
|
|
|
if (count > 100)
|
|
|
|
return NULL; /* prevent denial of service */
|
2011-05-24 10:59:53 +04:00
|
|
|
if (!strcmp(dom->dom_name, arg))
|
1993-04-26 12:09:02 +04:00
|
|
|
break;
|
1997-07-07 06:27:05 +04:00
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* If the domain doesn't exist, create it, then fail the call
|
|
|
|
* because we have no information yet.
|
|
|
|
*
|
|
|
|
* Set "check" so that checkwork() will run and look for a
|
|
|
|
* server.
|
|
|
|
*
|
|
|
|
* XXX: like during startup there's a spurious call to
|
|
|
|
* removelock() after domain_create().
|
|
|
|
*/
|
2011-05-24 10:59:35 +04:00
|
|
|
if (dom == NULL) {
|
|
|
|
dom = domain_create(arg);
|
|
|
|
removelock(dom);
|
1993-04-26 12:09:02 +04:00
|
|
|
check++;
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("unknown domain %s\n", arg);
|
1993-04-26 12:09:02 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (dom->dom_state == DOM_NEW) {
|
|
|
|
DPRINTF("new domain %s\n", arg);
|
1993-04-26 12:09:02 +04:00
|
|
|
return NULL;
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
1994-07-02 10:45:48 +04:00
|
|
|
#ifdef HEURISTIC
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Keep track of the last time we were explicitly asked about
|
|
|
|
* this domain. If it happens a lot, force a ping. This works
|
|
|
|
* (or "works") because we only get asked specifically when
|
|
|
|
* things aren't going; otherwise the client code in libc and
|
|
|
|
* elsewhere uses the binding file.
|
|
|
|
*
|
|
|
|
* Note: HEURISTIC is enabled by default.
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
*
|
|
|
|
* dholland 20140609: I think this is part of the mechanism
|
|
|
|
* that causes ypbind to spam. I'm changing this logic so it
|
|
|
|
* only triggers when the state is DOM_ALIVE: if the domain
|
|
|
|
* is new, lost, or dead we shouldn't send more requests than
|
|
|
|
* the ones already scheduled, and if we're already in the
|
|
|
|
* middle of pinging there's no point doing it again.
|
2014-06-10 21:19:12 +04:00
|
|
|
*/
|
2007-07-08 02:33:57 +04:00
|
|
|
(void)time(&now);
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (dom->dom_state == DOM_ALIVE && now < dom->dom_asktime + 5) {
|
1993-04-26 12:09:02 +04:00
|
|
|
/*
|
1994-07-02 10:45:48 +04:00
|
|
|
* Hmm. More than 2 requests in 5 seconds have indicated
|
|
|
|
* that my binding is possibly incorrect.
|
|
|
|
* Ok, do an immediate poll of the server.
|
1993-04-26 12:09:02 +04:00
|
|
|
*/
|
2011-05-24 10:59:35 +04:00
|
|
|
if (dom->dom_checktime >= now) {
|
1994-07-02 10:45:48 +04:00
|
|
|
/* don't flood it */
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_checktime = 0;
|
1994-07-02 10:45:48 +04:00
|
|
|
check++;
|
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_asktime = now;
|
1993-04-26 12:09:02 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
res.ypbind_status = YPBIND_SUCC_VAL;
|
|
|
|
res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_server_addr.sin_addr.s_addr;
|
1993-04-26 12:09:02 +04:00
|
|
|
res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_server_addr.sin_port;
|
2011-05-24 10:59:53 +04:00
|
|
|
DPRINTF("domain %s at %s/%d\n", dom->dom_name,
|
2011-05-24 10:59:35 +04:00
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr),
|
|
|
|
ntohs(dom->dom_server_addr.sin_port));
|
1993-04-26 12:09:02 +04:00
|
|
|
return &res;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* The SETDOM call: ypset.
|
|
|
|
*
|
|
|
|
* Unless -ypsetme was given on the command line, this is rejected;
|
|
|
|
* even then it's only allowed from localhost unless -ypset was
|
|
|
|
* given on the command line.
|
|
|
|
*
|
|
|
|
* Allowing anyone anywhere to ypset you (and therefore provide your
|
|
|
|
* password file and such) is a horrible thing and it isn't clear to
|
|
|
|
* me why this functionality even exists.
|
|
|
|
*
|
|
|
|
* ypset from localhost has some but limited utility.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static void *
|
2002-07-07 01:44:40 +04:00
|
|
|
ypbindproc_setdom_2(SVCXPRT *transp, void *argp)
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
1996-06-22 21:59:52 +04:00
|
|
|
struct ypbind_setdom *sd = argp;
|
1993-04-26 12:09:02 +04:00
|
|
|
struct sockaddr_in *fromsin, bindsin;
|
1994-09-28 04:39:54 +03:00
|
|
|
static bool_t res;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&res, 0, sizeof(res));
|
1993-04-26 12:09:02 +04:00
|
|
|
fromsin = svc_getcaller(transp);
|
2011-05-25 08:59:22 +04:00
|
|
|
DPRINTF("ypbindproc_setdom_2 from %s\n", inet_ntoa(fromsin->sin_addr));
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Reject unless enabled.
|
|
|
|
*/
|
|
|
|
|
2011-05-24 11:00:34 +04:00
|
|
|
if (allow_any_ypset) {
|
|
|
|
/* nothing */
|
|
|
|
} else if (allow_local_ypset) {
|
1996-06-22 21:59:52 +04:00
|
|
|
if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
|
2011-05-24 11:00:34 +04:00
|
|
|
DPRINTF("ypset denied from %s\n",
|
2011-05-23 06:36:35 +04:00
|
|
|
inet_ntoa(fromsin->sin_addr));
|
1996-06-22 21:59:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-24 11:00:34 +04:00
|
|
|
} else {
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("ypset denied\n");
|
1996-06-22 21:59:52 +04:00
|
|
|
return NULL;
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* Make a "security" check. */
|
1996-06-22 21:59:52 +04:00
|
|
|
if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED) {
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("ypset from unprivileged port denied\n");
|
1994-09-28 04:39:54 +03:00
|
|
|
return &res;
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
1993-11-15 11:20:05 +03:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* Ignore requests we don't understand. */
|
1996-06-22 21:59:52 +04:00
|
|
|
if (sd->ypsetdom_vers != YPVERS) {
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("ypset with wrong version denied\n");
|
1994-09-28 04:39:54 +03:00
|
|
|
return &res;
|
1996-06-22 21:59:52 +04:00
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Fetch the arguments out of the xdr-decoded blob and call
|
|
|
|
* rpc_received(), setting FORCE so that the domain will be
|
|
|
|
* created if we don't already know about it, and also saying
|
|
|
|
* that it's actually a ypset.
|
|
|
|
*
|
|
|
|
* Effectively we're telilng rpc_received() that we got an
|
|
|
|
* RPC response from the server specified by ypset.
|
|
|
|
*/
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&bindsin, 0, sizeof bindsin);
|
1993-04-26 12:09:02 +04:00
|
|
|
bindsin.sin_family = AF_INET;
|
1995-01-15 11:55:59 +03:00
|
|
|
bindsin.sin_len = sizeof(bindsin);
|
1996-06-22 21:59:52 +04:00
|
|
|
bindsin.sin_addr = sd->ypsetdom_addr;
|
|
|
|
bindsin.sin_port = sd->ypsetdom_port;
|
2011-05-24 11:01:24 +04:00
|
|
|
rpc_received(sd->ypsetdom_domain, &bindsin, 1, 1);
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-24 11:01:53 +04:00
|
|
|
DPRINTF("ypset to %s for domain %s succeeded\n",
|
|
|
|
inet_ntoa(bindsin.sin_addr), sd->ypsetdom_domain);
|
1993-04-26 12:09:02 +04:00
|
|
|
res = 1;
|
1994-09-28 04:39:54 +03:00
|
|
|
return &res;
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Dispatcher for the ypbind service.
|
|
|
|
*
|
|
|
|
* There are three calls: NULL, which does nothing, DOMAIN, which
|
|
|
|
* gets the binding for a particular domain, and SETDOM, which
|
|
|
|
* does ypset.
|
|
|
|
*/
|
1993-04-26 12:09:02 +04:00
|
|
|
static void
|
2002-07-07 01:44:40 +04:00
|
|
|
ypbindprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
|
|
|
union {
|
1996-06-04 00:32:55 +04:00
|
|
|
char ypbindproc_domain_2_arg[YPMAXDOMAIN + 1];
|
1993-04-26 12:09:02 +04:00
|
|
|
struct ypbind_setdom ypbindproc_setdom_2_arg;
|
2006-10-05 13:18:11 +04:00
|
|
|
void *alignment;
|
1993-04-26 12:09:02 +04:00
|
|
|
} argument;
|
|
|
|
struct authunix_parms *creds;
|
|
|
|
char *result;
|
1996-06-22 21:59:52 +04:00
|
|
|
xdrproc_t xdr_argument, xdr_result;
|
2002-07-07 01:44:40 +04:00
|
|
|
void *(*local)(SVCXPRT *, void *);
|
1993-04-26 12:09:02 +04:00
|
|
|
|
|
|
|
switch (rqstp->rq_proc) {
|
|
|
|
case YPBINDPROC_NULL:
|
2011-08-30 21:06:20 +04:00
|
|
|
xdr_argument = (xdrproc_t)xdr_void;
|
|
|
|
xdr_result = (xdrproc_t)xdr_void;
|
1996-06-22 21:59:52 +04:00
|
|
|
local = ypbindproc_null_2;
|
1993-04-26 12:09:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case YPBINDPROC_DOMAIN:
|
2011-08-30 21:06:20 +04:00
|
|
|
xdr_argument = (xdrproc_t)xdr_ypdomain_wrap_string;
|
|
|
|
xdr_result = (xdrproc_t)xdr_ypbind_resp;
|
1996-06-22 21:59:52 +04:00
|
|
|
local = ypbindproc_domain_2;
|
1993-04-26 12:09:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case YPBINDPROC_SETDOM:
|
1995-01-15 11:55:59 +03:00
|
|
|
switch (rqstp->rq_cred.oa_flavor) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case AUTH_UNIX:
|
|
|
|
creds = (struct authunix_parms *)rqstp->rq_clntcred;
|
1995-01-15 11:55:59 +03:00
|
|
|
if (creds->aup_uid != 0) {
|
1993-04-26 12:09:02 +04:00
|
|
|
svcerr_auth(transp, AUTH_BADCRED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
svcerr_auth(transp, AUTH_TOOWEAK);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-30 21:06:20 +04:00
|
|
|
xdr_argument = (xdrproc_t)xdr_ypbind_setdom;
|
|
|
|
xdr_result = (xdrproc_t)xdr_void;
|
1996-06-22 21:59:52 +04:00
|
|
|
local = ypbindproc_setdom_2;
|
1993-04-26 12:09:02 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
svcerr_noproc(transp);
|
|
|
|
return;
|
|
|
|
}
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&argument, 0, sizeof(argument));
|
2007-07-08 02:33:57 +04:00
|
|
|
if (!svc_getargs(transp, xdr_argument, (caddr_t)(void *)&argument)) {
|
1993-04-26 12:09:02 +04:00
|
|
|
svcerr_decode(transp);
|
|
|
|
return;
|
|
|
|
}
|
1996-06-22 21:59:52 +04:00
|
|
|
result = (*local)(transp, &argument);
|
1993-04-26 12:09:02 +04:00
|
|
|
if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
|
|
|
|
svcerr_systemerr(transp);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Set up sunrpc stuff.
|
|
|
|
*
|
|
|
|
* This sets up the ypbind service (both TCP and UDP) and also opens
|
|
|
|
* the sockets we use for talking to ypservers.
|
|
|
|
*/
|
2011-05-24 10:58:19 +04:00
|
|
|
static void
|
|
|
|
sunrpc_setup(void)
|
|
|
|
{
|
|
|
|
int one;
|
|
|
|
|
|
|
|
(void)pmap_unset(YPBINDPROG, YPBINDVERS);
|
|
|
|
|
|
|
|
udptransp = svcudp_create(RPC_ANYSOCK);
|
|
|
|
if (udptransp == NULL)
|
|
|
|
errx(1, "Cannot create udp service.");
|
|
|
|
|
|
|
|
if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
|
|
|
|
IPPROTO_UDP))
|
|
|
|
errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, udp).");
|
|
|
|
|
|
|
|
tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
|
|
|
|
if (tcptransp == NULL)
|
|
|
|
errx(1, "Cannot create tcp service.");
|
|
|
|
|
|
|
|
if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
|
|
|
|
IPPROTO_TCP))
|
|
|
|
errx(1, "Unable to register (YPBINDPROG, YPBINDVERS, tcp).");
|
|
|
|
|
|
|
|
/* XXX use SOCK_STREAM for direct queries? */
|
|
|
|
if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
err(1, "rpc socket");
|
|
|
|
if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
|
|
|
|
err(1, "ping socket");
|
|
|
|
|
|
|
|
(void)fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
|
|
|
|
(void)fcntl(pingsock, F_SETFL, fcntl(pingsock, F_GETFL, 0) | FNDELAY);
|
|
|
|
|
|
|
|
one = 1;
|
|
|
|
(void)setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one,
|
|
|
|
(socklen_t)sizeof(one));
|
|
|
|
rmtca.prog = YPPROG;
|
|
|
|
rmtca.vers = YPVERS;
|
|
|
|
rmtca.proc = YPPROC_DOMAIN_NONACK;
|
|
|
|
rmtca.xdr_args = NULL; /* set at call time */
|
|
|
|
rmtca.args_ptr = NULL; /* set at call time */
|
|
|
|
rmtcr.port_ptr = &rmtcr_port;
|
2011-08-30 21:06:20 +04:00
|
|
|
rmtcr.xdr_results = (xdrproc_t)xdr_bool;
|
2011-05-24 10:58:19 +04:00
|
|
|
rmtcr.results_ptr = (caddr_t)(void *)&rmtcr_outval;
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// operational logic
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Broadcast an RPC packet to hopefully contact some servers for a
|
|
|
|
* domain.
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
static int
|
|
|
|
broadcast(char *buf, int outlen)
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct sockaddr_in bindsin;
|
|
|
|
struct in_addr in;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
(void)memset(&bindsin, 0, sizeof bindsin);
|
|
|
|
bindsin.sin_family = AF_INET;
|
|
|
|
bindsin.sin_len = sizeof(bindsin);
|
|
|
|
bindsin.sin_port = htons(PMAPPORT);
|
1996-10-01 04:22:27 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
if (getifaddrs(&ifap) != 0) {
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "broadcast: getifaddrs: %s",
|
|
|
|
strerror(errno));
|
2011-05-24 10:56:16 +04:00
|
|
|
return (-1);
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
if (ifa->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
if ((ifa->ifa_flags & IFF_UP) == 0)
|
|
|
|
continue;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
switch (ifa->ifa_flags & (IFF_LOOPBACK | IFF_BROADCAST)) {
|
|
|
|
case IFF_BROADCAST:
|
|
|
|
if (!ifa->ifa_broadaddr)
|
|
|
|
continue;
|
|
|
|
if (ifa->ifa_broadaddr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
in = ((struct sockaddr_in *)(void *)ifa->ifa_broadaddr)->sin_addr;
|
|
|
|
break;
|
|
|
|
case IFF_LOOPBACK:
|
|
|
|
in = ((struct sockaddr_in *)(void *)ifa->ifa_addr)->sin_addr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
bindsin.sin_addr = in;
|
|
|
|
DPRINTF("broadcast %x\n", bindsin.sin_addr.s_addr);
|
|
|
|
if (sendto(rpcsock, buf, outlen, 0,
|
|
|
|
(struct sockaddr *)(void *)&bindsin,
|
|
|
|
(socklen_t)bindsin.sin_len) == -1)
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "broadcast: sendto: %s",
|
|
|
|
strerror(errno));
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
return (0);
|
|
|
|
}
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Send an RPC packet to all the configured (in /var/yp/foo.ypservers)
|
|
|
|
* servers for a domain.
|
|
|
|
*
|
|
|
|
* XXX: we should read and parse the file up front and reread it only
|
|
|
|
* if it changes.
|
|
|
|
*/
|
1996-10-01 04:22:27 +04:00
|
|
|
static int
|
2011-05-24 11:01:40 +04:00
|
|
|
direct(char *buf, int outlen, struct domain *dom)
|
1996-10-01 04:22:27 +04:00
|
|
|
{
|
2011-05-24 10:57:30 +04:00
|
|
|
const char *path;
|
1996-10-01 04:22:27 +04:00
|
|
|
char line[_POSIX2_LINE_MAX];
|
|
|
|
char *p;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct sockaddr_in bindsin;
|
|
|
|
int i, count = 0;
|
|
|
|
|
2011-05-24 11:01:40 +04:00
|
|
|
/*
|
|
|
|
* XXX what happens if someone's editor unlinks and replaces
|
|
|
|
* the servers file?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dom->dom_serversfile != NULL) {
|
|
|
|
rewind(dom->dom_serversfile);
|
|
|
|
} else {
|
|
|
|
path = ypservers_filename(dom->dom_name);
|
|
|
|
dom->dom_serversfile = fopen(path, "r");
|
|
|
|
if (dom->dom_serversfile == NULL) {
|
|
|
|
/*
|
|
|
|
* XXX there should be a time restriction on
|
|
|
|
* this (and/or on trying the open) so we
|
|
|
|
* don't flood the log. Or should we fall back
|
|
|
|
* to broadcast mode?
|
|
|
|
*/
|
|
|
|
yp_log(LOG_ERR, "%s: %s", path,
|
2011-05-24 10:57:55 +04:00
|
|
|
strerror(errno));
|
2011-05-24 11:01:40 +04:00
|
|
|
return -1;
|
1998-08-21 13:39:04 +04:00
|
|
|
}
|
1996-10-01 04:22:27 +04:00
|
|
|
}
|
|
|
|
|
2007-07-08 02:33:57 +04:00
|
|
|
(void)memset(&bindsin, 0, sizeof bindsin);
|
1996-10-01 04:22:27 +04:00
|
|
|
bindsin.sin_family = AF_INET;
|
|
|
|
bindsin.sin_len = sizeof(bindsin);
|
|
|
|
bindsin.sin_port = htons(PMAPPORT);
|
|
|
|
|
2011-05-24 11:01:40 +04:00
|
|
|
while (fgets(line, (int)sizeof(line), dom->dom_serversfile) != NULL) {
|
1996-10-01 04:22:27 +04:00
|
|
|
/* skip lines that are too big */
|
|
|
|
p = strchr(line, '\n');
|
|
|
|
if (p == NULL) {
|
|
|
|
int c;
|
|
|
|
|
2011-05-24 11:01:40 +04:00
|
|
|
while ((c = getc(dom->dom_serversfile)) != '\n' && c != EOF)
|
1996-10-01 04:22:27 +04:00
|
|
|
;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
p = line;
|
2004-10-30 19:51:20 +04:00
|
|
|
while (isspace((unsigned char)*p))
|
1996-10-01 04:22:27 +04:00
|
|
|
p++;
|
|
|
|
if (*p == '#')
|
|
|
|
continue;
|
|
|
|
hp = gethostbyname(p);
|
|
|
|
if (!hp) {
|
2001-01-11 04:47:55 +03:00
|
|
|
yp_log(LOG_WARNING, "%s: %s", p, hstrerror(h_errno));
|
1996-10-01 04:22:27 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* step through all addresses in case first is unavailable */
|
|
|
|
for (i = 0; hp->h_addr_list[i]; i++) {
|
2007-07-08 02:33:57 +04:00
|
|
|
(void)memcpy(&bindsin.sin_addr, hp->h_addr_list[0],
|
1996-10-01 04:22:27 +04:00
|
|
|
hp->h_length);
|
|
|
|
if (sendto(rpcsock, buf, outlen, 0,
|
2007-07-08 02:33:57 +04:00
|
|
|
(struct sockaddr *)(void *)&bindsin,
|
|
|
|
(socklen_t)sizeof(bindsin)) < 0) {
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "direct: sendto: %s",
|
|
|
|
strerror(errno));
|
1996-10-01 04:22:27 +04:00
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
1998-08-21 13:39:04 +04:00
|
|
|
if (!count) {
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "No contactable servers found in %s",
|
2011-05-24 11:01:40 +04:00
|
|
|
ypservers_filename(dom->dom_name));
|
1998-08-21 13:39:04 +04:00
|
|
|
return -1;
|
|
|
|
}
|
1996-10-01 04:22:27 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Send an RPC packet to the server that's been selected with ypset.
|
|
|
|
* (This is only used when in broadcast mode and when ypset is
|
|
|
|
* allowed.)
|
|
|
|
*/
|
1996-10-01 04:22:27 +04:00
|
|
|
static int
|
2011-05-24 10:59:35 +04:00
|
|
|
direct_set(char *buf, int outlen, struct domain *dom)
|
1996-10-01 04:22:27 +04:00
|
|
|
{
|
|
|
|
struct sockaddr_in bindsin;
|
|
|
|
char path[MAXPATHLEN];
|
|
|
|
struct iovec iov[2];
|
|
|
|
struct ypbind_resp ybr;
|
|
|
|
SVCXPRT dummy_svc;
|
2007-07-08 02:33:57 +04:00
|
|
|
int fd;
|
|
|
|
ssize_t bytes;
|
1996-10-01 04:22:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Gack, we lose if binding file went away. We reset
|
|
|
|
* "been_set" if this happens, otherwise we'll never
|
|
|
|
* bind again.
|
|
|
|
*/
|
2007-07-08 02:33:57 +04:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/%s.%ld", BINDINGDIR,
|
2011-05-24 10:59:53 +04:00
|
|
|
dom->dom_name, dom->dom_vers);
|
1996-10-01 04:22:27 +04:00
|
|
|
|
2011-05-24 10:58:07 +04:00
|
|
|
fd = open_locked(path, O_RDONLY, 0644);
|
|
|
|
if (fd == -1) {
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
|
2011-05-24 11:01:24 +04:00
|
|
|
dom->dom_been_ypset = 0;
|
1996-10-01 04:22:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the binding file... */
|
2007-07-08 02:33:57 +04:00
|
|
|
iov[0].iov_base = &(dummy_svc.xp_port);
|
1996-10-01 04:22:27 +04:00
|
|
|
iov[0].iov_len = sizeof(dummy_svc.xp_port);
|
2007-07-08 02:33:57 +04:00
|
|
|
iov[1].iov_base = &ybr;
|
1996-10-01 04:22:27 +04:00
|
|
|
iov[1].iov_len = sizeof(ybr);
|
|
|
|
bytes = readv(fd, iov, 2);
|
|
|
|
(void)close(fd);
|
2011-05-24 10:57:55 +04:00
|
|
|
if (bytes <0 || (size_t)bytes != (iov[0].iov_len + iov[1].iov_len)) {
|
1996-10-01 04:22:27 +04:00
|
|
|
/* Binding file corrupt? */
|
2011-05-24 10:57:55 +04:00
|
|
|
if (bytes < 0)
|
|
|
|
yp_log(LOG_WARNING, "%s: %s", path, strerror(errno));
|
|
|
|
else
|
|
|
|
yp_log(LOG_WARNING, "%s: short read", path);
|
2011-05-24 11:01:24 +04:00
|
|
|
dom->dom_been_ypset = 0;
|
1996-10-01 04:22:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bindsin.sin_addr =
|
|
|
|
ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
|
|
|
|
|
2007-07-08 02:33:57 +04:00
|
|
|
if (sendto(rpcsock, buf, outlen, 0,
|
|
|
|
(struct sockaddr *)(void *)&bindsin,
|
|
|
|
(socklen_t)sizeof(bindsin)) < 0) {
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "direct_set: sendto: %s", strerror(errno));
|
1996-10-01 04:22:27 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Receive and dispatch packets on the general RPC socket.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static enum clnt_stat
|
2002-07-07 01:44:40 +04:00
|
|
|
handle_replies(void)
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
1996-06-22 21:59:52 +04:00
|
|
|
char buf[BUFSIZE];
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t fromlen;
|
2007-07-08 02:33:57 +04:00
|
|
|
ssize_t inlen;
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
1993-04-26 12:09:02 +04:00
|
|
|
struct sockaddr_in raddr;
|
|
|
|
struct rpc_msg msg;
|
|
|
|
XDR xdr;
|
|
|
|
|
|
|
|
recv_again:
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("handle_replies receiving\n");
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&xdr, 0, sizeof(xdr));
|
|
|
|
(void)memset(&msg, 0, sizeof(msg));
|
1993-04-26 12:09:02 +04:00
|
|
|
msg.acpted_rply.ar_verf = _null_auth;
|
2007-07-08 02:33:57 +04:00
|
|
|
msg.acpted_rply.ar_results.where = (caddr_t)(void *)&rmtcr;
|
2011-08-30 21:06:20 +04:00
|
|
|
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_rmtcallres;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
|
|
|
try_again:
|
1998-07-13 16:01:03 +04:00
|
|
|
fromlen = sizeof(struct sockaddr);
|
1993-04-26 12:09:02 +04:00
|
|
|
inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
|
2007-07-08 02:33:57 +04:00
|
|
|
(struct sockaddr *)(void *)&raddr, &fromlen);
|
1995-01-15 11:55:59 +03:00
|
|
|
if (inlen < 0) {
|
|
|
|
if (errno == EINTR)
|
1993-04-26 12:09:02 +04:00
|
|
|
goto try_again;
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("handle_replies: recvfrom failed (%s)\n",
|
|
|
|
strerror(errno));
|
1993-04-26 12:09:02 +04:00
|
|
|
return RPC_CANTRECV;
|
|
|
|
}
|
2011-05-23 06:06:41 +04:00
|
|
|
if ((size_t)inlen < sizeof(uint32_t))
|
1993-04-26 12:09:02 +04:00
|
|
|
goto recv_again;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* see if reply transaction id matches sent id.
|
|
|
|
* If so, decode the results.
|
|
|
|
*/
|
2011-05-23 06:06:41 +04:00
|
|
|
xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
|
1995-01-15 11:55:59 +03:00
|
|
|
if (xdr_replymsg(&xdr, &msg)) {
|
|
|
|
if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
|
|
|
|
(msg.acpted_rply.ar_stat == SUCCESS)) {
|
2011-05-23 06:06:41 +04:00
|
|
|
raddr.sin_port = htons((uint16_t)rmtcr_port);
|
2011-05-24 10:59:35 +04:00
|
|
|
dom = domain_find(msg.rm_xid);
|
|
|
|
if (dom != NULL)
|
2011-05-24 11:01:24 +04:00
|
|
|
rpc_received(dom->dom_name, &raddr, 0, 0);
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xdr.x_op = XDR_FREE;
|
2011-08-30 21:06:20 +04:00
|
|
|
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
|
1993-04-26 12:09:02 +04:00
|
|
|
xdr_destroy(&xdr);
|
|
|
|
|
|
|
|
return RPC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Receive and dispatch packets on the ping socket.
|
|
|
|
*/
|
1996-06-22 21:59:52 +04:00
|
|
|
static enum clnt_stat
|
2002-07-07 01:44:40 +04:00
|
|
|
handle_ping(void)
|
1994-07-15 00:54:29 +04:00
|
|
|
{
|
1996-06-22 21:59:52 +04:00
|
|
|
char buf[BUFSIZE];
|
2006-05-10 00:18:05 +04:00
|
|
|
socklen_t fromlen;
|
2007-07-08 02:33:57 +04:00
|
|
|
ssize_t inlen;
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
1994-07-15 00:54:29 +04:00
|
|
|
struct sockaddr_in raddr;
|
|
|
|
struct rpc_msg msg;
|
|
|
|
XDR xdr;
|
|
|
|
bool_t res;
|
|
|
|
|
|
|
|
recv_again:
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("handle_ping receiving\n");
|
1998-07-13 16:01:03 +04:00
|
|
|
(void)memset(&xdr, 0, sizeof(xdr));
|
|
|
|
(void)memset(&msg, 0, sizeof(msg));
|
1994-07-15 00:54:29 +04:00
|
|
|
msg.acpted_rply.ar_verf = _null_auth;
|
2007-07-08 02:33:57 +04:00
|
|
|
msg.acpted_rply.ar_results.where = (caddr_t)(void *)&res;
|
2011-08-30 21:06:20 +04:00
|
|
|
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_bool;
|
1994-07-15 00:54:29 +04:00
|
|
|
|
|
|
|
try_again:
|
|
|
|
fromlen = sizeof (struct sockaddr);
|
|
|
|
inlen = recvfrom(pingsock, buf, sizeof buf, 0,
|
2007-07-08 02:33:57 +04:00
|
|
|
(struct sockaddr *)(void *)&raddr, &fromlen);
|
1995-01-15 11:55:59 +03:00
|
|
|
if (inlen < 0) {
|
|
|
|
if (errno == EINTR)
|
1994-07-15 00:54:29 +04:00
|
|
|
goto try_again;
|
2011-05-23 06:36:35 +04:00
|
|
|
DPRINTF("handle_ping: recvfrom failed (%s)\n",
|
|
|
|
strerror(errno));
|
1994-07-15 00:54:29 +04:00
|
|
|
return RPC_CANTRECV;
|
|
|
|
}
|
2011-05-23 06:06:41 +04:00
|
|
|
if ((size_t)inlen < sizeof(uint32_t))
|
1994-07-15 00:54:29 +04:00
|
|
|
goto recv_again;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* see if reply transaction id matches sent id.
|
|
|
|
* If so, decode the results.
|
|
|
|
*/
|
2011-05-23 06:06:41 +04:00
|
|
|
xdrmem_create(&xdr, buf, (unsigned)inlen, XDR_DECODE);
|
1995-01-15 11:55:59 +03:00
|
|
|
if (xdr_replymsg(&xdr, &msg)) {
|
|
|
|
if ((msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
|
|
|
|
(msg.acpted_rply.ar_stat == SUCCESS)) {
|
2011-05-24 10:59:35 +04:00
|
|
|
dom = domain_find(msg.rm_xid);
|
|
|
|
if (dom != NULL)
|
2011-05-24 11:01:24 +04:00
|
|
|
rpc_received(dom->dom_name, &raddr, 0, 0);
|
1994-07-15 00:54:29 +04:00
|
|
|
}
|
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
xdr.x_op = XDR_FREE;
|
2011-08-30 21:06:20 +04:00
|
|
|
msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
|
2011-05-24 10:56:16 +04:00
|
|
|
xdr_destroy(&xdr);
|
|
|
|
|
|
|
|
return RPC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Contact all known servers for a domain in the hopes that one of
|
|
|
|
* them's awake. Also, if we previously had a binding but it timed
|
|
|
|
* out, try the portmapper on that host in case ypserv moved ports for
|
|
|
|
* some reason.
|
|
|
|
*
|
|
|
|
* As a side effect, wipe out any existing binding file.
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
static int
|
2011-05-24 10:59:35 +04:00
|
|
|
nag_servers(struct domain *dom)
|
2011-05-24 10:56:16 +04:00
|
|
|
{
|
2011-05-24 10:59:53 +04:00
|
|
|
char *dom_name = dom->dom_name;
|
2011-05-24 10:56:16 +04:00
|
|
|
struct rpc_msg msg;
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
enum clnt_stat st;
|
|
|
|
int outlen;
|
|
|
|
AUTH *rpcua;
|
|
|
|
XDR xdr;
|
|
|
|
|
|
|
|
DPRINTF("nag_servers\n");
|
2011-08-30 21:06:20 +04:00
|
|
|
rmtca.xdr_args = (xdrproc_t)xdr_ypdomain_wrap_string;
|
2011-05-24 10:59:35 +04:00
|
|
|
rmtca.args_ptr = (caddr_t)(void *)&dom_name;
|
2011-05-24 10:56:16 +04:00
|
|
|
|
|
|
|
(void)memset(&xdr, 0, sizeof xdr);
|
|
|
|
(void)memset(&msg, 0, sizeof msg);
|
|
|
|
|
|
|
|
rpcua = authunix_create_default();
|
|
|
|
if (rpcua == NULL) {
|
|
|
|
DPRINTF("cannot get unix auth\n");
|
|
|
|
return RPC_SYSTEMERROR;
|
|
|
|
}
|
|
|
|
msg.rm_direction = CALL;
|
|
|
|
msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
|
|
|
|
msg.rm_call.cb_prog = PMAPPROG;
|
|
|
|
msg.rm_call.cb_vers = PMAPVERS;
|
|
|
|
msg.rm_call.cb_proc = PMAPPROC_CALLIT;
|
|
|
|
msg.rm_call.cb_cred = rpcua->ah_cred;
|
|
|
|
msg.rm_call.cb_verf = rpcua->ah_verf;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
msg.rm_xid = dom->dom_xid;
|
2011-05-24 10:56:16 +04:00
|
|
|
xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
|
|
|
|
if (!xdr_callmsg(&xdr, &msg)) {
|
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
if (!xdr_rmtcall_args(&xdr, &rmtca)) {
|
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
outlen = (int)xdr_getpos(&xdr);
|
|
|
|
xdr_destroy(&xdr);
|
|
|
|
if (outlen < 1) {
|
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
if (dom->dom_lockfd != -1) {
|
|
|
|
(void)close(dom->dom_lockfd);
|
|
|
|
dom->dom_lockfd = -1;
|
|
|
|
removelock(dom);
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (dom->dom_state == DOM_PINGING || dom->dom_state == DOM_LOST) {
|
2011-05-24 10:56:16 +04:00
|
|
|
/*
|
|
|
|
* This resolves the following situation:
|
|
|
|
* ypserver on other subnet was once bound,
|
|
|
|
* but rebooted and is now using a different port
|
|
|
|
*/
|
|
|
|
struct sockaddr_in bindsin;
|
|
|
|
|
|
|
|
(void)memset(&bindsin, 0, sizeof bindsin);
|
|
|
|
bindsin.sin_family = AF_INET;
|
|
|
|
bindsin.sin_len = sizeof(bindsin);
|
|
|
|
bindsin.sin_port = htons(PMAPPORT);
|
2011-05-24 10:59:35 +04:00
|
|
|
bindsin.sin_addr = dom->dom_server_addr.sin_addr;
|
2011-05-24 10:56:16 +04:00
|
|
|
|
|
|
|
if (sendto(rpcsock, buf, outlen, 0,
|
|
|
|
(struct sockaddr *)(void *)&bindsin,
|
|
|
|
(socklen_t)sizeof bindsin) == -1)
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "nag_servers: sendto: %s",
|
|
|
|
strerror(errno));
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
|
2011-05-24 11:01:15 +04:00
|
|
|
switch (dom->dom_ypbindmode) {
|
2011-05-24 10:56:16 +04:00
|
|
|
case YPBIND_BROADCAST:
|
2011-05-24 11:01:24 +04:00
|
|
|
if (dom->dom_been_ypset) {
|
2011-05-24 11:00:34 +04:00
|
|
|
return direct_set(buf, outlen, dom);
|
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
return broadcast(buf, outlen);
|
|
|
|
|
|
|
|
case YPBIND_DIRECT:
|
2011-05-24 11:01:40 +04:00
|
|
|
return direct(buf, outlen, dom);
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Send a ping message to a domain's current ypserver.
|
|
|
|
*/
|
2011-05-24 10:56:48 +04:00
|
|
|
static int
|
2011-05-24 10:59:35 +04:00
|
|
|
ping(struct domain *dom)
|
2011-05-24 10:56:16 +04:00
|
|
|
{
|
2011-05-24 10:59:53 +04:00
|
|
|
char *dom_name = dom->dom_name;
|
2011-05-24 10:56:16 +04:00
|
|
|
struct rpc_msg msg;
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
enum clnt_stat st;
|
|
|
|
int outlen;
|
|
|
|
AUTH *rpcua;
|
|
|
|
XDR xdr;
|
|
|
|
|
|
|
|
(void)memset(&xdr, 0, sizeof xdr);
|
|
|
|
(void)memset(&msg, 0, sizeof msg);
|
|
|
|
|
|
|
|
rpcua = authunix_create_default();
|
|
|
|
if (rpcua == NULL) {
|
|
|
|
DPRINTF("cannot get unix auth\n");
|
|
|
|
return RPC_SYSTEMERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.rm_direction = CALL;
|
|
|
|
msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
|
|
|
|
msg.rm_call.cb_prog = YPPROG;
|
|
|
|
msg.rm_call.cb_vers = YPVERS;
|
|
|
|
msg.rm_call.cb_proc = YPPROC_DOMAIN_NONACK;
|
|
|
|
msg.rm_call.cb_cred = rpcua->ah_cred;
|
|
|
|
msg.rm_call.cb_verf = rpcua->ah_verf;
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
msg.rm_xid = dom->dom_xid;
|
2011-05-24 10:56:16 +04:00
|
|
|
xdrmem_create(&xdr, buf, (unsigned)sizeof(buf), XDR_ENCODE);
|
|
|
|
if (!xdr_callmsg(&xdr, &msg)) {
|
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
2011-05-24 10:59:35 +04:00
|
|
|
if (!xdr_ypdomain_wrap_string(&xdr, &dom_name)) {
|
2011-05-24 10:56:16 +04:00
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
outlen = (int)xdr_getpos(&xdr);
|
|
|
|
xdr_destroy(&xdr);
|
|
|
|
if (outlen < 1) {
|
|
|
|
st = RPC_CANTENCODEARGS;
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
AUTH_DESTROY(rpcua);
|
|
|
|
|
2011-05-24 10:59:35 +04:00
|
|
|
DPRINTF("ping %x\n", dom->dom_server_addr.sin_addr.s_addr);
|
2011-05-24 10:56:16 +04:00
|
|
|
|
|
|
|
if (sendto(pingsock, buf, outlen, 0,
|
2011-05-24 10:59:35 +04:00
|
|
|
(struct sockaddr *)(void *)&dom->dom_server_addr,
|
|
|
|
(socklen_t)(sizeof dom->dom_server_addr)) == -1)
|
2011-05-24 10:57:55 +04:00
|
|
|
yp_log(LOG_WARNING, "ping: sendto: %s", strerror(errno));
|
2011-05-24 10:56:16 +04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-06-10 21:19:12 +04:00
|
|
|
* Scan for timer-based work to do.
|
2011-05-24 10:56:16 +04:00
|
|
|
*
|
2014-06-10 21:19:12 +04:00
|
|
|
* If the domain is currently alive, ping the server we're currently
|
|
|
|
* bound to. Otherwise, try all known servers and/or broadcast for a
|
|
|
|
* server via nag_servers.
|
|
|
|
*
|
|
|
|
* Try again in five seconds.
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
*
|
|
|
|
* If we get back here and the state is still DOM_PINGING, it means
|
|
|
|
* we didn't receive a ping response within five seconds. Declare the
|
|
|
|
* binding lost. If the binding is already lost, and it's been lost
|
|
|
|
* for 60 seconds, switch to DOM_DEAD and begin exponential backoff.
|
|
|
|
* The exponential backoff starts at 10 seconds and tops out at one
|
|
|
|
* hour; see above.
|
2011-05-24 10:56:16 +04:00
|
|
|
*/
|
2011-05-24 10:56:48 +04:00
|
|
|
static void
|
2011-05-24 10:56:16 +04:00
|
|
|
checkwork(void)
|
|
|
|
{
|
2011-05-24 10:59:35 +04:00
|
|
|
struct domain *dom;
|
2011-05-24 10:56:16 +04:00
|
|
|
time_t t;
|
|
|
|
|
|
|
|
check = 0;
|
|
|
|
|
|
|
|
(void)time(&t);
|
2011-05-24 10:59:35 +04:00
|
|
|
for (dom = domains; dom != NULL; dom = dom->dom_next) {
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (dom->dom_checktime >= t) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (dom->dom_state) {
|
|
|
|
case DOM_NEW:
|
|
|
|
/* XXX should be a timeout for this state */
|
|
|
|
dom->dom_checktime = t + 5;
|
|
|
|
(void)nag_servers(dom);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_ALIVE:
|
|
|
|
dom->dom_state = DOM_PINGING;
|
|
|
|
dom->dom_checktime = t + 5;
|
|
|
|
(void)ping(dom);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_PINGING:
|
|
|
|
dom->dom_state = DOM_LOST;
|
|
|
|
dom->dom_losttime = t;
|
2011-05-24 10:59:35 +04:00
|
|
|
dom->dom_checktime = t + 5;
|
2014-06-10 21:19:48 +04:00
|
|
|
yp_log(LOG_NOTICE, "Domain %s lost its binding to "
|
|
|
|
"server %s", dom->dom_name,
|
|
|
|
inet_ntoa(dom->dom_server_addr.sin_addr));
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
(void)nag_servers(dom);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_LOST:
|
|
|
|
if (t > dom->dom_losttime + 60) {
|
|
|
|
dom->dom_state = DOM_DEAD;
|
|
|
|
dom->dom_backofftime = 10;
|
2014-06-10 21:19:48 +04:00
|
|
|
yp_log(LOG_NOTICE, "Domain %s dead; "
|
|
|
|
"going to exponential backoff",
|
|
|
|
dom->dom_name);
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
}
|
|
|
|
dom->dom_checktime = t + 5;
|
|
|
|
(void)nag_servers(dom);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DOM_DEAD:
|
|
|
|
dom->dom_checktime = t + dom->dom_backofftime;
|
|
|
|
backoff(&dom->dom_backofftime);
|
|
|
|
(void)nag_servers(dom);
|
|
|
|
break;
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
/* re-fetch the time in case we hung sending packets */
|
|
|
|
(void)time(&t);
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
|
|
|
}
|
1994-07-15 00:54:29 +04:00
|
|
|
|
2014-06-10 21:19:36 +04:00
|
|
|
/*
|
|
|
|
* Process a hangup signal.
|
|
|
|
*
|
|
|
|
* Do an extra nag_servers() for any domains that are DEAD. This way
|
|
|
|
* if you know things are back up you can restore service by sending
|
|
|
|
* ypbind a SIGHUP rather than waiting for the timeout period.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dohup(void)
|
|
|
|
{
|
|
|
|
struct domain *dom;
|
|
|
|
|
|
|
|
hupped = 0;
|
|
|
|
for (dom = domains; dom != NULL; dom = dom->dom_next) {
|
|
|
|
if (dom->dom_state == DOM_DEAD) {
|
|
|
|
(void)nag_servers(dom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a hangup signal.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
hup(int __unused sig)
|
|
|
|
{
|
|
|
|
hupped = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize hangup processing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
starthup(void)
|
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
sa.sa_handler = hup;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
if (sigaction(SIGHUP, &sa, NULL) == -1) {
|
|
|
|
err(1, "sigaction");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// main
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Usage message.
|
|
|
|
*/
|
2011-08-30 00:38:53 +04:00
|
|
|
__dead static void
|
2011-05-24 10:56:16 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
const char *opt = "";
|
|
|
|
#ifdef DEBUG
|
|
|
|
opt = " [-d]";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
|
|
|
|
getprogname(), opt);
|
|
|
|
exit(1);
|
1994-07-15 00:54:29 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Main.
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
2011-05-24 10:56:16 +04:00
|
|
|
struct timeval tv;
|
|
|
|
fd_set fdsr;
|
|
|
|
int width, lockfd;
|
2014-06-10 21:18:45 +04:00
|
|
|
int started = 0;
|
2014-06-10 21:18:02 +04:00
|
|
|
char *domainname;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2011-05-24 10:56:16 +04:00
|
|
|
setprogname(argv[0]);
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Process arguments.
|
|
|
|
*/
|
|
|
|
|
2011-05-24 11:01:15 +04:00
|
|
|
default_ypbindmode = YPBIND_DIRECT;
|
2011-05-24 10:56:16 +04:00
|
|
|
while (--argc) {
|
|
|
|
++argv;
|
2011-05-24 11:00:34 +04:00
|
|
|
if (!strcmp("-insecure", *argv)) {
|
2011-05-24 10:56:16 +04:00
|
|
|
insecure = 1;
|
2011-05-24 11:00:34 +04:00
|
|
|
} else if (!strcmp("-ypset", *argv)) {
|
|
|
|
allow_any_ypset = 1;
|
|
|
|
allow_local_ypset = 1;
|
|
|
|
} else if (!strcmp("-ypsetme", *argv)) {
|
|
|
|
allow_any_ypset = 0;
|
|
|
|
allow_local_ypset = 1;
|
|
|
|
} else if (!strcmp("-broadcast", *argv)) {
|
2011-05-24 11:01:15 +04:00
|
|
|
default_ypbindmode = YPBIND_BROADCAST;
|
2011-05-24 10:56:16 +04:00
|
|
|
#ifdef DEBUG
|
2011-05-24 11:00:34 +04:00
|
|
|
} else if (!strcmp("-d", *argv)) {
|
|
|
|
debug = 1;
|
2011-05-24 10:56:16 +04:00
|
|
|
#endif
|
2011-05-24 11:00:34 +04:00
|
|
|
} else {
|
2011-05-24 10:56:16 +04:00
|
|
|
usage();
|
2011-05-24 11:00:34 +04:00
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
}
|
1997-07-07 06:27:05 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Look up the name of the default domain.
|
|
|
|
*/
|
|
|
|
|
2014-06-10 21:19:00 +04:00
|
|
|
(void)yp_get_default_domain(&domainname);
|
|
|
|
if (domainname[0] == '\0')
|
|
|
|
errx(1, "Domainname not set. Aborting.");
|
|
|
|
if (_yp_invalid_domain(domainname))
|
|
|
|
errx(1, "Invalid domainname: %s", domainname);
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Start things up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Open the system log. */
|
2011-05-24 10:56:16 +04:00
|
|
|
openlog("ypbind", LOG_PERROR | LOG_PID, LOG_DAEMON);
|
1997-07-07 06:27:05 +04:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* Acquire /var/run/ypbind.lock. */
|
2011-05-24 10:58:07 +04:00
|
|
|
lockfd = open_locked(_PATH_YPBIND_LOCK, O_CREAT|O_RDWR|O_TRUNC, 0644);
|
2011-05-24 10:56:16 +04:00
|
|
|
if (lockfd == -1)
|
|
|
|
err(1, "Cannot create %s", _PATH_YPBIND_LOCK);
|
1993-04-26 12:09:02 +04:00
|
|
|
|
2014-06-10 21:19:36 +04:00
|
|
|
/* Accept hangups. */
|
|
|
|
starthup();
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* Initialize sunrpc stuff. */
|
2011-05-24 10:58:19 +04:00
|
|
|
sunrpc_setup();
|
1996-03-31 01:49:08 +03:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/* Clean out BINDINGDIR, deleting all existing (now stale) bindings */
|
2011-05-24 10:56:16 +04:00
|
|
|
if (purge_bindingdir(BINDINGDIR) < 0)
|
2014-06-10 21:19:12 +04:00
|
|
|
errx(1, "Unable to purge old bindings from %s", BINDINGDIR);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We start with one binding, for the default domain. It starts
|
|
|
|
* out "unsuccessful".
|
|
|
|
*
|
|
|
|
* XXX: domain_create adds the new domain to 'domains' (the
|
|
|
|
* global linked list) and therefore we shouldn't assign
|
|
|
|
* 'domains' again on return.
|
|
|
|
*/
|
1996-03-31 01:49:08 +03:00
|
|
|
|
2011-05-24 10:59:07 +04:00
|
|
|
domains = domain_create(domainname);
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete the lock for the default domain again, just in case something
|
|
|
|
* magically caused it to appear since purge_bindingdir() was called.
|
|
|
|
* XXX: this is useless and redundant; remove it.
|
|
|
|
*/
|
2011-05-24 10:59:07 +04:00
|
|
|
removelock(domains);
|
1996-03-31 01:49:08 +03:00
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Main loop. Wake up at least once a second and check for
|
|
|
|
* timer-based work to do (checkwork) and also handle incoming
|
|
|
|
* responses from ypservers and any RPCs made to the ypbind
|
|
|
|
* service.
|
|
|
|
*
|
|
|
|
* There are two sockets used for ypserver traffic: one for
|
|
|
|
* pings and one for everything else. These call XDR manually
|
|
|
|
* for encoding and are *not* dispatched via the sunrpc
|
|
|
|
* libraries.
|
|
|
|
*
|
|
|
|
* The ypbind serivce *is* dispatched via the sunrpc libraries.
|
|
|
|
* svc_getreqset() does whatever internal muck and ultimately
|
|
|
|
* ypbind service calls arrive at ypbindprog_2().
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
checkwork();
|
|
|
|
for (;;) {
|
|
|
|
width = svc_maxfd;
|
|
|
|
if (rpcsock > width)
|
|
|
|
width = rpcsock;
|
|
|
|
if (pingsock > width)
|
|
|
|
width = pingsock;
|
|
|
|
width++;
|
|
|
|
fdsr = svc_fdset;
|
|
|
|
FD_SET(rpcsock, &fdsr);
|
|
|
|
FD_SET(pingsock, &fdsr);
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
switch (select(width, &fdsr, NULL, NULL, &tv)) {
|
|
|
|
case 0:
|
2014-06-10 21:19:12 +04:00
|
|
|
/* select timed out - check for timer-based work */
|
2014-06-10 21:19:36 +04:00
|
|
|
if (hupped) {
|
|
|
|
dohup();
|
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
checkwork();
|
|
|
|
break;
|
|
|
|
case -1:
|
2014-06-10 21:19:36 +04:00
|
|
|
if (hupped) {
|
|
|
|
dohup();
|
|
|
|
}
|
|
|
|
if (errno != EINTR) {
|
|
|
|
yp_log(LOG_WARNING, "select: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2011-05-24 10:56:16 +04:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-10 21:19:36 +04:00
|
|
|
if (hupped) {
|
|
|
|
dohup();
|
|
|
|
}
|
2014-06-10 21:19:12 +04:00
|
|
|
/* incoming of our own; read it */
|
2011-05-24 10:56:16 +04:00
|
|
|
if (FD_ISSET(rpcsock, &fdsr))
|
|
|
|
(void)handle_replies();
|
|
|
|
if (FD_ISSET(pingsock, &fdsr))
|
|
|
|
(void)handle_ping();
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/* read any incoming packets for the ypbind service */
|
2011-05-24 10:56:16 +04:00
|
|
|
svc_getreqset(&fdsr);
|
2014-06-10 21:19:12 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only check for timer-based work if
|
|
|
|
* something in the incoming RPC logic said
|
|
|
|
* to. This might be just a hack to avoid
|
|
|
|
* scanning the list unnecessarily, but I
|
|
|
|
* suspect it's also a hack to cover wrong
|
|
|
|
* state logic. - dholland 20140609
|
|
|
|
*/
|
2011-05-24 10:56:16 +04:00
|
|
|
if (check)
|
|
|
|
checkwork();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-06-10 21:19:12 +04:00
|
|
|
/*
|
|
|
|
* Defer daemonizing until the default domain binds
|
|
|
|
* successfully. XXX: there seems to be no timeout
|
|
|
|
* on this, which means that if the default domain
|
|
|
|
* is dead upstream boot will hang indefinitely.
|
|
|
|
*/
|
Instead of using magic numbers in what looks like a boolean
(dom_alive), create a state enumeration (domainstates) and use it
instead.
Instead of three states (new, alive, and, effectively, 'troubled') go
to five: new, alive, pinging, lost, and dead.
Domains start in the NEW state. When we get a reply from a server, the
state goes to ALIVE. The state is set to PINGING when we ping the
server (once a minute normally) and if the ping times out, it goes to
LOST. If we stay lost for a minute, go to DEAD, and in DEAD, do
exponential backoff of nag_servers calls.
Getting rid of the broken logic attached to the 'troubled' state fixes
PR 15355 (ypbind defeats disk idle spindown) -- it will now only
rewrite the binding file when the binding changes.
Also, fix the HEURISTIC code so it doesn't trigger except in ALIVE
state. I think this was the source of a lot of the spamming behavior
seen in PR 32519, which is now fixed.
Might also fix PR 23135 (broadcast ypbind sometimes fails to find
servers).
2014-06-10 21:19:22 +04:00
|
|
|
if (!started && domains->dom_state == DOM_ALIVE) {
|
2014-06-10 21:18:45 +04:00
|
|
|
started = 1;
|
2011-05-24 10:56:16 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (!debug)
|
|
|
|
#endif
|
|
|
|
(void)daemon(0, 0);
|
|
|
|
(void)pidfile(NULL);
|
|
|
|
}
|
|
|
|
}
|
1996-03-31 01:49:08 +03:00
|
|
|
}
|