yp_server.c revision 20100
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 3412891Swpaul#include "yp_extern.h" 3512891Swpaul#include "yp.h" 3612891Swpaul#include <stdlib.h> 3712891Swpaul#include <dirent.h> 3812891Swpaul#include <sys/stat.h> 3912891Swpaul#include <sys/param.h> 4012891Swpaul#include <errno.h> 4112891Swpaul#include <sys/types.h> 4212891Swpaul#include <sys/socket.h> 4312891Swpaul#include <netinet/in.h> 4412891Swpaul#include <arpa/inet.h> 4512997Swpaul#include <rpc/rpc.h> 4612891Swpaul 4712891Swpaul#ifndef lint 4820100Swpaulstatic const char rcsid[] = "$Id: yp_server.c,v 1.12 1996/10/24 18:58:26 wpaul Exp $"; 4912891Swpaul#endif /* not lint */ 5012891Swpaul 5112891Swpaulint forked = 0; 5212891Swpaulint children = 0; 5319161Swpaulstatic DB *spec_dbp = NULL; /* Special global DB handle for ypproc_all. */ 5419161Swpaulstatic char *master_string = "YP_MASTER_NAME"; 5519161Swpaulstatic char *order_string = "YP_LAST_MODIFIED"; 5619161Swpaulstatic int master_sz = sizeof("YP_MASTER_NAME") - 1; 5719161Swpaulstatic int order_sz = sizeof("YP_LAST_MODIFIED") - 1; 5812891Swpaul 5914262Swpaul/* 6014262Swpaul * NIS v2 support. This is where most of the action happens. 6114262Swpaul */ 6214262Swpaul 6312891Swpaulvoid * 6412891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp) 6512891Swpaul{ 6612891Swpaul static char * result; 6712891Swpaul static char rval = 0; 6812891Swpaul 6919161Swpaul#ifdef DB_CACHE 7019161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 7119161Swpaul#else 7212891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 7319161Swpaul#endif 7412891Swpaul return(NULL); 7512891Swpaul 7612891Swpaul result = &rval; 7712891Swpaul 7812891Swpaul return((void *) &result); 7912891Swpaul} 8012891Swpaul 8112891Swpaulbool_t * 8212891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp) 8312891Swpaul{ 8412891Swpaul static bool_t result; 8512891Swpaul 8619161Swpaul#ifdef DB_CACHE 8719161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) { 8819161Swpaul#else 8912891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 9019161Swpaul#endif 9112891Swpaul result = FALSE; 9212891Swpaul return (&result); 9312891Swpaul } 9412891Swpaul 9512891Swpaul if (argp == NULL || yp_validdomain(*argp)) 9612891Swpaul result = FALSE; 9712891Swpaul else 9812891Swpaul result = TRUE; 9912891Swpaul 10012891Swpaul return (&result); 10112891Swpaul} 10212891Swpaul 10312891Swpaulbool_t * 10412891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp) 10512891Swpaul{ 10612891Swpaul static bool_t result; 10712891Swpaul 10819161Swpaul#ifdef DB_CACHE 10919161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 11019161Swpaul#else 11112891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 11219161Swpaul#endif 11312891Swpaul return (NULL); 11412891Swpaul 11512891Swpaul if (argp == NULL || yp_validdomain(*argp)) 11612891Swpaul return (NULL); 11712891Swpaul else 11812891Swpaul result = TRUE; 11912891Swpaul 12012891Swpaul return (&result); 12112891Swpaul} 12212891Swpaul 12312891Swpaulypresp_val * 12412891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp) 12512891Swpaul{ 12612891Swpaul static ypresp_val result; 12712891Swpaul DBT key, data; 12812891Swpaul 12914304Swpaul result.val.valdat_val = ""; 13014304Swpaul result.val.valdat_len = 0; 13119161Swpaul 13219161Swpaul#ifdef DB_CACHE 13319161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 13419161Swpaul#else 13512891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 13619161Swpaul#endif 13712891Swpaul result.stat = YP_YPERR; 13812891Swpaul return (&result); 13912891Swpaul } 14012891Swpaul 14112891Swpaul if (argp->domain == NULL || argp->map == NULL) { 14212891Swpaul result.stat = YP_BADARGS; 14312891Swpaul return (&result); 14412891Swpaul } 14512891Swpaul 14612891Swpaul key.size = argp->key.keydat_len; 14712891Swpaul key.data = argp->key.keydat_val; 14812891Swpaul 14915426Swpaul if ((result.stat = yp_get_record(argp->domain, argp->map, 15015426Swpaul &key, &data, 1)) == YP_TRUE) { 15112891Swpaul result.val.valdat_len = data.size; 15212891Swpaul result.val.valdat_val = data.data; 15312891Swpaul } 15412891Swpaul 15512891Swpaul /* 15612891Swpaul * Do DNS lookups for hosts maps if database lookup failed. 15712891Swpaul */ 15812891Swpaul 15919161Swpaul#ifdef DB_CACHE 16019161Swpaul if (result.stat != YP_TRUE && 16119161Swpaul (yp_testflag(argp->map, argp->domain, YP_INTERDOMAIN) || 16219161Swpaul (strstr(argp->map, "hosts") && do_dns))) { 16319161Swpaul#else 16412891Swpaul if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) { 16519161Swpaul#endif 16612997Swpaul char *rval = NULL; 16712891Swpaul 16812891Swpaul /* DNS lookups can take time -- do them in a subprocess */ 16912891Swpaul 17012891Swpaul if (!debug && children < MAX_CHILDREN && fork()) { 17112891Swpaul children++; 17212891Swpaul forked = 0; 17312891Swpaul /* 17412891Swpaul * Returning NULL here prevents svc_sendreply() 17512891Swpaul * from being called by the parent. This is vital 17612891Swpaul * since having both the parent and the child process 17712891Swpaul * call it would confuse the client. 17812891Swpaul */ 17912891Swpaul return (NULL); 18012891Swpaul } else { 18112891Swpaul forked++; 18212891Swpaul } 18312891Swpaul 18412891Swpaul if (debug) 18512891Swpaul yp_error("Doing DNS lookup of %.*s", 18612891Swpaul argp->key.keydat_len, 18712891Swpaul argp->key.keydat_val); 18812891Swpaul 18912891Swpaul /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */ 19012891Swpaul argp->key.keydat_val[argp->key.keydat_len] = '\0'; 19112891Swpaul 19212891Swpaul if (!strcmp(argp->map, "hosts.byname")) 19312891Swpaul rval = yp_dnsname((char *)argp->key.keydat_val); 19412891Swpaul else if (!strcmp(argp->map, "hosts.byaddr")) 19512891Swpaul rval = yp_dnsaddr((const char *)argp->key.keydat_val); 19612891Swpaul 19712891Swpaul 19812891Swpaul if (rval) { 19912891Swpaul if (debug) 20014304Swpaul yp_error("DNS lookup successful. Result: %s", 20114304Swpaul rval); 20212891Swpaul result.val.valdat_len = strlen(rval); 20312891Swpaul result.val.valdat_val = rval; 20412891Swpaul result.stat = YP_TRUE; 20512891Swpaul } else { 20612891Swpaul if (debug) 20712891Swpaul yp_error("DNS lookup failed."); 20812891Swpaul result.stat = YP_NOKEY; 20912891Swpaul } 21012891Swpaul } 21112891Swpaul 21212891Swpaul return (&result); 21312891Swpaul} 21412891Swpaul 21512891Swpaulypresp_key_val * 21612891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 21712891Swpaul{ 21812891Swpaul static ypresp_key_val result; 21912891Swpaul DBT key, data; 22012891Swpaul DB *dbp; 22112891Swpaul 22214304Swpaul result.val.valdat_val = result.key.keydat_val = ""; 22314304Swpaul result.val.valdat_len = result.key.keydat_len = 0; 22419161Swpaul 22519161Swpaul#ifdef DB_CACHE 22619161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 22719161Swpaul#else 22812891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 22919161Swpaul#endif 23012891Swpaul result.stat = YP_YPERR; 23112891Swpaul return (&result); 23212891Swpaul } 23312891Swpaul 23412891Swpaul if (argp->domain == NULL) { 23512891Swpaul result.stat = YP_BADARGS; 23612891Swpaul return (&result); 23712891Swpaul } 23812891Swpaul 23915426Swpaul#ifdef DB_CACHE 24015426Swpaul if ((dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) { 24115426Swpaul#else 24212891Swpaul if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 24315426Swpaul#endif 24412891Swpaul result.stat = yp_errno; 24512891Swpaul return(&result); 24612891Swpaul } 24712891Swpaul 24812891Swpaul key.data = NULL; 24912891Swpaul key.size = 0; 25012891Swpaul 25115426Swpaul if ((result.stat = yp_first_record(dbp, &key, &data, 0)) == YP_TRUE) { 25212891Swpaul result.key.keydat_len = key.size; 25312891Swpaul result.key.keydat_val = key.data; 25412891Swpaul result.val.valdat_len = data.size; 25512891Swpaul result.val.valdat_val = data.data; 25612891Swpaul } 25715426Swpaul#ifndef DB_CACHE 25815426Swpaul (void)(dbp->close)(dbp); 25915426Swpaul#endif 26012891Swpaul return (&result); 26112891Swpaul} 26212891Swpaul 26312891Swpaulypresp_key_val * 26412891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp) 26512891Swpaul{ 26612891Swpaul static ypresp_key_val result; 26712891Swpaul DBT key, data; 26812891Swpaul DB *dbp; 26912891Swpaul 27014304Swpaul result.val.valdat_val = result.key.keydat_val = ""; 27114304Swpaul result.val.valdat_len = result.key.keydat_len = 0; 27215426Swpaul 27319161Swpaul#ifdef DB_CACHE 27419161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 27519161Swpaul#else 27612891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 27719161Swpaul#endif 27812891Swpaul result.stat = YP_YPERR; 27912891Swpaul return (&result); 28012891Swpaul } 28112891Swpaul 28212891Swpaul if (argp->domain == NULL || argp->map == NULL) { 28312891Swpaul result.stat = YP_BADARGS; 28412891Swpaul return (&result); 28512891Swpaul } 28612891Swpaul 28715426Swpaul#ifdef DB_CACHE 28815426Swpaul if ((dbp = yp_open_db_cache(argp->domain, argp->map, 28915426Swpaul argp->key.keydat_val, 29015426Swpaul argp->key.keydat_len)) == NULL) { 29115426Swpaul#else 29212891Swpaul if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 29315426Swpaul#endif 29412891Swpaul result.stat = yp_errno; 29512891Swpaul return(&result); 29612891Swpaul } 29712891Swpaul 29812891Swpaul key.size = argp->key.keydat_len; 29912891Swpaul key.data = argp->key.keydat_val; 30012891Swpaul 30115426Swpaul if ((result.stat = yp_next_record(dbp, &key, &data,0,0)) == YP_TRUE) { 30212891Swpaul result.key.keydat_len = key.size; 30312891Swpaul result.key.keydat_val = key.data; 30412891Swpaul result.val.valdat_len = data.size; 30512891Swpaul result.val.valdat_val = data.data; 30612891Swpaul } 30715426Swpaul#ifndef DB_CACHE 30815426Swpaul (void)(dbp->close)(dbp); 30915426Swpaul#endif 31012891Swpaul return (&result); 31112891Swpaul} 31212891Swpaul 31312997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port) 31412997Swpaul ypxfrstat rval; 31512997Swpaul struct sockaddr_in *addr; 31612997Swpaul unsigned int transid; 31712997Swpaul unsigned int prognum; 31812997Swpaul unsigned long port; 31912997Swpaul{ 32012997Swpaul CLIENT *clnt; 32112997Swpaul int sock = RPC_ANYSOCK; 32212997Swpaul struct timeval timeout; 32312997Swpaul yppushresp_xfr ypxfr_resp; 32413375Swpaul struct rpc_err err; 32512997Swpaul 32613375Swpaul timeout.tv_sec = 5; 32712997Swpaul timeout.tv_usec = 0; 32812997Swpaul addr->sin_port = htons(port); 32912997Swpaul 33019131Swpaul if ((clnt = clntudp_create(addr,prognum,1,timeout,&sock)) == NULL) { 33119131Swpaul yp_error("%s: %s", inet_ntoa(addr->sin_addr), 33219161Swpaul clnt_spcreateerror("failed to establish callback handle")); 33319131Swpaul return; 33419131Swpaul } 33512997Swpaul 33612997Swpaul ypxfr_resp.status = rval; 33712997Swpaul ypxfr_resp.transid = transid; 33812997Swpaul 33913375Swpaul /* Turn the timeout off -- we don't want to block. */ 34013375Swpaul timeout.tv_sec = 0; 34113375Swpaul if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE) 34213375Swpaul yp_error("failed to set timeout on ypproc_xfr callback"); 34312997Swpaul 34413375Swpaul if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) { 34513375Swpaul clnt_geterr(clnt, &err); 34613375Swpaul if (err.re_status != RPC_SUCCESS && 34713375Swpaul err.re_status != RPC_TIMEDOUT) 34813375Swpaul yp_error("%s", clnt_sperror(clnt, 34913375Swpaul "ypxfr callback failed")); 35013375Swpaul } 35113375Swpaul 35212997Swpaul clnt_destroy(clnt); 35312997Swpaul return; 35412997Swpaul} 35512997Swpaul 35615426Swpaul#define YPXFR_RETURN(CODE) \ 35715426Swpaul /* Order is important: send regular RPC reply, then callback */ \ 35815426Swpaul result.xfrstat = CODE; \ 35915426Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); \ 36015426Swpaul ypxfr_callback(CODE,rqhost,argp->transid, \ 36115426Swpaul argp->prog,argp->port); \ 36215426Swpaul return(NULL); 36315426Swpaul 36412891Swpaulypresp_xfr * 36512891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp) 36612891Swpaul{ 36712891Swpaul static ypresp_xfr result; 36812997Swpaul struct sockaddr_in *rqhost; 36912891Swpaul 37013375Swpaul result.transid = argp->transid; 37113375Swpaul rqhost = svc_getcaller(rqstp->rq_xprt); 37213375Swpaul 37319161Swpaul#ifdef DB_CACHE 37419161Swpaul if (yp_access(argp->map_parms.map, 37519161Swpaul argp->map_parms.domain, (struct svc_req *)rqstp)) { 37619161Swpaul#else 37712891Swpaul if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) { 37819161Swpaul#endif 37915426Swpaul YPXFR_RETURN(YPXFR_REFUSED); 38012891Swpaul } 38112891Swpaul 38212891Swpaul if (argp->map_parms.domain == NULL) { 38315426Swpaul YPXFR_RETURN(YPXFR_BADARGS); 38412891Swpaul } 38512891Swpaul 38612891Swpaul if (yp_validdomain(argp->map_parms.domain)) { 38715426Swpaul YPXFR_RETURN(YPXFR_NODOM); 38812891Swpaul } 38912891Swpaul 39012891Swpaul switch(fork()) { 39112891Swpaul case 0: 39212891Swpaul { 39312891Swpaul char g[11], t[11], p[11]; 39412891Swpaul char ypxfr_command[MAXPATHLEN + 2]; 39512891Swpaul 39612891Swpaul sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC); 39712891Swpaul sprintf (t, "%u", argp->transid); 39812891Swpaul sprintf (g, "%u", argp->prog); 39912891Swpaul sprintf (p, "%u", argp->port); 40012997Swpaul if (debug) 40112997Swpaul close(0); close(1); close(2); 40212997Swpaul if (strcmp(yp_dir, _PATH_YP)) { 40314304Swpaul execl(ypxfr_command, "ypxfr", 40414304Swpaul "-d", argp->map_parms.domain, 40514304Swpaul "-h", argp->map_parms.peer, 40614304Swpaul "-p", yp_dir, "-C", t, 40714304Swpaul g, inet_ntoa(rqhost->sin_addr), 40814304Swpaul p, argp->map_parms.map, 40912997Swpaul NULL); 41012997Swpaul } else { 41114304Swpaul execl(ypxfr_command, "ypxfr", 41214304Swpaul "-d", argp->map_parms.domain, 41314304Swpaul "-h", argp->map_parms.peer, 41414304Swpaul "-C", t, 41514304Swpaul g, inet_ntoa(rqhost->sin_addr), 41614304Swpaul p, argp->map_parms.map, 41712997Swpaul NULL); 41812997Swpaul } 41912997Swpaul forked++; 42015426Swpaul yp_error("ypxfr execl(%s): %s", ypxfr_command, strerror(errno)); 42115426Swpaul YPXFR_RETURN(YPXFR_XFRERR); 42212997Swpaul break; 42312891Swpaul } 42412891Swpaul case -1: 42512891Swpaul yp_error("ypxfr fork(): %s", strerror(errno)); 42615426Swpaul YPXFR_RETURN(YPXFR_XFRERR); 42712891Swpaul break; 42812891Swpaul default: 42913375Swpaul result.xfrstat = YPXFR_SUCC; 43012997Swpaul children++; 43112997Swpaul forked = 0; 43212891Swpaul break; 43312891Swpaul } 43413375Swpaul 43513375Swpaul return (&result); 43612891Swpaul} 43715426Swpaul#undef YPXFR_RETURN 43812891Swpaul 43912891Swpaulvoid * 44012891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp) 44112891Swpaul{ 44212891Swpaul static char * result; 44312891Swpaul static char rval = 0; 44412891Swpaul 44519161Swpaul#ifdef DB_CACHE 44619161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) 44719161Swpaul#else 44812891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 44919161Swpaul#endif 45012891Swpaul return (NULL); 45115426Swpaul#ifdef DB_CACHE 45215426Swpaul /* clear out the database cache */ 45315426Swpaul yp_flush_all(); 45415426Swpaul#endif 45514240Swpaul /* Re-read the securenets database for the hell of it. */ 45614240Swpaul load_securenets(); 45714240Swpaul 45812891Swpaul result = &rval; 45912891Swpaul return((void *) &result); 46012891Swpaul} 46112891Swpaul 46212891Swpaul/* 46312891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures 46412891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol 46512891Swpaul * definition file only serializes one such structure. This means that 46612891Swpaul * to send the whole stream, you need a wrapper which feeds all the 46712891Swpaul * records into the underlying XDR routine until it hits an 'EOF.' 46812891Swpaul * But to use the wrapper, you have to violate the boundaries between 46912891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all 47012891Swpaul * service routine instead of letting the RPC dispatcher do it. 47112891Swpaul * 47212891Swpaul * Bleah. 47312891Swpaul */ 47412891Swpaul 47512891Swpaul/* 47620100Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep 47720100Swpaul * reading from the database and spew until we run out of records 47820100Swpaul * or encounter an error. 47912891Swpaul */ 48012891Swpaulstatic bool_t 48112891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp) 48212891Swpaul{ 48320100Swpaul DBT key = { NULL, 0 } , data = { NULL, 0 }; 48420100Swpaul 48520100Swpaul while (1) { 48620100Swpaul /* Get a record. */ 48720100Swpaul if ((objp->ypresp_all_u.val.stat = 48820100Swpaul yp_next_record(spec_dbp,&key,&data,1,0)) == YP_TRUE) { 48920100Swpaul objp->ypresp_all_u.val.val.valdat_len = data.size; 49020100Swpaul objp->ypresp_all_u.val.val.valdat_val = data.data; 49120100Swpaul objp->ypresp_all_u.val.key.keydat_len = key.size; 49220100Swpaul objp->ypresp_all_u.val.key.keydat_val = key.data; 49320100Swpaul objp->more = TRUE; 49420100Swpaul } else { 49520100Swpaul objp->more = FALSE; 49620100Swpaul } 49720100Swpaul 49820100Swpaul /* Serialize. */ 49920100Swpaul if (!xdr_ypresp_all(xdrs, objp)) 50020100Swpaul return(FALSE); 50120100Swpaul if (objp->more == FALSE) 50220100Swpaul return(TRUE); 50320100Swpaul } 50412891Swpaul} 50512891Swpaul 50612891Swpaulypresp_all * 50712891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 50812891Swpaul{ 50912891Swpaul static ypresp_all result; 51012891Swpaul 51112891Swpaul /* 51212891Swpaul * Set this here so that the client will be forced to make 51312891Swpaul * at least one attempt to read from us even if all we're 51412891Swpaul * doing is returning an error. 51512891Swpaul */ 51612891Swpaul result.more = TRUE; 51714304Swpaul result.ypresp_all_u.val.key.keydat_len = 0; 51814304Swpaul result.ypresp_all_u.val.key.keydat_val = ""; 51912891Swpaul 52019161Swpaul#ifdef DB_CACHE 52119161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 52219161Swpaul#else 52312891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 52419161Swpaul#endif 52512891Swpaul result.ypresp_all_u.val.stat = YP_YPERR; 52612891Swpaul return (&result); 52712891Swpaul } 52812891Swpaul 52912891Swpaul if (argp->domain == NULL || argp->map == NULL) { 53012891Swpaul result.ypresp_all_u.val.stat = YP_BADARGS; 53112891Swpaul return (&result); 53212891Swpaul } 53312891Swpaul 53420100Swpaul /* 53520100Swpaul * The ypproc_all procedure can take a while to complete. 53620100Swpaul * Best to handle it in a subprocess so the parent doesn't 53720100Swpaul * block. (Is there a better way to do this? Maybe with 53820100Swpaul * async socket I/O?) 53920100Swpaul */ 54020100Swpaul if (!debug && children < MAX_CHILDREN && fork()) { 54120100Swpaul children++; 54220100Swpaul forked = 0; 54320100Swpaul return (NULL); 54420100Swpaul } else { 54520100Swpaul forked++; 54620100Swpaul } 54720100Swpaul 54820100Swpaul#ifndef DB_CACHE 54912891Swpaul if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 55012891Swpaul result.ypresp_all_u.val.stat = yp_errno; 55112891Swpaul return(&result); 55212891Swpaul } 55320100Swpaul#else 55420100Swpaul if ((spec_dbp = yp_open_db_cache(argp->domain, argp->map, NULL, 0)) == NULL) { 55520100Swpaul result.ypresp_all_u.val.stat = yp_errno; 55620100Swpaul return(&result); 55720100Swpaul } 55820100Swpaul#endif 55912891Swpaul 56012891Swpaul /* Kick off the actual data transfer. */ 56120100Swpaul svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result); 56220100Swpaul 56320100Swpaul#ifndef DB_CACHE 56420100Swpaul (void)(spec_dbp->close)(spec_dbp); 56520100Swpaul#endif 56612891Swpaul /* 56712891Swpaul * Returning NULL prevents the dispatcher from calling 56812891Swpaul * svc_sendreply() since we already did it. 56912891Swpaul */ 57012891Swpaul return (NULL); 57112891Swpaul} 57212891Swpaul 57312891Swpaulypresp_master * 57412891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 57512891Swpaul{ 57612891Swpaul static ypresp_master result; 57715426Swpaul static char ypvalbuf[YPMAXRECORD]; 57819161Swpaul DBT key = { master_string, master_sz }, data; 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 59615426Swpaul /* 59715426Swpaul * Note that we copy the data retrieved from the database to 59815426Swpaul * a private buffer and NUL terminate the buffer rather than 59915426Swpaul * terminating the data in place. We do this because by stuffing 60015426Swpaul * a '\0' into data.data, we will actually be corrupting memory 60115426Swpaul * allocated by the DB package. This is a bad thing now that we 60215426Swpaul * cache DB handles rather than closing the database immediately. 60315426Swpaul */ 60415426Swpaul if ((result.stat = yp_get_record(argp->domain, argp->map, 60515426Swpaul &key, &data, 1)) == YP_TRUE) { 60615426Swpaul bcopy((char *)data.data, (char *)&ypvalbuf, data.size); 60715426Swpaul ypvalbuf[data.size] = '\0'; 60815426Swpaul result.peer = (char *)&ypvalbuf; 60912891Swpaul } else 61012891Swpaul result.peer = ""; 61112891Swpaul 61212891Swpaul return (&result); 61312891Swpaul} 61412891Swpaul 61512891Swpaulypresp_order * 61612891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 61712891Swpaul{ 61812891Swpaul static ypresp_order result; 61919161Swpaul DBT key = { order_string, order_sz }, data; 62012891Swpaul 62114304Swpaul result.ordernum = 0; 62214304Swpaul 62319161Swpaul#ifdef DB_CACHE 62419161Swpaul if (yp_access(argp->map, argp->domain, (struct svc_req *)rqstp)) { 62519161Swpaul#else 62619161Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 62719161Swpaul#endif 62812891Swpaul result.stat = YP_YPERR; 62912891Swpaul return(&result); 63012891Swpaul } 63112891Swpaul 63212891Swpaul if (argp->domain == NULL) { 63312891Swpaul result.stat = YP_BADARGS; 63412891Swpaul return (&result); 63512891Swpaul } 63612891Swpaul 63712891Swpaul /* 63812891Swpaul * We could just check the timestamp on the map file, 63912891Swpaul * but that's a hack: we'll only know the last time the file 64012891Swpaul * was touched, not the last time the database contents were 64112891Swpaul * updated. 64212891Swpaul */ 64312891Swpaul 64415426Swpaul if ((result.stat = yp_get_record(argp->domain, argp->map, 64515426Swpaul &key, &data, 1)) == YP_TRUE) 64612891Swpaul result.ordernum = atoi((char *)data.data); 64712891Swpaul else 64812891Swpaul result.ordernum = 0; 64912891Swpaul 65015426Swpaul 65112891Swpaul return (&result); 65212891Swpaul} 65312891Swpaul 65412891Swpaulstatic void yp_maplist_free(yp_maplist) 65512891Swpaul struct ypmaplist *yp_maplist; 65612891Swpaul{ 65712891Swpaul register struct ypmaplist *next; 65812891Swpaul 65912891Swpaul while(yp_maplist) { 66012891Swpaul next = yp_maplist->next; 66112891Swpaul free(yp_maplist->map); 66212891Swpaul free(yp_maplist); 66312891Swpaul yp_maplist = next; 66412891Swpaul } 66512891Swpaul return; 66612891Swpaul} 66712891Swpaul 66812891Swpaulstatic struct ypmaplist *yp_maplist_create(domain) 66912891Swpaul const char *domain; 67012891Swpaul{ 67112891Swpaul char yp_mapdir[MAXPATHLEN + 2]; 67212891Swpaul char yp_mapname[MAXPATHLEN + 2]; 67312891Swpaul struct ypmaplist *cur = NULL; 67412891Swpaul struct ypmaplist *yp_maplist = NULL; 67512891Swpaul DIR *dird; 67612891Swpaul struct dirent *dirp; 67712891Swpaul struct stat statbuf; 67812891Swpaul 67912891Swpaul snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain); 68012891Swpaul 68112891Swpaul if ((dird = opendir(yp_mapdir)) == NULL) { 68213800Swpaul yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno)); 68312891Swpaul return(NULL); 68412891Swpaul } 68512891Swpaul 68612891Swpaul while ((dirp = readdir(dird)) != NULL) { 68712891Swpaul if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) { 68814304Swpaul snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s", 68914304Swpaul yp_mapdir,dirp->d_name); 69014304Swpaul if (stat(yp_mapname, &statbuf) < 0 || 69114304Swpaul !S_ISREG(statbuf.st_mode)) 69212891Swpaul continue; 69314304Swpaul if ((cur = (struct ypmaplist *) 69416044Swpaul malloc(sizeof(struct ypmaplist))) == NULL) { 69514304Swpaul yp_error("malloc() failed: %s",strerror(errno)); 69612891Swpaul closedir(dird); 69712891Swpaul yp_maplist_free(yp_maplist); 69812891Swpaul return(NULL); 69912891Swpaul } 70012891Swpaul if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) { 70114304Swpaul yp_error("strdup() failed: %s",strerror(errno)); 70212891Swpaul closedir(dird); 70312891Swpaul yp_maplist_free(yp_maplist); 70412891Swpaul return(NULL); 70512891Swpaul } 70612891Swpaul cur->next = yp_maplist; 70712891Swpaul yp_maplist = cur; 70812891Swpaul if (debug) 70912891Swpaul yp_error("map: %s", yp_maplist->map); 71012891Swpaul } 71112891Swpaul 71212891Swpaul } 71312891Swpaul closedir(dird); 71412891Swpaul return(yp_maplist); 71512891Swpaul} 71612891Swpaul 71712891Swpaulypresp_maplist * 71812891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp) 71912891Swpaul{ 72015426Swpaul static ypresp_maplist result = { 0, NULL }; 72112891Swpaul 72219161Swpaul#ifdef DB_CACHE 72319161Swpaul if (yp_access(NULL, NULL, (struct svc_req *)rqstp)) { 72419161Swpaul#else 72512891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 72619161Swpaul#endif 72712891Swpaul result.stat = YP_YPERR; 72812891Swpaul return(&result); 72912891Swpaul } 73012891Swpaul 73112891Swpaul if (argp == NULL) { 73212891Swpaul result.stat = YP_BADARGS; 73312891Swpaul return (&result); 73412891Swpaul } 73512891Swpaul 73612891Swpaul if (yp_validdomain(*argp)) { 73712891Swpaul result.stat = YP_NODOM; 73812891Swpaul return (&result); 73912891Swpaul } 74012891Swpaul 74112891Swpaul /* 74212891Swpaul * We have to construct a linked list for the ypproc_maplist 74312891Swpaul * procedure using dynamically allocated memory. Since the XDR 74412891Swpaul * layer won't free this list for us, we have to deal with it 74512891Swpaul * ourselves. We call yp_maplist_free() first to free any 74612891Swpaul * previously allocated data we may have accumulated to insure 74712891Swpaul * that we have only one linked list in memory at any given 74812891Swpaul * time. 74912891Swpaul */ 75012891Swpaul 75112891Swpaul yp_maplist_free(result.maps); 75212891Swpaul 75312891Swpaul if ((result.maps = yp_maplist_create(*argp)) == NULL) { 75412891Swpaul yp_error("yp_maplist_create failed"); 75512891Swpaul result.stat = YP_YPERR; 75612891Swpaul return(&result); 75712891Swpaul } else 75812891Swpaul result.stat = YP_TRUE; 75912891Swpaul 76012891Swpaul return (&result); 76112891Swpaul} 76214262Swpaul 76314262Swpaul/* 76414262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack 76514262Swpaul * functions from v1 are identical to those in v2, so all 76614262Swpaul * we have to do is hand off to them. 76714262Swpaul * 76814262Swpaul * The other functions are mostly just wrappers around their v2 76914262Swpaul * counterparts. For example, for the v1 'match' procedure, we 77014262Swpaul * crack open the argument structure, make a request to the v2 77114262Swpaul * 'match' function, repackage the data into a v1 response and 77214262Swpaul * then send it on its way. 77314262Swpaul * 77414262Swpaul * Note that we don't support the pull, push and get procedures. 77514262Swpaul * There's little documentation available to show what they 77614262Swpaul * do, and I suspect they're meant largely for map transfers 77714262Swpaul * between master and slave servers. 77814262Swpaul */ 77914262Swpaul 78014262Swpaulvoid * 78114262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp) 78214262Swpaul{ 78314262Swpaul return(ypproc_null_2_svc(argp, rqstp)); 78414262Swpaul} 78514262Swpaul 78614262Swpaulbool_t * 78714262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp) 78814262Swpaul{ 78914262Swpaul return(ypproc_domain_2_svc(argp, rqstp)); 79014262Swpaul} 79114262Swpaul 79214262Swpaulbool_t * 79314262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp) 79414262Swpaul{ 79514262Swpaul return (ypproc_domain_nonack_2_svc(argp, rqstp)); 79614262Swpaul} 79714262Swpaul 79814304Swpaul/* 79914304Swpaul * the 'match' procedure sends a response of type YPRESP_VAL 80014304Swpaul */ 80114262Swpaulypresponse * 80214262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp) 80314262Swpaul{ 80414262Swpaul static ypresponse result; 80514262Swpaul ypresp_val *v2_result; 80614262Swpaul 80714262Swpaul result.yp_resptype = YPRESP_VAL; 80814304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_val = ""; 80914304Swpaul result.ypresponse_u.yp_resp_valtype.val.valdat_len = 0; 81014262Swpaul 81114262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 81214262Swpaul result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS; 81314262Swpaul return(&result); 81414262Swpaul } 81514262Swpaul 81614262Swpaul v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 81714262Swpaul if (v2_result == NULL) 81814262Swpaul return(NULL); 81914262Swpaul 82014262Swpaul bcopy((char *)v2_result, 82114262Swpaul (char *)&result.ypresponse_u.yp_resp_valtype, 82214262Swpaul sizeof(ypresp_val)); 82314262Swpaul 82414262Swpaul return (&result); 82514262Swpaul} 82614262Swpaul 82714304Swpaul/* 82814304Swpaul * the 'first' procedure sends a response of type YPRESP_KEY_VAL 82914304Swpaul */ 83014262Swpaulypresponse * 83114262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp) 83214262Swpaul{ 83314262Swpaul static ypresponse result; 83414262Swpaul ypresp_key_val *v2_result; 83514262Swpaul 83614262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 83714304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 83814304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 83914304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 84014304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 84114262Swpaul 84214262Swpaul if (argp->yp_reqtype != YPREQ_NOKEY) { 84314262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 84414262Swpaul return(&result); 84514262Swpaul } 84614262Swpaul 84714262Swpaul v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype, 84814262Swpaul rqstp); 84914262Swpaul if (v2_result == NULL) 85014262Swpaul return(NULL); 85114262Swpaul 85214262Swpaul bcopy((char *)v2_result, 85314262Swpaul (char *)&result.ypresponse_u.yp_resp_key_valtype, 85414262Swpaul sizeof(ypresp_key_val)); 85514262Swpaul 85614262Swpaul return (&result); 85714262Swpaul} 85814262Swpaul 85914304Swpaul/* 86014304Swpaul * the 'next' procedure sends a response of type YPRESP_KEY_VAL 86114304Swpaul */ 86214262Swpaulypresponse * 86314262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp) 86414262Swpaul{ 86514262Swpaul static ypresponse result; 86614262Swpaul ypresp_key_val *v2_result; 86714262Swpaul 86814262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 86914304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_val = 87014304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_val = ""; 87114304Swpaul result.ypresponse_u.yp_resp_key_valtype.val.valdat_len = 87214304Swpaul result.ypresponse_u.yp_resp_key_valtype.key.keydat_len = 0; 87314262Swpaul 87414262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 87514262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 87614262Swpaul return(&result); 87714262Swpaul } 87814262Swpaul 87914262Swpaul v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 88014262Swpaul if (v2_result == NULL) 88114262Swpaul return(NULL); 88214262Swpaul 88314262Swpaul bcopy((char *)v2_result, 88414262Swpaul (char *)&result.ypresponse_u.yp_resp_key_valtype, 88514262Swpaul sizeof(ypresp_key_val)); 88614262Swpaul 88714262Swpaul return (&result); 88814262Swpaul} 88914262Swpaul 89014304Swpaul/* 89114304Swpaul * the 'poll' procedure sends a response of type YPRESP_MAP_PARMS 89214304Swpaul */ 89314262Swpaulypresponse * 89414262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp) 89514262Swpaul{ 89614262Swpaul static ypresponse result; 89714262Swpaul ypresp_master *v2_result1; 89814262Swpaul ypresp_order *v2_result2; 89914262Swpaul 90014262Swpaul result.yp_resptype = YPRESP_MAP_PARMS; 90114262Swpaul result.ypresponse_u.yp_resp_map_parmstype.domain = 90214262Swpaul argp->yprequest_u.yp_req_nokeytype.domain; 90314262Swpaul result.ypresponse_u.yp_resp_map_parmstype.map = 90414262Swpaul argp->yprequest_u.yp_req_nokeytype.map; 90514262Swpaul /* 90614262Swpaul * Hmm... there is no 'status' value in the 90714262Swpaul * yp_resp_map_parmstype structure, so I have to 90814262Swpaul * guess at what to do to indicate a failure. 90914262Swpaul * I hope this is right. 91014262Swpaul */ 91114262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0; 91214262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = ""; 91314262Swpaul 91414262Swpaul if (argp->yp_reqtype != YPREQ_MAP_PARMS) { 91514262Swpaul return(&result); 91614262Swpaul } 91714262Swpaul 91814262Swpaul v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype, 91914262Swpaul rqstp); 92014262Swpaul if (v2_result1 == NULL) 92114262Swpaul return(NULL); 92214262Swpaul 92314262Swpaul if (v2_result1->stat != YP_TRUE) { 92414262Swpaul return(&result); 92514262Swpaul } 92614262Swpaul 92714262Swpaul v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype, 92814262Swpaul rqstp); 92914262Swpaul if (v2_result2 == NULL) 93014262Swpaul return(NULL); 93114262Swpaul 93214262Swpaul if (v2_result2->stat != YP_TRUE) { 93314262Swpaul return(&result); 93414262Swpaul } 93514262Swpaul 93614262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = 93714262Swpaul v2_result1->peer; 93814262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 93914262Swpaul v2_result2->ordernum; 94014262Swpaul 94114262Swpaul return (&result); 94214262Swpaul} 94314262Swpaul 94414262Swpaulypresponse * 94514262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp) 94614262Swpaul{ 94714262Swpaul static ypresponse result; 94814262Swpaul 94914262Swpaul /* 95014262Swpaul * Not implemented. 95114262Swpaul */ 95214262Swpaul 95314262Swpaul return (&result); 95414262Swpaul} 95514262Swpaul 95614262Swpaulypresponse * 95714262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp) 95814262Swpaul{ 95914262Swpaul static ypresponse result; 96014262Swpaul 96114262Swpaul /* 96214262Swpaul * Not implemented. 96314262Swpaul */ 96414262Swpaul 96514262Swpaul return (&result); 96614262Swpaul} 96714262Swpaul 96814262Swpaulypresponse * 96914262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp) 97014262Swpaul{ 97114262Swpaul static ypresponse result; 97214262Swpaul 97314262Swpaul /* 97414262Swpaul * Not implemented. 97514262Swpaul */ 97614262Swpaul 97714262Swpaul return (&result); 97814262Swpaul} 979