2015-08-25 15:27:32 +00:00
|
|
|
/* $OpenBSD: ypwhich.c,v 1.23 2015/02/08 23:40:35 deraadt Exp $ */
|
|
|
|
/* $NetBSD: ypwhich.c,v 1.6 1996/05/13 02:43:48 thorpej Exp $ */
|
|
|
|
|
2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-NetBSD
|
|
|
|
*
|
2015-08-25 15:27:32 +00:00
|
|
|
* Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
|
1994-08-08 01:13:35 +00: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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 22:08:58 +00:00
|
|
|
#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>
|
2001-12-11 22:08:58 +00:00
|
|
|
|
1998-06-12 13:04:59 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2001-12-11 22:08:58 +00:00
|
|
|
|
2009-12-13 03:14:06 +00:00
|
|
|
#include <ctype.h>
|
1997-08-29 11:56:51 +00:00
|
|
|
#include <err.h>
|
1994-08-08 01:13:35 +00:00
|
|
|
#include <netdb.h>
|
1997-08-29 11:56:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1994-08-08 01:13:35 +00:00
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/xdr.h>
|
|
|
|
#include <rpcsvc/yp.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
1995-04-22 00:23:59 +00:00
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
#include "yplib_host.h"
|
1994-08-08 01:13:35 +00:00
|
|
|
|
2015-07-29 02:21:35 +00:00
|
|
|
static const struct ypalias {
|
2003-10-26 05:30:40 +00:00
|
|
|
char *alias, *name;
|
2015-07-29 02:21:35 +00:00
|
|
|
} ypaliases[] = {
|
1994-08-08 01:13:35 +00:00
|
|
|
{ "passwd", "passwd.byname" },
|
1997-07-12 14:34:26 +00:00
|
|
|
{ "master.passwd", "master.passwd.byname" },
|
2009-06-25 16:15:39 +00:00
|
|
|
{ "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" },
|
|
|
|
};
|
|
|
|
|
1997-08-29 11:56:51 +00:00
|
|
|
static void
|
2001-12-11 22:08:58 +00:00
|
|
|
usage(void)
|
1994-08-08 01:13:35 +00:00
|
|
|
{
|
2015-08-25 15:27:32 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: ypwhich [-t] [-d domain] [[-h] host]\n"
|
|
|
|
" ypwhich [-t] [-d domain] [-h host] -m [mname]\n"
|
|
|
|
" ypwhich -x\n");
|
|
|
|
exit(1);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like yp_bind except can query a specific host
|
|
|
|
*/
|
2001-12-11 22:08:58 +00:00
|
|
|
static int
|
2015-08-25 15:27:32 +00:00
|
|
|
bind_host(char *dom, struct sockaddr_in *sin)
|
1994-08-08 01:13:35 +00:00
|
|
|
{
|
|
|
|
struct hostent *hent = NULL;
|
|
|
|
struct ypbind_resp ypbr;
|
2015-08-25 15:27:32 +00:00
|
|
|
struct in_addr ss_addr;
|
1994-08-08 01:13:35 +00:00
|
|
|
struct timeval tv;
|
|
|
|
CLIENT *client;
|
|
|
|
int sock, r;
|
|
|
|
|
|
|
|
sock = RPC_ANYSOCK;
|
|
|
|
tv.tv_sec = 15;
|
|
|
|
tv.tv_usec = 0;
|
2015-08-25 15:27:32 +00:00
|
|
|
client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
|
|
|
|
|
2002-02-06 13:30:31 +00:00
|
|
|
if (client == NULL) {
|
2015-08-25 15:27:32 +00:00
|
|
|
warnx("host is not bound to a ypmaster");
|
2002-02-06 13:30:31 +00:00
|
|
|
return (YPERR_YPBIND);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = 5;
|
|
|
|
tv.tv_usec = 0;
|
2015-08-25 15:27:32 +00:00
|
|
|
|
1994-08-08 01:13:35 +00:00
|
|
|
r = clnt_call(client, YPBINDPROC_DOMAIN,
|
2003-10-26 05:30:40 +00:00
|
|
|
(xdrproc_t)xdr_domainname, &dom,
|
|
|
|
(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
|
2002-02-06 13:30:31 +00:00
|
|
|
if (r != RPC_SUCCESS) {
|
1997-08-29 11:56:51 +00:00
|
|
|
warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
|
1994-08-08 01:13:35 +00:00
|
|
|
clnt_destroy(client);
|
2002-02-06 13:30:31 +00:00
|
|
|
return (YPERR_YPBIND);
|
1994-08-08 01:13:35 +00:00
|
|
|
} else {
|
|
|
|
if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
|
1997-08-29 11:56:51 +00:00
|
|
|
warnx("can't yp_bind: reason: %s",
|
2015-08-25 15:27:32 +00:00
|
|
|
yperr_string(ypbr.ypbind_status));
|
1994-08-08 01:13:35 +00:00
|
|
|
clnt_destroy(client);
|
2002-02-06 13:30:31 +00:00
|
|
|
return (r);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
clnt_destroy(client);
|
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
|
|
|
|
sizeof (ss_addr));
|
|
|
|
|
|
|
|
hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
|
|
|
|
AF_INET);
|
|
|
|
if (hent != NULL)
|
1994-08-08 01:13:35 +00:00
|
|
|
printf("%s\n", hent->h_name);
|
|
|
|
else
|
|
|
|
printf("%s\n", inet_ntoa(ss_addr));
|
2015-08-25 15:27:32 +00:00
|
|
|
|
2002-02-06 13:30:31 +00:00
|
|
|
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
|
2001-12-11 22:08:58 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-08-08 01:13:35 +00:00
|
|
|
{
|
2015-08-25 15:27:32 +00:00
|
|
|
char *domain, *master, *map = NULL, *host = NULL;
|
|
|
|
int notrans = 0, mode = 0, c, r, i;
|
1994-08-08 01:13:35 +00:00
|
|
|
struct ypmaplist *ypml, *y;
|
2015-08-25 15:27:32 +00:00
|
|
|
struct sockaddr_in sin;
|
1994-08-08 01:13:35 +00:00
|
|
|
struct hostent *hent;
|
2015-08-25 15:27:32 +00:00
|
|
|
CLIENT *client = NULL;
|
1994-08-08 01:13:35 +00:00
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
yp_get_default_domain(&domain);
|
|
|
|
if (domain == NULL)
|
|
|
|
errx(1, "YP domain name not set");
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
|
2002-02-06 13:30:31 +00:00
|
|
|
switch (c) {
|
1994-08-08 01:13:35 +00:00
|
|
|
case 'x':
|
2015-08-13 02:36:37 +00:00
|
|
|
for (i = 0; i < nitems(ypaliases); i++)
|
1995-07-20 16:30:15 +00:00
|
|
|
printf("\"%s\" is an alias for \"%s\"\n",
|
1994-08-08 01:13:35 +00:00
|
|
|
ypaliases[i].alias,
|
|
|
|
ypaliases[i].name);
|
|
|
|
exit(0);
|
2015-08-25 15:27:32 +00:00
|
|
|
case 'h':
|
|
|
|
host = optarg;
|
|
|
|
break;
|
1994-08-08 01:13:35 +00:00
|
|
|
case 'd':
|
2015-08-25 15:27:32 +00:00
|
|
|
domain = optarg;
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2015-08-25 15:27:32 +00:00
|
|
|
notrans = 1;
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2015-08-25 15:27:32 +00:00
|
|
|
mode = 1;
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
2015-08-25 15:27:32 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1997-08-29 11:56:51 +00:00
|
|
|
|
2002-02-06 13:30:31 +00:00
|
|
|
if (mode == 0) {
|
2015-08-25 15:27:32 +00:00
|
|
|
switch (argc) {
|
1994-08-08 01:13:35 +00:00
|
|
|
case 0:
|
2015-08-25 15:27:32 +00:00
|
|
|
memset(&sin, 0, sizeof sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
1994-08-08 01:13:35 +00:00
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
if (bind_host(domain, &sin))
|
|
|
|
exit(1);
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2015-08-25 15:27:32 +00:00
|
|
|
bzero(&sin, sizeof sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
if (inet_aton(argv[0], &sin.sin_addr) == 0) {
|
|
|
|
hent = gethostbyname(argv[0]);
|
|
|
|
if (!hent) {
|
|
|
|
errx(1, "host %s unknown",
|
|
|
|
argv[0]);
|
|
|
|
}
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
2015-08-25 15:27:32 +00:00
|
|
|
if (bind_host(domain, &sin))
|
|
|
|
exit(1);
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
if (argc > 1)
|
1994-08-08 01:13:35 +00:00
|
|
|
usage();
|
|
|
|
|
2015-08-25 15:27:32 +00:00
|
|
|
if (host != NULL)
|
|
|
|
client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
|
|
|
|
|
|
|
|
if (argv[0]) {
|
|
|
|
map = argv[0];
|
2015-08-13 02:36:37 +00:00
|
|
|
if (notrans == 0) {
|
|
|
|
for (i = 0; i < nitems(ypaliases); i++)
|
|
|
|
if (strcmp(map, ypaliases[i].alias) == 0)
|
|
|
|
map = ypaliases[i].name;
|
|
|
|
}
|
2015-08-25 15:27:32 +00:00
|
|
|
|
|
|
|
if (host != NULL)
|
|
|
|
r = yp_master_host(client, domain, map, &master);
|
|
|
|
else
|
|
|
|
r = yp_master(domain, map, &master);
|
|
|
|
|
2002-02-06 13:30:31 +00:00
|
|
|
switch (r) {
|
1994-08-08 01:13:35 +00:00
|
|
|
case 0:
|
|
|
|
printf("%s\n", master);
|
|
|
|
free(master);
|
|
|
|
break;
|
|
|
|
case YPERR_YPBIND:
|
2015-08-25 15:27:32 +00:00
|
|
|
errx(1, "not running ypbind");
|
1994-08-08 01:13:35 +00:00
|
|
|
default:
|
2015-08-25 15:27:32 +00:00
|
|
|
errx(1, "can't find master for map %s: reason: %s",
|
|
|
|
map, yperr_string(r));
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ypml = NULL;
|
2015-08-25 15:27:32 +00:00
|
|
|
if (host != NULL)
|
|
|
|
r = yp_maplist_host(client, domain, &ypml);
|
|
|
|
else
|
|
|
|
r = yp_maplist(domain, &ypml);
|
|
|
|
|
|
|
|
r = 0;
|
2002-02-06 13:30:31 +00:00
|
|
|
switch (r) {
|
1994-08-08 01:13:35 +00:00
|
|
|
case 0:
|
2015-08-25 15:27:32 +00:00
|
|
|
for (y = ypml; y; ) {
|
1994-08-08 01:13:35 +00:00
|
|
|
ypml = y;
|
2015-08-25 15:27:32 +00:00
|
|
|
if (host != NULL) {
|
|
|
|
r = yp_master_host(client,
|
|
|
|
domain, ypml->map, &master);
|
|
|
|
} else {
|
|
|
|
r = yp_master(domain, ypml->map, &master);
|
|
|
|
}
|
2002-02-06 13:30:31 +00:00
|
|
|
switch (r) {
|
1994-08-08 01:13:35 +00:00
|
|
|
case 0:
|
2015-08-25 15:27:32 +00:00
|
|
|
printf("%s %s\n", ypml->map, master);
|
1994-08-08 01:13:35 +00:00
|
|
|
free(master);
|
|
|
|
break;
|
|
|
|
default:
|
1997-08-29 11:56:51 +00:00
|
|
|
warnx("can't find the master of %s: reason: %s",
|
2015-08-25 15:27:32 +00:00
|
|
|
ypml->map, yperr_string(r));
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-25 15:27:32 +00:00
|
|
|
y = ypml->next;
|
1994-08-08 01:13:35 +00:00
|
|
|
free(ypml);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YPERR_YPBIND:
|
2015-08-25 15:27:32 +00:00
|
|
|
errx(1, "not running ypbind");
|
1994-08-08 01:13:35 +00:00
|
|
|
default:
|
2015-08-25 15:27:32 +00:00
|
|
|
errx(1, "can't get map list for domain %s: reason: %s",
|
|
|
|
domain, yperr_string(r));
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|