2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1998 A. Hartgers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Bart Hartgers for inclusion in
22 * util-int.c Various functions to replace missing threadsafe ones.
23 * Without the real *_r funcs, things will
24 * work, but might not be threadsafe.
29 #include <ac/stdlib.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
35 #include <ac/unistd.h>
40 /* newer systems declare this in <netdb.h> for you, older ones don't.
41 * harmless to declare it again (unless defined by a macro).
46 #ifndef LDAP_R_COMPILE
47 # undef HAVE_REENTRANT_FUNCTIONS
49 # undef HAVE_GETHOSTBYNAME_R
50 # undef HAVE_GETHOSTBYADDR_R
53 # include <ldap_pvt_thread.h>
54 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
56 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
57 && defined( CTIME_R_NARGS )
60 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
63 # if defined(HAVE_GETHOSTBYNAME_R) && \
64 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
65 /* Don't know how to handle this version, pretend it's not there */
66 # undef HAVE_GETHOSTBYNAME_R
68 # if defined(HAVE_GETHOSTBYADDR_R) && \
69 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
70 /* Don't know how to handle this version, pretend it's not there */
71 # undef HAVE_GETHOSTBYADDR_R
73 #endif /* LDAP_R_COMPILE */
75 char *ldap_pvt_ctime( const time_t *tp, char *buf )
78 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
79 # error "CTIME_R_NARGS should be 2 or 3"
80 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
81 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
82 # elif CTIME_R_NARGS > 2
83 return ctime_r(tp,buf,26);
85 return ctime_r(tp,buf);
90 # ifdef LDAP_R_COMPILE
91 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
94 AC_MEMCPY( buf, ctime(tp), 26 );
96 # ifdef LDAP_R_COMPILE
97 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
104 #define BUFSTART (1024-32)
105 #define BUFMAX (32*1024-32)
107 static char *safe_realloc( char **buf, int len );
109 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
110 static int copy_hostent( struct hostent *res,
111 char **buf, struct hostent * src );
114 int ldap_pvt_gethostbyname_a(
116 struct hostent *resbuf,
118 struct hostent **result,
121 #if defined( HAVE_GETHOSTBYNAME_R )
123 # define NEED_SAFE_REALLOC 1
127 for(;buflen<BUFMAX;) {
128 if (safe_realloc( buf, buflen )==NULL)
131 #if (GETHOSTBYNAME_R_NARGS < 6)
132 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
133 r = (*result == NULL) ? -1 : 0;
135 r = gethostbyname_r( name, resbuf, *buf,
136 buflen, result, herrno_ptr );
139 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
142 #ifdef NETDB_INTERNAL
144 (*herrno_ptr==NETDB_INTERNAL) &&
154 #elif defined( LDAP_R_COMPILE )
155 # define NEED_COPY_HOSTENT
160 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
162 he = gethostbyname( name );
165 *herrno_ptr = h_errno;
167 } else if (copy_hostent( resbuf, buf, he )<0) {
175 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
180 *result = gethostbyname( name );
186 *herrno_ptr = h_errno;
194 hp_strerror( int err )
197 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
198 case TRY_AGAIN: return _("Host not found (server fail?)");
199 case NO_RECOVERY: return _("Non-recoverable failure");
200 case NO_DATA: return _("No data of requested type");
201 #ifdef NETDB_INTERNAL
202 case NETDB_INTERNAL: return STRERROR( errno );
205 return _("Unknown resolver error");
209 int ldap_pvt_get_hname(
210 const struct sockaddr *sa,
217 #if defined( HAVE_GETNAMEINFO )
219 #if defined( LDAP_R_COMPILE )
220 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
222 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
223 #if defined( LDAP_R_COMPILE )
224 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
226 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
229 #else /* !HAVE_GETNAMEINFO */
232 struct hostent *hp = NULL;
233 #ifdef HAVE_GETHOSTBYADDR_R
235 int buflen=BUFSTART, h_errno;
240 if (sa->sa_family == AF_INET6) {
241 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
242 addr = (char *)&sin->sin6_addr;
243 alen = sizeof(sin->sin6_addr);
246 if (sa->sa_family == AF_INET) {
247 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
248 addr = (char *)&sin->sin_addr;
249 alen = sizeof(sin->sin_addr);
252 *err = (char *)hp_strerror( rc );
255 #if defined( HAVE_GETHOSTBYADDR_R )
256 for(;buflen<BUFMAX;) {
257 if (safe_realloc( &buf, buflen )==NULL) {
258 *err = (char *)STRERROR( ENOMEM );
261 #if (GETHOSTBYADDR_R_NARGS < 8)
262 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
263 &hb, buf, buflen, &h_errno );
264 rc = (hp == NULL) ? -1 : 0;
266 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
270 #ifdef NETDB_INTERNAL
272 (h_errno==NETDB_INTERNAL) &&
282 strncpy( name, hp->h_name, namelen );
284 *err = (char *)hp_strerror( h_errno );
287 #else /* HAVE_GETHOSTBYADDR_R */
289 #if defined( LDAP_R_COMPILE )
290 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
292 hp = gethostbyaddr( addr, alen, sa->sa_family );
294 strncpy( name, hp->h_name, namelen );
298 *err = (char *)hp_strerror( h_errno );
300 #if defined( LDAP_R_COMPILE )
301 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
304 #endif /* !HAVE_GETHOSTBYADDR_R */
306 #endif /* !HAVE_GETNAMEINFO */
309 int ldap_pvt_gethostbyaddr_a(
313 struct hostent *resbuf,
315 struct hostent **result,
318 #if defined( HAVE_GETHOSTBYADDR_R )
320 # undef NEED_SAFE_REALLOC
321 # define NEED_SAFE_REALLOC
325 for(;buflen<BUFMAX;) {
326 if (safe_realloc( buf, buflen )==NULL)
328 #if (GETHOSTBYADDR_R_NARGS < 8)
329 *result=gethostbyaddr_r( addr, len, type,
330 resbuf, *buf, buflen, herrno_ptr );
331 r = (*result == NULL) ? -1 : 0;
333 r = gethostbyaddr_r( addr, len, type,
334 resbuf, *buf, buflen,
335 result, herrno_ptr );
338 #ifdef NETDB_INTERNAL
340 (*herrno_ptr==NETDB_INTERNAL) &&
350 #elif defined( LDAP_R_COMPILE )
351 # undef NEED_COPY_HOSTENT
352 # define NEED_COPY_HOSTENT
357 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
359 he = gethostbyaddr( addr, len, type );
362 *herrno_ptr = h_errno;
364 } else if (copy_hostent( resbuf, buf, he )<0) {
372 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
376 #else /* gethostbyaddr() */
378 *result = gethostbyaddr( addr, len, type );
387 * ldap_int_utils_init() should be called before any other function.
390 void ldap_int_utils_init( void )
397 #ifdef LDAP_R_COMPILE
398 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
399 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
401 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
403 #ifdef HAVE_CYRUS_SASL
404 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
408 /* call other module init functions here... */
411 #if defined( NEED_COPY_HOSTENT )
412 # undef NEED_SAFE_REALLOC
413 #define NEED_SAFE_REALLOC
415 static char *cpy_aliases(
422 for( ; (*src) ; src++ ) {
423 len = strlen( *src ) + 1;
424 AC_MEMCPY( buf, *src, len );
432 static char *cpy_addresses(
439 for( ; (*src) ; src++ ) {
440 AC_MEMCPY( buf, *src, len );
448 static int copy_hostent(
451 struct hostent * src )
458 int total_alias_len=0;
463 /* calculate the size needed for the buffer */
464 name_len = strlen( src->h_name ) + 1;
466 if( src->h_aliases != NULL ) {
467 for( p = src->h_aliases; (*p) != NULL; p++ ) {
468 total_alias_len += strlen( *p ) + 1;
473 if( src->h_addr_list != NULL ) {
474 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
477 total_addr_len = n_addr * src->h_length;
480 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
481 total_addr_len + total_alias_len + name_len;
483 if (safe_realloc( buf, total_len )) {
485 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
486 AC_MEMCPY( res, src, sizeof( struct hostent ) );
487 /* first the name... */
488 AC_MEMCPY( tbuf, src->h_name, name_len );
489 res->h_name = tbuf; tbuf+=name_len;
490 /* now the aliases */
492 if ( src->h_aliases != NULL ) {
493 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
496 /* finally the addresses */
497 res->h_addr_list = tp;
498 if ( src->h_addr_list != NULL ) {
499 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
508 #if defined( NEED_SAFE_REALLOC )
509 static char *safe_realloc( char **buf, int len )
512 tmpbuf = LDAP_REALLOC( *buf, len );
520 char * ldap_pvt_get_fqdn( char *name )
523 char hostbuf[MAXHOSTNAMELEN+1];
524 struct hostent *hp, he_buf;
525 int rc, local_h_errno;
528 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
529 hostbuf[MAXHOSTNAMELEN] = '\0';
536 rc = ldap_pvt_gethostbyname_a( name,
537 &he_buf, &ha_buf, &hp, &local_h_errno );
539 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
540 fqdn = LDAP_STRDUP( name );
542 fqdn = LDAP_STRDUP( hp->h_name );
549 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
550 && !defined( HAVE_GAI_STRERROR )
551 char *ldap_pvt_gai_strerror (int code) {
556 #ifdef EAI_ADDRFAMILY
557 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
559 { EAI_AGAIN, N_("Temporary failure in name resolution") },
560 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
561 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
562 { EAI_FAMILY, N_("ai_family not supported") },
563 { EAI_MEMORY, N_("Memory allocation failure") },
565 { EAI_NODATA, N_("No address associated with hostname") },
567 { EAI_NONAME, N_("Name or service not known") },
568 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
569 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
570 { EAI_SYSTEM, N_("System error") },
576 for ( i = 0; values[i].msg != NULL; i++ ) {
577 if ( values[i].code == code ) {
578 return (char *) _(values[i].msg);
582 return _("Unknown error");