yp_server.c revision 14262
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 4814262Swpaulstatic char rcsid[] = "$Id: yp_server.c,v 1.3 1996/02/26 02:23:39 wpaul Exp $"; 4912891Swpaul#endif /* not lint */ 5012891Swpaul 5112891Swpaulint forked = 0; 5212891Swpaulint children = 0; 5312891SwpaulDB *spec_dbp = NULL; /* Special global DB handle for ypproc_all. */ 5412891Swpaul 5514262Swpaul/* 5614262Swpaul * NIS v2 support. This is where most of the action happens. 5714262Swpaul */ 5814262Swpaul 5912891Swpaulvoid * 6012891Swpaulypproc_null_2_svc(void *argp, struct svc_req *rqstp) 6112891Swpaul{ 6212891Swpaul static char * result; 6312891Swpaul static char rval = 0; 6412891Swpaul 6512891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 6612891Swpaul return(NULL); 6712891Swpaul 6812891Swpaul result = &rval; 6912891Swpaul 7012891Swpaul return((void *) &result); 7112891Swpaul} 7212891Swpaul 7312891Swpaulbool_t * 7412891Swpaulypproc_domain_2_svc(domainname *argp, struct svc_req *rqstp) 7512891Swpaul{ 7612891Swpaul static bool_t result; 7712891Swpaul 7812891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 7912891Swpaul result = FALSE; 8012891Swpaul return (&result); 8112891Swpaul } 8212891Swpaul 8312891Swpaul if (argp == NULL || yp_validdomain(*argp)) 8412891Swpaul result = FALSE; 8512891Swpaul else 8612891Swpaul result = TRUE; 8712891Swpaul 8812891Swpaul return (&result); 8912891Swpaul} 9012891Swpaul 9112891Swpaulbool_t * 9212891Swpaulypproc_domain_nonack_2_svc(domainname *argp, struct svc_req *rqstp) 9312891Swpaul{ 9412891Swpaul static bool_t result; 9512891Swpaul 9612891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 9712891Swpaul return (NULL); 9812891Swpaul 9912891Swpaul if (argp == NULL || yp_validdomain(*argp)) 10012891Swpaul return (NULL); 10112891Swpaul else 10212891Swpaul result = TRUE; 10312891Swpaul 10412891Swpaul return (&result); 10512891Swpaul} 10612891Swpaul 10712891Swpaulypresp_val * 10812891Swpaulypproc_match_2_svc(ypreq_key *argp, struct svc_req *rqstp) 10912891Swpaul{ 11012891Swpaul static ypresp_val result; 11112891Swpaul DBT key, data; 11212891Swpaul 11312891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 11412891Swpaul result.stat = YP_YPERR; 11512891Swpaul return (&result); 11612891Swpaul } 11712891Swpaul 11812891Swpaul if (argp->domain == NULL || argp->map == NULL) { 11912891Swpaul result.stat = YP_BADARGS; 12012891Swpaul return (&result); 12112891Swpaul } 12212891Swpaul 12312891Swpaul if (yp_validdomain(argp->domain)) { 12412891Swpaul result.stat = YP_NODOM; 12512891Swpaul return(&result); 12612891Swpaul } 12712891Swpaul 12812891Swpaul key.size = argp->key.keydat_len; 12912891Swpaul key.data = argp->key.keydat_val; 13012891Swpaul 13112891Swpaul result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 0); 13212891Swpaul 13312891Swpaul if (result.stat == YP_TRUE) { 13412891Swpaul result.val.valdat_len = data.size; 13512891Swpaul result.val.valdat_val = data.data; 13612891Swpaul } 13712891Swpaul 13812891Swpaul /* 13912891Swpaul * Do DNS lookups for hosts maps if database lookup failed. 14012891Swpaul */ 14112891Swpaul 14212891Swpaul if (do_dns && result.stat != YP_TRUE && strstr(argp->map, "hosts")) { 14312997Swpaul char *rval = NULL; 14412891Swpaul 14512891Swpaul /* DNS lookups can take time -- do them in a subprocess */ 14612891Swpaul 14712891Swpaul if (!debug && children < MAX_CHILDREN && fork()) { 14812891Swpaul children++; 14912891Swpaul forked = 0; 15012891Swpaul /* 15112891Swpaul * Returning NULL here prevents svc_sendreply() 15212891Swpaul * from being called by the parent. This is vital 15312891Swpaul * since having both the parent and the child process 15412891Swpaul * call it would confuse the client. 15512891Swpaul */ 15612891Swpaul return (NULL); 15712891Swpaul } else { 15812891Swpaul forked++; 15912891Swpaul } 16012891Swpaul 16112891Swpaul if (debug) 16212891Swpaul yp_error("Doing DNS lookup of %.*s", 16312891Swpaul argp->key.keydat_len, 16412891Swpaul argp->key.keydat_val); 16512891Swpaul 16612891Swpaul /* NUL terminate! NUL terminate!! NUL TERMINATE!!! */ 16712891Swpaul argp->key.keydat_val[argp->key.keydat_len] = '\0'; 16812891Swpaul 16912891Swpaul if (!strcmp(argp->map, "hosts.byname")) 17012891Swpaul rval = yp_dnsname((char *)argp->key.keydat_val); 17112891Swpaul else if (!strcmp(argp->map, "hosts.byaddr")) 17212891Swpaul rval = yp_dnsaddr((const char *)argp->key.keydat_val); 17312891Swpaul 17412891Swpaul 17512891Swpaul if (rval) { 17612891Swpaul if (debug) 17712891Swpaul yp_error("DNS lookup successful. Result: %s", rval); 17812891Swpaul result.val.valdat_len = strlen(rval); 17912891Swpaul result.val.valdat_val = rval; 18012891Swpaul result.stat = YP_TRUE; 18112891Swpaul } else { 18212891Swpaul if (debug) 18312891Swpaul yp_error("DNS lookup failed."); 18412891Swpaul result.stat = YP_NOKEY; 18512891Swpaul } 18612891Swpaul } 18712891Swpaul 18812891Swpaul return (&result); 18912891Swpaul} 19012891Swpaul 19112891Swpaulypresp_key_val * 19212891Swpaulypproc_first_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 19312891Swpaul{ 19412891Swpaul static ypresp_key_val result; 19512891Swpaul DBT key, data; 19612891Swpaul DB *dbp; 19712891Swpaul 19812891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 19912891Swpaul result.stat = YP_YPERR; 20012891Swpaul return (&result); 20112891Swpaul } 20212891Swpaul 20312891Swpaul if (argp->domain == NULL) { 20412891Swpaul result.stat = YP_BADARGS; 20512891Swpaul return (&result); 20612891Swpaul } 20712891Swpaul 20812891Swpaul if (yp_validdomain(argp->domain)) { 20912891Swpaul result.stat = YP_NODOM; 21012891Swpaul return(&result); 21112891Swpaul } 21212891Swpaul 21312891Swpaul if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 21412891Swpaul result.stat = yp_errno; 21512891Swpaul return(&result); 21612891Swpaul } 21712891Swpaul 21812891Swpaul key.data = NULL; 21912891Swpaul key.size = 0; 22012891Swpaul result.stat = yp_first_record(dbp, &key, &data); 22112891Swpaul (void)(dbp->close)(dbp); 22212891Swpaul 22312891Swpaul if (result.stat == YP_TRUE) { 22412891Swpaul result.key.keydat_len = key.size; 22512891Swpaul result.key.keydat_val = key.data; 22612891Swpaul result.val.valdat_len = data.size; 22712891Swpaul result.val.valdat_val = data.data; 22812891Swpaul } 22912891Swpaul 23012891Swpaul return (&result); 23112891Swpaul} 23212891Swpaul 23312891Swpaulypresp_key_val * 23412891Swpaulypproc_next_2_svc(ypreq_key *argp, struct svc_req *rqstp) 23512891Swpaul{ 23612891Swpaul static ypresp_key_val result; 23712891Swpaul DBT key, data; 23812891Swpaul DB *dbp; 23912891Swpaul 24012891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 24112891Swpaul result.stat = YP_YPERR; 24212891Swpaul return (&result); 24312891Swpaul } 24412891Swpaul 24512891Swpaul if (argp->domain == NULL || argp->map == NULL) { 24612891Swpaul result.stat = YP_BADARGS; 24712891Swpaul return (&result); 24812891Swpaul } 24912891Swpaul 25012891Swpaul if (yp_validdomain(argp->domain)) { 25112891Swpaul result.stat = YP_NODOM; 25212891Swpaul return(&result); 25312891Swpaul } 25412891Swpaul 25512891Swpaul if ((dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 25612891Swpaul result.stat = yp_errno; 25712891Swpaul return(&result); 25812891Swpaul } 25912891Swpaul 26012891Swpaul key.size = argp->key.keydat_len; 26112891Swpaul key.data = argp->key.keydat_val; 26212891Swpaul 26312891Swpaul result.stat = yp_next_record(dbp, &key, &data, 0); 26412891Swpaul (void)(dbp->close)(dbp); 26512891Swpaul 26612891Swpaul if (result.stat == YP_TRUE) { 26712891Swpaul result.key.keydat_len = key.size; 26812891Swpaul result.key.keydat_val = key.data; 26912891Swpaul result.val.valdat_len = data.size; 27012891Swpaul result.val.valdat_val = data.data; 27112891Swpaul } 27212891Swpaul 27312891Swpaul return (&result); 27412891Swpaul} 27512891Swpaul 27612997Swpaulstatic void ypxfr_callback(rval,addr,transid,prognum,port) 27712997Swpaul ypxfrstat rval; 27812997Swpaul struct sockaddr_in *addr; 27912997Swpaul unsigned int transid; 28012997Swpaul unsigned int prognum; 28112997Swpaul unsigned long port; 28212997Swpaul{ 28312997Swpaul CLIENT *clnt; 28412997Swpaul int sock = RPC_ANYSOCK; 28512997Swpaul struct timeval timeout; 28612997Swpaul yppushresp_xfr ypxfr_resp; 28713375Swpaul struct rpc_err err; 28812997Swpaul 28913375Swpaul timeout.tv_sec = 5; 29012997Swpaul timeout.tv_usec = 0; 29112997Swpaul addr->sin_port = htons(port); 29212997Swpaul 29312997Swpaul if ((clnt = clntudp_create(addr, prognum, 1, timeout, &sock)) == NULL) 29412997Swpaul yp_error("%s", clnt_spcreateerror("failed to establish \ 29512997Swpaulcallback handle")); 29612997Swpaul 29712997Swpaul ypxfr_resp.status = rval; 29812997Swpaul ypxfr_resp.transid = transid; 29912997Swpaul 30013375Swpaul /* Turn the timeout off -- we don't want to block. */ 30113375Swpaul timeout.tv_sec = 0; 30213375Swpaul if (clnt_control(clnt, CLSET_TIMEOUT, (char *)&timeout) == FALSE) 30313375Swpaul yp_error("failed to set timeout on ypproc_xfr callback"); 30412997Swpaul 30513375Swpaul if (yppushproc_xfrresp_1(&ypxfr_resp, clnt) == NULL) { 30613375Swpaul clnt_geterr(clnt, &err); 30713375Swpaul if (err.re_status != RPC_SUCCESS && 30813375Swpaul err.re_status != RPC_TIMEDOUT) 30913375Swpaul yp_error("%s", clnt_sperror(clnt, 31013375Swpaul "ypxfr callback failed")); 31113375Swpaul } 31213375Swpaul 31312997Swpaul clnt_destroy(clnt); 31412997Swpaul return; 31512997Swpaul} 31612997Swpaul 31712891Swpaulypresp_xfr * 31812891Swpaulypproc_xfr_2_svc(ypreq_xfr *argp, struct svc_req *rqstp) 31912891Swpaul{ 32012891Swpaul static ypresp_xfr result; 32112997Swpaul struct sockaddr_in *rqhost; 32212891Swpaul 32313375Swpaul result.transid = argp->transid; 32413375Swpaul rqhost = svc_getcaller(rqstp->rq_xprt); 32513375Swpaul 32612891Swpaul if (yp_access(argp->map_parms.map, (struct svc_req *)rqstp)) { 32713375Swpaul /* Order is important: send regular RPC reply, then callback */ 32812891Swpaul result.xfrstat = YPXFR_REFUSED; 32913375Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); 33013375Swpaul ypxfr_callback(YPXFR_REFUSED,rqhost,argp->transid, 33113375Swpaul argp->prog,argp->port); 33213375Swpaul return(NULL); 33312891Swpaul } 33412891Swpaul 33512891Swpaul if (argp->map_parms.domain == NULL) { 33612891Swpaul result.xfrstat = YPXFR_BADARGS; 33713375Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); 33813375Swpaul ypxfr_callback(YPXFR_BADARGS,rqhost,argp->transid, 33913375Swpaul argp->prog,argp->port); 34013375Swpaul return(NULL); 34112891Swpaul } 34212891Swpaul 34312891Swpaul if (yp_validdomain(argp->map_parms.domain)) { 34412891Swpaul result.xfrstat = YPXFR_NODOM; 34513375Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); 34613375Swpaul ypxfr_callback(YPXFR_NODOM,rqhost,argp->transid, 34713375Swpaul argp->prog,argp->port); 34813375Swpaul return(NULL); 34912891Swpaul } 35012891Swpaul 35112891Swpaul switch(fork()) { 35212891Swpaul case 0: 35312891Swpaul { 35412891Swpaul char g[11], t[11], p[11]; 35512891Swpaul char ypxfr_command[MAXPATHLEN + 2]; 35612891Swpaul 35712891Swpaul sprintf (ypxfr_command, "%sypxfr", _PATH_LIBEXEC); 35812891Swpaul sprintf (t, "%u", argp->transid); 35912891Swpaul sprintf (g, "%u", argp->prog); 36012891Swpaul sprintf (p, "%u", argp->port); 36112997Swpaul if (debug) 36212997Swpaul close(0); close(1); close(2); 36312997Swpaul if (strcmp(yp_dir, _PATH_YP)) { 36412997Swpaul execl(ypxfr_command, "ypxfr", "-d", argp->map_parms.domain, 36513375Swpaul "-h", argp->map_parms.peer, "-p", yp_dir, "-C", t, 36612997Swpaul g, inet_ntoa(rqhost->sin_addr), p, argp->map_parms.map, 36712997Swpaul NULL); 36812997Swpaul } else { 36912997Swpaul execl(ypxfr_command, "ypxfr", "-d", argp->map_parms.domain, 37013375Swpaul "-h", argp->map_parms.peer, "-C", t, g, 37112997Swpaul inet_ntoa(rqhost->sin_addr), p, argp->map_parms.map, 37212997Swpaul NULL); 37312997Swpaul } 37412997Swpaul forked++; 37513375Swpaul result.xfrstat = YPXFR_XFRERR; 37612891Swpaul yp_error("ypxfr execl(): %s", strerror(errno)); 37713375Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); 37812997Swpaul ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid, 37912997Swpaul argp->prog,argp->port); 38013375Swpaul return(NULL); 38112997Swpaul break; 38212891Swpaul } 38312891Swpaul case -1: 38412891Swpaul yp_error("ypxfr fork(): %s", strerror(errno)); 38513375Swpaul result.xfrstat = YPXFR_XFRERR; 38613375Swpaul svc_sendreply(rqstp->rq_xprt, xdr_ypresp_xfr, (char *)&result); 38712997Swpaul ypxfr_callback(YPXFR_XFRERR,rqhost,argp->transid, 38812997Swpaul argp->prog,argp->port); 38913375Swpaul return(NULL); 39012891Swpaul break; 39112891Swpaul default: 39213375Swpaul result.xfrstat = YPXFR_SUCC; 39312997Swpaul children++; 39412997Swpaul forked = 0; 39512891Swpaul break; 39612891Swpaul } 39713375Swpaul 39813375Swpaul return (&result); 39912891Swpaul} 40012891Swpaul 40112891Swpaulvoid * 40212891Swpaulypproc_clear_2_svc(void *argp, struct svc_req *rqstp) 40312891Swpaul{ 40412891Swpaul static char * result; 40512891Swpaul static char rval = 0; 40612891Swpaul 40712891Swpaul /* 40812891Swpaul * We don't have to do anything for ypproc_clear. Unlike 40912891Swpaul * the SunOS ypserv, we don't hold out database descriptors 41012891Swpaul * open forever. 41112891Swpaul */ 41212891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) 41312891Swpaul return (NULL); 41412891Swpaul 41514240Swpaul /* Re-read the securenets database for the hell of it. */ 41614240Swpaul load_securenets(); 41714240Swpaul 41812891Swpaul result = &rval; 41912891Swpaul return((void *) &result); 42012891Swpaul} 42112891Swpaul 42212891Swpaul/* 42312891Swpaul * For ypproc_all, we have to send a stream of ypresp_all structures 42412891Swpaul * via TCP, but the XDR filter generated from the yp.x protocol 42512891Swpaul * definition file only serializes one such structure. This means that 42612891Swpaul * to send the whole stream, you need a wrapper which feeds all the 42712891Swpaul * records into the underlying XDR routine until it hits an 'EOF.' 42812891Swpaul * But to use the wrapper, you have to violate the boundaries between 42912891Swpaul * RPC layers by calling svc_sendreply() directly from the ypproc_all 43012891Swpaul * service routine instead of letting the RPC dispatcher do it. 43112891Swpaul * 43212891Swpaul * Bleah. 43312891Swpaul */ 43412891Swpaul 43512891Swpaul/* 43612891Swpaul * Custom XDR routine for serialzing results of ypproc_all: keep 43712891Swpaul * reading from the database and spew until we run out of records 43812891Swpaul * or encounter an error. 43912891Swpaul */ 44012891Swpaulstatic bool_t 44112891Swpaulxdr_my_ypresp_all(register XDR *xdrs, ypresp_all *objp) 44212891Swpaul{ 44312891Swpaul DBT key, data; 44412891Swpaul 44512891Swpaul while (1) { 44612891Swpaul /* Get a record. */ 44712891Swpaul key.size = objp->ypresp_all_u.val.key.keydat_len; 44812891Swpaul key.data = objp->ypresp_all_u.val.key.keydat_val; 44912891Swpaul 45012891Swpaul if ((objp->ypresp_all_u.val.stat = 45112891Swpaul yp_next_record(spec_dbp,&key,&data,1)) == YP_TRUE) { 45212891Swpaul objp->ypresp_all_u.val.val.valdat_len = data.size; 45312891Swpaul objp->ypresp_all_u.val.val.valdat_val = data.data; 45412891Swpaul objp->ypresp_all_u.val.key.keydat_len = key.size; 45512891Swpaul objp->ypresp_all_u.val.key.keydat_val = key.data; 45612891Swpaul objp->more = TRUE; 45712891Swpaul } else { 45812891Swpaul objp->more = FALSE; 45912891Swpaul } 46012891Swpaul 46112891Swpaul /* Serialize. */ 46212891Swpaul if (!xdr_ypresp_all(xdrs, objp)) 46312891Swpaul return(FALSE); 46412891Swpaul if (objp->more == FALSE) 46512891Swpaul return(TRUE); 46612891Swpaul } 46712891Swpaul} 46812891Swpaul 46912891Swpaulypresp_all * 47012891Swpaulypproc_all_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 47112891Swpaul{ 47212891Swpaul static ypresp_all result; 47312891Swpaul 47412891Swpaul /* 47512891Swpaul * Set this here so that the client will be forced to make 47612891Swpaul * at least one attempt to read from us even if all we're 47712891Swpaul * doing is returning an error. 47812891Swpaul */ 47912891Swpaul result.more = TRUE; 48012891Swpaul 48112891Swpaul if (yp_access(argp->map, (struct svc_req *)rqstp)) { 48212891Swpaul result.ypresp_all_u.val.stat = YP_YPERR; 48312891Swpaul return (&result); 48412891Swpaul } 48512891Swpaul 48612891Swpaul if (argp->domain == NULL || argp->map == NULL) { 48712891Swpaul result.ypresp_all_u.val.stat = YP_BADARGS; 48812891Swpaul return (&result); 48912891Swpaul } 49012891Swpaul 49112891Swpaul if (yp_validdomain(argp->domain)) { 49212891Swpaul result.ypresp_all_u.val.stat = YP_NODOM; 49312891Swpaul return(&result); 49412891Swpaul } 49512891Swpaul 49612891Swpaul /* 49712891Swpaul * The ypproc_all procedure can take a while to complete. 49812891Swpaul * Best to handle it in a subprocess so the parent doesn't 49912891Swpaul * block. We fork() here so we don't end up sharing a 50012891Swpaul * DB file handle with the parent. 50112891Swpaul */ 50212891Swpaul 50312891Swpaul if (!debug && children < MAX_CHILDREN && fork()) { 50412891Swpaul children++; 50512891Swpaul forked = 0; 50612891Swpaul return (NULL); 50712891Swpaul } else { 50812891Swpaul forked++; 50912891Swpaul } 51012891Swpaul 51112891Swpaul if ((spec_dbp = yp_open_db(argp->domain, argp->map)) == NULL) { 51212891Swpaul result.ypresp_all_u.val.stat = yp_errno; 51312891Swpaul return(&result); 51412891Swpaul } 51512891Swpaul 51612891Swpaul /* Kick off the actual data transfer. */ 51712891Swpaul svc_sendreply(rqstp->rq_xprt, xdr_my_ypresp_all, (char *)&result); 51812891Swpaul 51912891Swpaul /* Close database when done. */ 52012891Swpaul (void)(spec_dbp->close)(spec_dbp); 52112891Swpaul 52212891Swpaul /* 52312891Swpaul * Returning NULL prevents the dispatcher from calling 52412891Swpaul * svc_sendreply() since we already did it. 52512891Swpaul */ 52612891Swpaul return (NULL); 52712891Swpaul} 52812891Swpaul 52912891Swpaulypresp_master * 53012891Swpaulypproc_master_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 53112891Swpaul{ 53212891Swpaul static ypresp_master result; 53312891Swpaul DBT key,data; 53412891Swpaul 53512891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 53612891Swpaul result.stat = YP_YPERR; 53712891Swpaul return(&result); 53812891Swpaul } 53912891Swpaul 54012891Swpaul if (argp->domain == NULL) { 54112891Swpaul result.stat = YP_BADARGS; 54212891Swpaul return (&result); 54312891Swpaul } 54412891Swpaul 54512891Swpaul if (yp_validdomain(argp->domain)) { 54612891Swpaul result.stat = YP_NODOM; 54712891Swpaul return (&result); 54812891Swpaul } 54912891Swpaul 55012891Swpaul key.data = "YP_MASTER_NAME"; 55112891Swpaul key.size = sizeof("YP_MASTER_NAME") - 1; 55212891Swpaul 55312891Swpaul result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1); 55412891Swpaul 55512891Swpaul if (result.stat == YP_TRUE) { 55612891Swpaul result.peer = (char *)data.data; 55712891Swpaul result.peer[data.size] = '\0'; 55812891Swpaul } else 55912891Swpaul result.peer = ""; 56012891Swpaul 56112891Swpaul return (&result); 56212891Swpaul} 56312891Swpaul 56412891Swpaulypresp_order * 56512891Swpaulypproc_order_2_svc(ypreq_nokey *argp, struct svc_req *rqstp) 56612891Swpaul{ 56712891Swpaul static ypresp_order result; 56812891Swpaul DBT key,data; 56912891Swpaul 57012891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 57112891Swpaul result.stat = YP_YPERR; 57212891Swpaul return(&result); 57312891Swpaul } 57412891Swpaul 57512891Swpaul if (argp->domain == NULL) { 57612891Swpaul result.stat = YP_BADARGS; 57712891Swpaul return (&result); 57812891Swpaul } 57912891Swpaul 58012891Swpaul if (yp_validdomain(argp->domain)) { 58112891Swpaul result.stat = YP_NODOM; 58212891Swpaul return (&result); 58312891Swpaul } 58412891Swpaul 58512891Swpaul /* 58612891Swpaul * We could just check the timestamp on the map file, 58712891Swpaul * but that's a hack: we'll only know the last time the file 58812891Swpaul * was touched, not the last time the database contents were 58912891Swpaul * updated. 59012891Swpaul */ 59112891Swpaul key.data = "YP_LAST_MODIFIED"; 59212891Swpaul key.size = sizeof("YP_LAST_MODIFIED") - 1; 59312891Swpaul 59412891Swpaul result.stat = yp_get_record(argp->domain, argp->map, &key, &data, 1); 59512891Swpaul 59612891Swpaul if (result.stat == YP_TRUE) 59712891Swpaul result.ordernum = atoi((char *)data.data); 59812891Swpaul else 59912891Swpaul result.ordernum = 0; 60012891Swpaul 60112891Swpaul return (&result); 60212891Swpaul} 60312891Swpaul 60412891Swpaulstatic void yp_maplist_free(yp_maplist) 60512891Swpaul struct ypmaplist *yp_maplist; 60612891Swpaul{ 60712891Swpaul register struct ypmaplist *next; 60812891Swpaul 60912891Swpaul while(yp_maplist) { 61012891Swpaul next = yp_maplist->next; 61112891Swpaul free(yp_maplist->map); 61212891Swpaul free(yp_maplist); 61312891Swpaul yp_maplist = next; 61412891Swpaul } 61512891Swpaul return; 61612891Swpaul} 61712891Swpaul 61812891Swpaulstatic struct ypmaplist *yp_maplist_create(domain) 61912891Swpaul const char *domain; 62012891Swpaul{ 62112891Swpaul char yp_mapdir[MAXPATHLEN + 2]; 62212891Swpaul char yp_mapname[MAXPATHLEN + 2]; 62312891Swpaul struct ypmaplist *cur = NULL; 62412891Swpaul struct ypmaplist *yp_maplist = NULL; 62512891Swpaul DIR *dird; 62612891Swpaul struct dirent *dirp; 62712891Swpaul struct stat statbuf; 62812891Swpaul 62912891Swpaul snprintf(yp_mapdir, sizeof(yp_mapdir), "%s/%s", yp_dir, domain); 63012891Swpaul 63112891Swpaul if ((dird = opendir(yp_mapdir)) == NULL) { 63213800Swpaul yp_error("opendir(%s) failed: %s", yp_mapdir, strerror(errno)); 63312891Swpaul return(NULL); 63412891Swpaul } 63512891Swpaul 63612891Swpaul while ((dirp = readdir(dird)) != NULL) { 63712891Swpaul if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) { 63812891Swpaul snprintf(yp_mapname, sizeof(yp_mapname), "%s/%s",yp_mapdir,dirp->d_name); 63912891Swpaul if (stat(yp_mapname, &statbuf) < 0 || !S_ISREG(statbuf.st_mode)) 64012891Swpaul continue; 64112891Swpaul if ((cur = (struct ypmaplist *)malloc(sizeof(struct ypmaplist))) < 0) { 64212891Swpaul yp_error("malloc() failed: %s", strerror(errno)); 64312891Swpaul closedir(dird); 64412891Swpaul yp_maplist_free(yp_maplist); 64512891Swpaul return(NULL); 64612891Swpaul } 64712891Swpaul if ((cur->map = (char *)strdup(dirp->d_name)) == NULL) { 64812891Swpaul yp_error("strdup() failed: %s", strerror(errno)); 64912891Swpaul closedir(dird); 65012891Swpaul yp_maplist_free(yp_maplist); 65112891Swpaul return(NULL); 65212891Swpaul } 65312891Swpaul cur->next = yp_maplist; 65412891Swpaul yp_maplist = cur; 65512891Swpaul if (debug) 65612891Swpaul yp_error("map: %s", yp_maplist->map); 65712891Swpaul } 65812891Swpaul 65912891Swpaul } 66012891Swpaul closedir(dird); 66112891Swpaul return(yp_maplist); 66212891Swpaul} 66312891Swpaul 66412891Swpaulypresp_maplist * 66512891Swpaulypproc_maplist_2_svc(domainname *argp, struct svc_req *rqstp) 66612891Swpaul{ 66712891Swpaul static ypresp_maplist result; 66812891Swpaul 66912891Swpaul if (yp_access(NULL, (struct svc_req *)rqstp)) { 67012891Swpaul result.stat = YP_YPERR; 67112891Swpaul return(&result); 67212891Swpaul } 67312891Swpaul 67412891Swpaul if (argp == NULL) { 67512891Swpaul result.stat = YP_BADARGS; 67612891Swpaul return (&result); 67712891Swpaul } 67812891Swpaul 67912891Swpaul if (yp_validdomain(*argp)) { 68012891Swpaul result.stat = YP_NODOM; 68112891Swpaul return (&result); 68212891Swpaul } 68312891Swpaul 68412891Swpaul /* 68512891Swpaul * We have to construct a linked list for the ypproc_maplist 68612891Swpaul * procedure using dynamically allocated memory. Since the XDR 68712891Swpaul * layer won't free this list for us, we have to deal with it 68812891Swpaul * ourselves. We call yp_maplist_free() first to free any 68912891Swpaul * previously allocated data we may have accumulated to insure 69012891Swpaul * that we have only one linked list in memory at any given 69112891Swpaul * time. 69212891Swpaul */ 69312891Swpaul 69412891Swpaul yp_maplist_free(result.maps); 69512891Swpaul 69612891Swpaul if ((result.maps = yp_maplist_create(*argp)) == NULL) { 69712891Swpaul yp_error("yp_maplist_create failed"); 69812891Swpaul result.stat = YP_YPERR; 69912891Swpaul return(&result); 70012891Swpaul } else 70112891Swpaul result.stat = YP_TRUE; 70212891Swpaul 70312891Swpaul return (&result); 70412891Swpaul} 70514262Swpaul 70614262Swpaul/* 70714262Swpaul * NIS v1 support. The nullproc, domain and domain_nonack 70814262Swpaul * functions from v1 are identical to those in v2, so all 70914262Swpaul * we have to do is hand off to them. 71014262Swpaul * 71114262Swpaul * The other functions are mostly just wrappers around their v2 71214262Swpaul * counterparts. For example, for the v1 'match' procedure, we 71314262Swpaul * crack open the argument structure, make a request to the v2 71414262Swpaul * 'match' function, repackage the data into a v1 response and 71514262Swpaul * then send it on its way. 71614262Swpaul * 71714262Swpaul * Note that we don't support the pull, push and get procedures. 71814262Swpaul * There's little documentation available to show what they 71914262Swpaul * do, and I suspect they're meant largely for map transfers 72014262Swpaul * between master and slave servers. 72114262Swpaul */ 72214262Swpaul 72314262Swpaulvoid * 72414262Swpaulypoldproc_null_1_svc(void *argp, struct svc_req *rqstp) 72514262Swpaul{ 72614262Swpaul return(ypproc_null_2_svc(argp, rqstp)); 72714262Swpaul} 72814262Swpaul 72914262Swpaulbool_t * 73014262Swpaulypoldproc_domain_1_svc(domainname *argp, struct svc_req *rqstp) 73114262Swpaul{ 73214262Swpaul return(ypproc_domain_2_svc(argp, rqstp)); 73314262Swpaul} 73414262Swpaul 73514262Swpaulbool_t * 73614262Swpaulypoldproc_domain_nonack_1_svc(domainname *argp, struct svc_req *rqstp) 73714262Swpaul{ 73814262Swpaul return (ypproc_domain_nonack_2_svc(argp, rqstp)); 73914262Swpaul} 74014262Swpaul 74114262Swpaulypresponse * 74214262Swpaulypoldproc_match_1_svc(yprequest *argp, struct svc_req *rqstp) 74314262Swpaul{ 74414262Swpaul static ypresponse result; 74514262Swpaul ypresp_val *v2_result; 74614262Swpaul 74714262Swpaul result.yp_resptype = YPRESP_VAL; 74814262Swpaul 74914262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 75014262Swpaul result.ypresponse_u.yp_resp_valtype.stat = YP_BADARGS; 75114262Swpaul return(&result); 75214262Swpaul } 75314262Swpaul 75414262Swpaul v2_result = ypproc_match_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 75514262Swpaul if (v2_result == NULL) 75614262Swpaul return(NULL); 75714262Swpaul 75814262Swpaul bcopy((char *)v2_result, 75914262Swpaul (char *)&result.ypresponse_u.yp_resp_valtype, 76014262Swpaul sizeof(ypresp_val)); 76114262Swpaul 76214262Swpaul return (&result); 76314262Swpaul} 76414262Swpaul 76514262Swpaulypresponse * 76614262Swpaulypoldproc_first_1_svc(yprequest *argp, struct svc_req *rqstp) 76714262Swpaul{ 76814262Swpaul static ypresponse result; 76914262Swpaul ypresp_key_val *v2_result; 77014262Swpaul 77114262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 77214262Swpaul 77314262Swpaul if (argp->yp_reqtype != YPREQ_NOKEY) { 77414262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 77514262Swpaul return(&result); 77614262Swpaul } 77714262Swpaul 77814262Swpaul v2_result = ypproc_first_2_svc(&argp->yprequest_u.yp_req_nokeytype, 77914262Swpaul rqstp); 78014262Swpaul if (v2_result == NULL) 78114262Swpaul return(NULL); 78214262Swpaul 78314262Swpaul bcopy((char *)v2_result, 78414262Swpaul (char *)&result.ypresponse_u.yp_resp_key_valtype, 78514262Swpaul sizeof(ypresp_key_val)); 78614262Swpaul 78714262Swpaul return (&result); 78814262Swpaul} 78914262Swpaul 79014262Swpaulypresponse * 79114262Swpaulypoldproc_next_1_svc(yprequest *argp, struct svc_req *rqstp) 79214262Swpaul{ 79314262Swpaul static ypresponse result; 79414262Swpaul ypresp_key_val *v2_result; 79514262Swpaul 79614262Swpaul result.yp_resptype = YPRESP_KEY_VAL; 79714262Swpaul 79814262Swpaul if (argp->yp_reqtype != YPREQ_KEY) { 79914262Swpaul result.ypresponse_u.yp_resp_key_valtype.stat = YP_BADARGS; 80014262Swpaul return(&result); 80114262Swpaul } 80214262Swpaul 80314262Swpaul v2_result = ypproc_next_2_svc(&argp->yprequest_u.yp_req_keytype,rqstp); 80414262Swpaul if (v2_result == NULL) 80514262Swpaul return(NULL); 80614262Swpaul 80714262Swpaul bcopy((char *)v2_result, 80814262Swpaul (char *)&result.ypresponse_u.yp_resp_key_valtype, 80914262Swpaul sizeof(ypresp_key_val)); 81014262Swpaul 81114262Swpaul return (&result); 81214262Swpaul} 81314262Swpaul 81414262Swpaulypresponse * 81514262Swpaulypoldproc_poll_1_svc(yprequest *argp, struct svc_req *rqstp) 81614262Swpaul{ 81714262Swpaul static ypresponse result; 81814262Swpaul ypresp_master *v2_result1; 81914262Swpaul ypresp_order *v2_result2; 82014262Swpaul 82114262Swpaul result.yp_resptype = YPRESP_MAP_PARMS; 82214262Swpaul result.ypresponse_u.yp_resp_map_parmstype.domain = 82314262Swpaul argp->yprequest_u.yp_req_nokeytype.domain; 82414262Swpaul result.ypresponse_u.yp_resp_map_parmstype.map = 82514262Swpaul argp->yprequest_u.yp_req_nokeytype.map; 82614262Swpaul /* 82714262Swpaul * Hmm... there is no 'status' value in the 82814262Swpaul * yp_resp_map_parmstype structure, so I have to 82914262Swpaul * guess at what to do to indicate a failure. 83014262Swpaul * I hope this is right. 83114262Swpaul */ 83214262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 0; 83314262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = ""; 83414262Swpaul 83514262Swpaul if (argp->yp_reqtype != YPREQ_MAP_PARMS) { 83614262Swpaul return(&result); 83714262Swpaul } 83814262Swpaul 83914262Swpaul v2_result1 = ypproc_master_2_svc(&argp->yprequest_u.yp_req_nokeytype, 84014262Swpaul rqstp); 84114262Swpaul if (v2_result1 == NULL) 84214262Swpaul return(NULL); 84314262Swpaul 84414262Swpaul if (v2_result1->stat != YP_TRUE) { 84514262Swpaul return(&result); 84614262Swpaul } 84714262Swpaul 84814262Swpaul v2_result2 = ypproc_order_2_svc(&argp->yprequest_u.yp_req_nokeytype, 84914262Swpaul rqstp); 85014262Swpaul if (v2_result2 == NULL) 85114262Swpaul return(NULL); 85214262Swpaul 85314262Swpaul if (v2_result2->stat != YP_TRUE) { 85414262Swpaul return(&result); 85514262Swpaul } 85614262Swpaul 85714262Swpaul result.ypresponse_u.yp_resp_map_parmstype.peer = 85814262Swpaul v2_result1->peer; 85914262Swpaul result.ypresponse_u.yp_resp_map_parmstype.ordernum = 86014262Swpaul v2_result2->ordernum; 86114262Swpaul 86214262Swpaul return (&result); 86314262Swpaul} 86414262Swpaul 86514262Swpaulypresponse * 86614262Swpaulypoldproc_push_1_svc(yprequest *argp, struct svc_req *rqstp) 86714262Swpaul{ 86814262Swpaul static ypresponse result; 86914262Swpaul 87014262Swpaul /* 87114262Swpaul * Not implemented. 87214262Swpaul */ 87314262Swpaul 87414262Swpaul return (&result); 87514262Swpaul} 87614262Swpaul 87714262Swpaulypresponse * 87814262Swpaulypoldproc_pull_1_svc(yprequest *argp, struct svc_req *rqstp) 87914262Swpaul{ 88014262Swpaul static ypresponse result; 88114262Swpaul 88214262Swpaul /* 88314262Swpaul * Not implemented. 88414262Swpaul */ 88514262Swpaul 88614262Swpaul return (&result); 88714262Swpaul} 88814262Swpaul 88914262Swpaulypresponse * 89014262Swpaulypoldproc_get_1_svc(yprequest *argp, struct svc_req *rqstp) 89114262Swpaul{ 89214262Swpaul static ypresponse result; 89314262Swpaul 89414262Swpaul /* 89514262Swpaul * Not implemented. 89614262Swpaul */ 89714262Swpaul 89814262Swpaul return (&result); 89914262Swpaul} 900