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