xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_match.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved   */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
347c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of
357c478bd9Sstevel@tonic-gate  * California.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
38e8031f0aSraf #include "mt.h"
3961961e0fSrobinson #include <stdlib.h>
4061961e0fSrobinson #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include "../rpc/rpc_mt.h"
427c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include "yp_b.h"
457c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
467c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
477c478bd9Sstevel@tonic-gate #include <malloc.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <sys/time.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate extern int __yp_dobind_cflookup(char *, struct dom_binding **, int);
527c478bd9Sstevel@tonic-gate extern int __yp_dobind_rsvdport_cflookup(char *, struct dom_binding **, int);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static int domatch(char *, char *, char *, int, struct dom_binding *,
557c478bd9Sstevel@tonic-gate     struct timeval *, char **, int *);
567c478bd9Sstevel@tonic-gate int yp_match_rsvdport();
577c478bd9Sstevel@tonic-gate int yp_match_rsvdport_cflookup();
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct cache {
607c478bd9Sstevel@tonic-gate 	struct cache *next;
617c478bd9Sstevel@tonic-gate 	unsigned int birth;
627c478bd9Sstevel@tonic-gate 	char *domain;
637c478bd9Sstevel@tonic-gate 	char *map;
647c478bd9Sstevel@tonic-gate 	char *key;
657c478bd9Sstevel@tonic-gate 	int  keylen;
667c478bd9Sstevel@tonic-gate 	char *val;
677c478bd9Sstevel@tonic-gate 	int  vallen;
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static mutex_t		cache_lock = DEFAULTMUTEX;
717c478bd9Sstevel@tonic-gate static int		generation;	/* Incremented when we add to cache */
727c478bd9Sstevel@tonic-gate static struct cache	*head;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	CACHESZ 16
757c478bd9Sstevel@tonic-gate #define	CACHETO 600
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static void
freenode(struct cache * n)7861961e0fSrobinson freenode(struct cache *n)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	if (n->val != 0)
8161961e0fSrobinson 		free(n->val);
827c478bd9Sstevel@tonic-gate 	if (n->key != 0)
8361961e0fSrobinson 		free(n->key);
847c478bd9Sstevel@tonic-gate 	if (n->map != 0)
8561961e0fSrobinson 		free(n->map);
867c478bd9Sstevel@tonic-gate 	if (n->domain != 0)
8761961e0fSrobinson 		free(n->domain);
8861961e0fSrobinson 	free(n);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917de476d1Ssm /*
927de476d1Ssm  * Attempt to Add item to cache
937de476d1Ssm  */
947c478bd9Sstevel@tonic-gate static struct cache *
makenode(char * domain,char * map,int keylen,int vallen)9561961e0fSrobinson makenode(char *domain, char *map, int keylen, int vallen)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct cache *n;
987c478bd9Sstevel@tonic-gate 
997de476d1Ssm 	/* Do not cache 'passwd' values i.e. passwd.byname or passwd.byuid. */
1007de476d1Ssm 	if (strncmp(map, "passwd", 6) == 0)
1017de476d1Ssm 		return (0);
1027de476d1Ssm 
10361961e0fSrobinson 	if ((n = calloc(1, sizeof (*n))) == 0)
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 	if (((n->domain = strdup(domain)) == 0) ||
1067c478bd9Sstevel@tonic-gate 	    ((n->map = strdup(map)) == 0) ||
1077c478bd9Sstevel@tonic-gate 	    ((n->key = malloc(keylen)) == 0) ||
1087c478bd9Sstevel@tonic-gate 	    ((n->val = malloc(vallen)) == 0)) {
1097c478bd9Sstevel@tonic-gate 		freenode(n);
1107c478bd9Sstevel@tonic-gate 		return (0);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	return (n);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157de476d1Ssm /*
1167de476d1Ssm  * Look for a matching result in the per-process cache.
1177de476d1Ssm  * Upon finding a match set the passed in 'val' and 'vallen'
1187de476d1Ssm  * parameters and return 1.  Otherwise return 0.
1197de476d1Ssm  */
1207c478bd9Sstevel@tonic-gate static int
in_cache(char * domain,char * map,char * key,int keylen,char ** val,int * vallen)12161961e0fSrobinson in_cache(char *domain, char *map, char *key, int keylen, char **val,
12261961e0fSrobinson 								int *vallen)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct cache *c, **pp;
1257c478bd9Sstevel@tonic-gate 	int cnt;
1267c478bd9Sstevel@tonic-gate 	struct timeval now;
1277c478bd9Sstevel@tonic-gate 	struct timezone tz;
1287c478bd9Sstevel@tonic-gate 
1297de476d1Ssm 	/* The 'passwd' data is not cached. */
1307de476d1Ssm 	if (strncmp(map, "passwd", 6) == 0)
1317de476d1Ssm 		return (0);
1327de476d1Ssm 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Assumes that caller (yp_match) has locked the cache
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	for (pp = &head, cnt = 0;  (c = *pp) != 0;  pp = &c->next, cnt++) {
1377c478bd9Sstevel@tonic-gate 		if ((c->keylen == keylen) &&
1387c478bd9Sstevel@tonic-gate 		    (memcmp(key, c->key, (size_t)keylen) == 0) &&
1397c478bd9Sstevel@tonic-gate 		    (strcmp(map, c->map) == 0) &&
1407c478bd9Sstevel@tonic-gate 		    (strcmp(domain, c->domain) == 0)) {
1417c478bd9Sstevel@tonic-gate 			/* cache hit */
1427c478bd9Sstevel@tonic-gate 			(void) gettimeofday(&now, &tz);
1437c478bd9Sstevel@tonic-gate 			if ((now.tv_sec - c->birth) > CACHETO) {
1447c478bd9Sstevel@tonic-gate 				/* rats.  it is too old to use */
1457c478bd9Sstevel@tonic-gate 				*pp = c->next;
1467c478bd9Sstevel@tonic-gate 				freenode(c);
1477c478bd9Sstevel@tonic-gate 				break;
1487c478bd9Sstevel@tonic-gate 			} else {
1497c478bd9Sstevel@tonic-gate 				*val = c->val;
1507c478bd9Sstevel@tonic-gate 				*vallen = c->vallen;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 				/* Ersatz LRU:  Move this entry to the front */
1537c478bd9Sstevel@tonic-gate 				*pp = c->next;
1547c478bd9Sstevel@tonic-gate 				c->next = head;
1557c478bd9Sstevel@tonic-gate 				head = c;
1567c478bd9Sstevel@tonic-gate 				return (1);
1577c478bd9Sstevel@tonic-gate 			}
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 		if (cnt >= CACHESZ) {
1607c478bd9Sstevel@tonic-gate 			*pp = c->next;
1617c478bd9Sstevel@tonic-gate 			freenode(c);
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	return (0);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Requests the yp server associated with a given domain to attempt to match
1707c478bd9Sstevel@tonic-gate  * the passed key datum in the named map, and to return the associated value
1717c478bd9Sstevel@tonic-gate  * datum. This part does parameter checking, and implements the "infinite"
1727c478bd9Sstevel@tonic-gate  * (until success) sleep loop if 'hardlookup' parameter is set.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate int
__yp_match_cflookup(char * domain,char * map,char * key,int keylen,char ** val,int * vallen,int hardlookup)17561961e0fSrobinson __yp_match_cflookup(char *domain, char *map, char *key, int keylen, char **val,
17661961e0fSrobinson 						int *vallen, int hardlookup)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	size_t domlen;
1797c478bd9Sstevel@tonic-gate 	size_t maplen;
1807c478bd9Sstevel@tonic-gate 	int reason;
1817c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
1827c478bd9Sstevel@tonic-gate 	int savesize;
1837c478bd9Sstevel@tonic-gate 	struct timeval now;
1847c478bd9Sstevel@tonic-gate 	struct timezone tz;
1857c478bd9Sstevel@tonic-gate 	char *my_val;
1867c478bd9Sstevel@tonic-gate 	int  my_vallen;
1877c478bd9Sstevel@tonic-gate 	int  found_it;
1887c478bd9Sstevel@tonic-gate 	int  cachegen;
1897c478bd9Sstevel@tonic-gate 
19061961e0fSrobinson 	if ((map == NULL) || (domain == NULL))
1917c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	domlen = strlen(domain);
1947c478bd9Sstevel@tonic-gate 	maplen = strlen(map);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
1977c478bd9Sstevel@tonic-gate 	    (maplen == 0) || (maplen > YPMAXMAP) ||
19861961e0fSrobinson 	    (key == NULL) || (keylen == 0))
1997c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
2007c478bd9Sstevel@tonic-gate 
20161961e0fSrobinson 	(void) mutex_lock(&cache_lock);
2027c478bd9Sstevel@tonic-gate 	found_it = in_cache(domain, map, key, keylen, &my_val, &my_vallen);
2037c478bd9Sstevel@tonic-gate 	cachegen = generation;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (found_it) {
2067c478bd9Sstevel@tonic-gate 		/* NB: Copy two extra bytes; see below */
2077c478bd9Sstevel@tonic-gate 		savesize = my_vallen + 2;
2087c478bd9Sstevel@tonic-gate 		if ((*val = malloc((size_t)savesize)) == 0) {
20961961e0fSrobinson 			(void) mutex_unlock(&cache_lock);
2107c478bd9Sstevel@tonic-gate 			return (YPERR_RESRC);
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 		(void) memcpy(*val, my_val, (size_t)savesize);
2137c478bd9Sstevel@tonic-gate 		*vallen = my_vallen;
21461961e0fSrobinson 		(void) mutex_unlock(&cache_lock);
2157c478bd9Sstevel@tonic-gate 		return (0);	/* Success */
2167c478bd9Sstevel@tonic-gate 	}
21761961e0fSrobinson 	(void) mutex_unlock(&cache_lock);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	for (;;) {
2207c478bd9Sstevel@tonic-gate 
22161961e0fSrobinson 		if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup))
2227c478bd9Sstevel@tonic-gate 			return (reason);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		if (pdomb->dom_binding->ypbind_hi_vers >= YPVERS) {
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 			reason = domatch(domain, map, key, keylen, pdomb,
2277c478bd9Sstevel@tonic-gate 			    &_ypserv_timeout, val, vallen);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
2307c478bd9Sstevel@tonic-gate 			if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
2317c478bd9Sstevel@tonic-gate 			    reason == YPERR_BUSY /* as if */) {
2327c478bd9Sstevel@tonic-gate 				yp_unbind(domain);
2337c478bd9Sstevel@tonic-gate 				if (hardlookup)
234e8031f0aSraf 					(void) sleep(_ypsleeptime); /* retry */
23561961e0fSrobinson 				else
2367c478bd9Sstevel@tonic-gate 					return (reason);
2377c478bd9Sstevel@tonic-gate 			} else
2387c478bd9Sstevel@tonic-gate 				break;
2397c478bd9Sstevel@tonic-gate 		} else {
2407c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
2417c478bd9Sstevel@tonic-gate 			return (YPERR_VERS);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/* add to our cache */
2467c478bd9Sstevel@tonic-gate 	if (reason == 0) {
24761961e0fSrobinson 		(void) mutex_lock(&cache_lock);
2487c478bd9Sstevel@tonic-gate 		/*
2497c478bd9Sstevel@tonic-gate 		 * Check whether some other annoying thread did the same
2507c478bd9Sstevel@tonic-gate 		 * thing in parallel with us.  I hate it when that happens...
2517c478bd9Sstevel@tonic-gate 		 */
2527c478bd9Sstevel@tonic-gate 		if (generation != cachegen &&
2537c478bd9Sstevel@tonic-gate 		    in_cache(domain, map, key, keylen, &my_val, &my_vallen)) {
2547c478bd9Sstevel@tonic-gate 			/*
2557c478bd9Sstevel@tonic-gate 			 * Could get cute and update the birth time, but it's
2567c478bd9Sstevel@tonic-gate 			 *   not worth the bother.
2577c478bd9Sstevel@tonic-gate 			 * It looks strange that we return one val[] array
2587c478bd9Sstevel@tonic-gate 			 *   to the caller and have a different copy of the
2597c478bd9Sstevel@tonic-gate 			 *   val[] array in the cache (presumably with the
2607c478bd9Sstevel@tonic-gate 			 *   same contents), but it should work just fine.
2617c478bd9Sstevel@tonic-gate 			 * So, do absolutely nothing...
2627c478bd9Sstevel@tonic-gate 			 */
2637c478bd9Sstevel@tonic-gate 			/* EMPTY */
2647c478bd9Sstevel@tonic-gate 		} else {
2657c478bd9Sstevel@tonic-gate 			struct cache	*c;
2667c478bd9Sstevel@tonic-gate 			/*
2677c478bd9Sstevel@tonic-gate 			 * NB: allocate and copy extract two bytes of the
2687c478bd9Sstevel@tonic-gate 			 * value;  these are mandatory CR and NULL bytes.
2697c478bd9Sstevel@tonic-gate 			 */
2707c478bd9Sstevel@tonic-gate 			savesize = *vallen + 2;
2717c478bd9Sstevel@tonic-gate 			c = makenode(domain, map, keylen, savesize);
2727c478bd9Sstevel@tonic-gate 			if (c != 0) {
2737c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&now, &tz);
2747c478bd9Sstevel@tonic-gate 				c->birth = now.tv_sec;
2757c478bd9Sstevel@tonic-gate 				c->keylen = keylen;
2767c478bd9Sstevel@tonic-gate 				c->vallen = *vallen;
2777c478bd9Sstevel@tonic-gate 				(void) memcpy(c->key, key, (size_t)keylen);
2787c478bd9Sstevel@tonic-gate 				(void) memcpy(c->val, *val, (size_t)savesize);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 				c->next = head;
2817c478bd9Sstevel@tonic-gate 				head = c;
2827c478bd9Sstevel@tonic-gate 				++generation;
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 		}
28561961e0fSrobinson 		(void) mutex_unlock(&cache_lock);
2867c478bd9Sstevel@tonic-gate 	} else if (reason == YPERR_MAP && geteuid() == 0) {
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * Lookup could be for a secure map; fail over to retry
2897c478bd9Sstevel@tonic-gate 		 * from a reserved port. Only useful to try this if we're
2907c478bd9Sstevel@tonic-gate 		 * the super user.
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		int rsvdreason;
2937c478bd9Sstevel@tonic-gate 		rsvdreason = yp_match_rsvdport(domain, map, key, keylen, val,
2947c478bd9Sstevel@tonic-gate 						vallen);
2957c478bd9Sstevel@tonic-gate 		if (rsvdreason == 0)
2967c478bd9Sstevel@tonic-gate 			reason = rsvdreason;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	return (reason);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
yp_match(char * domain,char * map,char * key,int keylen,char ** val,int * vallen)3027c478bd9Sstevel@tonic-gate yp_match(
3037c478bd9Sstevel@tonic-gate 	char *domain,
3047c478bd9Sstevel@tonic-gate 	char *map,
3057c478bd9Sstevel@tonic-gate 	char *key,
3067c478bd9Sstevel@tonic-gate 	int  keylen,
3077c478bd9Sstevel@tonic-gate 	char **val,		/* returns value array */
3087c478bd9Sstevel@tonic-gate 	int  *vallen)		/* returns bytes in val */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	/* the traditional yp_match loops forever thus hardlookup is set */
3127c478bd9Sstevel@tonic-gate 	return (__yp_match_cflookup(domain, map, key, keylen, val, vallen, 1));
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate extern void
__empty_yp_cache(void)31661961e0fSrobinson __empty_yp_cache(void)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 	struct cache *p, *n;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Copy the cache pointer and make it ZERO */
32161961e0fSrobinson 	(void) mutex_lock(&cache_lock);
3227c478bd9Sstevel@tonic-gate 	p = head;
3237c478bd9Sstevel@tonic-gate 	head = 0;
32461961e0fSrobinson 	(void) mutex_unlock(&cache_lock);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (p == 0)
3277c478bd9Sstevel@tonic-gate 		return;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	/* Empty the cache */
3307c478bd9Sstevel@tonic-gate 	n = p->next;
3317c478bd9Sstevel@tonic-gate 	while (p) {
3327c478bd9Sstevel@tonic-gate 		freenode(p);
3337c478bd9Sstevel@tonic-gate 		p = n;
3347c478bd9Sstevel@tonic-gate 		if (p)
3357c478bd9Sstevel@tonic-gate 			n = p->next;
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * Requests the yp server associated with a given domain to attempt to match
3417c478bd9Sstevel@tonic-gate  * the passed key datum in the named map, and to return the associated value
3427c478bd9Sstevel@tonic-gate  * datum. This part does parameter checking, and implements the "infinite"
3437c478bd9Sstevel@tonic-gate  * (until success) sleep loop.
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * XXX special version for handling C2 (passwd.adjunct) lookups when we need
3467c478bd9Sstevel@tonic-gate  * a reserved port.
3477c478bd9Sstevel@tonic-gate  * Only difference against yp_match is that this function uses
3487c478bd9Sstevel@tonic-gate  * __yp_dobind_rsvdport().
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  * Only called from NIS switch backend.
3517c478bd9Sstevel@tonic-gate  */
3527c478bd9Sstevel@tonic-gate int
__yp_match_rsvdport_cflookup(char * domain,char * map,char * key,int keylen,char ** val,int * vallen,int hardlookup)3537c478bd9Sstevel@tonic-gate __yp_match_rsvdport_cflookup(
3547c478bd9Sstevel@tonic-gate 	char *domain,
3557c478bd9Sstevel@tonic-gate 	char *map,
3567c478bd9Sstevel@tonic-gate 	char *key,
3577c478bd9Sstevel@tonic-gate 	int  keylen,
3587c478bd9Sstevel@tonic-gate 	char **val,		/* returns value array */
3597c478bd9Sstevel@tonic-gate 	int  *vallen,		/* returns bytes in val */
3607c478bd9Sstevel@tonic-gate 	int  hardlookup)	/* retry until we can an answer */
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	size_t domlen;
3637c478bd9Sstevel@tonic-gate 	size_t maplen;
3647c478bd9Sstevel@tonic-gate 	int reason;
3657c478bd9Sstevel@tonic-gate 	struct dom_binding *pdomb;
3667c478bd9Sstevel@tonic-gate 	int savesize;
3677c478bd9Sstevel@tonic-gate 	struct timeval now;
3687c478bd9Sstevel@tonic-gate 	struct timezone tz;
3697c478bd9Sstevel@tonic-gate 	char *my_val;
3707c478bd9Sstevel@tonic-gate 	int  my_vallen;
3717c478bd9Sstevel@tonic-gate 	int  found_it;
3727c478bd9Sstevel@tonic-gate 	int  cachegen;
3737c478bd9Sstevel@tonic-gate 
37461961e0fSrobinson 	if ((map == NULL) || (domain == NULL))
3757c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	domlen = strlen(domain);
3787c478bd9Sstevel@tonic-gate 	maplen = strlen(map);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if ((domlen == 0) || (domlen > YPMAXDOMAIN) ||
3817c478bd9Sstevel@tonic-gate 	    (maplen == 0) || (maplen > YPMAXMAP) ||
38261961e0fSrobinson 	    (key == NULL) || (keylen == 0))
3837c478bd9Sstevel@tonic-gate 		return (YPERR_BADARGS);
3847c478bd9Sstevel@tonic-gate 
38561961e0fSrobinson 	(void) mutex_lock(&cache_lock);
3867c478bd9Sstevel@tonic-gate 	found_it = in_cache(domain, map, key, keylen, &my_val, &my_vallen);
3877c478bd9Sstevel@tonic-gate 	cachegen = generation;
3887c478bd9Sstevel@tonic-gate 	if (found_it) {
3897c478bd9Sstevel@tonic-gate 		/* NB: Copy two extra bytes; see below */
3907c478bd9Sstevel@tonic-gate 		savesize = my_vallen + 2;
3917c478bd9Sstevel@tonic-gate 		if ((*val = malloc((size_t)savesize)) == 0) {
39261961e0fSrobinson 			(void) mutex_unlock(&cache_lock);
3937c478bd9Sstevel@tonic-gate 			return (YPERR_RESRC);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		(void) memcpy(*val, my_val, (size_t)savesize);
3967c478bd9Sstevel@tonic-gate 		*vallen = my_vallen;
39761961e0fSrobinson 		(void) mutex_unlock(&cache_lock);
3987c478bd9Sstevel@tonic-gate 		return (0);	/* Success */
3997c478bd9Sstevel@tonic-gate 	}
40061961e0fSrobinson 	(void) mutex_unlock(&cache_lock);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	for (;;) {
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		if (reason = __yp_dobind_rsvdport_cflookup(domain, &pdomb,
40561961e0fSrobinson 							hardlookup))
4067c478bd9Sstevel@tonic-gate 			return (reason);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		if (pdomb->dom_binding->ypbind_hi_vers >= YPVERS) {
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			reason = domatch(domain, map, key, keylen,
4117c478bd9Sstevel@tonic-gate 				pdomb, &_ypserv_timeout, val, vallen);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			/*
4147c478bd9Sstevel@tonic-gate 			 * Have to free the binding since the reserved
4157c478bd9Sstevel@tonic-gate 			 * port bindings are not cached.
4167c478bd9Sstevel@tonic-gate 			 */
4177c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
4187c478bd9Sstevel@tonic-gate 			free_dom_binding(pdomb);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			if (reason == YPERR_RPC || reason == YPERR_YPSERV ||
4217c478bd9Sstevel@tonic-gate 			    reason == YPERR_BUSY /* as if */) {
4227c478bd9Sstevel@tonic-gate 				yp_unbind(domain);
4237c478bd9Sstevel@tonic-gate 				if (hardlookup)
424e8031f0aSraf 					(void) sleep(_ypsleeptime); /* retry */
42561961e0fSrobinson 				else
4267c478bd9Sstevel@tonic-gate 					return (reason);
4277c478bd9Sstevel@tonic-gate 			} else
4287c478bd9Sstevel@tonic-gate 				break;
4297c478bd9Sstevel@tonic-gate 		} else {
4307c478bd9Sstevel@tonic-gate 			/*
4317c478bd9Sstevel@tonic-gate 			 * Have to free the binding since the reserved
4327c478bd9Sstevel@tonic-gate 			 * port bindings are not cached.
4337c478bd9Sstevel@tonic-gate 			 */
4347c478bd9Sstevel@tonic-gate 			__yp_rel_binding(pdomb);
4357c478bd9Sstevel@tonic-gate 			free_dom_binding(pdomb);
4367c478bd9Sstevel@tonic-gate 			return (YPERR_VERS);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* add to our cache */
4417c478bd9Sstevel@tonic-gate 	if (reason == 0) {
44261961e0fSrobinson 		(void) mutex_lock(&cache_lock);
4437c478bd9Sstevel@tonic-gate 		/*
4447c478bd9Sstevel@tonic-gate 		 * Check whether some other annoying thread did the same
4457c478bd9Sstevel@tonic-gate 		 * thing in parallel with us.  I hate it when that happens...
4467c478bd9Sstevel@tonic-gate 		 */
4477c478bd9Sstevel@tonic-gate 		if (generation != cachegen &&
4487c478bd9Sstevel@tonic-gate 		    in_cache(domain, map, key, keylen, &my_val, &my_vallen)) {
4497c478bd9Sstevel@tonic-gate 			/*
4507c478bd9Sstevel@tonic-gate 			 * Could get cute and update the birth time, but it's
4517c478bd9Sstevel@tonic-gate 			 *   not worth the bother.
4527c478bd9Sstevel@tonic-gate 			 * It looks strange that we return one val[] array
4537c478bd9Sstevel@tonic-gate 			 *   to the caller and have a different copy of the
4547c478bd9Sstevel@tonic-gate 			 *   val[] array in the cache (presumably with the
4557c478bd9Sstevel@tonic-gate 			 *   same contents), but it should work just fine.
4567c478bd9Sstevel@tonic-gate 			 * So, do absolutely nothing...
4577c478bd9Sstevel@tonic-gate 			 */
4587c478bd9Sstevel@tonic-gate 			/* EMPTY */
4597c478bd9Sstevel@tonic-gate 		} else {
4607c478bd9Sstevel@tonic-gate 			struct cache	*c;
4617c478bd9Sstevel@tonic-gate 			/*
4627c478bd9Sstevel@tonic-gate 			 * NB: allocate and copy extract two bytes of the
4637c478bd9Sstevel@tonic-gate 			 * value;  these are mandatory CR and NULL bytes.
4647c478bd9Sstevel@tonic-gate 			 */
4657c478bd9Sstevel@tonic-gate 			savesize = *vallen + 2;
4667c478bd9Sstevel@tonic-gate 			c = makenode(domain, map, keylen, savesize);
4677c478bd9Sstevel@tonic-gate 			if (c != 0) {
4687c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&now, &tz);
4697c478bd9Sstevel@tonic-gate 				c->birth = now.tv_sec;
4707c478bd9Sstevel@tonic-gate 				c->keylen = keylen;
4717c478bd9Sstevel@tonic-gate 				c->vallen = *vallen;
4727c478bd9Sstevel@tonic-gate 				(void) memcpy(c->key, key, (size_t)keylen);
4737c478bd9Sstevel@tonic-gate 				(void) memcpy(c->val, *val, (size_t)savesize);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 				c->next = head;
4767c478bd9Sstevel@tonic-gate 				head = c;
4777c478bd9Sstevel@tonic-gate 				++generation;
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 		}
48061961e0fSrobinson 		(void) mutex_unlock(&cache_lock);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	return (reason);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate int
yp_match_rsvdport(char * domain,char * map,char * key,int keylen,char ** val,int * vallen)4877c478bd9Sstevel@tonic-gate yp_match_rsvdport(
4887c478bd9Sstevel@tonic-gate 	char *domain,
4897c478bd9Sstevel@tonic-gate 	char *map,
4907c478bd9Sstevel@tonic-gate 	char *key,
4917c478bd9Sstevel@tonic-gate 	int  keylen,
4927c478bd9Sstevel@tonic-gate 	char **val,		/* returns value array */
4937c478bd9Sstevel@tonic-gate 	int  *vallen)		/* returns bytes in val */
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	/* traditional yp_match retries forever so set hardlookup */
4967c478bd9Sstevel@tonic-gate 	return (__yp_match_rsvdport_cflookup(domain, map, key, keylen, val,
4977c478bd9Sstevel@tonic-gate 					vallen, 1));
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * This talks v3 protocol to ypserv
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate static int
domatch(char * domain,char * map,char * key,int keylen,struct dom_binding * pdomb,struct timeval * timeoutp,char ** val,int * vallen)5057c478bd9Sstevel@tonic-gate domatch(char *domain, char *map, char *key, int  keylen,
5067c478bd9Sstevel@tonic-gate     struct dom_binding *pdomb, struct timeval *timeoutp, char **val,
5077c478bd9Sstevel@tonic-gate     int  *vallen)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct ypreq_key req;
5107c478bd9Sstevel@tonic-gate 	struct ypresp_val resp;
5117c478bd9Sstevel@tonic-gate 	unsigned int retval = 0;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	req.domain = domain;
5147c478bd9Sstevel@tonic-gate 	req.map = map;
5157c478bd9Sstevel@tonic-gate 	req.keydat.dptr = key;
5167c478bd9Sstevel@tonic-gate 	req.keydat.dsize = keylen;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	resp.valdat.dptr = NULL;
5197c478bd9Sstevel@tonic-gate 	resp.valdat.dsize = 0;
5207c478bd9Sstevel@tonic-gate 	(void) memset((char *)&resp, 0, sizeof (struct ypresp_val));
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Do the match request.  If the rpc call failed, return with status
5247c478bd9Sstevel@tonic-gate 	 * from this point.
5257c478bd9Sstevel@tonic-gate 	 */
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	switch (clnt_call(pdomb->dom_client, YPPROC_MATCH,
5287c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypreq_key, (char *)&req,
5297c478bd9Sstevel@tonic-gate 			(xdrproc_t)xdr_ypresp_val, (char *)&resp,
5307c478bd9Sstevel@tonic-gate 			*timeoutp)) {
5317c478bd9Sstevel@tonic-gate 	case RPC_SUCCESS:
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 	case RPC_TIMEDOUT:
5347c478bd9Sstevel@tonic-gate 		return (YPERR_YPSERV);
5357c478bd9Sstevel@tonic-gate 	default:
5367c478bd9Sstevel@tonic-gate 		return (YPERR_RPC);
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/* See if the request succeeded */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (resp.status != YP_TRUE) {
5427c478bd9Sstevel@tonic-gate 		retval = ypprot_err(resp.status);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 
545*48bbca81SDaniel Hoffman 	/* Get some memory which the user can get rid of as they likes */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (!retval && ((*val = malloc((size_t)
5487c478bd9Sstevel@tonic-gate 	    resp.valdat.dsize + 2)) == NULL)) {
5497c478bd9Sstevel@tonic-gate 		retval = YPERR_RESRC;
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/* Copy the returned value byte string into the new memory */
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (!retval) {
5557c478bd9Sstevel@tonic-gate 		*vallen = (int)resp.valdat.dsize;
5567c478bd9Sstevel@tonic-gate 		(void) memcpy(*val, resp.valdat.dptr,
5577c478bd9Sstevel@tonic-gate 		    (size_t)resp.valdat.dsize);
5587c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize] = '\n';
5597c478bd9Sstevel@tonic-gate 		(*val)[resp.valdat.dsize + 1] = '\0';
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	CLNT_FREERES(pdomb->dom_client,
5637c478bd9Sstevel@tonic-gate 		(xdrproc_t)xdr_ypresp_val, (char *)&resp);
5647c478bd9Sstevel@tonic-gate 	return (retval);
5657c478bd9Sstevel@tonic-gate }
566