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