2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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).
47 # define HSTRERROR(e) hstrerror(e)
49 # define HSTRERROR(e) hp_strerror(e)
52 #ifndef LDAP_R_COMPILE
53 # undef HAVE_REENTRANT_FUNCTIONS
55 # undef HAVE_GETHOSTBYNAME_R
56 # undef HAVE_GETHOSTBYADDR_R
59 # include <ldap_pvt_thread.h>
60 ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
62 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
63 && defined( CTIME_R_NARGS )
66 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
69 # if defined(HAVE_GETHOSTBYNAME_R) && \
70 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
71 /* Don't know how to handle this version, pretend it's not there */
72 # undef HAVE_GETHOSTBYNAME_R
74 # if defined(HAVE_GETHOSTBYADDR_R) && \
75 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
76 /* Don't know how to handle this version, pretend it's not there */
77 # undef HAVE_GETHOSTBYADDR_R
79 #endif /* LDAP_R_COMPILE */
81 char *ldap_pvt_ctime( const time_t *tp, char *buf )
84 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
85 # error "CTIME_R_NARGS should be 2 or 3"
86 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
87 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
88 # elif CTIME_R_NARGS > 2
89 return ctime_r(tp,buf,26);
91 return ctime_r(tp,buf);
96 # ifdef LDAP_R_COMPILE
97 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
100 AC_MEMCPY( buf, ctime(tp), 26 );
102 # ifdef LDAP_R_COMPILE
103 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
110 #define BUFSTART (1024-32)
111 #define BUFMAX (32*1024-32)
113 static char *safe_realloc( char **buf, int len );
115 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
116 static int copy_hostent( struct hostent *res,
117 char **buf, struct hostent * src );
120 int ldap_pvt_gethostbyname_a(
122 struct hostent *resbuf,
124 struct hostent **result,
127 #if defined( HAVE_GETHOSTBYNAME_R )
129 # define NEED_SAFE_REALLOC 1
133 for(;buflen<BUFMAX;) {
134 if (safe_realloc( buf, buflen )==NULL)
137 #if (GETHOSTBYNAME_R_NARGS < 6)
138 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
139 r = (*result == NULL) ? -1 : 0;
141 r = gethostbyname_r( name, resbuf, *buf,
142 buflen, result, herrno_ptr );
145 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
148 #ifdef NETDB_INTERNAL
150 (*herrno_ptr==NETDB_INTERNAL) &&
160 #elif defined( LDAP_R_COMPILE )
161 # define NEED_COPY_HOSTENT
166 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
168 he = gethostbyname( name );
171 *herrno_ptr = h_errno;
173 } else if (copy_hostent( resbuf, buf, he )<0) {
181 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
186 *result = gethostbyname( name );
192 *herrno_ptr = h_errno;
198 #if !defined( GETNAMEINFO ) && !defined( HAVE_HERROR )
200 hp_strerror( int err )
203 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
204 case TRY_AGAIN: return _("Host not found (server fail?)");
205 case NO_RECOVERY: return _("Non-recoverable failure");
206 case NO_DATA: return _("No data of requested type");
207 #ifdef NETDB_INTERNAL
208 case NETDB_INTERNAL: return STRERROR( errno );
211 return _("Unknown resolver error");
215 int ldap_pvt_get_hname(
216 const struct sockaddr *sa,
223 #if defined( HAVE_GETNAMEINFO )
225 #if defined( LDAP_R_COMPILE )
226 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
228 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
229 #if defined( LDAP_R_COMPILE )
230 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
232 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
235 #else /* !HAVE_GETNAMEINFO */
238 struct hostent *hp = NULL;
239 #ifdef HAVE_GETHOSTBYADDR_R
241 int buflen=BUFSTART, h_errno;
246 if (sa->sa_family == AF_INET6) {
247 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
248 addr = (char *)&sin->sin6_addr;
249 alen = sizeof(sin->sin6_addr);
252 if (sa->sa_family == AF_INET) {
253 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
254 addr = (char *)&sin->sin_addr;
255 alen = sizeof(sin->sin_addr);
258 *err = (char *)HSTRERROR( rc );
261 #if defined( HAVE_GETHOSTBYADDR_R )
262 for(;buflen<BUFMAX;) {
263 if (safe_realloc( &buf, buflen )==NULL) {
264 *err = (char *)STRERROR( ENOMEM );
267 #if (GETHOSTBYADDR_R_NARGS < 8)
268 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
269 &hb, buf, buflen, &h_errno );
270 rc = (hp == NULL) ? -1 : 0;
272 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
276 #ifdef NETDB_INTERNAL
278 (h_errno==NETDB_INTERNAL) &&
288 strncpy( name, hp->h_name, namelen );
290 *err = (char *)HSTRERROR( h_errno );
293 #else /* HAVE_GETHOSTBYADDR_R */
295 #if defined( LDAP_R_COMPILE )
296 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
298 hp = gethostbyaddr( addr, alen, sa->sa_family );
300 strncpy( name, hp->h_name, namelen );
304 *err = (char *)hp_strerror( h_errno );
306 #if defined( LDAP_R_COMPILE )
307 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
310 #endif /* !HAVE_GETHOSTBYADDR_R */
312 #endif /* !HAVE_GETNAMEINFO */
315 int ldap_pvt_gethostbyaddr_a(
319 struct hostent *resbuf,
321 struct hostent **result,
324 #if defined( HAVE_GETHOSTBYADDR_R )
326 # undef NEED_SAFE_REALLOC
327 # define NEED_SAFE_REALLOC
331 for(;buflen<BUFMAX;) {
332 if (safe_realloc( buf, buflen )==NULL)
334 #if (GETHOSTBYADDR_R_NARGS < 8)
335 *result=gethostbyaddr_r( addr, len, type,
336 resbuf, *buf, buflen, herrno_ptr );
337 r = (*result == NULL) ? -1 : 0;
339 r = gethostbyaddr_r( addr, len, type,
340 resbuf, *buf, buflen,
341 result, herrno_ptr );
344 #ifdef NETDB_INTERNAL
346 (*herrno_ptr==NETDB_INTERNAL) &&
356 #elif defined( LDAP_R_COMPILE )
357 # undef NEED_COPY_HOSTENT
358 # define NEED_COPY_HOSTENT
363 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
365 he = gethostbyaddr( addr, len, type );
368 *herrno_ptr = h_errno;
370 } else if (copy_hostent( resbuf, buf, he )<0) {
378 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
382 #else /* gethostbyaddr() */
384 *result = gethostbyaddr( addr, len, type );
393 * ldap_int_utils_init() should be called before any other function.
396 void ldap_int_utils_init( void )
403 #ifdef LDAP_R_COMPILE
404 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
405 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
407 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
409 #ifdef HAVE_CYRUS_SASL
410 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
414 /* call other module init functions here... */
417 #if defined( NEED_COPY_HOSTENT )
418 # undef NEED_SAFE_REALLOC
419 #define NEED_SAFE_REALLOC
421 static char *cpy_aliases(
428 for( ; (*src) ; src++ ) {
429 len = strlen( *src ) + 1;
430 AC_MEMCPY( buf, *src, len );
438 static char *cpy_addresses(
445 for( ; (*src) ; src++ ) {
446 AC_MEMCPY( buf, *src, len );
454 static int copy_hostent(
457 struct hostent * src )
464 int total_alias_len=0;
469 /* calculate the size needed for the buffer */
470 name_len = strlen( src->h_name ) + 1;
472 if( src->h_aliases != NULL ) {
473 for( p = src->h_aliases; (*p) != NULL; p++ ) {
474 total_alias_len += strlen( *p ) + 1;
479 if( src->h_addr_list != NULL ) {
480 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
483 total_addr_len = n_addr * src->h_length;
486 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
487 total_addr_len + total_alias_len + name_len;
489 if (safe_realloc( buf, total_len )) {
491 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
492 AC_MEMCPY( res, src, sizeof( struct hostent ) );
493 /* first the name... */
494 AC_MEMCPY( tbuf, src->h_name, name_len );
495 res->h_name = tbuf; tbuf+=name_len;
496 /* now the aliases */
498 if ( src->h_aliases != NULL ) {
499 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
502 /* finally the addresses */
503 res->h_addr_list = tp;
504 if ( src->h_addr_list != NULL ) {
505 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
514 #if defined( NEED_SAFE_REALLOC )
515 static char *safe_realloc( char **buf, int len )
518 tmpbuf = LDAP_REALLOC( *buf, len );
526 char * ldap_pvt_get_fqdn( char *name )
529 char hostbuf[MAXHOSTNAMELEN+1];
530 struct hostent *hp, he_buf;
531 int rc, local_h_errno;
534 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
535 hostbuf[MAXHOSTNAMELEN] = '\0';
542 rc = ldap_pvt_gethostbyname_a( name,
543 &he_buf, &ha_buf, &hp, &local_h_errno );
545 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
546 fqdn = LDAP_STRDUP( name );
548 fqdn = LDAP_STRDUP( hp->h_name );
555 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
556 && !defined( HAVE_GAI_STRERROR )
557 char *ldap_pvt_gai_strerror (int code) {
562 #ifdef EAI_ADDRFAMILY
563 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
565 { EAI_AGAIN, N_("Temporary failure in name resolution") },
566 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
567 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
568 { EAI_FAMILY, N_("ai_family not supported") },
569 { EAI_MEMORY, N_("Memory allocation failure") },
571 { EAI_NODATA, N_("No address associated with hostname") },
573 { EAI_NONAME, N_("Name or service not known") },
574 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
575 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
576 { EAI_SYSTEM, N_("System error") },
582 for ( i = 0; values[i].msg != NULL; i++ ) {
583 if ( values[i].code == code ) {
584 return (char *) _(values[i].msg);
588 return _("Unknown error");