yp_match.c revision 1.1 1 /* $NetBSD: yp_match.c,v 1.1 1996/05/15 05:27:52 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.1 1996/05/15 05:27:52 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 char _yp_domain[];
47 extern int _yplib_timeout;
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 timeval tv;
170 struct ypreq_key yprk;
171 int r;
172
173 *outval = NULL;
174 *outvallen = 0;
175
176 again:
177 if (_yp_dobind(indomain, &ysd) != 0)
178 return YPERR_DOMAIN;
179
180 #ifdef YPMATCHCACHE
181 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
182 inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
183 *outvallen = yprv.valdat.dsize;
184 if ((*outval = malloc(*outvallen + 1)) == NULL)
185 return YPERR_YPERR;
186 (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
187 (*outval)[*outvallen] = '\0';
188 return 0;
189 }
190 #endif
191
192 tv.tv_sec = _yplib_timeout;
193 tv.tv_usec = 0;
194
195 yprk.domain = indomain;
196 yprk.map = inmap;
197 yprk.keydat.dptr = (char *) inkey;
198 yprk.keydat.dsize = inkeylen;
199
200 memset(&yprv, 0, sizeof yprv);
201
202 r = clnt_call(ysd->dom_client, YPPROC_MATCH,
203 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv);
204 if (r != RPC_SUCCESS) {
205 clnt_perror(ysd->dom_client, "yp_match: clnt_call");
206 ysd->dom_vers = -1;
207 goto again;
208 }
209 if (!(r = ypprot_err(yprv.status))) {
210 *outvallen = yprv.valdat.dsize;
211 if ((*outval = malloc(*outvallen + 1)) == NULL)
212 return YPERR_YPERR;
213 (void)memcpy(*outval, yprv.valdat.dptr, *outvallen);
214 (*outval)[*outvallen] = '\0';
215 #ifdef YPMATCHCACHE
216 if (strcmp(_yp_domain, indomain) == 0)
217 if (!ypmatch_add(inmap, inkey, inkeylen,
218 *outval, *outvallen))
219 r = RPC_SYSTEMERROR;
220 #endif
221 }
222 xdr_free(xdr_ypresp_val, (char *) &yprv);
223 _yp_unbind(ysd);
224 return r;
225 }
226