1997-07-18 11:40:42 +04:00
|
|
|
/* $NetBSD: ypwhich.c,v 1.8 1997/07/18 07:40:43 thorpej Exp $ */
|
1996-05-13 06:43:35 +04:00
|
|
|
|
1993-05-16 06:47:20 +04:00
|
|
|
/*
|
1994-05-25 13:52:05 +04:00
|
|
|
* Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
|
1993-05-16 06:47:20 +04:00
|
|
|
* 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.
|
1994-05-25 13:52:05 +04:00
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Theo de Raadt.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote
|
1993-05-16 06:47:20 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
|
|
|
__RCSID("$NetBSD: ypwhich.c,v 1.8 1997/07/18 07:40:43 thorpej Exp $");
|
1993-05-16 06:47:20 +04:00
|
|
|
#endif
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
1997-07-18 11:40:42 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <stdio.h>
|
1995-05-21 19:42:54 +04:00
|
|
|
#include <stdlib.h>
|
1997-07-18 11:40:42 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <netdb.h>
|
1997-07-18 11:40:42 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/xdr.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
const struct ypalias {
|
1993-04-26 12:09:02 +04:00
|
|
|
char *alias, *name;
|
|
|
|
} ypaliases[] = {
|
|
|
|
{ "passwd", "passwd.byname" },
|
|
|
|
{ "group", "group.byname" },
|
|
|
|
{ "networks", "networks.byaddr" },
|
|
|
|
{ "hosts", "hosts.byaddr" },
|
|
|
|
{ "protocols", "protocols.bynumber" },
|
|
|
|
{ "services", "services.byname" },
|
|
|
|
{ "aliases", "mail.aliases" },
|
|
|
|
{ "ethers", "ethers.byname" },
|
|
|
|
};
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
int main __P((int, char *[]));
|
|
|
|
int bind_host __P((char *dom, struct sockaddr_in *));
|
|
|
|
void usage __P((void));
|
1993-04-26 12:09:02 +04:00
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
extern char *__progname;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
1997-07-18 11:40:42 +04:00
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-04-26 12:09:02 +04:00
|
|
|
{
|
|
|
|
char *domainname, *master, *map;
|
|
|
|
struct ypmaplist *ypml, *y;
|
|
|
|
struct hostent *hent;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int notrans, mode, getmap;
|
|
|
|
int c, r, i;
|
|
|
|
|
|
|
|
yp_get_default_domain(&domainname);
|
|
|
|
|
|
|
|
map = NULL;
|
|
|
|
getmap = notrans = mode = 0;
|
1997-07-18 11:40:42 +04:00
|
|
|
while((c = getopt(argc, argv, "xd:mt")) != -1) {
|
|
|
|
switch (c) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case 'x':
|
1997-07-18 11:40:42 +04:00
|
|
|
for(i = 0;
|
|
|
|
i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
|
1993-04-26 12:09:02 +04:00
|
|
|
printf("Use \"%s\" for \"%s\"\n",
|
|
|
|
ypaliases[i].alias,
|
|
|
|
ypaliases[i].name);
|
|
|
|
exit(0);
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case 'd':
|
|
|
|
domainname = optarg;
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case 't':
|
|
|
|
notrans++;
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case 'm':
|
|
|
|
mode++;
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
1997-07-18 11:40:42 +04:00
|
|
|
}
|
|
|
|
|
1995-05-21 19:33:22 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-04-26 12:09:02 +04:00
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
if (domainname == NULL)
|
|
|
|
errx(1, "YP domain name not set");
|
|
|
|
|
|
|
|
if (mode == 0) {
|
|
|
|
switch (argc) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case 0:
|
1997-07-18 11:40:42 +04:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
1993-04-26 12:09:02 +04:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
if (bind_host(domainname, &sin))
|
1993-04-26 12:09:02 +04:00
|
|
|
exit(1);
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case 1:
|
1997-07-18 11:40:42 +04:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
1993-04-26 12:09:02 +04:00
|
|
|
sin.sin_family = AF_INET;
|
1995-05-21 19:33:22 +04:00
|
|
|
if (inet_aton(argv[0], &sin.sin_addr) == 0) {
|
|
|
|
hent = gethostbyname(argv[0]);
|
1997-07-18 11:40:42 +04:00
|
|
|
if (hent == NULL)
|
|
|
|
errx(1, "host %s unknown", argv[0]);
|
|
|
|
memcpy(&sin.sin_addr, hent->h_addr,
|
|
|
|
sizeof(sin.sin_addr));
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
1997-07-18 11:40:42 +04:00
|
|
|
if (bind_host(domainname, &sin))
|
1993-04-26 12:09:02 +04:00
|
|
|
exit(1);
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
if (argc > 1)
|
1993-04-26 12:09:02 +04:00
|
|
|
usage();
|
|
|
|
|
1997-07-18 11:40:42 +04:00
|
|
|
if (argc == 1) {
|
1995-05-21 19:33:22 +04:00
|
|
|
map = argv[0];
|
1997-07-18 11:40:42 +04:00
|
|
|
for (i = 0; (notrans == 0) &&
|
|
|
|
i < sizeof(ypaliases)/sizeof(ypaliases[0]); i++)
|
|
|
|
if (strcmp(map, ypaliases[i].alias) == 0)
|
1993-04-26 12:09:02 +04:00
|
|
|
map = ypaliases[i].name;
|
|
|
|
r = yp_master(domainname, map, &master);
|
1997-07-18 11:40:42 +04:00
|
|
|
switch (r) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case 0:
|
|
|
|
printf("%s\n", master);
|
|
|
|
free(master);
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case YPERR_YPBIND:
|
1997-07-18 11:40:42 +04:00
|
|
|
errx(1, "not running upbind");
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
default:
|
1997-07-18 11:40:42 +04:00
|
|
|
errx(1, "can't find master for map %s. Reason: %s",
|
|
|
|
map, yperr_string(r));
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ypml = NULL;
|
|
|
|
r = yp_maplist(domainname, &ypml);
|
1997-07-18 11:40:42 +04:00
|
|
|
switch (r) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case 0:
|
1997-07-18 11:40:42 +04:00
|
|
|
for (y = ypml; y != NULL; ) {
|
1993-04-26 12:09:02 +04:00
|
|
|
ypml = y;
|
|
|
|
r = yp_master(domainname, ypml->ypml_name, &master);
|
1997-07-18 11:40:42 +04:00
|
|
|
switch (r) {
|
1993-04-26 12:09:02 +04:00
|
|
|
case 0:
|
|
|
|
printf("%s %s\n", ypml->ypml_name, master);
|
|
|
|
free(master);
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
default:
|
1997-07-18 11:40:42 +04:00
|
|
|
warnx("can't find master for map %s. "
|
|
|
|
"Reason: %s", ypml->ypml_name,
|
|
|
|
yperr_string(r));
|
1993-04-26 12:09:02 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
y = ypml->ypml_next;
|
|
|
|
free(ypml);
|
|
|
|
}
|
|
|
|
break;
|
1997-07-18 11:40:42 +04:00
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
case YPERR_YPBIND:
|
1997-07-18 11:40:42 +04:00
|
|
|
errx(1, "not running ypbind");
|
|
|
|
|
1993-04-26 12:09:02 +04:00
|
|
|
default:
|
1997-07-18 11:40:42 +04:00
|
|
|
errx(1, "can't get map list for domain %s. Reason: %s",
|
|
|
|
domainname, yperr_string(r));
|
1993-04-26 12:09:02 +04:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
1997-07-18 11:40:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Like yp_bind except can query a specific host
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bind_host(dom, sin)
|
|
|
|
char *dom;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
{
|
|
|
|
struct hostent *hent = NULL;
|
|
|
|
struct ypbind_resp ypbr;
|
|
|
|
struct timeval tv;
|
|
|
|
CLIENT *client;
|
|
|
|
int sock, r;
|
|
|
|
struct in_addr ina;
|
|
|
|
|
|
|
|
sock = RPC_ANYSOCK;
|
|
|
|
tv.tv_sec = 15;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
|
|
|
|
if (client == NULL) {
|
|
|
|
warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND));
|
|
|
|
return YPERR_YPBIND;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = 5;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
r = clnt_call(client, YPBINDPROC_DOMAIN, xdr_ypdomain_wrap_string,
|
|
|
|
&dom, xdr_ypbind_resp, &ypbr, tv);
|
|
|
|
if (r != RPC_SUCCESS) {
|
|
|
|
warnx("can't cnlt_call: %s", yperr_string(YPERR_YPBIND));
|
|
|
|
clnt_destroy(client);
|
|
|
|
return YPERR_YPBIND;
|
|
|
|
} else {
|
|
|
|
if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
|
|
|
|
warnx("can't bind. Reason: %s\n",
|
|
|
|
yperr_string(ypbr.ypbind_status));
|
|
|
|
clnt_destroy(client);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clnt_destroy(client);
|
|
|
|
|
|
|
|
ina.s_addr =
|
|
|
|
ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
|
|
|
|
hent = gethostbyaddr((const char *)&ina.s_addr, sizeof(ina.s_addr),
|
|
|
|
AF_INET);
|
|
|
|
if (hent != NULL)
|
|
|
|
printf("%s\n", hent->h_name);
|
|
|
|
else
|
|
|
|
printf("%s\n", inet_ntoa(ina));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
|
|
|
|
fprintf(stderr, "usage: %s [-d domain] [[-t] -m [mapname] | host]\n",
|
|
|
|
__progname);
|
|
|
|
fprintf(stderr, " %s -x\n", __progname);
|
|
|
|
exit(1);
|
|
|
|
}
|