xref: /freebsd/usr.sbin/rpc.ypxfrd/ypxfrd_server.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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 
52 int forked = 0;
53 int children = 0;
54 int fp = 0;
55 
56 static bool_t
57 xdr_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 
87 struct xfr *
88 ypxfrd_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(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(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, (xdrproc_t)xdr_my_xfr, &result);
143 
144 	close(fp);
145 
146 	return (NULL);
147 }
148