ypxfrd_server.c revision 30378
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
3316125Swpaul#ifndef lint
3430378Scharnierstatic const char rcsid[] =
3530378Scharnier	"$Id$";
3616125Swpaul#endif /* not lint */
3716125Swpaul
3830378Scharnier#include "ypxfrd.h"
3930378Scharnier
4016125Swpaul#include <stdio.h>
4116125Swpaul#include <stdlib.h>
4216125Swpaul#include <unistd.h>
4316125Swpaul#include <string.h>
4416125Swpaul#include <errno.h>
4516125Swpaul#include <sys/types.h>
4616125Swpaul#include <sys/param.h>
4716125Swpaul#include <sys/uio.h>
4816125Swpaul#include <sys/fcntl.h>
4916959Swpaul#include <machine/endian.h>
5016125Swpaul#include "ypxfrd_extern.h"
5116125Swpaul
5216125Swpaulint forked = 0;
5316125Swpaulint children = 0;
5416125Swpaulint fp = 0;
5516125Swpaul
5616125Swpaulstatic bool_t
5716125Swpaulxdr_my_xfr(register XDR *xdrs, xfr *objp)
5816125Swpaul{
5916125Swpaul	unsigned char buf[XFRBLOCKSIZE];
6016125Swpaul
6116125Swpaul	while(1) {
6216125Swpaul		if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
6316125Swpaul		    read(fp, &buf, XFRBLOCKSIZE)) != -1) {
6416125Swpaul			objp->ok = TRUE;
6516125Swpaul			objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
6616125Swpaul		} else {
6716125Swpaul			objp->ok = FALSE;
6816125Swpaul			objp->xfr_u.xfrstat = XFR_READ_ERR;
6916125Swpaul			yp_error("read error: %s", strerror(errno));
7016125Swpaul		}
7116125Swpaul
7216125Swpaul		/* Serialize */
7316125Swpaul		if (!xdr_xfr(xdrs, objp))
7416125Swpaul			return(FALSE);
7516125Swpaul		if (objp->ok == FALSE)
7616125Swpaul			return(TRUE);
7716125Swpaul		if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
7816125Swpaul			objp->ok = FALSE;
7916125Swpaul			objp->xfr_u.xfrstat = XFR_DONE;
8016125Swpaul			if (!xdr_xfr(xdrs, objp))
8116125Swpaul				return(FALSE);
8216125Swpaul			return(TRUE);
8316125Swpaul		}
8416125Swpaul	}
8516125Swpaul}
8616125Swpaul
8716125Swpaulstruct xfr *
8816125Swpaulypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
8916125Swpaul{
9016125Swpaul	static struct xfr  result;
9116125Swpaul	char buf[MAXPATHLEN];
9216125Swpaul
9316125Swpaul	result.ok = FALSE;
9416125Swpaul	result.xfr_u.xfrstat = XFR_DENIED;
9516125Swpaul
9616125Swpaul	if (yp_validdomain(argp->xfrdomain)) {
9716125Swpaul		return(&result);
9816125Swpaul	}
9916125Swpaul
10016125Swpaul	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
10116125Swpaul		return(&result);
10216125Swpaul	}
10316125Swpaul
10416125Swpaul	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
10516125Swpaul							argp->xfrmap);
10616125Swpaul	if (access((char *)&buf, R_OK) == -1) {
10716125Swpaul		result.xfr_u.xfrstat = XFR_ACCESS;
10816125Swpaul		return(&result);
10916125Swpaul	}
11016959Swpaul
11116959Swpaul	if (argp->xfr_db_type != XFR_DB_BSD_HASH &&
11216959Swpaul	    argp->xfr_db_type != XFR_DB_ANY) {
11316959Swpaul		result.xfr_u.xfrstat = XFR_DB_TYPE_MISMATCH;
11416959Swpaul		return(&result);
11516959Swpaul	}
11616959Swpaul
11716959Swpaul#if BYTE_ORDER == LITTLE_ENDIAN
11816959Swpaul	if (argp->xfr_byte_order == XFR_ENDIAN_BIG) {
11916959Swpaul#else
12016959Swpaul	if (argp->xfr_byte_order == XFR_ENDIAN_LITTLE) {
12116959Swpaul#endif
12216959Swpaul		result.xfr_u.xfrstat = XFR_DB_ENDIAN_MISMATCH;
12316959Swpaul		return(&result);
12416959Swpaul	}
12516959Swpaul
12616125Swpaul#ifndef DEBUG
12716125Swpaul	if (children < MAX_CHILDREN && fork()) {
12816125Swpaul		children++;
12916125Swpaul		forked = 0;
13016125Swpaul		return (NULL);
13116125Swpaul	} else {
13216125Swpaul		forked++;
13316125Swpaul	}
13416125Swpaul#endif
13516125Swpaul	if ((fp = open((char *)&buf, O_RDONLY)) == -1) {
13616125Swpaul		result.xfr_u.xfrstat = XFR_READ_ERR;
13716125Swpaul		return(&result);
13816125Swpaul	}
13916125Swpaul
14016125Swpaul	/* Start sending the file. */
14116125Swpaul
14216125Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_xfr, (char *)&result);
14316125Swpaul
14416125Swpaul	close(fp);
14516125Swpaul
14616125Swpaul	return (NULL);
14716125Swpaul}
148