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
21132Srobinson */
22132Srobinson
23132Srobinson /*
24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
330Sstevel@tonic-gate * under license from the Regents of the University of
340Sstevel@tonic-gate * California.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
380Sstevel@tonic-gate
39*1219Sraf #include "mt.h"
40132Srobinson #include <stdlib.h>
41132Srobinson #include <unistd.h>
420Sstevel@tonic-gate #include <rpc/rpc.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include "yp_b.h"
450Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
460Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
470Sstevel@tonic-gate #include <string.h>
480Sstevel@tonic-gate
490Sstevel@tonic-gate extern int __yp_dobind_cflookup(char *, struct dom_binding **, int);
500Sstevel@tonic-gate
510Sstevel@tonic-gate static int dofirst(char *, char *, struct dom_binding *, struct timeval,
520Sstevel@tonic-gate char **, int *, char **, int *);
530Sstevel@tonic-gate
540Sstevel@tonic-gate static int donext(char *, char *, char *, int, struct dom_binding *,
550Sstevel@tonic-gate struct timeval, char **, int *, char **val, int *);
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * This requests the yp server associated with a given domain to return the
590Sstevel@tonic-gate * first key/value pair from the map data base. The returned key should be
600Sstevel@tonic-gate * used as an input to the call to ypclnt_next. This part does the parameter
610Sstevel@tonic-gate * checking, and the do-until-success loop if 'hardlookup' is set.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate int
__yp_first_cflookup(char * domain,char * map,char ** key,int * keylen,char ** val,int * vallen,int hardlookup)640Sstevel@tonic-gate __yp_first_cflookup(
650Sstevel@tonic-gate char *domain,
660Sstevel@tonic-gate char *map,
670Sstevel@tonic-gate char **key, /* return: key array */
680Sstevel@tonic-gate int *keylen, /* return: bytes in key */
690Sstevel@tonic-gate char **val, /* return: value array */
700Sstevel@tonic-gate int *vallen, /* return: bytes in val */
710Sstevel@tonic-gate int hardlookup)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate size_t domlen;
740Sstevel@tonic-gate size_t maplen;
750Sstevel@tonic-gate struct dom_binding *pdomb;
760Sstevel@tonic-gate int reason;
770Sstevel@tonic-gate
78132Srobinson if ((map == NULL) || (domain == NULL))
790Sstevel@tonic-gate return (YPERR_BADARGS);
800Sstevel@tonic-gate
810Sstevel@tonic-gate domlen = strlen(domain);
820Sstevel@tonic-gate maplen = strlen(map);
830Sstevel@tonic-gate
840Sstevel@tonic-gate if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
85132Srobinson (maplen == 0) || (maplen > YPMAXMAP))
860Sstevel@tonic-gate return (YPERR_BADARGS);
870Sstevel@tonic-gate
880Sstevel@tonic-gate for (;;) {
890Sstevel@tonic-gate
90132Srobinson if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
910Sstevel@tonic-gate return (reason);
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (pdomb->dom_binding->ypbind_hi_vers == YPVERS) {
940Sstevel@tonic-gate
950Sstevel@tonic-gate reason = dofirst(domain, map, pdomb, _ypserv_timeout,
960Sstevel@tonic-gate key, keylen, val, vallen);
970Sstevel@tonic-gate
980Sstevel@tonic-gate __yp_rel_binding(pdomb);
990Sstevel@tonic-gate if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
1000Sstevel@tonic-gate reason == YPERR_BUSY /* as if */) {
1010Sstevel@tonic-gate yp_unbind(domain);
1020Sstevel@tonic-gate if (hardlookup)
103*1219Sraf (void) sleep(_ypsleeptime); /* retry */
104132Srobinson else
1050Sstevel@tonic-gate return (reason);
1060Sstevel@tonic-gate } else
1070Sstevel@tonic-gate break;
1080Sstevel@tonic-gate } else {
1090Sstevel@tonic-gate __yp_rel_binding(pdomb);
1100Sstevel@tonic-gate return (YPERR_VERS);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate return (reason);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate int
yp_first(char * domain,char * map,char ** key,int * keylen,char ** val,int * vallen)1170Sstevel@tonic-gate yp_first(
1180Sstevel@tonic-gate char *domain,
1190Sstevel@tonic-gate char *map,
1200Sstevel@tonic-gate char **key, /* return: key array */
1210Sstevel@tonic-gate int *keylen, /* return: bytes in key */
1220Sstevel@tonic-gate char **val, /* return: value array */
1230Sstevel@tonic-gate int *vallen) /* return: bytes in val */
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate /* traditional yp_firs loops forever until success */
1260Sstevel@tonic-gate return (__yp_first_cflookup(domain, map, key, keylen, val, vallen, 1));
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate * This part of the "get first" interface talks to ypserv.
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate static int
dofirst(domain,map,pdomb,timeout,key,keylen,val,vallen)1340Sstevel@tonic-gate dofirst(domain, map, pdomb, timeout, key, keylen, val, vallen)
1350Sstevel@tonic-gate char *domain;
1360Sstevel@tonic-gate char *map;
1370Sstevel@tonic-gate struct dom_binding *pdomb;
1380Sstevel@tonic-gate struct timeval timeout;
1390Sstevel@tonic-gate char **key;
1400Sstevel@tonic-gate int *keylen;
1410Sstevel@tonic-gate char **val;
1420Sstevel@tonic-gate int *vallen;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate struct ypreq_nokey req;
1460Sstevel@tonic-gate struct ypresp_key_val resp;
1470Sstevel@tonic-gate unsigned int retval = 0;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate req.domain = domain;
1500Sstevel@tonic-gate req.map = map;
1510Sstevel@tonic-gate resp.keydat.dptr = resp.valdat.dptr = NULL;
1520Sstevel@tonic-gate resp.keydat.dsize = resp.valdat.dsize = 0;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate * Do the get first request. If the rpc call failed, return with status
1560Sstevel@tonic-gate * from this point.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate (void) memset((char *)&resp, 0, sizeof (struct ypresp_key_val));
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate switch (clnt_call(pdomb->dom_client, YPPROC_FIRST,
1620Sstevel@tonic-gate (xdrproc_t)xdr_ypreq_nokey,
1630Sstevel@tonic-gate (char *)&req, (xdrproc_t)xdr_ypresp_key_val,
1640Sstevel@tonic-gate (char *)&resp, timeout)) {
1650Sstevel@tonic-gate case RPC_SUCCESS:
1660Sstevel@tonic-gate break;
1670Sstevel@tonic-gate case RPC_TIMEDOUT:
1680Sstevel@tonic-gate return (YPERR_YPSERV);
1690Sstevel@tonic-gate default:
1700Sstevel@tonic-gate return (YPERR_RPC);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* See if the request succeeded */
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (resp.status != YP_TRUE) {
1760Sstevel@tonic-gate retval = ypprot_err(resp.status);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /* Get some memory which the user can get rid of as he likes */
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate if (!retval) {
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if ((*key = malloc((size_t)resp.keydat.dsize + 2)) != NULL) {
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if ((*val = malloc(
1860Sstevel@tonic-gate (size_t)resp.valdat.dsize + 2)) == NULL) {
187132Srobinson free(*key);
1880Sstevel@tonic-gate retval = YPERR_RESRC;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate } else {
1920Sstevel@tonic-gate retval = YPERR_RESRC;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /* Copy the returned key and value byte strings into the new memory */
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (!retval) {
1990Sstevel@tonic-gate *keylen = (int)resp.keydat.dsize;
2000Sstevel@tonic-gate (void) memcpy(*key, resp.keydat.dptr,
2010Sstevel@tonic-gate (size_t)resp.keydat.dsize);
2020Sstevel@tonic-gate (*key)[resp.keydat.dsize] = '\n';
2030Sstevel@tonic-gate (*key)[resp.keydat.dsize + 1] = '\0';
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate *vallen = (int)resp.valdat.dsize;
2060Sstevel@tonic-gate (void) memcpy(*val, resp.valdat.dptr,
2070Sstevel@tonic-gate (size_t)resp.valdat.dsize);
2080Sstevel@tonic-gate (*val)[resp.valdat.dsize] = '\n';
2090Sstevel@tonic-gate (*val)[resp.valdat.dsize + 1] = '\0';
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate CLNT_FREERES(pdomb->dom_client,
2130Sstevel@tonic-gate (xdrproc_t)xdr_ypresp_key_val, (char *)&resp);
2140Sstevel@tonic-gate return (retval);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * This requests the yp server associated with a given domain to return the
2190Sstevel@tonic-gate * "next" key/value pair from the map data base. The input key should be
2200Sstevel@tonic-gate * one returned by ypclnt_first or a previous call to ypclnt_next. The
2210Sstevel@tonic-gate * returned key should be used as an input to the next call to ypclnt_next.
2220Sstevel@tonic-gate * This part does the parameter checking, and the do-until-success loop.
2230Sstevel@tonic-gate * if 'hardlookup' is set.
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate int
__yp_next_cflookup(char * domain,char * map,char * inkey,int inkeylen,char ** outkey,int * outkeylen,char ** val,int * vallen,int hardlookup)2260Sstevel@tonic-gate __yp_next_cflookup(
2270Sstevel@tonic-gate char *domain,
2280Sstevel@tonic-gate char *map,
2290Sstevel@tonic-gate char *inkey,
2300Sstevel@tonic-gate int inkeylen,
2310Sstevel@tonic-gate char **outkey, /* return: key array associated with val */
2320Sstevel@tonic-gate int *outkeylen, /* return: bytes in key */
2330Sstevel@tonic-gate char **val, /* return: value array associated with outkey */
2340Sstevel@tonic-gate int *vallen, /* return: bytes in val */
2350Sstevel@tonic-gate int hardlookup)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate size_t domlen;
2380Sstevel@tonic-gate size_t maplen;
2390Sstevel@tonic-gate struct dom_binding *pdomb;
2400Sstevel@tonic-gate int reason;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate
243132Srobinson if ((map == NULL) || (domain == NULL) || (inkey == NULL))
2440Sstevel@tonic-gate return (YPERR_BADARGS);
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate domlen = strlen(domain);
2470Sstevel@tonic-gate maplen = strlen(map);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
250132Srobinson (maplen == 0) || (maplen > YPMAXMAP))
2510Sstevel@tonic-gate return (YPERR_BADARGS);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate for (;;) {
254132Srobinson if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
2550Sstevel@tonic-gate return (reason);
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if (pdomb->dom_binding->ypbind_hi_vers == YPVERS) {
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate reason = donext(domain, map, inkey, inkeylen, pdomb,
2600Sstevel@tonic-gate _ypserv_timeout, outkey, outkeylen, val, vallen);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate __yp_rel_binding(pdomb);
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
2650Sstevel@tonic-gate reason == YPERR_BUSY /* as if */) {
2660Sstevel@tonic-gate yp_unbind(domain);
2670Sstevel@tonic-gate if (hardlookup)
268*1219Sraf (void) sleep(_ypsleeptime); /* retry */
269132Srobinson else
2700Sstevel@tonic-gate return (reason);
2710Sstevel@tonic-gate } else
2720Sstevel@tonic-gate break;
2730Sstevel@tonic-gate } else {
2740Sstevel@tonic-gate __yp_rel_binding(pdomb);
2750Sstevel@tonic-gate return (YPERR_VERS);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate return (reason);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate int
yp_next(char * domain,char * map,char * inkey,int inkeylen,char ** outkey,int * outkeylen,char ** val,int * vallen)2830Sstevel@tonic-gate yp_next(
2840Sstevel@tonic-gate char *domain,
2850Sstevel@tonic-gate char *map,
2860Sstevel@tonic-gate char *inkey,
2870Sstevel@tonic-gate int inkeylen,
2880Sstevel@tonic-gate char **outkey, /* return: key array associated with val */
2890Sstevel@tonic-gate int *outkeylen, /* return: bytes in key */
2900Sstevel@tonic-gate char **val, /* return: value array associated with outkey */
2910Sstevel@tonic-gate int *vallen) /* return: bytes in val */
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate /* traditional yp_next loops forever until success */
2940Sstevel@tonic-gate return (__yp_next_cflookup(domain, map, inkey, inkeylen, outkey,
2950Sstevel@tonic-gate outkeylen, val, vallen, 1));
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate * This part of the "get next" interface talks to ypserv.
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate static int
donext(domain,map,inkey,inkeylen,pdomb,timeout,outkey,outkeylen,val,vallen)3030Sstevel@tonic-gate donext(domain, map, inkey, inkeylen, pdomb, timeout, outkey, outkeylen,
3040Sstevel@tonic-gate val, vallen)
3050Sstevel@tonic-gate char *domain;
3060Sstevel@tonic-gate char *map;
3070Sstevel@tonic-gate char *inkey;
3080Sstevel@tonic-gate int inkeylen;
3090Sstevel@tonic-gate struct dom_binding *pdomb;
3100Sstevel@tonic-gate struct timeval timeout;
3110Sstevel@tonic-gate char **outkey; /* return: key array associated with val */
3120Sstevel@tonic-gate int *outkeylen; /* return: bytes in key */
3130Sstevel@tonic-gate char **val; /* return: value array associated with outkey */
3140Sstevel@tonic-gate int *vallen; /* return: bytes in val */
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate struct ypreq_key req;
3180Sstevel@tonic-gate struct ypresp_key_val resp;
3190Sstevel@tonic-gate unsigned int retval = 0;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate req.domain = domain;
3220Sstevel@tonic-gate req.map = map;
3230Sstevel@tonic-gate req.keydat.dptr = inkey;
3240Sstevel@tonic-gate req.keydat.dsize = inkeylen;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate resp.keydat.dptr = resp.valdat.dptr = NULL;
3270Sstevel@tonic-gate resp.keydat.dsize = resp.valdat.dsize = 0;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate * Do the get next request. If the rpc call failed, return with status
3310Sstevel@tonic-gate * from this point.
3320Sstevel@tonic-gate */
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate switch (clnt_call(pdomb->dom_client,
3350Sstevel@tonic-gate YPPROC_NEXT, (xdrproc_t)xdr_ypreq_key, (char *)&req,
3360Sstevel@tonic-gate (xdrproc_t)xdr_ypresp_key_val, (char *)&resp,
3370Sstevel@tonic-gate timeout)) {
3380Sstevel@tonic-gate case RPC_SUCCESS:
3390Sstevel@tonic-gate break;
3400Sstevel@tonic-gate case RPC_TIMEDOUT:
3410Sstevel@tonic-gate return (YPERR_YPSERV);
3420Sstevel@tonic-gate default:
3430Sstevel@tonic-gate return (YPERR_RPC);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /* See if the request succeeded */
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if (resp.status != YP_TRUE) {
3490Sstevel@tonic-gate retval = ypprot_err(resp.status);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /* Get some memory which the user can get rid of as he likes */
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (!retval) {
3550Sstevel@tonic-gate if ((*outkey = malloc((size_t)
3560Sstevel@tonic-gate resp.keydat.dsize + 2)) != NULL) {
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate if ((*val = malloc((size_t)
3590Sstevel@tonic-gate resp.valdat.dsize + 2)) == NULL) {
360132Srobinson free(*outkey);
3610Sstevel@tonic-gate retval = YPERR_RESRC;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate } else {
3650Sstevel@tonic-gate retval = YPERR_RESRC;
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate /* Copy the returned key and value byte strings into the new memory */
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (!retval) {
3720Sstevel@tonic-gate *outkeylen = (int)resp.keydat.dsize;
3730Sstevel@tonic-gate (void) memcpy(*outkey, resp.keydat.dptr,
3740Sstevel@tonic-gate (size_t)resp.keydat.dsize);
3750Sstevel@tonic-gate (*outkey)[resp.keydat.dsize] = '\n';
3760Sstevel@tonic-gate (*outkey)[resp.keydat.dsize + 1] = '\0';
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate *vallen = (int)resp.valdat.dsize;
3790Sstevel@tonic-gate (void) memcpy(*val, resp.valdat.dptr,
3800Sstevel@tonic-gate (size_t)resp.valdat.dsize);
3810Sstevel@tonic-gate (*val)[resp.valdat.dsize] = '\n';
3820Sstevel@tonic-gate (*val)[resp.valdat.dsize + 1] = '\0';
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate CLNT_FREERES(pdomb->dom_client, (xdrproc_t)xdr_ypresp_key_val,
3860Sstevel@tonic-gate (char *)&resp);
3870Sstevel@tonic-gate return (retval);
3880Sstevel@tonic-gate }
389