1*4be2f99dSchristos /* $NetBSD: yp_first.c,v 1.17 2024/01/03 18:41:53 christos Exp $ */
22039c263Sjtc
32039c263Sjtc /*
42039c263Sjtc * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
52039c263Sjtc * All rights reserved.
62039c263Sjtc *
72039c263Sjtc * Redistribution and use in source and binary forms, with or without
82039c263Sjtc * modification, are permitted provided that the following conditions
92039c263Sjtc * are met:
102039c263Sjtc * 1. Redistributions of source code must retain the above copyright
112039c263Sjtc * notice, this list of conditions and the following disclaimer.
122039c263Sjtc * 2. Redistributions in binary form must reproduce the above copyright
132039c263Sjtc * notice, this list of conditions and the following disclaimer in the
142039c263Sjtc * documentation and/or other materials provided with the distribution.
152039c263Sjtc *
162039c263Sjtc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
172039c263Sjtc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
182039c263Sjtc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192039c263Sjtc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
202039c263Sjtc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212039c263Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222039c263Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232039c263Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242039c263Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252039c263Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262039c263Sjtc * SUCH DAMAGE.
272039c263Sjtc */
282039c263Sjtc
29e7cc5503Schristos #include <sys/cdefs.h>
302039c263Sjtc #if defined(LIBC_SCCS) && !defined(lint)
31*4be2f99dSchristos __RCSID("$NetBSD: yp_first.c,v 1.17 2024/01/03 18:41:53 christos Exp $");
322039c263Sjtc #endif
332039c263Sjtc
3443fa6fe3Sjtc #include "namespace.h"
352039c263Sjtc #include <stdlib.h>
3664331ae1Scgd #include <string.h>
372039c263Sjtc #include <rpc/rpc.h>
382039c263Sjtc #include <rpcsvc/yp_prot.h>
392039c263Sjtc #include <rpcsvc/ypclnt.h>
40e7cc5503Schristos #include "local.h"
412039c263Sjtc
4243fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(yp_first,_yp_first)4360549036Smycroft __weak_alias(yp_first,_yp_first)
4460549036Smycroft __weak_alias(yp_next,_yp_next)
4543fa6fe3Sjtc #endif
4643fa6fe3Sjtc
472039c263Sjtc int
489e66e6d7Sabs yp_first(const char *indomain, const char *inmap, char **outkey,
499e66e6d7Sabs int *outkeylen, char **outval, int *outvallen)
502039c263Sjtc {
512039c263Sjtc struct ypresp_key_val yprkv;
522039c263Sjtc struct ypreq_nokey yprnk;
532039c263Sjtc struct dom_binding *ysd;
54409a9590Schristos int r, nerrs = 0;
552039c263Sjtc
56b035f7abSlukem if (outkey == NULL || outkeylen == NULL || \
57b035f7abSlukem outval == NULL || outvallen == NULL)
58301e6e6cSlukem return YPERR_BADARGS;
59301e6e6cSlukem *outkey = *outval = NULL;
60301e6e6cSlukem *outkeylen = *outvallen = 0;
6142736edbSlukem if (_yp_invalid_domain(indomain))
622039c263Sjtc return YPERR_BADARGS;
632039c263Sjtc if (inmap == NULL || *inmap == '\0'
642039c263Sjtc || strlen(inmap) > YPMAXMAP)
652039c263Sjtc return YPERR_BADARGS;
662039c263Sjtc
672039c263Sjtc again:
682039c263Sjtc if (_yp_dobind(indomain, &ysd) != 0)
692039c263Sjtc return YPERR_DOMAIN;
702039c263Sjtc
712039c263Sjtc yprnk.domain = indomain;
722039c263Sjtc yprnk.map = inmap;
732039c263Sjtc (void)memset(&yprkv, 0, sizeof yprkv);
742039c263Sjtc
75473ea2c4Schristos r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_FIRST,
76473ea2c4Schristos (xdrproc_t)xdr_ypreq_nokey,
77b2a14ab2Schristos &yprnk, (xdrproc_t)xdr_ypresp_key_val, &yprkv, _yplib_timeout);
782039c263Sjtc if (r != RPC_SUCCESS) {
792b01a8adSchristos if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
802039c263Sjtc clnt_perror(ysd->dom_client, "yp_first: clnt_call");
81409a9590Schristos nerrs = 0;
822b01a8adSchristos } else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
832b01a8adSchristos return YPERR_YPSERV;
842039c263Sjtc ysd->dom_vers = -1;
852039c263Sjtc goto again;
862039c263Sjtc }
872039c263Sjtc if (!(r = ypprot_err(yprkv.status))) {
882039c263Sjtc *outkeylen = yprkv.keydat.dsize;
89f4c14791Schristos if ((*outkey = malloc((size_t)(*outkeylen + 1))) == NULL)
902039c263Sjtc r = YPERR_RESRC;
912039c263Sjtc else {
92f4c14791Schristos (void)memcpy(*outkey, yprkv.keydat.dptr,
93f4c14791Schristos (size_t)*outkeylen);
942039c263Sjtc (*outkey)[*outkeylen] = '\0';
952039c263Sjtc }
962039c263Sjtc *outvallen = yprkv.valdat.dsize;
97f4c14791Schristos if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
982039c263Sjtc r = YPERR_RESRC;
992039c263Sjtc else {
100f4c14791Schristos (void)memcpy(*outval, yprkv.valdat.dptr,
101f4c14791Schristos (size_t)*outvallen);
1022039c263Sjtc (*outval)[*outvallen] = '\0';
1032039c263Sjtc }
1042039c263Sjtc }
105b2a14ab2Schristos xdr_free((xdrproc_t)xdr_ypresp_key_val, (char *)(void *)&yprkv);
10643fa6fe3Sjtc __yp_unbind(ysd);
107db4fd8d5Slukem if (r != 0) {
108db4fd8d5Slukem if (*outkey) {
109db4fd8d5Slukem free(*outkey);
110db4fd8d5Slukem *outkey = NULL;
111db4fd8d5Slukem }
112db4fd8d5Slukem if (*outval) {
113db4fd8d5Slukem free(*outval);
114db4fd8d5Slukem *outval = NULL;
115db4fd8d5Slukem }
116db4fd8d5Slukem }
1172039c263Sjtc return r;
1182039c263Sjtc }
1192039c263Sjtc
1202039c263Sjtc int
yp_next(const char * indomain,const char * inmap,const char * inkey,int inkeylen,char ** outkey,int * outkeylen,char ** outval,int * outvallen)1219e66e6d7Sabs yp_next(const char *indomain, const char *inmap, const char *inkey,
1229e66e6d7Sabs int inkeylen, char **outkey, int *outkeylen, char **outval, int *outvallen)
1232039c263Sjtc {
1242039c263Sjtc struct ypresp_key_val yprkv;
1252039c263Sjtc struct ypreq_key yprk;
1262039c263Sjtc struct dom_binding *ysd;
127409a9590Schristos int r, nerrs = 0;
1282039c263Sjtc
129b035f7abSlukem if (outkey == NULL || outkeylen == NULL || \
130b035f7abSlukem outval == NULL || outvallen == NULL || \
131b035f7abSlukem inkey == NULL)
132301e6e6cSlukem return YPERR_BADARGS;
133301e6e6cSlukem *outkey = *outval = NULL;
134301e6e6cSlukem *outkeylen = *outvallen = 0;
135301e6e6cSlukem
13642736edbSlukem if (_yp_invalid_domain(indomain))
1372039c263Sjtc return YPERR_BADARGS;
1382039c263Sjtc if (inmap == NULL || *inmap == '\0'
1392039c263Sjtc || strlen(inmap) > YPMAXMAP)
1402039c263Sjtc return YPERR_BADARGS;
1412039c263Sjtc
1422039c263Sjtc again:
1432039c263Sjtc if (_yp_dobind(indomain, &ysd) != 0)
1442039c263Sjtc return YPERR_DOMAIN;
1452039c263Sjtc
1462039c263Sjtc yprk.domain = indomain;
1472039c263Sjtc yprk.map = inmap;
1482039c263Sjtc yprk.keydat.dptr = inkey;
1492039c263Sjtc yprk.keydat.dsize = inkeylen;
1502039c263Sjtc (void)memset(&yprkv, 0, sizeof yprkv);
1512039c263Sjtc
152473ea2c4Schristos r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_NEXT,
153473ea2c4Schristos (xdrproc_t)xdr_ypreq_key,
154b2a14ab2Schristos &yprk, (xdrproc_t)xdr_ypresp_key_val, &yprkv, _yplib_timeout);
1552039c263Sjtc if (r != RPC_SUCCESS) {
1562b01a8adSchristos if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
1572039c263Sjtc clnt_perror(ysd->dom_client, "yp_next: clnt_call");
158409a9590Schristos nerrs = 0;
1592b01a8adSchristos } else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries)
1602b01a8adSchristos return YPERR_YPSERV;
1612039c263Sjtc ysd->dom_vers = -1;
1622039c263Sjtc goto again;
1632039c263Sjtc }
1642039c263Sjtc if (!(r = ypprot_err(yprkv.status))) {
1652039c263Sjtc *outkeylen = yprkv.keydat.dsize;
166f4c14791Schristos if ((*outkey = malloc((size_t)(*outkeylen + 1))) == NULL)
1672039c263Sjtc r = YPERR_RESRC;
1682039c263Sjtc else {
169f4c14791Schristos (void)memcpy(*outkey, yprkv.keydat.dptr,
170f4c14791Schristos (size_t)*outkeylen);
1712039c263Sjtc (*outkey)[*outkeylen] = '\0';
1722039c263Sjtc }
1732039c263Sjtc *outvallen = yprkv.valdat.dsize;
174f4c14791Schristos if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
1752039c263Sjtc r = YPERR_RESRC;
1762039c263Sjtc else {
177f4c14791Schristos (void)memcpy(*outval, yprkv.valdat.dptr,
178f4c14791Schristos (size_t)*outvallen);
1792039c263Sjtc (*outval)[*outvallen] = '\0';
1802039c263Sjtc }
1812039c263Sjtc }
182b2a14ab2Schristos xdr_free((xdrproc_t)xdr_ypresp_key_val, (char *)(void *)&yprkv);
18343fa6fe3Sjtc __yp_unbind(ysd);
184db4fd8d5Slukem if (r != 0) {
185db4fd8d5Slukem if (*outkey) {
186db4fd8d5Slukem free(*outkey);
187db4fd8d5Slukem *outkey = NULL;
188db4fd8d5Slukem }
189db4fd8d5Slukem if (*outval) {
190db4fd8d5Slukem free(*outval);
191db4fd8d5Slukem *outval = NULL;
192db4fd8d5Slukem }
193db4fd8d5Slukem }
1942039c263Sjtc return r;
1952039c263Sjtc }
196