3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * util-int.c Various functions to replace missing threadsafe ones.
8 * Without the real *_r funcs, things will
9 * work, but might not be threadsafe.
11 * Written by Bart Hartgers.
13 * Copyright 1998, A. Hartgers, All rights reserved.
14 * This software is not subject to any license of Eindhoven University of
15 * Technology, since it was written in my spare time.
17 * Redistribution and use in source and binary forms are permitted only
18 * as authorized by the OpenLDAP Public License. A copy of this
19 * license is available at http://www.OpenLDAP.org/license.html or
20 * in file LICENSE in the top-level directory of the distribution.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include <ac/unistd.h>
37 /* newer systems declare this in <netdb.h> for you, older ones don't.
38 * harmless to declare it again (unless defined by a macro).
43 #ifndef LDAP_R_COMPILE
44 # undef HAVE_REENTRANT_FUNCTIONS
46 # undef HAVE_GETHOSTBYNAME_R
47 # undef HAVE_GETHOSTBYADDR_R
50 # include <ldap_pvt_thread.h>
51 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
53 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
54 && defined( CTIME_R_NARGS )
57 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
60 # if defined(HAVE_GETHOSTBYNAME_R) && \
61 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
62 /* Don't know how to handle this version, pretend it's not there */
63 # undef HAVE_GETHOSTBYNAME_R
65 # if defined(HAVE_GETHOSTBYADDR_R) && \
66 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
67 /* Don't know how to handle this version, pretend it's not there */
68 # undef HAVE_GETHOSTBYADDR_R
70 #endif /* LDAP_R_COMPILE */
72 char *ldap_pvt_ctime( const time_t *tp, char *buf )
75 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
76 # error "CTIME_R_NARGS should be 2 or 3"
77 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
78 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
79 # elif CTIME_R_NARGS > 2
80 return ctime_r(tp,buf,26);
82 return ctime_r(tp,buf);
87 # ifdef LDAP_R_COMPILE
88 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
91 AC_MEMCPY( buf, ctime(tp), 26 );
93 # ifdef LDAP_R_COMPILE
94 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
101 #define BUFSTART (1024-32)
102 #define BUFMAX (32*1024-32)
104 static char *safe_realloc( char **buf, int len );
106 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
107 static int copy_hostent( struct hostent *res,
108 char **buf, struct hostent * src );
111 int ldap_pvt_gethostbyname_a(
113 struct hostent *resbuf,
115 struct hostent **result,
118 #if defined( HAVE_GETHOSTBYNAME_R )
120 # define NEED_SAFE_REALLOC 1
124 for(;buflen<BUFMAX;) {
125 if (safe_realloc( buf, buflen )==NULL)
128 #if (GETHOSTBYNAME_R_NARGS < 6)
129 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
130 r = (*result == NULL) ? -1 : 0;
132 r = gethostbyname_r( name, resbuf, *buf,
133 buflen, result, herrno_ptr );
136 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
139 #ifdef NETDB_INTERNAL
141 (*herrno_ptr==NETDB_INTERNAL) &&
151 #elif defined( LDAP_R_COMPILE )
152 # define NEED_COPY_HOSTENT
157 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
159 he = gethostbyname( name );
162 *herrno_ptr = h_errno;
164 } else if (copy_hostent( resbuf, buf, he )<0) {
172 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
177 *result = gethostbyname( name );
183 *herrno_ptr = h_errno;
191 hp_strerror( int err )
194 case HOST_NOT_FOUND: return "Host not found (authoritative)";
195 case TRY_AGAIN: return "Host not found (server fail?)";
196 case NO_RECOVERY: return "Non-recoverable failure";
197 case NO_DATA: return "No data of requested type";
198 #ifdef NETDB_INTERNAL
199 case NETDB_INTERNAL: return STRERROR( errno );
203 return "Unknown resolver error";
207 int ldap_pvt_get_hname(
208 const struct sockaddr *sa,
215 #if defined( HAVE_GETNAMEINFO )
217 #if defined( LDAP_R_COMPILE )
218 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
220 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
221 #if defined( LDAP_R_COMPILE )
222 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
224 if ( rc ) *err = AC_GAI_STRERROR( rc );
227 #else /* !HAVE_GETNAMEINFO */
230 struct hostent *hp = NULL;
231 #ifdef HAVE_GETHOSTBYADDR_R
233 int buflen=BUFSTART, h_errno;
238 if (sa->sa_family == AF_INET6) {
239 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
240 addr = (char *)&sin->sin6_addr;
241 alen = sizeof(sin->sin6_addr);
244 if (sa->sa_family == AF_INET) {
245 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
246 addr = (char *)&sin->sin_addr;
247 alen = sizeof(sin->sin_addr);
250 *err = (char *)hp_strerror( rc );
253 #if defined( HAVE_GETHOSTBYADDR_R )
254 for(;buflen<BUFMAX;) {
255 if (safe_realloc( &buf, buflen )==NULL) {
256 *err = (char *)STRERROR( ENOMEM );
259 #if (GETHOSTBYADDR_R_NARGS < 8)
260 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
261 &hb, buf, buflen, &h_errno );
262 rc = (hp == NULL) ? -1 : 0;
264 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
268 #ifdef NETDB_INTERNAL
270 (h_errno==NETDB_INTERNAL) &&
280 strncpy( name, hp->h_name, namelen );
282 *err = (char *)hp_strerror( h_errno );
285 #else /* HAVE_GETHOSTBYADDR_R */
287 #if defined( LDAP_R_COMPILE )
288 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
290 hp = gethostbyaddr( addr, alen, sa->sa_family );
292 strncpy( name, hp->h_name, namelen );
296 *err = (char *)hp_strerror( h_errno );
298 #if defined( LDAP_R_COMPILE )
299 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
302 #endif /* !HAVE_GETHOSTBYADDR_R */
304 #endif /* !HAVE_GETNAMEINFO */
307 int ldap_pvt_gethostbyaddr_a(
311 struct hostent *resbuf,
313 struct hostent **result,
316 #if defined( HAVE_GETHOSTBYADDR_R )
318 # undef NEED_SAFE_REALLOC
319 # define NEED_SAFE_REALLOC
323 for(;buflen<BUFMAX;) {
324 if (safe_realloc( buf, buflen )==NULL)
326 #if (GETHOSTBYADDR_R_NARGS < 8)
327 *result=gethostbyaddr_r( addr, len, type,
328 resbuf, *buf, buflen, herrno_ptr );
329 r = (*result == NULL) ? -1 : 0;
331 r = gethostbyaddr_r( addr, len, type,
332 resbuf, *buf, buflen,
333 result, herrno_ptr );
336 #ifdef NETDB_INTERNAL
338 (*herrno_ptr==NETDB_INTERNAL) &&
348 #elif defined( LDAP_R_COMPILE )
349 # undef NEED_COPY_HOSTENT
350 # define NEED_COPY_HOSTENT
355 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
357 he = gethostbyaddr( addr, len, type );
360 *herrno_ptr = h_errno;
362 } else if (copy_hostent( resbuf, buf, he )<0) {
370 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
374 #else /* gethostbyaddr() */
376 *result = gethostbyaddr( addr, len, type );
385 * ldap_int_utils_init() should be called before any other function.
388 void ldap_int_utils_init( void )
395 #ifdef LDAP_R_COMPILE
396 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
397 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
399 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
402 /* call other module init functions here... */
405 #if defined( NEED_COPY_HOSTENT )
406 # undef NEED_SAFE_REALLOC
407 #define NEED_SAFE_REALLOC
409 static char *cpy_aliases(
416 for( ; (*src) ; src++ ) {
417 len = strlen( *src ) + 1;
418 AC_MEMCPY( buf, *src, len );
426 static char *cpy_addresses(
433 for( ; (*src) ; src++ ) {
434 AC_MEMCPY( buf, *src, len );
442 static int copy_hostent(
445 struct hostent * src )
452 int total_alias_len=0;
457 /* calculate the size needed for the buffer */
458 name_len = strlen( src->h_name ) + 1;
460 if( src->h_aliases != NULL ) {
461 for( p = src->h_aliases; (*p) != NULL; p++ ) {
462 total_alias_len += strlen( *p ) + 1;
467 if( src->h_addr_list != NULL ) {
468 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
471 total_addr_len = n_addr * src->h_length;
474 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
475 total_addr_len + total_alias_len + name_len;
477 if (safe_realloc( buf, total_len )) {
479 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
480 AC_MEMCPY( res, src, sizeof( struct hostent ) );
481 /* first the name... */
482 AC_MEMCPY( tbuf, src->h_name, name_len );
483 res->h_name = tbuf; tbuf+=name_len;
484 /* now the aliases */
486 if ( src->h_aliases != NULL ) {
487 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
490 /* finally the addresses */
491 res->h_addr_list = tp;
492 if ( src->h_addr_list != NULL ) {
493 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
502 #if defined( NEED_SAFE_REALLOC )
503 static char *safe_realloc( char **buf, int len )
506 tmpbuf = LDAP_REALLOC( *buf, len );
514 char * ldap_pvt_get_fqdn( char *name )
517 char hostbuf[MAXHOSTNAMELEN+1];
518 struct hostent *hp, he_buf;
519 int rc, local_h_errno;
522 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
523 hostbuf[MAXHOSTNAMELEN] = '\0';
530 rc = ldap_pvt_gethostbyname_a( name,
531 &he_buf, &ha_buf, &hp, &local_h_errno );
533 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
534 fqdn = LDAP_STRDUP( name );
536 fqdn = LDAP_STRDUP( hp->h_name );
543 #if defined( HAVE_GETADDRINFO ) && !defined( HAVE_GAI_STRERROR )
544 char *ldap_pvt_gai_strerror (int code) {
549 #ifdef EAI_ADDRFAMILY
550 { EAI_ADDRFAMILY, "Address family for hostname not supported" },
552 { EAI_AGAIN, "Temporary failure in name resolution" },
553 { EAI_BADFLAGS, "Bad value for ai_flags" },
554 { EAI_FAIL, "Non-recoverable failure in name resolution" },
555 { EAI_FAMILY, "ai_family not supported" },
556 { EAI_MEMORY, "Memory allocation failure" },
558 { EAI_NODATA, "No address associated with hostname" },
560 { EAI_NONAME, "Name or service not known" },
561 { EAI_SERVICE, "Servname not supported for ai_socktype" },
562 { EAI_SOCKTYPE, "ai_socktype not supported" },
563 { EAI_SYSTEM, "System error" },
569 for ( i = 0; values[i].msg != NULL; i++ ) {
570 if ( values[i].code == code ) {
571 return (char *) values[i].msg;
575 return "Unknown error";