#include <sys/cdefs.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <rpc/clnt.h>
#include <rpc/pmap_clnt.h>
#include <rpcsvc/yp.h>
#include <rpcsvc/ypclnt.h>
#include "ypxfr_extern.h"
#include "yppush_extern.h"
char *progname = "yppush";
int debug = 1;
int _rpcpmstart = 0;
char *yp_dir = _PATH_YP;
static char *yppush_mapname = NULL;
static char *yppush_domain = NULL;
static char *yppush_master = NULL;
static int skip_master = 0;
static int verbose = 0;
static unsigned long yppush_transid = 0;
static int yppush_timeout = 80;
static int yppush_jobs = 1;
static int yppush_running_jobs = 0;
struct jobs {
unsigned long tid;
int port;
ypxfrstat stat;
unsigned long prognum;
char *server;
char *map;
int polled;
struct jobs *next;
};
static struct jobs *yppush_joblist;
static int yppush_svc_run(int);
static const char *
yppusherr_string(int err)
{
switch (err) {
case YPPUSH_TIMEDOUT:
return("transfer or callback timed out");
case YPPUSH_YPSERV:
return("failed to contact ypserv");
case YPPUSH_NOHOST:
return("no such host");
case YPPUSH_PMAP:
return("portmapper failure");
default:
return("unknown error code");
}
}
static int
yppush_show_status(ypxfrstat status, unsigned long tid)
{
struct jobs *job;
job = yppush_joblist;
while (job != NULL) {
if (job->tid == tid)
break;
job = job->next;
}
if (job == NULL) {
yp_error("warning: received callback with invalid transaction ID: %lu",
tid);
return (0);
}
if (job->polled) {
yp_error("warning: received callback with duplicate transaction ID: %lu",
tid);
return (0);
}
if (verbose > 1) {
yp_error("checking return status: transaction ID: %lu",
job->tid);
}
if (status != YPXFR_SUCC || verbose) {
yp_error("transfer of map %s to server %s %s",
job->map, job->server, status == YPXFR_SUCC ?
"succeeded" : "failed");
yp_error("status returned by ypxfr: %s", status > YPXFR_AGE ?
yppusherr_string(status) :
ypxfrerr_string(status));
}
job->polled = 1;
svc_unregister(job->prognum, 1);
yppush_running_jobs--;
return(0);
}
static void
yppush_exit(int now)
{
struct jobs *jptr;
int still_pending = 1;
while (!now && still_pending) {
jptr = yppush_joblist;
still_pending = 0;
while (jptr) {
if (jptr->polled == 0) {
still_pending++;
if (verbose > 1)
yp_error("%s has not responded",
jptr->server);
} else {
if (verbose > 1)
yp_error("%s has responded",
jptr->server);
}
jptr = jptr->next;
}
if (still_pending) {
if (verbose > 1)
yp_error("%d transfer%sstill pending",
still_pending,
still_pending > 1 ? "s " : " ");
if (yppush_svc_run (YPPUSH_RESPONSE_TIMEOUT) == 0) {
yp_error("timed out");
now = 1;
}
} else {
if (verbose)
yp_error("all transfers complete");
break;
}
}
jptr = yppush_joblist;
while (jptr) {
if (!jptr->polled)
yp_error("warning: exiting with transfer \
to %s (transid = %lu) still pending", jptr->server, jptr->tid);
svc_unregister(jptr->prognum, 1);
jptr = jptr->next;
}
exit(0);
}
static void
handler(int sig)
{
yppush_exit (1);
}
static int
yppush_svc_run(int timeout_secs)
{
int rc;
fd_set readfds;
struct timeval timeout;
timeout.tv_usec = 0;
timeout.tv_sec = timeout_secs;
retry:
readfds = svc_fdset;
rc = select(svc_maxfd + 1, &readfds, NULL, NULL, &timeout);
switch (rc) {
case -1:
if (errno == EINTR)
goto retry;
yp_error("select failed: %s", strerror(errno));
break;
case 0:
yp_error("select() timed out");
break;
default:
svc_getreqset(&readfds);
break;
}
return rc;
}
void *
yppushproc_null_1_svc(void *argp, struct svc_req *rqstp)
{
static char * result;
return((void *) &result);
}
void *
yppushproc_xfrresp_1_svc(yppushresp_xfr *argp, struct svc_req *rqstp)
{
static char * result;
yppush_show_status(argp->status, argp->transid);
return((void *) &result);
}
static int
yppush_send_xfr(struct jobs *job)
{
ypreq_xfr req;
DBT key, data;
CLIENT *clnt;
struct rpc_err err;
struct timeval timeout;
timeout.tv_usec = 0;
timeout.tv_sec = 0;
key.data = "YP_LAST_MODIFIED";
key.size = sizeof ("YP_LAST_MODIFIED") - 1;
if (yp_get_record(yppush_domain, yppush_mapname, &key, &data,
1) != YP_TRUE) {
yp_error("failed to read order number from %s: %s: %s",
yppush_mapname, yperr_string(yp_errno),
strerror(errno));
return(1);
}
req.map_parms.ordernum = atoi(data.data);
req.map_parms.domain = yppush_domain;
req.map_parms.peer = yppush_master;
req.map_parms.map = job->map;
req.transid = job->tid;
req.prog = job->prognum;
req.port = job->port;
if ((clnt = clnt_create(job->server, YPPROG, YPVERS, "udp")) == NULL) {
yp_error("%s: %s",job->server,clnt_spcreateerror("couldn't \
create udp handle to NIS server"));
switch (rpc_createerr.cf_stat) {
case RPC_UNKNOWNHOST:
job->stat = YPPUSH_NOHOST;
break;
case RPC_PMAPFAILURE:
job->stat = YPPUSH_PMAP;
break;
default:
job->stat = YPPUSH_RPC;
break;
}
return(1);
}
if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE)
yp_error("failed to set timeout on ypproc_xfr call");
if (ypproc_xfr_2(&req, clnt) == NULL) {
clnt_geterr(clnt, &err);
if (err.re_status != RPC_SUCCESS &&
err.re_status != RPC_TIMEDOUT) {
yp_error("%s: %s", job->server, clnt_sperror(clnt,
"yp_xfr failed"));
job->stat = YPPUSH_YPSERV;
clnt_destroy(clnt);
return(1);
}
}
clnt_destroy(clnt);
return(0);
}
static int
yp_push(char *server, char *map, unsigned long tid)
{
unsigned long prognum;
int sock = RPC_ANYSOCK;
SVCXPRT *xprt;
struct jobs *job;
if ((job = (struct jobs *)malloc(sizeof (struct jobs))) == NULL) {
yp_error("malloc failed");
yppush_exit (1);
}
xprt = svcudp_create(sock);
for (prognum = 0x40000000; prognum < 0x5FFFFFFF; prognum++) {
if (svc_register(xprt, prognum, 1,
yppush_xfrrespprog_1, IPPROTO_UDP) == TRUE)
break;
}
if (prognum == 0x5FFFFFFF) {
yp_error ("can't register yppush_xfrrespprog_1");
yppush_exit (1);
}
job->stat = 0;
job->tid = tid;
job->port = xprt->xp_port;
job->server = strdup(server);
job->map = strdup(map);
job->prognum = prognum;
job->polled = 0;
job->next = yppush_joblist;
yppush_joblist = job;
if (verbose) {
yp_error("initiating transfer: %s -> %s (transid = %lu)",
yppush_mapname, server, tid);
}
if (yppush_send_xfr(job)){
yppush_show_status(job->stat ? job->stat :
YPPUSH_YPSERV,job->tid);
} else {
if (verbose > 1)
yp_error("%s has been called", server);
}
return(0);
}
static int
yppush_foreach(int status, char *key, int keylen, char *val, int vallen,
char *data)
{
char *server;
if (status != YP_TRUE)
return (status);
asprintf(&server, "%.*s", vallen, val);
if (server == NULL)
return (0);
if (skip_master && strcasecmp(server, yppush_master) == 0) {
free(server);
return (0);
}
while (yppush_running_jobs >= yppush_jobs && (yppush_svc_run (yppush_timeout) > 0))
;
if (yp_push(server, yppush_mapname, yppush_transid)) {
free(server);
return(yp_errno);
}
yppush_running_jobs++;
yppush_transid++;
free(server);
return (0);
}
static void
usage()
{
fprintf (stderr, "%s\n%s\n",
"usage: yppush [-d domain] [-t timeout] [-j #parallel jobs] [-h host]",
" [-p path] mapname");
exit(1);
}
int
main(int argc, char *argv[])
{
int ch;
DBT key, data;
char myname[MAXHOSTNAMELEN];
struct hostlist {
char *name;
struct hostlist *next;
};
struct hostlist *yppush_hostlist = NULL;
struct hostlist *tmp;
while ((ch = getopt(argc, argv, "d:j:p:h:t:v")) != -1) {
switch (ch) {
case 'd':
yppush_domain = optarg;
break;
case 'j':
yppush_jobs = atoi(optarg);
if (yppush_jobs <= 0)
yppush_jobs = 1;
break;
case 'p':
yp_dir = optarg;
break;
case 'h':
if ((tmp = (struct hostlist *)malloc(sizeof(struct hostlist))) == NULL) {
yp_error("malloc failed");
yppush_exit(1);
}
tmp->name = strdup(optarg);
tmp->next = yppush_hostlist;
yppush_hostlist = tmp;
break;
case 't':
yppush_timeout = atoi(optarg);
break;
case 'v':
verbose++;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
yppush_mapname = argv[0];
if (yppush_mapname == NULL) {
usage();
}
if (yppush_domain == NULL) {
char *yppush_check_domain;
if (!yp_get_default_domain(&yppush_check_domain) &&
!_yp_check(&yppush_check_domain)) {
yp_error("no domain specified and NIS not running");
usage();
} else
yp_get_default_domain(&yppush_domain);
}
if (gethostname ((char *)&myname, sizeof(myname))) {
yp_error("failed to get name of local host: %s",
strerror(errno));
yppush_exit(1);
}
key.data = "YP_MASTER_NAME";
key.size = sizeof("YP_MASTER_NAME") - 1;
if (yp_get_record(yppush_domain, yppush_mapname,
&key, &data, 1) != YP_TRUE) {
yp_error("couldn't open %s map: %s", yppush_mapname,
strerror(errno));
yppush_exit(1);
}
if (strncasecmp(myname, data.data, data.size) == 0) {
if (yppush_hostlist == NULL)
skip_master = 1;
} else {
yp_error("warning: this host is not the master for %s",
yppush_mapname);
#ifdef NITPICKY
yppush_exit(1);
#endif
}
yppush_master = malloc(data.size + 1);
strncpy(yppush_master, data.data, data.size);
yppush_master[data.size] = '\0';
signal(SIGTERM, handler);
signal(SIGINT, handler);
yppush_transid = time((time_t *)NULL);
if (yppush_hostlist) {
tmp = yppush_hostlist;
while (tmp) {
yppush_foreach(YP_TRUE, NULL, 0, tmp->name,
strlen(tmp->name), NULL);
tmp = tmp->next;
}
} else {
ypxfr_get_map("ypservers", yppush_domain,
"localhost", yppush_foreach);
}
if (verbose > 1)
yp_error("all jobs dispatched");
yppush_exit(0);
exit(0);
}