ypxfrd_server.c revision 16125
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 *	$Id: ypxfrd_server.c,v 1.5 1996/06/02 17:34:49 wpaul Exp $
3316125Swpaul */
3416125Swpaul
3516125Swpaul#include "ypxfrd.h"
3616125Swpaul#ifndef lint
3716125Swpaulstatic const char rcsid[] = "$Id: ypxfrd_server.c,v 1.5 1996/06/02 17:34:49 wpaul Exp $";
3816125Swpaul#endif /* not lint */
3916125Swpaul
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>
4916125Swpaul#include "ypxfrd_extern.h"
5016125Swpaul
5116125Swpaulint forked = 0;
5216125Swpaulint children = 0;
5316125Swpaulint fp = 0;
5416125Swpaul
5516125Swpaulstatic bool_t
5616125Swpaulxdr_my_xfr(register XDR *xdrs, xfr *objp)
5716125Swpaul{
5816125Swpaul	unsigned char buf[XFRBLOCKSIZE];
5916125Swpaul
6016125Swpaul	while(1) {
6116125Swpaul		if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
6216125Swpaul		    read(fp, &buf, XFRBLOCKSIZE)) != -1) {
6316125Swpaul			objp->ok = TRUE;
6416125Swpaul			objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
6516125Swpaul		} else {
6616125Swpaul			objp->ok = FALSE;
6716125Swpaul			objp->xfr_u.xfrstat = XFR_READ_ERR;
6816125Swpaul			yp_error("read error: %s", strerror(errno));
6916125Swpaul		}
7016125Swpaul
7116125Swpaul		/* Serialize */
7216125Swpaul		if (!xdr_xfr(xdrs, objp))
7316125Swpaul			return(FALSE);
7416125Swpaul		if (objp->ok == FALSE)
7516125Swpaul			return(TRUE);
7616125Swpaul		if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
7716125Swpaul			objp->ok = FALSE;
7816125Swpaul			objp->xfr_u.xfrstat = XFR_DONE;
7916125Swpaul			if (!xdr_xfr(xdrs, objp))
8016125Swpaul				return(FALSE);
8116125Swpaul			return(TRUE);
8216125Swpaul		}
8316125Swpaul	}
8416125Swpaul}
8516125Swpaul
8616125Swpaulstruct xfr *
8716125Swpaulypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
8816125Swpaul{
8916125Swpaul	static struct xfr  result;
9016125Swpaul	char buf[MAXPATHLEN];
9116125Swpaul
9216125Swpaul	result.ok = FALSE;
9316125Swpaul	result.xfr_u.xfrstat = XFR_DENIED;
9416125Swpaul
9516125Swpaul	if (yp_validdomain(argp->xfrdomain)) {
9616125Swpaul		return(&result);
9716125Swpaul	}
9816125Swpaul
9916125Swpaul	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
10016125Swpaul		return(&result);
10116125Swpaul	}
10216125Swpaul
10316125Swpaul	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
10416125Swpaul							argp->xfrmap);
10516125Swpaul	if (access((char *)&buf, R_OK) == -1) {
10616125Swpaul		result.xfr_u.xfrstat = XFR_ACCESS;
10716125Swpaul		return(&result);
10816125Swpaul	}
10916125Swpaul#ifndef DEBUG
11016125Swpaul	if (children < MAX_CHILDREN && fork()) {
11116125Swpaul		children++;
11216125Swpaul		forked = 0;
11316125Swpaul		return (NULL);
11416125Swpaul	} else {
11516125Swpaul		forked++;
11616125Swpaul	}
11716125Swpaul#endif
11816125Swpaul	if ((fp = open((char *)&buf, O_RDONLY)) == -1) {
11916125Swpaul		result.xfr_u.xfrstat = XFR_READ_ERR;
12016125Swpaul		return(&result);
12116125Swpaul	}
12216125Swpaul
12316125Swpaul	/* Start sending the file. */
12416125Swpaul
12516125Swpaul	svc_sendreply(rqstp->rq_xprt, xdr_my_xfr, (char *)&result);
12616125Swpaul
12716125Swpaul	close(fp);
12816125Swpaul
12916125Swpaul	return (NULL);
13016125Swpaul}
131