1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
416118c2bSBill Paul * Copyright (c) 1995, 1996
516118c2bSBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
616118c2bSBill Paul *
716118c2bSBill Paul * Redistribution and use in source and binary forms, with or without
816118c2bSBill Paul * modification, are permitted provided that the following conditions
916118c2bSBill Paul * are met:
1016118c2bSBill Paul * 1. Redistributions of source code must retain the above copyright
1116118c2bSBill Paul * notice, this list of conditions and the following disclaimer.
1216118c2bSBill Paul * 2. Redistributions in binary form must reproduce the above copyright
1316118c2bSBill Paul * notice, this list of conditions and the following disclaimer in the
1416118c2bSBill Paul * documentation and/or other materials provided with the distribution.
1516118c2bSBill Paul * 3. All advertising materials mentioning features or use of this software
1616118c2bSBill Paul * must display the following acknowledgement:
1716118c2bSBill Paul * This product includes software developed by Bill Paul.
1816118c2bSBill Paul * 4. Neither the name of the author nor the names of any co-contributors
1916118c2bSBill Paul * may be used to endorse or promote products derived from this software
2016118c2bSBill Paul * without specific prior written permission.
2116118c2bSBill Paul *
2216118c2bSBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2316118c2bSBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2416118c2bSBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2516118c2bSBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2616118c2bSBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2716118c2bSBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2816118c2bSBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2916118c2bSBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3016118c2bSBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3116118c2bSBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3216118c2bSBill Paul * SUCH DAMAGE.
3316118c2bSBill Paul */
3416118c2bSBill Paul
35b728350eSDavid E. O'Brien #include <sys/cdefs.h>
361e96bb57SPhilippe Charnier #include "ypxfrd.h"
371e96bb57SPhilippe Charnier
3816118c2bSBill Paul #include <stdio.h>
3916118c2bSBill Paul #include <stdlib.h>
4016118c2bSBill Paul #include <unistd.h>
4116118c2bSBill Paul #include <string.h>
4216118c2bSBill Paul #include <errno.h>
4316118c2bSBill Paul #include <sys/types.h>
4416118c2bSBill Paul #include <sys/param.h>
4516118c2bSBill Paul #include <sys/uio.h>
4616118c2bSBill Paul #include <sys/fcntl.h>
4793b1ab3dSBill Paul #include <machine/endian.h>
4816118c2bSBill Paul #include "ypxfrd_extern.h"
4916118c2bSBill Paul
5016118c2bSBill Paul int forked = 0;
5116118c2bSBill Paul int children = 0;
5216118c2bSBill Paul int fp = 0;
5316118c2bSBill Paul
5416118c2bSBill Paul static bool_t
xdr_my_xfr(register XDR * xdrs,xfr * objp)5516118c2bSBill Paul xdr_my_xfr(register XDR *xdrs, xfr *objp)
5616118c2bSBill Paul {
5716118c2bSBill Paul unsigned char buf[XFRBLOCKSIZE];
5816118c2bSBill Paul
5916118c2bSBill Paul while (1) {
6016118c2bSBill Paul if ((objp->xfr_u.xfrblock_buf.xfrblock_buf_len =
6116118c2bSBill Paul read(fp, &buf, XFRBLOCKSIZE)) != -1) {
6216118c2bSBill Paul objp->ok = TRUE;
6316118c2bSBill Paul objp->xfr_u.xfrblock_buf.xfrblock_buf_val = (char *)&buf;
6416118c2bSBill Paul } else {
6516118c2bSBill Paul objp->ok = FALSE;
6616118c2bSBill Paul objp->xfr_u.xfrstat = XFR_READ_ERR;
6716118c2bSBill Paul yp_error("read error: %s", strerror(errno));
6816118c2bSBill Paul }
6916118c2bSBill Paul
7016118c2bSBill Paul /* Serialize */
7116118c2bSBill Paul if (!xdr_xfr(xdrs, objp))
7216118c2bSBill Paul return(FALSE);
7316118c2bSBill Paul if (objp->ok == FALSE)
7416118c2bSBill Paul return(TRUE);
7516118c2bSBill Paul if (objp->xfr_u.xfrblock_buf.xfrblock_buf_len < XFRBLOCKSIZE) {
7616118c2bSBill Paul objp->ok = FALSE;
7716118c2bSBill Paul objp->xfr_u.xfrstat = XFR_DONE;
7816118c2bSBill Paul if (!xdr_xfr(xdrs, objp))
7916118c2bSBill Paul return(FALSE);
8016118c2bSBill Paul return(TRUE);
8116118c2bSBill Paul }
8216118c2bSBill Paul }
8316118c2bSBill Paul }
8416118c2bSBill Paul
8516118c2bSBill Paul struct xfr *
ypxfrd_getmap_1_svc(ypxfr_mapname * argp,struct svc_req * rqstp)8616118c2bSBill Paul ypxfrd_getmap_1_svc(ypxfr_mapname *argp, struct svc_req *rqstp)
8716118c2bSBill Paul {
8816118c2bSBill Paul static struct xfr result;
8916118c2bSBill Paul char buf[MAXPATHLEN];
9016118c2bSBill Paul
9116118c2bSBill Paul result.ok = FALSE;
9216118c2bSBill Paul result.xfr_u.xfrstat = XFR_DENIED;
9316118c2bSBill Paul
9416118c2bSBill Paul if (yp_validdomain(argp->xfrdomain)) {
9516118c2bSBill Paul return(&result);
9616118c2bSBill Paul }
9716118c2bSBill Paul
9816118c2bSBill Paul if (yp_access(argp->xfrmap, (struct svc_req *)rqstp)) {
9916118c2bSBill Paul return(&result);
10016118c2bSBill Paul }
10116118c2bSBill Paul
10216118c2bSBill Paul snprintf (buf, sizeof(buf), "%s/%s/%s", yp_dir, argp->xfrdomain,
10316118c2bSBill Paul argp->xfrmap);
104f249dbccSDag-Erling Smørgrav if (access(buf, R_OK) == -1) {
10516118c2bSBill Paul result.xfr_u.xfrstat = XFR_ACCESS;
10616118c2bSBill Paul return(&result);
10716118c2bSBill Paul }
10893b1ab3dSBill Paul
10993b1ab3dSBill Paul if (argp->xfr_db_type != XFR_DB_BSD_HASH &&
11093b1ab3dSBill Paul argp->xfr_db_type != XFR_DB_ANY) {
11193b1ab3dSBill Paul result.xfr_u.xfrstat = XFR_DB_TYPE_MISMATCH;
11293b1ab3dSBill Paul return(&result);
11393b1ab3dSBill Paul }
11493b1ab3dSBill Paul
11593b1ab3dSBill Paul #if BYTE_ORDER == LITTLE_ENDIAN
11693b1ab3dSBill Paul if (argp->xfr_byte_order == XFR_ENDIAN_BIG) {
11793b1ab3dSBill Paul #else
11893b1ab3dSBill Paul if (argp->xfr_byte_order == XFR_ENDIAN_LITTLE) {
11993b1ab3dSBill Paul #endif
12093b1ab3dSBill Paul result.xfr_u.xfrstat = XFR_DB_ENDIAN_MISMATCH;
12193b1ab3dSBill Paul return(&result);
12293b1ab3dSBill Paul }
12393b1ab3dSBill Paul
12416118c2bSBill Paul #ifndef DEBUG
12516118c2bSBill Paul if (children < MAX_CHILDREN && fork()) {
12616118c2bSBill Paul children++;
12716118c2bSBill Paul forked = 0;
12816118c2bSBill Paul return (NULL);
12916118c2bSBill Paul } else {
13016118c2bSBill Paul forked++;
13116118c2bSBill Paul }
13216118c2bSBill Paul #endif
133f249dbccSDag-Erling Smørgrav if ((fp = open(buf, O_RDONLY)) == -1) {
13416118c2bSBill Paul result.xfr_u.xfrstat = XFR_READ_ERR;
13516118c2bSBill Paul return(&result);
13616118c2bSBill Paul }
13716118c2bSBill Paul
13816118c2bSBill Paul /* Start sending the file. */
13916118c2bSBill Paul
140f249dbccSDag-Erling Smørgrav svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_xfr, &result);
14116118c2bSBill Paul
14216118c2bSBill Paul close(fp);
14316118c2bSBill Paul
14416118c2bSBill Paul return (NULL);
14516118c2bSBill Paul }
146