xref: /onnv-gate/usr/src/lib/libmapid/common/mapid.h (revision 1741:6cf5a4a34604)
1*1741Srmesta /*
2*1741Srmesta  * CDDL HEADER START
3*1741Srmesta  *
4*1741Srmesta  * The contents of this file are subject to the terms of the
5*1741Srmesta  * Common Development and Distribution License (the "License").
6*1741Srmesta  * You may not use this file except in compliance with the License.
7*1741Srmesta  *
8*1741Srmesta  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1741Srmesta  * or http://www.opensolaris.org/os/licensing.
10*1741Srmesta  * See the License for the specific language governing permissions
11*1741Srmesta  * and limitations under the License.
12*1741Srmesta  *
13*1741Srmesta  * When distributing Covered Code, include this CDDL HEADER in each
14*1741Srmesta  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1741Srmesta  * If applicable, add the following below this CDDL HEADER, with the
16*1741Srmesta  * fields enclosed by brackets "[]" replaced with your own identifying
17*1741Srmesta  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1741Srmesta  *
19*1741Srmesta  * CDDL HEADER END
20*1741Srmesta  */
21*1741Srmesta /*
22*1741Srmesta  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1741Srmesta  * Use is subject to license terms.
24*1741Srmesta  */
25*1741Srmesta 
26*1741Srmesta #ifndef	_MAPID_H
27*1741Srmesta #define	_MAPID_H
28*1741Srmesta 
29*1741Srmesta #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*1741Srmesta 
31*1741Srmesta #ifdef	__cplusplus
32*1741Srmesta extern "C" {
33*1741Srmesta #endif
34*1741Srmesta 
35*1741Srmesta #include <stdio.h>
36*1741Srmesta #include <stdlib.h>
37*1741Srmesta #include <unistd.h>
38*1741Srmesta #include <string.h>
39*1741Srmesta #include <strings.h>
40*1741Srmesta #include <sys/types.h>
41*1741Srmesta #include <sys/stat.h>
42*1741Srmesta #include <rpc/types.h>
43*1741Srmesta #include <netinet/in.h>
44*1741Srmesta #include <arpa/nameser.h>
45*1741Srmesta #include <resolv.h>
46*1741Srmesta #include <netdb.h>
47*1741Srmesta #include <errno.h>
48*1741Srmesta #include <ctype.h>
49*1741Srmesta #include <sys/socket.h>
50*1741Srmesta #include <arpa/inet.h>
51*1741Srmesta #include <assert.h>
52*1741Srmesta #include <synch.h>
53*1741Srmesta #include <syslog.h>
54*1741Srmesta #include <locale.h>
55*1741Srmesta #include <thread.h>
56*1741Srmesta #include <deflt.h>
57*1741Srmesta #include <nfs/nfs4.h>
58*1741Srmesta 
59*1741Srmesta #define	DNAMEMAX			(NS_MAXCDNAME + 1)
60*1741Srmesta 
61*1741Srmesta typedef struct {
62*1741Srmesta 	void	*(*fcn)(void *);
63*1741Srmesta 	int	 signal;
64*1741Srmesta } cb_t;
65*1741Srmesta 
66*1741Srmesta #ifdef	__LIBMAPID_IMPL
67*1741Srmesta 
68*1741Srmesta /*
69*1741Srmesta  * Error Messages
70*1741Srmesta  */
71*1741Srmesta #define	EMSG_NETDB_INTERNAL	"Internal Resolver Error: %s"
72*1741Srmesta 
73*1741Srmesta #define	EMSG_TRY_AGAIN		"\"%s\" DNS nameserver(s) not responding" \
74*1741Srmesta 				"...\tRetrying"
75*1741Srmesta 
76*1741Srmesta #define	EMSG_NO_RECOVERY	"Unrecoverable Resolver Error: %s"
77*1741Srmesta 
78*1741Srmesta #define	EMSG_HOST_NOT_FOUND	"Authoritative nameserver unresponsive " \
79*1741Srmesta 				"to queries for domain \"%s\""
80*1741Srmesta 
81*1741Srmesta #define	EMSG_NO_DATA		"\"%s\" DNS TXT record not found: "\
82*1741Srmesta 				"Defaulting to \"%s\""
83*1741Srmesta 
84*1741Srmesta #define	EMSG_DNS_THREAD_ERROR	"Unable to create DNS query thread"
85*1741Srmesta 
86*1741Srmesta #define	EMSG_DNS_DISABLE	"%s: Further DNS queries disabled !"
87*1741Srmesta 
88*1741Srmesta #define	EMSG_DNS_RR_INVAL	"\"%s\" Invalid DNS TXT record: "\
89*1741Srmesta 				"Defaulting to \"%s\""
90*1741Srmesta 
91*1741Srmesta /*
92*1741Srmesta  * DNS related info
93*1741Srmesta  */
94*1741Srmesta #define	NFSMAPID_DNS_RR			"_nfsv4idmapdomain"
95*1741Srmesta #define	NFSMAPID_DNS_TOUT_SECS		(30LL)
96*1741Srmesta #define	NFSMAPID_SLOG_RATE		20	/* ~10 mins */
97*1741Srmesta 
98*1741Srmesta #define	NS_ERRS				6	/* netdb.h */
99*1741Srmesta 
100*1741Srmesta typedef union {
101*1741Srmesta 	HEADER	hdr;
102*1741Srmesta 	uchar_t	buf[PACKETSZ];
103*1741Srmesta } ans_t;
104*1741Srmesta 
105*1741Srmesta /*
106*1741Srmesta  * NOTE: All s_ prefixed variables are only to be used by the DNS
107*1741Srmesta  *       feature implementation (mapid.c). The exported globals
108*1741Srmesta  *	 (ie. seen by nfsmapid.c/nfsmapid_server.c) are the
109*1741Srmesta  *       dns_ prefixed variables along with sysdns_domain.
110*1741Srmesta  */
111*1741Srmesta static ans_t			 s_ans;
112*1741Srmesta static int			 s_anslen;
113*1741Srmesta static char			 s_dname[DNAMEMAX] = {0};
114*1741Srmesta static char			 s_txt_rr[DNAMEMAX] = {0};
115*1741Srmesta 
116*1741Srmesta static rwlock_t			 s_dns_data_lock = DEFAULTRWLOCK;
117*1741Srmesta static rwlock_t			 s_dns_impl_lock = DEFAULTRWLOCK;
118*1741Srmesta static mutex_t			 s_res_lock = ERRORCHECKMUTEX;
119*1741Srmesta static uint32_t			 s_dns_tout = 0;
120*1741Srmesta static thread_t			 s_dns_qthread;
121*1741Srmesta static bool_t			 s_dns_qthr_created = FALSE;
122*1741Srmesta static bool_t			 s_dns_disabled = FALSE;
123*1741Srmesta static struct __res_state	 s_res = {0};
124*1741Srmesta static thread_key_t		 s_thr_key;
125*1741Srmesta int				 lib_init_done = 0;
126*1741Srmesta 
127*1741Srmesta static int			 resolv_init(void);
128*1741Srmesta static void			 resolv_decode(void);
129*1741Srmesta static int			 resolv_error(void);
130*1741Srmesta static void			 resolv_get_txt_data(void);
131*1741Srmesta static void			 resolv_txt_reset(void);
132*1741Srmesta static void			 resolve_process_txt(uchar_t *, int);
133*1741Srmesta static int			 resolv_search(void);
134*1741Srmesta static uchar_t			*resolv_skip_rr(uchar_t *, uchar_t *);
135*1741Srmesta static void			 domain_sync(cb_t *, char *);
136*1741Srmesta static int			 get_mtime(const char *, timestruc_t *);
137*1741Srmesta static void			 get_nfs_domain(void);
138*1741Srmesta static void			 get_dns_domain(void);
139*1741Srmesta static void			 get_dns_txt_domain(cb_t *);
140*1741Srmesta void				 _lib_init(void);
141*1741Srmesta 
142*1741Srmesta #ifdef	DEBUG
143*1741Srmesta bool_t				 nfsmapid_debug = FALSE;
144*1741Srmesta #endif	/* DEBUG */
145*1741Srmesta 
146*1741Srmesta /*
147*1741Srmesta  * mapid_domain_lock:	rwlock used to serialize access/changes
148*1741Srmesta  *			to the library's mapid_domain global var.
149*1741Srmesta  *
150*1741Srmesta  * mapid_domain:	Library variable used to store the current
151*1741Srmesta  *			domain configured for use in decoding/encoding
152*1741Srmesta  *			outbound and inbound attr strings, accordingly.
153*1741Srmesta  *
154*1741Srmesta  * nfs_domain:		If /etc/default/nfs NFSMAPID_DOMAIN var
155*1741Srmesta  *			has been set, nfs_domain will hold this
156*1741Srmesta  *			value for the duration of the instance;
157*1741Srmesta  *			If the value ever changes, the change is
158*1741Srmesta  *			detected via the use of nfs_mtime and
159*1741Srmesta  *			nfs_domain is updated accordingly.
160*1741Srmesta  *
161*1741Srmesta  * dns_domain:		If the system's resolver (/etc/resolv.conf)
162*1741Srmesta  *			has been configured, dns_domain will hold
163*1741Srmesta  *			the configured DNS domain as reported by the
164*1741Srmesta  *			res_ninit() resolver interface. If the system's
165*1741Srmesta  *			/etc/resolv.conf file is updated, the change
166*1741Srmesta  *			is detected via the use of dns_mtime and
167*1741Srmesta  *			dns_domain is updated accordingly.
168*1741Srmesta  */
169*1741Srmesta rwlock_t			mapid_domain_lock = DEFAULTRWLOCK;
170*1741Srmesta uint32_t			mapid_domain_len = 0;
171*1741Srmesta char				mapid_domain[DNAMEMAX] = {0};
172*1741Srmesta 
173*1741Srmesta timestruc_t			nfs_mtime = {0};
174*1741Srmesta uint32_t			nfs_domain_len = 0;
175*1741Srmesta char				nfs_domain[DNAMEMAX] = {0};
176*1741Srmesta 
177*1741Srmesta timestruc_t			dns_mtime = {0};
178*1741Srmesta uint32_t			dns_domain_len = 0;
179*1741Srmesta char				dns_domain[DNAMEMAX] = {0};
180*1741Srmesta 
181*1741Srmesta int				dns_txt_cached = 0;
182*1741Srmesta uint32_t			dns_txt_domain_len = 0;
183*1741Srmesta char				dns_txt_domain[DNAMEMAX] = {0};
184*1741Srmesta char				sysdns_domain[DNAMEMAX] = {0};
185*1741Srmesta 
186*1741Srmesta timestruc_t			zapped_mtime = {0};
187*1741Srmesta 
188*1741Srmesta #define	ZAP_DOMAIN(which)			\
189*1741Srmesta 	{					\
190*1741Srmesta 		bzero(which##_domain, DNAMEMAX);\
191*1741Srmesta 		which##_domain_len = 0;		\
192*1741Srmesta 		which##_mtime = zapped_mtime;	\
193*1741Srmesta 	}
194*1741Srmesta 
195*1741Srmesta #define	TIMESTRUC_EQ(a, b)			\
196*1741Srmesta 		(((a).tv_sec == (b).tv_sec) &&	\
197*1741Srmesta 		((a).tv_nsec == (b).tv_nsec))
198*1741Srmesta 
199*1741Srmesta 
200*1741Srmesta 
201*1741Srmesta #endif	/* __LIBMAPID_IMPL */
202*1741Srmesta 
203*1741Srmesta /*
204*1741Srmesta  * PSARC 2005/487 Consolidation Private Interfaces
205*1741Srmesta  * mapid_reeval_domain(), mapid_get_domain()
206*1741Srmesta  * Changes must be reviewed by Solaris File Sharing
207*1741Srmesta  */
208*1741Srmesta extern void			 mapid_reeval_domain(cb_t *);
209*1741Srmesta extern char			*mapid_get_domain(void);
210*1741Srmesta 
211*1741Srmesta /*
212*1741Srmesta  * PSARC 2005/487 Contracted Sun Private Interface
213*1741Srmesta  * mapid_derive_domain(), mapid_stdchk_domain()
214*1741Srmesta  * Changes must be reviewed by Solaris File Sharing
215*1741Srmesta  * Changes must be communicated to contract-2005-487-01@sun.com
216*1741Srmesta  */
217*1741Srmesta extern int			 mapid_stdchk_domain(const char *);
218*1741Srmesta extern char			*mapid_derive_domain(void);
219*1741Srmesta 
220*1741Srmesta #ifdef	__cplusplus
221*1741Srmesta }
222*1741Srmesta #endif
223*1741Srmesta 
224*1741Srmesta #endif	/* _MAPID_H */
225