2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Copyright 1998, A. Hartgers, All rights reserved.
16 * This software is not subject to any license of Eindhoven University of
17 * Technology, since it was written in my spare time.
19 * Redistribution and use in source and binary forms are permitted only
20 * as authorized by the OpenLDAP Public License. A copy of this
21 * license is available at http://www.OpenLDAP.org/license.html or
22 * in file LICENSE in the top-level directory of the distribution.
26 * This work was initially developed by Bart Hartgers for inclusion in
31 * util-int.c Various functions to replace missing threadsafe ones.
32 * Without the real *_r funcs, things will
33 * work, but might not be threadsafe.
38 #include <ac/stdlib.h>
41 #include <ac/socket.h>
42 #include <ac/string.h>
44 #include <ac/unistd.h>
49 /* newer systems declare this in <netdb.h> for you, older ones don't.
50 * harmless to declare it again (unless defined by a macro).
55 #ifndef LDAP_R_COMPILE
56 # undef HAVE_REENTRANT_FUNCTIONS
58 # undef HAVE_GETHOSTBYNAME_R
59 # undef HAVE_GETHOSTBYADDR_R
62 # include <ldap_pvt_thread.h>
63 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
65 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
66 && defined( CTIME_R_NARGS )
69 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
72 # if defined(HAVE_GETHOSTBYNAME_R) && \
73 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
74 /* Don't know how to handle this version, pretend it's not there */
75 # undef HAVE_GETHOSTBYNAME_R
77 # if defined(HAVE_GETHOSTBYADDR_R) && \
78 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
79 /* Don't know how to handle this version, pretend it's not there */
80 # undef HAVE_GETHOSTBYADDR_R
82 #endif /* LDAP_R_COMPILE */
84 char *ldap_pvt_ctime( const time_t *tp, char *buf )
87 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
88 # error "CTIME_R_NARGS should be 2 or 3"
89 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
90 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
91 # elif CTIME_R_NARGS > 2
92 return ctime_r(tp,buf,26);
94 return ctime_r(tp,buf);
99 # ifdef LDAP_R_COMPILE
100 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
103 AC_MEMCPY( buf, ctime(tp), 26 );
105 # ifdef LDAP_R_COMPILE
106 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
113 #define BUFSTART (1024-32)
114 #define BUFMAX (32*1024-32)
116 static char *safe_realloc( char **buf, int len );
118 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
119 static int copy_hostent( struct hostent *res,
120 char **buf, struct hostent * src );
123 int ldap_pvt_gethostbyname_a(
125 struct hostent *resbuf,
127 struct hostent **result,
130 #if defined( HAVE_GETHOSTBYNAME_R )
132 # define NEED_SAFE_REALLOC 1
136 for(;buflen<BUFMAX;) {
137 if (safe_realloc( buf, buflen )==NULL)
140 #if (GETHOSTBYNAME_R_NARGS < 6)
141 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
142 r = (*result == NULL) ? -1 : 0;
144 r = gethostbyname_r( name, resbuf, *buf,
145 buflen, result, herrno_ptr );
148 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
151 #ifdef NETDB_INTERNAL
153 (*herrno_ptr==NETDB_INTERNAL) &&
163 #elif defined( LDAP_R_COMPILE )
164 # define NEED_COPY_HOSTENT
169 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
171 he = gethostbyname( name );
174 *herrno_ptr = h_errno;
176 } else if (copy_hostent( resbuf, buf, he )<0) {
184 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
189 *result = gethostbyname( name );
195 *herrno_ptr = h_errno;
203 hp_strerror( int err )
206 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
207 case TRY_AGAIN: return _("Host not found (server fail?)");
208 case NO_RECOVERY: return _("Non-recoverable failure");
209 case NO_DATA: return _("No data of requested type");
210 #ifdef NETDB_INTERNAL
211 case NETDB_INTERNAL: return STRERROR( errno );
214 return _("Unknown resolver error");
218 int ldap_pvt_get_hname(
219 const struct sockaddr *sa,
226 #if defined( HAVE_GETNAMEINFO )
228 #if defined( LDAP_R_COMPILE )
229 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
231 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
232 #if defined( LDAP_R_COMPILE )
233 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
235 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
238 #else /* !HAVE_GETNAMEINFO */
241 struct hostent *hp = NULL;
242 #ifdef HAVE_GETHOSTBYADDR_R
244 int buflen=BUFSTART, h_errno;
249 if (sa->sa_family == AF_INET6) {
250 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
251 addr = (char *)&sin->sin6_addr;
252 alen = sizeof(sin->sin6_addr);
255 if (sa->sa_family == AF_INET) {
256 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
257 addr = (char *)&sin->sin_addr;
258 alen = sizeof(sin->sin_addr);
261 *err = (char *)hp_strerror( rc );
264 #if defined( HAVE_GETHOSTBYADDR_R )
265 for(;buflen<BUFMAX;) {
266 if (safe_realloc( &buf, buflen )==NULL) {
267 *err = (char *)STRERROR( ENOMEM );
270 #if (GETHOSTBYADDR_R_NARGS < 8)
271 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
272 &hb, buf, buflen, &h_errno );
273 rc = (hp == NULL) ? -1 : 0;
275 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
279 #ifdef NETDB_INTERNAL
281 (h_errno==NETDB_INTERNAL) &&
291 strncpy( name, hp->h_name, namelen );
293 *err = (char *)hp_strerror( h_errno );
296 #else /* HAVE_GETHOSTBYADDR_R */
298 #if defined( LDAP_R_COMPILE )
299 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
301 hp = gethostbyaddr( addr, alen, sa->sa_family );
303 strncpy( name, hp->h_name, namelen );
307 *err = (char *)hp_strerror( h_errno );
309 #if defined( LDAP_R_COMPILE )
310 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
313 #endif /* !HAVE_GETHOSTBYADDR_R */
315 #endif /* !HAVE_GETNAMEINFO */
318 int ldap_pvt_gethostbyaddr_a(
322 struct hostent *resbuf,
324 struct hostent **result,
327 #if defined( HAVE_GETHOSTBYADDR_R )
329 # undef NEED_SAFE_REALLOC
330 # define NEED_SAFE_REALLOC
334 for(;buflen<BUFMAX;) {
335 if (safe_realloc( buf, buflen )==NULL)
337 #if (GETHOSTBYADDR_R_NARGS < 8)
338 *result=gethostbyaddr_r( addr, len, type,
339 resbuf, *buf, buflen, herrno_ptr );
340 r = (*result == NULL) ? -1 : 0;
342 r = gethostbyaddr_r( addr, len, type,
343 resbuf, *buf, buflen,
344 result, herrno_ptr );
347 #ifdef NETDB_INTERNAL
349 (*herrno_ptr==NETDB_INTERNAL) &&
359 #elif defined( LDAP_R_COMPILE )
360 # undef NEED_COPY_HOSTENT
361 # define NEED_COPY_HOSTENT
366 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
368 he = gethostbyaddr( addr, len, type );
371 *herrno_ptr = h_errno;
373 } else if (copy_hostent( resbuf, buf, he )<0) {
381 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
385 #else /* gethostbyaddr() */
387 *result = gethostbyaddr( addr, len, type );
396 * ldap_int_utils_init() should be called before any other function.
399 void ldap_int_utils_init( void )
406 #ifdef LDAP_R_COMPILE
407 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
408 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
410 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
413 /* call other module init functions here... */
416 #if defined( NEED_COPY_HOSTENT )
417 # undef NEED_SAFE_REALLOC
418 #define NEED_SAFE_REALLOC
420 static char *cpy_aliases(
427 for( ; (*src) ; src++ ) {
428 len = strlen( *src ) + 1;
429 AC_MEMCPY( buf, *src, len );
437 static char *cpy_addresses(
444 for( ; (*src) ; src++ ) {
445 AC_MEMCPY( buf, *src, len );
453 static int copy_hostent(
456 struct hostent * src )
463 int total_alias_len=0;
468 /* calculate the size needed for the buffer */
469 name_len = strlen( src->h_name ) + 1;
471 if( src->h_aliases != NULL ) {
472 for( p = src->h_aliases; (*p) != NULL; p++ ) {
473 total_alias_len += strlen( *p ) + 1;
478 if( src->h_addr_list != NULL ) {
479 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
482 total_addr_len = n_addr * src->h_length;
485 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
486 total_addr_len + total_alias_len + name_len;
488 if (safe_realloc( buf, total_len )) {
490 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
491 AC_MEMCPY( res, src, sizeof( struct hostent ) );
492 /* first the name... */
493 AC_MEMCPY( tbuf, src->h_name, name_len );
494 res->h_name = tbuf; tbuf+=name_len;
495 /* now the aliases */
497 if ( src->h_aliases != NULL ) {
498 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
501 /* finally the addresses */
502 res->h_addr_list = tp;
503 if ( src->h_addr_list != NULL ) {
504 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
513 #if defined( NEED_SAFE_REALLOC )
514 static char *safe_realloc( char **buf, int len )
517 tmpbuf = LDAP_REALLOC( *buf, len );
525 char * ldap_pvt_get_fqdn( char *name )
528 char hostbuf[MAXHOSTNAMELEN+1];
529 struct hostent *hp, he_buf;
530 int rc, local_h_errno;
533 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
534 hostbuf[MAXHOSTNAMELEN] = '\0';
541 rc = ldap_pvt_gethostbyname_a( name,
542 &he_buf, &ha_buf, &hp, &local_h_errno );
544 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
545 fqdn = LDAP_STRDUP( name );
547 fqdn = LDAP_STRDUP( hp->h_name );
554 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
555 && !defined( HAVE_GAI_STRERROR )
556 char *ldap_pvt_gai_strerror (int code) {
561 #ifdef EAI_ADDRFAMILY
562 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
564 { EAI_AGAIN, N_("Temporary failure in name resolution") },
565 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
566 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
567 { EAI_FAMILY, N_("ai_family not supported") },
568 { EAI_MEMORY, N_("Memory allocation failure") },
570 { EAI_NODATA, N_("No address associated with hostname") },
572 { EAI_NONAME, N_("Name or service not known") },
573 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
574 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
575 { EAI_SYSTEM, N_("System error") },
581 for ( i = 0; values[i].msg != NULL; i++ ) {
582 if ( values[i].code == code ) {
583 return (char *) _(values[i].msg);
587 return _("Unknown error");