113007Swpaul/*
213007Swpaul * Copyright (c) 1995
313007Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
413007Swpaul *
513007Swpaul * Redistribution and use in source and binary forms, with or without
613007Swpaul * modification, are permitted provided that the following conditions
713007Swpaul * are met:
813007Swpaul * 1. Redistributions of source code must retain the above copyright
913007Swpaul *    notice, this list of conditions and the following disclaimer.
1013007Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1113007Swpaul *    notice, this list of conditions and the following disclaimer in the
1213007Swpaul *    documentation and/or other materials provided with the distribution.
1313007Swpaul * 3. All advertising materials mentioning features or use of this software
1413007Swpaul *    must display the following acknowledgement:
1513007Swpaul *	This product includes software developed by Bill Paul.
1613007Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1713007Swpaul *    may be used to endorse or promote products derived from this software
1813007Swpaul *    without specific prior written permission.
1913007Swpaul *
2013007Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2113007Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2213007Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2313007Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2413007Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2513007Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2613007Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2713007Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2813007Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2913007Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3013007Swpaul * SUCH DAMAGE.
3113007Swpaul */
3231626Scharnier
33114626Sobrien#include <sys/cdefs.h>
34114626Sobrien__FBSDID("$FreeBSD$");
3531626Scharnier
3613007Swpaul#include <stdio.h>
3731626Scharnier#include <time.h>
3813007Swpaul#include <sys/types.h>
3913007Swpaul#include <rpc/rpc.h>
4013007Swpaul#include <rpc/xdr.h>
4113007Swpaul#include <rpcsvc/yp.h>
4213007Swpaul#include "ypxfr_extern.h"
4313007Swpaul
4490297Sdesextern bool_t xdr_ypresp_all_seq(XDR *, unsigned long *);
4513007Swpaul
4613007Swpaulint (*ypresp_allfn)();
4713007Swpaulvoid *ypresp_data;
4813007Swpaulextern DB *specdbp;
4980281Sddextern enum ypstat yp_errno;
5013007Swpaul
5113007Swpaul/*
5213007Swpaul * This is largely the same as yp_all() except we do the transfer
5313007Swpaul * from a specific server without the aid of ypbind(8). We need to
5413007Swpaul * be able to specify the source host explicitly since ypxfr may
5513007Swpaul * only transfer maps from the NIS master server for any given domain.
5613007Swpaul * However, if we use the libc version of yp_all(), we could end up
5713007Swpaul * talking to one of the slaves instead. We do need to dig into libc
5813007Swpaul * a little though, since it contains the magic XDR function we need.
5913007Swpaul */
6090298Sdesint
6190298Sdesypxfr_get_map(char *map, char *domain, char *host,
6290298Sdes    int (*callback)(int, char *, int, char *, int, char*))
6313007Swpaul{
6413007Swpaul	CLIENT *clnt;
6513007Swpaul	ypreq_nokey req;
6613007Swpaul	unsigned long status;
6713007Swpaul	struct timeval timeout;
6813007Swpaul
6913007Swpaul	timeout.tv_usec = 0;
7013007Swpaul	timeout.tv_sec = 10;
7113007Swpaul
7213007Swpaul	/* YPPROC_ALL is a TCP service */
7313007Swpaul	if ((clnt = clnt_create(host, YPPROG, YPVERS, "tcp")) == NULL) {
7416132Swpaul		yp_error("%s", clnt_spcreateerror("failed to \
7516132Swpaulcreate tcp handle"));
76229142Sdim		yp_errno = (enum ypstat)YPXFR_YPERR;
7713007Swpaul		return(1);
7813007Swpaul	}
7913007Swpaul
8013007Swpaul	req.domain = domain;
8113007Swpaul	req.map = map;
8213007Swpaul	ypresp_allfn = callback;
8313007Swpaul	ypresp_data = NULL;
8413007Swpaul
8595658Sdes	(void)clnt_call(clnt, YPPROC_ALL, (xdrproc_t)xdr_ypreq_nokey, &req,
8695658Sdes	    (xdrproc_t)xdr_ypresp_all_seq, &status, timeout);
8713007Swpaul
8813007Swpaul	clnt_destroy(clnt);
8913007Swpaul
9013895Swpaul	if (status == YP_NOMORE)
9113895Swpaul		return(0);
9213895Swpaul
9313007Swpaul	if (status != YP_TRUE) {
94229142Sdim		yp_errno = (enum ypstat)YPXFR_YPERR;
9513007Swpaul		return(1);
9613007Swpaul	}
9713007Swpaul
9813007Swpaul	return(0);
9913007Swpaul}
100