#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
#include "ypalias_init.h"
static void find_mapmaster(const char *, const char *, const char *,
int, int, int, const struct ypalias *);
static struct in_addr *find_server(const char *, const char *, int);
static CLIENT *mkclient(struct sockaddr_in *, unsigned long, unsigned long,
int);
static void usage(void) __attribute__((__noreturn__));
int
main(int argc, char *argv[])
{
const char *targhost = "localhost";
char *ourdomain;
int inhibit = 0, force = 0, tcp = 0;
char *targmap = NULL;
int ch, saw_m;
struct in_addr *inaddr;
struct hostent *he;
size_t i;
const struct ypalias *ypaliases;
ypaliases = ypalias_init();
(void)yp_get_default_domain(&ourdomain);
saw_m = 0;
while ((ch = getopt(argc, argv, "h:d:xtTfm")) != -1) {
switch (ch) {
case 'h':
targhost = optarg;
break;
case 'd':
ourdomain = optarg;
break;
case 'x':
for (i = 0; ypaliases[i].alias; i++)
(void)printf("Use \"%s\" for map \"%s\"\n",
ypaliases[i].alias, ypaliases[i].name);
return 0;
case 'f':
force = 1;
break;
case 't':
inhibit = 1;
break;
case 'T':
tcp = 1;
break;
case 'm':
if (optind < argc && argv[optind][0] != '-')
targmap = argv[optind++];
saw_m = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc) {
if (argc > 1)
usage();
targhost = argv[0];
}
#ifdef DEBUG
(void)printf("target_host=%s, domain=%s, inhibit=%d, saw_m=%d, map=%s, "
"force=%d, tcp=%d\n",
targhost, ourdomain, inhibit, saw_m, targmap, force, tcp);
#endif
if (ourdomain == NULL)
errx(1, "the domain hasn't been set on this machine.");
if (saw_m)
find_mapmaster(targhost, ourdomain, targmap, inhibit, force,
tcp, ypaliases);
else {
inaddr = find_server(targhost, ourdomain, tcp);
he = gethostbyaddr((void *)&inaddr->s_addr,
sizeof(inaddr->s_addr), AF_INET);
if (he)
(void)printf("%s\n", he->h_name);
else
(void)printf("%s\n", inet_ntoa(*inaddr));
}
return 0;
}
static void
usage(void)
{
const char *pname = getprogname();
(void)fprintf(stderr, "Usage:\t%s [-T] [-d domain] [[-h] host]\n"
"\t%s [-fTt] [-d domain] [-h host] -m [mapname]\n"
"\t%s [-T] -x\n", pname, pname, pname);
exit(1);
}
static CLIENT *
mkclient(struct sockaddr_in *sin, unsigned long prog, unsigned long vers,
int tcp)
{
static struct timeval tv = { 15, 0 };
int fd = RPC_ANYSOCK;
if (tcp)
return clnttcp_create(sin, prog, vers, &fd, 0, 0);
else
return clntudp_create(sin, prog, vers, tv, &fd);
}
static struct in_addr *
find_server(const char *host, const char *domain, int tcp)
{
static struct in_addr result;
struct sockaddr_in sin;
CLIENT *ypbind;
struct timeval tv;
enum clnt_stat retval;
struct ypbind_resp ypbind_resp;
(void)memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if (inet_aton(host, &sin.sin_addr) == 0) {
struct hostent *he;
he = gethostbyname(host);
if (he == NULL)
errx(1, "%s: %s", host, hstrerror(h_errno));
(void)memmove(&sin.sin_addr, he->h_addr, sizeof(sin.sin_addr));
}
ypbind = mkclient(&sin, YPBINDPROG, YPBINDVERS, tcp);
if (ypbind == NULL)
errx(1, "clnt%s_create: %s: %s", tcp ? "tcp" : "udp", host,
yperr_string(YPERR_YPBIND));
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = clnt_call(ypbind, (unsigned int)YPBINDPROC_DOMAIN,
xdr_ypdomain_wrap_string, &domain, xdr_ypbind_resp, &ypbind_resp,
tv);
clnt_destroy(ypbind);
if (retval != RPC_SUCCESS)
errx(1, "clnt_call: %s: %s", host, clnt_sperrno(retval));
if (ypbind_resp.ypbind_status != YPBIND_SUCC_VAL)
errx(1, "ypbind on %s for domain %s failed: %s", host, domain,
yperr_string(ypbind_resp.ypbind_status));
result.s_addr = ypbind_resp.ypbind_respbody.
ypbind_bindinfo.ypbind_binding_addr.s_addr;
return (&result);
}
static void
find_mapmaster(const char *host, const char *domain, const char *map,
int inhibit, int force, int tcp, const struct ypalias *ypaliases)
{
struct in_addr *inaddr, faddr;
struct hostent *he;
struct sockaddr_in sin;
CLIENT *ypserv;
int yperr;
enum clnt_stat retval;
struct timeval tv;
struct ypresp_maplist yprespmlist;
struct ypmaplist fakelist, *ypml;
struct ypresp_master yprespmaster;
struct ypreq_nokey ypreqkey;
size_t i;
if (force) {
if (inet_aton(host, &faddr) == 0) {
he = gethostbyname(host);
if (he == NULL)
errx(1, "%s: %s", host, hstrerror(h_errno));
(void)memmove(&faddr, he->h_addr, sizeof(faddr));
}
inaddr = &faddr;
} else {
inaddr = find_server(host, domain, tcp);
}
if (map && !inhibit) {
for (i = 0; ypaliases[i].alias; i++) {
if (strcmp(map, ypaliases[i].alias) == 0) {
map = ypaliases[i].name;
break;
}
}
#ifdef DEBUG
(void)printf("translated map name = %s\n", map);
#endif
}
(void)memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inaddr->s_addr;
ypserv = mkclient(&sin, YPPROG, YPVERS, tcp);
if (ypserv == NULL) {
warnx("clnt%s_create: %s: %s", tcp ? "tcp" : "udp", host,
yperr_string(YPERR_YPSERV));
goto error;
}
if (map == NULL) {
(void)memset(&yprespmlist, 0, sizeof(yprespmlist));
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = clnt_call(ypserv, (unsigned int)YPPROC_MAPLIST,
xdr_ypdomain_wrap_string, &domain, xdr_ypresp_maplist,
&yprespmlist, tv);
if (retval != RPC_SUCCESS) {
warnx("clnt_call MAPLIST: %s: %s", host,
clnt_sperrno(retval));
goto error;
}
yperr = ypprot_err(yprespmlist.status);
if (yperr) {
warnx("clnt_call: %s: %s", host, yperr_string(yperr));
goto error;
}
ypml = yprespmlist.list;
} else {
(void)memset(&fakelist, 0, sizeof(fakelist));
(void)strlcpy(fakelist.ypml_name, map, sizeof(fakelist.ypml_name));
fakelist.ypml_next = NULL;
ypml = &fakelist;
}
for ( ; ypml != NULL; ypml = ypml->ypml_next) {
ypreqkey.domain = domain;
ypreqkey.map = ypml->ypml_name;
(void)memset(&yprespmaster, 0, sizeof(yprespmaster));
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = clnt_call(ypserv, (unsigned int)YPPROC_MASTER,
xdr_ypreq_nokey, &ypreqkey, xdr_ypresp_master,
&yprespmaster, tv);
if (retval != RPC_SUCCESS) {
warnx("clnt_call MASTER: %s: %s", host,
clnt_sperrno(retval));
goto error;
}
yperr = ypprot_err(yprespmaster.status);
if (yperr) {
warnx("clnt_call: %s: %s: %s", host, ypml->ypml_name,
yperr_string(yperr));
} else {
(void)printf("%s %s\n", ypml->ypml_name,
yprespmaster.master);
}
xdr_free((xdrproc_t)xdr_ypresp_master, (void *)&yprespmaster);
}
clnt_destroy(ypserv);
return;
error:
if (ypserv)
clnt_destroy(ypserv);
if (!force)
(void)fprintf(stderr,
"\t[note %s's ypserv running on host %s]\n",
host, inet_ntoa(*inaddr));
exit(1);
}