xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_enum.c (revision 1da57d55)
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
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
347c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
357c478bd9Sstevel@tonic-gate  * California.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
38e8031f0aSraf #include "mt.h"
3961961e0fSrobinson #include <stdlib.h>
4061961e0fSrobinson #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include "yp_b.h"
447c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
457c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern int __yp_dobind_cflookup(char *, struct dom_binding **, int);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int dofirst(char *, char *, struct dom_binding *, struct timeval,
517c478bd9Sstevel@tonic-gate     char **, int  *, char **, int  *);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static int donext(char *, char *, char *, int, struct dom_binding *,
547c478bd9Sstevel@tonic-gate     struct timeval, char **, int *, char **val, int *);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This requests the yp server associated with a given domain to return the
587c478bd9Sstevel@tonic-gate  * first key/value pair from the map data base.  The returned key should be
597c478bd9Sstevel@tonic-gate  * used as an input to the call to ypclnt_next.  This part does the parameter
607c478bd9Sstevel@tonic-gate  * checking, and the do-until-success loop if 'hardlookup' is set.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate int
__yp_first_cflookup(char * domain,char * map,char ** key,int * keylen,char ** val,int * vallen,int hardlookup)637c478bd9Sstevel@tonic-gate __yp_first_cflookup(
647c478bd9Sstevel@tonic-gate 	char *domain,
657c478bd9Sstevel@tonic-gate 	char *map,
667c478bd9Sstevel@tonic-gate 	char **key,		/* return: key array */
677c478bd9Sstevel@tonic-gate 	int  *keylen,		/* return: bytes in key */
687c478bd9Sstevel@tonic-gate 	char **val,		/* return: value array */
697c478bd9Sstevel@tonic-gate 	int  *vallen,		/* return: bytes in val */
707c478bd9Sstevel@tonic-gate 	int  hardlookup)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	size_t domlen;
737c478bd9Sstevel@tonic-gate 	size_t maplen;
747c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
757c478bd9Sstevel@tonic-gate 	int reason;
767c478bd9Sstevel@tonic-gate 
7761961e0fSrobinson 	if ((map == NULL) || (domain == NULL))
787c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	domlen =  strlen(domain);
817c478bd9Sstevel@tonic-gate 	maplen =  strlen(map);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
8461961e0fSrobinson 	    (maplen == 0) || (maplen > YPMAXMAP))
857c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	for (;;) {
887c478bd9Sstevel@tonic-gate 
8961961e0fSrobinson 		if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
907c478bd9Sstevel@tonic-gate 			return (reason);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		if (pdomb->dom_binding->ypbind_hi_vers == YPVERS) {
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 			reason = dofirst(domain, map, pdomb, _ypserv_timeout,
957c478bd9Sstevel@tonic-gate 			    key, keylen, val, vallen);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
987c478bd9Sstevel@tonic-gate 			if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
997c478bd9Sstevel@tonic-gate 			    reason == YPERR_BUSY /* as if */) {
1007c478bd9Sstevel@tonic-gate 				yp_unbind(domain);
1017c478bd9Sstevel@tonic-gate 				if (hardlookup)
102e8031f0aSraf 					(void) sleep(_ypsleeptime); /* retry */
10361961e0fSrobinson 				else
1047c478bd9Sstevel@tonic-gate 					return (reason);
1057c478bd9Sstevel@tonic-gate 			} else
1067c478bd9Sstevel@tonic-gate 				break;
1077c478bd9Sstevel@tonic-gate 		} else {
1087c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
1097c478bd9Sstevel@tonic-gate 			return (YPERR_VERS);
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	return (reason);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
yp_first(char * domain,char * map,char ** key,int * keylen,char ** val,int * vallen)1167c478bd9Sstevel@tonic-gate yp_first(
1177c478bd9Sstevel@tonic-gate 	char *domain,
1187c478bd9Sstevel@tonic-gate 	char *map,
1197c478bd9Sstevel@tonic-gate 	char **key,		/* return: key array */
1207c478bd9Sstevel@tonic-gate 	int  *keylen,		/* return: bytes in key */
1217c478bd9Sstevel@tonic-gate 	char **val,		/* return: value array */
1227c478bd9Sstevel@tonic-gate 	int  *vallen)		/* return: bytes in val */
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	/* traditional yp_firs loops forever until success */
1257c478bd9Sstevel@tonic-gate 	return (__yp_first_cflookup(domain, map, key, keylen, val, vallen, 1));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * This part of the "get first" interface talks to ypserv.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static int
dofirst(domain,map,pdomb,timeout,key,keylen,val,vallen)1337c478bd9Sstevel@tonic-gate dofirst(domain, map, pdomb, timeout, key, keylen, val, vallen)
1347c478bd9Sstevel@tonic-gate 	char *domain;
1357c478bd9Sstevel@tonic-gate 	char *map;
1367c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
1377c478bd9Sstevel@tonic-gate 	struct timeval timeout;
1387c478bd9Sstevel@tonic-gate 	char **key;
1397c478bd9Sstevel@tonic-gate 	int  *keylen;
1407c478bd9Sstevel@tonic-gate 	char **val;
1417c478bd9Sstevel@tonic-gate 	int  *vallen;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	struct ypreq_nokey req;
1457c478bd9Sstevel@tonic-gate 	struct ypresp_key_val resp;
1467c478bd9Sstevel@tonic-gate 	unsigned int retval = 0;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	req.domain = domain;
1497c478bd9Sstevel@tonic-gate 	req.map = map;
1507c478bd9Sstevel@tonic-gate 	resp.keydat.dptr = resp.valdat.dptr = NULL;
1517c478bd9Sstevel@tonic-gate 	resp.keydat.dsize = resp.valdat.dsize = 0;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Do the get first request.  If the rpc call failed, return with status
1557c478bd9Sstevel@tonic-gate 	 * from this point.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	(void) memset((char *)&resp, 0, sizeof (struct ypresp_key_val));
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	switch (clnt_call(pdomb->dom_client, YPPROC_FIRST,
1617c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypreq_nokey,
1627c478bd9Sstevel@tonic-gate 			(char *)&req, (xdrproc_t)xdr_ypresp_key_val,
1637c478bd9Sstevel@tonic-gate 			(char *)&resp, timeout)) {
1647c478bd9Sstevel@tonic-gate 	case RPC_SUCCESS:
1657c478bd9Sstevel@tonic-gate 		break;
1667c478bd9Sstevel@tonic-gate 	case RPC_TIMEDOUT:
1677c478bd9Sstevel@tonic-gate 		return (YPERR_YPSERV);
1687c478bd9Sstevel@tonic-gate 	default:
1697c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/* See if the request succeeded */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (resp.status != YP_TRUE) {
1757c478bd9Sstevel@tonic-gate 		retval = ypprot_err(resp.status);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
178*48bbca81SDaniel Hoffman 	/* Get some memory which the user can get rid of as they like */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (!retval) {
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		if ((*key = malloc((size_t)resp.keydat.dsize + 2)) != NULL) {
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 			if ((*val = malloc(
1857c478bd9Sstevel@tonic-gate 			    (size_t)resp.valdat.dsize + 2)) == NULL) {
18661961e0fSrobinson 				free(*key);
1877c478bd9Sstevel@tonic-gate 				retval = YPERR_RESRC;
1887c478bd9Sstevel@tonic-gate 			}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		} else {
1917c478bd9Sstevel@tonic-gate 			retval = YPERR_RESRC;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/* Copy the returned key and value byte strings into the new memory */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (!retval) {
1987c478bd9Sstevel@tonic-gate 		*keylen = (int)resp.keydat.dsize;
1997c478bd9Sstevel@tonic-gate 		(void) memcpy(*key, resp.keydat.dptr,
2007c478bd9Sstevel@tonic-gate 		    (size_t)resp.keydat.dsize);
2017c478bd9Sstevel@tonic-gate 		(*key)[resp.keydat.dsize] = '\n';
2027c478bd9Sstevel@tonic-gate 		(*key)[resp.keydat.dsize + 1] = '\0';
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		*vallen = (int)resp.valdat.dsize;
2057c478bd9Sstevel@tonic-gate 		(void) memcpy(*val, resp.valdat.dptr,
2067c478bd9Sstevel@tonic-gate 		    (size_t)resp.valdat.dsize);
2077c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize] = '\n';
2087c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize + 1] = '\0';
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	CLNT_FREERES(pdomb->dom_client,
2127c478bd9Sstevel@tonic-gate 		(xdrproc_t)xdr_ypresp_key_val, (char *)&resp);
2137c478bd9Sstevel@tonic-gate 	return (retval);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * This requests the yp server associated with a given domain to return the
2187c478bd9Sstevel@tonic-gate  * "next" key/value pair from the map data base.  The input key should be
2197c478bd9Sstevel@tonic-gate  * one returned by ypclnt_first or a previous call to ypclnt_next.  The
2207c478bd9Sstevel@tonic-gate  * returned key should be used as an input to the next call to ypclnt_next.
2217c478bd9Sstevel@tonic-gate  * This part does the parameter checking, and the do-until-success loop.
2227c478bd9Sstevel@tonic-gate  * if 'hardlookup' is set.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate int
__yp_next_cflookup(char * domain,char * map,char * inkey,int inkeylen,char ** outkey,int * outkeylen,char ** val,int * vallen,int hardlookup)2257c478bd9Sstevel@tonic-gate __yp_next_cflookup(
2267c478bd9Sstevel@tonic-gate 	char *domain,
2277c478bd9Sstevel@tonic-gate 	char *map,
2287c478bd9Sstevel@tonic-gate 	char *inkey,
2297c478bd9Sstevel@tonic-gate 	int  inkeylen,
2307c478bd9Sstevel@tonic-gate 	char **outkey,		/* return: key array associated with val */
2317c478bd9Sstevel@tonic-gate 	int  *outkeylen,	/* return: bytes in key */
2327c478bd9Sstevel@tonic-gate 	char **val,		/* return: value array associated with outkey */
2337c478bd9Sstevel@tonic-gate 	int  *vallen,		/* return: bytes in val */
2347c478bd9Sstevel@tonic-gate 	int  hardlookup)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	size_t domlen;
2377c478bd9Sstevel@tonic-gate 	size_t maplen;
2387c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
2397c478bd9Sstevel@tonic-gate 	int reason;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
24261961e0fSrobinson 	if ((map == NULL) || (domain == NULL) || (inkey == NULL))
2437c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	domlen =  strlen(domain);
2467c478bd9Sstevel@tonic-gate 	maplen =  strlen(map);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
24961961e0fSrobinson 	    (maplen == 0) || (maplen > YPMAXMAP))
2507c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	for (;;) {
25361961e0fSrobinson 		if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
2547c478bd9Sstevel@tonic-gate 			return (reason);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		if (pdomb->dom_binding->ypbind_hi_vers == YPVERS) {
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 			reason = donext(domain, map, inkey, inkeylen, pdomb,
2597c478bd9Sstevel@tonic-gate 			    _ypserv_timeout, outkey, outkeylen, val, vallen);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
2647c478bd9Sstevel@tonic-gate 			    reason == YPERR_BUSY /* as if */) {
2657c478bd9Sstevel@tonic-gate 				yp_unbind(domain);
2667c478bd9Sstevel@tonic-gate 				if (hardlookup)
267e8031f0aSraf 					(void) sleep(_ypsleeptime); /* retry */
26861961e0fSrobinson 				else
2697c478bd9Sstevel@tonic-gate 					return (reason);
2707c478bd9Sstevel@tonic-gate 			} else
2717c478bd9Sstevel@tonic-gate 				break;
2727c478bd9Sstevel@tonic-gate 		} else {
2737c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
2747c478bd9Sstevel@tonic-gate 			return (YPERR_VERS);
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	return (reason);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate int
yp_next(char * domain,char * map,char * inkey,int inkeylen,char ** outkey,int * outkeylen,char ** val,int * vallen)2827c478bd9Sstevel@tonic-gate yp_next(
2837c478bd9Sstevel@tonic-gate 	char *domain,
2847c478bd9Sstevel@tonic-gate 	char *map,
2857c478bd9Sstevel@tonic-gate 	char *inkey,
2867c478bd9Sstevel@tonic-gate 	int  inkeylen,
2877c478bd9Sstevel@tonic-gate 	char **outkey,		/* return: key array associated with val */
2887c478bd9Sstevel@tonic-gate 	int  *outkeylen,	/* return: bytes in key */
2897c478bd9Sstevel@tonic-gate 	char **val,		/* return: value array associated with outkey */
2907c478bd9Sstevel@tonic-gate 	int  *vallen)		/* return: bytes in val */
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	/* traditional yp_next loops forever until success */
2937c478bd9Sstevel@tonic-gate 	return (__yp_next_cflookup(domain, map, inkey, inkeylen, outkey,
2947c478bd9Sstevel@tonic-gate 				outkeylen, val, vallen, 1));
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate /*
2997c478bd9Sstevel@tonic-gate  * This part of the "get next" interface talks to ypserv.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate static int
donext(domain,map,inkey,inkeylen,pdomb,timeout,outkey,outkeylen,val,vallen)3027c478bd9Sstevel@tonic-gate donext(domain, map, inkey, inkeylen, pdomb, timeout, outkey, outkeylen,
3037c478bd9Sstevel@tonic-gate     val, vallen)
3047c478bd9Sstevel@tonic-gate 	char *domain;
3057c478bd9Sstevel@tonic-gate 	char *map;
3067c478bd9Sstevel@tonic-gate 	char *inkey;
3077c478bd9Sstevel@tonic-gate 	int  inkeylen;
3087c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
3097c478bd9Sstevel@tonic-gate 	struct timeval timeout;
3107c478bd9Sstevel@tonic-gate 	char **outkey;		/* return: key array associated with val */
3117c478bd9Sstevel@tonic-gate 	int  *outkeylen;	/* return: bytes in key */
3127c478bd9Sstevel@tonic-gate 	char **val;		/* return: value array associated with outkey */
3137c478bd9Sstevel@tonic-gate 	int  *vallen;		/* return: bytes in val */
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	struct ypreq_key req;
3177c478bd9Sstevel@tonic-gate 	struct ypresp_key_val resp;
3187c478bd9Sstevel@tonic-gate 	unsigned int retval = 0;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	req.domain = domain;
3217c478bd9Sstevel@tonic-gate 	req.map = map;
3227c478bd9Sstevel@tonic-gate 	req.keydat.dptr = inkey;
3237c478bd9Sstevel@tonic-gate 	req.keydat.dsize = inkeylen;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	resp.keydat.dptr = resp.valdat.dptr = NULL;
3267c478bd9Sstevel@tonic-gate 	resp.keydat.dsize = resp.valdat.dsize = 0;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Do the get next request.  If the rpc call failed, return with status
3307c478bd9Sstevel@tonic-gate 	 * from this point.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	switch (clnt_call(pdomb->dom_client,
3347c478bd9Sstevel@tonic-gate 			YPPROC_NEXT, (xdrproc_t)xdr_ypreq_key, (char *)&req,
3357c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_key_val, (char *)&resp,
3367c478bd9Sstevel@tonic-gate 			timeout)) {
3377c478bd9Sstevel@tonic-gate 	case RPC_SUCCESS:
3387c478bd9Sstevel@tonic-gate 		break;
3397c478bd9Sstevel@tonic-gate 	case RPC_TIMEDOUT:
3407c478bd9Sstevel@tonic-gate 		return (YPERR_YPSERV);
3417c478bd9Sstevel@tonic-gate 	default:
3427c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/* See if the request succeeded */
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (resp.status != YP_TRUE) {
3487c478bd9Sstevel@tonic-gate 		retval = ypprot_err(resp.status);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
351*48bbca81SDaniel Hoffman 	/* Get some memory which the user can get rid of as they like */
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (!retval) {
3547c478bd9Sstevel@tonic-gate 		if ((*outkey = malloc((size_t)
3557c478bd9Sstevel@tonic-gate 		    resp.keydat.dsize + 2)) != NULL) {
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 			if ((*val = malloc((size_t)
3587c478bd9Sstevel@tonic-gate 			    resp.valdat.dsize + 2)) == NULL) {
35961961e0fSrobinson 				free(*outkey);
3607c478bd9Sstevel@tonic-gate 				retval = YPERR_RESRC;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		} else {
3647c478bd9Sstevel@tonic-gate 			retval = YPERR_RESRC;
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/* Copy the returned key and value byte strings into the new memory */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (!retval) {
3717c478bd9Sstevel@tonic-gate 		*outkeylen = (int)resp.keydat.dsize;
3727c478bd9Sstevel@tonic-gate 		(void) memcpy(*outkey, resp.keydat.dptr,
3737c478bd9Sstevel@tonic-gate 		    (size_t)resp.keydat.dsize);
3747c478bd9Sstevel@tonic-gate 		(*outkey)[resp.keydat.dsize] = '\n';
3757c478bd9Sstevel@tonic-gate 		(*outkey)[resp.keydat.dsize + 1] = '\0';
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		*vallen = (int)resp.valdat.dsize;
3787c478bd9Sstevel@tonic-gate 		(void) memcpy(*val, resp.valdat.dptr,
3797c478bd9Sstevel@tonic-gate 		    (size_t)resp.valdat.dsize);
3807c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize] = '\n';
3817c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize + 1] = '\0';
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	CLNT_FREERES(pdomb->dom_client, (xdrproc_t)xdr_ypresp_key_val,
3857c478bd9Sstevel@tonic-gate 		    (char *)&resp);
3867c478bd9Sstevel@tonic-gate 	return (retval);
3877c478bd9Sstevel@tonic-gate }
388