yp_server.c revision 145792
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: head/usr.sbin/ypserv/yp_server.c 145792 2005-05-02 09:34:07Z ume $"); 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 16719161Swpaul if (result.stat != YP_TRUE && 16819161Swpaul (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) || 169145792Sume ((strstr(argp->map, "hosts") || strstr(argp->map, "ipnode")) && do_dns))) { 17019161Swpaul#else 171145792Sume if (do_dns && result.stat != YP_TRUE && 172145792Sume (strstr(argp->map, "hosts") || strstr(argp->map, "ipnode"))) { 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 */ 56646205Swpaul _exit(0); 56712891Swpaul} 56812891Swpaul 56912891Swpaulypresp_master * 57012891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 57112891Swpaul{ 57212891Swpaul static ypresp_master result; 57315426Swpaul static char ypvalbuf[YPMAXRECORD]; 57428042Swpaul keydat key = { MASTER_SZ, MASTER_STRING }; 57520818Swpaul valdat val; 57612891Swpaul 57714303Swpaul result.peer = ""; 57814303Swpaul 57919161Swpaul#ifdef DB_CACHE 58019161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 58119161Swpaul#else 58219161Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 58319161Swpaul#endif 58412891Swpaul result.stat = YP_YPERR; 58512891Swpaul return(&result); 58612891Swpaul } 58712891Swpaul 58812891Swpaul if (argp->domain == NULL) { 58912891Swpaul result.stat = YP_BADARGS; 59012891Swpaul return (&result); 59112891Swpaul } 59212891Swpaul 59320818Swpaul if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) { 59420818Swpaul result.stat = yp_errno; 59520818Swpaul return(&result); 59620818Swpaul } 59720818Swpaul 59815426Swpaul /* 59915426Swpaul * Note that we copy the data retrieved from the database to 60015426Swpaul * a private buffer and NUL terminate the buffer rather than 60115426Swpaul * terminating the data in place. We do this because by stuffing 60215426Swpaul * a '\0' into data.data, we will actually be corrupting memory 60315426Swpaul * allocated by the DB package. This is a bad thing now that we 60415426Swpaul * cache DB handles rather than closing the database immediately. 60515426Swpaul */ 60620818Swpaul result.stat = yp_getbykey(&key, &val); 60720818Swpaul if (result.stat == YP_TRUE) { 60895658Sdes bcopy(val.valdat_val, &ypvalbuf, val.valdat_len); 60920818Swpaul ypvalbuf[val.valdat_len] = '\0'; 61095658Sdes result.peer = ypvalbuf; 61112891Swpaul } else 61212891Swpaul result.peer = ""; 61312891Swpaul 61412891Swpaul return (&result); 61512891Swpaul} 61612891Swpaul 61712891Swpaulypresp_order * 61812891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 61912891Swpaul{ 62012891Swpaul static ypresp_order result; 62128042Swpaul keydat key = { ORDER_SZ, ORDER_STRING }; 62220818Swpaul valdat val; 62312891Swpaul 62414304Swpaul result.ordernum = 0; 62514304Swpaul 62619161Swpaul#ifdef DB_CACHE 62719161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 62819161Swpaul#else 62919161Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 63019161Swpaul#endif 63112891Swpaul result.stat = YP_YPERR; 63212891Swpaul return(&result); 63312891Swpaul } 63412891Swpaul 63512891Swpaul if (argp->domain == NULL) { 63612891Swpaul result.stat = YP_BADARGS; 63712891Swpaul return (&result); 63812891Swpaul } 63990297Sdes 64012891Swpaul /* 64112891Swpaul * We could just check the timestamp on the map file, 64212891Swpaul * but that's a hack: we'll only know the last time the file 64312891Swpaul * was touched, not the last time the database contents were 64412891Swpaul * updated. 64512891Swpaul */ 64612891Swpaul 64720818Swpaul if (yp_select_map(argp->map, argp->domain, &key, 1) != YP_TRUE) { 64820818Swpaul result.stat = yp_errno; 64920818Swpaul return(&result); 65020818Swpaul } 65120818Swpaul 65220818Swpaul result.stat = yp_getbykey(&key, &val); 65320818Swpaul 65420818Swpaul if (result.stat == YP_TRUE) 65595658Sdes result.ordernum = atoi(val.valdat_val); 65612891Swpaul else 65712891Swpaul result.ordernum = 0; 65812891Swpaul 65912891Swpaul return (&result); 66012891Swpaul} 66112891Swpaul 66290298Sdesstatic void yp_maplist_free(struct ypmaplist *yp_maplist) 66312891Swpaul{ 66412891Swpaul register struct ypmaplist *next; 66512891Swpaul 66690297Sdes while (yp_maplist) { 66712891Swpaul next = yp_maplist->next; 66812891Swpaul free(yp_maplist->map); 66912891Swpaul free(yp_maplist); 67012891Swpaul yp_maplist = next; 67112891Swpaul } 67212891Swpaul return; 67312891Swpaul} 67412891Swpaul 67590298Sdesstatic struct ypmaplist * 67690298Sdesyp_maplist_create(const char *domain) 67712891Swpaul{ 67812891Swpaul char yp_mapdir[MAXPATHLEN + 2]; 67912891Swpaul char yp_mapname[MAXPATHLEN + 2]; 68012891Swpaul struct ypmaplist *cur = NULL; 68112891Swpaul struct ypmaplist *yp_maplist = NULL; 68212891Swpaul DIR *dird; 68312891Swpaul struct dirent *dirp; 68412891Swpaul struct stat statbuf; 68512891Swpaul 68612891Swpaul snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain); 68712891Swpaul 68812891Swpaul if ((dird = opendir(yp_mapdir)) == NULL) { 68913800Swpaul yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno)); 69012891Swpaul return(NULL); 69112891Swpaul } 69212891Swpaul 69312891Swpaul while ((dirp = readdir(dird)) != NULL) { 69412891Swpaul if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) { 69514304Swpaul snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s", 69614304Swpaul yp_mapdir,dirp->d_name); 69714304Swpaul if (stat(yp_mapname, &statbuf) < 0 || 69814304Swpaul !S_ISREG(statbuf.st_mode)) 69912891Swpaul continue; 70014304Swpaul if ((cur = (struct ypmaplist *) 70116044Swpaul malloc(sizeof(struct ypmaplist))) == NULL) { 70230827Scharnier yp_error("malloc() failed"); 70312891Swpaul closedir(dird); 70412891Swpaul yp_maplist_free(yp_maplist); 70512891Swpaul return(NULL); 70612891Swpaul } 70795658Sdes if ((cur->map = strdup(dirp->d_name)) == NULL) { 70814304Swpaul yp_error("strdup() failed: %s",strerror(errno)); 70912891Swpaul closedir(dird); 71012891Swpaul yp_maplist_free(yp_maplist); 71112891Swpaul return(NULL); 71212891Swpaul } 71312891Swpaul cur->next = yp_maplist; 71412891Swpaul yp_maplist = cur; 71512891Swpaul if (debug) 71612891Swpaul yp_error("map: %s", yp_maplist->map); 71712891Swpaul } 71812891Swpaul 71912891Swpaul } 72012891Swpaul closedir(dird); 72112891Swpaul return(yp_maplist); 72212891Swpaul} 72312891Swpaul 72412891Swpaulypresp_maplist * 72512891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp) 72612891Swpaul{ 72715426Swpaul static ypresp_maplist result = { 0, NULL }; 72812891Swpaul 72919161Swpaul#ifdef DB_CACHE 73019161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) { 73119161Swpaul#else 73212891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 73319161Swpaul#endif 73412891Swpaul result.stat = YP_YPERR; 73512891Swpaul return(&result); 73612891Swpaul } 73712891Swpaul 73812891Swpaul if (argp == NULL) { 73912891Swpaul result.stat = YP_BADARGS; 74012891Swpaul return (&result); 74112891Swpaul } 74290297Sdes 74312891Swpaul if (yp_validdomain(*argp)) { 74412891Swpaul result.stat = YP_NODOM; 74512891Swpaul return (&result); 74612891Swpaul } 74712891Swpaul 74812891Swpaul /* 74912891Swpaul * We have to construct a linked list for the ypproc_maplist 75012891Swpaul * procedure using dynamically allocated memory. Since the XDR 75112891Swpaul * layer won't free this list for us, we have to deal with it 75212891Swpaul * ourselves. We call yp_maplist_free() first to free any 75312891Swpaul * previously allocated data we may have accumulated to insure 75412891Swpaul * that we have only one linked list in memory at any given 75512891Swpaul * time. 75612891Swpaul */ 75712891Swpaul 75812891Swpaul yp_maplist_free(result.maps); 75912891Swpaul 76012891Swpaul if ((result.maps = yp_maplist_create(*argp)) == NULL) { 76112891Swpaul yp_error("yp_maplist_create failed"); 76212891Swpaul result.stat = YP_YPERR; 76312891Swpaul return(&result); 76412891Swpaul } else 76512891Swpaul result.stat = YP_TRUE; 76612891Swpaul 76712891Swpaul return (&result); 76812891Swpaul} 76914262Swpaul 77014262Swpaul/* 77114262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack 77214262Swpaul * functions from v1 are identical to those in v2, so all 77314262Swpaul * we have to do is hand off to them. 77414262Swpaul * 77514262Swpaul * The other functions are mostly just wrappers around their v2 77614262Swpaul * counterparts. For example, for the v1 'match' procedure, we 77714262Swpaul * crack open the argument structure, make a request to the v2 77814262Swpaul * 'match' function, repackage the data into a v1 response and 77914262Swpaul * then send it on its way. 78014262Swpaul * 78114262Swpaul * Note that we don't support the pull, push and get procedures. 78214262Swpaul * There's little documentation available to show what they 78314262Swpaul * do, and I suspect they're meant largely for map transfers 78414262Swpaul * between master and slave servers. 78514262Swpaul */ 78614262Swpaul 78714262Swpaulvoid * 78814262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp) 78914262Swpaul{ 79014262Swpaul return(ypproc_null_2_svc(argp, rqstp)); 79114262Swpaul} 79214262Swpaul 79314262Swpaulbool_t * 79414262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp) 79514262Swpaul{ 79614262Swpaul return(ypproc_domain_2_svc(argp, rqstp)); 79714262Swpaul} 79814262Swpaul 79914262Swpaulbool_t * 80014262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp) 80114262Swpaul{ 80214262Swpaul return (ypproc_domain_nonack_2_svc(argp, rqstp)); 80314262Swpaul} 80414262Swpaul 80514304Swpaul/* 80614304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL 80714304Swpaul */ 80814262Swpaulypresponse * 80914262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp) 81014262Swpaul{ 81114262Swpaul static ypresponse result; 81214262Swpaul ypresp_val *v2_result; 81314262Swpaul 81414262Swpaul result.yp_resptype = YPRESP_VAL; 81514304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_val = ""; 81614304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0; 81714262Swpaul 81814262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 81914262Swpaul result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS; 82014262Swpaul return(&result); 82114262Swpaul } 82214262Swpaul 82314262Swpaul v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 82414262Swpaul if (v2_result == NULL) 82514262Swpaul return(NULL); 82614262Swpaul 82795658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_valtype, 82814262Swpaul sizeof(ypresp_val)); 82914262Swpaul 83014262Swpaul return (&result); 83114262Swpaul} 83214262Swpaul 83314304Swpaul/* 83414304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL 83514304Swpaul */ 83614262Swpaulypresponse * 83714262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp) 83814262Swpaul{ 83914262Swpaul static ypresponse result; 84014262Swpaul ypresp_key_val *v2_result; 84114262Swpaul 84214262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 84314304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 84414304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 84514304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 84614304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 84714262Swpaul 84814262Swpaul if (argp->yp_reqtype != YPREQ_NOKEY) { 84914262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 85014262Swpaul return(&result); 85114262Swpaul } 85214262Swpaul 85314262Swpaul v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype, 85414262Swpaul rqstp); 85514262Swpaul if (v2_result == NULL) 85614262Swpaul return(NULL); 85714262Swpaul 85895658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype, 85914262Swpaul sizeof(ypresp_key_val)); 86014262Swpaul 86114262Swpaul return (&result); 86214262Swpaul} 86314262Swpaul 86414304Swpaul/* 86514304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL 86614304Swpaul */ 86714262Swpaulypresponse * 86814262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp) 86914262Swpaul{ 87014262Swpaul static ypresponse result; 87114262Swpaul ypresp_key_val *v2_result; 87214262Swpaul 87314262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 87414304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 87514304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 87614304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 87714304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 87814262Swpaul 87914262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 88014262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 88114262Swpaul return(&result); 88214262Swpaul } 88314262Swpaul 88414262Swpaul v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 88514262Swpaul if (v2_result == NULL) 88614262Swpaul return(NULL); 88714262Swpaul 88895658Sdes bcopy(v2_result, &result.ypresponse_u.yp_resp_key_valtype, 88914262Swpaul sizeof(ypresp_key_val)); 89014262Swpaul 89114262Swpaul return (&result); 89214262Swpaul} 89314262Swpaul 89414304Swpaul/* 89514304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS 89614304Swpaul */ 89714262Swpaulypresponse * 89814262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp) 89914262Swpaul{ 90014262Swpaul static ypresponse result; 90114262Swpaul ypresp_master *v2_result1; 90214262Swpaul ypresp_order *v2_result2; 90314262Swpaul 90414262Swpaul result.yp_resptype = YPRESP_MAP_PARMS; 90514262Swpaul result.ypresponse_u.yp_resp_map_parmstype.domain = 90614262Swpaul argp->yprequest_u.yp_req_nokeytype.domain; 90714262Swpaul result.ypresponse_u.yp_resp_map_parmstype.map = 90814262Swpaul argp->yprequest_u.yp_req_nokeytype.map; 90914262Swpaul /* 91014262Swpaul * Hmm... there is no 'status' value in the 91114262Swpaul * yp_resp_map_parmstype structure, so I have to 91214262Swpaul * guess at what to do to indicate a failure. 91314262Swpaul * I hope this is right. 91414262Swpaul */ 91514262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0; 91614262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = ""; 91714262Swpaul 91814262Swpaul if (argp->yp_reqtype != YPREQ_MAP_PARMS) { 91914262Swpaul return(&result); 92014262Swpaul } 92114262Swpaul 92214262Swpaul v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype, 92314262Swpaul rqstp); 92414262Swpaul if (v2_result1 == NULL) 92514262Swpaul return(NULL); 92614262Swpaul 92714262Swpaul if (v2_result1->stat != YP_TRUE) { 92814262Swpaul return(&result); 92914262Swpaul } 93014262Swpaul 93114262Swpaul v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype, 93214262Swpaul rqstp); 93314262Swpaul if (v2_result2 == NULL) 93414262Swpaul return(NULL); 93514262Swpaul 93614262Swpaul if (v2_result2->stat != YP_TRUE) { 93714262Swpaul return(&result); 93814262Swpaul } 93914262Swpaul 94014262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = 94114262Swpaul v2_result1->peer; 94214262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 94314262Swpaul v2_result2->ordernum; 94414262Swpaul 94514262Swpaul return (&result); 94614262Swpaul} 94714262Swpaul 94814262Swpaulypresponse * 94914262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp) 95014262Swpaul{ 95114262Swpaul static ypresponse result; 95214262Swpaul 95314262Swpaul /* 95414262Swpaul * Not implemented. 95514262Swpaul */ 95614262Swpaul 95714262Swpaul return (&result); 95814262Swpaul} 95914262Swpaul 96014262Swpaulypresponse * 96114262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp) 96214262Swpaul{ 96314262Swpaul static ypresponse result; 96414262Swpaul 96514262Swpaul /* 96614262Swpaul * Not implemented. 96714262Swpaul */ 96814262Swpaul 96914262Swpaul return (&result); 97014262Swpaul} 97114262Swpaul 97214262Swpaulypresponse * 97314262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp) 97414262Swpaul{ 97514262Swpaul static ypresponse result; 97614262Swpaul 97714262Swpaul /* 97814262Swpaul * Not implemented. 97914262Swpaul */ 98014262Swpaul 98114262Swpaul return (&result); 98214262Swpaul} 983