#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
void usage(void);
struct ypalias {
char *alias, *name;
} ypaliases[] = {
{ "passwd", "passwd.byname" },
{ "group", "group.byname" },
{ "networks", "networks.byaddr" },
{ "hosts", "hosts.byname" },
{ "protocols", "protocols.bynumber" },
{ "services", "services.byname" },
{ "aliases", "mail.aliases" },
{ "ethers", "ethers.byname" },
};
void
usage(void)
{
fprintf(stderr,
"usage: ypmatch [-kt] [-d domain] key ... mapname\n"
" ypmatch -x\n");
exit(1);
}
int
main(int argc, char *argv[])
{
char *domainname, *inkey, *inmap, *outbuf;
extern char *optarg;
extern int optind;
int outbuflen, key, notrans, rval;
int c, r, i;
domainname = NULL;
notrans = key = 0;
while ((c=getopt(argc, argv, "xd:kt")) != -1)
switch (c) {
case 'x':
for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; 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) {
yp_get_default_domain(&domainname);
}
inmap = argv[argc-1];
if (!notrans) {
for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; 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:
fprintf(stderr, "yp_match: not running ypbind\n");
exit(1);
default:
fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
inkey, inmap, yperr_string(r));
rval = 1;
break;
}
}
exit(rval);
}