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