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