freebsd-dev/usr.bin/ypwhich/ypwhich.c

258 lines
6.4 KiB
C
Raw Normal View History

1994-08-08 01:13:35 +00:00
/*
* Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-08-08 01:13:35 +00:00
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <rpc/rpc.h>
#include <rpc/xdr.h>
#include <rpcsvc/yp_prot.h>
#include <rpcsvc/ypclnt.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <err.h>
1994-08-08 01:13:35 +00:00
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
1994-08-08 01:13:35 +00:00
#define ERR_USAGE 1 /* bad arguments - display 'usage' message */
#define ERR_NOSUCHHOST 2 /* no such host */
#define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */
#define ERR_NOYPBIND 4 /* ypbind not running */
#define ERR_NOMASTER 5 /* could not find master server */
1994-08-08 01:13:35 +00:00
extern bool_t xdr_domainname();
static const struct ypalias {
char *alias, *name;
} ypaliases[] = {
1994-08-08 01:13:35 +00:00
{ "passwd", "passwd.byname" },
{ "master.passwd", "master.passwd.byname" },
{ "shadow", "shadow.byname" },
1994-08-08 01:13:35 +00:00
{ "group", "group.byname" },
{ "networks", "networks.byaddr" },
{ "hosts", "hosts.byaddr" },
{ "protocols", "protocols.bynumber" },
{ "services", "services.byname" },
{ "aliases", "mail.aliases" },
{ "ethers", "ethers.byname" },
};
static void
usage(void)
1994-08-08 01:13:35 +00:00
{
fprintf(stderr, "%s\n%s\n",
"usage: ypwhich [-d domain] [[-t] -m [mname] | host]",
" ypwhich -x");
exit(ERR_USAGE);
1994-08-08 01:13:35 +00:00
}
/*
* Like yp_bind except can query a specific host
*/
static int
bind_host(char *dom, struct sockaddr_in *lsin)
1994-08-08 01:13:35 +00:00
{
struct hostent *hent = NULL;
struct ypbind_resp ypbr;
struct timeval tv;
CLIENT *client;
int sock, r;
struct in_addr ss_addr;
1994-08-08 01:13:35 +00:00
sock = RPC_ANYSOCK;
tv.tv_sec = 15;
tv.tv_usec = 0;
client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock);
if (client == NULL) {
warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND));
return (YPERR_YPBIND);
1994-08-08 01:13:35 +00:00
}
tv.tv_sec = 5;
tv.tv_usec = 0;
r = clnt_call(client, YPBINDPROC_DOMAIN,
(xdrproc_t)xdr_domainname, &dom,
(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
if (r != RPC_SUCCESS) {
warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
1994-08-08 01:13:35 +00:00
clnt_destroy(client);
return (YPERR_YPBIND);
1994-08-08 01:13:35 +00:00
} else {
if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
warnx("can't yp_bind: reason: %s",
ypbinderr_string(ypbr.ypbind_respbody.ypbind_error));
1994-08-08 01:13:35 +00:00
clnt_destroy(client);
return (r);
1994-08-08 01:13:35 +00:00
}
}
clnt_destroy(client);
ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
1994-08-08 01:13:35 +00:00
/*printf("%08x\n", ss_addr);*/
hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
if (hent)
printf("%s\n", hent->h_name);
else
printf("%s\n", inet_ntoa(ss_addr));
return (0);
1994-08-08 01:13:35 +00:00
}
1995-05-30 06:41:30 +00:00
1994-08-08 01:13:35 +00:00
int
main(int argc, char *argv[])
1994-08-08 01:13:35 +00:00
{
char *domnam = NULL, *master;
char *map = NULL;
1994-08-08 01:13:35 +00:00
struct ypmaplist *ypml, *y;
struct hostent *hent;
struct sockaddr_in lsin;
int notrans, mode;
int c, r;
u_int i;
1994-08-08 01:13:35 +00:00
notrans = mode = 0;
while ((c = getopt(argc, argv, "xd:mt")) != -1)
switch (c) {
1994-08-08 01:13:35 +00:00
case 'x':
for (i = 0; i < nitems(ypaliases); i++)
printf("\"%s\" is an alias for \"%s\"\n",
1994-08-08 01:13:35 +00:00
ypaliases[i].alias,
ypaliases[i].name);
exit(0);
case 'd':
domnam = optarg;
1994-08-08 01:13:35 +00:00
break;
case 't':
notrans++;
break;
case 'm':
mode++;
break;
default:
usage();
}
if (domnam == NULL)
yp_get_default_domain(&domnam);
if (mode == 0) {
switch (argc-optind) {
1994-08-08 01:13:35 +00:00
case 0:
bzero(&lsin, sizeof lsin);
lsin.sin_family = AF_INET;
lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1994-08-08 01:13:35 +00:00
if (bind_host(domnam, &lsin))
exit(ERR_NOBINDING);
1994-08-08 01:13:35 +00:00
break;
case 1:
bzero(&lsin, sizeof lsin);
lsin.sin_family = AF_INET;
if ((lsin.sin_addr.s_addr = inet_addr(argv[optind])) == INADDR_NONE) {
1994-08-08 01:13:35 +00:00
hent = gethostbyname(argv[optind]);
if (!hent)
errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]);
1994-08-08 01:13:35 +00:00
bcopy((char *)hent->h_addr_list[0],
(char *)&lsin.sin_addr, sizeof lsin.sin_addr);
1994-08-08 01:13:35 +00:00
}
if (bind_host(domnam, &lsin))
exit(ERR_NOBINDING);
1994-08-08 01:13:35 +00:00
break;
default:
usage();
}
exit(0);
}
if (argc-optind > 1)
1994-08-08 01:13:35 +00:00
usage();
if (argv[optind]) {
1994-08-08 01:13:35 +00:00
map = argv[optind];
if (notrans == 0) {
for (i = 0; i < nitems(ypaliases); i++)
if (strcmp(map, ypaliases[i].alias) == 0)
map = ypaliases[i].name;
}
r = yp_master(domnam, map, &master);
switch (r) {
1994-08-08 01:13:35 +00:00
case 0:
printf("%s\n", master);
free(master);
break;
case YPERR_YPBIND:
errx(ERR_NOYPBIND, "not running ypbind");
1994-08-08 01:13:35 +00:00
default:
errx(ERR_NOMASTER, "can't find master for map %s: reason: %s",
1994-08-08 01:13:35 +00:00
map, yperr_string(r));
}
exit(0);
}
ypml = NULL;
r = yp_maplist(domnam, &ypml);
switch (r) {
1994-08-08 01:13:35 +00:00
case 0:
for (y = ypml; y;) {
1994-08-08 01:13:35 +00:00
ypml = y;
r = yp_master(domnam, ypml->ypml_name, &master);
switch (r) {
1994-08-08 01:13:35 +00:00
case 0:
printf("%s %s\n", ypml->ypml_name, master);
1994-08-08 01:13:35 +00:00
free(master);
break;
default:
warnx("can't find the master of %s: reason: %s",
ypml->ypml_name, yperr_string(r));
1994-08-08 01:13:35 +00:00
break;
}
y = ypml->ypml_next;
1994-08-08 01:13:35 +00:00
free(ypml);
}
break;
case YPERR_YPBIND:
errx(ERR_NOYPBIND, "not running ypbind");
1994-08-08 01:13:35 +00:00
default:
errx(ERR_NOMASTER, "can't get map list for domain %s: reason: %s",
domnam, yperr_string(r));
1994-08-08 01:13:35 +00:00
}
exit(0);
}