#include <sys/cdefs.h>
#include "yp.h"
#include "yp_extern.h"
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <rpc/rpc.h>
int children = 0;
#define MASTER_STRING "YP_MASTER_NAME"
#define MASTER_SZ sizeof(MASTER_STRING) - 1
#define ORDER_STRING "YP_LAST_MODIFIED"
#define ORDER_SZ sizeof(ORDER_STRING) - 1
static pid_t
yp_fork(void)
{
if (yp_pid != getpid()) {
yp_error("child %d trying to fork!", getpid());
errno = EEXIST;
return(-1);
}
return(fork());
}
void *
ypproc_null_2_svc(void *argp, struct svc_req *rqstp)
{
static char * result;
static char rval = 0;
#ifdef DB_CACHE
if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
#else
if (yp_access(NULL, (struct svc_req *)rqstp))
#endif
return(NULL);
result = &rval;
return((void *) &result);
}
bool_t *
ypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp)
{
static bool_t result;
#ifdef DB_CACHE
if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
#else
if (yp_access(NULL, (struct svc_req *)rqstp)) {
#endif
result = FALSE;
return (&result);
}
if (argp == NULL || yp_validdomain(*argp))
result = FALSE;
else
result = TRUE;
return (&result);
}
bool_t *
ypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp)
{
static bool_t result;
#ifdef DB_CACHE
if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
#else
if (yp_access(NULL, (struct svc_req *)rqstp))
#endif
return (NULL);
if (argp == NULL || yp_validdomain(*argp))
return (NULL);
else
result = TRUE;
return (&result);
}
ypresp_val *
ypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp)
{
static ypresp_val result;
result.val.valdat_val = "";
result.val.valdat_len = 0;
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return (&result);
}
if (argp->domain == NULL || argp->map == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
result.stat = yp_getbykey(&argp->key, &result.val);
#ifdef DB_CACHE
if (do_dns && result.stat != YP_TRUE &&
(yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) ||
(strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes")))) {
#else
if (do_dns && result.stat != YP_TRUE &&
(strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes"))) {
#endif
char *nbuf;
nbuf = alloca(argp->key.keydat_len + 1);
bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len);
nbuf[argp->key.keydat_len] = '\0';
if (debug)
yp_error("doing DNS lookup of %s", nbuf);
if (!strcmp(argp->map, "hosts.byname"))
result.stat = yp_async_lookup_name(rqstp, nbuf,
AF_INET);
else if (!strcmp(argp->map, "hosts.byaddr"))
result.stat = yp_async_lookup_addr(rqstp, nbuf,
AF_INET);
else if (!strcmp(argp->map, "ipnodes.byname"))
result.stat = yp_async_lookup_name(rqstp, nbuf,
AF_INET6);
else if (!strcmp(argp->map, "ipnodes.byaddr"))
result.stat = yp_async_lookup_addr(rqstp, nbuf,
AF_INET6);
if (result.stat == YP_TRUE)
return(NULL);
}
return (&result);
}
ypresp_key_val *
ypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_key_val result;
result.val.valdat_val = result.key.keydat_val = "";
result.val.valdat_len = result.key.keydat_len = 0;
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return (&result);
}
if (argp->domain == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
result.stat = yp_firstbykey(&result.key, &result.val);
return (&result);
}
ypresp_key_val *
ypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp)
{
static ypresp_key_val result;
result.val.valdat_val = result.key.keydat_val = "";
result.val.valdat_len = result.key.keydat_len = 0;
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return (&result);
}
if (argp->domain == NULL || argp->map == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
result.key.keydat_len = argp->key.keydat_len;
result.key.keydat_val = argp->key.keydat_val;
result.stat = yp_nextbykey(&result.key, &result.val);
return (&result);
}
static void
ypxfr_callback(ypxfrstat rval, struct sockaddr_in *addr, unsigned int transid,
unsigned int prognum, unsigned long port)
{
CLIENT *clnt;
int sock = RPC_ANYSOCK;
struct timeval timeout;
yppushresp_xfr ypxfr_resp;
struct rpc_err err;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
addr->sin_port = htons(port);
if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) {
yp_error("%s: %s", inet_ntoa(addr->sin_addr),
clnt_spcreateerror("failed to establish callback handle"));
return;
}
ypxfr_resp.status = rval;
ypxfr_resp.transid = transid;
timeout.tv_sec = 0;
if (clnt_control(clnt, CLSET_TIMEOUT, &timeout) == FALSE)
yp_error("failed to set timeout on ypproc_xfr callback");
if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) {
clnt_geterr(clnt, &err);
if (err.re_status != RPC_SUCCESS &&
err.re_status != RPC_TIMEDOUT)
yp_error("%s", clnt_sperror(clnt,
"ypxfr callback failed"));
}
clnt_destroy(clnt);
}
#define YPXFR_RETURN(CODE) \
\
result.xfrstat = CODE; \
svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_ypresp_xfr, &result); \
ypxfr_callback(CODE,rqhost,argp->transid, \
argp->prog,argp->port); \
return(NULL);
ypresp_xfr *
ypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp)
{
static ypresp_xfr result;
struct sockaddr_in *rqhost;
ypresp_master *mres;
ypreq_nokey mreq;
result.transid = argp->transid;
rqhost = svc_getcaller(rqstp->rq_xprt);
#ifdef DB_CACHE
if (yp_access(argp->map_parms.map,
argp->map_parms.domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) {
#endif
YPXFR_RETURN(YPXFR_REFUSED)
}
if (argp->map_parms.domain == NULL) {
YPXFR_RETURN(YPXFR_BADARGS)
}
if (yp_validdomain(argp->map_parms.domain)) {
YPXFR_RETURN(YPXFR_NODOM)
}
mreq.domain = argp->map_parms.domain;
mreq.map = argp->map_parms.map;
mres = ypproc_master_2_svc(&mreq, rqstp);
if (mres->stat != YP_TRUE) {
yp_error("couldn't find master for map %s@%s",
argp->map_parms.map,
argp->map_parms.domain);
yp_error("host at %s (%s) may be pulling my leg",
argp->map_parms.peer,
inet_ntoa(rqhost->sin_addr));
YPXFR_RETURN(YPXFR_REFUSED)
}
switch (yp_fork()) {
case 0:
{
char g[11], t[11], p[11];
char ypxfr_command[MAXPATHLEN + 2];
snprintf (ypxfr_command, sizeof(ypxfr_command), "%sypxfr", _PATH_LIBEXEC);
snprintf (t, sizeof(t), "%u", argp->transid);
snprintf (g, sizeof(g), "%u", argp->prog);
snprintf (p, sizeof(p), "%u", argp->port);
if (debug) {
close(0); close(1); close(2);
}
if (strcmp(yp_dir, _PATH_YP)) {
execl(ypxfr_command, "ypxfr",
"-d", argp->map_parms.domain,
"-h", mres->peer,
"-p", yp_dir, "-C", t,
g, inet_ntoa(rqhost->sin_addr),
p, argp->map_parms.map,
NULL);
} else {
execl(ypxfr_command, "ypxfr",
"-d", argp->map_parms.domain,
"-h", mres->peer,
"-C", t,
g, inet_ntoa(rqhost->sin_addr),
p, argp->map_parms.map,
NULL);
}
yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno));
YPXFR_RETURN(YPXFR_XFRERR)
_exit(0);
break;
}
case -1:
yp_error("ypxfr fork(): %s", strerror(errno));
YPXFR_RETURN(YPXFR_XFRERR)
break;
default:
result.xfrstat = YPXFR_SUCC;
children++;
break;
}
return (&result);
}
#undef YPXFR_RETURN
void *
ypproc_clear_2_svc(void *argp, struct svc_req *rqstp)
{
static char * result;
static char rval = 0;
#ifdef DB_CACHE
if (yp_access(NULL, NULL, (struct svc_req *)rqstp))
#else
if (yp_access(NULL, (struct svc_req *)rqstp))
#endif
return (NULL);
#ifdef DB_CACHE
yp_flush_all();
#endif
load_securenets();
result = &rval;
return((void *) &result);
}
static bool_t
xdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp)
{
while (1) {
if ((objp->ypresp_all_u.val.stat =
yp_nextbykey(&objp->ypresp_all_u.val.key,
&objp->ypresp_all_u.val.val)) == YP_TRUE) {
objp->more = TRUE;
} else {
objp->more = FALSE;
}
if (!xdr_ypresp_all(xdrs, objp))
return(FALSE);
if (objp->more == FALSE)
return(TRUE);
}
}
ypresp_all *
ypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_all result;
result.more = TRUE;
result.ypresp_all_u.val.key.keydat_len = 0;
result.ypresp_all_u.val.key.keydat_val = "";
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.ypresp_all_u.val.stat = YP_YPERR;
return (&result);
}
if (argp->domain == NULL || argp->map == NULL) {
result.ypresp_all_u.val.stat = YP_BADARGS;
return (&result);
}
if (children >= MAX_CHILDREN) {
result.ypresp_all_u.val.stat = YP_YPERR;
return(&result);
}
if (!debug) {
switch (yp_fork()) {
case 0:
break;
case -1:
yp_error("ypall fork(): %s", strerror(errno));
result.ypresp_all_u.val.stat = YP_YPERR;
return(&result);
break;
default:
children++;
return (NULL);
break;
}
}
#ifdef DB_CACHE
yp_flush_all();
#endif
if (yp_select_map(argp->map, argp->domain,
&result.ypresp_all_u.val.key, 0) != YP_TRUE) {
result.ypresp_all_u.val.stat = yp_errno;
return(&result);
}
svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_ypresp_all, &result);
if (!debug)
_exit(0);
return &result;
}
ypresp_master *
ypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_master result;
static char ypvalbuf[YPMAXRECORD];
keydat key = { MASTER_SZ, MASTER_STRING };
valdat val;
result.peer = "";
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return(&result);
}
if (argp->domain == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
result.stat = yp_getbykey(&key, &val);
if (result.stat == YP_TRUE) {
bcopy(val.valdat_val, &ypvalbuf, val.valdat_len);
ypvalbuf[val.valdat_len] = '\0';
result.peer = ypvalbuf;
} else
result.peer = "";
return (&result);
}
ypresp_order *
ypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp)
{
static ypresp_order result;
keydat key = { ORDER_SZ, ORDER_STRING };
valdat val;
result.ordernum = 0;
#ifdef DB_CACHE
if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) {
#else
if (yp_access(argp->map, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return(&result);
}
if (argp->domain == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) {
result.stat = yp_errno;
return(&result);
}
result.stat = yp_getbykey(&key, &val);
if (result.stat == YP_TRUE)
result.ordernum = atoi(val.valdat_val);
else
result.ordernum = 0;
return (&result);
}
static void yp_maplist_free(struct ypmaplist *yp_maplist)
{
register struct ypmaplist *next;
while (yp_maplist) {
next = yp_maplist->next;
free(yp_maplist->map);
free(yp_maplist);
yp_maplist = next;
}
}
static struct ypmaplist *
yp_maplist_create(const char *domain)
{
char yp_mapdir[MAXPATHLEN + 2];
char yp_mapname[MAXPATHLEN + 2];
struct ypmaplist *cur = NULL;
struct ypmaplist *yp_maplist = NULL;
DIR *dird;
struct dirent *dirp;
struct stat statbuf;
snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain);
if ((dird = opendir(yp_mapdir)) == NULL) {
yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno));
return(NULL);
}
while ((dirp = readdir(dird)) != NULL) {
if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) {
snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",
yp_mapdir,dirp->d_name);
if (stat(yp_mapname, &statbuf) < 0 ||
!S_ISREG(statbuf.st_mode))
continue;
if ((cur = (struct ypmaplist *)
malloc(sizeof(struct ypmaplist))) == NULL) {
yp_error("malloc() failed");
closedir(dird);
yp_maplist_free(yp_maplist);
return(NULL);
}
if ((cur->map = strdup(dirp->d_name)) == NULL) {
yp_error("strdup() failed: %s",strerror(errno));
closedir(dird);
yp_maplist_free(yp_maplist);
free(cur);
return(NULL);
}
cur->next = yp_maplist;
yp_maplist = cur;
if (debug)
yp_error("map: %s", yp_maplist->map);
}
}
closedir(dird);
return(yp_maplist);
}
ypresp_maplist *
ypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp)
{
static ypresp_maplist result = { 0, NULL };
#ifdef DB_CACHE
if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) {
#else
if (yp_access(NULL, (struct svc_req *)rqstp)) {
#endif
result.stat = YP_YPERR;
return(&result);
}
if (argp == NULL) {
result.stat = YP_BADARGS;
return (&result);
}
if (yp_validdomain(*argp)) {
result.stat = YP_NODOM;
return (&result);
}
yp_maplist_free(result.maps);
if ((result.maps = yp_maplist_create(*argp)) == NULL) {
yp_error("yp_maplist_create failed");
result.stat = YP_YPERR;
return(&result);
} else
result.stat = YP_TRUE;
return (&result);
}
void *
ypoldproc_null_1_svc(void *argp, struct svc_req *rqstp)
{
return(ypproc_null_2_svc(argp, rqstp));
}
bool_t *
ypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp)
{
return(ypproc_domain_2_svc(argp, rqstp));
}
bool_t *
ypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp)
{
return (ypproc_domain_nonack_2_svc(argp, rqstp));
}
ypresponse *
ypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
ypresp_val *v2_result;
result.yp_resptype = YPRESP_VAL;
result.ypresponse_u.yp_resp_valtype.val.valdat_val = "";
result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0;
if (argp->yp_reqtype != YPREQ_KEY) {
result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS;
return(&result);
}
v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
if (v2_result == NULL)
return(NULL);
bcopy(v2_result, &result.ypresponse_u.yp_resp_valtype,
sizeof(ypresp_val));
return (&result);
}
ypresponse *
ypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
ypresp_key_val *v2_result;
result.yp_resptype = YPRESP_KEY_VAL;
result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
if (argp->yp_reqtype != YPREQ_NOKEY) {
result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
return(&result);
}
v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype,
rqstp);
if (v2_result == NULL)
return(NULL);
bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
sizeof(ypresp_key_val));
return (&result);
}
ypresponse *
ypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
ypresp_key_val *v2_result;
result.yp_resptype = YPRESP_KEY_VAL;
result.ypresponse_u.yp_resp_key_valtype.val.valdat_val =
result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = "";
result.ypresponse_u.yp_resp_key_valtype.val.valdat_len =
result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0;
if (argp->yp_reqtype != YPREQ_KEY) {
result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS;
return(&result);
}
v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp);
if (v2_result == NULL)
return(NULL);
bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype,
sizeof(ypresp_key_val));
return (&result);
}
ypresponse *
ypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
ypresp_master *v2_result1;
ypresp_order *v2_result2;
result.yp_resptype = YPRESP_MAP_PARMS;
result.ypresponse_u.yp_resp_map_parmstype.domain =
argp->yprequest_u.yp_req_nokeytype.domain;
result.ypresponse_u.yp_resp_map_parmstype.map =
argp->yprequest_u.yp_req_nokeytype.map;
result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0;
result.ypresponse_u.yp_resp_map_parmstype.peer = "";
if (argp->yp_reqtype != YPREQ_MAP_PARMS) {
return(&result);
}
v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype,
rqstp);
if (v2_result1 == NULL)
return(NULL);
if (v2_result1->stat != YP_TRUE) {
return(&result);
}
v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype,
rqstp);
if (v2_result2 == NULL)
return(NULL);
if (v2_result2->stat != YP_TRUE) {
return(&result);
}
result.ypresponse_u.yp_resp_map_parmstype.peer =
v2_result1->peer;
result.ypresponse_u.yp_resp_map_parmstype.ordernum =
v2_result2->ordernum;
return (&result);
}
ypresponse *
ypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
return (&result);
}
ypresponse *
ypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
return (&result);
}
ypresponse *
ypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp)
{
static ypresponse result;
return (&result);
}