/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 1995, 19964* Bill Paul <[email protected]>. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. All advertising materials mentioning features or use of this software15* must display the following acknowledgement:16* This product includes software developed by Bill Paul.17* 4. Neither the name of the author nor the names of any co-contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/cdefs.h>35#include <errno.h>36#include <stdlib.h>37#include <string.h>38#include <time.h>39#include <unistd.h>40#include <rpcsvc/ypxfrd.h>41#include <rpcsvc/yp.h>42#include <rpc/rpc.h>43#include <sys/uio.h>44#include <sys/fcntl.h>45#include <sys/stat.h>46#include <sys/types.h>47#include "ypxfr_extern.h"4849static int fp = 0;5051static bool_t52xdr_my_xfr(register XDR *xdrs, xfr *objp)53{54while (1) {55if (!xdr_xfr(xdrs, objp))56return(FALSE);57if (objp->ok == TRUE) {58if (write(fp, objp->xfr_u.xfrblock_buf.xfrblock_buf_val,59objp->xfr_u.xfrblock_buf.xfrblock_buf_len) == -1) {60yp_error("write failed: %s", strerror(errno));61return(FALSE);62}63}64xdr_free((xdrproc_t)xdr_xfr, (char *)objp);65if (objp->ok == FALSE) {66switch (objp->xfr_u.xfrstat) {67case(XFR_DONE):68return(TRUE);69break;70case(XFR_READ_ERR):71yp_error("got read error from rpc.ypxfrd");72return(FALSE);73break;74case(XFR_ACCESS):75yp_error("rpc.ypxfrd couldn't access the map");76return(FALSE);77break;78case(XFR_DENIED):79yp_error("access to map denied by rpc.ypxfrd");80return(FALSE);81break;82case(XFR_DB_TYPE_MISMATCH):83yp_error("client/server DB type mismatch");84return(FALSE);85break;86case(XFR_DB_ENDIAN_MISMATCH):87yp_error("client/server byte order mismatch");88return(FALSE);89break;90default:91yp_error("got unknown status from rpc.ypxfrd");92return(FALSE);93break;94}95}96}97}9899#define PERM_SECURE (S_IRUSR|S_IWUSR)100101int102ypxfrd_get_map(char *host, char *map, char *domain, char *tmpname)103{104CLIENT *clnt;105struct ypxfr_mapname req;106struct xfr resp;107struct timeval timeout = { 0, 25 };108int status = 0;109110req.xfrmap = map;111req.xfrdomain = domain;112req.xfrmap_filename = "";113req.xfr_db_type = XFR_DB_BSD_HASH; /*114req.xfr_byte_order = XFR_ENDIAN_ANY; * Berkeley DB isn't115* byte-order sensitive.116*/117118bzero((char *)&resp, sizeof(resp));119120if ((clnt = clnt_create(host, YPXFRD_FREEBSD_PROG,121YPXFRD_FREEBSD_VERS, "tcp")) == NULL) {122return(1);123}124125if ((fp = open(tmpname, O_RDWR|O_CREAT, PERM_SECURE)) == -1) {126clnt_destroy(clnt);127yp_error("couldn't open %s: %s", tmpname, strerror(errno));128return(1);129}130131if (clnt_call(clnt,YPXFRD_GETMAP,132(xdrproc_t)xdr_ypxfr_mapname, (char *)&req,133(xdrproc_t)xdr_my_xfr, (char *)&resp,134timeout) != RPC_SUCCESS) {135yp_error("%s", clnt_sperror(clnt,"call to rpc.ypxfrd failed"));136status++;137unlink(tmpname);138}139140clnt_destroy(clnt);141close(fp);142return(status);143}144145146