xref: /titanic_51/usr/src/lib/libmapid/common/mapid.h (revision bfa62c284402a2e7ee6a349df0ef8d1aa2e908e8)
18200fe25Srmesta /*
28200fe25Srmesta  * CDDL HEADER START
38200fe25Srmesta  *
48200fe25Srmesta  * The contents of this file are subject to the terms of the
58200fe25Srmesta  * Common Development and Distribution License (the "License").
68200fe25Srmesta  * You may not use this file except in compliance with the License.
78200fe25Srmesta  *
88200fe25Srmesta  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98200fe25Srmesta  * or http://www.opensolaris.org/os/licensing.
108200fe25Srmesta  * See the License for the specific language governing permissions
118200fe25Srmesta  * and limitations under the License.
128200fe25Srmesta  *
138200fe25Srmesta  * When distributing Covered Code, include this CDDL HEADER in each
148200fe25Srmesta  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158200fe25Srmesta  * If applicable, add the following below this CDDL HEADER, with the
168200fe25Srmesta  * fields enclosed by brackets "[]" replaced with your own identifying
178200fe25Srmesta  * information: Portions Copyright [yyyy] [name of copyright owner]
188200fe25Srmesta  *
198200fe25Srmesta  * CDDL HEADER END
208200fe25Srmesta  */
218200fe25Srmesta /*
22*bfa62c28SVallish Vaidyeshwara  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
238200fe25Srmesta  * Use is subject to license terms.
248200fe25Srmesta  */
258200fe25Srmesta 
268200fe25Srmesta #ifndef	_MAPID_H
278200fe25Srmesta #define	_MAPID_H
288200fe25Srmesta 
298200fe25Srmesta #ifdef	__cplusplus
308200fe25Srmesta extern "C" {
318200fe25Srmesta #endif
328200fe25Srmesta 
338200fe25Srmesta #include <stdio.h>
348200fe25Srmesta #include <stdlib.h>
358200fe25Srmesta #include <unistd.h>
368200fe25Srmesta #include <string.h>
378200fe25Srmesta #include <strings.h>
388200fe25Srmesta #include <sys/types.h>
398200fe25Srmesta #include <sys/stat.h>
408200fe25Srmesta #include <rpc/types.h>
418200fe25Srmesta #include <netinet/in.h>
428200fe25Srmesta #include <arpa/nameser.h>
438200fe25Srmesta #include <resolv.h>
448200fe25Srmesta #include <netdb.h>
458200fe25Srmesta #include <errno.h>
468200fe25Srmesta #include <ctype.h>
478200fe25Srmesta #include <sys/socket.h>
488200fe25Srmesta #include <arpa/inet.h>
498200fe25Srmesta #include <assert.h>
508200fe25Srmesta #include <synch.h>
518200fe25Srmesta #include <syslog.h>
528200fe25Srmesta #include <locale.h>
538200fe25Srmesta #include <thread.h>
548200fe25Srmesta #include <deflt.h>
558200fe25Srmesta #include <nfs/nfs4.h>
568200fe25Srmesta 
578200fe25Srmesta #define	DNAMEMAX			(NS_MAXCDNAME + 1)
588200fe25Srmesta 
598200fe25Srmesta typedef struct {
608200fe25Srmesta 	void	*(*fcn)(void *);
618200fe25Srmesta 	int	 signal;
628200fe25Srmesta } cb_t;
638200fe25Srmesta 
648200fe25Srmesta #ifdef	__LIBMAPID_IMPL
658200fe25Srmesta 
668200fe25Srmesta /*
678200fe25Srmesta  * Error Messages
688200fe25Srmesta  */
698200fe25Srmesta #define	EMSG_NETDB_INTERNAL	"Internal Resolver Error: %s"
708200fe25Srmesta 
718200fe25Srmesta #define	EMSG_TRY_AGAIN		"\"%s\" DNS nameserver(s) not responding" \
728200fe25Srmesta 				"...\tRetrying"
738200fe25Srmesta 
748200fe25Srmesta #define	EMSG_NO_RECOVERY	"Unrecoverable Resolver Error: %s"
758200fe25Srmesta 
768200fe25Srmesta #define	EMSG_HOST_NOT_FOUND	"Authoritative nameserver unresponsive " \
778200fe25Srmesta 				"to queries for domain \"%s\""
788200fe25Srmesta 
798200fe25Srmesta #define	EMSG_NO_DATA		"\"%s\" DNS TXT record not found: "\
808200fe25Srmesta 				"Defaulting to \"%s\""
818200fe25Srmesta 
828200fe25Srmesta #define	EMSG_DNS_THREAD_ERROR	"Unable to create DNS query thread"
838200fe25Srmesta 
848200fe25Srmesta #define	EMSG_DNS_DISABLE	"%s: Further DNS queries disabled !"
858200fe25Srmesta 
868200fe25Srmesta #define	EMSG_DNS_RR_INVAL	"\"%s\" Invalid DNS TXT record: "\
878200fe25Srmesta 				"Defaulting to \"%s\""
888200fe25Srmesta 
898200fe25Srmesta /*
908200fe25Srmesta  * DNS related info
918200fe25Srmesta  */
928200fe25Srmesta #define	NFSMAPID_DNS_RR			"_nfsv4idmapdomain"
938200fe25Srmesta #define	NFSMAPID_DNS_TOUT_SECS		(30LL)
948200fe25Srmesta #define	NFSMAPID_SLOG_RATE		20	/* ~10 mins */
958200fe25Srmesta 
968200fe25Srmesta #define	NS_ERRS				6	/* netdb.h */
978200fe25Srmesta 
988200fe25Srmesta typedef union {
998200fe25Srmesta 	HEADER	hdr;
1008200fe25Srmesta 	uchar_t	buf[PACKETSZ];
1018200fe25Srmesta } ans_t;
1028200fe25Srmesta 
1038200fe25Srmesta /*
1048200fe25Srmesta  * NOTE: All s_ prefixed variables are only to be used by the DNS
1058200fe25Srmesta  *       feature implementation (mapid.c). The exported globals
1068200fe25Srmesta  *	 (ie. seen by nfsmapid.c/nfsmapid_server.c) are the
1078200fe25Srmesta  *       dns_ prefixed variables along with sysdns_domain.
1088200fe25Srmesta  */
1098200fe25Srmesta static ans_t			 s_ans;
1108200fe25Srmesta static int			 s_anslen;
1118200fe25Srmesta static char			 s_dname[DNAMEMAX] = {0};
1128200fe25Srmesta static char			 s_txt_rr[DNAMEMAX] = {0};
1138200fe25Srmesta 
1148200fe25Srmesta static rwlock_t			 s_dns_data_lock = DEFAULTRWLOCK;
1158200fe25Srmesta static rwlock_t			 s_dns_impl_lock = DEFAULTRWLOCK;
1168200fe25Srmesta static mutex_t			 s_res_lock = ERRORCHECKMUTEX;
1178200fe25Srmesta static uint32_t			 s_dns_tout = 0;
1188200fe25Srmesta static thread_t			 s_dns_qthread;
1198200fe25Srmesta static bool_t			 s_dns_qthr_created = FALSE;
1208200fe25Srmesta static bool_t			 s_dns_disabled = FALSE;
1218200fe25Srmesta static struct __res_state	 s_res = {0};
1228200fe25Srmesta static thread_key_t		 s_thr_key;
1238200fe25Srmesta int				 lib_init_done = 0;
1248200fe25Srmesta 
1258200fe25Srmesta static int			 resolv_init(void);
1268200fe25Srmesta static void			 resolv_decode(void);
1278200fe25Srmesta static int			 resolv_error(void);
1288200fe25Srmesta static void			 resolv_get_txt_data(void);
1298200fe25Srmesta static void			 resolv_txt_reset(void);
1308200fe25Srmesta static void			 resolve_process_txt(uchar_t *, int);
1318200fe25Srmesta static int			 resolv_search(void);
132*bfa62c28SVallish Vaidyeshwara static void			 resolv_destroy(void);
1338200fe25Srmesta static uchar_t			*resolv_skip_rr(uchar_t *, uchar_t *);
1348200fe25Srmesta static void			 domain_sync(cb_t *, char *);
1358200fe25Srmesta static int			 get_mtime(const char *, timestruc_t *);
1368200fe25Srmesta static void			 get_nfs_domain(void);
1378200fe25Srmesta static void			 get_dns_domain(void);
1388200fe25Srmesta static void			 get_dns_txt_domain(cb_t *);
1398200fe25Srmesta void				 _lib_init(void);
1408200fe25Srmesta 
1418200fe25Srmesta #ifdef	DEBUG
1428200fe25Srmesta bool_t				 nfsmapid_debug = FALSE;
1438200fe25Srmesta #endif	/* DEBUG */
1448200fe25Srmesta 
1458200fe25Srmesta /*
1468200fe25Srmesta  * mapid_domain_lock:	rwlock used to serialize access/changes
1478200fe25Srmesta  *			to the library's mapid_domain global var.
1488200fe25Srmesta  *
1498200fe25Srmesta  * mapid_domain:	Library variable used to store the current
1508200fe25Srmesta  *			domain configured for use in decoding/encoding
1518200fe25Srmesta  *			outbound and inbound attr strings, accordingly.
1528200fe25Srmesta  *
1538200fe25Srmesta  * nfs_domain:		If /etc/default/nfs NFSMAPID_DOMAIN var
1548200fe25Srmesta  *			has been set, nfs_domain will hold this
1558200fe25Srmesta  *			value for the duration of the instance;
1568200fe25Srmesta  *			If the value ever changes, the change is
1578200fe25Srmesta  *			detected via the use of nfs_mtime and
1588200fe25Srmesta  *			nfs_domain is updated accordingly.
1598200fe25Srmesta  *
1608200fe25Srmesta  * dns_domain:		If the system's resolver (/etc/resolv.conf)
1618200fe25Srmesta  *			has been configured, dns_domain will hold
1628200fe25Srmesta  *			the configured DNS domain as reported by the
1638200fe25Srmesta  *			res_ninit() resolver interface. If the system's
1648200fe25Srmesta  *			/etc/resolv.conf file is updated, the change
1658200fe25Srmesta  *			is detected via the use of dns_mtime and
1668200fe25Srmesta  *			dns_domain is updated accordingly.
1678200fe25Srmesta  */
1688200fe25Srmesta rwlock_t			mapid_domain_lock = DEFAULTRWLOCK;
1698200fe25Srmesta uint32_t			mapid_domain_len = 0;
1708200fe25Srmesta char				mapid_domain[DNAMEMAX] = {0};
1718200fe25Srmesta 
1728200fe25Srmesta timestruc_t			nfs_mtime = {0};
1738200fe25Srmesta uint32_t			nfs_domain_len = 0;
1748200fe25Srmesta char				nfs_domain[DNAMEMAX] = {0};
1758200fe25Srmesta 
1768200fe25Srmesta timestruc_t			dns_mtime = {0};
1778200fe25Srmesta uint32_t			dns_domain_len = 0;
1788200fe25Srmesta char				dns_domain[DNAMEMAX] = {0};
1798200fe25Srmesta 
1808200fe25Srmesta int				dns_txt_cached = 0;
1818200fe25Srmesta uint32_t			dns_txt_domain_len = 0;
1828200fe25Srmesta char				dns_txt_domain[DNAMEMAX] = {0};
1838200fe25Srmesta char				sysdns_domain[DNAMEMAX] = {0};
1848200fe25Srmesta 
1858200fe25Srmesta timestruc_t			zapped_mtime = {0};
1868200fe25Srmesta 
1878200fe25Srmesta #define	ZAP_DOMAIN(which)			\
1888200fe25Srmesta 	{					\
1898200fe25Srmesta 		bzero(which##_domain, DNAMEMAX);\
1908200fe25Srmesta 		which##_domain_len = 0;		\
1918200fe25Srmesta 		which##_mtime = zapped_mtime;	\
1928200fe25Srmesta 	}
1938200fe25Srmesta 
1948200fe25Srmesta #define	TIMESTRUC_EQ(a, b)			\
1958200fe25Srmesta 		(((a).tv_sec == (b).tv_sec) &&	\
1968200fe25Srmesta 		((a).tv_nsec == (b).tv_nsec))
1978200fe25Srmesta 
1988200fe25Srmesta 
1998200fe25Srmesta 
2008200fe25Srmesta #endif	/* __LIBMAPID_IMPL */
2018200fe25Srmesta 
2028200fe25Srmesta /*
2038200fe25Srmesta  * PSARC 2005/487 Consolidation Private Interfaces
2048200fe25Srmesta  * mapid_reeval_domain(), mapid_get_domain()
2058200fe25Srmesta  * Changes must be reviewed by Solaris File Sharing
2068200fe25Srmesta  */
2078200fe25Srmesta extern void			 mapid_reeval_domain(cb_t *);
2088200fe25Srmesta extern char			*mapid_get_domain(void);
2098200fe25Srmesta 
2108200fe25Srmesta /*
2118200fe25Srmesta  * PSARC 2005/487 Contracted Sun Private Interface
2128200fe25Srmesta  * mapid_derive_domain(), mapid_stdchk_domain()
2138200fe25Srmesta  * Changes must be reviewed by Solaris File Sharing
2148200fe25Srmesta  * Changes must be communicated to contract-2005-487-01@sun.com
2158200fe25Srmesta  */
2168200fe25Srmesta extern int			 mapid_stdchk_domain(const char *);
2178200fe25Srmesta extern char			*mapid_derive_domain(void);
2188200fe25Srmesta 
2198200fe25Srmesta #ifdef	__cplusplus
2208200fe25Srmesta }
2218200fe25Srmesta #endif
2228200fe25Srmesta 
2238200fe25Srmesta #endif	/* _MAPID_H */
224