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