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