116125Swpaul/*
216125Swpaul * Copyright (c) 1995, 1996
316125Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
416125Swpaul *
516125Swpaul * Redistribution and use in source and binary forms, with or without
616125Swpaul * modification, are permitted provided that the following conditions
716125Swpaul * are met:
816125Swpaul * 1. Redistributions of source code must retain the above copyright
916125Swpaul *    notice, this list of conditions and the following disclaimer.
1016125Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1116125Swpaul *    notice, this list of conditions and the following disclaimer in the
1216125Swpaul *    documentation and/or other materials provided with the distribution.
1316125Swpaul * 3. All advertising materials mentioning features or use of this software
1416125Swpaul *    must display the following acknowledgement:
1516125Swpaul *	This product includes software developed by Bill Paul.
1616125Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1716125Swpaul *    may be used to endorse or promote products derived from this software
1816125Swpaul *    without specific prior written permission.
1916125Swpaul *
2016125Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2116125Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2216125Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2316125Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2416125Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2516125Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2616125Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2716125Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2816125Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2916125Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3016125Swpaul * SUCH DAMAGE.
3116125Swpaul */
3216125Swpaul
33114601Sobrien#include <sys/cdefs.h>
34114601Sobrien__FBSDID("$FreeBSD$");
3516125Swpaul
3630378Scharnier#include "ypxfrd.h"
3730378Scharnier
3816125Swpaul#include <stdio.h>
3916125Swpaul#include <stdlib.h>
4016125Swpaul#include <unistd.h>
4116125Swpaul#include <string.h>
4216125Swpaul#include <errno.h>
4316125Swpaul#include <sys/types.h>
4416125Swpaul#include <sys/param.h>
4516125Swpaul#include <sys/uio.h>
4616125Swpaul#include <sys/fcntl.h>
4716959Swpaul#include <machine/endian.h>
4816125Swpaul#include "ypxfrd_extern.h"
4916125Swpaul
5016125Swpaulint forked = 0;
5116125Swpaulint children = 0;
5216125Swpaulint fp = 0;
5316125Swpaul
5416125Swpaulstatic bool_t
5516125Swpaulxdr_my_xfr(register XDR *xdrs, xfr *objp)
5616125Swpaul{
5716125Swpaul	unsigned char buf[XFRBLOCKSIZE];
5816125Swpaul
5990297Sdes	while (1) {
6016125Swpaul		if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
6116125Swpaul		    read(fp, &buf, XFRBLOCKSIZE)) != -1) {
6216125Swpaul			objp->ok = TRUE;
6316125Swpaul			objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
6416125Swpaul		} else {
6516125Swpaul			objp->ok = FALSE;
6616125Swpaul			objp->xfr_u.xfrstat = XFR_READ_ERR;
6716125Swpaul			yp_error("read error: %s", strerror(errno));
6816125Swpaul		}
6916125Swpaul
7016125Swpaul		/* Serialize */
7116125Swpaul		if (!xdr_xfr(xdrs, objp))
7216125Swpaul			return(FALSE);
7316125Swpaul		if (objp->ok == FALSE)
7416125Swpaul			return(TRUE);
7516125Swpaul		if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
7616125Swpaul			objp->ok = FALSE;
7716125Swpaul			objp->xfr_u.xfrstat = XFR_DONE;
7816125Swpaul			if (!xdr_xfr(xdrs, objp))
7916125Swpaul				return(FALSE);
8016125Swpaul			return(TRUE);
8116125Swpaul		}
8216125Swpaul	}
8316125Swpaul}
8416125Swpaul
8516125Swpaulstruct xfr *
8616125Swpaulypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
8716125Swpaul{
8816125Swpaul	static struct xfr  result;
8916125Swpaul	char buf[MAXPATHLEN];
9016125Swpaul
9116125Swpaul	result.ok = FALSE;
9216125Swpaul	result.xfr_u.xfrstat = XFR_DENIED;
9316125Swpaul
9416125Swpaul	if (yp_validdomain(argp->xfrdomain)) {
9516125Swpaul		return(&result);
9616125Swpaul	}
9716125Swpaul
9816125Swpaul	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
9916125Swpaul		return(&result);
10016125Swpaul	}
10116125Swpaul
10216125Swpaul	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
10316125Swpaul							argp->xfrmap);
10495658Sdes	if (access(buf, R_OK) == -1) {
10516125Swpaul		result.xfr_u.xfrstat = XFR_ACCESS;
10616125Swpaul		return(&result);
10716125Swpaul	}
10816959Swpaul
10916959Swpaul	if (argp->xfr_db_type != XFR_DB_BSD_HASH &&
11016959Swpaul	    argp->xfr_db_type != XFR_DB_ANY) {
11116959Swpaul		result.xfr_u.xfrstat = XFR_DB_TYPE_MISMATCH;
11216959Swpaul		return(&result);
11316959Swpaul	}
11416959Swpaul
11516959Swpaul#if BYTE_ORDER == LITTLE_ENDIAN
11616959Swpaul	if (argp->xfr_byte_order == XFR_ENDIAN_BIG) {
11716959Swpaul#else
11816959Swpaul	if (argp->xfr_byte_order == XFR_ENDIAN_LITTLE) {
11916959Swpaul#endif
12016959Swpaul		result.xfr_u.xfrstat = XFR_DB_ENDIAN_MISMATCH;
12116959Swpaul		return(&result);
12216959Swpaul	}
12316959Swpaul
12416125Swpaul#ifndef DEBUG
12516125Swpaul	if (children < MAX_CHILDREN && fork()) {
12616125Swpaul		children++;
12716125Swpaul		forked = 0;
12816125Swpaul		return (NULL);
12916125Swpaul	} else {
13016125Swpaul		forked++;
13116125Swpaul	}
13216125Swpaul#endif
13395658Sdes	if ((fp = open(buf, O_RDONLY)) == -1) {
13416125Swpaul		result.xfr_u.xfrstat = XFR_READ_ERR;
13516125Swpaul		return(&result);
13616125Swpaul	}
13716125Swpaul
13816125Swpaul	/* Start sending the file. */
13916125Swpaul
14095658Sdes	svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_xfr, &result);
14116125Swpaul
14216125Swpaul	close(fp);
14316125Swpaul
14416125Swpaul	return (NULL);
14516125Swpaul}
146