ypxfrd_server.c revision 90297
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
33#ifndef lint
34static const char rcsid[] =
35  "$FreeBSD: head/usr.sbin/rpc.ypxfrd/ypxfrd_server.c 90297 2002-02-06 13:30:31Z des $";
36#endif /* not lint */
37
38#include "ypxfrd.h"
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
93	result.ok = FALSE;
94	result.xfr_u.xfrstat = XFR_DENIED;
95
96	if (yp_validdomain(argp->xfrdomain)) {
97		return(&result);
98	}
99
100	if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
101		return(&result);
102	}
103
104	snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
105							argp->xfrmap);
106	if (access((char *)&buf, R_OK) == -1) {
107		result.xfr_u.xfrstat = XFR_ACCESS;
108		return(&result);
109	}
110
111	if (argp->xfr_db_type != XFR_DB_BSD_HASH &&
112	    argp->xfr_db_type != XFR_DB_ANY) {
113		result.xfr_u.xfrstat = XFR_DB_TYPE_MISMATCH;
114		return(&result);
115	}
116
117#if BYTE_ORDER == LITTLE_ENDIAN
118	if (argp->xfr_byte_order == XFR_ENDIAN_BIG) {
119#else
120	if (argp->xfr_byte_order == XFR_ENDIAN_LITTLE) {
121#endif
122		result.xfr_u.xfrstat = XFR_DB_ENDIAN_MISMATCH;
123		return(&result);
124	}
125
126#ifndef DEBUG
127	if (children < MAX_CHILDREN && fork()) {
128		children++;
129		forked = 0;
130		return (NULL);
131	} else {
132		forked++;
133	}
134#endif
135	if ((fp = open((char *)&buf, O_RDONLY)) == -1) {
136		result.xfr_u.xfrstat = XFR_READ_ERR;
137		return(&result);
138	}
139
140	/* Start sending the file. */
141
142	svc_sendreply(rqstp->rq_xprt, xdr_my_xfr, (char *)&result);
143
144	close(fp);
145
146	return (NULL);
147}
148