NetBSD/usr.sbin/ypbind/ypbind.c

835 lines
20 KiB
C
Raw Normal View History

/*
1994-05-25 13:55:39 +04:00
* Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
* 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.
1994-05-25 13:55:39 +04:00
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Theo de Raadt.
* 4. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* 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.
*/
#ifndef LINT
static char rcsid[] = "$Id: ypbind.c,v 1.19 1995/04/21 04:40:36 cgd Exp $";
#endif
1993-04-26 12:09:02 +04:00
#include <sys/param.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
1993-04-26 12:09:02 +04:00
#include <sys/syslog.h>
#include <stdio.h>
#include <stdlib.h>
1993-04-26 12:09:02 +04:00
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <netdb.h>
#include <string.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <rpc/pmap_clnt.h>
#include <rpc/pmap_prot.h>
#include <rpc/pmap_rmt.h>
#include <unistd.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
1994-09-28 04:39:54 +03:00
#define BINDINGDIR "/var/yp/binding"
#define YPBINDLOCK "/var/run/ypbind.lock"
1993-04-26 12:09:02 +04:00
struct _dom_binding {
struct _dom_binding *dom_pnext;
char dom_domain[YPMAXDOMAIN + 1];
struct sockaddr_in dom_server_addr;
unsigned short int dom_server_port;
int dom_socket;
CLIENT *dom_client;
long int dom_vers;
time_t dom_check_t;
time_t dom_ask_t;
1993-04-26 12:09:02 +04:00
int dom_lockfd;
int dom_alive;
};
extern bool_t xdr_domainname(), xdr_ypbind_resp();
extern bool_t xdr_ypreq_key(), xdr_ypresp_val();
extern bool_t xdr_ypbind_setdom();
char *domainname;
struct _dom_binding *ypbindlist;
int check;
#define YPSET_NO 0
#define YPSET_LOCAL 1
#define YPSET_ALL 2
int ypsetmode = YPSET_NO;
int rpcsock, pingsock;
1993-04-26 12:09:02 +04:00
struct rmtcallargs rmtca;
struct rmtcallres rmtcr;
char rmtcr_outval;
u_long rmtcr_port;
SVCXPRT *udptransp, *tcptransp;
1993-04-26 12:09:02 +04:00
void *
ypbindproc_null_2(transp, argp, clnt)
1995-01-15 11:55:59 +03:00
SVCXPRT *transp;
void *argp;
CLIENT *clnt;
1993-04-26 12:09:02 +04:00
{
static char res;
1995-01-15 11:55:59 +03:00
memset(&res, 0, sizeof(res));
1993-04-26 12:09:02 +04:00
return (void *)&res;
}
struct ypbind_resp *
ypbindproc_domain_2(transp, argp, clnt)
1995-01-15 11:55:59 +03:00
SVCXPRT *transp;
char *argp;
CLIENT *clnt;
1993-04-26 12:09:02 +04:00
{
static struct ypbind_resp res;
struct _dom_binding *ypdb;
char path[MAXPATHLEN];
time_t now;
1993-04-26 12:09:02 +04:00
1995-01-15 11:55:59 +03:00
memset(&res, 0, sizeof res);
1993-04-26 12:09:02 +04:00
res.ypbind_status = YPBIND_FAIL_VAL;
1995-01-15 11:55:59 +03:00
for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
if (!strcmp(ypdb->dom_domain, argp))
1993-04-26 12:09:02 +04:00
break;
1995-01-15 11:55:59 +03:00
if (ypdb == NULL) {
1993-04-26 12:09:02 +04:00
ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
1995-01-15 11:55:59 +03:00
memset(ypdb, 0, sizeof *ypdb);
1993-04-26 12:09:02 +04:00
strncpy(ypdb->dom_domain, argp, sizeof ypdb->dom_domain);
ypdb->dom_vers = YPVERS;
ypdb->dom_alive = 0;
ypdb->dom_lockfd = -1;
sprintf(path, "%s/%s.%d", BINDINGDIR, ypdb->dom_domain, ypdb->dom_vers);
unlink(path);
ypdb->dom_pnext = ypbindlist;
ypbindlist = ypdb;
check++;
return NULL;
}
1995-01-15 11:55:59 +03:00
if (ypdb->dom_alive == 0)
1993-04-26 12:09:02 +04:00
return NULL;
#ifdef HEURISTIC
time(&now);
if (now < ypdb->dom_ask_t + 5) {
1993-04-26 12:09:02 +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
*/
if (ypdb->dom_check_t >= now) {
/* don't flood it */
ypdb->dom_check_t = 0;
check++;
}
1993-04-26 12:09:02 +04:00
}
ypdb->dom_ask_t = now;
1993-04-26 12:09:02 +04:00
#endif
answer:
res.ypbind_status = YPBIND_SUCC_VAL;
res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr =
ypdb->dom_server_addr.sin_addr.s_addr;
res.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
ypdb->dom_server_port;
/*printf("domain %s at %s/%d\n", ypdb->dom_domain,
inet_ntoa(ypdb->dom_server_addr.sin_addr),
ntohs(ypdb->dom_server_addr.sin_port));*/
return &res;
}
bool_t *
ypbindproc_setdom_2(transp, argp, clnt)
1995-01-15 11:55:59 +03:00
SVCXPRT *transp;
struct ypbind_setdom *argp;
CLIENT *clnt;
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
1995-01-15 11:55:59 +03:00
memset(&res, 0, sizeof(res));
1993-04-26 12:09:02 +04:00
fromsin = svc_getcaller(transp);
1995-01-15 11:55:59 +03:00
switch (ypsetmode) {
1993-04-26 12:09:02 +04:00
case YPSET_LOCAL:
1995-01-15 11:55:59 +03:00
if (fromsin->sin_addr.s_addr != htonl(INADDR_LOOPBACK))
1994-09-28 04:39:54 +03:00
return (bool_t *)NULL;
1993-04-26 12:09:02 +04:00
break;
case YPSET_ALL:
break;
case YPSET_NO:
default:
1994-09-28 04:39:54 +03:00
return (bool_t *)NULL;
1993-04-26 12:09:02 +04:00
}
1995-01-15 11:55:59 +03:00
if (ntohs(fromsin->sin_port) >= IPPORT_RESERVED)
1994-09-28 04:39:54 +03:00
return &res;
1995-01-15 11:55:59 +03:00
if (argp->ypsetdom_vers != YPVERS)
1994-09-28 04:39:54 +03:00
return &res;
1993-04-26 12:09:02 +04:00
1995-01-15 11:55:59 +03:00
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);
bindsin.sin_addr = argp->ypsetdom_addr;
1993-04-26 12:09:02 +04:00
bindsin.sin_port = argp->ypsetdom_port;
rpc_received(argp->ypsetdom_domain, &bindsin, 1);
res = 1;
1994-09-28 04:39:54 +03:00
return &res;
1993-04-26 12:09:02 +04:00
}
static void
ypbindprog_2(rqstp, transp)
1995-01-15 11:55:59 +03:00
struct svc_req *rqstp;
register SVCXPRT *transp;
1993-04-26 12:09:02 +04:00
{
union {
char ypbindproc_domain_2_arg[MAXHOSTNAMELEN];
struct ypbind_setdom ypbindproc_setdom_2_arg;
} argument;
struct authunix_parms *creds;
char *result;
bool_t (*xdr_argument)(), (*xdr_result)();
char *(*local)();
switch (rqstp->rq_proc) {
case YPBINDPROC_NULL:
xdr_argument = xdr_void;
xdr_result = xdr_void;
local = (char *(*)()) ypbindproc_null_2;
break;
case YPBINDPROC_DOMAIN:
xdr_argument = xdr_domainname;
xdr_result = xdr_ypbind_resp;
local = (char *(*)()) ypbindproc_domain_2;
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;
}
xdr_argument = xdr_ypbind_setdom;
xdr_result = xdr_void;
local = (char *(*)()) ypbindproc_setdom_2;
break;
default:
svcerr_noproc(transp);
return;
}
1995-01-15 11:55:59 +03:00
memset(&argument, 0, sizeof(argument));
if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
1993-04-26 12:09:02 +04:00
svcerr_decode(transp);
return;
}
result = (*local)(transp, &argument, rqstp);
if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
svcerr_systemerr(transp);
}
return;
}
main(argc, argv)
1995-01-15 11:55:59 +03:00
int argc;
char *argv[];
1993-04-26 12:09:02 +04:00
{
char path[MAXPATHLEN];
struct timeval tv;
fd_set fdsr;
1994-09-28 04:39:54 +03:00
int width, lockfd;
int evil = 0, one;
1993-04-26 12:09:02 +04:00
yp_get_default_domain(&domainname);
1995-01-15 11:55:59 +03:00
if (domainname[0] == '\0') {
1993-04-26 12:09:02 +04:00
fprintf(stderr, "domainname not set. Aborting.\n");
exit(1);
}
while (--argc) {
++argv;
if (!strcmp("-ypset", *argv))
1993-04-26 12:09:02 +04:00
ypsetmode = YPSET_ALL;
else if (!strcmp("-ypsetme", *argv))
1993-04-26 12:09:02 +04:00
ypsetmode = YPSET_LOCAL;
}
/* blow away everything in BINDINGDIR */
1994-09-28 04:39:54 +03:00
#ifdef O_SHLOCK
1995-01-15 11:55:59 +03:00
if ((lockfd = open(YPBINDLOCK, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
1994-09-28 04:39:54 +03:00
fprintf(stderr, "ypbind: cannot create %s\n", YPBINDLOCK);
exit(1);
}
#else
1995-01-15 11:55:59 +03:00
if ((lockfd = open(YPBINDLOCK, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
1994-09-28 04:39:54 +03:00
fprintf(stderr, "ypbind: cannot create %s\n", YPBINDLOCK);
exit(1);
}
flock(lockfd, LOCK_SH);
#endif
1993-04-26 12:09:02 +04:00
(void)pmap_unset(YPBINDPROG, YPBINDVERS);
1993-04-26 12:09:02 +04:00
udptransp = svcudp_create(RPC_ANYSOCK);
if (udptransp == NULL) {
1993-04-26 12:09:02 +04:00
fprintf(stderr, "cannot create udp service.");
exit(1);
}
if (!svc_register(udptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
IPPROTO_UDP)) {
1993-04-26 12:09:02 +04:00
fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, udp).");
exit(1);
}
tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (tcptransp == NULL) {
1993-04-26 12:09:02 +04:00
fprintf(stderr, "cannot create tcp service.");
exit(1);
}
if (!svc_register(tcptransp, YPBINDPROG, YPBINDVERS, ypbindprog_2,
IPPROTO_TCP)) {
1993-04-26 12:09:02 +04:00
fprintf(stderr, "unable to register (YPBINDPROG, YPBINDVERS, tcp).");
exit(1);
}
1995-01-15 11:55:59 +03:00
if ((rpcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
1993-04-26 12:09:02 +04:00
perror("socket");
return -1;
}
1995-01-15 11:55:59 +03:00
if ((pingsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket");
return -1;
}
1993-04-26 12:09:02 +04:00
fcntl(rpcsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
fcntl(pingsock, F_SETFL, fcntl(rpcsock, F_GETFL, 0) | FNDELAY);
one = 1;
setsockopt(rpcsock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
1993-04-26 12:09:02 +04:00
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;
rmtcr.xdr_results = xdr_bool;
rmtcr.results_ptr = (caddr_t)&rmtcr_outval;
/* build initial domain binding, make it "unsuccessful" */
ypbindlist = (struct _dom_binding *)malloc(sizeof *ypbindlist);
1995-01-15 11:55:59 +03:00
memset(ypbindlist, 0, sizeof *ypbindlist);
1993-04-26 12:09:02 +04:00
strncpy(ypbindlist->dom_domain, domainname, sizeof ypbindlist->dom_domain);
ypbindlist->dom_vers = YPVERS;
ypbindlist->dom_alive = 0;
ypbindlist->dom_lockfd = -1;
sprintf(path, "%s/%s.%d", BINDINGDIR, ypbindlist->dom_domain,
ypbindlist->dom_vers);
(void)unlink(path);
checkwork();
1995-01-15 11:55:59 +03:00
while (1) {
width = svc_maxfd;
if (rpcsock > width)
width = rpcsock;
if (pingsock > width)
width = pingsock;
width++;
1995-01-15 11:55:59 +03:00
1993-04-26 12:09:02 +04:00
fdsr = svc_fdset;
FD_SET(rpcsock, &fdsr);
FD_SET(pingsock, &fdsr);
1993-04-26 12:09:02 +04:00
tv.tv_sec = 1;
tv.tv_usec = 0;
1995-01-15 11:55:59 +03:00
switch (select(width, &fdsr, NULL, NULL, &tv)) {
1993-04-26 12:09:02 +04:00
case 0:
checkwork();
break;
case -1:
perror("select\n");
break;
default:
if (FD_ISSET(rpcsock, &fdsr))
1993-04-26 12:09:02 +04:00
handle_replies();
if (FD_ISSET(pingsock, &fdsr))
handle_ping();
1993-04-26 12:09:02 +04:00
svc_getreqset(&fdsr);
if (check)
1993-04-26 12:09:02 +04:00
checkwork();
break;
}
if (!evil && ypbindlist->dom_alive) {
evil = 1;
daemon(0, 0);
}
1993-04-26 12:09:02 +04:00
}
}
/*
* State transition is done like this:
*
* STATE EVENT ACTION NEWSTATE TIMEOUT
* no binding timeout broadcast no binding 5 sec
* no binding answer -- binding 60 sec
* binding timeout ping server checking 5 sec
* checking timeout ping server + broadcast checking 5 sec
* checking answer -- binding 60 sec
1993-04-26 12:09:02 +04:00
*/
checkwork()
{
struct _dom_binding *ypdb;
time_t t;
check = 0;
time(&t);
1995-01-15 11:55:59 +03:00
for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext) {
if (ypdb->dom_check_t < t) {
if (ypdb->dom_alive == 1)
ping(ypdb);
else
broadcast(ypdb);
1993-04-26 12:09:02 +04:00
time(&t);
ypdb->dom_check_t = t + 5;
}
}
}
1994-07-01 23:40:23 +04:00
ping(ypdb)
struct _dom_binding *ypdb;
{
char *dom = ypdb->dom_domain;
1995-01-15 11:55:59 +03:00
struct rpc_msg msg;
char buf[1400];
enum clnt_stat st;
int outlen;
AUTH *rpcua;
1995-01-15 11:55:59 +03:00
XDR xdr;
1995-01-15 11:55:59 +03:00
memset(&xdr, 0, sizeof xdr);
memset(&msg, 0, sizeof msg);
rpcua = authunix_create_default();
1995-01-15 11:55:59 +03:00
if (rpcua == (AUTH *)NULL) {
/*printf("cannot get unix auth\n");*/
return RPC_SYSTEMERROR;
}
1995-01-15 11:55:59 +03:00
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;
msg.rm_xid = (long)dom;
xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
if (!xdr_callmsg(&xdr, &msg)) {
st = RPC_CANTENCODEARGS;
AUTH_DESTROY(rpcua);
return st;
}
1995-01-15 11:55:59 +03:00
if (!xdr_domainname(&xdr, dom)) {
st = RPC_CANTENCODEARGS;
AUTH_DESTROY(rpcua);
return st;
}
1995-01-15 11:55:59 +03:00
outlen = (int)xdr_getpos(&xdr);
xdr_destroy(&xdr);
if (outlen < 1) {
st = RPC_CANTENCODEARGS;
AUTH_DESTROY(rpcua);
return st;
}
AUTH_DESTROY(rpcua);
ypdb->dom_alive = 2;
if (sendto(pingsock, buf, outlen, 0,
(struct sockaddr *)&ypdb->dom_server_addr,
sizeof ypdb->dom_server_addr) < 0)
perror("sendto");
return 0;
}
broadcast(ypdb)
struct _dom_binding *ypdb;
1993-04-26 12:09:02 +04:00
{
1994-07-01 23:40:23 +04:00
char *dom = ypdb->dom_domain;
1995-01-15 11:55:59 +03:00
struct rpc_msg msg;
1993-04-26 12:09:02 +04:00
char buf[1400], inbuf[8192];
char path[MAXPATHLEN];
1993-04-26 12:09:02 +04:00
enum clnt_stat st;
int outlen, i, sock, len;
1995-01-15 11:55:59 +03:00
struct sockaddr_in bindsin;
1993-04-26 12:09:02 +04:00
struct ifconf ifc;
struct ifreq ifreq, *ifr;
struct in_addr in;
AUTH *rpcua;
1995-01-15 11:55:59 +03:00
XDR xdr;
1993-04-26 12:09:02 +04:00
rmtca.xdr_args = xdr_domainname;
rmtca.args_ptr = dom;
1995-01-15 11:55:59 +03:00
memset(&xdr, 0, sizeof xdr);
memset(&msg, 0, sizeof msg);
1993-04-26 12:09:02 +04:00
rpcua = authunix_create_default();
1995-01-15 11:55:59 +03:00
if (rpcua == (AUTH *)NULL) {
1993-04-26 12:09:02 +04:00
/*printf("cannot get unix auth\n");*/
return RPC_SYSTEMERROR;
}
1995-01-15 11:55:59 +03:00
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;
msg.rm_xid = (long)dom;
xdrmem_create(&xdr, buf, sizeof buf, XDR_ENCODE);
if (!xdr_callmsg(&xdr, &msg)) {
1993-04-26 12:09:02 +04:00
st = RPC_CANTENCODEARGS;
AUTH_DESTROY(rpcua);
return st;
}
1995-01-15 11:55:59 +03:00
if (!xdr_rmtcall_args(&xdr, &rmtca)) {
1993-04-26 12:09:02 +04:00
st = RPC_CANTENCODEARGS;
AUTH_DESTROY(rpcua);
return st;
}
1995-01-15 11:55:59 +03:00
outlen = (int)xdr_getpos(&xdr);
xdr_destroy(&xdr);
if (outlen < 1) {
st = RPC_CANTENCODEARGS;
1993-04-26 12:09:02 +04:00
AUTH_DESTROY(rpcua);
return st;
}
AUTH_DESTROY(rpcua);
1995-01-15 11:55:59 +03:00
if (ypdb->dom_lockfd != -1) {
close(ypdb->dom_lockfd);
ypdb->dom_lockfd = -1;
sprintf(path, "%s/%s.%d", BINDINGDIR,
ypdb->dom_domain, ypdb->dom_vers);
unlink(path);
}
1994-07-01 23:40:23 +04:00
1995-01-15 11:55:59 +03:00
memset(&bindsin, 0, sizeof bindsin);
bindsin.sin_family = AF_INET;
bindsin.sin_len = sizeof(bindsin);
bindsin.sin_port = htons(PMAPPORT);
if (ypdb->dom_alive == 2) {
/*
* This resolves the following situation:
* ypserver on other subnet was once bound,
* but rebooted and is now using a different port
*/
1995-01-15 11:55:59 +03:00
bindsin.sin_addr = ypdb->dom_server_addr.sin_addr;
if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
sizeof bindsin) < 0)
perror("sendto");
}
1993-04-26 12:09:02 +04:00
/* find all networks and send the RPC packet out them all */
1995-01-15 11:55:59 +03:00
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
1993-04-26 12:09:02 +04:00
perror("socket");
return -1;
}
1993-04-26 12:09:02 +04:00
ifc.ifc_len = sizeof inbuf;
ifc.ifc_buf = inbuf;
1995-01-15 11:55:59 +03:00
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
1993-04-26 12:09:02 +04:00
close(sock);
perror("ioctl(SIOCGIFCONF)");
return -1;
}
ifr = ifc.ifc_req;
ifreq.ifr_name[0] = '\0';
1995-01-15 11:55:59 +03:00
for (i = 0; i < ifc.ifc_len; i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
1993-04-26 12:09:02 +04:00
#if defined(BSD) && BSD >= 199103
len = sizeof ifr->ifr_name + ifr->ifr_addr.sa_len;
#else
len = sizeof ifc.ifc_len / sizeof(struct ifreq);
#endif
ifreq = *ifr;
1995-01-15 11:55:59 +03:00
if (ifreq.ifr_addr.sa_family != AF_INET)
1993-04-26 12:09:02 +04:00
continue;
1995-01-15 11:55:59 +03:00
if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) {
1993-04-26 12:09:02 +04:00
perror("ioctl(SIOCGIFFLAGS)");
continue;
}
1995-01-15 11:55:59 +03:00
if ((ifreq.ifr_flags & IFF_UP) == 0)
1993-04-26 12:09:02 +04:00
continue;
ifreq.ifr_flags &= (IFF_LOOPBACK | IFF_BROADCAST);
1995-01-15 11:55:59 +03:00
if (ifreq.ifr_flags == IFF_BROADCAST) {
if (ioctl(sock, SIOCGIFBRDADDR, &ifreq) < 0) {
1993-04-26 12:09:02 +04:00
perror("ioctl(SIOCGIFBRDADDR)");
continue;
}
1995-01-15 11:55:59 +03:00
} else if (ifreq.ifr_flags == IFF_LOOPBACK) {
if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
1993-04-26 12:09:02 +04:00
perror("ioctl(SIOCGIFADDR)");
continue;
}
} else
continue;
in = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
1995-01-15 11:55:59 +03:00
bindsin.sin_addr = in;
if (sendto(rpcsock, buf, outlen, 0, (struct sockaddr *)&bindsin,
sizeof bindsin) < 0)
1993-04-26 12:09:02 +04:00
perror("sendto");
}
close(sock);
return 0;
}
/*enum clnt_stat*/
handle_replies()
{
char buf[1400];
int fromlen, inlen;
struct sockaddr_in raddr;
struct rpc_msg msg;
XDR xdr;
recv_again:
1995-01-15 11:55:59 +03:00
memset(&xdr, 0, sizeof(xdr));
memset(&msg, 0, sizeof(msg));
1993-04-26 12:09:02 +04:00
msg.acpted_rply.ar_verf = _null_auth;
msg.acpted_rply.ar_results.where = (caddr_t)&rmtcr;
msg.acpted_rply.ar_results.proc = xdr_rmtcallres;
try_again:
fromlen = sizeof (struct sockaddr);
inlen = recvfrom(rpcsock, buf, sizeof buf, 0,
(struct sockaddr *)&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;
return RPC_CANTRECV;
}
1995-01-15 11:55:59 +03:00
if (inlen < sizeof(u_int32_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.
*/
xdrmem_create(&xdr, buf, (u_int)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)) {
1993-04-26 12:09:02 +04:00
raddr.sin_port = htons((u_short)rmtcr_port);
rpc_received(msg.rm_xid, &raddr, 0);
}
}
xdr.x_op = XDR_FREE;
msg.acpted_rply.ar_results.proc = xdr_void;
xdr_destroy(&xdr);
return RPC_SUCCESS;
}
/*enum clnt_stat*/
handle_ping()
{
char buf[1400];
int fromlen, inlen;
struct sockaddr_in raddr;
struct rpc_msg msg;
XDR xdr;
bool_t res;
recv_again:
1995-01-15 11:55:59 +03:00
memset(&xdr, 0, sizeof(xdr));
memset(&msg, 0, sizeof(msg));
msg.acpted_rply.ar_verf = _null_auth;
msg.acpted_rply.ar_results.where = (caddr_t)&res;
msg.acpted_rply.ar_results.proc = xdr_bool;
try_again:
fromlen = sizeof (struct sockaddr);
inlen = recvfrom(pingsock, buf, sizeof buf, 0,
(struct sockaddr *)&raddr, &fromlen);
1995-01-15 11:55:59 +03:00
if (inlen < 0) {
if (errno == EINTR)
goto try_again;
return RPC_CANTRECV;
}
1995-01-15 11:55:59 +03:00
if (inlen < sizeof(u_int32_t))
goto recv_again;
/*
* see if reply transaction id matches sent id.
* If so, decode the results.
*/
xdrmem_create(&xdr, buf, (u_int)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)) {
rpc_received(msg.rm_xid, &raddr, 0);
}
}
xdr.x_op = XDR_FREE;
msg.acpted_rply.ar_results.proc = xdr_void;
xdr_destroy(&xdr);
return RPC_SUCCESS;
}
1993-04-26 12:09:02 +04:00
/*
* LOOPBACK IS MORE IMPORTANT: PUT IN HACK
*/
rpc_received(dom, raddrp, force)
char *dom;
struct sockaddr_in *raddrp;
int force;
{
struct _dom_binding *ypdb;
struct iovec iov[2];
struct ypbind_resp ybr;
1993-04-26 12:09:02 +04:00
char path[MAXPATHLEN];
int fd;
/* XXX XXX USING POINTERS OVER THE NET LIKE THIS IS TOTALLY WRONG. */
1993-04-26 12:09:02 +04:00
/*printf("returned from %s about %s\n", inet_ntoa(raddrp->sin_addr), dom);*/
#ifdef __alpha__ /* XXX XXX */
/* you _REALLY_ do not want to know... */ /* XXX XXX */
dom = (char *)((u_long)dom | (u_long)0x100000000); /* XXX XXX */
#endif /* XXX XXX */
1995-01-15 11:55:59 +03:00
if (dom == NULL)
1993-04-26 12:09:02 +04:00
return;
1995-01-15 11:55:59 +03:00
for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
if (!strcmp(ypdb->dom_domain, dom))
1993-04-26 12:09:02 +04:00
break;
1995-01-15 11:55:59 +03:00
if (ypdb == NULL) {
if (force == 0)
1993-04-26 12:09:02 +04:00
return;
ypdb = (struct _dom_binding *)malloc(sizeof *ypdb);
1995-01-15 11:55:59 +03:00
memset(ypdb, 0, sizeof *ypdb);
1993-04-26 12:09:02 +04:00
strncpy(ypdb->dom_domain, dom, sizeof ypdb->dom_domain);
ypdb->dom_lockfd = -1;
ypdb->dom_pnext = ypbindlist;
ypbindlist = ypdb;
}
1994-07-01 23:40:23 +04:00
/* soft update, alive */
1995-01-15 11:55:59 +03:00
if (ypdb->dom_alive == 1 && force == 0) {
if (!memcmp(&ypdb->dom_server_addr, raddrp,
sizeof ypdb->dom_server_addr)) {
1994-07-01 23:40:23 +04:00
ypdb->dom_alive = 1;
ypdb->dom_check_t = time(NULL) + 60; /* recheck binding in 60 sec */
1994-07-01 23:40:23 +04:00
}
1993-04-26 12:09:02 +04:00
return;
1994-07-01 23:40:23 +04:00
}
1995-01-15 11:55:59 +03:00
memcpy(&ypdb->dom_server_addr, raddrp, sizeof ypdb->dom_server_addr);
ypdb->dom_check_t = time(NULL) + 60; /* recheck binding in 60 seconds */
1993-04-26 12:09:02 +04:00
ypdb->dom_vers = YPVERS;
ypdb->dom_alive = 1;
1995-01-15 11:55:59 +03:00
if (ypdb->dom_lockfd != -1)
1993-04-26 12:09:02 +04:00
close(ypdb->dom_lockfd);
sprintf(path, "%s/%s.%d", BINDINGDIR,
ypdb->dom_domain, ypdb->dom_vers);
#ifdef O_SHLOCK
1995-01-15 11:55:59 +03:00
if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1) {
1993-04-26 12:09:02 +04:00
(void)mkdir(BINDINGDIR, 0755);
1995-01-15 11:55:59 +03:00
if ((fd = open(path, O_CREAT|O_SHLOCK|O_RDWR|O_TRUNC, 0644)) == -1)
1993-04-26 12:09:02 +04:00
return;
}
#else
1995-01-15 11:55:59 +03:00
if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1) {
1993-04-26 12:09:02 +04:00
(void)mkdir(BINDINGDIR, 0755);
1995-01-15 11:55:59 +03:00
if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) == -1)
1993-04-26 12:09:02 +04:00
return;
}
flock(fd, LOCK_SH);
1993-09-05 20:10:01 +04:00
#endif
1993-04-26 12:09:02 +04:00
/*
* ok, if BINDINGDIR exists, and we can create the binding file,
* then write to it..
*/
ypdb->dom_lockfd = fd;
iov[0].iov_base = (caddr_t)&(udptransp->xp_port);
iov[0].iov_len = sizeof udptransp->xp_port;
iov[1].iov_base = (caddr_t)&ybr;
iov[1].iov_len = sizeof ybr;
1995-01-15 11:55:59 +03:00
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;
1995-01-15 11:55:59 +03:00
if (writev(ypdb->dom_lockfd, iov, 2) != iov[0].iov_len + iov[1].iov_len) {
1993-04-26 12:09:02 +04:00
perror("write");
close(ypdb->dom_lockfd);
unlink(path);
1993-04-26 12:09:02 +04:00
ypdb->dom_lockfd = -1;
return;
}
}