1*4be2f99dSchristos /* $NetBSD: yp_match.c,v 1.20 2024/01/03 18:41:53 christos Exp $ */
25d8adb68Sjtc
35d8adb68Sjtc /*
45d8adb68Sjtc * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
55d8adb68Sjtc * All rights reserved.
65d8adb68Sjtc *
75d8adb68Sjtc * Redistribution and use in source and binary forms, with or without
85d8adb68Sjtc * modification, are permitted provided that the following conditions
95d8adb68Sjtc * are met:
105d8adb68Sjtc * 1. Redistributions of source code must retain the above copyright
115d8adb68Sjtc * notice, this list of conditions and the following disclaimer.
125d8adb68Sjtc * 2. Redistributions in binary form must reproduce the above copyright
135d8adb68Sjtc * notice, this list of conditions and the following disclaimer in the
145d8adb68Sjtc * documentation and/or other materials provided with the distribution.
155d8adb68Sjtc *
165d8adb68Sjtc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
175d8adb68Sjtc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
185d8adb68Sjtc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
195d8adb68Sjtc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
205d8adb68Sjtc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
215d8adb68Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
225d8adb68Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
235d8adb68Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
245d8adb68Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
255d8adb68Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
265d8adb68Sjtc * SUCH DAMAGE.
275d8adb68Sjtc */
285d8adb68Sjtc
29e7cc5503Schristos #include <sys/cdefs.h>
305d8adb68Sjtc #if defined(LIBC_SCCS) && !defined(lint)
31*4be2f99dSchristos __RCSID("$NetBSD: yp_match.c,v 1.20 2024/01/03 18:41:53 christos Exp $");
325d8adb68Sjtc #endif
335d8adb68Sjtc
3443fa6fe3Sjtc #include "namespace.h"
35b48252f3Slukem
36b48252f3Slukem #include <assert.h>
375d8adb68Sjtc #include <stdlib.h>
385d8adb68Sjtc #include <string.h>
398d6b3ab0Skleink #include <time.h>
40b48252f3Slukem
415d8adb68Sjtc #include <rpc/rpc.h>
425d8adb68Sjtc #include <rpcsvc/yp_prot.h>
435d8adb68Sjtc #include <rpcsvc/ypclnt.h>
44e7cc5503Schristos #include "local.h"
455d8adb68Sjtc
465d8adb68Sjtc #define YPMATCHCACHE
475d8adb68Sjtc
4843fa6fe3Sjtc #ifdef __weak_alias
4960549036Smycroft __weak_alias(yp_match,_yp_match)
5043fa6fe3Sjtc #endif
5143fa6fe3Sjtc
525d8adb68Sjtc #ifdef YPMATCHCACHE
535d8adb68Sjtc int _yplib_cache = 5;
545d8adb68Sjtc
555d8adb68Sjtc static struct ypmatch_ent {
565d8adb68Sjtc struct ypmatch_ent *next;
575d8adb68Sjtc char *map, *key;
585d8adb68Sjtc char *val;
595d8adb68Sjtc int keylen, vallen;
605d8adb68Sjtc time_t expire_t;
615d8adb68Sjtc } *ypmc;
625d8adb68Sjtc
638147d218Smatt static bool_t ypmatch_add(const char *, const char *, int, char *, int);
648147d218Smatt static bool_t ypmatch_find(const char *, const char *, int, const char **,
658147d218Smatt int *);
66e7cc5503Schristos
675d8adb68Sjtc static bool_t
ypmatch_add(const char * map,const char * key,int keylen,char * val,int vallen)688147d218Smatt ypmatch_add(const char *map, const char *key, int keylen, char *val, int vallen)
695d8adb68Sjtc {
705d8adb68Sjtc struct ypmatch_ent *ep;
715d8adb68Sjtc time_t t;
725d8adb68Sjtc
73b48252f3Slukem _DIAGASSERT(map != NULL);
74b48252f3Slukem _DIAGASSERT(key != NULL);
75b48252f3Slukem _DIAGASSERT(val != NULL);
76b48252f3Slukem
775d8adb68Sjtc (void)time(&t);
785d8adb68Sjtc
795d8adb68Sjtc for (ep = ypmc; ep; ep = ep->next)
805d8adb68Sjtc if (ep->expire_t < t)
815d8adb68Sjtc break;
825d8adb68Sjtc if (ep == NULL) {
835d8adb68Sjtc if ((ep = malloc(sizeof *ep)) == NULL)
845d8adb68Sjtc return 0;
855d8adb68Sjtc (void)memset(ep, 0, sizeof *ep);
865d8adb68Sjtc if (ypmc)
875d8adb68Sjtc ep->next = ypmc;
885d8adb68Sjtc ypmc = ep;
895d8adb68Sjtc }
905d8adb68Sjtc
915d8adb68Sjtc if (ep->key) {
925d8adb68Sjtc free(ep->key);
935d8adb68Sjtc ep->key = NULL;
945d8adb68Sjtc }
955d8adb68Sjtc if (ep->val) {
965d8adb68Sjtc free(ep->val);
975d8adb68Sjtc ep->val = NULL;
985d8adb68Sjtc }
995d8adb68Sjtc
100f4c14791Schristos if ((ep->key = malloc((size_t)keylen)) == NULL)
1015d8adb68Sjtc return 0;
1025d8adb68Sjtc
103f4c14791Schristos if ((ep->val = malloc((size_t)vallen)) == NULL) {
1045d8adb68Sjtc free(ep->key);
1055d8adb68Sjtc ep->key = NULL;
1065d8adb68Sjtc return 0;
1075d8adb68Sjtc }
1085d8adb68Sjtc
1095d8adb68Sjtc ep->keylen = keylen;
1105d8adb68Sjtc ep->vallen = vallen;
1115d8adb68Sjtc
112f4c14791Schristos (void)memcpy(ep->key, key, (size_t)ep->keylen);
113f4c14791Schristos (void)memcpy(ep->val, val, (size_t)ep->vallen);
1145d8adb68Sjtc
1155d8adb68Sjtc if (ep->map) {
1165d8adb68Sjtc if (strcmp(ep->map, map)) {
1175d8adb68Sjtc free(ep->map);
1185d8adb68Sjtc if ((ep->map = strdup(map)) == NULL)
1195d8adb68Sjtc return 0;
1205d8adb68Sjtc }
1215d8adb68Sjtc } else {
1225d8adb68Sjtc if ((ep->map = strdup(map)) == NULL)
1235d8adb68Sjtc return 0;
1245d8adb68Sjtc }
1255d8adb68Sjtc
1265d8adb68Sjtc ep->expire_t = t + _yplib_cache;
1275d8adb68Sjtc return 1;
1285d8adb68Sjtc }
1295d8adb68Sjtc
1305d8adb68Sjtc static bool_t
ypmatch_find(const char * map,const char * key,int keylen,const char ** val,int * vallen)1318147d218Smatt ypmatch_find(const char *map, const char *key, int keylen, const char **val,
1328147d218Smatt int *vallen)
1335d8adb68Sjtc {
1345d8adb68Sjtc struct ypmatch_ent *ep;
1355d8adb68Sjtc time_t t;
1365d8adb68Sjtc
137b48252f3Slukem _DIAGASSERT(map != NULL);
138b48252f3Slukem _DIAGASSERT(key != NULL);
139b48252f3Slukem _DIAGASSERT(val != NULL);
140b48252f3Slukem
1415d8adb68Sjtc if (ypmc == NULL)
1425d8adb68Sjtc return 0;
1435d8adb68Sjtc
1445d8adb68Sjtc (void) time(&t);
1455d8adb68Sjtc
1465d8adb68Sjtc for (ep = ypmc; ep; ep = ep->next) {
1475d8adb68Sjtc if (ep->keylen != keylen)
1485d8adb68Sjtc continue;
1495d8adb68Sjtc if (strcmp(ep->map, map))
1505d8adb68Sjtc continue;
151f4c14791Schristos if (memcmp(ep->key, key, (size_t)keylen))
1525d8adb68Sjtc continue;
1535d8adb68Sjtc if (t > ep->expire_t)
1545d8adb68Sjtc continue;
1555d8adb68Sjtc
1565d8adb68Sjtc *val = ep->val;
1575d8adb68Sjtc *vallen = ep->vallen;
1585d8adb68Sjtc return 1;
1595d8adb68Sjtc }
1605d8adb68Sjtc return 0;
1615d8adb68Sjtc }
1625d8adb68Sjtc #endif
1635d8adb68Sjtc
1645d8adb68Sjtc int
yp_match(const char * indomain,const char * inmap,const char * inkey,int inkeylen,char ** outval,int * outvallen)1658147d218Smatt yp_match(const char *indomain, const char *inmap, const char *inkey,
1668147d218Smatt int inkeylen, char **outval, int *outvallen)
1675d8adb68Sjtc {
1685d8adb68Sjtc struct dom_binding *ysd;
1695d8adb68Sjtc struct ypresp_val yprv;
1705d8adb68Sjtc struct ypreq_key yprk;
171409a9590Schristos int r, nerrs = 0;
1725d8adb68Sjtc
173b48252f3Slukem if (outval == NULL || outvallen == NULL)
174301e6e6cSlukem return YPERR_BADARGS;
175301e6e6cSlukem *outval = NULL;
176301e6e6cSlukem *outvallen = 0;
1770724069fSjtc
17842736edbSlukem if (_yp_invalid_domain(indomain))
1790724069fSjtc return YPERR_BADARGS;
1800724069fSjtc if (inmap == NULL || *inmap == '\0'
1810724069fSjtc || strlen(inmap) > YPMAXMAP)
1820724069fSjtc return YPERR_BADARGS;
1830724069fSjtc if (inkey == NULL || inkeylen == 0)
1840724069fSjtc return YPERR_BADARGS;
1850724069fSjtc
1865d8adb68Sjtc again:
1875d8adb68Sjtc if (_yp_dobind(indomain, &ysd) != 0)
1885d8adb68Sjtc return YPERR_DOMAIN;
1895d8adb68Sjtc
1905d8adb68Sjtc #ifdef YPMATCHCACHE
1915d8adb68Sjtc if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
1925d8adb68Sjtc inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
1935d8adb68Sjtc *outvallen = yprv.valdat.dsize;
194f4c14791Schristos if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
1955d8adb68Sjtc return YPERR_YPERR;
196f4c14791Schristos (void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
1975d8adb68Sjtc (*outval)[*outvallen] = '\0';
1985d8adb68Sjtc return 0;
1995d8adb68Sjtc }
2005d8adb68Sjtc #endif
2015d8adb68Sjtc
2025d8adb68Sjtc yprk.domain = indomain;
2035d8adb68Sjtc yprk.map = inmap;
20403256c6eSchristos yprk.keydat.dptr = __UNCONST(inkey);
2055d8adb68Sjtc yprk.keydat.dsize = inkeylen;
2065d8adb68Sjtc
2075d8adb68Sjtc memset(&yprv, 0, sizeof yprv);
2085d8adb68Sjtc
209473ea2c4Schristos r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_MATCH,
210b2a14ab2Schristos (xdrproc_t)xdr_ypreq_key, &yprk,
211b2a14ab2Schristos (xdrproc_t)xdr_ypresp_val, &yprv,
2120724069fSjtc _yplib_timeout);
2135d8adb68Sjtc if (r != RPC_SUCCESS) {
2142b01a8adSchristos if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
2155d8adb68Sjtc clnt_perror(ysd->dom_client, "yp_match: clnt_call");
216409a9590Schristos nerrs = 0;
217409a9590Schristos }
2182b01a8adSchristos else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries) {
2192b01a8adSchristos return YPERR_YPSERV;
2202b01a8adSchristos }
2215d8adb68Sjtc ysd->dom_vers = -1;
2225d8adb68Sjtc goto again;
2235d8adb68Sjtc }
2245d8adb68Sjtc if (!(r = ypprot_err(yprv.status))) {
2255d8adb68Sjtc *outvallen = yprv.valdat.dsize;
226f4c14791Schristos if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
2275d8adb68Sjtc return YPERR_YPERR;
228f4c14791Schristos (void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
2295d8adb68Sjtc (*outval)[*outvallen] = '\0';
2305d8adb68Sjtc #ifdef YPMATCHCACHE
2315d8adb68Sjtc if (strcmp(_yp_domain, indomain) == 0)
2325d8adb68Sjtc if (!ypmatch_add(inmap, inkey, inkeylen,
2335d8adb68Sjtc *outval, *outvallen))
23422041060Slukem r = YPERR_RESRC;
2355d8adb68Sjtc #endif
2365d8adb68Sjtc }
237b2a14ab2Schristos xdr_free((xdrproc_t)xdr_ypresp_val, (char *)(void *)&yprv);
23843fa6fe3Sjtc __yp_unbind(ysd);
239db4fd8d5Slukem if (r != 0) {
240db4fd8d5Slukem if (*outval) {
241db4fd8d5Slukem free(*outval);
242db4fd8d5Slukem *outval = NULL;
243db4fd8d5Slukem }
244db4fd8d5Slukem }
2455d8adb68Sjtc return r;
2465d8adb68Sjtc }
247