xref: /onnv-gate/usr/src/cmd/fs.d/autofs/ns_generic.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
5*11262SRajagopal.Andra@Sun.COM  * Common Development and Distribution License (the "License").
6*11262SRajagopal.Andra@Sun.COM  * 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_generic.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 <syslog.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <nsswitch.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <netdb.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <assert.h>
370Sstevel@tonic-gate #include <rpc/rpc.h>
380Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
390Sstevel@tonic-gate #include "automount.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Each name service is represented by a ns_info structure.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate struct ns_info {
450Sstevel@tonic-gate 	char	*ns_name;		/* service name */
460Sstevel@tonic-gate 	void	(*ns_init)();		/* initialization routine */
470Sstevel@tonic-gate 	int	(*ns_getmapent)();	/* get map entry given key */
480Sstevel@tonic-gate 	int	(*ns_loadmaster)();	/* load master map */
490Sstevel@tonic-gate 	int	(*ns_loaddirect)();	/* load direct map */
500Sstevel@tonic-gate 	int	(*ns_getmapkeys)();	/* readdir */
510Sstevel@tonic-gate };
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static struct ns_info ns_info[] = {
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	"files",   init_files,  getmapent_files,
560Sstevel@tonic-gate 	loadmaster_files, loaddirect_files,
570Sstevel@tonic-gate 	getmapkeys_files,
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	"ldap",   init_ldap,  getmapent_ldap,
600Sstevel@tonic-gate 	loadmaster_ldap, loaddirect_ldap,
610Sstevel@tonic-gate 	getmapkeys_ldap,
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	"nis",	   init_nis,	getmapent_nis,
640Sstevel@tonic-gate 	loadmaster_nis,   loaddirect_nis,
650Sstevel@tonic-gate 	getmapkeys_nis,
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	NULL, NULL, NULL, NULL, NULL, NULL, NULL
680Sstevel@tonic-gate };
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static struct ns_info *get_next_ns(struct __nsw_lookup **, int);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate void
ns_setup(char ** stack,char *** stkptr)730Sstevel@tonic-gate ns_setup(char **stack, char ***stkptr)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	struct ns_info *nsp;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	for (nsp = ns_info; nsp->ns_name; nsp++) {
780Sstevel@tonic-gate 		nsp->ns_init(stack, stkptr);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static struct ns_info *
get_next_ns(curr_ns,curr_nserr)830Sstevel@tonic-gate get_next_ns(curr_ns, curr_nserr)
840Sstevel@tonic-gate 	struct __nsw_lookup **curr_ns;
850Sstevel@tonic-gate 	int curr_nserr;
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	static struct __nsw_switchconfig *conf = NULL;
880Sstevel@tonic-gate 	enum __nsw_parse_err pserr;
890Sstevel@tonic-gate 	struct __nsw_lookup *lkp;
900Sstevel@tonic-gate 	struct ns_info *nsp;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if (conf == NULL) {
930Sstevel@tonic-gate 		/* __nsw_getconfig() is protected by a lock */
940Sstevel@tonic-gate 		conf = __nsw_getconfig("automount", &pserr);
950Sstevel@tonic-gate 		if (conf == NULL) {
960Sstevel@tonic-gate 			return (NULL);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (*curr_ns == NULL)
1010Sstevel@tonic-gate 		/* first time */
1020Sstevel@tonic-gate 		lkp = conf->lookups;
1030Sstevel@tonic-gate 	else {
1040Sstevel@tonic-gate 		lkp = *curr_ns;
1050Sstevel@tonic-gate 		/* __NSW_ACTION is MT-Safe */
1060Sstevel@tonic-gate 		if (__NSW_ACTION(lkp, curr_nserr) == __NSW_RETURN)
1070Sstevel@tonic-gate 			return (NULL);
1080Sstevel@tonic-gate 		lkp = lkp->next;
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	for (; lkp; lkp = lkp->next) {
1120Sstevel@tonic-gate 		for (nsp = ns_info; nsp->ns_name; nsp++) {
1130Sstevel@tonic-gate 			if (strcmp(lkp->service_name, nsp->ns_name) == 0) {
1140Sstevel@tonic-gate 				*curr_ns = lkp;
1150Sstevel@tonic-gate 				return (nsp);
1160Sstevel@tonic-gate 			}
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 		/*
1190Sstevel@tonic-gate 		 * Note: if we get here then we've found
1200Sstevel@tonic-gate 		 * an unsupported name service.
1210Sstevel@tonic-gate 		 */
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	return (NULL);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int
getmapent(key,mapname,ml,stack,stkptr,iswildcard,isrestricted)1280Sstevel@tonic-gate getmapent(key, mapname, ml, stack, stkptr, iswildcard, isrestricted)
1290Sstevel@tonic-gate 	char *key, *mapname;
1300Sstevel@tonic-gate 	struct mapline *ml;
1310Sstevel@tonic-gate 	char **stack, ***stkptr;
1320Sstevel@tonic-gate 	bool_t *iswildcard;
1330Sstevel@tonic-gate 	bool_t isrestricted;
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	struct __nsw_lookup *curr_ns = NULL;
1360Sstevel@tonic-gate 	int ns_err = __NSW_SUCCESS;
1370Sstevel@tonic-gate 	struct ns_info *nsp;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (strcmp(mapname, "-hosts") == 0) {
1400Sstevel@tonic-gate 		(void) strcpy(ml->linebuf, "-hosts");
1410Sstevel@tonic-gate 		return (__NSW_SUCCESS);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (*mapname == '/') 		/* must be a file */
1450Sstevel@tonic-gate 		return (getmapent_files(key, mapname, ml, stack, stkptr,
1460Sstevel@tonic-gate 					iswildcard, isrestricted));
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) {
1490Sstevel@tonic-gate 		ns_err = nsp->ns_getmapent(key, mapname, ml, stack, stkptr,
1500Sstevel@tonic-gate 						iswildcard, isrestricted);
1510Sstevel@tonic-gate 		if (ns_err == __NSW_SUCCESS)
1520Sstevel@tonic-gate 			return (__NSW_SUCCESS);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (__NSW_UNAVAIL);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate int
loadmaster_map(mapname,defopts,stack,stkptr)1590Sstevel@tonic-gate loadmaster_map(mapname, defopts, stack, stkptr)
1600Sstevel@tonic-gate 	char *mapname, *defopts;
1610Sstevel@tonic-gate 	char **stack, ***stkptr;
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	struct __nsw_lookup *curr_ns = NULL;
1640Sstevel@tonic-gate 	int ns_err = __NSW_SUCCESS;
1650Sstevel@tonic-gate 	struct ns_info *nsp;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (*mapname == '/')		/* must be a file */
1680Sstevel@tonic-gate 		return (loadmaster_files(mapname, defopts, stack, stkptr));
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) {
1710Sstevel@tonic-gate 		ns_err = nsp->ns_loadmaster(mapname, defopts, stack, stkptr);
1720Sstevel@tonic-gate 		if (ns_err == __NSW_SUCCESS)
1730Sstevel@tonic-gate 			return (__NSW_SUCCESS);
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	return (__NSW_UNAVAIL);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
179249Sjwahlig int
loaddirect_map(mapname,localmap,defopts,stack,stkptr)1800Sstevel@tonic-gate loaddirect_map(mapname, localmap, defopts, stack, stkptr)
1810Sstevel@tonic-gate 	char *mapname, *localmap, *defopts;
1820Sstevel@tonic-gate 	char **stack, ***stkptr;
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	struct __nsw_lookup *curr_ns = NULL;
1850Sstevel@tonic-gate 	int ns_err = __NSW_SUCCESS;
1860Sstevel@tonic-gate 	struct ns_info *nsp;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (*mapname == '/')		/* must be a file */
1890Sstevel@tonic-gate 		return (loaddirect_files(mapname, localmap, defopts,
1900Sstevel@tonic-gate 				stack, stkptr));
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) {
1930Sstevel@tonic-gate 		ns_err = nsp->ns_loaddirect(mapname, localmap, defopts, stack,
1940Sstevel@tonic-gate 					stkptr);
1950Sstevel@tonic-gate 		if (ns_err == __NSW_SUCCESS)
1960Sstevel@tonic-gate 			return (__NSW_SUCCESS);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	return (__NSW_UNAVAIL);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate int
gethostkeys(mapname,list,error,cache_time)2030Sstevel@tonic-gate gethostkeys(mapname, list, error, cache_time)
2040Sstevel@tonic-gate 	char *mapname;
2050Sstevel@tonic-gate 	struct dir_entry **list;
2060Sstevel@tonic-gate 	int *error;
2070Sstevel@tonic-gate 	int *cache_time;
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	char *buffer, **p;
2100Sstevel@tonic-gate 	int bufferlen = 1000;
2110Sstevel@tonic-gate 	struct dir_entry *last = NULL;
2120Sstevel@tonic-gate 	struct hostent ent;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate #ifdef lint
2150Sstevel@tonic-gate 	mapname = mapname;
2160Sstevel@tonic-gate #endif
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	*cache_time = RDDIR_CACHE_TIME * 2;
2190Sstevel@tonic-gate 	*error = 0;
2200Sstevel@tonic-gate 	if (trace  > 1)
2210Sstevel@tonic-gate 		trace_prt(1, "gethostkeys called\n");
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (sethostent(1)) {
2240Sstevel@tonic-gate 		syslog(LOG_ERR, "gethostkeys: sethostent failed");
2250Sstevel@tonic-gate 		*error = EIO;
2260Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	buffer = (char *)malloc(bufferlen);
2300Sstevel@tonic-gate 	if (buffer == NULL) {
2310Sstevel@tonic-gate 		syslog(LOG_ERR, "gethostkeys: malloc of buffer failed");
2320Sstevel@tonic-gate 		*error = ENOMEM;
2330Sstevel@tonic-gate 		return (__NSW_UNAVAIL);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	while (gethostent_r(&ent, buffer, bufferlen, error)) {
2370Sstevel@tonic-gate 		/*
2380Sstevel@tonic-gate 		 * add canonical name
2390Sstevel@tonic-gate 		 */
2400Sstevel@tonic-gate 		if (add_dir_entry(ent.h_name, list, &last)) {
2410Sstevel@tonic-gate 			*error = ENOMEM;
2420Sstevel@tonic-gate 			goto done;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 		if (ent.h_aliases == NULL)
2450Sstevel@tonic-gate 			goto done;	/* no aliases */
2460Sstevel@tonic-gate 		for (p = ent.h_aliases; *p != 0; p++) {
2470Sstevel@tonic-gate 			if (strcmp(*p, ent.h_name) != 0) {
2480Sstevel@tonic-gate 				/*
2490Sstevel@tonic-gate 				 * add alias only if different
2500Sstevel@tonic-gate 				 * from canonical name
2510Sstevel@tonic-gate 				 */
2520Sstevel@tonic-gate 				if (add_dir_entry(*p, list, &last)) {
2530Sstevel@tonic-gate 					*error = ENOMEM;
2540Sstevel@tonic-gate 					goto done;
2550Sstevel@tonic-gate 				}
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 		assert(last != NULL);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate done:	if (*list != NULL) {
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * list of entries found
2630Sstevel@tonic-gate 		 */
2640Sstevel@tonic-gate 		*error = 0;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	endhostent();
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	return (__NSW_SUCCESS);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate  * enumerate all entries in the map in the various name services.
2730Sstevel@tonic-gate  */
274249Sjwahlig int
getmapkeys(mapname,list,error,cache_time,stack,stkptr,uid)2750Sstevel@tonic-gate getmapkeys(mapname, list, error, cache_time, stack, stkptr, uid)
2760Sstevel@tonic-gate 	char *mapname;
2770Sstevel@tonic-gate 	struct dir_entry **list;
2780Sstevel@tonic-gate 	int *error;
2790Sstevel@tonic-gate 	int *cache_time;
2800Sstevel@tonic-gate 	char **stack, ***stkptr;
2810Sstevel@tonic-gate 	uid_t uid;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate 	struct __nsw_lookup *curr_ns = NULL;
2850Sstevel@tonic-gate 	int ns_err = __NSW_SUCCESS;
2860Sstevel@tonic-gate 	int success = 0;
2870Sstevel@tonic-gate 	struct ns_info *nsp;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (*mapname == '/') 		/* must be a file */
2900Sstevel@tonic-gate 		return (getmapkeys_files(mapname, list, error, cache_time,
2910Sstevel@tonic-gate 				stack, stkptr));
2920Sstevel@tonic-gate 	if (strcmp(mapname, "-hosts") == 0) {
2930Sstevel@tonic-gate 		return (gethostkeys(mapname, list, error, cache_time));
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	while ((nsp = get_next_ns(&curr_ns, ns_err)) != NULL) {
2970Sstevel@tonic-gate 		ns_err = nsp->ns_getmapkeys(mapname, list, error,
2980Sstevel@tonic-gate 				cache_time, stack, stkptr);
2990Sstevel@tonic-gate 		if (*error == 0) {
3000Sstevel@tonic-gate 			/*
3010Sstevel@tonic-gate 			 * return success if listing was successful
3020Sstevel@tonic-gate 			 * for at least one name service
3030Sstevel@tonic-gate 			 */
3040Sstevel@tonic-gate 			success++;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		/*
3080Sstevel@tonic-gate 		 * XXX force next name service
3090Sstevel@tonic-gate 		 */
3100Sstevel@tonic-gate 		if (ns_err != __NSW_UNAVAIL)
3110Sstevel@tonic-gate 			ns_err = __NSW_NOTFOUND;
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	if (success) {
3140Sstevel@tonic-gate 		/*
3150Sstevel@tonic-gate 		 * if succeeded at least once, return error=0
3160Sstevel@tonic-gate 		 */
3170Sstevel@tonic-gate 		*error = 0;
3180Sstevel@tonic-gate 	};
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	return (success ? __NSW_SUCCESS : __NSW_NOTFOUND);
3210Sstevel@tonic-gate }
322