1331722Seadler/*
216132Swpaul * Copyright (c) 1995, 1996
316132Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
416132Swpaul *
516132Swpaul * Redistribution and use in source and binary forms, with or without
616132Swpaul * modification, are permitted provided that the following conditions
716132Swpaul * are met:
816132Swpaul * 1. Redistributions of source code must retain the above copyright
916132Swpaul *    notice, this list of conditions and the following disclaimer.
1016132Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1116132Swpaul *    notice, this list of conditions and the following disclaimer in the
1216132Swpaul *    documentation and/or other materials provided with the distribution.
1316132Swpaul * 3. All advertising materials mentioning features or use of this software
1416132Swpaul *    must display the following acknowledgement:
1516132Swpaul *	This product includes software developed by Bill Paul.
1616132Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1716132Swpaul *    may be used to endorse or promote products derived from this software
1816132Swpaul *    without specific prior written permission.
1916132Swpaul *
2016132Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2116132Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2216132Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2316132Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2416132Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2516132Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2616132Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2716132Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2816132Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2916132Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3016132Swpaul * SUCH DAMAGE.
3116132Swpaul */
3216132Swpaul
33114626Sobrien#include <sys/cdefs.h>
34114626Sobrien__FBSDID("$FreeBSD$");
3531626Scharnier
3631626Scharnier#include <errno.h>
3719181Swpaul#include <stdlib.h>
3819181Swpaul#include <string.h>
3916132Swpaul#include <time.h>
4031626Scharnier#include <unistd.h>
4116132Swpaul#include <rpcsvc/ypxfrd.h>
4216132Swpaul#include <rpcsvc/yp.h>
4316132Swpaul#include <rpc/rpc.h>
4416132Swpaul#include <sys/uio.h>
4516132Swpaul#include <sys/fcntl.h>
4616132Swpaul#include <sys/stat.h>
4731626Scharnier#include <sys/types.h>
4816132Swpaul#include "ypxfr_extern.h"
4916132Swpaul
50285926Saraujostatic int fp = 0;
5116132Swpaul
5290298Sdesstatic bool_t
5390298Sdesxdr_my_xfr(register XDR *xdrs, xfr *objp)
5416132Swpaul{
5590297Sdes	while (1) {
5616132Swpaul		if (!xdr_xfr(xdrs, objp))
5716132Swpaul			return(FALSE);
5816132Swpaul		if (objp->ok == TRUE) {
5916132Swpaul			if (write(fp, objp->xfr_u.xfrblock_buf.xfrblock_buf_val,
6016132Swpaul			    objp->xfr_u.xfrblock_buf.xfrblock_buf_len) == -1) {
6116132Swpaul				yp_error("write failed: %s", strerror(errno));
6216132Swpaul				return(FALSE);
6316132Swpaul			}
6416132Swpaul		}
65121538Speter		xdr_free((xdrproc_t)xdr_xfr, (char *)objp);
6616132Swpaul		if (objp->ok == FALSE) {
6790297Sdes			switch (objp->xfr_u.xfrstat) {
6816132Swpaul			case(XFR_DONE):
6916132Swpaul				return(TRUE);
7016132Swpaul				break;
7116132Swpaul			case(XFR_READ_ERR):
7216132Swpaul				yp_error("got read error from rpc.ypxfrd");
7316132Swpaul				return(FALSE);
7416132Swpaul				break;
7516132Swpaul			case(XFR_ACCESS):
7616132Swpaul				yp_error("rpc.ypxfrd couldn't access the map");
7716132Swpaul				return(FALSE);
7816132Swpaul				break;
7916132Swpaul			case(XFR_DENIED):
8016132Swpaul				yp_error("access to map denied by rpc.ypxfrd");
8116132Swpaul				return(FALSE);
8216132Swpaul				break;
8316958Swpaul			case(XFR_DB_TYPE_MISMATCH):
8416958Swpaul				yp_error("client/server DB type mismatch");
8516958Swpaul				return(FALSE);
8616958Swpaul				break;
8716958Swpaul			case(XFR_DB_ENDIAN_MISMATCH):
8816958Swpaul				yp_error("client/server byte order mismatch");
8916958Swpaul				return(FALSE);
9016958Swpaul				break;
9116132Swpaul			default:
9216132Swpaul				yp_error("got unknown status from rpc.ypxfrd");
9316132Swpaul				return(FALSE);
9416132Swpaul				break;
9516132Swpaul			}
9616132Swpaul		}
9716132Swpaul	}
9816132Swpaul}
9916132Swpaul
10016132Swpaul#define PERM_SECURE (S_IRUSR|S_IWUSR)
10116132Swpaul
10290298Sdesint
10390298Sdesypxfrd_get_map(char *host, char *map, char *domain, char *tmpname)
10416132Swpaul{
10516132Swpaul	CLIENT *clnt;
10616132Swpaul	struct ypxfr_mapname req;
10716132Swpaul	struct xfr resp;
10816132Swpaul	struct timeval timeout = { 0, 25 };
10916132Swpaul	int status = 0;
11016132Swpaul
11116132Swpaul	req.xfrmap = map;
11216132Swpaul	req.xfrdomain = domain;
11316958Swpaul	req.xfrmap_filename = "";
11416958Swpaul	req.xfr_db_type = XFR_DB_BSD_HASH;	/*
11516958Swpaul	req.xfr_byte_order = XFR_ENDIAN_ANY;	 * Berkeley DB isn't
11616958Swpaul						 * byte-order sensitive.
11716958Swpaul						 */
11816958Swpaul
11916132Swpaul	bzero((char *)&resp, sizeof(resp));
12016132Swpaul
12116132Swpaul	if ((clnt = clnt_create(host, YPXFRD_FREEBSD_PROG,
12216132Swpaul				YPXFRD_FREEBSD_VERS, "tcp")) == NULL) {
12316132Swpaul		return(1);
12416132Swpaul	}
12516132Swpaul
12616132Swpaul	if ((fp = open(tmpname, O_RDWR|O_CREAT, PERM_SECURE)) == -1) {
12716132Swpaul		clnt_destroy(clnt);
12816132Swpaul		yp_error("couldn't open %s: %s", tmpname, strerror(errno));
12916132Swpaul		return(1);
13016132Swpaul	}
13116132Swpaul
132121538Speter	if (clnt_call(clnt,YPXFRD_GETMAP,
133121538Speter			(xdrproc_t)xdr_ypxfr_mapname, (char *)&req,
134121538Speter			(xdrproc_t)xdr_my_xfr, (char *)&resp,
135121538Speter			timeout) != RPC_SUCCESS) {
13616132Swpaul		yp_error("%s", clnt_sperror(clnt,"call to rpc.ypxfrd failed"));
13716132Swpaul		status++;
13816132Swpaul		unlink(tmpname);
13916132Swpaul	}
14016132Swpaul
14116132Swpaul	clnt_destroy(clnt);
14216132Swpaul	close(fp);
14316132Swpaul	return(status);
14416132Swpaul}
145