ypwhich.c revision a506a34ceb0e9dcc6c61bf0560202f8538928650
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* under license from the Regents of the University of
* California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This is a user command which tells which yp server is being used by a
* given machine, or which yp server is the master for a named map.
*
* Usage is:
* ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
* ypwhich -x
* where: the -d switch can be used to specify a domain other than the
* default domain. -m tells the master of that map. mname is a mapname
* If the -m option is used, ypwhich will act like a vanilla yp client,
* and will not attempt to choose a particular yp server. On the
* other hand, if no -m switch is used, ypwhich will talk directly to the yp
* bind process on the named host, or to the local ypbind process if no host
* name is specified. -t switch inhibits nickname translation of map names.
*
*/
#include <stdio.h>
#include <ctype.h>
#include "yp_b.h"
#include "ypv2_bind.h"
#include <string.h>
#include <netdir.h>
#include <unistd.h>
#include <netdb.h>
static char default_domain_name[YPMAXDOMAIN];
static int vers = YPBINDVERS;
static char default_host_name[256];
static bool get_master = FALSE;
static bool get_server = FALSE;
TIMEOUT, /* Seconds */
0 /* Microseconds */
};
static char nullstring[] = "\000";
static char err_usage[] =
"Usage:\n\
ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
ypwhich -x\n\
where\n\
mname may be either a mapname or a nickname for a map.\n\
host if specified, is the machine whose NIS server is to be found.\n\
-t inhibits map nickname translation.\n\
-Vn version of ypbind, V3 is default.\n\
-x dumps the map nickname translation table.\n";
static char err_bad_args[] =
"ypwhich: %s argument is bad.\n";
static char err_cant_get_kname[] =
"ypwhich: can't get %s back from system call.\n";
static char err_null_kname[] =
"ypwhich: the %s hasn't been set on this machine.\n";
static char err_bad_mapname[] = "mapname";
static char err_bad_domainname[] = "domainname";
static char err_bad_hostname[] = "hostname";
static void get_command_line_args();
static void getdomain();
static void getlochost();
static void get_server_name();
static int call_binder();
static void get_map_master();
extern void maketable();
extern int getmapname();
#ifdef DEBUG
static void dump_response();
#endif
static void dump_ypmaps();
static void dumpmaps();
static bool xdr_yp_inaddr();
static bool xdr_old_ypbind_resp();
static bool xdr_old_yp_binding();
static int old_call_binder();
static void print_server();
/* need these for call to (remote) V2 ypbind */
struct old_ypbind_binding {
unsigned short int ypbind_binding_port; /* In network order */
};
struct old_ypbind_resp {
enum ypbind_resptype ypbind_status;
union {
unsigned long ypbind_error;
struct old_ypbind_binding ypbind_bindinfo;
};
/*
* This is the main line for the ypwhich process.
*/
int
char **argv;
{
if (dodump) {
exit(0);
}
if (!domain) {
getdomain();
}
}
if (get_server) {
if (!host)
getlochost();
} else {
if (map)
else
dump_ypmaps();
}
return (0);
}
/*
* This does the command line argument processing.
*/
static void
int argc;
char **argv;
{
argv++;
if (argc == 1) {
get_server = TRUE;
return;
}
while (--argc) {
if ((*argv)[0] == '-') {
switch ((*argv)[1]) {
case 'V':
if (vers < 1) {
exit(1);
}
argv++;
break;
case 'm':
get_master = TRUE;
argv++;
if (argc > 1) {
if ((*(argv))[0] == '-') {
break;
}
argc--;
argv++;
exit(1);
}
}
break;
case 'd':
if (argc > 1) {
argv++;
argc--;
argv++;
exit(1);
}
} else {
exit(1);
}
break;
case 't':
argv++;
break;
case 'x':
argv++;
break;
default:
exit(1);
}
} else {
if (get_server) {
exit(1);
}
get_server = TRUE;
argv++;
exit(1);
}
}
}
if (get_master && get_server) {
exit(1);
}
if (!get_master && !get_server) {
get_server = TRUE;
}
}
/*
* This gets the local default domainname, and makes sure that it's set
* to something reasonable. domain is set here.
*/
static void
{
} else {
exit(1);
}
exit(1);
}
}
/*
* This gets the local hostname back from the kernel
*/
static void
{
} else {
exit(1);
}
}
/*
* This tries to find the name of the server to which the binder in question
* is bound. If one of the -Vx flags was specified, it will try only for
* that protocol version, otherwise, it will start with the current version,
* then drop back to the previous version.
*/
static void
{
char *notbound = "Domain %s not bound on %s.\n";
if (vers >= 3) {
if (!call_binder(vers))
} else {
if (!old_call_binder(vers))
}
}
extern CLIENT *__clnt_create_loopback();
/*
* This sends a message to the ypbind process on the node with
* the host name
*/
static int
int vers;
{
struct ypbind_resp *response;
struct ypbind_domain ypbd;
char errstring[256];
extern struct rpc_createerr rpc_createerr;
int yperr = 0;
const char *str;
/*
* CAUTION: Do not go to NIS if the host is the same as the local host
* XXX: Lots of special magic to distinguish between local and remote
* case. We want to make sure the local case doesn't hang.
*/
else
if (yperr)
else {
"ypwhich: %s is not running ypbind\n", host);
"ypwhich: %s is not running rpcbind\n",
host);
} else
(void) clnt_pcreateerror("ypwhich: \
clnt_create error");
}
exit(1);
}
"ypwhich: can't call ypbind on %s", host);
exit(1);
}
return (FALSE);
}
char *server =
/* depends on a hack in ypbind */
struct sockaddr_in *sa4;
struct sockaddr_in6 *sa6;
char buf[INET6_ADDRSTRLEN];
char xbuf[IPV6_ADDR_LEN];
int af;
void *addr;
sa6 = (struct sockaddr_in6 *)
xbuf;
} else {
sa4 = (struct sockaddr_in *)
xbuf;
}
} else {
sa6 = (struct sockaddr_in6 *)
} else {
}
}
perror("inet_ntop");
else
} else {
str = "<unknown>";
}
} else {
}
}
#ifdef DEBUG
#endif
return (TRUE);
}
/*
* Serializes/deserializes an in_addr struct.
*
* Note: There is a data coupling between the "definition" of a struct
* in_addr implicit in this xdr routine, and the true data definition in
*/
{
}
/*
* Serializes/deserializes an old ypbind_binding struct.
*/
struct old_ypbind_binding *ps;
{
}
/*
* Serializes/deserializes a ypbind_resp structure.
*/
struct old_ypbind_resp *ps;
{
return (FALSE);
}
switch (ps->ypbind_status) {
case YPBIND_SUCC_VAL:
return (xdr_old_yp_binding(xdrs,
case YPBIND_FAIL_VAL:
return (xdr_u_long(xdrs,
}
return (FALSE);
}
/* This sends a message to the old ypbind process on host. */
static int old_call_binder(vers)
int vers;
{
int sock = RPC_ANYSOCK;
struct old_ypbind_resp response;
char errstring[256];
extern struct rpc_createerr rpc_createerr;
(void) printf("ypwhich: %s is not running ypbind\n",
host);
exit(1);
}
(void) printf("ypwhich: %s is not running port mapper\n",
host);
exit(1);
}
(void) clnt_pcreateerror("ypwhich: clnt_create error");
exit(1);
}
timeout);
if ((rpc_stat != RPC_SUCCESS) &&
(rpc_stat != RPC_PROGVERSMISMATCH)) {
"ypwhich: can't call ypbind on %s", host);
exit(1);
}
if ((rpc_stat != RPC_SUCCESS) ||
return (FALSE);
}
return (TRUE);
}
/*
* For old version:
* This translates a server address to a name and prints it.
* We'll get a name by using the standard library routine.
*/
static void print_server(server)
{
char buf[256];
}
#ifdef DEBUG
static void
ypbind_resp * which;
{
ypbind_binding * b;
int i;
{
if (b == NULL)
else {
"server=%s lovers=%ld hivers=%ld\n",
b->ypbind_lo_vers, b->ypbind_hi_vers);
nc = b->ypbind_nconf;
ua = b->ypbind_svcaddr;
"ypwhich: NO netconfig information\n");
else {
"ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
}
"ypwhich: NO netbuf information available from binder\n");
else {
else
}
}
}
}
}
#endif
/*
* This translates a server address to a name and prints it. If the address
* is the same as the local address as returned by get_myaddress, the name
* is that retrieved from the kernel. If it's any other address (including
* another ip address for the local machine), we'll get a name by using the
* standard library routine (which calls the yp).
*/
/*
* This asks any yp server for the map's master.
*/
static void
{
int err;
char *master;
if (err) {
"ypwhich: Can't find the master of %s. Reason: %s.\n",
exit(1);
} else {
}
}
/*
* This enumerates the entries within map "ypmaps" in the domain at global
* "domain", and prints them out key and value per single line. dump_ypmaps
* just decides whether we are (probably) able to speak the new YP protocol,
* and dispatches to the appropriate function.
*/
static void
{
int err;
struct dom_binding *binding;
"dump_ypmaps: Can't bind for domain %s. Reason: %s\n",
return;
}
}
}
static void
struct dom_binding *binding;
{
int err;
char *master;
struct ypresp_maplist maplist;
timeout);
if (rpc_stat != RPC_SUCCESS) {
"ypwhich(dumpmaps): can't get maplist");
exit(1);
}
"ypwhich: Can't get maplist. Reason: %s.\n",
exit(1);
}
if (err) {
(void) printf("????????\n");
"ypwhich: Can't find the master of %s. Reason: %s.\n",
} else {
}
}
}