Home | History | Annotate | Line # | Download | only in yppoll
yppoll.c revision 1.13.22.1
      1  1.13.22.1      matt /*	yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp	*/
      2        1.5   thorpej 
      3        1.1    brezak /*
      4        1.3   deraadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5       1.12       agc  * All rights reserved.
      6       1.12       agc  *
      7       1.12       agc  * Redistribution and use in source and binary forms, with or without
      8       1.12       agc  * modification, are permitted provided that the following conditions
      9       1.12       agc  * are met:
     10       1.12       agc  * 1. Redistributions of source code must retain the above copyright
     11       1.12       agc  *    notice, this list of conditions and the following disclaimer.
     12       1.12       agc  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.12       agc  *    notice, this list of conditions and the following disclaimer in the
     14       1.12       agc  *    documentation and/or other materials provided with the distribution.
     15       1.12       agc  *
     16       1.12       agc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17       1.12       agc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18       1.12       agc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.12       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20       1.12       agc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.12       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.12       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.12       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.12       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.12       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.12       agc  * SUCH DAMAGE.
     27       1.12       agc  */
     28       1.12       agc 
     29       1.12       agc /*
     30        1.3   deraadt  * Copyright (c) 1992, 1993 John Brezak
     31        1.1    brezak  * All rights reserved.
     32        1.1    brezak  *
     33        1.1    brezak  * Redistribution and use in source and binary forms, with or without
     34        1.1    brezak  * modification, are permitted provided that the following conditions
     35        1.1    brezak  * are met:
     36        1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     37        1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     38        1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     39        1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     40        1.1    brezak  *    documentation and/or other materials provided with the distribution.
     41       1.13  jdolecek  * 3. The name of the author may not be used to endorse or promote
     42        1.1    brezak  *    products derived from this software without specific prior written
     43        1.1    brezak  *    permission.
     44        1.1    brezak  *
     45        1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     46        1.1    brezak  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     47        1.1    brezak  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48        1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     49        1.1    brezak  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50        1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51        1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52        1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53        1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54        1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55        1.1    brezak  * SUCH DAMAGE.
     56        1.1    brezak  */
     57        1.1    brezak 
     58        1.6   thorpej #include <sys/cdefs.h>
     59        1.2   mycroft #ifndef lint
     60  1.13.22.1      matt __RCSID("yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp");
     61        1.2   mycroft #endif /* not lint */
     62        1.1    brezak 
     63        1.1    brezak #include <sys/param.h>
     64        1.1    brezak #include <sys/types.h>
     65        1.1    brezak #include <sys/socket.h>
     66        1.6   thorpej #include <err.h>
     67        1.1    brezak #include <stdio.h>
     68        1.8      matt #include <stdlib.h>
     69        1.1    brezak #include <time.h>
     70        1.4   deraadt #include <netdb.h>
     71        1.4   deraadt #include <unistd.h>
     72        1.4   deraadt #include <string.h>
     73        1.4   deraadt #include <netinet/in.h>
     74        1.4   deraadt #include <arpa/inet.h>
     75        1.1    brezak 
     76        1.1    brezak #include <rpc/rpc.h>
     77        1.1    brezak #include <rpc/xdr.h>
     78        1.1    brezak #include <rpcsvc/yp_prot.h>
     79        1.1    brezak #include <rpcsvc/ypclnt.h>
     80        1.1    brezak 
     81  1.13.22.1      matt static CLIENT *mkclient(struct sockaddr_in *, unsigned long, unsigned long,
     82  1.13.22.1      matt     int);
     83  1.13.22.1      matt static int get_remote_info(const char *, const char *, const char *, int *,
     84  1.13.22.1      matt     char **, int);
     85  1.13.22.1      matt static void usage(void) __attribute__((__noreturn__));
     86        1.6   thorpej 
     87        1.6   thorpej int
     88  1.13.22.1      matt main(int  argc, char *argv[])
     89        1.1    brezak {
     90        1.6   thorpej 	char *domainname;
     91        1.6   thorpej 	char *hostname = NULL;
     92        1.6   thorpej 	char *inmap, *master;
     93        1.6   thorpej 	int order;
     94  1.13.22.1      matt 	int c, r, tcp = 0;
     95        1.6   thorpej 
     96  1.13.22.1      matt 	(void)yp_get_default_domain(&domainname);
     97        1.6   thorpej 
     98  1.13.22.1      matt 	while ((c = getopt(argc, argv, "Th:d:")) != -1) {
     99        1.6   thorpej 		switch (c) {
    100  1.13.22.1      matt 		case 'T':
    101  1.13.22.1      matt 			tcp = 1;
    102  1.13.22.1      matt 			break;
    103        1.6   thorpej 		case 'd':
    104        1.6   thorpej 			domainname = optarg;
    105        1.6   thorpej 			break;
    106        1.6   thorpej 
    107        1.6   thorpej 		case 'h':
    108        1.6   thorpej 			hostname = optarg;
    109        1.6   thorpej 			break;
    110        1.6   thorpej 
    111        1.6   thorpej 		default:
    112        1.6   thorpej 			usage();
    113        1.6   thorpej 			/*NOTREACHED*/
    114        1.6   thorpej 		}
    115        1.6   thorpej 	}
    116        1.6   thorpej 
    117        1.6   thorpej 	if (domainname == NULL)
    118        1.6   thorpej 		errx(1, "YP domain name not set");
    119        1.6   thorpej 
    120        1.6   thorpej 	argc -= optind;
    121        1.6   thorpej 	argv += optind;
    122        1.6   thorpej 
    123        1.6   thorpej 	if (argc != 1)
    124        1.6   thorpej 		usage();
    125        1.6   thorpej 
    126        1.6   thorpej 	inmap = argv[0];
    127        1.6   thorpej 
    128        1.6   thorpej 	if (hostname != NULL)
    129        1.6   thorpej 		r = get_remote_info(domainname, inmap, hostname,
    130  1.13.22.1      matt 		    &order, &master, tcp);
    131        1.6   thorpej 	else {
    132        1.6   thorpej 		r = yp_order(domainname, inmap, &order);
    133        1.6   thorpej 		if (r == 0)
    134        1.6   thorpej 			r = yp_master(domainname, inmap, &master);
    135        1.6   thorpej 	}
    136        1.6   thorpej 
    137        1.6   thorpej 	if (r != 0)
    138       1.10     grant 		errx(1, "no such map %s. Reason: %s",
    139        1.6   thorpej 		    inmap, yperr_string(r));
    140        1.6   thorpej 
    141  1.13.22.1      matt 	(void)printf("Map %s has order number %d. %s", inmap, order,
    142       1.11   thorpej 	    ctime((void *)&order));
    143  1.13.22.1      matt 	(void)printf("The master server is %s.\n", master);
    144  1.13.22.1      matt 	return 0;
    145        1.1    brezak }
    146        1.1    brezak 
    147  1.13.22.1      matt static CLIENT *
    148  1.13.22.1      matt mkclient(struct sockaddr_in *sin, unsigned long prog, unsigned long vers,
    149  1.13.22.1      matt     int tcp)
    150  1.13.22.1      matt {
    151  1.13.22.1      matt 	static struct timeval tv = { 10, 0 };
    152  1.13.22.1      matt 	int fd = RPC_ANYSOCK;
    153  1.13.22.1      matt 
    154  1.13.22.1      matt 	if (tcp)
    155  1.13.22.1      matt 		return clnttcp_create(sin, prog, vers, &fd, 0, 0);
    156  1.13.22.1      matt 	else
    157  1.13.22.1      matt 		return clntudp_create(sin, prog, vers, tv, &fd);
    158  1.13.22.1      matt }
    159  1.13.22.1      matt 
    160  1.13.22.1      matt static int
    161  1.13.22.1      matt get_remote_info(const char *indomain, const char *inmap, const char *server,
    162  1.13.22.1      matt     int *outorder, char **outname, int tcp)
    163        1.4   deraadt {
    164        1.4   deraadt 	struct ypresp_order ypro;
    165        1.4   deraadt 	struct ypresp_master yprm;
    166        1.4   deraadt 	struct ypreq_nokey yprnk;
    167        1.4   deraadt 	struct timeval tv;
    168        1.4   deraadt 	int r;
    169        1.4   deraadt 	struct sockaddr_in rsrv_sin;
    170        1.4   deraadt 	CLIENT *client;
    171        1.4   deraadt 	struct hostent *h;
    172        1.4   deraadt 
    173  1.13.22.1      matt 	(void)memset(&rsrv_sin, 0, sizeof(rsrv_sin));
    174        1.4   deraadt 	rsrv_sin.sin_len = sizeof rsrv_sin;
    175        1.4   deraadt 	rsrv_sin.sin_family = AF_INET;
    176        1.4   deraadt 
    177        1.4   deraadt 	h = gethostbyname(server);
    178        1.4   deraadt 	if (h == NULL) {
    179        1.6   thorpej 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
    180        1.6   thorpej 			errx(1, "unknown host %s", server);
    181        1.6   thorpej 	} else
    182  1.13.22.1      matt 		(void)memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr,
    183  1.13.22.1      matt 		    (size_t)h->h_length);
    184        1.4   deraadt 
    185  1.13.22.1      matt 	client = mkclient(&rsrv_sin, YPPROG, YPVERS, tcp);
    186        1.6   thorpej 	if (client == NULL)
    187  1.13.22.1      matt 		errx(1, "clnt%s_create: no contact with host %s.",
    188  1.13.22.1      matt 		    tcp ? "tcp" : "udp", server);
    189        1.4   deraadt 
    190        1.4   deraadt 	yprnk.domain = indomain;
    191        1.4   deraadt 	yprnk.map = inmap;
    192        1.4   deraadt 
    193  1.13.22.1      matt 	(void)memset(&ypro, 0, sizeof(ypro));
    194  1.13.22.1      matt 
    195  1.13.22.1      matt 	tv.tv_sec = 10;
    196  1.13.22.1      matt 	tv.tv_usec = 0;
    197        1.4   deraadt 
    198  1.13.22.1      matt 	r = clnt_call(client, (unsigned int)YPPROC_ORDER, xdr_ypreq_nokey,
    199  1.13.22.1      matt 	    &yprnk, xdr_ypresp_order, &ypro, tv);
    200        1.4   deraadt 	if (r != RPC_SUCCESS)
    201        1.4   deraadt 		clnt_perror(client, "yp_order: clnt_call");
    202        1.4   deraadt 
    203        1.4   deraadt 	*outorder = ypro.ordernum;
    204  1.13.22.1      matt 	xdr_free(xdr_ypresp_order, (void *)&ypro);
    205        1.4   deraadt 
    206        1.4   deraadt 	r = ypprot_err(ypro.status);
    207        1.4   deraadt 	if (r == RPC_SUCCESS) {
    208  1.13.22.1      matt 		(void)memset(&yprm, 0, sizeof(yprm));
    209        1.4   deraadt 
    210  1.13.22.1      matt 		r = clnt_call(client, (unsigned int)YPPROC_MASTER,
    211  1.13.22.1      matt 		    xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv);
    212        1.4   deraadt 		if (r != RPC_SUCCESS)
    213        1.4   deraadt 			clnt_perror(client, "yp_master: clnt_call");
    214        1.4   deraadt 		r = ypprot_err(yprm.status);
    215        1.6   thorpej 		if (r == 0)
    216        1.4   deraadt 			*outname = (char *)strdup(yprm.master);
    217  1.13.22.1      matt 		xdr_free(xdr_ypresp_master, (void *)&yprm);
    218        1.4   deraadt 	}
    219        1.4   deraadt 	clnt_destroy(client);
    220        1.4   deraadt 	return r;
    221        1.4   deraadt }
    222        1.4   deraadt 
    223  1.13.22.1      matt static void
    224  1.13.22.1      matt usage(void)
    225        1.1    brezak {
    226        1.1    brezak 
    227  1.13.22.1      matt 	(void)fprintf(stderr,
    228  1.13.22.1      matt 	    "Usage: %s [-T] [-h host] [-d domainname] mapname\n",
    229        1.9       cgd 	    getprogname());
    230        1.6   thorpej 	exit(1);
    231        1.1    brezak }
    232