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