ypxfrd_server.c revision 24776
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.4 1997/02/22 16:13:03 peter Exp $
33 */
34
35#include "ypxfrd.h"
36#ifndef lint
37static const char rcsid[] = "$Id: ypxfrd_server.c,v 1.4 1997/02/22 16:13:03 peter 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 <machine/endian.h>
50#include "ypxfrd_extern.h"
51
52int forked = 0;
53int children = 0;
54int fp = 0;
55
56static bool_t
57xdr_my_xfr(register XDR *xdrs, xfr *objp)
58{
59	unsigned char buf[XFRBLOCKSIZE];
60
61	while(1) {
62		if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
63		    read(fp, &buf, XFRBLOCKSIZE)) != -1) {
64			objp->ok = TRUE;
65			objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
66		} else {
67			objp->ok = FALSE;
68			objp->xfr_u.xfrstat = XFR_READ_ERR;
69			yp_error("read error: %s", strerror(errno));
70		}
71
72		/* Serialize */
73		if (!xdr_xfr(xdrs, objp))
74			return(FALSE);
75		if (objp->ok == FALSE)
76			return(TRUE);
77		if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
78			objp->ok = FALSE;
79			objp->xfr_u.xfrstat = XFR_DONE;
80			if (!xdr_xfr(xdrs, objp))
81				return(FALSE);
82			return(TRUE);
83		}
84	}
85}
86
87struct xfr *
88ypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
89{
90	static struct xfr  result;
91	char buf[MAXPATHLEN];
92	struct sockaddr_in *rqhost;
93
94	result.ok = FALSE;
95	result.xfr_u.xfrstat = XFR_DENIED;
96
97	rqhost = svc_getcaller(rqstp->rq_xprt);
98
99	if (ntohs(rqhost->sin_port) >= IPPORT_RESERVED) {
100		yp_error("%s:%d didn't use reserved port -- rejecting",
101			inet_ntoa(rqhost->sin_addr),
102			ntohs(rqhost->sin_port));
103		return(&result);
104	}
105
106	if (yp_validdomain(argp->xfrdomain)) {
107		return(&result);
108	}
109
110	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
111		return(&result);
112	}
113
114	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
115							argp->xfrmap);
116	if (access((char *)&buf, R_OK) == -1) {
117		result.xfr_u.xfrstat = XFR_ACCESS;
118		return(&result);
119	}
120
121	if (argp->xfr_db_type != XFR_DB_BSD_HASH &&
122	    argp->xfr_db_type != XFR_DB_ANY) {
123		result.xfr_u.xfrstat = XFR_DB_TYPE_MISMATCH;
124		return(&result);
125	}
126
127#if BYTE_ORDER == LITTLE_ENDIAN
128	if (argp->xfr_byte_order == XFR_ENDIAN_BIG) {
129#else
130	if (argp->xfr_byte_order == XFR_ENDIAN_LITTLE) {
131#endif
132		result.xfr_u.xfrstat = XFR_DB_ENDIAN_MISMATCH;
133		return(&result);
134	}
135
136#ifndef DEBUG
137	if (children < MAX_CHILDREN && fork()) {
138		children++;
139		forked = 0;
140		return (NULL);
141	} else {
142		forked++;
143	}
144#endif
145	if ((fp = open((char *)&buf, O_RDONLY)) == -1) {
146		result.xfr_u.xfrstat = XFR_READ_ERR;
147		return(&result);
148	}
149
150	/* Start sending the file. */
151
152	svc_sendreply(rqstp->rq_xprt, xdr_my_xfr, (char *)&result);
153
154	close(fp);
155
156	return (NULL);
157}
158