xref: /onnv-gate/usr/src/cmd/ypcmd/ypxfrd_server.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23141Ssdussud  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
26141Ssdussud /* Portions Copyright 2005 Juergen Keil */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <signal.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <ndbm.h>
360Sstevel@tonic-gate #include <rpc/rpc.h>
370Sstevel@tonic-gate #include <rpc/svc.h>
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <syslog.h>
410Sstevel@tonic-gate #include "ypxfrd.h"
420Sstevel@tonic-gate #include "ypsym.h"
430Sstevel@tonic-gate #include "ypdefs.h"
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Because this code hacks into DBM underneath its API it can't use the N2L
460Sstevel@tonic-gate  * shim in it's normal way. It thus includes shim.h instead of shim_hooks.h
470Sstevel@tonic-gate  * and has knowledge of shim internals. While copying the DBM files it does
480Sstevel@tonic-gate  * not lock them. This reflects the behavior of the pre N2L code.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate #include "shim.h"
510Sstevel@tonic-gate #include "yptol.h"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #if (defined(vax) || defined(i386))
540Sstevel@tonic-gate #define	DOSWAB 1
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate 
570Sstevel@tonic-gate USE_YP_SECURE
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /* per connection stuff */
600Sstevel@tonic-gate struct mycon {
610Sstevel@tonic-gate 	map_ctrl *map;
620Sstevel@tonic-gate 	int	lblk;
630Sstevel@tonic-gate 	int	firstd;
640Sstevel@tonic-gate 	datum	key;
650Sstevel@tonic-gate };
660Sstevel@tonic-gate 
670Sstevel@tonic-gate bool_t xdr_myfyl(XDR *xdrs, struct mycon *objp);
680Sstevel@tonic-gate bool_t xdr_pages(XDR *xdrs, struct mycon *m);
690Sstevel@tonic-gate bool_t xdr_dirs(XDR *xdrs, struct mycon *m);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate int mygetdir(char *block, int *no, struct mycon *m);
720Sstevel@tonic-gate int mygetpage(char *block, int *pageno, struct mycon *m);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate datum mydbm_topkey(DBM *db, datum okey);
750Sstevel@tonic-gate datum dbm_do_nextkey();
760Sstevel@tonic-gate datum shim_dbm_do_nextkey();
770Sstevel@tonic-gate 
780Sstevel@tonic-gate extern void get_secure_nets(char *);
790Sstevel@tonic-gate extern int check_secure_net_ti(struct netbuf *, char *);
800Sstevel@tonic-gate extern int _main(int, char **);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate int
main(int argc,char ** argv)830Sstevel@tonic-gate main(int argc, char **argv)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate 	int connmaxrec = RPC_MAXDATASIZE;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/* load up the securenet file */
880Sstevel@tonic-gate 	get_secure_nets(argv[0]);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * Set non-blocking mode and maximum record size for
920Sstevel@tonic-gate 	 * connection oriented RPC transports.
930Sstevel@tonic-gate 	 */
940Sstevel@tonic-gate 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
950Sstevel@tonic-gate 		syslog(LOG_INFO|LOG_DAEMON,
960Sstevel@tonic-gate 			"unable to set maximum RPC record size");
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* Initialize file locking etc. */
1000Sstevel@tonic-gate 	if (!init_lock_system(TRUE))
1010Sstevel@tonic-gate 		/* An detailed error will already have been logged */
1020Sstevel@tonic-gate 		exit(-1);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	return (_main(argc, argv));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * In yptol mode we may start a cache update thread within a child process.
1090Sstevel@tonic-gate  * It is thus important that child processes do not exit, killing any such
1100Sstevel@tonic-gate  * threads, before the thread has completed. They must thus call this version
1110Sstevel@tonic-gate  * of the exit() function.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate void
yptol_exit(int status)1140Sstevel@tonic-gate yptol_exit(int status)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	if (yptol_mode) {
1170Sstevel@tonic-gate 		thr_join(0, NULL, NULL);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 	exit(status);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate dbmfyl *
getdbm_1_svc(hosereq * argp,struct svc_req * rqstp)1230Sstevel@tonic-gate getdbm_1_svc(hosereq *argp, struct svc_req *rqstp)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	static dbmfyl  result;
1260Sstevel@tonic-gate 	char path[MAXNAMLEN + 1];
1270Sstevel@tonic-gate 	SVCXPRT *xprt;
1280Sstevel@tonic-gate 	int pid;
1290Sstevel@tonic-gate 	int res;
1300Sstevel@tonic-gate 	struct mycon m;
1310Sstevel@tonic-gate 	char *ypname = "ypxfrd";
1320Sstevel@tonic-gate 	struct netbuf *nbuf;
1330Sstevel@tonic-gate 	sa_family_t af;
1340Sstevel@tonic-gate 	in_port_t port;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	xprt = rqstp->rq_xprt;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	signal(SIGPIPE, SIG_IGN);
1390Sstevel@tonic-gate 	signal(SIGCHLD, SIG_IGN);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/*
1420Sstevel@tonic-gate 	 * Build up path name. If we are working in N2L mode also conv
1430Sstevel@tonic-gate 	 * to the new N2L style mapname.
1440Sstevel@tonic-gate 	 *
1450Sstevel@tonic-gate 	 * Do not allow any path as a domain name or map name.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	if ((strchr(argp->domain, '/') != NULL) ||
1480Sstevel@tonic-gate 		(strchr(argp->map, '/') != NULL) ||
1490Sstevel@tonic-gate 		(!ypmkfilename(argp->domain, argp->map, (char *)&path))) {
1500Sstevel@tonic-gate 		res = GETDBM_ERROR;
1510Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1520Sstevel@tonic-gate 					(caddr_t)&res)) {
1530Sstevel@tonic-gate 			svcerr_systemerr(rqstp->rq_xprt);
1540Sstevel@tonic-gate 		}
1550Sstevel@tonic-gate 		return (NULL);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	pid = fork1();
1590Sstevel@tonic-gate 	if (pid < 0) {
1600Sstevel@tonic-gate 		perror("fork");
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		res = GETDBM_ERROR;
1630Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1640Sstevel@tonic-gate 					(caddr_t)&res)) {
1650Sstevel@tonic-gate 			svcerr_systemerr(rqstp->rq_xprt);
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 		return (NULL);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	if (pid != 0)
1700Sstevel@tonic-gate 		return (NULL);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	m.map = (map_ctrl *)shim_dbm_open(path, 0, 0);
1730Sstevel@tonic-gate 	if (m.map == NULL) {
1740Sstevel@tonic-gate 		perror(path);
1750Sstevel@tonic-gate 		res = GETDBM_ERROR;
1760Sstevel@tonic-gate 		if (!svc_sendreply(rqstp->rq_xprt, xdr_answer,
1770Sstevel@tonic-gate 					(caddr_t)&res)) {
1780Sstevel@tonic-gate 		    svcerr_systemerr(rqstp->rq_xprt);
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 		yptol_exit(0);
1810Sstevel@tonic-gate 		return (NULL);
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/* Do the security thing */
1850Sstevel@tonic-gate 	if ((nbuf = svc_getrpccaller(xprt)) == 0) {
1860Sstevel@tonic-gate 		res = GETDBM_ERROR;
1870Sstevel@tonic-gate 		if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
1880Sstevel@tonic-gate 			svcerr_systemerr(xprt);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		shim_dbm_close((DBM *)m.map);
1910Sstevel@tonic-gate 		yptol_exit(0);
1920Sstevel@tonic-gate 		return (NULL);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	if (!check_secure_net_ti(nbuf, ypname)) {
1950Sstevel@tonic-gate 		res = GETDBM_ERROR;
1960Sstevel@tonic-gate 		if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
1970Sstevel@tonic-gate 			svcerr_systemerr(xprt);
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 		shim_dbm_close((DBM *)m.map);
2000Sstevel@tonic-gate 		yptol_exit(1);
2010Sstevel@tonic-gate 		return (NULL);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
2050Sstevel@tonic-gate 	port = (af == AF_INET6) ?
2060Sstevel@tonic-gate 		((struct sockaddr_in6 *)nbuf->buf)->sin6_port :
2070Sstevel@tonic-gate 		((struct sockaddr_in  *)nbuf->buf)->sin_port;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if ((af == AF_INET || af == AF_INET6) &&
2100Sstevel@tonic-gate 		(ntohs(port) > IPPORT_RESERVED)) {
2110Sstevel@tonic-gate 		datum key, val;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		key.dptr = yp_secure;
2140Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
2150Sstevel@tonic-gate 		val = shim_dbm_fetch((DBM *)m.map, key);
2160Sstevel@tonic-gate 		if (val.dptr != NULL) {
2170Sstevel@tonic-gate 			res = GETDBM_ERROR;
2180Sstevel@tonic-gate 			if (!svc_sendreply(xprt, xdr_answer, (caddr_t)&res)) {
2190Sstevel@tonic-gate 				svcerr_systemerr(xprt);
2200Sstevel@tonic-gate 			}
2210Sstevel@tonic-gate 			shim_dbm_close((DBM *)m.map);
2220Sstevel@tonic-gate 			yptol_exit(1);
2230Sstevel@tonic-gate 			return (NULL);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/* OK, we're through */
2280Sstevel@tonic-gate 	m.key = shim_dbm_firstkey((DBM *)m.map);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	m.lblk = -1;
2310Sstevel@tonic-gate 	m.firstd = 0;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (!svc_sendreply(rqstp->rq_xprt, xdr_myfyl, (caddr_t)&m)) {
2340Sstevel@tonic-gate 		svcerr_systemerr(rqstp->rq_xprt);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	shim_dbm_close((DBM *)m.map);
2370Sstevel@tonic-gate 	yptol_exit(0);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	return (&result);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate bool_t
xdr_myfyl(XDR * xdrs,struct mycon * objp)2430Sstevel@tonic-gate xdr_myfyl(XDR *xdrs, struct mycon *objp)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	int	ans = OK;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (!xdr_answer(xdrs, (answer *) &ans))
2480Sstevel@tonic-gate 		return (FALSE);
2490Sstevel@tonic-gate 	if (!xdr_pages(xdrs, objp))
2500Sstevel@tonic-gate 		return (FALSE);
2510Sstevel@tonic-gate 	if (!xdr_dirs(xdrs, objp))
2520Sstevel@tonic-gate 		return (FALSE);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	return (TRUE);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate bool_t
xdr_pages(XDR * xdrs,struct mycon * m)2580Sstevel@tonic-gate xdr_pages(XDR *xdrs, struct mycon *m)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	static	struct pag res;
2610Sstevel@tonic-gate 	bool_t	false = FALSE;
2620Sstevel@tonic-gate 	bool_t	true = TRUE;
2630Sstevel@tonic-gate #ifdef DOSWAB
2640Sstevel@tonic-gate 	short	*s;
2650Sstevel@tonic-gate 	int	i;
2660Sstevel@tonic-gate 	int	cnt;
2670Sstevel@tonic-gate #endif
2680Sstevel@tonic-gate 	res.status = mygetpage(res.pag_u.ok.blkdat, &(res.pag_u.ok.blkno), m);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate #ifdef DOSWAB
271141Ssdussud 	if (res.status == OK) {
272141Ssdussud 		s = (short *)res.pag_u.ok.blkdat;
273141Ssdussud 		cnt = s[0];
274141Ssdussud 		for (i = 0; i <= cnt; i++)
275141Ssdussud 			s[i] = ntohs(s[i]);
276141Ssdussud 	}
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (!xdr_pag(xdrs, &res))
2800Sstevel@tonic-gate 		return (FALSE);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	while (res.status == OK) {
2830Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &true))
2840Sstevel@tonic-gate 			return (FALSE);
2850Sstevel@tonic-gate 		res.status = mygetpage(res.pag_u.ok.blkdat,
2860Sstevel@tonic-gate 					&(res.pag_u.ok.blkno), m);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate #ifdef DOSWAB
289141Ssdussud 		if (res.status == OK) {
290141Ssdussud 			s = (short *)res.pag_u.ok.blkdat;
291141Ssdussud 			cnt = s[0];
292141Ssdussud 			for (i = 0; i <= cnt; i++)
293141Ssdussud 				s[i] = ntohs(s[i]);
294141Ssdussud 		}
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		if (!xdr_pag(xdrs, &res))
2980Sstevel@tonic-gate 			return (FALSE);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	return (xdr_bool(xdrs, &false));
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate int
mygetdir(char * block,int * no,struct mycon * m)3050Sstevel@tonic-gate mygetdir(char *block, int *no, struct mycon *m)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	int	status;
3080Sstevel@tonic-gate 	int	len;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (m->firstd == 0) {
3110Sstevel@tonic-gate 		lseek(m->map->entries->dbm_dirf, 0, 0);
3120Sstevel@tonic-gate 		m->firstd = 1;
3130Sstevel@tonic-gate 	} else
3140Sstevel@tonic-gate 		m->firstd++;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	len = read(m->map->entries->dbm_dirf, block, DBLKSIZ);
3170Sstevel@tonic-gate 	*no = (m->firstd) - 1;
3180Sstevel@tonic-gate 	status = OK;
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * printf("dir block %d\n", (m->firstd) - 1);
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (len < 0) {
3250Sstevel@tonic-gate 		perror("read directory");
3260Sstevel@tonic-gate 		status = GETDBM_ERROR;
3270Sstevel@tonic-gate 	} else if (len == 0) {
3280Sstevel@tonic-gate 		status = GETDBM_EOF;
3290Sstevel@tonic-gate 		/*
3300Sstevel@tonic-gate 		 * printf("dir EOF\n");
3310Sstevel@tonic-gate 		 */
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	return (status);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate bool_t
xdr_dirs(XDR * xdrs,struct mycon * m)3370Sstevel@tonic-gate xdr_dirs(XDR *xdrs, struct mycon *m)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	static	struct dir res;
3400Sstevel@tonic-gate 	bool_t	false = FALSE;
3410Sstevel@tonic-gate 	bool_t	true = TRUE;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	res.status = mygetdir(res.dir_u.ok.blkdat, &(res.dir_u.ok.blkno), m);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (!xdr_dir(xdrs, &res))
3460Sstevel@tonic-gate 		return (FALSE);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	while (res.status == OK) {
3490Sstevel@tonic-gate 		if (!xdr_bool(xdrs, &true))
3500Sstevel@tonic-gate 			return (FALSE);
3510Sstevel@tonic-gate 		res.status = mygetdir(res.dir_u.ok.blkdat,
3520Sstevel@tonic-gate 					&(res.dir_u.ok.blkno), m);
3530Sstevel@tonic-gate 		if (!xdr_dir(xdrs, &res))
3540Sstevel@tonic-gate 			return (FALSE);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	return (xdr_bool(xdrs, &false));
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate int
mygetpage(char * block,int * pageno,struct mycon * m)3610Sstevel@tonic-gate mygetpage(char *block, int *pageno, struct mycon *m)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	for (; m->key.dptr;
3650Sstevel@tonic-gate 			m->key = shim_dbm_do_nextkey((DBM *)m->map, m->key)) {
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		if (m->map->entries->dbm_pagbno != m->lblk) {
3680Sstevel@tonic-gate 			/*
3690Sstevel@tonic-gate 			 * printf("block=%d lblk=%d\n",
3700Sstevel@tonic-gate 			 *		m->map->entries->dbm_pagbno,
3710Sstevel@tonic-gate 			 * 		m->lblk);
3720Sstevel@tonic-gate 			 */
3730Sstevel@tonic-gate 			m->lblk = m->map->entries->dbm_pagbno;
3740Sstevel@tonic-gate 			*pageno = m->lblk;
3750Sstevel@tonic-gate 			memmove(block, m->map->entries->dbm_pagbuf, PBLKSIZ);
3760Sstevel@tonic-gate 			/* advance key on first  try	*/
3770Sstevel@tonic-gate 			m->key = mydbm_topkey(m->map->entries, m->key);
3780Sstevel@tonic-gate 			m->key = shim_dbm_do_nextkey((DBM *)m->map, m->key);
3790Sstevel@tonic-gate 			return (OK);
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 	}
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * printf("EOF\n");
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 	return (GETDBM_EOF);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate datum
mydbm_topkey(DBM * db,datum okey)3890Sstevel@tonic-gate mydbm_topkey(DBM *db, datum okey)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate 	datum		ans;
3920Sstevel@tonic-gate 	datum		tmp;
3930Sstevel@tonic-gate 	register char	*buf;
3940Sstevel@tonic-gate 	int		n;
3950Sstevel@tonic-gate 	register short	*sp;
396*702Sth160488 	register short 	t;
3970Sstevel@tonic-gate 	datum		item;
398*702Sth160488 #if defined(_XPG4_2)
399*702Sth160488 	register size_t	m;
400*702Sth160488 #else
401*702Sth160488 	register long	m;
402*702Sth160488 #endif
4030Sstevel@tonic-gate 	register char	*p1, *p2;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	buf = db->dbm_pagbuf;
4060Sstevel@tonic-gate 	sp = (short *)buf;
4070Sstevel@tonic-gate 	/* find the maximum key in cmpdatum order */
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if ((unsigned)0 >= sp[0]) {
4100Sstevel@tonic-gate 		return (okey);
4110Sstevel@tonic-gate 	} else {
4120Sstevel@tonic-gate 		ans.dptr = buf + sp[1];
4130Sstevel@tonic-gate 		ans.dsize = PBLKSIZ - sp[1];
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 	for (n = 2; ; n += 2) {
4160Sstevel@tonic-gate 		if ((unsigned)n >= sp[0]) {
4170Sstevel@tonic-gate 			if (ans.dptr == NULL) {
4180Sstevel@tonic-gate 				return (okey);
4190Sstevel@tonic-gate 			} else {
4200Sstevel@tonic-gate 				return (ans);
4210Sstevel@tonic-gate 			}
4220Sstevel@tonic-gate 		} else {
4230Sstevel@tonic-gate 			t = PBLKSIZ;
4240Sstevel@tonic-gate 			if (n > 0)
4250Sstevel@tonic-gate 				t = sp[n];
4260Sstevel@tonic-gate 			tmp.dptr = buf + sp[n + 1];
4270Sstevel@tonic-gate 			tmp.dsize = t - sp[n + 1];
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		m = tmp.dsize;
4310Sstevel@tonic-gate 		if (m != ans.dsize) {
4320Sstevel@tonic-gate 			if ((m - ans.dsize) < 0)
4330Sstevel@tonic-gate 				ans = tmp;
4340Sstevel@tonic-gate 		} else if (m == 0) {
4350Sstevel@tonic-gate 		} else {
4360Sstevel@tonic-gate 			p1 = tmp.dptr;
4370Sstevel@tonic-gate 			p2 = ans.dptr;
4380Sstevel@tonic-gate 			do
4390Sstevel@tonic-gate 				if (*p1++ != *p2++) {
4400Sstevel@tonic-gate 					if ((*--p1 - *--p2) < 0)
4410Sstevel@tonic-gate 						ans = tmp;
4420Sstevel@tonic-gate 				break;
4430Sstevel@tonic-gate 				}
4440Sstevel@tonic-gate 			while (--m);
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate }
448