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