1/*	$NetBSD: yp_match.c,v 1.20 2024/01/03 18:41:53 christos Exp $	 */
2
3/*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30#if defined(LIBC_SCCS) && !defined(lint)
31__RCSID("$NetBSD: yp_match.c,v 1.20 2024/01/03 18:41:53 christos Exp $");
32#endif
33
34#include "namespace.h"
35
36#include <assert.h>
37#include <stdlib.h>
38#include <string.h>
39#include <time.h>
40
41#include <rpc/rpc.h>
42#include <rpcsvc/yp_prot.h>
43#include <rpcsvc/ypclnt.h>
44#include "local.h"
45
46#define YPMATCHCACHE
47
48#ifdef __weak_alias
49__weak_alias(yp_match,_yp_match)
50#endif
51
52#ifdef YPMATCHCACHE
53int _yplib_cache = 5;
54
55static struct ypmatch_ent {
56	struct ypmatch_ent 	*next;
57	char     		*map, *key;
58	char           		*val;
59	int             	 keylen, vallen;
60	time_t          	 expire_t;
61} *ypmc;
62
63static bool_t ypmatch_add(const char *, const char *, int, char *, int);
64static bool_t ypmatch_find(const char *, const char *, int, const char **,
65    int *);
66
67static bool_t
68ypmatch_add(const char *map, const char *key, int keylen, char *val, int vallen)
69{
70	struct ypmatch_ent *ep;
71	time_t t;
72
73	_DIAGASSERT(map != NULL);
74	_DIAGASSERT(key != NULL);
75	_DIAGASSERT(val != NULL);
76
77	(void)time(&t);
78
79	for (ep = ypmc; ep; ep = ep->next)
80		if (ep->expire_t < t)
81			break;
82	if (ep == NULL) {
83		if ((ep = malloc(sizeof *ep)) == NULL)
84			return 0;
85		(void)memset(ep, 0, sizeof *ep);
86		if (ypmc)
87			ep->next = ypmc;
88		ypmc = ep;
89	}
90
91	if (ep->key) {
92		free(ep->key);
93		ep->key = NULL;
94	}
95	if (ep->val) {
96		free(ep->val);
97		ep->val = NULL;
98	}
99
100	if ((ep->key = malloc((size_t)keylen)) == NULL)
101		return 0;
102
103	if ((ep->val = malloc((size_t)vallen)) == NULL) {
104		free(ep->key);
105		ep->key = NULL;
106		return 0;
107	}
108
109	ep->keylen = keylen;
110	ep->vallen = vallen;
111
112	(void)memcpy(ep->key, key, (size_t)ep->keylen);
113	(void)memcpy(ep->val, val, (size_t)ep->vallen);
114
115	if (ep->map) {
116		if (strcmp(ep->map, map)) {
117			free(ep->map);
118			if ((ep->map = strdup(map)) == NULL)
119				return 0;
120		}
121	} else {
122		if ((ep->map = strdup(map)) == NULL)
123			return 0;
124	}
125
126	ep->expire_t = t + _yplib_cache;
127	return 1;
128}
129
130static bool_t
131ypmatch_find(const char *map, const char *key, int keylen, const char **val,
132	int *vallen)
133{
134	struct ypmatch_ent *ep;
135	time_t          t;
136
137	_DIAGASSERT(map != NULL);
138	_DIAGASSERT(key != NULL);
139	_DIAGASSERT(val != NULL);
140
141	if (ypmc == NULL)
142		return 0;
143
144	(void) time(&t);
145
146	for (ep = ypmc; ep; ep = ep->next) {
147		if (ep->keylen != keylen)
148			continue;
149		if (strcmp(ep->map, map))
150			continue;
151		if (memcmp(ep->key, key, (size_t)keylen))
152			continue;
153		if (t > ep->expire_t)
154			continue;
155
156		*val = ep->val;
157		*vallen = ep->vallen;
158		return 1;
159	}
160	return 0;
161}
162#endif
163
164int
165yp_match(const char *indomain, const char *inmap, const char *inkey,
166	int inkeylen, char **outval, int *outvallen)
167{
168	struct dom_binding *ysd;
169	struct ypresp_val yprv;
170	struct ypreq_key yprk;
171	int r, nerrs = 0;
172
173	if (outval == NULL || outvallen == NULL)
174		return YPERR_BADARGS;
175	*outval = NULL;
176	*outvallen = 0;
177
178	if (_yp_invalid_domain(indomain))
179		return YPERR_BADARGS;
180	if (inmap == NULL || *inmap == '\0'
181	    || strlen(inmap) > YPMAXMAP)
182		return YPERR_BADARGS;
183	if (inkey == NULL || inkeylen == 0)
184		return YPERR_BADARGS;
185
186again:
187	if (_yp_dobind(indomain, &ysd) != 0)
188		return YPERR_DOMAIN;
189
190#ifdef YPMATCHCACHE
191	if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
192			 inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
193		*outvallen = yprv.valdat.dsize;
194		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
195			return YPERR_YPERR;
196		(void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
197		(*outval)[*outvallen] = '\0';
198		return 0;
199	}
200#endif
201
202	yprk.domain = indomain;
203	yprk.map = inmap;
204	yprk.keydat.dptr = __UNCONST(inkey);
205	yprk.keydat.dsize = inkeylen;
206
207	memset(&yprv, 0, sizeof yprv);
208
209	r = clnt_call(ysd->dom_client, (rpcproc_t)YPPROC_MATCH,
210		      (xdrproc_t)xdr_ypreq_key, &yprk,
211		      (xdrproc_t)xdr_ypresp_val, &yprv,
212		      _yplib_timeout);
213	if (r != RPC_SUCCESS) {
214		if (_yplib_bindtries <= 0 && ++nerrs == _yplib_nerrs) {
215			clnt_perror(ysd->dom_client, "yp_match: clnt_call");
216			nerrs = 0;
217		}
218		else if (_yplib_bindtries > 0 && ++nerrs == _yplib_bindtries) {
219			return YPERR_YPSERV;
220		}
221		ysd->dom_vers = -1;
222		goto again;
223	}
224	if (!(r = ypprot_err(yprv.status))) {
225		*outvallen = yprv.valdat.dsize;
226		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
227			return YPERR_YPERR;
228		(void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
229		(*outval)[*outvallen] = '\0';
230#ifdef YPMATCHCACHE
231		if (strcmp(_yp_domain, indomain) == 0)
232			if (!ypmatch_add(inmap, inkey, inkeylen,
233					 *outval, *outvallen))
234				r = YPERR_RESRC;
235#endif
236	}
237	xdr_free((xdrproc_t)xdr_ypresp_val, (char *)(void *)&yprv);
238	__yp_unbind(ysd);
239	if (r != 0) {
240		if (*outval) {
241			free(*outval);
242			*outval = NULL;
243		}
244	}
245	return r;
246}
247