xref: /titanic_52/usr/src/cmd/fs.d/autofs/ns_nis.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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
539d3e169Sevanl  * Common Development and Distribution License (the "License").
639d3e169Sevanl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  *	ns_nis.c
237c478bd9Sstevel@tonic-gate  *
24*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <nsswitch.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
387c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
397c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
407c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
417c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include "automount.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #define	KEY		0
467c478bd9Sstevel@tonic-gate #define	CONTENTS	1
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static int replace_undscr_by_dot(char *);
497c478bd9Sstevel@tonic-gate static int nis_err(int);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static char nis_mydomain[YPMAXDOMAIN];
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct dir_cbdata {
547c478bd9Sstevel@tonic-gate 	struct dir_entry **list;
557c478bd9Sstevel@tonic-gate 	struct dir_entry *last;
567c478bd9Sstevel@tonic-gate 	int error;
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int readdir_callback(int, char *, int, const char *,
607c478bd9Sstevel@tonic-gate 				int, struct dir_cbdata *);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate void
637c478bd9Sstevel@tonic-gate init_nis(char **stack, char ***stkptr)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate #ifdef lint
667c478bd9Sstevel@tonic-gate 	stack = stack;
677c478bd9Sstevel@tonic-gate 	stkptr = stkptr;
687c478bd9Sstevel@tonic-gate #endif /* lint */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	(void) sysinfo(SI_SRPC_DOMAIN, nis_mydomain, sizeof (nis_mydomain));
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
747c478bd9Sstevel@tonic-gate int
757c478bd9Sstevel@tonic-gate getmapent_nis(key, map, ml, stack, stkptr, iswildcard, isrestricted)
767c478bd9Sstevel@tonic-gate 	char *key, *map;
777c478bd9Sstevel@tonic-gate 	struct mapline *ml;
787c478bd9Sstevel@tonic-gate 	char **stack;
797c478bd9Sstevel@tonic-gate 	char ***stkptr;
807c478bd9Sstevel@tonic-gate 	bool_t *iswildcard;
817c478bd9Sstevel@tonic-gate 	bool_t isrestricted;
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	char *nisline = NULL;
847c478bd9Sstevel@tonic-gate 	char *my_map = NULL;
857c478bd9Sstevel@tonic-gate 	char *lp, *lq;
867c478bd9Sstevel@tonic-gate 	int nislen, len;
877c478bd9Sstevel@tonic-gate 	int nserr;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (iswildcard)
907c478bd9Sstevel@tonic-gate 		*iswildcard = FALSE;
917c478bd9Sstevel@tonic-gate 	nserr = yp_match(nis_mydomain, map, key, strlen(key),
927c478bd9Sstevel@tonic-gate 						&nisline, &nislen);
937c478bd9Sstevel@tonic-gate 	if (nserr == YPERR_MAP) {
947c478bd9Sstevel@tonic-gate 		my_map = strdup(map);
957c478bd9Sstevel@tonic-gate 		if (my_map == NULL) {
967c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
977c478bd9Sstevel@tonic-gate 				"getmapent_nis: memory alloc failed: %m");
987c478bd9Sstevel@tonic-gate 			return (__NSW_UNAVAIL);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		if (replace_undscr_by_dot(my_map))
1017c478bd9Sstevel@tonic-gate 			nserr = yp_match(nis_mydomain, my_map, key,
1027c478bd9Sstevel@tonic-gate 					strlen(key), &nisline, &nislen);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (nserr) {
1067c478bd9Sstevel@tonic-gate 		if (nserr == YPERR_KEY) {
1077c478bd9Sstevel@tonic-gate 			/*
1087c478bd9Sstevel@tonic-gate 			 * Try the default entry "*"
1097c478bd9Sstevel@tonic-gate 			 */
1107c478bd9Sstevel@tonic-gate 			if (my_map == NULL)
1117c478bd9Sstevel@tonic-gate 				nserr = yp_match(nis_mydomain, map, "*", 1,
1127c478bd9Sstevel@tonic-gate 						&nisline, &nislen);
1137c478bd9Sstevel@tonic-gate 			else
1147c478bd9Sstevel@tonic-gate 				nserr = yp_match(nis_mydomain, my_map, "*", 1,
1157c478bd9Sstevel@tonic-gate 						&nisline, &nislen);
1167c478bd9Sstevel@tonic-gate 			if (!nserr && iswildcard)
1177c478bd9Sstevel@tonic-gate 				*iswildcard = TRUE;
1187c478bd9Sstevel@tonic-gate 		} else {
1197c478bd9Sstevel@tonic-gate 			if (verbose)
1207c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
1217c478bd9Sstevel@tonic-gate 					map, yperr_string(nserr));
1227c478bd9Sstevel@tonic-gate 			nserr = 1;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	if (my_map != NULL)
1267c478bd9Sstevel@tonic-gate 		free(my_map);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	nserr = nis_err(nserr);
1297c478bd9Sstevel@tonic-gate 	if (nserr)
1307c478bd9Sstevel@tonic-gate 		goto done;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * at this point we are sure that yp_match succeeded
1347c478bd9Sstevel@tonic-gate 	 * so massage the entry by
1357c478bd9Sstevel@tonic-gate 	 * 1. ignoring # and beyond
1367c478bd9Sstevel@tonic-gate 	 * 2. trim the trailing whitespace
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	if (lp = strchr(nisline, '#'))
1397c478bd9Sstevel@tonic-gate 		*lp = '\0';
1407c478bd9Sstevel@tonic-gate 	len = strlen(nisline);
1417c478bd9Sstevel@tonic-gate 	if (len == 0) {
1427c478bd9Sstevel@tonic-gate 		nserr = __NSW_NOTFOUND;
1437c478bd9Sstevel@tonic-gate 		goto done;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	lp = &nisline[len - 1];
1467c478bd9Sstevel@tonic-gate 	while (lp > nisline && isspace(*lp))
1477c478bd9Sstevel@tonic-gate 		*lp-- = '\0';
1487c478bd9Sstevel@tonic-gate 	if (lp == nisline) {
1497c478bd9Sstevel@tonic-gate 		nserr = __NSW_NOTFOUND;
1507c478bd9Sstevel@tonic-gate 		goto done;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	(void) strcpy(ml->linebuf, nisline);
1537c478bd9Sstevel@tonic-gate 	lp = ml->linebuf;
1547c478bd9Sstevel@tonic-gate 	lq = ml->lineqbuf;
1557c478bd9Sstevel@tonic-gate 	unquote(lp, lq);
1567c478bd9Sstevel@tonic-gate 	/* now we have the correct line */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	nserr = __NSW_SUCCESS;
1597c478bd9Sstevel@tonic-gate done:
1607c478bd9Sstevel@tonic-gate 	if (nisline)
1617c478bd9Sstevel@tonic-gate 		free((char *)nisline);
1627c478bd9Sstevel@tonic-gate 	return (nserr);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
16611606941Sjwahlig int
1677c478bd9Sstevel@tonic-gate loadmaster_nis(mapname, defopts, stack, stkptr)
1687c478bd9Sstevel@tonic-gate 	char *mapname, *defopts;
1697c478bd9Sstevel@tonic-gate 	char **stack;
1707c478bd9Sstevel@tonic-gate 	char ***stkptr;
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	int first, err;
1737c478bd9Sstevel@tonic-gate 	char *key, *nkey, *val;
1747c478bd9Sstevel@tonic-gate 	int kl, nkl, vl;
1757c478bd9Sstevel@tonic-gate 	char dir[256], map[256], qbuff[256];
1767c478bd9Sstevel@tonic-gate 	char *pmap, *opts, *my_mapname;
1777c478bd9Sstevel@tonic-gate 	int count = 0;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	first = 1;
1807c478bd9Sstevel@tonic-gate 	key  = NULL; kl  = 0;
1817c478bd9Sstevel@tonic-gate 	nkey = NULL; nkl = 0;
1827c478bd9Sstevel@tonic-gate 	val  = NULL; vl  = 0;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * need a private copy of mapname, because we may change
1867c478bd9Sstevel@tonic-gate 	 * the underscores by dots. We however do not want the
1877c478bd9Sstevel@tonic-gate 	 * orignal to be changed, as we may want to use the
1887c478bd9Sstevel@tonic-gate 	 * original name in some other name service
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	my_mapname = strdup(mapname);
1917c478bd9Sstevel@tonic-gate 	if (my_mapname == NULL) {
1927c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "loadmaster_yp: memory alloc failed: %m");
1937c478bd9Sstevel@tonic-gate 		/* not the name svc's fault but ... */
1947c478bd9Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	for (;;) {
1977c478bd9Sstevel@tonic-gate 		if (first) {
1987c478bd9Sstevel@tonic-gate 			first = 0;
1997c478bd9Sstevel@tonic-gate 			err = yp_first(nis_mydomain, my_mapname,
2007c478bd9Sstevel@tonic-gate 				&nkey, &nkl, &val, &vl);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 			if ((err == YPERR_MAP) &&
2037c478bd9Sstevel@tonic-gate 			    (replace_undscr_by_dot(my_mapname)))
2047c478bd9Sstevel@tonic-gate 				err = yp_first(nis_mydomain, my_mapname,
2057c478bd9Sstevel@tonic-gate 					&nkey, &nkl, &val, &vl);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			if ((err == YPERR_DOMAIN) || (err == YPERR_YPBIND)) {
2087c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2097c478bd9Sstevel@tonic-gate 					"can't read nis map %s: %s - retrying",
2107c478bd9Sstevel@tonic-gate 					my_mapname, yperr_string(err));
2117c478bd9Sstevel@tonic-gate 				while ((err == YPERR_DOMAIN) ||
2127c478bd9Sstevel@tonic-gate 					(err == YPERR_YPBIND)) {
2137c478bd9Sstevel@tonic-gate 					(void) sleep(20);
2147c478bd9Sstevel@tonic-gate 					err = yp_first(nis_mydomain, my_mapname,
2157c478bd9Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
2167c478bd9Sstevel@tonic-gate 				}
2177c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2187c478bd9Sstevel@tonic-gate 					"nis map %s: read OK.", my_mapname);
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		} else {
2217c478bd9Sstevel@tonic-gate 			err = yp_next(nis_mydomain, my_mapname, key, kl,
2227c478bd9Sstevel@tonic-gate 				&nkey, &nkl, &val, &vl);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 		if (err) {
2257c478bd9Sstevel@tonic-gate 			if (err != YPERR_NOMORE && err != YPERR_MAP)
2267c478bd9Sstevel@tonic-gate 				if (verbose)
2277c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, "%s: %s",
2287c478bd9Sstevel@tonic-gate 					my_mapname, yperr_string(err));
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 		if (key)
2327c478bd9Sstevel@tonic-gate 			free(key);
2337c478bd9Sstevel@tonic-gate 		key = nkey;
2347c478bd9Sstevel@tonic-gate 		kl = nkl;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		if (kl >= 256 || vl >= 256)
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		if (kl < 2 || vl < 1)
2407c478bd9Sstevel@tonic-gate 			break;
2417c478bd9Sstevel@tonic-gate 		if (isspace(*key) || *key == '#')
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		(void) strncpy(dir, key, kl);
2447c478bd9Sstevel@tonic-gate 		dir[kl] = '\0';
2457c478bd9Sstevel@tonic-gate 		if (macro_expand("", dir, qbuff, sizeof (dir))) {
2467c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
2477c478bd9Sstevel@tonic-gate 			    "%s in NIS map %s: entry too long (max %d chars)",
2487c478bd9Sstevel@tonic-gate 			    dir, my_mapname, sizeof (dir) - 1);
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 		(void) strncpy(map, val, vl);
2527c478bd9Sstevel@tonic-gate 		map[vl] = '\0';
2537c478bd9Sstevel@tonic-gate 		if (macro_expand(dir, map, qbuff, sizeof (map))) {
2547c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
2557c478bd9Sstevel@tonic-gate 			    "%s in NIS map %s: entry too long (max %d chars)",
2567c478bd9Sstevel@tonic-gate 			    map, my_mapname, sizeof (map) - 1);
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 		pmap = map;
2607c478bd9Sstevel@tonic-gate 		while (*pmap && isspace(*pmap))
2617c478bd9Sstevel@tonic-gate 			pmap++;		/* skip blanks in front of map */
2627c478bd9Sstevel@tonic-gate 		opts = pmap;
2637c478bd9Sstevel@tonic-gate 		while (*opts && !isspace(*opts))
2647c478bd9Sstevel@tonic-gate 			opts++;
2657c478bd9Sstevel@tonic-gate 		if (*opts) {
2667c478bd9Sstevel@tonic-gate 			*opts++ = '\0';
2677c478bd9Sstevel@tonic-gate 			while (*opts && isspace(*opts))
2687c478bd9Sstevel@tonic-gate 				opts++;
2697c478bd9Sstevel@tonic-gate 			if (*opts == '-')
2707c478bd9Sstevel@tonic-gate 				opts++;
2717c478bd9Sstevel@tonic-gate 			else
2727c478bd9Sstevel@tonic-gate 				opts = defopts;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		free(val);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		/*
2777c478bd9Sstevel@tonic-gate 		 * Check for no embedded blanks.
2787c478bd9Sstevel@tonic-gate 		 */
2797c478bd9Sstevel@tonic-gate 		if (strcspn(opts, " 	") == strlen(opts)) {
2807c478bd9Sstevel@tonic-gate 			dirinit(dir, pmap, opts, 0, stack, stkptr);
2817c478bd9Sstevel@tonic-gate 			count++;
2827c478bd9Sstevel@tonic-gate 		} else {
2837c478bd9Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in NIS map %s ignored.\n", dir, mapname);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	if (my_mapname)
2887c478bd9Sstevel@tonic-gate 		free(my_mapname);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * In the context of a master map, if no entry is
2927c478bd9Sstevel@tonic-gate 	 * found, it is like NOTFOUND
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (count > 0 && err == YPERR_NOMORE)
2957c478bd9Sstevel@tonic-gate 		return (__NSW_SUCCESS);
2967c478bd9Sstevel@tonic-gate 	else {
2977c478bd9Sstevel@tonic-gate 		if (err)
2987c478bd9Sstevel@tonic-gate 			return (nis_err(err));
2997c478bd9Sstevel@tonic-gate 		else
3007c478bd9Sstevel@tonic-gate 			/*
3017c478bd9Sstevel@tonic-gate 			 * This case will happen if map is empty
3027c478bd9Sstevel@tonic-gate 			 *  or none of the entries is valid
3037c478bd9Sstevel@tonic-gate 			 */
3047c478bd9Sstevel@tonic-gate 			return (__NSW_NOTFOUND);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
30811606941Sjwahlig int
3097c478bd9Sstevel@tonic-gate loaddirect_nis(nismap, localmap, opts, stack, stkptr)
3107c478bd9Sstevel@tonic-gate 	char *nismap, *localmap, *opts;
3117c478bd9Sstevel@tonic-gate 	char **stack;
3127c478bd9Sstevel@tonic-gate 	char ***stkptr;
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	int first, err, count;
3157c478bd9Sstevel@tonic-gate 	char *key, *nkey, *val, *my_nismap;
3167c478bd9Sstevel@tonic-gate 	int kl, nkl, vl;
3177c478bd9Sstevel@tonic-gate 	char dir[100];
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	first = 1;
3207c478bd9Sstevel@tonic-gate 	key  = NULL; kl  = 0;
3217c478bd9Sstevel@tonic-gate 	nkey = NULL; nkl = 0;
3227c478bd9Sstevel@tonic-gate 	val  = NULL; vl  = 0;
3237c478bd9Sstevel@tonic-gate 	count = 0;
3247c478bd9Sstevel@tonic-gate 	my_nismap = NULL;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	my_nismap = strdup(nismap);
3277c478bd9Sstevel@tonic-gate 	if (my_nismap == NULL) {
3287c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "loadmaster_yp: memory alloc failed: %m");
3297c478bd9Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	for (;;) {
3327c478bd9Sstevel@tonic-gate 		if (first) {
3337c478bd9Sstevel@tonic-gate 			first = 0;
3347c478bd9Sstevel@tonic-gate 			err = yp_first(nis_mydomain, my_nismap, &nkey, &nkl,
3357c478bd9Sstevel@tonic-gate 					&val, &vl);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 			if ((err == YPERR_MAP) &&
3387c478bd9Sstevel@tonic-gate 			    (replace_undscr_by_dot(my_nismap)))
3397c478bd9Sstevel@tonic-gate 				err = yp_first(nis_mydomain, my_nismap,
3407c478bd9Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			if ((err == YPERR_DOMAIN) || (err == YPERR_YPBIND)) {
3437c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3447c478bd9Sstevel@tonic-gate 					"can't read nis map %s: %s - retrying",
3457c478bd9Sstevel@tonic-gate 					my_nismap, yperr_string(err));
3467c478bd9Sstevel@tonic-gate 				while ((err == YPERR_DOMAIN) ||
3477c478bd9Sstevel@tonic-gate 					(err == YPERR_YPBIND)) {
3487c478bd9Sstevel@tonic-gate 					(void) sleep(20);
3497c478bd9Sstevel@tonic-gate 					err = yp_first(nis_mydomain, my_nismap,
3507c478bd9Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
3517c478bd9Sstevel@tonic-gate 				}
3527c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3537c478bd9Sstevel@tonic-gate 					"nis map %s: read OK.", my_nismap);
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 		} else {
3567c478bd9Sstevel@tonic-gate 			err = yp_next(nis_mydomain, my_nismap, key, kl,
3577c478bd9Sstevel@tonic-gate 					&nkey, &nkl, &val, &vl);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 		if (err) {
3607c478bd9Sstevel@tonic-gate 			if (err != YPERR_NOMORE && err != YPERR_MAP)
3617c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
3627c478bd9Sstevel@tonic-gate 					my_nismap, yperr_string(err));
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 		if (key)
3667c478bd9Sstevel@tonic-gate 			free(key);
3677c478bd9Sstevel@tonic-gate 		key = nkey;
3687c478bd9Sstevel@tonic-gate 		kl = nkl;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		if (kl < 2 || kl >= 100)
3717c478bd9Sstevel@tonic-gate 			continue;
3727c478bd9Sstevel@tonic-gate 		if (isspace(*key) || *key == '#')
3737c478bd9Sstevel@tonic-gate 			continue;
3747c478bd9Sstevel@tonic-gate 		(void) strncpy(dir, key, kl);
3757c478bd9Sstevel@tonic-gate 		dir[kl] = '\0';
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		dirinit(dir, localmap, opts, 1, stack, stkptr);
3787c478bd9Sstevel@tonic-gate 		count++;
3797c478bd9Sstevel@tonic-gate 		free(val);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (my_nismap)
3837c478bd9Sstevel@tonic-gate 		free(my_nismap);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (count > 0 && err == YPERR_NOMORE)
3867c478bd9Sstevel@tonic-gate 			return (__NSW_SUCCESS);
3877c478bd9Sstevel@tonic-gate 	else
3887c478bd9Sstevel@tonic-gate 		return (nis_err(err));
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static int
3937c478bd9Sstevel@tonic-gate replace_undscr_by_dot(map)
3947c478bd9Sstevel@tonic-gate 	char *map;
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	int ret_val = 0;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	while (*map) {
3997c478bd9Sstevel@tonic-gate 		if (*map == '_') {
4007c478bd9Sstevel@tonic-gate 			ret_val = 1;
4017c478bd9Sstevel@tonic-gate 			*map = '.';
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		map++;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	return (ret_val);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static int
4097c478bd9Sstevel@tonic-gate nis_err(err)
4107c478bd9Sstevel@tonic-gate 	int err;
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	switch (err) {
4137c478bd9Sstevel@tonic-gate 	case 0:
4147c478bd9Sstevel@tonic-gate 		return (__NSW_SUCCESS);
4157c478bd9Sstevel@tonic-gate 	case YPERR_KEY:
4167c478bd9Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
4177c478bd9Sstevel@tonic-gate 	case YPERR_MAP:
4187c478bd9Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4197c478bd9Sstevel@tonic-gate 	default:
4207c478bd9Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate int
4257c478bd9Sstevel@tonic-gate getmapkeys_nis(nsmap, list, error, cache_time, stack, stkptr)
4267c478bd9Sstevel@tonic-gate 	char *nsmap;
4277c478bd9Sstevel@tonic-gate 	struct dir_entry **list;
4287c478bd9Sstevel@tonic-gate 	int *error;
4297c478bd9Sstevel@tonic-gate 	int *cache_time;
4307c478bd9Sstevel@tonic-gate 	char **stack;
4317c478bd9Sstevel@tonic-gate 	char ***stkptr;
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	int nserr;
4347c478bd9Sstevel@tonic-gate 	struct dir_cbdata readdir_cbdata;
4357c478bd9Sstevel@tonic-gate 	struct ypall_callback cback;
4367c478bd9Sstevel@tonic-gate 	char *my_map = NULL;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	char *key = NULL, *val = NULL;
4397c478bd9Sstevel@tonic-gate 	int nkl, vl;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate #ifdef lint
4427c478bd9Sstevel@tonic-gate 	stack = stack;
4437c478bd9Sstevel@tonic-gate 	stkptr = stkptr;
4447c478bd9Sstevel@tonic-gate #endif /* lint */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	*cache_time = RDDIR_CACHE_TIME;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * XXX Hack to determine if we need to replace '_' with '.'
4507c478bd9Sstevel@tonic-gate 	 * Have to use yp_first() since yp_all() simply fails if
4517c478bd9Sstevel@tonic-gate 	 * the map is not present
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	my_map = strdup(nsmap);
4547c478bd9Sstevel@tonic-gate 	if (my_map == NULL) {
4557c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
4567c478bd9Sstevel@tonic-gate 			"getmapkeys_nis: memory alloc failed: %m");
4577c478bd9Sstevel@tonic-gate 		*error = ENOMEM;
4587c478bd9Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	nserr = yp_first(nis_mydomain, my_map, &key, &nkl, &val, &vl);
4617c478bd9Sstevel@tonic-gate 	if (nserr == YPERR_MAP) {
4627c478bd9Sstevel@tonic-gate 		if (replace_undscr_by_dot(my_map)) {
4637c478bd9Sstevel@tonic-gate 			nserr = yp_first(nis_mydomain, my_map,
4647c478bd9Sstevel@tonic-gate 					&key, &nkl, &val, &vl);
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		if (nserr == YPERR_MAP) {
4677c478bd9Sstevel@tonic-gate 			/*
4687c478bd9Sstevel@tonic-gate 			 * map not found
4697c478bd9Sstevel@tonic-gate 			 */
4707c478bd9Sstevel@tonic-gate 			*error = 0;	/* return an empty list */
4717c478bd9Sstevel@tonic-gate 			if (verbose) {
4727c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
4737c478bd9Sstevel@tonic-gate 					nsmap, yperr_string(nserr));
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			free(my_map);
4767c478bd9Sstevel@tonic-gate 			return (nis_err(nserr));
4777c478bd9Sstevel@tonic-gate 		}
47839d3e169Sevanl 	}
4797c478bd9Sstevel@tonic-gate 	if (key)
4807c478bd9Sstevel@tonic-gate 		free(key);
4817c478bd9Sstevel@tonic-gate 	if (val)
4827c478bd9Sstevel@tonic-gate 		free(val);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	readdir_cbdata.list = list;
4857c478bd9Sstevel@tonic-gate 	readdir_cbdata.last = NULL;
4867c478bd9Sstevel@tonic-gate 	readdir_cbdata.error = 0;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	cback.foreach = readdir_callback;
4897c478bd9Sstevel@tonic-gate 	cback.data = (char *)&readdir_cbdata;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * after all this song and dance we finally
4937c478bd9Sstevel@tonic-gate 	 * ask for the list of entries
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	nserr = yp_all(nis_mydomain, my_map, &cback);
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	free(my_map);
4987c478bd9Sstevel@tonic-gate 	*error = readdir_cbdata.error;
4997c478bd9Sstevel@tonic-gate 	if (nserr) {
5007c478bd9Sstevel@tonic-gate 		if (verbose)
5017c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s", nsmap, yperr_string(nserr));
5027c478bd9Sstevel@tonic-gate 		nserr = 1;
5037c478bd9Sstevel@tonic-gate 		if (*error == 0)
5047c478bd9Sstevel@tonic-gate 			*error = ENOENT;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		return (nis_err(nserr));
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	return (__NSW_SUCCESS);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate static int
5137c478bd9Sstevel@tonic-gate readdir_callback(instatus, inkey, inkeylen, inval, invallen, indata)
5147c478bd9Sstevel@tonic-gate 	int instatus;
5157c478bd9Sstevel@tonic-gate 	char *inkey;
5167c478bd9Sstevel@tonic-gate 	int inkeylen;
5177c478bd9Sstevel@tonic-gate 	const char *inval;
5187c478bd9Sstevel@tonic-gate 	int invallen;
5197c478bd9Sstevel@tonic-gate 	struct dir_cbdata *indata;
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	struct dir_entry **list = indata->list;
5227c478bd9Sstevel@tonic-gate 	struct dir_entry *last = indata->last;
5237c478bd9Sstevel@tonic-gate 	char key[MAXPATHLEN];
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate #ifdef lint
5267c478bd9Sstevel@tonic-gate 	inval = inval;
5277c478bd9Sstevel@tonic-gate 	invallen = invallen;
5287c478bd9Sstevel@tonic-gate #endif
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	if (instatus != YP_TRUE)
5317c478bd9Sstevel@tonic-gate 		return (0);	/* next entry. yp_all may decide otherwise... */
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (inkeylen == 0 || isspace(*inkey) || *inkey == '#')
5347c478bd9Sstevel@tonic-gate 		return (0);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * yp_all allocates inkey with two extra bytes which contain
5387c478bd9Sstevel@tonic-gate 	 * NEWLINE and null but these two bytes are not reflected in
5397c478bd9Sstevel@tonic-gate 	 * inkeylen.
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	strncpy(key, inkey, inkeylen);
5427c478bd9Sstevel@tonic-gate 	key[inkeylen] = '\0';
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * Wildcard entry should be ignored - following entries should continue
5467c478bd9Sstevel@tonic-gate 	 * to be read to corroborate with the way we search for entries in yp,
5477c478bd9Sstevel@tonic-gate 	 * i.e., first for an exact key match and then a wildcard, if there's
5487c478bd9Sstevel@tonic-gate 	 * no exact key match.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (key[0] == '*' && key[1] == '\0')
5517c478bd9Sstevel@tonic-gate 		return (0);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (add_dir_entry(key, list, &last)) {
5547c478bd9Sstevel@tonic-gate 		indata->error = ENOMEM;
5557c478bd9Sstevel@tonic-gate 		return (1);	/* get no more entries */
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	indata->last = last;
5597c478bd9Sstevel@tonic-gate 	indata->error = 0;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	return (0);
5627c478bd9Sstevel@tonic-gate }
563