1287424Saraujo/* $OpenBSD: yppoll.c,v 1.15 2015/01/16 06:40:22 deraadt Exp $ */ 2287424Saraujo/* $NetBSD: yppoll.c,v 1.5 1996/05/13 02:46:36 thorpej Exp $ */ 3287424Saraujo 41929Swollman/* 5287424Saraujo * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@openbsd.org> 6287424Saraujo * Copyright (c) 1992, 1993 John Brezak 71929Swollman * All rights reserved. 81929Swollman * 91929Swollman * Redistribution and use in source and binary forms, with or without 101929Swollman * modification, are permitted provided that the following conditions 111929Swollman * are met: 121929Swollman * 1. Redistributions of source code must retain the above copyright 131929Swollman * notice, this list of conditions and the following disclaimer. 141929Swollman * 2. Redistributions in binary form must reproduce the above copyright 151929Swollman * notice, this list of conditions and the following disclaimer in the 161929Swollman * documentation and/or other materials provided with the distribution. 171929Swollman * 3. The name of the author may not be used to endorse or promote 181929Swollman * products derived from this software without specific prior written 191929Swollman * permission. 201929Swollman * 211929Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 221929Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 231929Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241929Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 251929Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261929Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271929Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281929Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291929Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301929Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311929Swollman * SUCH DAMAGE. 321929Swollman */ 331929Swollman 34114601Sobrien#include <sys/cdefs.h> 35114601Sobrien__FBSDID("$FreeBSD: releng/11.0/usr.sbin/yppoll/yppoll.c 287424 2015-09-03 07:12:40Z araujo $"); 361929Swollman 371929Swollman#include <sys/param.h> 381929Swollman#include <sys/types.h> 391929Swollman#include <sys/socket.h> 40287424Saraujo 41287424Saraujo#include <arpa/inet.h> 42287424Saraujo#include <netinet/in.h> 43287424Saraujo 44287424Saraujo#include <ctype.h> 4530778Scharnier#include <err.h> 46287424Saraujo#include <netdb.h> 471929Swollman#include <stdio.h> 4878720Sdd#include <stdlib.h> 49287424Saraujo#include <string.h> 501929Swollman#include <time.h> 5130778Scharnier#include <unistd.h> 521929Swollman 531929Swollman#include <rpc/rpc.h> 541929Swollman#include <rpc/xdr.h> 551929Swollman#include <rpcsvc/yp_prot.h> 561929Swollman#include <rpcsvc/ypclnt.h> 571929Swollman 5830778Scharnierstatic void 5990298Sdesusage(void) 601929Swollman{ 61287424Saraujo fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n"); 621929Swollman exit(1); 631929Swollman} 641929Swollman 65287424Saraujostatic int 66287424Saraujoget_remote_info(char *indomain, char *inmap, char *server, int *outorder, 67287424Saraujo char **outname) 68287424Saraujo{ 69287424Saraujo struct ypresp_order ypro; 70287424Saraujo struct ypresp_master yprm; 71287424Saraujo struct ypreq_nokey yprnk; 72287424Saraujo struct timeval tv; 73287424Saraujo struct sockaddr_in rsrv_sin; 74287424Saraujo int rsrv_sock; 75287424Saraujo CLIENT *client; 76287424Saraujo struct hostent *h; 77287424Saraujo int r; 78287424Saraujo 79287424Saraujo bzero((char *)&rsrv_sin, sizeof rsrv_sin); 80287424Saraujo rsrv_sin.sin_len = sizeof rsrv_sin; 81287424Saraujo rsrv_sin.sin_family = AF_INET; 82287424Saraujo rsrv_sock = RPC_ANYSOCK; 83287424Saraujo 84287424Saraujo h = gethostbyname(server); 85287424Saraujo if (h == NULL) { 86287424Saraujo if (inet_aton(server, &rsrv_sin.sin_addr) == 0) 87287424Saraujo errx(1, "unknown host %s.", server); 88287424Saraujo } else 89287424Saraujo rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr; 90287424Saraujo 91287424Saraujo tv.tv_sec = 10; 92287424Saraujo tv.tv_usec = 0; 93287424Saraujo 94287424Saraujo client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock); 95287424Saraujo if (client == NULL) 96287424Saraujo errx(1, "clntudp_create: no contact with host %s.", server); 97287424Saraujo 98287424Saraujo yprnk.domain = indomain; 99287424Saraujo yprnk.map = inmap; 100287424Saraujo 101287424Saraujo bzero((char *)(char *)&ypro, sizeof ypro); 102287424Saraujo 103287424Saraujo r = clnt_call(client, YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, &yprnk, 104287424Saraujo (xdrproc_t)xdr_ypresp_order, &ypro, tv); 105287424Saraujo if (r != RPC_SUCCESS) 106287424Saraujo clnt_perror(client, "yp_order: clnt_call"); 107287424Saraujo 108287424Saraujo *outorder = ypro.ordernum; 109287424Saraujo xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro); 110287424Saraujo 111287424Saraujo r = ypprot_err(ypro.status); 112287424Saraujo if (r == RPC_SUCCESS) { 113287424Saraujo bzero((char *)&yprm, sizeof yprm); 114287424Saraujo 115287424Saraujo r = clnt_call(client, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey, 116287424Saraujo &yprnk, (xdrproc_t)xdr_ypresp_master, &yprm, tv); 117287424Saraujo if (r != RPC_SUCCESS) 118287424Saraujo clnt_perror(client, "yp_master: clnt_call"); 119287424Saraujo r = ypprot_err(yprm.status); 120287424Saraujo if (r == 0) 121287424Saraujo *outname = (char *)strdup(yprm.master); 122287424Saraujo xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm); 123287424Saraujo } 124287424Saraujo clnt_destroy(client); 125287424Saraujo return (r); 126287424Saraujo} 127287424Saraujo 1281929Swollmanint 12990298Sdesmain(int argc, char *argv[]) 1301929Swollman{ 131287424Saraujo char *domainname, *hostname = NULL, *inmap, *master; 132287424Saraujo int order, c, r; 133287424Saraujo time_t torder; 1341929Swollman 135287424Saraujo yp_get_default_domain(&domainname); 1361929Swollman 13790297Sdes while ((c = getopt(argc, argv, "h:d:")) != -1) 13890297Sdes switch (c) { 1391929Swollman case 'd': 140287424Saraujo domainname = optarg; 1411929Swollman break; 142287424Saraujo case 'h': 143287424Saraujo hostname = optarg; 144287424Saraujo break; 145287424Saraujo default: 146287424Saraujo usage(); 147287424Saraujo /*NOTREACHED*/ 1481929Swollman } 1491929Swollman 15090297Sdes if (optind + 1 != argc) 1511929Swollman usage(); 1521929Swollman inmap = argv[optind]; 1531929Swollman 154287424Saraujo if (hostname != NULL) { 155287424Saraujo r = get_remote_info(domainname, inmap, hostname, 156287424Saraujo &order, &master); 157287424Saraujo } else { 158287424Saraujo r = yp_order(domainname, inmap, &order); 159287424Saraujo if (r == 0) 160287424Saraujo r = yp_master(domainname, inmap, &master); 161287424Saraujo } 1628857Srgrimes 163287424Saraujo if (r != 0) 164287424Saraujo errx(1, "no such map %s: reason: %s", 165287424Saraujo inmap, yperr_string(r)); 166287424Saraujo 167287424Saraujo torder = order; 168287424Saraujo printf("Map %s has order number %lld. %s", inmap, 169287424Saraujo (long long)order, ctime(&torder)); 170287424Saraujo printf("The master server is %s.\n", master); 171287424Saraujo 172287424Saraujo exit(0); 1731929Swollman} 174