xref: /illumos-gate/usr/src/cmd/ypcmd/ypxfrd_server.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23c458f227Ssdussud  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26c458f227Ssdussud /* Portions Copyright 2005 Juergen Keil */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <signal.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <ndbm.h>
347c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
357c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
387c478bd9Sstevel@tonic-gate #include <syslog.h>
397c478bd9Sstevel@tonic-gate #include "ypxfrd.h"
407c478bd9Sstevel@tonic-gate #include "ypsym.h"
417c478bd9Sstevel@tonic-gate #include "ypdefs.h"
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Because this code hacks into DBM underneath its API it can't use the N2L
447c478bd9Sstevel@tonic-gate  * shim in it's normal way. It thus includes shim.h instead of shim_hooks.h
457c478bd9Sstevel@tonic-gate  * and has knowledge of shim internals. While copying the DBM files it does
467c478bd9Sstevel@tonic-gate  * not lock them. This reflects the behavior of the pre N2L code.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #include "shim.h"
497c478bd9Sstevel@tonic-gate #include "yptol.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if (defined(vax) || defined(i386))
527c478bd9Sstevel@tonic-gate #define	DOSWAB 1
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate USE_YP_SECURE
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* per connection stuff */
587c478bd9Sstevel@tonic-gate struct mycon {
597c478bd9Sstevel@tonic-gate 	map_ctrl *map;
607c478bd9Sstevel@tonic-gate 	int	lblk;
617c478bd9Sstevel@tonic-gate 	int	firstd;
627c478bd9Sstevel@tonic-gate 	datum	key;
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate bool_t xdr_myfyl(XDR *xdrs, struct mycon *objp);
667c478bd9Sstevel@tonic-gate bool_t xdr_pages(XDR *xdrs, struct mycon *m);
677c478bd9Sstevel@tonic-gate bool_t xdr_dirs(XDR *xdrs, struct mycon *m);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int mygetdir(char *block, int *no, struct mycon *m);
707c478bd9Sstevel@tonic-gate int mygetpage(char *block, int *pageno, struct mycon *m);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate datum mydbm_topkey(DBM *db, datum okey);
737c478bd9Sstevel@tonic-gate datum dbm_do_nextkey();
747c478bd9Sstevel@tonic-gate datum shim_dbm_do_nextkey();
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate extern void get_secure_nets(char *);
777c478bd9Sstevel@tonic-gate extern int check_secure_net_ti(struct netbuf *, char *);
787c478bd9Sstevel@tonic-gate extern int _main(int, char **);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)817c478bd9Sstevel@tonic-gate main(int argc, char **argv)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	int connmaxrec = RPC_MAXDATASIZE;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/* load up the securenet file */
867c478bd9Sstevel@tonic-gate 	get_secure_nets(argv[0]);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * Set non-blocking mode and maximum record size for
907c478bd9Sstevel@tonic-gate 	 * connection oriented RPC transports.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
937c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO|LOG_DAEMON,
947c478bd9Sstevel@tonic-gate 			"unable to set maximum RPC record size");
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* Initialize file locking etc. */
987c478bd9Sstevel@tonic-gate 	if (!init_lock_system(TRUE))
997c478bd9Sstevel@tonic-gate 		/* An detailed error will already have been logged */
1007c478bd9Sstevel@tonic-gate 		exit(-1);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	return (_main(argc, argv));
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * In yptol mode we may start a cache update thread within a child process.
1077c478bd9Sstevel@tonic-gate  * It is thus important that child processes do not exit, killing any such
1087c478bd9Sstevel@tonic-gate  * threads, before the thread has completed. They must thus call this version
1097c478bd9Sstevel@tonic-gate  * of the exit() function.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate void
yptol_exit(int status)1127c478bd9Sstevel@tonic-gate yptol_exit(int status)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	if (yptol_mode) {
1157c478bd9Sstevel@tonic-gate 		thr_join(0, NULL, NULL);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	exit(status);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate dbmfyl *
getdbm_1_svc(hosereq * argp,struct svc_req * rqstp)1217c478bd9Sstevel@tonic-gate getdbm_1_svc(hosereq *argp, struct svc_req *rqstp)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	static dbmfyl  result;
1247c478bd9Sstevel@tonic-gate 	char path[MAXNAMLEN + 1];
1257c478bd9Sstevel@tonic-gate 	SVCXPRT *xprt;
1267c478bd9Sstevel@tonic-gate 	int pid;
1277c478bd9Sstevel@tonic-gate 	int res;
1287c478bd9Sstevel@tonic-gate 	struct mycon m;
1297c478bd9Sstevel@tonic-gate 	char *ypname = "ypxfrd";
1307c478bd9Sstevel@tonic-gate 	struct netbuf *nbuf;
1317c478bd9Sstevel@tonic-gate 	sa_family_t af;
1327c478bd9Sstevel@tonic-gate 	in_port_t port;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	xprt = rqstp->rq_xprt;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	signal(SIGPIPE, SIG_IGN);
1377c478bd9Sstevel@tonic-gate 	signal(SIGCHLD, SIG_IGN);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * Build up path name. If we are working in N2L mode also conv
1417c478bd9Sstevel@tonic-gate 	 * to the new N2L style mapname.
1427c478bd9Sstevel@tonic-gate 	 *
1437c478bd9Sstevel@tonic-gate 	 * Do not allow any path as a domain name or map name.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if ((strchr(argp->domain, '/') != NULL) ||
1467c478bd9Sstevel@tonic-gate 		(strchr(argp->map, '/') != NULL) ||
1477c478bd9Sstevel@tonic-gate 		(!ypmkfilename(argp->domain, argp->map, (char *)&path))) {
1487c478bd9Sstevel@tonic-gate 		res = GETDBM_ERROR;
1497c478bd9Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1507c478bd9Sstevel@tonic-gate 					(caddr_t)&res)) {
1517c478bd9Sstevel@tonic-gate 			svcerr_systemerr(rqstp->rq_xprt);
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		return (NULL);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	pid = fork1();
1577c478bd9Sstevel@tonic-gate 	if (pid < 0) {
1587c478bd9Sstevel@tonic-gate 		perror("fork");
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		res = GETDBM_ERROR;
1617c478bd9Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1627c478bd9Sstevel@tonic-gate 					(caddr_t)&res)) {
1637c478bd9Sstevel@tonic-gate 			svcerr_systemerr(rqstp->rq_xprt);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		return (NULL);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	if (pid != 0)
1687c478bd9Sstevel@tonic-gate 		return (NULL);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	m.map = (map_ctrl *)shim_dbm_open(path, 0, 0);
1717c478bd9Sstevel@tonic-gate 	if (m.map == NULL) {
1727c478bd9Sstevel@tonic-gate 		perror(path);
1737c478bd9Sstevel@tonic-gate 		res = GETDBM_ERROR;
1747c478bd9Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1757c478bd9Sstevel@tonic-gate 					(caddr_t)&res)) {
1767c478bd9Sstevel@tonic-gate 		    svcerr_systemerr(rqstp->rq_xprt);
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		yptol_exit(0);
1797c478bd9Sstevel@tonic-gate 		return (NULL);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Do the security thing */
1837c478bd9Sstevel@tonic-gate 	if ((nbuf = svc_getrpccaller(xprt)) == 0) {
1847c478bd9Sstevel@tonic-gate 		res = GETDBM_ERROR;
1857c478bd9Sstevel@tonic-gate 		if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
1867c478bd9Sstevel@tonic-gate 			svcerr_systemerr(xprt);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		shim_dbm_close((DBM *)m.map);
1897c478bd9Sstevel@tonic-gate 		yptol_exit(0);
1907c478bd9Sstevel@tonic-gate 		return (NULL);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (!check_secure_net_ti(nbuf, ypname)) {
1937c478bd9Sstevel@tonic-gate 		res = GETDBM_ERROR;
1947c478bd9Sstevel@tonic-gate 		if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
1957c478bd9Sstevel@tonic-gate 			svcerr_systemerr(xprt);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		shim_dbm_close((DBM *)m.map);
1987c478bd9Sstevel@tonic-gate 		yptol_exit(1);
1997c478bd9Sstevel@tonic-gate 		return (NULL);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
2037c478bd9Sstevel@tonic-gate 	port = (af == AF_INET6) ?
2047c478bd9Sstevel@tonic-gate 		((struct sockaddr_in6 *)nbuf->buf)->sin6_port :
2057c478bd9Sstevel@tonic-gate 		((struct sockaddr_in  *)nbuf->buf)->sin_port;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if ((af == AF_INET || af == AF_INET6) &&
2087c478bd9Sstevel@tonic-gate 		(ntohs(port) > IPPORT_RESERVED)) {
2097c478bd9Sstevel@tonic-gate 		datum key, val;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
2127c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
2137c478bd9Sstevel@tonic-gate 		val = shim_dbm_fetch((DBM *)m.map, key);
2147c478bd9Sstevel@tonic-gate 		if (val.dptr != NULL) {
2157c478bd9Sstevel@tonic-gate 			res = GETDBM_ERROR;
2167c478bd9Sstevel@tonic-gate 			if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
2177c478bd9Sstevel@tonic-gate 				svcerr_systemerr(xprt);
2187c478bd9Sstevel@tonic-gate 			}
2197c478bd9Sstevel@tonic-gate 			shim_dbm_close((DBM *)m.map);
2207c478bd9Sstevel@tonic-gate 			yptol_exit(1);
2217c478bd9Sstevel@tonic-gate 			return (NULL);
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/* OK, we're through */
2267c478bd9Sstevel@tonic-gate 	m.key = shim_dbm_firstkey((DBM *)m.map);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	m.lblk = -1;
2297c478bd9Sstevel@tonic-gate 	m.firstd = 0;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (!svc_sendreply(rqstp->rq_xprt, xdr_myfyl, (caddr_t)&m)) {
2327c478bd9Sstevel@tonic-gate 		svcerr_systemerr(rqstp->rq_xprt);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	shim_dbm_close((DBM *)m.map);
2357c478bd9Sstevel@tonic-gate 	yptol_exit(0);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	return (&result);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate bool_t
xdr_myfyl(XDR * xdrs,struct mycon * objp)2417c478bd9Sstevel@tonic-gate xdr_myfyl(XDR *xdrs, struct mycon *objp)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	int	ans = OK;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (!xdr_answer(xdrs, (answer *) &ans))
2467c478bd9Sstevel@tonic-gate 		return (FALSE);
2477c478bd9Sstevel@tonic-gate 	if (!xdr_pages(xdrs, objp))
2487c478bd9Sstevel@tonic-gate 		return (FALSE);
2497c478bd9Sstevel@tonic-gate 	if (!xdr_dirs(xdrs, objp))
2507c478bd9Sstevel@tonic-gate 		return (FALSE);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	return (TRUE);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate bool_t
xdr_pages(XDR * xdrs,struct mycon * m)2567c478bd9Sstevel@tonic-gate xdr_pages(XDR *xdrs, struct mycon *m)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	static	struct pag res;
2597c478bd9Sstevel@tonic-gate 	bool_t	false = FALSE;
2607c478bd9Sstevel@tonic-gate 	bool_t	true = TRUE;
2617c478bd9Sstevel@tonic-gate #ifdef DOSWAB
2627c478bd9Sstevel@tonic-gate 	short	*s;
2637c478bd9Sstevel@tonic-gate 	int	i;
2647c478bd9Sstevel@tonic-gate 	int	cnt;
2657c478bd9Sstevel@tonic-gate #endif
2667c478bd9Sstevel@tonic-gate 	res.status = mygetpage(res.pag_u.ok.blkdat, &(res.pag_u.ok.blkno), m);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate #ifdef DOSWAB
269c458f227Ssdussud 	if (res.status == OK) {
270c458f227Ssdussud 		s = (short *)res.pag_u.ok.blkdat;
271c458f227Ssdussud 		cnt = s[0];
272c458f227Ssdussud 		for (i = 0; i <= cnt; i++)
273c458f227Ssdussud 			s[i] = ntohs(s[i]);
274c458f227Ssdussud 	}
2757c478bd9Sstevel@tonic-gate #endif
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (!xdr_pag(xdrs, &res))
2787c478bd9Sstevel@tonic-gate 		return (FALSE);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	while (res.status == OK) {
2817c478bd9Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &true))
2827c478bd9Sstevel@tonic-gate 			return (FALSE);
2837c478bd9Sstevel@tonic-gate 		res.status = mygetpage(res.pag_u.ok.blkdat,
2847c478bd9Sstevel@tonic-gate 					&(res.pag_u.ok.blkno), m);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate #ifdef DOSWAB
287c458f227Ssdussud 		if (res.status == OK) {
288c458f227Ssdussud 			s = (short *)res.pag_u.ok.blkdat;
289c458f227Ssdussud 			cnt = s[0];
290c458f227Ssdussud 			for (i = 0; i <= cnt; i++)
291c458f227Ssdussud 				s[i] = ntohs(s[i]);
292c458f227Ssdussud 		}
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		if (!xdr_pag(xdrs, &res))
2967c478bd9Sstevel@tonic-gate 			return (FALSE);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return (xdr_bool(xdrs, &false));
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate int
mygetdir(char * block,int * no,struct mycon * m)3037c478bd9Sstevel@tonic-gate mygetdir(char *block, int *no, struct mycon *m)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	int	status;
3067c478bd9Sstevel@tonic-gate 	int	len;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (m->firstd == 0) {
3097c478bd9Sstevel@tonic-gate 		lseek(m->map->entries->dbm_dirf, 0, 0);
3107c478bd9Sstevel@tonic-gate 		m->firstd = 1;
3117c478bd9Sstevel@tonic-gate 	} else
3127c478bd9Sstevel@tonic-gate 		m->firstd++;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	len = read(m->map->entries->dbm_dirf, block, DBLKSIZ);
3157c478bd9Sstevel@tonic-gate 	*no = (m->firstd) - 1;
3167c478bd9Sstevel@tonic-gate 	status = OK;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * printf("dir block %d\n", (m->firstd) - 1);
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (len < 0) {
3237c478bd9Sstevel@tonic-gate 		perror("read directory");
3247c478bd9Sstevel@tonic-gate 		status = GETDBM_ERROR;
3257c478bd9Sstevel@tonic-gate 	} else if (len == 0) {
3267c478bd9Sstevel@tonic-gate 		status = GETDBM_EOF;
3277c478bd9Sstevel@tonic-gate 		/*
3287c478bd9Sstevel@tonic-gate 		 * printf("dir EOF\n");
3297c478bd9Sstevel@tonic-gate 		 */
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	return (status);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate bool_t
xdr_dirs(XDR * xdrs,struct mycon * m)3357c478bd9Sstevel@tonic-gate xdr_dirs(XDR *xdrs, struct mycon *m)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	static	struct dir res;
3387c478bd9Sstevel@tonic-gate 	bool_t	false = FALSE;
3397c478bd9Sstevel@tonic-gate 	bool_t	true = TRUE;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	res.status = mygetdir(res.dir_u.ok.blkdat, &(res.dir_u.ok.blkno), m);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (!xdr_dir(xdrs, &res))
3447c478bd9Sstevel@tonic-gate 		return (FALSE);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	while (res.status == OK) {
3477c478bd9Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &true))
3487c478bd9Sstevel@tonic-gate 			return (FALSE);
3497c478bd9Sstevel@tonic-gate 		res.status = mygetdir(res.dir_u.ok.blkdat,
3507c478bd9Sstevel@tonic-gate 					&(res.dir_u.ok.blkno), m);
3517c478bd9Sstevel@tonic-gate 		if (!xdr_dir(xdrs, &res))
3527c478bd9Sstevel@tonic-gate 			return (FALSE);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	return (xdr_bool(xdrs, &false));
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate int
mygetpage(char * block,int * pageno,struct mycon * m)3597c478bd9Sstevel@tonic-gate mygetpage(char *block, int *pageno, struct mycon *m)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	for (; m->key.dptr;
3637c478bd9Sstevel@tonic-gate 			m->key = shim_dbm_do_nextkey((DBM *)m->map, m->key)) {
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		if (m->map->entries->dbm_pagbno != m->lblk) {
3667c478bd9Sstevel@tonic-gate 			/*
3677c478bd9Sstevel@tonic-gate 			 * printf("block=%d lblk=%d\n",
3687c478bd9Sstevel@tonic-gate 			 *		m->map->entries->dbm_pagbno,
3697c478bd9Sstevel@tonic-gate 			 * 		m->lblk);
3707c478bd9Sstevel@tonic-gate 			 */
3717c478bd9Sstevel@tonic-gate 			m->lblk = m->map->entries->dbm_pagbno;
3727c478bd9Sstevel@tonic-gate 			*pageno = m->lblk;
3737c478bd9Sstevel@tonic-gate 			memmove(block, m->map->entries->dbm_pagbuf, PBLKSIZ);
3747c478bd9Sstevel@tonic-gate 			/* advance key on first  try	*/
3757c478bd9Sstevel@tonic-gate 			m->key = mydbm_topkey(m->map->entries, m->key);
3767c478bd9Sstevel@tonic-gate 			m->key = shim_dbm_do_nextkey((DBM *)m->map, m->key);
3777c478bd9Sstevel@tonic-gate 			return (OK);
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * printf("EOF\n");
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	return (GETDBM_EOF);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate datum
mydbm_topkey(DBM * db,datum okey)3877c478bd9Sstevel@tonic-gate mydbm_topkey(DBM *db, datum okey)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	datum		ans;
3907c478bd9Sstevel@tonic-gate 	datum		tmp;
3917c478bd9Sstevel@tonic-gate 	register char	*buf;
3927c478bd9Sstevel@tonic-gate 	int		n;
3937c478bd9Sstevel@tonic-gate 	register short	*sp;
394*a506a34cSth 	register short 	t;
3957c478bd9Sstevel@tonic-gate 	datum		item;
396*a506a34cSth #if defined(_XPG4_2)
397*a506a34cSth 	register size_t	m;
398*a506a34cSth #else
399*a506a34cSth 	register long	m;
400*a506a34cSth #endif
4017c478bd9Sstevel@tonic-gate 	register char	*p1, *p2;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	buf = db->dbm_pagbuf;
4047c478bd9Sstevel@tonic-gate 	sp = (short *)buf;
4057c478bd9Sstevel@tonic-gate 	/* find the maximum key in cmpdatum order */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ((unsigned)0 >= sp[0]) {
4087c478bd9Sstevel@tonic-gate 		return (okey);
4097c478bd9Sstevel@tonic-gate 	} else {
4107c478bd9Sstevel@tonic-gate 		ans.dptr = buf + sp[1];
4117c478bd9Sstevel@tonic-gate 		ans.dsize = PBLKSIZ - sp[1];
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	for (n = 2; ; n += 2) {
4147c478bd9Sstevel@tonic-gate 		if ((unsigned)n >= sp[0]) {
4157c478bd9Sstevel@tonic-gate 			if (ans.dptr == NULL) {
4167c478bd9Sstevel@tonic-gate 				return (okey);
4177c478bd9Sstevel@tonic-gate 			} else {
4187c478bd9Sstevel@tonic-gate 				return (ans);
4197c478bd9Sstevel@tonic-gate 			}
4207c478bd9Sstevel@tonic-gate 		} else {
4217c478bd9Sstevel@tonic-gate 			t = PBLKSIZ;
4227c478bd9Sstevel@tonic-gate 			if (n > 0)
4237c478bd9Sstevel@tonic-gate 				t = sp[n];
4247c478bd9Sstevel@tonic-gate 			tmp.dptr = buf + sp[n + 1];
4257c478bd9Sstevel@tonic-gate 			tmp.dsize = t - sp[n + 1];
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		m = tmp.dsize;
4297c478bd9Sstevel@tonic-gate 		if (m != ans.dsize) {
4307c478bd9Sstevel@tonic-gate 			if ((m - ans.dsize) < 0)
4317c478bd9Sstevel@tonic-gate 				ans = tmp;
4327c478bd9Sstevel@tonic-gate 		} else if (m == 0) {
4337c478bd9Sstevel@tonic-gate 		} else {
4347c478bd9Sstevel@tonic-gate 			p1 = tmp.dptr;
4357c478bd9Sstevel@tonic-gate 			p2 = ans.dptr;
4367c478bd9Sstevel@tonic-gate 			do
4377c478bd9Sstevel@tonic-gate 				if (*p1++ != *p2++) {
4387c478bd9Sstevel@tonic-gate 					if ((*--p1 - *--p2) < 0)
4397c478bd9Sstevel@tonic-gate 						ans = tmp;
4407c478bd9Sstevel@tonic-gate 				break;
4417c478bd9Sstevel@tonic-gate 				}
4427c478bd9Sstevel@tonic-gate 			while (--m);
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
446