xref: /onnv-gate/usr/src/lib/libmapid/common/mapid.h (revision 13080:fcc1e406c13f)
11741Srmesta /*
21741Srmesta  * CDDL HEADER START
31741Srmesta  *
41741Srmesta  * The contents of this file are subject to the terms of the
51741Srmesta  * Common Development and Distribution License (the "License").
61741Srmesta  * You may not use this file except in compliance with the License.
71741Srmesta  *
81741Srmesta  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91741Srmesta  * or http://www.opensolaris.org/os/licensing.
101741Srmesta  * See the License for the specific language governing permissions
111741Srmesta  * and limitations under the License.
121741Srmesta  *
131741Srmesta  * When distributing Covered Code, include this CDDL HEADER in each
141741Srmesta  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151741Srmesta  * If applicable, add the following below this CDDL HEADER, with the
161741Srmesta  * fields enclosed by brackets "[]" replaced with your own identifying
171741Srmesta  * information: Portions Copyright [yyyy] [name of copyright owner]
181741Srmesta  *
191741Srmesta  * CDDL HEADER END
201741Srmesta  */
211741Srmesta /*
22*13080SPavan.Mettu@Oracle.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
231741Srmesta  */
241741Srmesta 
251741Srmesta #ifndef	_MAPID_H
261741Srmesta #define	_MAPID_H
271741Srmesta 
281741Srmesta #ifdef	__cplusplus
291741Srmesta extern "C" {
301741Srmesta #endif
311741Srmesta 
321741Srmesta #include <stdio.h>
331741Srmesta #include <stdlib.h>
341741Srmesta #include <unistd.h>
351741Srmesta #include <string.h>
361741Srmesta #include <strings.h>
371741Srmesta #include <sys/types.h>
381741Srmesta #include <sys/stat.h>
391741Srmesta #include <rpc/types.h>
401741Srmesta #include <netinet/in.h>
411741Srmesta #include <arpa/nameser.h>
421741Srmesta #include <resolv.h>
431741Srmesta #include <netdb.h>
441741Srmesta #include <errno.h>
451741Srmesta #include <ctype.h>
461741Srmesta #include <sys/socket.h>
471741Srmesta #include <arpa/inet.h>
481741Srmesta #include <assert.h>
491741Srmesta #include <synch.h>
501741Srmesta #include <syslog.h>
511741Srmesta #include <locale.h>
521741Srmesta #include <thread.h>
531741Srmesta #include <deflt.h>
541741Srmesta #include <nfs/nfs4.h>
551741Srmesta 
561741Srmesta #define	DNAMEMAX			(NS_MAXCDNAME + 1)
571741Srmesta 
581741Srmesta typedef struct {
591741Srmesta 	void	*(*fcn)(void *);
601741Srmesta 	int	 signal;
611741Srmesta } cb_t;
621741Srmesta 
631741Srmesta #ifdef	__LIBMAPID_IMPL
641741Srmesta 
651741Srmesta /*
661741Srmesta  * Error Messages
671741Srmesta  */
681741Srmesta #define	EMSG_NETDB_INTERNAL	"Internal Resolver Error: %s"
691741Srmesta 
701741Srmesta #define	EMSG_TRY_AGAIN		"\"%s\" DNS nameserver(s) not responding" \
711741Srmesta 				"...\tRetrying"
721741Srmesta 
731741Srmesta #define	EMSG_NO_RECOVERY	"Unrecoverable Resolver Error: %s"
741741Srmesta 
751741Srmesta #define	EMSG_HOST_NOT_FOUND	"Authoritative nameserver unresponsive " \
761741Srmesta 				"to queries for domain \"%s\""
771741Srmesta 
781741Srmesta #define	EMSG_NO_DATA		"\"%s\" DNS TXT record not found: "\
791741Srmesta 				"Defaulting to \"%s\""
801741Srmesta 
811741Srmesta #define	EMSG_DNS_THREAD_ERROR	"Unable to create DNS query thread"
821741Srmesta 
831741Srmesta #define	EMSG_DNS_DISABLE	"%s: Further DNS queries disabled !"
841741Srmesta 
851741Srmesta #define	EMSG_DNS_RR_INVAL	"\"%s\" Invalid DNS TXT record: "\
861741Srmesta 				"Defaulting to \"%s\""
871741Srmesta 
881741Srmesta /*
891741Srmesta  * DNS related info
901741Srmesta  */
911741Srmesta #define	NFSMAPID_DNS_RR			"_nfsv4idmapdomain"
921741Srmesta #define	NFSMAPID_DNS_TOUT_SECS		(30LL)
931741Srmesta #define	NFSMAPID_SLOG_RATE		20	/* ~10 mins */
941741Srmesta 
951741Srmesta #define	NS_ERRS				6	/* netdb.h */
961741Srmesta 
971741Srmesta typedef union {
981741Srmesta 	HEADER	hdr;
991741Srmesta 	uchar_t	buf[PACKETSZ];
1001741Srmesta } ans_t;
1011741Srmesta 
1021741Srmesta /*
1031741Srmesta  * NOTE: All s_ prefixed variables are only to be used by the DNS
1041741Srmesta  *       feature implementation (mapid.c). The exported globals
1051741Srmesta  *	 (ie. seen by nfsmapid.c/nfsmapid_server.c) are the
1061741Srmesta  *       dns_ prefixed variables along with sysdns_domain.
1071741Srmesta  */
1081741Srmesta static ans_t			 s_ans;
1091741Srmesta static int			 s_anslen;
1101741Srmesta static char			 s_dname[DNAMEMAX] = {0};
1111741Srmesta static char			 s_txt_rr[DNAMEMAX] = {0};
1121741Srmesta 
1131741Srmesta static rwlock_t			 s_dns_data_lock = DEFAULTRWLOCK;
1141741Srmesta static rwlock_t			 s_dns_impl_lock = DEFAULTRWLOCK;
1151741Srmesta static mutex_t			 s_res_lock = ERRORCHECKMUTEX;
1161741Srmesta static uint32_t			 s_dns_tout = 0;
1171741Srmesta static thread_t			 s_dns_qthread;
1181741Srmesta static bool_t			 s_dns_qthr_created = FALSE;
1191741Srmesta static bool_t			 s_dns_disabled = FALSE;
1201741Srmesta static struct __res_state	 s_res = {0};
1211741Srmesta static thread_key_t		 s_thr_key;
1221741Srmesta int				 lib_init_done = 0;
1231741Srmesta 
1241741Srmesta static int			 resolv_init(void);
1251741Srmesta static void			 resolv_decode(void);
1261741Srmesta static int			 resolv_error(void);
1271741Srmesta static void			 resolv_get_txt_data(void);
1281741Srmesta static void			 resolv_txt_reset(void);
1291741Srmesta static void			 resolve_process_txt(uchar_t *, int);
1301741Srmesta static int			 resolv_search(void);
1317693SVallish.Vaidyeshwara@Sun.COM static void			 resolv_destroy(void);
1321741Srmesta static uchar_t			*resolv_skip_rr(uchar_t *, uchar_t *);
1331741Srmesta static void			 domain_sync(cb_t *, char *);
1341741Srmesta static int			 get_mtime(const char *, timestruc_t *);
1351741Srmesta static void			 get_nfs_domain(void);
1361741Srmesta static void			 get_dns_domain(void);
1371741Srmesta static void			 get_dns_txt_domain(cb_t *);
1381741Srmesta void				 _lib_init(void);
1391741Srmesta 
1401741Srmesta #ifdef	DEBUG
1411741Srmesta bool_t				 nfsmapid_debug = FALSE;
1421741Srmesta #endif	/* DEBUG */
1431741Srmesta 
1441741Srmesta /*
1451741Srmesta  * mapid_domain_lock:	rwlock used to serialize access/changes
1461741Srmesta  *			to the library's mapid_domain global var.
1471741Srmesta  *
1481741Srmesta  * mapid_domain:	Library variable used to store the current
1491741Srmesta  *			domain configured for use in decoding/encoding
1501741Srmesta  *			outbound and inbound attr strings, accordingly.
1511741Srmesta  *
152*13080SPavan.Mettu@Oracle.COM  * nfs_domain:		If nfsmapid_domain var in SMF
1531741Srmesta  *			has been set, nfs_domain will hold this
1541741Srmesta  *			value for the duration of the instance;
1551741Srmesta  *			If the value ever changes, the change is
1561741Srmesta  *			detected via the use of nfs_mtime and
1571741Srmesta  *			nfs_domain is updated accordingly.
1581741Srmesta  *
1591741Srmesta  * dns_domain:		If the system's resolver (/etc/resolv.conf)
1601741Srmesta  *			has been configured, dns_domain will hold
1611741Srmesta  *			the configured DNS domain as reported by the
1621741Srmesta  *			res_ninit() resolver interface. If the system's
1631741Srmesta  *			/etc/resolv.conf file is updated, the change
1641741Srmesta  *			is detected via the use of dns_mtime and
1651741Srmesta  *			dns_domain is updated accordingly.
1661741Srmesta  */
1671741Srmesta rwlock_t			mapid_domain_lock = DEFAULTRWLOCK;
1681741Srmesta uint32_t			mapid_domain_len = 0;
1691741Srmesta char				mapid_domain[DNAMEMAX] = {0};
1701741Srmesta 
1711741Srmesta timestruc_t			nfs_mtime = {0};
1721741Srmesta uint32_t			nfs_domain_len = 0;
1731741Srmesta char				nfs_domain[DNAMEMAX] = {0};
1741741Srmesta 
1751741Srmesta timestruc_t			dns_mtime = {0};
1761741Srmesta uint32_t			dns_domain_len = 0;
1771741Srmesta char				dns_domain[DNAMEMAX] = {0};
1781741Srmesta 
1791741Srmesta int				dns_txt_cached = 0;
1801741Srmesta uint32_t			dns_txt_domain_len = 0;
1811741Srmesta char				dns_txt_domain[DNAMEMAX] = {0};
1821741Srmesta char				sysdns_domain[DNAMEMAX] = {0};
1831741Srmesta 
1841741Srmesta timestruc_t			zapped_mtime = {0};
1851741Srmesta 
1861741Srmesta #define	ZAP_DOMAIN(which)			\
1871741Srmesta 	{					\
1881741Srmesta 		bzero(which##_domain, DNAMEMAX);\
1891741Srmesta 		which##_domain_len = 0;		\
1901741Srmesta 		which##_mtime = zapped_mtime;	\
1911741Srmesta 	}
1921741Srmesta 
1931741Srmesta #define	TIMESTRUC_EQ(a, b)			\
1941741Srmesta 		(((a).tv_sec == (b).tv_sec) &&	\
1951741Srmesta 		((a).tv_nsec == (b).tv_nsec))
1961741Srmesta 
1971741Srmesta 
1981741Srmesta 
1991741Srmesta #endif	/* __LIBMAPID_IMPL */
2001741Srmesta 
2011741Srmesta /*
2021741Srmesta  * PSARC 2005/487 Consolidation Private Interfaces
2031741Srmesta  * mapid_reeval_domain(), mapid_get_domain()
2041741Srmesta  * Changes must be reviewed by Solaris File Sharing
2051741Srmesta  */
2061741Srmesta extern void			 mapid_reeval_domain(cb_t *);
2071741Srmesta extern char			*mapid_get_domain(void);
2081741Srmesta 
2091741Srmesta /*
2101741Srmesta  * PSARC 2005/487 Contracted Sun Private Interface
2111741Srmesta  * mapid_derive_domain(), mapid_stdchk_domain()
2121741Srmesta  * Changes must be reviewed by Solaris File Sharing
2131741Srmesta  * Changes must be communicated to contract-2005-487-01@sun.com
2141741Srmesta  */
2151741Srmesta extern int			 mapid_stdchk_domain(const char *);
2161741Srmesta extern char			*mapid_derive_domain(void);
2171741Srmesta 
2181741Srmesta #ifdef	__cplusplus
2191741Srmesta }
2201741Srmesta #endif
2211741Srmesta 
2221741Srmesta #endif	/* _MAPID_H */
223