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