11929Swollman/*
21929Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
31929Swollman * Copyright (c) 1992/3 John Brezak
41929Swollman * All rights reserved.
51929Swollman *
61929Swollman * Redistribution and use in source and binary forms, with or without
71929Swollman * modification, are permitted provided that the following conditions
81929Swollman * are met:
91929Swollman * 1. Redistributions of source code must retain the above copyright
101929Swollman *    notice, this list of conditions and the following disclaimer.
111929Swollman * 2. Redistributions in binary form must reproduce the above copyright
121929Swollman *    notice, this list of conditions and the following disclaimer in the
131929Swollman *    documentation and/or other materials provided with the distribution.
141929Swollman * 3. The name of the author may not be used to endorse or promote
151929Swollman *    products derived from this software without specific prior written
161929Swollman *    permission.
171929Swollman *
181929Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
191929Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
201929Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211929Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
221929Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231929Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241929Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251929Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261929Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271929Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281929Swollman * SUCH DAMAGE.
291929Swollman */
301929Swollman
31114601Sobrien#include <sys/cdefs.h>
32114601Sobrien__FBSDID("$FreeBSD$");
331929Swollman
341929Swollman#include <sys/param.h>
351929Swollman#include <sys/types.h>
361929Swollman#include <sys/socket.h>
3730778Scharnier#include <err.h>
381929Swollman#include <stdio.h>
3978720Sdd#include <stdlib.h>
401929Swollman#include <time.h>
41129665Sstefanf#include <timeconv.h>
4230778Scharnier#include <unistd.h>
431929Swollman
441929Swollman#include <rpc/rpc.h>
451929Swollman#include <rpc/xdr.h>
461929Swollman#include <rpcsvc/yp_prot.h>
471929Swollman#include <rpcsvc/ypclnt.h>
481929Swollman
4930778Scharnierstatic void
5090298Sdesusage(void)
511929Swollman{
5230778Scharnier	fprintf(stderr, "usage: yppoll [-h host] [-d domainname] mapname\n");
531929Swollman	exit(1);
541929Swollman}
551929Swollman
561929Swollmanint
5790298Sdesmain(int argc, char *argv[])
581929Swollman{
591929Swollman	char *domainname;
601929Swollman        char *hostname = "localhost";
611929Swollman        char *inmap, *master;
621929Swollman        int order;
631929Swollman	int c, r;
6485641Sdillon	time_t t;
651929Swollman
661929Swollman        yp_get_default_domain(&domainname);
671929Swollman
6890297Sdes	while ((c = getopt(argc, argv, "h:d:")) != -1)
6990297Sdes		switch (c) {
701929Swollman		case 'd':
711929Swollman                        domainname = optarg;
721929Swollman			break;
731929Swollman                case 'h':
741929Swollman                        hostname = optarg;
751929Swollman                        break;
761929Swollman                case '?':
771929Swollman                        usage();
781929Swollman                        /*NOTREACHED*/
791929Swollman		}
801929Swollman
8190297Sdes	if (optind + 1 != argc)
821929Swollman		usage();
831929Swollman
841929Swollman	inmap = argv[optind];
851929Swollman
861929Swollman	r = yp_order(domainname, inmap, &order);
8730778Scharnier        if (r != 0)
8830778Scharnier		errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r));
8989572Sdillon	t = _int_to_time(order);
9085641Sdillon        printf("Map %s has order number %d. %s", inmap, order, ctime(&t));
911929Swollman	r = yp_master(domainname, inmap, &master);
9230778Scharnier        if (r != 0)
9330778Scharnier		errx(1, "no such map %s. Reason: %s", inmap, yperr_string(r));
941929Swollman        printf("The master server is %s.\n", master);
958857Srgrimes
961929Swollman        exit(0);
971929Swollman}
98