xref: /onnv-gate/usr/src/cmd/fs.d/autofs/ns_nis.c (revision 11262:b7ebfbf2359e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52170Sevanl  * Common Development and Distribution License (the "License").
62170Sevanl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  *	ns_nis.c
230Sstevel@tonic-gate  *
24*11262SRajagopal.Andra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <syslog.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <nsswitch.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/systeminfo.h>
380Sstevel@tonic-gate #include <rpc/rpc.h>
390Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
400Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
410Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include "automount.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #define	KEY		0
460Sstevel@tonic-gate #define	CONTENTS	1
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static int replace_undscr_by_dot(char *);
490Sstevel@tonic-gate static int nis_err(int);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static char nis_mydomain[YPMAXDOMAIN];
520Sstevel@tonic-gate 
530Sstevel@tonic-gate struct dir_cbdata {
540Sstevel@tonic-gate 	struct dir_entry **list;
550Sstevel@tonic-gate 	struct dir_entry *last;
560Sstevel@tonic-gate 	int error;
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static int readdir_callback(int, char *, int, const char *,
600Sstevel@tonic-gate 				int, struct dir_cbdata *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate void
init_nis(char ** stack,char *** stkptr)630Sstevel@tonic-gate init_nis(char **stack, char ***stkptr)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate #ifdef lint
660Sstevel@tonic-gate 	stack = stack;
670Sstevel@tonic-gate 	stkptr = stkptr;
680Sstevel@tonic-gate #endif /* lint */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	(void) sysinfo(SI_SRPC_DOMAIN, nis_mydomain, sizeof (nis_mydomain));
710Sstevel@tonic-gate }
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*ARGSUSED*/
740Sstevel@tonic-gate int
getmapent_nis(key,map,ml,stack,stkptr,iswildcard,isrestricted)750Sstevel@tonic-gate getmapent_nis(key, map, ml, stack, stkptr, iswildcard, isrestricted)
760Sstevel@tonic-gate 	char *key, *map;
770Sstevel@tonic-gate 	struct mapline *ml;
780Sstevel@tonic-gate 	char **stack;
790Sstevel@tonic-gate 	char ***stkptr;
800Sstevel@tonic-gate 	bool_t *iswildcard;
810Sstevel@tonic-gate 	bool_t isrestricted;
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	char *nisline = NULL;
840Sstevel@tonic-gate 	char *my_map = NULL;
850Sstevel@tonic-gate 	char *lp, *lq;
860Sstevel@tonic-gate 	int nislen, len;
870Sstevel@tonic-gate 	int nserr;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (iswildcard)
900Sstevel@tonic-gate 		*iswildcard = FALSE;
910Sstevel@tonic-gate 	nserr = yp_match(nis_mydomain, map, key, strlen(key),
920Sstevel@tonic-gate 						&nisline, &nislen);
930Sstevel@tonic-gate 	if (nserr == YPERR_MAP) {
940Sstevel@tonic-gate 		my_map = strdup(map);
950Sstevel@tonic-gate 		if (my_map == NULL) {
960Sstevel@tonic-gate 			syslog(LOG_ERR,
970Sstevel@tonic-gate 				"getmapent_nis: memory alloc failed: %m");
980Sstevel@tonic-gate 			return (__NSW_UNAVAIL);
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 		if (replace_undscr_by_dot(my_map))
1010Sstevel@tonic-gate 			nserr = yp_match(nis_mydomain, my_map, key,
1020Sstevel@tonic-gate 					strlen(key), &nisline, &nislen);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (nserr) {
1060Sstevel@tonic-gate 		if (nserr == YPERR_KEY) {
1070Sstevel@tonic-gate 			/*
1080Sstevel@tonic-gate 			 * Try the default entry "*"
1090Sstevel@tonic-gate 			 */
1100Sstevel@tonic-gate 			if (my_map == NULL)
1110Sstevel@tonic-gate 				nserr = yp_match(nis_mydomain, map, "*", 1,
1120Sstevel@tonic-gate 						&nisline, &nislen);
1130Sstevel@tonic-gate 			else
1140Sstevel@tonic-gate 				nserr = yp_match(nis_mydomain, my_map, "*", 1,
1150Sstevel@tonic-gate 						&nisline, &nislen);
1160Sstevel@tonic-gate 			if (!nserr && iswildcard)
1170Sstevel@tonic-gate 				*iswildcard = TRUE;
1180Sstevel@tonic-gate 		} else {
1190Sstevel@tonic-gate 			if (verbose)
1200Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
1210Sstevel@tonic-gate 					map, yperr_string(nserr));
1220Sstevel@tonic-gate 			nserr = 1;
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	if (my_map != NULL)
1260Sstevel@tonic-gate 		free(my_map);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	nserr = nis_err(nserr);
1290Sstevel@tonic-gate 	if (nserr)
1300Sstevel@tonic-gate 		goto done;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/*
1330Sstevel@tonic-gate 	 * at this point we are sure that yp_match succeeded
1340Sstevel@tonic-gate 	 * so massage the entry by
1350Sstevel@tonic-gate 	 * 1. ignoring # and beyond
1360Sstevel@tonic-gate 	 * 2. trim the trailing whitespace
1370Sstevel@tonic-gate 	 */
1380Sstevel@tonic-gate 	if (lp = strchr(nisline, '#'))
1390Sstevel@tonic-gate 		*lp = '\0';
1400Sstevel@tonic-gate 	len = strlen(nisline);
1410Sstevel@tonic-gate 	if (len == 0) {
1420Sstevel@tonic-gate 		nserr = __NSW_NOTFOUND;
1430Sstevel@tonic-gate 		goto done;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	lp = &nisline[len - 1];
1460Sstevel@tonic-gate 	while (lp > nisline && isspace(*lp))
1470Sstevel@tonic-gate 		*lp-- = '\0';
1480Sstevel@tonic-gate 	if (lp == nisline) {
1490Sstevel@tonic-gate 		nserr = __NSW_NOTFOUND;
1500Sstevel@tonic-gate 		goto done;
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 	(void) strcpy(ml->linebuf, nisline);
1530Sstevel@tonic-gate 	lp = ml->linebuf;
1540Sstevel@tonic-gate 	lq = ml->lineqbuf;
1550Sstevel@tonic-gate 	unquote(lp, lq);
1560Sstevel@tonic-gate 	/* now we have the correct line */
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	nserr = __NSW_SUCCESS;
1590Sstevel@tonic-gate done:
1600Sstevel@tonic-gate 	if (nisline)
1610Sstevel@tonic-gate 		free((char *)nisline);
1620Sstevel@tonic-gate 	return (nserr);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
166249Sjwahlig int
loadmaster_nis(mapname,defopts,stack,stkptr)1670Sstevel@tonic-gate loadmaster_nis(mapname, defopts, stack, stkptr)
1680Sstevel@tonic-gate 	char *mapname, *defopts;
1690Sstevel@tonic-gate 	char **stack;
1700Sstevel@tonic-gate 	char ***stkptr;
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	int first, err;
1730Sstevel@tonic-gate 	char *key, *nkey, *val;
1740Sstevel@tonic-gate 	int kl, nkl, vl;
1750Sstevel@tonic-gate 	char dir[256], map[256], qbuff[256];
1760Sstevel@tonic-gate 	char *pmap, *opts, *my_mapname;
1770Sstevel@tonic-gate 	int count = 0;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	first = 1;
1800Sstevel@tonic-gate 	key  = NULL; kl  = 0;
1810Sstevel@tonic-gate 	nkey = NULL; nkl = 0;
1820Sstevel@tonic-gate 	val  = NULL; vl  = 0;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * need a private copy of mapname, because we may change
1860Sstevel@tonic-gate 	 * the underscores by dots. We however do not want the
1870Sstevel@tonic-gate 	 * orignal to be changed, as we may want to use the
1880Sstevel@tonic-gate 	 * original name in some other name service
1890Sstevel@tonic-gate 	 */
1900Sstevel@tonic-gate 	my_mapname = strdup(mapname);
1910Sstevel@tonic-gate 	if (my_mapname == NULL) {
1920Sstevel@tonic-gate 		syslog(LOG_ERR, "loadmaster_yp: memory alloc failed: %m");
1930Sstevel@tonic-gate 		/* not the name svc's fault but ... */
1940Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	for (;;) {
1970Sstevel@tonic-gate 		if (first) {
1980Sstevel@tonic-gate 			first = 0;
1990Sstevel@tonic-gate 			err = yp_first(nis_mydomain, my_mapname,
2000Sstevel@tonic-gate 				&nkey, &nkl, &val, &vl);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 			if ((err == YPERR_MAP) &&
2030Sstevel@tonic-gate 			    (replace_undscr_by_dot(my_mapname)))
2040Sstevel@tonic-gate 				err = yp_first(nis_mydomain, my_mapname,
2050Sstevel@tonic-gate 					&nkey, &nkl, &val, &vl);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 			if ((err == YPERR_DOMAIN) || (err == YPERR_YPBIND)) {
2080Sstevel@tonic-gate 				syslog(LOG_ERR,
2090Sstevel@tonic-gate 					"can't read nis map %s: %s - retrying",
2100Sstevel@tonic-gate 					my_mapname, yperr_string(err));
2110Sstevel@tonic-gate 				while ((err == YPERR_DOMAIN) ||
2120Sstevel@tonic-gate 					(err == YPERR_YPBIND)) {
2130Sstevel@tonic-gate 					(void) sleep(20);
2140Sstevel@tonic-gate 					err = yp_first(nis_mydomain, my_mapname,
2150Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
2160Sstevel@tonic-gate 				}
2170Sstevel@tonic-gate 				syslog(LOG_ERR,
2180Sstevel@tonic-gate 					"nis map %s: read OK.", my_mapname);
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 		} else {
2210Sstevel@tonic-gate 			err = yp_next(nis_mydomain, my_mapname, key, kl,
2220Sstevel@tonic-gate 				&nkey, &nkl, &val, &vl);
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		if (err) {
2250Sstevel@tonic-gate 			if (err != YPERR_NOMORE && err != YPERR_MAP)
2260Sstevel@tonic-gate 				if (verbose)
2270Sstevel@tonic-gate 					syslog(LOG_ERR, "%s: %s",
2280Sstevel@tonic-gate 					my_mapname, yperr_string(err));
2290Sstevel@tonic-gate 			break;
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 		if (key)
2320Sstevel@tonic-gate 			free(key);
2330Sstevel@tonic-gate 		key = nkey;
2340Sstevel@tonic-gate 		kl = nkl;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		if (kl >= 256 || vl >= 256)
2380Sstevel@tonic-gate 			break;
2390Sstevel@tonic-gate 		if (kl < 2 || vl < 1)
2400Sstevel@tonic-gate 			break;
2410Sstevel@tonic-gate 		if (isspace(*key) || *key == '#')
2420Sstevel@tonic-gate 			break;
2430Sstevel@tonic-gate 		(void) strncpy(dir, key, kl);
2440Sstevel@tonic-gate 		dir[kl] = '\0';
2450Sstevel@tonic-gate 		if (macro_expand("", dir, qbuff, sizeof (dir))) {
2460Sstevel@tonic-gate 			syslog(LOG_ERR,
2470Sstevel@tonic-gate 			    "%s in NIS map %s: entry too long (max %d chars)",
2480Sstevel@tonic-gate 			    dir, my_mapname, sizeof (dir) - 1);
2490Sstevel@tonic-gate 			break;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		(void) strncpy(map, val, vl);
2520Sstevel@tonic-gate 		map[vl] = '\0';
2530Sstevel@tonic-gate 		if (macro_expand(dir, map, qbuff, sizeof (map))) {
2540Sstevel@tonic-gate 			syslog(LOG_ERR,
2550Sstevel@tonic-gate 			    "%s in NIS map %s: entry too long (max %d chars)",
2560Sstevel@tonic-gate 			    map, my_mapname, sizeof (map) - 1);
2570Sstevel@tonic-gate 			break;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 		pmap = map;
2600Sstevel@tonic-gate 		while (*pmap && isspace(*pmap))
2610Sstevel@tonic-gate 			pmap++;		/* skip blanks in front of map */
2620Sstevel@tonic-gate 		opts = pmap;
2630Sstevel@tonic-gate 		while (*opts && !isspace(*opts))
2640Sstevel@tonic-gate 			opts++;
2650Sstevel@tonic-gate 		if (*opts) {
2660Sstevel@tonic-gate 			*opts++ = '\0';
2670Sstevel@tonic-gate 			while (*opts && isspace(*opts))
2680Sstevel@tonic-gate 				opts++;
2690Sstevel@tonic-gate 			if (*opts == '-')
2700Sstevel@tonic-gate 				opts++;
2710Sstevel@tonic-gate 			else
2720Sstevel@tonic-gate 				opts = defopts;
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 		free(val);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		/*
2770Sstevel@tonic-gate 		 * Check for no embedded blanks.
2780Sstevel@tonic-gate 		 */
2790Sstevel@tonic-gate 		if (strcspn(opts, " 	") == strlen(opts)) {
2800Sstevel@tonic-gate 			dirinit(dir, pmap, opts, 0, stack, stkptr);
2810Sstevel@tonic-gate 			count++;
2820Sstevel@tonic-gate 		} else {
2830Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in NIS map %s ignored.\n", dir, mapname);
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (my_mapname)
2880Sstevel@tonic-gate 		free(my_mapname);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/*
2910Sstevel@tonic-gate 	 * In the context of a master map, if no entry is
2920Sstevel@tonic-gate 	 * found, it is like NOTFOUND
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	if (count > 0 && err == YPERR_NOMORE)
2950Sstevel@tonic-gate 		return (__NSW_SUCCESS);
2960Sstevel@tonic-gate 	else {
2970Sstevel@tonic-gate 		if (err)
2980Sstevel@tonic-gate 			return (nis_err(err));
2990Sstevel@tonic-gate 		else
3000Sstevel@tonic-gate 			/*
3010Sstevel@tonic-gate 			 * This case will happen if map is empty
3020Sstevel@tonic-gate 			 *  or none of the entries is valid
3030Sstevel@tonic-gate 			 */
3040Sstevel@tonic-gate 			return (__NSW_NOTFOUND);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
308249Sjwahlig int
loaddirect_nis(nismap,localmap,opts,stack,stkptr)3090Sstevel@tonic-gate loaddirect_nis(nismap, localmap, opts, stack, stkptr)
3100Sstevel@tonic-gate 	char *nismap, *localmap, *opts;
3110Sstevel@tonic-gate 	char **stack;
3120Sstevel@tonic-gate 	char ***stkptr;
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	int first, err, count;
3150Sstevel@tonic-gate 	char *key, *nkey, *val, *my_nismap;
3160Sstevel@tonic-gate 	int kl, nkl, vl;
3170Sstevel@tonic-gate 	char dir[100];
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	first = 1;
3200Sstevel@tonic-gate 	key  = NULL; kl  = 0;
3210Sstevel@tonic-gate 	nkey = NULL; nkl = 0;
3220Sstevel@tonic-gate 	val  = NULL; vl  = 0;
3230Sstevel@tonic-gate 	count = 0;
3240Sstevel@tonic-gate 	my_nismap = NULL;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	my_nismap = strdup(nismap);
3270Sstevel@tonic-gate 	if (my_nismap == NULL) {
3280Sstevel@tonic-gate 		syslog(LOG_ERR, "loadmaster_yp: memory alloc failed: %m");
3290Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	for (;;) {
3320Sstevel@tonic-gate 		if (first) {
3330Sstevel@tonic-gate 			first = 0;
3340Sstevel@tonic-gate 			err = yp_first(nis_mydomain, my_nismap, &nkey, &nkl,
3350Sstevel@tonic-gate 					&val, &vl);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			if ((err == YPERR_MAP) &&
3380Sstevel@tonic-gate 			    (replace_undscr_by_dot(my_nismap)))
3390Sstevel@tonic-gate 				err = yp_first(nis_mydomain, my_nismap,
3400Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 			if ((err == YPERR_DOMAIN) || (err == YPERR_YPBIND)) {
3430Sstevel@tonic-gate 				syslog(LOG_ERR,
3440Sstevel@tonic-gate 					"can't read nis map %s: %s - retrying",
3450Sstevel@tonic-gate 					my_nismap, yperr_string(err));
3460Sstevel@tonic-gate 				while ((err == YPERR_DOMAIN) ||
3470Sstevel@tonic-gate 					(err == YPERR_YPBIND)) {
3480Sstevel@tonic-gate 					(void) sleep(20);
3490Sstevel@tonic-gate 					err = yp_first(nis_mydomain, my_nismap,
3500Sstevel@tonic-gate 						&nkey, &nkl, &val, &vl);
3510Sstevel@tonic-gate 				}
3520Sstevel@tonic-gate 				syslog(LOG_ERR,
3530Sstevel@tonic-gate 					"nis map %s: read OK.", my_nismap);
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 		} else {
3560Sstevel@tonic-gate 			err = yp_next(nis_mydomain, my_nismap, key, kl,
3570Sstevel@tonic-gate 					&nkey, &nkl, &val, &vl);
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 		if (err) {
3600Sstevel@tonic-gate 			if (err != YPERR_NOMORE && err != YPERR_MAP)
3610Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
3620Sstevel@tonic-gate 					my_nismap, yperr_string(err));
3630Sstevel@tonic-gate 			break;
3640Sstevel@tonic-gate 		}
3650Sstevel@tonic-gate 		if (key)
3660Sstevel@tonic-gate 			free(key);
3670Sstevel@tonic-gate 		key = nkey;
3680Sstevel@tonic-gate 		kl = nkl;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		if (kl < 2 || kl >= 100)
3710Sstevel@tonic-gate 			continue;
3720Sstevel@tonic-gate 		if (isspace(*key) || *key == '#')
3730Sstevel@tonic-gate 			continue;
3740Sstevel@tonic-gate 		(void) strncpy(dir, key, kl);
3750Sstevel@tonic-gate 		dir[kl] = '\0';
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		dirinit(dir, localmap, opts, 1, stack, stkptr);
3780Sstevel@tonic-gate 		count++;
3790Sstevel@tonic-gate 		free(val);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (my_nismap)
3830Sstevel@tonic-gate 		free(my_nismap);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if (count > 0 && err == YPERR_NOMORE)
3860Sstevel@tonic-gate 			return (__NSW_SUCCESS);
3870Sstevel@tonic-gate 	else
3880Sstevel@tonic-gate 		return (nis_err(err));
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate static int
replace_undscr_by_dot(map)3930Sstevel@tonic-gate replace_undscr_by_dot(map)
3940Sstevel@tonic-gate 	char *map;
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	int ret_val = 0;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	while (*map) {
3990Sstevel@tonic-gate 		if (*map == '_') {
4000Sstevel@tonic-gate 			ret_val = 1;
4010Sstevel@tonic-gate 			*map = '.';
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		map++;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	return (ret_val);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate static int
nis_err(err)4090Sstevel@tonic-gate nis_err(err)
4100Sstevel@tonic-gate 	int err;
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	switch (err) {
4130Sstevel@tonic-gate 	case 0:
4140Sstevel@tonic-gate 		return (__NSW_SUCCESS);
4150Sstevel@tonic-gate 	case YPERR_KEY:
4160Sstevel@tonic-gate 		return (__NSW_NOTFOUND);
4170Sstevel@tonic-gate 	case YPERR_MAP:
4180Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4190Sstevel@tonic-gate 	default:
4200Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate int
getmapkeys_nis(nsmap,list,error,cache_time,stack,stkptr)4250Sstevel@tonic-gate getmapkeys_nis(nsmap, list, error, cache_time, stack, stkptr)
4260Sstevel@tonic-gate 	char *nsmap;
4270Sstevel@tonic-gate 	struct dir_entry **list;
4280Sstevel@tonic-gate 	int *error;
4290Sstevel@tonic-gate 	int *cache_time;
4300Sstevel@tonic-gate 	char **stack;
4310Sstevel@tonic-gate 	char ***stkptr;
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	int nserr;
4340Sstevel@tonic-gate 	struct dir_cbdata readdir_cbdata;
4350Sstevel@tonic-gate 	struct ypall_callback cback;
4360Sstevel@tonic-gate 	char *my_map = NULL;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	char *key = NULL, *val = NULL;
4390Sstevel@tonic-gate 	int nkl, vl;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate #ifdef lint
4420Sstevel@tonic-gate 	stack = stack;
4430Sstevel@tonic-gate 	stkptr = stkptr;
4440Sstevel@tonic-gate #endif /* lint */
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	*cache_time = RDDIR_CACHE_TIME;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * XXX Hack to determine if we need to replace '_' with '.'
4500Sstevel@tonic-gate 	 * Have to use yp_first() since yp_all() simply fails if
4510Sstevel@tonic-gate 	 * the map is not present
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	my_map = strdup(nsmap);
4540Sstevel@tonic-gate 	if (my_map == NULL) {
4550Sstevel@tonic-gate 		syslog(LOG_ERR,
4560Sstevel@tonic-gate 			"getmapkeys_nis: memory alloc failed: %m");
4570Sstevel@tonic-gate 		*error = ENOMEM;
4580Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	nserr = yp_first(nis_mydomain, my_map, &key, &nkl, &val, &vl);
4610Sstevel@tonic-gate 	if (nserr == YPERR_MAP) {
4620Sstevel@tonic-gate 		if (replace_undscr_by_dot(my_map)) {
4630Sstevel@tonic-gate 			nserr = yp_first(nis_mydomain, my_map,
4640Sstevel@tonic-gate 					&key, &nkl, &val, &vl);
4650Sstevel@tonic-gate 		}
4660Sstevel@tonic-gate 		if (nserr == YPERR_MAP) {
4670Sstevel@tonic-gate 			/*
4680Sstevel@tonic-gate 			 * map not found
4690Sstevel@tonic-gate 			 */
4700Sstevel@tonic-gate 			*error = 0;	/* return an empty list */
4710Sstevel@tonic-gate 			if (verbose) {
4720Sstevel@tonic-gate 				syslog(LOG_ERR, "%s: %s",
4730Sstevel@tonic-gate 					nsmap, yperr_string(nserr));
4740Sstevel@tonic-gate 			}
4750Sstevel@tonic-gate 			free(my_map);
4760Sstevel@tonic-gate 			return (nis_err(nserr));
4770Sstevel@tonic-gate 		}
4780Sstevel@tonic-gate 	}
4792170Sevanl 	if (key)
4802170Sevanl 		free(key);
4812170Sevanl 	if (val)
4822170Sevanl 		free(val);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	readdir_cbdata.list = list;
4850Sstevel@tonic-gate 	readdir_cbdata.last = NULL;
4860Sstevel@tonic-gate 	readdir_cbdata.error = 0;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	cback.foreach = readdir_callback;
4890Sstevel@tonic-gate 	cback.data = (char *)&readdir_cbdata;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * after all this song and dance we finally
4930Sstevel@tonic-gate 	 * ask for the list of entries
4940Sstevel@tonic-gate 	 */
4950Sstevel@tonic-gate 	nserr = yp_all(nis_mydomain, my_map, &cback);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	free(my_map);
4980Sstevel@tonic-gate 	*error = readdir_cbdata.error;
4990Sstevel@tonic-gate 	if (nserr) {
5000Sstevel@tonic-gate 		if (verbose)
5010Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s", nsmap, yperr_string(nserr));
5020Sstevel@tonic-gate 		nserr = 1;
5030Sstevel@tonic-gate 		if (*error == 0)
5040Sstevel@tonic-gate 			*error = ENOENT;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		return (nis_err(nserr));
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	return (__NSW_SUCCESS);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate static int
readdir_callback(instatus,inkey,inkeylen,inval,invallen,indata)5130Sstevel@tonic-gate readdir_callback(instatus, inkey, inkeylen, inval, invallen, indata)
5140Sstevel@tonic-gate 	int instatus;
5150Sstevel@tonic-gate 	char *inkey;
5160Sstevel@tonic-gate 	int inkeylen;
5170Sstevel@tonic-gate 	const char *inval;
5180Sstevel@tonic-gate 	int invallen;
5190Sstevel@tonic-gate 	struct dir_cbdata *indata;
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	struct dir_entry **list = indata->list;
5220Sstevel@tonic-gate 	struct dir_entry *last = indata->last;
5230Sstevel@tonic-gate 	char key[MAXPATHLEN];
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate #ifdef lint
5260Sstevel@tonic-gate 	inval = inval;
5270Sstevel@tonic-gate 	invallen = invallen;
5280Sstevel@tonic-gate #endif
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (instatus != YP_TRUE)
5310Sstevel@tonic-gate 		return (0);	/* next entry. yp_all may decide otherwise... */
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (inkeylen == 0 || isspace(*inkey) || *inkey == '#')
5340Sstevel@tonic-gate 		return (0);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * yp_all allocates inkey with two extra bytes which contain
5380Sstevel@tonic-gate 	 * NEWLINE and null but these two bytes are not reflected in
5390Sstevel@tonic-gate 	 * inkeylen.
5400Sstevel@tonic-gate 	 */
5410Sstevel@tonic-gate 	strncpy(key, inkey, inkeylen);
5420Sstevel@tonic-gate 	key[inkeylen] = '\0';
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * Wildcard entry should be ignored - following entries should continue
5460Sstevel@tonic-gate 	 * to be read to corroborate with the way we search for entries in yp,
5470Sstevel@tonic-gate 	 * i.e., first for an exact key match and then a wildcard, if there's
5480Sstevel@tonic-gate 	 * no exact key match.
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	if (key[0] == '*' && key[1] == '\0')
5510Sstevel@tonic-gate 		return (0);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if (add_dir_entry(key, list, &last)) {
5540Sstevel@tonic-gate 		indata->error = ENOMEM;
5550Sstevel@tonic-gate 		return (1);	/* get no more entries */
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	indata->last = last;
5590Sstevel@tonic-gate 	indata->error = 0;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	return (0);
5620Sstevel@tonic-gate }
563