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