Home | History | Annotate | Line # | Download | only in yppoll
yppoll.c revision 1.13
      1  1.13  jdolecek /*	$NetBSD: 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  jdolecek __RCSID("$NetBSD: 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.6   thorpej int	main __P((int, char *[]));
     82   1.6   thorpej int	get_remote_info __P((char *, char *, char *, int *, char **));
     83   1.6   thorpej void	usage __P((void));
     84   1.6   thorpej 
     85   1.6   thorpej int
     86   1.6   thorpej main(argc, argv)
     87   1.6   thorpej 	int  argc;
     88   1.6   thorpej 	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.6   thorpej 	int c, r;
     95   1.6   thorpej 
     96   1.6   thorpej 	yp_get_default_domain(&domainname);
     97   1.6   thorpej 
     98   1.6   thorpej 	while ((c = getopt(argc, argv, "h:d:")) != -1) {
     99   1.6   thorpej 		switch (c) {
    100   1.6   thorpej 		case 'd':
    101   1.6   thorpej 			domainname = optarg;
    102   1.6   thorpej 			break;
    103   1.6   thorpej 
    104   1.6   thorpej 		case 'h':
    105   1.6   thorpej 			hostname = optarg;
    106   1.6   thorpej 			break;
    107   1.6   thorpej 
    108   1.6   thorpej 		default:
    109   1.6   thorpej 			usage();
    110   1.6   thorpej 			/*NOTREACHED*/
    111   1.6   thorpej 		}
    112   1.6   thorpej 	}
    113   1.6   thorpej 
    114   1.6   thorpej 	if (domainname == NULL)
    115   1.6   thorpej 		errx(1, "YP domain name not set");
    116   1.6   thorpej 
    117   1.6   thorpej 	argc -= optind;
    118   1.6   thorpej 	argv += optind;
    119   1.6   thorpej 
    120   1.6   thorpej 	if (argc != 1)
    121   1.6   thorpej 		usage();
    122   1.6   thorpej 
    123   1.6   thorpej 	inmap = argv[0];
    124   1.6   thorpej 
    125   1.6   thorpej 	if (hostname != NULL)
    126   1.6   thorpej 		r = get_remote_info(domainname, inmap, hostname,
    127   1.6   thorpej 		    &order, &master);
    128   1.6   thorpej 	else {
    129   1.6   thorpej 		r = yp_order(domainname, inmap, &order);
    130   1.6   thorpej 		if (r == 0)
    131   1.6   thorpej 			r = yp_master(domainname, inmap, &master);
    132   1.6   thorpej 	}
    133   1.6   thorpej 
    134   1.6   thorpej 	if (r != 0)
    135  1.10     grant 		errx(1, "no such map %s. Reason: %s",
    136   1.6   thorpej 		    inmap, yperr_string(r));
    137   1.6   thorpej 
    138   1.6   thorpej 	printf("Map %s has order number %d. %s", inmap, order,
    139  1.11   thorpej 	    ctime((void *)&order));
    140   1.6   thorpej 	printf("The master server is %s.\n", master);
    141   1.6   thorpej 	exit(0);
    142   1.1    brezak }
    143   1.1    brezak 
    144   1.1    brezak int
    145   1.4   deraadt get_remote_info(indomain, inmap, server, outorder, outname)
    146   1.4   deraadt 	char *indomain;
    147   1.4   deraadt 	char *inmap;
    148   1.4   deraadt 	char *server;
    149   1.4   deraadt 	int *outorder;
    150   1.4   deraadt 	char **outname;
    151   1.4   deraadt {
    152   1.4   deraadt 	struct ypresp_order ypro;
    153   1.4   deraadt 	struct ypresp_master yprm;
    154   1.4   deraadt 	struct ypreq_nokey yprnk;
    155   1.4   deraadt 	struct timeval tv;
    156   1.4   deraadt 	int r;
    157   1.4   deraadt 	struct sockaddr_in rsrv_sin;
    158   1.4   deraadt 	int rsrv_sock;
    159   1.4   deraadt 	CLIENT *client;
    160   1.4   deraadt 	struct hostent *h;
    161   1.4   deraadt 
    162   1.6   thorpej 	memset(&rsrv_sin, 0, sizeof(rsrv_sin));
    163   1.4   deraadt 	rsrv_sin.sin_len = sizeof rsrv_sin;
    164   1.4   deraadt 	rsrv_sin.sin_family = AF_INET;
    165   1.4   deraadt 	rsrv_sock = RPC_ANYSOCK;
    166   1.4   deraadt 
    167   1.4   deraadt 	h = gethostbyname(server);
    168   1.4   deraadt 	if (h == NULL) {
    169   1.6   thorpej 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
    170   1.6   thorpej 			errx(1, "unknown host %s", server);
    171   1.6   thorpej 	} else
    172   1.6   thorpej 		memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr, h->h_length);
    173   1.4   deraadt 
    174   1.4   deraadt 	tv.tv_sec = 10;
    175   1.4   deraadt 	tv.tv_usec = 0;
    176   1.4   deraadt 
    177   1.4   deraadt 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
    178   1.6   thorpej 	if (client == NULL)
    179  1.10     grant 		errx(1, "clntudp_create: no contact with host %s.", server);
    180   1.4   deraadt 
    181   1.4   deraadt 	yprnk.domain = indomain;
    182   1.4   deraadt 	yprnk.map = inmap;
    183   1.4   deraadt 
    184   1.6   thorpej 	memset(&ypro, 0, sizeof(ypro));
    185   1.4   deraadt 
    186   1.4   deraadt 	r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
    187   1.4   deraadt 	    xdr_ypresp_order, &ypro, tv);
    188   1.4   deraadt 	if (r != RPC_SUCCESS)
    189   1.4   deraadt 		clnt_perror(client, "yp_order: clnt_call");
    190   1.4   deraadt 
    191   1.4   deraadt 	*outorder = ypro.ordernum;
    192   1.4   deraadt 	xdr_free(xdr_ypresp_order, (char *)&ypro);
    193   1.4   deraadt 
    194   1.4   deraadt 	r = ypprot_err(ypro.status);
    195   1.4   deraadt 	if (r == RPC_SUCCESS) {
    196   1.6   thorpej 		memset(&yprm, 0, sizeof(yprm));
    197   1.4   deraadt 
    198   1.4   deraadt 		r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
    199   1.4   deraadt 		    &yprnk, xdr_ypresp_master, &yprm, tv);
    200   1.4   deraadt 		if (r != RPC_SUCCESS)
    201   1.4   deraadt 			clnt_perror(client, "yp_master: clnt_call");
    202   1.4   deraadt 		r = ypprot_err(yprm.status);
    203   1.6   thorpej 		if (r == 0)
    204   1.4   deraadt 			*outname = (char *)strdup(yprm.master);
    205   1.4   deraadt 		xdr_free(xdr_ypresp_master, (char *)&yprm);
    206   1.4   deraadt 	}
    207   1.4   deraadt 	clnt_destroy(client);
    208   1.4   deraadt 	return r;
    209   1.4   deraadt }
    210   1.4   deraadt 
    211   1.6   thorpej void
    212   1.6   thorpej usage()
    213   1.1    brezak {
    214   1.1    brezak 
    215   1.6   thorpej 	fprintf(stderr, "usage: %s [-h host] [-d domainname] mapname\n",
    216   1.9       cgd 	    getprogname());
    217   1.6   thorpej 	exit(1);
    218   1.1    brezak }
    219