#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
static const struct ypalias {
char *alias, *name;
} ypaliases[] = {
{ "passwd", "passwd.byname" },
{ "master.passwd", "master.passwd.byname" },
{ "shadow", "shadow.byname" },
{ "group", "group.byname" },
{ "networks", "networks.byaddr" },
{ "hosts", "hosts.byname" },
{ "protocols", "protocols.bynumber" },
{ "services", "services.byname" },
{ "aliases", "mail.aliases" },
{ "ethers", "ethers.byname" },
};
static void
usage(void)
{
fprintf(stderr, "%s\n%s\n",
"usage: ypmatch [-kt] [-d domainname] key ... mapname",
" ypmatch -x");
exit(1);
}
int
main(int argc, char *argv[])
{
char *domainname, *inkey, *inmap, *outbuf;
int outbuflen, key, notrans, rval;
int c, r;
u_int i;
domainname = NULL;
notrans = key = 0;
while ((c = getopt(argc, argv, "xd:kt")) != -1)
switch (c) {
case 'x':
for (i = 0; i < nitems(ypaliases); i++)
printf("Use \"%s\" for \"%s\"\n",
ypaliases[i].alias,
ypaliases[i].name);
exit(0);
case 'd':
domainname = optarg;
break;
case 't':
notrans = 1;
break;
case 'k':
key = 1;
break;
default:
usage();
}
if (argc - optind < 2)
usage();
if (domainname == NULL)
yp_get_default_domain(&domainname);
inmap = argv[argc-1];
if (notrans == 0) {
for (i = 0; i < nitems(ypaliases); i++)
if (strcmp(inmap, ypaliases[i].alias) == 0)
inmap = ypaliases[i].name;
}
rval = 0;
for (; optind < argc - 1; optind++) {
inkey = argv[optind];
r = yp_match(domainname, inmap, inkey,
strlen(inkey), &outbuf, &outbuflen);
switch (r) {
case 0:
if (key)
printf("%s: ", inkey);
printf("%*.*s\n", outbuflen, outbuflen, outbuf);
break;
case YPERR_YPBIND:
errx(1, "not running ypbind");
default:
errx(1, "can't match key %s in map %s. reason: %s",
inkey, inmap, yperr_string(r));
rval = 1;
break;
}
}
exit(rval);
}