ypxfrd_server.c revision 16126
1/*
2 * Copyright (c) 1995, 1996
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	$Id: ypxfrd_server.c,v 1.5 1996/06/02 17:34:49 wpaul Exp $
33 */
34
35#include "ypxfrd.h"
36#ifndef lint
37static const char rcsid[] = "$Id: ypxfrd_server.c,v 1.5 1996/06/02 17:34:49 wpaul Exp $";
38#endif /* not lint */
39
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <string.h>
44#include <errno.h>
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/uio.h>
48#include <sys/fcntl.h>
49#include "ypxfrd_extern.h"
50
51int forked = 0;
52int children = 0;
53int fp = 0;
54
55static bool_t
56xdr_my_xfr(register XDR *xdrs, xfr *objp)
57{
58	unsigned char buf[XFRBLOCKSIZE];
59
60	while(1) {
61		if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
62		    read(fp, &buf, XFRBLOCKSIZE)) != -1) {
63			objp->ok = TRUE;
64			objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
65		} else {
66			objp->ok = FALSE;
67			objp->xfr_u.xfrstat = XFR_READ_ERR;
68			yp_error("read error: %s", strerror(errno));
69		}
70
71		/* Serialize */
72		if (!xdr_xfr(xdrs, objp))
73			return(FALSE);
74		if (objp->ok == FALSE)
75			return(TRUE);
76		if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
77			objp->ok = FALSE;
78			objp->xfr_u.xfrstat = XFR_DONE;
79			if (!xdr_xfr(xdrs, objp))
80				return(FALSE);
81			return(TRUE);
82		}
83	}
84}
85
86struct xfr *
87ypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
88{
89	static struct xfr  result;
90	char buf[MAXPATHLEN];
91
92	result.ok = FALSE;
93	result.xfr_u.xfrstat = XFR_DENIED;
94
95	if (yp_validdomain(argp->xfrdomain)) {
96		return(&result);
97	}
98
99	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
100		return(&result);
101	}
102
103	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
104							argp->xfrmap);
105	if (access((char *)&buf, R_OK) == -1) {
106		result.xfr_u.xfrstat = XFR_ACCESS;
107		return(&result);
108	}
109#ifndef DEBUG
110	if (children < MAX_CHILDREN && fork()) {
111		children++;
112		forked = 0;
113		return (NULL);
114	} else {
115		forked++;
116	}
117#endif
118	if ((fp = open((char *)&buf, O_RDONLY)) == -1) {
119		result.xfr_u.xfrstat = XFR_READ_ERR;
120		return(&result);
121	}
122
123	/* Start sending the file. */
124
125	svc_sendreply(rqstp->rq_xprt, xdr_my_xfr, (char *)&result);
126
127	close(fp);
128
129	return (NULL);
130}
131