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