112891Swpaul/* 212891Swpaul * Copyright (c) 1995 312891Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 412891Swpaul * 512891Swpaul * Redistribution and use in source and binary forms, with or without 612891Swpaul * modification, are permitted provided that the following conditions 712891Swpaul * are met: 812891Swpaul * 1. Redistributions of source code must retain the above copyright 912891Swpaul * notice, this list of conditions and the following disclaimer. 1012891Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1112891Swpaul * notice, this list of conditions and the following disclaimer in the 1212891Swpaul * documentation and/or other materials provided with the distribution. 1312891Swpaul * 3. All advertising materials mentioning features or use of this software 1412891Swpaul * must display the following acknowledgement: 1512891Swpaul * This product includes software developed by Bill Paul. 1612891Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1712891Swpaul * may be used to endorse or promote products derived from this software 1812891Swpaul * without specific prior written permission. 1912891Swpaul * 2012891Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2112891Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2212891Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2312891Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 2412891Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2512891Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2612891Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2712891Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2812891Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2912891Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3012891Swpaul * SUCH DAMAGE. 3112891Swpaul * 3212891Swpaul */ 3312891Swpaul 34114601Sobrien#include <sys/cdefs.h> 35114601Sobrien__FBSDID("$FreeBSD$"); 3630827Scharnier 3720818Swpaul#include "yp.h" 3812891Swpaul#include "yp_extern.h" 3930827Scharnier#include <dirent.h> 4030827Scharnier#include <errno.h> 4112891Swpaul#include <stdlib.h> 4212891Swpaul#include <sys/stat.h> 4312891Swpaul#include <sys/param.h> 4412891Swpaul#include <sys/types.h> 4512891Swpaul#include <sys/socket.h> 4612891Swpaul#include <netinet/in.h> 4712891Swpaul#include <arpa/inet.h> 4812997Swpaul#include <rpc/rpc.h> 4912891Swpaul 5012891Swpaulint children = 0; 5112891Swpaul 5228042Swpaul#define MASTER_STRING "YP_MASTER_NAME" 5328042Swpaul#define MASTER_SZ sizeof(MASTER_STRING) - 1 5428042Swpaul#define ORDER_STRING "YP_LAST_MODIFIED" 5528042Swpaul#define ORDER_SZ sizeof(ORDER_STRING) - 1 5628042Swpaul 5790298Sdesstatic pid_t 5890298Sdesyp_fork(void) 5946186Swpaul{ 6046186Swpaul if (yp_pid != getpid()) { 6146186Swpaul yp_error("child %d trying to fork!", getpid()); 6246186Swpaul errno = EEXIST; 6346186Swpaul return(-1); 6446186Swpaul } 6546186Swpaul 6646186Swpaul return(fork()); 6746186Swpaul} 6846186Swpaul 6914262Swpaul/* 7014262Swpaul * NIS v2 support. This is where most of the action happens. 7114262Swpaul */ 7214262Swpaul 7312891Swpaulvoid * 7412891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp) 7512891Swpaul{ 7612891Swpaul static char * result; 7712891Swpaul static char rval = 0; 7812891Swpaul 7919161Swpaul#ifdef DB_CACHE 8019161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 8119161Swpaul#else 8212891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 8319161Swpaul#endif 8412891Swpaul return(NULL); 8512891Swpaul 8612891Swpaul result = &rval; 8712891Swpaul 8812891Swpaul return((void *) &result); 8912891Swpaul} 9012891Swpaul 9112891Swpaulbool_t * 9212891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp) 9312891Swpaul{ 9412891Swpaul static bool_t result; 9512891Swpaul 9619161Swpaul#ifdef DB_CACHE 9719161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) { 9819161Swpaul#else 9912891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 10019161Swpaul#endif 10112891Swpaul result = FALSE; 10212891Swpaul return (&result); 10312891Swpaul } 10412891Swpaul 10512891Swpaul if (argp == NULL || yp_validdomain(*argp)) 10612891Swpaul result = FALSE; 10712891Swpaul else 10812891Swpaul result = TRUE; 10912891Swpaul 11012891Swpaul return (&result); 11112891Swpaul} 11212891Swpaul 11312891Swpaulbool_t * 11412891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp) 11512891Swpaul{ 11612891Swpaul static bool_t result; 11712891Swpaul 11819161Swpaul#ifdef DB_CACHE 11919161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 12019161Swpaul#else 12112891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 12219161Swpaul#endif 12312891Swpaul return (NULL); 12412891Swpaul 12512891Swpaul if (argp == NULL || yp_validdomain(*argp)) 12612891Swpaul return (NULL); 12712891Swpaul else 12812891Swpaul result = TRUE; 12912891Swpaul 13012891Swpaul return (&result); 13112891Swpaul} 13212891Swpaul 13312891Swpaulypresp_val * 13412891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp) 13512891Swpaul{ 13612891Swpaul static ypresp_val result; 13712891Swpaul 13814304Swpaul result.val.valdat_val = ""; 13914304Swpaul result.val.valdat_len = 0; 14019161Swpaul 14119161Swpaul#ifdef DB_CACHE 14219161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 14319161Swpaul#else 14412891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 14519161Swpaul#endif 14612891Swpaul result.stat = YP_YPERR; 14712891Swpaul return (&result); 14812891Swpaul } 14912891Swpaul 15012891Swpaul if (argp->domain == NULL || argp->map == NULL) { 15112891Swpaul result.stat = YP_BADARGS; 15212891Swpaul return (&result); 15312891Swpaul } 15412891Swpaul 15533250Swpaul if (yp_select_map(argp->map, argp->domain, NULL, 1) != YP_TRUE) { 15620818Swpaul result.stat = yp_errno; 15720818Swpaul return(&result); 15812891Swpaul } 15912891Swpaul 16020818Swpaul result.stat = yp_getbykey(&argp->key, &result.val); 16120818Swpaul 16212891Swpaul /* 16312891Swpaul * Do DNS lookups for hosts maps if database lookup failed. 16412891Swpaul */ 16512891Swpaul 16619161Swpaul#ifdef DB_CACHE 167159461Smaxim if (do_dns && result.stat != YP_TRUE && 16819161Swpaul (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) || 169159461Smaxim (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes")))) { 17019161Swpaul#else 171145792Sume if (do_dns && result.stat != YP_TRUE && 172145793Sume (strstr(argp->map, "hosts") || strstr(argp->map, "ipnodes"))) { 17319161Swpaul#endif 17427589Swpaul char nbuf[YPMAXRECORD]; 17590297Sdes 17620818Swpaul /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */ 17727589Swpaul bcopy(argp->key.keydat_val, nbuf, argp->key.keydat_len); 17827589Swpaul nbuf[argp->key.keydat_len] = '\0'; 17912891Swpaul 18012891Swpaul if (debug) 18130827Scharnier yp_error("doing DNS lookup of %s", nbuf); 18212891Swpaul 18312891Swpaul if (!strcmp(argp->map, "hosts.byname")) 184145792Sume result.stat = yp_async_lookup_name(rqstp, nbuf, 185145792Sume AF_INET); 18612891Swpaul else if (!strcmp(argp->map, "hosts.byaddr")) 187145792Sume result.stat = yp_async_lookup_addr(rqstp, nbuf, 188145792Sume AF_INET); 189145792Sume else if (!strcmp(argp->map, "ipnodes.byname")) 190145792Sume result.stat = yp_async_lookup_name(rqstp, nbuf, 191145792Sume AF_INET6); 192145792Sume else if (!strcmp(argp->map, "ipnodes.byaddr")) 193145792Sume result.stat = yp_async_lookup_addr(rqstp, nbuf, 194145792Sume AF_INET6); 19512891Swpaul 19620818Swpaul if (result.stat == YP_TRUE) 19720818Swpaul return(NULL); 19812891Swpaul } 19912891Swpaul 20012891Swpaul return (&result); 20112891Swpaul} 20212891Swpaul 20312891Swpaulypresp_key_val * 20412891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 20512891Swpaul{ 20612891Swpaul static ypresp_key_val result; 20712891Swpaul 20814304Swpaul result.val.valdat_val = result.key.keydat_val = ""; 20914304Swpaul result.val.valdat_len = result.key.keydat_len = 0; 21019161Swpaul 21119161Swpaul#ifdef DB_CACHE 21219161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 21319161Swpaul#else 21412891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 21519161Swpaul#endif 21612891Swpaul result.stat = YP_YPERR; 21712891Swpaul return (&result); 21812891Swpaul } 21912891Swpaul 22012891Swpaul if (argp->domain == NULL) { 22112891Swpaul result.stat = YP_BADARGS; 22212891Swpaul return (&result); 22312891Swpaul } 22412891Swpaul 22533250Swpaul if (yp_select_map(argp->map, argp->domain, NULL, 0) != YP_TRUE) { 22612891Swpaul result.stat = yp_errno; 22712891Swpaul return(&result); 22812891Swpaul } 22912891Swpaul 23020818Swpaul result.stat = yp_firstbykey(&result.key, &result.val); 23112891Swpaul 23212891Swpaul return (&result); 23312891Swpaul} 23412891Swpaul 23512891Swpaulypresp_key_val * 23612891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp) 23712891Swpaul{ 23812891Swpaul static ypresp_key_val result; 23912891Swpaul 24014304Swpaul result.val.valdat_val = result.key.keydat_val = ""; 24114304Swpaul result.val.valdat_len = result.key.keydat_len = 0; 24215426Swpaul 24319161Swpaul#ifdef DB_CACHE 24419161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 24519161Swpaul#else 24612891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 24719161Swpaul#endif 24812891Swpaul result.stat = YP_YPERR; 24912891Swpaul return (&result); 25012891Swpaul } 25112891Swpaul 25212891Swpaul if (argp->domain == NULL || argp->map == NULL) { 25312891Swpaul result.stat = YP_BADARGS; 25412891Swpaul return (&result); 25512891Swpaul } 25612891Swpaul 25720818Swpaul if (yp_select_map(argp->map, argp->domain, &argp->key, 0) != YP_TRUE) { 25812891Swpaul result.stat = yp_errno; 25912891Swpaul return(&result); 26012891Swpaul } 26112891Swpaul 26220818Swpaul result.key.keydat_len = argp->key.keydat_len; 26320818Swpaul result.key.keydat_val = argp->key.keydat_val; 26412891Swpaul 26520818Swpaul result.stat = yp_nextbykey(&result.key, &result.val); 26620818Swpaul 26712891Swpaul return (&result); 26812891Swpaul} 26912891Swpaul 27090298Sdesstatic void 27190298Sdesypxfr_callback(ypxfrstat rval, struct sockaddr_in *addr, unsigned int transid, 27290298Sdes unsigned int prognum, unsigned long port) 27312997Swpaul{ 27412997Swpaul CLIENT *clnt; 27512997Swpaul int sock = RPC_ANYSOCK; 27612997Swpaul struct timeval timeout; 27712997Swpaul yppushresp_xfr ypxfr_resp; 27813375Swpaul struct rpc_err err; 27912997Swpaul 28013375Swpaul timeout.tv_sec = 5; 28112997Swpaul timeout.tv_usec = 0; 28212997Swpaul addr->sin_port = htons(port); 28312997Swpaul 28419131Swpaul if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) { 28519131Swpaul yp_error("%s: %s", inet_ntoa(addr->sin_addr), 28619161Swpaul clnt_spcreateerror("failed to establish callback handle")); 28719131Swpaul return; 28819131Swpaul } 28912997Swpaul 29012997Swpaul ypxfr_resp.status = rval; 29112997Swpaul ypxfr_resp.transid = transid; 29212997Swpaul 29313375Swpaul /* Turn the timeout off -- we don't want to block. */ 29413375Swpaul timeout.tv_sec = 0; 29595658Sdes if (clnt_control(clnt, CLSET_TIMEOUT, &timeout) == FALSE) 29613375Swpaul yp_error("failed to set timeout on ypproc_xfr callback"); 29712997Swpaul 29813375Swpaul if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) { 29913375Swpaul clnt_geterr(clnt, &err); 30013375Swpaul if (err.re_status != RPC_SUCCESS && 30113375Swpaul err.re_status != RPC_TIMEDOUT) 30213375Swpaul yp_error("%s", clnt_sperror(clnt, 30313375Swpaul "ypxfr callback failed")); 30413375Swpaul } 30513375Swpaul 30612997Swpaul clnt_destroy(clnt); 30712997Swpaul return; 30812997Swpaul} 30912997Swpaul 31015426Swpaul#define YPXFR_RETURN(CODE) \ 31115426Swpaul /* Order is important: send regular RPC reply, then callback */ \ 31215426Swpaul result.xfrstat = CODE; \ 31395658Sdes svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_ypresp_xfr, &result); \ 31415426Swpaul ypxfr_callback(CODE,rqhost,argp->transid, \ 31515426Swpaul argp->prog,argp->port); \ 31615426Swpaul return(NULL); 31715426Swpaul 31812891Swpaulypresp_xfr * 31912891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp) 32012891Swpaul{ 32112891Swpaul static ypresp_xfr result; 32212997Swpaul struct sockaddr_in *rqhost; 32324780Swpaul ypresp_master *mres; 32424780Swpaul ypreq_nokey mreq; 32512891Swpaul 32613375Swpaul result.transid = argp->transid; 32713375Swpaul rqhost = svc_getcaller(rqstp->rq_xprt); 32813375Swpaul 32919161Swpaul#ifdef DB_CACHE 33019161Swpaul if (yp_access(argp->map_parms.map, 33119161Swpaul argp->map_parms.domain, (struct svc_req *)rqstp)) { 33219161Swpaul#else 33312891Swpaul if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) { 33419161Swpaul#endif 33524780Swpaul YPXFR_RETURN(YPXFR_REFUSED) 33612891Swpaul } 33712891Swpaul 33824780Swpaul 33912891Swpaul if (argp->map_parms.domain == NULL) { 34024780Swpaul YPXFR_RETURN(YPXFR_BADARGS) 34112891Swpaul } 34212891Swpaul 34312891Swpaul if (yp_validdomain(argp->map_parms.domain)) { 34424780Swpaul YPXFR_RETURN(YPXFR_NODOM) 34512891Swpaul } 34612891Swpaul 34724780Swpaul /* 34824780Swpaul * Determine the master host ourselves. The caller may 34924780Swpaul * be up to no good. This has the side effect of verifying 35024780Swpaul * that the requested map and domain actually exist. 35124780Swpaul */ 35224780Swpaul 35324780Swpaul mreq.domain = argp->map_parms.domain; 35424780Swpaul mreq.map = argp->map_parms.map; 35524780Swpaul 35624780Swpaul mres = ypproc_master_2_svc(&mreq, rqstp); 35724780Swpaul 35824780Swpaul if (mres->stat != YP_TRUE) { 35924780Swpaul yp_error("couldn't find master for map %s@%s", 36024780Swpaul argp->map_parms.map, 36124780Swpaul argp->map_parms.domain); 36224780Swpaul yp_error("host at %s (%s) may be pulling my leg", 36324780Swpaul argp->map_parms.peer, 36424780Swpaul inet_ntoa(rqhost->sin_addr)); 36524780Swpaul YPXFR_RETURN(YPXFR_REFUSED) 36624780Swpaul } 36724780Swpaul 36890297Sdes switch (yp_fork()) { 36912891Swpaul case 0: 37012891Swpaul { 37112891Swpaul char g[11], t[11], p[11]; 37212891Swpaul char ypxfr_command[MAXPATHLEN + 2]; 37312891Swpaul 37480184Skris snprintf (ypxfr_command, sizeof(ypxfr_command), "%sypxfr", _PATH_LIBEXEC); 37580184Skris snprintf (t, sizeof(t), "%u", argp->transid); 37680184Skris snprintf (g, sizeof(g), "%u", argp->prog); 37780184Skris snprintf (p, sizeof(p), "%u", argp->port); 37822321Swpaul if (debug) { 37912997Swpaul close(0); close(1); close(2); 38022321Swpaul } 38112997Swpaul if (strcmp(yp_dir, _PATH_YP)) { 38214304Swpaul execl(ypxfr_command, "ypxfr", 38314304Swpaul "-d", argp->map_parms.domain, 38424780Swpaul "-h", mres->peer, 38514304Swpaul "-p", yp_dir, "-C", t, 38614304Swpaul g, inet_ntoa(rqhost->sin_addr), 38714304Swpaul p, argp->map_parms.map, 38895658Sdes NULL); 38912997Swpaul } else { 39014304Swpaul execl(ypxfr_command, "ypxfr", 39114304Swpaul "-d", argp->map_parms.domain, 39224780Swpaul "-h", mres->peer, 39314304Swpaul "-C", t, 39414304Swpaul g, inet_ntoa(rqhost->sin_addr), 39514304Swpaul p, argp->map_parms.map, 39695658Sdes NULL); 39712997Swpaul } 39815426Swpaul yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno)); 39924780Swpaul YPXFR_RETURN(YPXFR_XFRERR) 40046205Swpaul /* 40146205Swpaul * Just to safe, prevent PR #10970 from biting us in 40246205Swpaul * the unlikely case that execing ypxfr fails. We don't 40346205Swpaul * want to have any child processes spawned from this 40446205Swpaul * child process. 40546205Swpaul */ 40646205Swpaul _exit(0); 40712997Swpaul break; 40812891Swpaul } 40912891Swpaul case -1: 41012891Swpaul yp_error("ypxfr fork(): %s", strerror(errno)); 41124780Swpaul YPXFR_RETURN(YPXFR_XFRERR) 41212891Swpaul break; 41312891Swpaul default: 41413375Swpaul result.xfrstat = YPXFR_SUCC; 41512997Swpaul children++; 41612891Swpaul break; 41712891Swpaul } 41813375Swpaul 41913375Swpaul return (&result); 42012891Swpaul} 42115426Swpaul#undef YPXFR_RETURN 42212891Swpaul 42312891Swpaulvoid * 42412891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp) 42512891Swpaul{ 42612891Swpaul static char * result; 42712891Swpaul static char rval = 0; 42812891Swpaul 42919161Swpaul#ifdef DB_CACHE 43019161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 43119161Swpaul#else 43212891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 43319161Swpaul#endif 43412891Swpaul return (NULL); 43515426Swpaul#ifdef DB_CACHE 43615426Swpaul /* clear out the database cache */ 43715426Swpaul yp_flush_all(); 43815426Swpaul#endif 43914240Swpaul /* Re-read the securenets database for the hell of it. */ 44014240Swpaul load_securenets(); 44114240Swpaul 44212891Swpaul result = &rval; 44312891Swpaul return((void *) &result); 44412891Swpaul} 44512891Swpaul 44612891Swpaul/* 44712891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures 44812891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol 44912891Swpaul * definition file only serializes one such structure. This means that 45012891Swpaul * to send the whole stream, you need a wrapper which feeds all the 45112891Swpaul * records into the underlying XDR routine until it hits an 'EOF.' 45212891Swpaul * But to use the wrapper, you have to violate the boundaries between 45312891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all 45412891Swpaul * service routine instead of letting the RPC dispatcher do it. 45512891Swpaul * 45612891Swpaul * Bleah. 45712891Swpaul */ 45812891Swpaul 45912891Swpaul/* 46020100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep 46120100Swpaul * reading from the database and spew until we run out of records 46220100Swpaul * or encounter an error. 46312891Swpaul */ 46412891Swpaulstatic bool_t 46512891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp) 46612891Swpaul{ 46720100Swpaul while (1) { 46820100Swpaul /* Get a record. */ 46920100Swpaul if ((objp->ypresp_all_u.val.stat = 47020818Swpaul yp_nextbykey(&objp->ypresp_all_u.val.key, 47120818Swpaul &objp->ypresp_all_u.val.val)) == YP_TRUE) { 47220100Swpaul objp->more = TRUE; 47320100Swpaul } else { 47420100Swpaul objp->more = FALSE; 47520100Swpaul } 47620100Swpaul 47720100Swpaul /* Serialize. */ 47820100Swpaul if (!xdr_ypresp_all(xdrs, objp)) 47920100Swpaul return(FALSE); 48020100Swpaul if (objp->more == FALSE) 48120100Swpaul return(TRUE); 48220100Swpaul } 48312891Swpaul} 48412891Swpaul 48512891Swpaulypresp_all * 48612891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 48712891Swpaul{ 48812891Swpaul static ypresp_all result; 48912891Swpaul 49012891Swpaul /* 49112891Swpaul * Set this here so that the client will be forced to make 49212891Swpaul * at least one attempt to read from us even if all we're 49312891Swpaul * doing is returning an error. 49412891Swpaul */ 49512891Swpaul result.more = TRUE; 49614304Swpaul result.ypresp_all_u.val.key.keydat_len = 0; 49714304Swpaul result.ypresp_all_u.val.key.keydat_val = ""; 49812891Swpaul 49919161Swpaul#ifdef DB_CACHE 50019161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 50119161Swpaul#else 50212891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 50319161Swpaul#endif 50412891Swpaul result.ypresp_all_u.val.stat = YP_YPERR; 50512891Swpaul return (&result); 50612891Swpaul } 50712891Swpaul 50812891Swpaul if (argp->domain == NULL || argp->map == NULL) { 50912891Swpaul result.ypresp_all_u.val.stat = YP_BADARGS; 51012891Swpaul return (&result); 51112891Swpaul } 51212891Swpaul 51320100Swpaul /* 51421389Swpaul * XXX If we hit the child limit, fail the request. 51521389Swpaul * If we don't, and the map is large, we could block for 51621389Swpaul * a long time in the parent. 51721389Swpaul */ 51821389Swpaul if (children >= MAX_CHILDREN) { 51921389Swpaul result.ypresp_all_u.val.stat = YP_YPERR; 52021389Swpaul return(&result); 52121389Swpaul } 52221389Swpaul 52321389Swpaul /* 52420100Swpaul * The ypproc_all procedure can take a while to complete. 52520100Swpaul * Best to handle it in a subprocess so the parent doesn't 52620100Swpaul * block. (Is there a better way to do this? Maybe with 52720100Swpaul * async socket I/O?) 52820100Swpaul */ 52943847Swpaul if (!debug) { 53090297Sdes switch (yp_fork()) { 53143847Swpaul case 0: 53243847Swpaul break; 53343847Swpaul case -1: 53443847Swpaul yp_error("ypall fork(): %s", strerror(errno)); 53543847Swpaul result.ypresp_all_u.val.stat = YP_YPERR; 53643847Swpaul return(&result); 53743847Swpaul break; 53843847Swpaul default: 53943847Swpaul children++; 54043847Swpaul return (NULL); 54143847Swpaul break; 54243847Swpaul } 54320100Swpaul } 54420100Swpaul 54546207Swpaul /* 54646207Swpaul * Fix for PR #10971: don't let the child ypserv share 54746207Swpaul * DB handles with the parent process. 54846207Swpaul */ 54946207Swpaul#ifdef DB_CACHE 55046207Swpaul yp_flush_all(); 55146207Swpaul#endif 55246207Swpaul 55320818Swpaul if (yp_select_map(argp->map, argp->domain, 55420818Swpaul &result.ypresp_all_u.val.key, 0) != YP_TRUE) { 55512891Swpaul result.ypresp_all_u.val.stat = yp_errno; 55612891Swpaul return(&result); 55712891Swpaul } 55812891Swpaul 55912891Swpaul /* Kick off the actual data transfer. */ 56095658Sdes svc_sendreply(rqstp->rq_xprt, (xdrproc_t)xdr_my_ypresp_all, &result); 56120100Swpaul 56212891Swpaul /* 56346205Swpaul * Proper fix for PR #10970: exit here so that we don't risk 56446205Swpaul * having a child spawned from this sub-process. 56512891Swpaul */ 566200100Skuriyama if (!debug) 567200100Skuriyama _exit(0); 568200100Skuriyama 569200100Skuriyama return &result; 57012891Swpaul} 57112891Swpaul 57212891Swpaulypresp_master * 57312891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 57412891Swpaul{ 57512891Swpaul static ypresp_master result; 57615426Swpaul static char ypvalbuf[YPMAXRECORD]; 57728042Swpaul keydat key = { MASTER_SZ, MASTER_STRING }; 57820818Swpaul valdat val; 57912891Swpaul 58014303Swpaul result.peer = ""; 58114303Swpaul 58219161Swpaul#ifdef DB_CACHE 58319161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 58419161Swpaul#else 58519161Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 58619161Swpaul#endif 58712891Swpaul result.stat = YP_YPERR; 58812891Swpaul return(&result); 58912891Swpaul } 59012891Swpaul 59112891Swpaul if (argp->domain == NULL) { 59212891Swpaul result.stat = YP_BADARGS; 59312891Swpaul return (&result); 59412891Swpaul } 59512891Swpaul 59620818Swpaul if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) { 59720818Swpaul result.stat = yp_errno; 59820818Swpaul return(&result); 59920818Swpaul } 60020818Swpaul 60115426Swpaul /* 60215426Swpaul * Note that we copy the data retrieved from the database to 60315426Swpaul * a private buffer and NUL terminate the buffer rather than 60415426Swpaul * terminating the data in place. We do this because by stuffing 60515426Swpaul * a '\0' into data.data, we will actually be corrupting memory 60615426Swpaul * allocated by the DB package. This is a bad thing now that we 60715426Swpaul * cache DB handles rather than closing the database immediately. 60815426Swpaul */ 60920818Swpaul result.stat = yp_getbykey(&key, &val); 61020818Swpaul if (result.stat == YP_TRUE) { 61195658Sdes bcopy(val.valdat_val, &ypvalbuf, val.valdat_len); 61220818Swpaul ypvalbuf[val.valdat_len] = '\0'; 61395658Sdes result.peer = ypvalbuf; 61412891Swpaul } else 61512891Swpaul result.peer = ""; 61612891Swpaul 61712891Swpaul return (&result); 61812891Swpaul} 61912891Swpaul 62012891Swpaulypresp_order * 62112891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 62212891Swpaul{ 62312891Swpaul static ypresp_order result; 62428042Swpaul keydat key = { ORDER_SZ, ORDER_STRING }; 62520818Swpaul valdat val; 62612891Swpaul 62714304Swpaul result.ordernum = 0; 62814304Swpaul 62919161Swpaul#ifdef DB_CACHE 63019161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 63119161Swpaul#else 63219161Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 63319161Swpaul#endif 63412891Swpaul result.stat = YP_YPERR; 63512891Swpaul return(&result); 63612891Swpaul } 63712891Swpaul 63812891Swpaul if (argp->domain == NULL) { 63912891Swpaul result.stat = YP_BADARGS; 64012891Swpaul return (&result); 64112891Swpaul } 64290297Sdes 64312891Swpaul /* 64412891Swpaul * We could just check the timestamp on the map file, 64512891Swpaul * but that's a hack: we'll only know the last time the file 64612891Swpaul * was touched, not the last time the database contents were 64712891Swpaul * updated. 64812891Swpaul */ 64912891Swpaul 65020818Swpaul if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) { 65120818Swpaul result.stat = yp_errno; 65220818Swpaul return(&result); 65320818Swpaul } 65420818Swpaul 65520818Swpaul result.stat = yp_getbykey(&key, &val); 65620818Swpaul 65720818Swpaul if (result.stat == YP_TRUE) 65895658Sdes result.ordernum = atoi(val.valdat_val); 65912891Swpaul else 66012891Swpaul result.ordernum = 0; 66112891Swpaul 66212891Swpaul return (&result); 66312891Swpaul} 66412891Swpaul 66590298Sdesstatic void yp_maplist_free(struct ypmaplist *yp_maplist) 66612891Swpaul{ 66712891Swpaul register struct ypmaplist *next; 66812891Swpaul 66990297Sdes while (yp_maplist) { 67012891Swpaul next = yp_maplist->next; 67112891Swpaul free(yp_maplist->map); 67212891Swpaul free(yp_maplist); 67312891Swpaul yp_maplist = next; 67412891Swpaul } 67512891Swpaul return; 67612891Swpaul} 67712891Swpaul 67890298Sdesstatic struct ypmaplist * 67990298Sdesyp_maplist_create(const char *domain) 68012891Swpaul{ 68112891Swpaul char yp_mapdir[MAXPATHLEN + 2]; 68212891Swpaul char yp_mapname[MAXPATHLEN + 2]; 68312891Swpaul struct ypmaplist *cur = NULL; 68412891Swpaul struct ypmaplist *yp_maplist = NULL; 68512891Swpaul DIR *dird; 68612891Swpaul struct dirent *dirp; 68712891Swpaul struct stat statbuf; 68812891Swpaul 68912891Swpaul snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain); 69012891Swpaul 69112891Swpaul if ((dird = opendir(yp_mapdir)) == NULL) { 69213800Swpaul yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno)); 69312891Swpaul return(NULL); 69412891Swpaul } 69512891Swpaul 69612891Swpaul while ((dirp = readdir(dird)) != NULL) { 69712891Swpaul if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) { 69814304Swpaul snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s", 69914304Swpaul yp_mapdir,dirp->d_name); 70014304Swpaul if (stat(yp_mapname, &statbuf) < 0 || 70114304Swpaul !S_ISREG(statbuf.st_mode)) 70212891Swpaul continue; 70314304Swpaul if ((cur = (struct ypmaplist *) 70416044Swpaul malloc(sizeof(struct ypmaplist))) == NULL) { 70530827Scharnier yp_error("malloc() failed"); 70612891Swpaul closedir(dird); 70712891Swpaul yp_maplist_free(yp_maplist); 70812891Swpaul return(NULL); 70912891Swpaul } 71095658Sdes if ((cur->map = strdup(dirp->d_name)) == NULL) { 71114304Swpaul yp_error("strdup() failed: %s",strerror(errno)); 71212891Swpaul closedir(dird); 71312891Swpaul yp_maplist_free(yp_maplist); 714301143Struckman free(cur); 71512891Swpaul return(NULL); 71612891Swpaul } 71712891Swpaul cur->next = yp_maplist; 71812891Swpaul yp_maplist = cur; 71912891Swpaul if (debug) 72012891Swpaul yp_error("map: %s", yp_maplist->map); 72112891Swpaul } 72212891Swpaul 72312891Swpaul } 72412891Swpaul closedir(dird); 72512891Swpaul return(yp_maplist); 72612891Swpaul} 72712891Swpaul 72812891Swpaulypresp_maplist * 72912891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp) 73012891Swpaul{ 73115426Swpaul static ypresp_maplist result = { 0, NULL }; 73212891Swpaul 73319161Swpaul#ifdef DB_CACHE 73419161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) { 73519161Swpaul#else 73612891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 73719161Swpaul#endif 73812891Swpaul result.stat = YP_YPERR; 73912891Swpaul return(&result); 74012891Swpaul } 74112891Swpaul 74212891Swpaul if (argp == NULL) { 74312891Swpaul result.stat = YP_BADARGS; 74412891Swpaul return (&result); 74512891Swpaul } 74690297Sdes 74712891Swpaul if (yp_validdomain(*argp)) { 74812891Swpaul result.stat = YP_NODOM; 74912891Swpaul return (&result); 75012891Swpaul } 75112891Swpaul 75212891Swpaul /* 75312891Swpaul * We have to construct a linked list for the ypproc_maplist 75412891Swpaul * procedure using dynamically allocated memory. Since the XDR 75512891Swpaul * layer won't free this list for us, we have to deal with it 75612891Swpaul * ourselves. We call yp_maplist_free() first to free any 75712891Swpaul * previously allocated data we may have accumulated to insure 75812891Swpaul * that we have only one linked list in memory at any given 75912891Swpaul * time. 76012891Swpaul */ 76112891Swpaul 76212891Swpaul yp_maplist_free(result.maps); 76312891Swpaul 76412891Swpaul if ((result.maps = yp_maplist_create(*argp)) == NULL) { 76512891Swpaul yp_error("yp_maplist_create failed"); 76612891Swpaul result.stat = YP_YPERR; 76712891Swpaul return(&result); 76812891Swpaul } else 76912891Swpaul result.stat = YP_TRUE; 77012891Swpaul 77112891Swpaul return (&result); 77212891Swpaul} 77314262Swpaul 77414262Swpaul/* 77514262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack 77614262Swpaul * functions from v1 are identical to those in v2, so all 77714262Swpaul * we have to do is hand off to them. 77814262Swpaul * 77914262Swpaul * The other functions are mostly just wrappers around their v2 78014262Swpaul * counterparts. For example, for the v1 'match' procedure, we 78114262Swpaul * crack open the argument structure, make a request to the v2 78214262Swpaul * 'match' function, repackage the data into a v1 response and 78314262Swpaul * then send it on its way. 78414262Swpaul * 78514262Swpaul * Note that we don't support the pull, push and get procedures. 78614262Swpaul * There's little documentation available to show what they 78714262Swpaul * do, and I suspect they're meant largely for map transfers 78814262Swpaul * between master and slave servers. 78914262Swpaul */ 79014262Swpaul 79114262Swpaulvoid * 79214262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp) 79314262Swpaul{ 79414262Swpaul return(ypproc_null_2_svc(argp, rqstp)); 79514262Swpaul} 79614262Swpaul 79714262Swpaulbool_t * 79814262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp) 79914262Swpaul{ 80014262Swpaul return(ypproc_domain_2_svc(argp, rqstp)); 80114262Swpaul} 80214262Swpaul 80314262Swpaulbool_t * 80414262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp) 80514262Swpaul{ 80614262Swpaul return (ypproc_domain_nonack_2_svc(argp, rqstp)); 80714262Swpaul} 80814262Swpaul 80914304Swpaul/* 81014304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL 81114304Swpaul */ 81214262Swpaulypresponse * 81314262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp) 81414262Swpaul{ 81514262Swpaul static ypresponse result; 81614262Swpaul ypresp_val *v2_result; 81714262Swpaul 81814262Swpaul result.yp_resptype = YPRESP_VAL; 81914304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_val = ""; 82014304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0; 82114262Swpaul 82214262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 82314262Swpaul result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS; 82414262Swpaul return(&result); 82514262Swpaul } 82614262Swpaul 82714262Swpaul v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 82814262Swpaul if (v2_result == NULL) 82914262Swpaul return(NULL); 83014262Swpaul 83195658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_valtype, 83214262Swpaul sizeof(ypresp_val)); 83314262Swpaul 83414262Swpaul return (&result); 83514262Swpaul} 83614262Swpaul 83714304Swpaul/* 83814304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL 83914304Swpaul */ 84014262Swpaulypresponse * 84114262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp) 84214262Swpaul{ 84314262Swpaul static ypresponse result; 84414262Swpaul ypresp_key_val *v2_result; 84514262Swpaul 84614262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 84714304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 84814304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 84914304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 85014304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 85114262Swpaul 85214262Swpaul if (argp->yp_reqtype != YPREQ_NOKEY) { 85314262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 85414262Swpaul return(&result); 85514262Swpaul } 85614262Swpaul 85714262Swpaul v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype, 85814262Swpaul rqstp); 85914262Swpaul if (v2_result == NULL) 86014262Swpaul return(NULL); 86114262Swpaul 86295658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype, 86314262Swpaul sizeof(ypresp_key_val)); 86414262Swpaul 86514262Swpaul return (&result); 86614262Swpaul} 86714262Swpaul 86814304Swpaul/* 86914304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL 87014304Swpaul */ 87114262Swpaulypresponse * 87214262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp) 87314262Swpaul{ 87414262Swpaul static ypresponse result; 87514262Swpaul ypresp_key_val *v2_result; 87614262Swpaul 87714262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 87814304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 87914304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 88014304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 88114304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 88214262Swpaul 88314262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 88414262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 88514262Swpaul return(&result); 88614262Swpaul } 88714262Swpaul 88814262Swpaul v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 88914262Swpaul if (v2_result == NULL) 89014262Swpaul return(NULL); 89114262Swpaul 89295658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype, 89314262Swpaul sizeof(ypresp_key_val)); 89414262Swpaul 89514262Swpaul return (&result); 89614262Swpaul} 89714262Swpaul 89814304Swpaul/* 89914304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS 90014304Swpaul */ 90114262Swpaulypresponse * 90214262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp) 90314262Swpaul{ 90414262Swpaul static ypresponse result; 90514262Swpaul ypresp_master *v2_result1; 90614262Swpaul ypresp_order *v2_result2; 90714262Swpaul 90814262Swpaul result.yp_resptype = YPRESP_MAP_PARMS; 90914262Swpaul result.ypresponse_u.yp_resp_map_parmstype.domain = 91014262Swpaul argp->yprequest_u.yp_req_nokeytype.domain; 91114262Swpaul result.ypresponse_u.yp_resp_map_parmstype.map = 91214262Swpaul argp->yprequest_u.yp_req_nokeytype.map; 91314262Swpaul /* 91414262Swpaul * Hmm... there is no 'status' value in the 91514262Swpaul * yp_resp_map_parmstype structure, so I have to 91614262Swpaul * guess at what to do to indicate a failure. 91714262Swpaul * I hope this is right. 91814262Swpaul */ 91914262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0; 92014262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = ""; 92114262Swpaul 92214262Swpaul if (argp->yp_reqtype != YPREQ_MAP_PARMS) { 92314262Swpaul return(&result); 92414262Swpaul } 92514262Swpaul 92614262Swpaul v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype, 92714262Swpaul rqstp); 92814262Swpaul if (v2_result1 == NULL) 92914262Swpaul return(NULL); 93014262Swpaul 93114262Swpaul if (v2_result1->stat != YP_TRUE) { 93214262Swpaul return(&result); 93314262Swpaul } 93414262Swpaul 93514262Swpaul v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype, 93614262Swpaul rqstp); 93714262Swpaul if (v2_result2 == NULL) 93814262Swpaul return(NULL); 93914262Swpaul 94014262Swpaul if (v2_result2->stat != YP_TRUE) { 94114262Swpaul return(&result); 94214262Swpaul } 94314262Swpaul 94414262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = 94514262Swpaul v2_result1->peer; 94614262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 94714262Swpaul v2_result2->ordernum; 94814262Swpaul 94914262Swpaul return (&result); 95014262Swpaul} 95114262Swpaul 95214262Swpaulypresponse * 95314262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp) 95414262Swpaul{ 95514262Swpaul static ypresponse result; 95614262Swpaul 95714262Swpaul /* 95814262Swpaul * Not implemented. 95914262Swpaul */ 96014262Swpaul 96114262Swpaul return (&result); 96214262Swpaul} 96314262Swpaul 96414262Swpaulypresponse * 96514262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp) 96614262Swpaul{ 96714262Swpaul static ypresponse result; 96814262Swpaul 96914262Swpaul /* 97014262Swpaul * Not implemented. 97114262Swpaul */ 97214262Swpaul 97314262Swpaul return (&result); 97414262Swpaul} 97514262Swpaul 97614262Swpaulypresponse * 97714262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp) 97814262Swpaul{ 97914262Swpaul static ypresponse result; 98014262Swpaul 98114262Swpaul /* 98214262Swpaul * Not implemented. 98314262Swpaul */ 98414262Swpaul 98514262Swpaul return (&result); 98614262Swpaul} 987