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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LINT
|
|
|
|
static char rcsid[] = "ypwhich.c,v 1.2 1993/05/16 02:49:10 deraadt Exp";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/xdr.h>
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
|
1995-04-22 00:23:59 +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();
|
|
|
|
|
|
|
|
struct ypalias {
|
|
|
|
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" },
|
|
|
|
};
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage:\n");
|
|
|
|
fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
|
|
|
|
fprintf(stderr, "\typwhich -x\n");
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_USAGE);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Like yp_bind except can query a specific host
|
|
|
|
*/
|
|
|
|
bind_host(dom, sin)
|
|
|
|
char *dom;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
{
|
|
|
|
struct hostent *hent = NULL;
|
|
|
|
struct ypbind_resp ypbr;
|
|
|
|
struct dom_binding *ysd;
|
|
|
|
struct timeval tv;
|
|
|
|
CLIENT *client;
|
|
|
|
int sock, r;
|
|
|
|
u_long ss_addr;
|
|
|
|
|
|
|
|
sock = RPC_ANYSOCK;
|
|
|
|
tv.tv_sec = 15;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
|
|
|
|
if(client==NULL) {
|
|
|
|
fprintf(stderr, "can't clntudp_create: %s\n",
|
|
|
|
yperr_string(YPERR_YPBIND));
|
|
|
|
return YPERR_YPBIND;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv.tv_sec = 5;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
r = clnt_call(client, YPBINDPROC_DOMAIN,
|
|
|
|
xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
|
|
|
|
if( r != RPC_SUCCESS) {
|
|
|
|
fprintf(stderr, "can't clnt_call: %s\n",
|
|
|
|
yperr_string(YPERR_YPBIND));
|
|
|
|
clnt_destroy(client);
|
|
|
|
return YPERR_YPBIND;
|
|
|
|
} else {
|
|
|
|
if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
|
|
|
|
fprintf(stderr, "can't yp_bind: Reason: %s\n",
|
ypbind.c: Major overhaul.
- Moved to a more client-driven model. We aggressively attempt to keep
the default domain bound (as before) but we give up on non-default
domains if we lose contact with a server and fail to get a response
after one round of broadcasting. This helps drastically reduce the
amount of network bandwitdh that ypbind consumes: if a client references
the secondary domain at some later point, this will prod ypbind into
establishing a new binding anyway, so continuously broadcasting without
need is pointless.
Note that we still actively seek out a binding for our default domain
even if no client program has queried us yet. I'm not exactly sure if
this matches SunOS's behavior or not, but I decided to do it this way
since we can get into all sorts of trouble if our default domain comes
unbound. Even so, we're still much quieter than we used to be.
- Removed a bunch of no-longer pertinent comments and a couple of
chunks of #ifdef 0'ed code that no longer fit in to the new layout.
- Theo deRaadt must have become frustrated with the callback mechanism
in clnt_broadcast(), because he shamelessly stole the clnt_broadcast()
code right out of the RPC library and hacked it up to suit his needs.
(Comments and all! :)
I can understand why: clnt_broadcast() blocks while awaiting replies.
Changing this behavior requires surgery. However, you can work around
this: fork the broadcast into a child process and relay the results
back to the parent via a pipe. (Careful obervation has shown that the
SunOS ypbind forks children for broadcasting too, though I can only
guess what sort of interprocess communication it uses. pipe() seems to
do the job well enough.)
This may seem like the long way around, but it's not really that
hard to implement, and I'd prefer to use documented RPC library functions
wherever possible. We're careful to limit the number of simultaneous
broadcasters to avoid swamping the system (the current limit is 5).
Each clnt_broadcast() call only sends out a small number of packets
at increasing intervals. We're also careful not to spawn more than one
bradcaster for a given domain.
- Used clntudp_bufcreate() and clnt_call() to implement a ping()
function for directly querying a particular server so that we can
check if it's still alive. This lets me completely remove the old
bradcasting code and use actual RPC library calls instead, at the
cost of more than a few handfulls of torn-out hair. (Make no mistake
folks: I *HATE* RPC.) Currently, the ping interval is one minute.
- Fixed another potential 'nfds too big for select()' bug: use
_rpc_dtablesize() instead of getdtablesize().
- Quieted gcc -Wall a bit.
- Probably a bunch of other stuff that I've forgotten.
ypbind.8:
- Updated man page to reflect modifications.
ypwhich.c:
- Small mind-o fix from last time: decode error results from
ypbind correctly (*groan*)
yplib.c:
- same as above
- Change behavior of _yp_dobind() a little: if we get back a 'Domain
not bound' error for a given domain, retry a few times before giving
up and passing the error back to the caller. We have to sleep for a
few seconds between tries since the 'Domain not bound' error comes
back immediately (by repeatedly looping, we end up pounding on ypbind).
We retry at most 20 times at 5 second intervals. This gives us a full
minute to get a response. This seems to deviate a bit from SunOS
behavior -- it appears to wait forever -- but I don't like the idea
of perpetually hanging inside a library call.
Note that this should fix the problems some people have with bindings
not being established fast enough at boot time; sometimes amd is started
in /etc/rc after ypbind has run but before it gets a binding set up. The
automounter gets annoyed at this and tends to exit. By pausing ther YP
calls until a binding is ready, we avoid this situation.
- Another _yp_dobind() change: if we determine that our binding files
are unlocked or nonexistent, jump directly to code that pokes ypbind
into restablishing the binding. Again, if it fails, we'll time out
eventually and return.
1995-04-26 19:03:16 +00:00
|
|
|
ypbinderr_string(ypbr.ypbind_respbody.ypbind_error));
|
1994-08-08 01:13:35 +00:00
|
|
|
clnt_destroy(client);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clnt_destroy(client);
|
|
|
|
|
|
|
|
ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
|
|
|
|
/*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;
|
|
|
|
}
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-08-08 01:13:35 +00:00
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
char *domainname, *master, *map;
|
|
|
|
struct ypmaplist *ypml, *y;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
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;
|
|
|
|
while( (c=getopt(argc, argv, "xd:mt")) != -1)
|
|
|
|
switch(c) {
|
|
|
|
case 'x':
|
|
|
|
for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; 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);
|
|
|
|
case 'd':
|
|
|
|
domainname = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
notrans++;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
mode++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mode==0) {
|
|
|
|
switch(argc-optind) {
|
|
|
|
case 0:
|
|
|
|
bzero(&sin, sizeof sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
|
|
|
|
if(bind_host(domainname, &sin))
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOBINDING);
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bzero(&sin, sizeof sin);
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
if( (sin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) {
|
|
|
|
hent = gethostbyname(argv[optind]);
|
|
|
|
if(!hent) {
|
|
|
|
fprintf(stderr, "ypwhich: host %s unknown\n",
|
|
|
|
argv[optind]);
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOSUCHHOST);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
bcopy((char *)hent->h_addr_list[0],
|
|
|
|
(char *)&sin.sin_addr, sizeof sin.sin_addr);
|
|
|
|
}
|
|
|
|
if(bind_host(domainname, &sin))
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOBINDING);
|
1994-08-08 01:13:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( argc-optind > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if(argv[optind]) {
|
|
|
|
map = argv[optind];
|
|
|
|
for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
|
|
|
|
if( strcmp(map, ypaliases[i].alias) == 0)
|
|
|
|
map = ypaliases[i].name;
|
|
|
|
r = yp_master(domainname, map, &master);
|
|
|
|
switch(r) {
|
|
|
|
case 0:
|
|
|
|
printf("%s\n", master);
|
|
|
|
free(master);
|
|
|
|
break;
|
|
|
|
case YPERR_YPBIND:
|
|
|
|
fprintf(stderr, "ypwhich: not running ypbind\n");
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOYPBIND);
|
1994-08-08 01:13:35 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
|
|
|
|
map, yperr_string(r));
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOMASTER);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ypml = NULL;
|
|
|
|
r = yp_maplist(domainname, &ypml);
|
|
|
|
switch(r) {
|
|
|
|
case 0:
|
|
|
|
for(y=ypml; y; ) {
|
|
|
|
ypml = y;
|
|
|
|
r = yp_master(domainname, ypml->ypml_name, &master);
|
|
|
|
switch(r) {
|
|
|
|
case 0:
|
|
|
|
printf("%s %s\n", ypml->ypml_name, master);
|
|
|
|
free(master);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
|
|
|
"YP: can't find the master of %s: Reason: %s\n",
|
|
|
|
ypml->ypml_name, yperr_string(r));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
y = ypml->ypml_next;
|
|
|
|
free(ypml);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case YPERR_YPBIND:
|
|
|
|
fprintf(stderr, "ypwhich: not running ypbind\n");
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOYPBIND);
|
1994-08-08 01:13:35 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
|
|
|
|
domainname, yperr_string(r));
|
1995-04-22 00:23:59 +00:00
|
|
|
exit(ERR_NOMASTER);
|
1994-08-08 01:13:35 +00:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|