yp_match.c revision 1.2 1 /* $NetBSD: yp_match.c,v 1.2 1996/05/18 19:01:27 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Theo de Raadt.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char rcsid[] = "$NetBSD: yp_match.c,v 1.2 1996/05/18 19:01:27 jtc Exp $";
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <rpc/rpc.h>
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
43
44 #define YPMATCHCACHE
45
46 extern struct timeval _yplib_timeout;
47 extern char _yp_domain[];
48
49 #ifdef YPMATCHCACHE
50 int _yplib_cache = 5;
51
52 static struct ypmatch_ent {
53 struct ypmatch_ent *next;
54 char *map, *key;
55 char *val;
56 int keylen, vallen;
57 time_t expire_t;
58 } *ypmc;
59
60 static bool_t
61 ypmatch_add(map, key, keylen, val, vallen)
62 const char *map;
63 const char *key;
64 int keylen;
65 char *val;
66 int vallen;
67 {
68 struct ypmatch_ent *ep;
69 time_t t;
70
71 (void)time(&t);
72
73 for (ep = ypmc; ep; ep = ep->next)
74 if (ep->expire_t < t)
75 break;
76 if (ep == NULL) {
77 if ((ep = malloc(sizeof *ep)) == NULL)
78 return 0;
79 (void)memset(ep, 0, sizeof *ep);
80 if (ypmc)
81 ep->next = ypmc;
82 ypmc = ep;
83 }
84
85 if (ep->key) {
86 free(ep->key);
87 ep->key = NULL;
88 }
89 if (ep->val) {
90 free(ep->val);
91 ep->val = NULL;
92 }
93
94 if ((ep->key = malloc(keylen)) == NULL)
95 return 0;
96
97 if ((ep->val = malloc(vallen)) == NULL) {
98 free(ep->key);
99 ep->key = NULL;
100 return 0;
101 }
102
103 ep->keylen = keylen;
104 ep->vallen = vallen;
105
106 (void)memcpy(ep->key, key, ep->keylen);
107 (void)memcpy(ep->val, val, ep->vallen);
108
109 if (ep->map) {
110 if (strcmp(ep->map, map)) {
111 free(ep->map);
112 if ((ep->map = strdup(map)) == NULL)
113 return 0;
114 }
115 } else {
116 if ((ep->map = strdup(map)) == NULL)
117 return 0;
118 }
119
120 ep->expire_t = t + _yplib_cache;
121 return 1;
122 }
123
124 static bool_t
125 ypmatch_find(map, key, keylen, val, vallen)
126 const char *map;
127 const char *key;
128 int keylen;
129 const char **val;
130 int *vallen;
131 {
132 struct ypmatch_ent *ep;
133 time_t t;
134
135 if (ypmc == NULL)
136 return 0;
137
138 (void) time(&t);
139
140 for (ep = ypmc; ep; ep = ep->next) {
141 if (ep->keylen != keylen)
142 continue;
143 if (strcmp(ep->map, map))
144 continue;
145 if (memcmp(ep->key, key, keylen))
146 continue;
147 if (t > ep->expire_t)
148 continue;
149
150 *val = ep->val;
151 *vallen = ep->vallen;
152 return 1;
153 }
154 return 0;
155 }
156 #endif
157
158 int
159 yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
160 const char *indomain;
161 const char *inmap;
162 const char *inkey;
163 int inkeylen;
164 char **outval;
165 int *outvallen;
166 {
167 struct dom_binding *ysd;
168 struct ypresp_val yprv;
169 struct ypreq_key yprk;
170 int r;
171
172
173 if (indomain == NULL || *indomain == '\0'
174 || strlen(indomain) > YPMAXDOMAIN)
175 return YPERR_BADARGS;
176 if (inmap == NULL || *inmap == '\0'
177 || strlen(inmap) > YPMAXMAP)
178 return YPERR_BADARGS;
179 if (inkey == NULL || inkeylen == 0)
180 return YPERR_BADARGS;
181
182 *outval = NULL;
183 *outvallen = 0;
184
185 again:
186 if (_yp_dobind(indomain, &ysd) != 0)
187 return YPERR_DOMAIN;
188
189 #ifdef YPMATCHCACHE
190 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
191 inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
192 *outvallen = yprv.valdat.dsize;
193 if ((*outval = malloc(*outvallen + 1)) == NULL)
194 return YPERR_YPERR;
195 (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
196 (*outval)[*outvallen] = '\0';
197 return 0;
198 }
199 #endif
200
201 yprk.domain = indomain;
202 yprk.map = inmap;
203 yprk.keydat.dptr = (char *) inkey;
204 yprk.keydat.dsize = inkeylen;
205
206 memset(&yprv, 0, sizeof yprv);
207
208 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
209 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv,
210 _yplib_timeout);
211 if (r != RPC_SUCCESS) {
212 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
213 ysd->dom_vers = -1;
214 goto again;
215 }
216 if (!(r = ypprot_err(yprv.status))) {
217 *outvallen = yprv.valdat.dsize;
218 if ((*outval = malloc(*outvallen + 1)) == NULL)
219 return YPERR_YPERR;
220 (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
221 (*outval)[*outvallen] = '\0';
222 #ifdef YPMATCHCACHE
223 if (strcmp(_yp_domain, indomain) == 0)
224 if (!ypmatch_add(inmap, inkey, inkeylen,
225 *outval, *outvallen))
226 r = RPC_SYSTEMERROR;
227 #endif
228 }
229 xdr_free(xdr_ypresp_val, (char *) &yprv);
230 _yp_unbind(ysd);
231 return r;
232 }
233