1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/*
* DNS callback functions for libldap that use the NSPR (Netscape
* Portable Runtime) thread API.
*
*/
#ifdef _SOLARIS_SDK
#include "solaris-int.h"
#include <libintl.h>
#include <syslog.h>
#include <nsswitch.h>
#include <synch.h>
#include <nss_dbdefs.h>
#include <netinet/in.h>
static char *host_service = NULL;
static DEFINE_NSS_DB_ROOT(db_root_hosts);
#endif
#include "ldappr-int.h"
static LDAPHostEnt *prldap_gethostbyname( const char *name,
LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
void *extradata );
static LDAPHostEnt *prldap_gethostbyaddr( const char *addr, int length,
int type, LDAPHostEnt *result, char *buffer, int buflen,
int *statusp, void *extradata );
static int prldap_getpeername( LDAP *ld, struct sockaddr *addr,
char *buffer, int buflen );
static LDAPHostEnt *prldap_convert_hostent( LDAPHostEnt *ldhp,
PRHostEnt *prhp );
#ifdef _SOLARIS_SDK
static LDAPHostEnt *
prldap_gethostbyname1(const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata);
extern int
str2hostent(const char *instr, int lenstr, void *ent, char *buffer,
int buflen);
#endif /* _SOLARIS_SDK */
/*
* Install NSPR DNS functions into ld (if ld is NULL, they are installed
* as the default functions for new LDAP * handles).
*
* Returns 0 if all goes well and -1 if not.
*/
int
prldap_install_dns_functions( LDAP *ld )
{
struct ldap_dns_fns dnsfns;
memset( &dnsfns, '\0', sizeof(struct ldap_dns_fns) );
dnsfns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
dnsfns.lddnsfn_gethostbyname = prldap_gethostbyname;
dnsfns.lddnsfn_gethostbyaddr = prldap_gethostbyaddr;
dnsfns.lddnsfn_getpeername = prldap_getpeername;
if ( ldap_set_option( ld, LDAP_OPT_DNS_FN_PTRS, (void *)&dnsfns ) != 0 ) {
return( -1 );
}
return( 0 );
}
static LDAPHostEnt *
prldap_gethostbyname( const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata )
{
PRHostEnt prhent;
if( !statusp || ( *statusp = (int)PR_GetIPNodeByName( name,
PRLDAP_DEFAULT_ADDRESS_FAMILY, PR_AI_DEFAULT,
buffer, buflen, &prhent )) == PR_FAILURE ) {
return( NULL );
}
return( prldap_convert_hostent( result, &prhent ));
}
static LDAPHostEnt *
prldap_gethostbyaddr( const char *addr, int length, int type,
LDAPHostEnt *result, char *buffer, int buflen, int *statusp,
void *extradata )
{
PRHostEnt prhent;
PRNetAddr iaddr;
if ( PR_SetNetAddr(PR_IpAddrNull, PRLDAP_DEFAULT_ADDRESS_FAMILY,
0, &iaddr) == PR_FAILURE
|| PR_StringToNetAddr( addr, &iaddr ) == PR_FAILURE ) {
return( NULL );
}
if( !statusp || (*statusp = PR_GetHostByAddr(&iaddr, buffer,
buflen, &prhent )) == PR_FAILURE ) {
return( NULL );
}
return( prldap_convert_hostent( result, &prhent ));
}
static int
prldap_getpeername( LDAP *ld, struct sockaddr *addr, char *buffer, int buflen)
{
PRLDAPIOSocketArg *sa;
PRFileDesc *fd;
PRNetAddr iaddr;
int ret;
if (NULL != ld) {
ret = prldap_socket_arg_from_ld( ld, &sa );
if (ret != LDAP_SUCCESS) {
return (-1);
}
ret = PR_GetPeerName(sa->prsock_prfd, &iaddr);
if( ret == PR_FAILURE ) {
return( -1 );
}
*addr = *((struct sockaddr *)&iaddr.raw);
ret = PR_NetAddrToString(&iaddr, buffer, buflen);
if( ret == PR_FAILURE ) {
return( -1 );
}
return (0);
}
return (-1);
}
/*
* Function: prldap_convert_hostent()
* Description: copy the fields of a PRHostEnt struct to an LDAPHostEnt
* Returns: the LDAPHostEnt pointer passed in.
*/
static LDAPHostEnt *
prldap_convert_hostent( LDAPHostEnt *ldhp, PRHostEnt *prhp )
{
ldhp->ldaphe_name = prhp->h_name;
ldhp->ldaphe_aliases = prhp->h_aliases;
ldhp->ldaphe_addrtype = prhp->h_addrtype;
ldhp->ldaphe_length = prhp->h_length;
ldhp->ldaphe_addr_list = prhp->h_addr_list;
return( ldhp );
}
#ifdef _SOLARIS_SDK
/*
* prldap_x_install_dns_skipdb attempts to prevent recursion in resolving
* the hostname to an IP address when a host name is given to LDAP user.
*
* For example, libsldap cannot use LDAP to resolve the host name to an
* address because of recursion. The caller is instructing libldap to skip
* the specified name service when resolving addresses for the specified
* ldap connection.
*
* Note:
* This only supports ipv4 addresses currently.
*
* Since host_service applies to all connections, calling
* prldap_x_install_dns_skipdb with name services other than
* ldap or what uses ldap (for example nis+ might use ldap) to
* skip will lead to unpredictable results.
*
* Returns:
* 0 if success and data base found
* -1 if failure
*/
int
prldap_x_install_dns_skipdb(LDAP *ld, const char *skip)
{
enum __nsw_parse_err pserr;
struct __nsw_switchconfig *conf;
struct __nsw_lookup *lkp;
struct ldap_dns_fns dns_fns;
char *name_list = NULL;
char *tmp;
const char *name;
int len;
boolean_t got_skip = B_FALSE;
/*
* db_root_hosts.lock mutex is used to ensure that the name list
* is not in use by the name service switch while we are updating
* the host_service
*/
(void) mutex_lock(&db_root_hosts.lock);
conf = __nsw_getconfig("hosts", &pserr);
if (conf == NULL) {
(void) mutex_unlock(&db_root_hosts.lock);
return (0);
}
/* check for skip and count other backends */
for (lkp = conf->lookups; lkp != NULL; lkp = lkp->next) {
name = lkp->service_name;
if (strcmp(name, skip) == 0) {
got_skip = B_TRUE;
continue;
}
if (name_list == NULL)
name_list = strdup(name);
else {
len = strlen(name_list);
tmp = realloc(name_list, len + strlen(name) + 2);
if (tmp == NULL) {
free(name_list);
name_list = NULL;
} else {
name_list = tmp;
name_list[len++] = ' ';
(void) strcpy(name_list+len, name);
}
}
if (name_list == NULL) { /* alloc error */
(void) mutex_unlock(&db_root_hosts.lock);
__nsw_freeconfig(conf);
return (-1);
}
}
__nsw_freeconfig(conf);
if (!got_skip) {
/*
* Since skip name service not used for hosts, we do not need
* to install our private address resolution function
*/
(void) mutex_unlock(&db_root_hosts.lock);
if (name_list != NULL)
free(name_list);
return (0);
}
if (host_service != NULL)
free(host_service);
host_service = name_list;
(void) mutex_unlock(&db_root_hosts.lock);
if (ldap_get_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
return (-1);
dns_fns.lddnsfn_bufsize = PR_NETDB_BUF_SIZE;
dns_fns.lddnsfn_gethostbyname = prldap_gethostbyname1;
if (ldap_set_option(ld, LDAP_OPT_DNS_FN_PTRS, &dns_fns) != 0)
return (-1);
return (0);
}
/*
* prldap_initf_hosts is passed to and called by nss_search() as a
* service routine.
*
* Returns:
* None
*/
static void
prldap_initf_hosts(nss_db_params_t *p)
{
static char *no_service = "";
p->name = NSS_DBNAM_HOSTS;
p->flags |= NSS_USE_DEFAULT_CONFIG;
p->default_config = host_service == NULL ? no_service : host_service;
}
/*
* called by prldap_gethostbyname1()
*/
/*
* prldap_switch_gethostbyname_r is called by prldap_gethostbyname1 as a
* substitute for gethostbyname_r(). A method which prevents recursion. see
* prldap_gethostbyname1() and prldap_x_install_dns_skipdb().
*
* Returns:
* PR_SUCCESS if success
* PR_FAILURE if failure
*/
static int
prldap_switch_gethostbyname_r(const char *name,
struct hostent *result, char *buffer, int buflen,
int *h_errnop)
{
nss_XbyY_args_t arg;
nss_status_t res;
struct hostent *resp;
/*
* Log the information indicating that we are trying to
* resolve the LDAP server name.
*/
syslog(LOG_INFO, "libldap: Resolving server name \"%s\"", name);
NSS_XbyY_INIT(&arg, result, buffer, buflen, str2hostent);
arg.key.name = name;
arg.stayopen = 0;
res = nss_search(&db_root_hosts, prldap_initf_hosts,
NSS_DBOP_HOSTS_BYNAME, &arg);
arg.status = res;
*h_errnop = arg.h_errno;
resp = (struct hostent *)NSS_XbyY_FINI(&arg);
return (resp != NULL ? PR_SUCCESS : PR_FAILURE);
}
/*
* prldap_gethostbyname1 is used to be a substitute gethostbyname_r for
* libldap when it is unsafe to use the normal nameservice functions.
*
* Returns:
* pointer to LDAPHostEnt: if success contains the address
* NULL pointer: if failure
*/
static LDAPHostEnt *
prldap_gethostbyname1(const char *name, LDAPHostEnt *result,
char *buffer, int buflen, int *statusp, void *extradata)
{
int h_errno;
LDAPHostEnt prhent;
memset(&prhent, '\0', sizeof (prhent));
if (!statusp || ( *statusp = prldap_switch_gethostbyname_r(name,
&prhent, buffer, buflen, &h_errno )) == PR_FAILURE) {
/*
* If we got here, it means that we are not able to
* resolve the LDAP server name and so warn the system
* adminstrator accordingly.
*/
syslog(LOG_WARNING, "libldap: server name \"%s\" could not "
"be resolved", name);
return (NULL);
}
return (prldap_convert_hostent(result, &prhent));
}
#endif /* _SOLARIS_SDK */
|