Home | History | Annotate | Line # | Download | only in yp
yp_match.c revision 1.11
      1 /*	$NetBSD: yp_match.c,v 1.11 1998/11/15 17:10:31 christos 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 #include <sys/cdefs.h>
     35 #if defined(LIBC_SCCS) && !defined(lint)
     36 __RCSID("$NetBSD: yp_match.c,v 1.11 1998/11/15 17:10:31 christos Exp $");
     37 #endif
     38 
     39 #include "namespace.h"
     40 #include <stdlib.h>
     41 #include <string.h>
     42 #include <time.h>
     43 #include <rpc/rpc.h>
     44 #include <rpcsvc/yp_prot.h>
     45 #include <rpcsvc/ypclnt.h>
     46 #include "local.h"
     47 
     48 #define YPMATCHCACHE
     49 
     50 extern struct timeval _yplib_timeout;
     51 extern int _yplib_nerrs;
     52 extern char _yp_domain[];
     53 
     54 #ifdef __weak_alias
     55 __weak_alias(yp_match,_yp_match);
     56 #endif
     57 
     58 #ifdef YPMATCHCACHE
     59 int _yplib_cache = 5;
     60 
     61 static struct ypmatch_ent {
     62 	struct ypmatch_ent 	*next;
     63 	char     		*map, *key;
     64 	char           		*val;
     65 	int             	 keylen, vallen;
     66 	time_t          	 expire_t;
     67 } *ypmc;
     68 
     69 static bool_t ypmatch_add __P((const char *, const char *, int, char *, int));
     70 static bool_t ypmatch_find __P((const char *, const char *, int, const char **,
     71     int *));
     72 
     73 static bool_t
     74 ypmatch_add(map, key, keylen, val, vallen)
     75 	const char     *map;
     76 	const char     *key;
     77 	int             keylen;
     78 	char           *val;
     79 	int             vallen;
     80 {
     81 	struct ypmatch_ent *ep;
     82 	time_t t;
     83 
     84 	(void)time(&t);
     85 
     86 	for (ep = ypmc; ep; ep = ep->next)
     87 		if (ep->expire_t < t)
     88 			break;
     89 	if (ep == NULL) {
     90 		if ((ep = malloc(sizeof *ep)) == NULL)
     91 			return 0;
     92 		(void)memset(ep, 0, sizeof *ep);
     93 		if (ypmc)
     94 			ep->next = ypmc;
     95 		ypmc = ep;
     96 	}
     97 
     98 	if (ep->key) {
     99 		free(ep->key);
    100 		ep->key = NULL;
    101 	}
    102 	if (ep->val) {
    103 		free(ep->val);
    104 		ep->val = NULL;
    105 	}
    106 
    107 	if ((ep->key = malloc((size_t)keylen)) == NULL)
    108 		return 0;
    109 
    110 	if ((ep->val = malloc((size_t)vallen)) == NULL) {
    111 		free(ep->key);
    112 		ep->key = NULL;
    113 		return 0;
    114 	}
    115 
    116 	ep->keylen = keylen;
    117 	ep->vallen = vallen;
    118 
    119 	(void)memcpy(ep->key, key, (size_t)ep->keylen);
    120 	(void)memcpy(ep->val, val, (size_t)ep->vallen);
    121 
    122 	if (ep->map) {
    123 		if (strcmp(ep->map, map)) {
    124 			free(ep->map);
    125 			if ((ep->map = strdup(map)) == NULL)
    126 				return 0;
    127 		}
    128 	} else {
    129 		if ((ep->map = strdup(map)) == NULL)
    130 			return 0;
    131 	}
    132 
    133 	ep->expire_t = t + _yplib_cache;
    134 	return 1;
    135 }
    136 
    137 static bool_t
    138 ypmatch_find(map, key, keylen, val, vallen)
    139 	const char     *map;
    140 	const char     *key;
    141 	int             keylen;
    142 	const char    **val;
    143 	int            *vallen;
    144 {
    145 	struct ypmatch_ent *ep;
    146 	time_t          t;
    147 
    148 	if (ypmc == NULL)
    149 		return 0;
    150 
    151 	(void) time(&t);
    152 
    153 	for (ep = ypmc; ep; ep = ep->next) {
    154 		if (ep->keylen != keylen)
    155 			continue;
    156 		if (strcmp(ep->map, map))
    157 			continue;
    158 		if (memcmp(ep->key, key, (size_t)keylen))
    159 			continue;
    160 		if (t > ep->expire_t)
    161 			continue;
    162 
    163 		*val = ep->val;
    164 		*vallen = ep->vallen;
    165 		return 1;
    166 	}
    167 	return 0;
    168 }
    169 #endif
    170 
    171 int
    172 yp_match(indomain, inmap, inkey, inkeylen, outval, outvallen)
    173 	const char     *indomain;
    174 	const char     *inmap;
    175 	const char     *inkey;
    176 	int             inkeylen;
    177 	char          **outval;
    178 	int            *outvallen;
    179 {
    180 	struct dom_binding *ysd;
    181 	struct ypresp_val yprv;
    182 	struct ypreq_key yprk;
    183 	int r, nerrs = 0;
    184 
    185 	if (outval == NULL)
    186 		return YPERR_BADARGS;
    187 	*outval = NULL;
    188 	*outvallen = 0;
    189 
    190 	if (_yp_invalid_domain(indomain))
    191 		return YPERR_BADARGS;
    192 	if (inmap == NULL || *inmap == '\0'
    193 	    || strlen(inmap) > YPMAXMAP)
    194 		return YPERR_BADARGS;
    195 	if (inkey == NULL || inkeylen == 0)
    196 		return YPERR_BADARGS;
    197 
    198 again:
    199 	if (_yp_dobind(indomain, &ysd) != 0)
    200 		return YPERR_DOMAIN;
    201 
    202 #ifdef YPMATCHCACHE
    203 	if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey,
    204 			 inkeylen, &yprv.valdat.dptr, &yprv.valdat.dsize)) {
    205 		*outvallen = yprv.valdat.dsize;
    206 		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
    207 			return YPERR_YPERR;
    208 		(void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
    209 		(*outval)[*outvallen] = '\0';
    210 		return 0;
    211 	}
    212 #endif
    213 
    214 	yprk.domain = indomain;
    215 	yprk.map = inmap;
    216 	/* LINTED const castaway */
    217 	yprk.keydat.dptr = (char *)(void *)inkey;
    218 	yprk.keydat.dsize = inkeylen;
    219 
    220 	memset(&yprv, 0, sizeof yprv);
    221 
    222 	r = clnt_call(ysd->dom_client, YPPROC_MATCH,
    223 		      xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv,
    224 		      _yplib_timeout);
    225 	if (r != RPC_SUCCESS) {
    226 		if (++nerrs == _yplib_nerrs) {
    227 			clnt_perror(ysd->dom_client, "yp_match: clnt_call");
    228 			nerrs = 0;
    229 		}
    230 		ysd->dom_vers = -1;
    231 		goto again;
    232 	}
    233 	if (!(r = ypprot_err(yprv.status))) {
    234 		*outvallen = yprv.valdat.dsize;
    235 		if ((*outval = malloc((size_t)(*outvallen + 1))) == NULL)
    236 			return YPERR_YPERR;
    237 		(void)memcpy(*outval, yprv.valdat.dptr, (size_t)*outvallen);
    238 		(*outval)[*outvallen] = '\0';
    239 #ifdef YPMATCHCACHE
    240 		if (strcmp(_yp_domain, indomain) == 0)
    241 			if (!ypmatch_add(inmap, inkey, inkeylen,
    242 					 *outval, *outvallen))
    243 				r = YPERR_RESRC;
    244 #endif
    245 	}
    246 	xdr_free(xdr_ypresp_val, (char *)(void *)&yprv);
    247 	__yp_unbind(ysd);
    248 	if (r != 0) {
    249 		if (*outval) {
    250 			free(*outval);
    251 			*outval = NULL;
    252 		}
    253 	}
    254 	return r;
    255 }
    256