2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
72 /* to be released with 2.5 */
73 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
74 /* we use the same mutex for gmtime(3) and localtime(3)
75 * because implementations may use the same buffer
76 * for both functions */
77 static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
79 #else /* ! LDAP_DEVEL */
80 ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
81 #endif /* ! LDAP_DEVEL */
83 # if defined(HAVE_GETHOSTBYNAME_R) && \
84 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
85 /* Don't know how to handle this version, pretend it's not there */
86 # undef HAVE_GETHOSTBYNAME_R
88 # if defined(HAVE_GETHOSTBYADDR_R) && \
89 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
90 /* Don't know how to handle this version, pretend it's not there */
91 # undef HAVE_GETHOSTBYADDR_R
93 #endif /* LDAP_R_COMPILE */
95 char *ldap_pvt_ctime( const time_t *tp, char *buf )
98 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
99 # error "CTIME_R_NARGS should be 2 or 3"
100 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
101 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
102 # elif CTIME_R_NARGS > 2
103 return ctime_r(tp,buf,26);
105 return ctime_r(tp,buf);
110 LDAP_MUTEX_LOCK( &ldap_int_ctime_mutex );
111 AC_MEMCPY( buf, ctime(tp), 26 );
112 LDAP_MUTEX_UNLOCK( &ldap_int_ctime_mutex );
118 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
120 ldap_pvt_gmtime_lock( void )
122 # ifndef LDAP_R_COMPILE
124 # else /* LDAP_R_COMPILE */
125 return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
126 # endif /* LDAP_R_COMPILE */
130 ldap_pvt_gmtime_unlock( void )
132 # ifndef LDAP_R_COMPILE
134 # else /* LDAP_R_COMPILE */
135 return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
136 # endif /* LDAP_R_COMPILE */
138 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
142 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
146 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
147 tm_ptr = gmtime( timep );
148 if ( tm_ptr == NULL ) {
154 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
158 #endif /* !USE_GMTIME_R */
160 #ifndef USE_LOCALTIME_R
162 ldap_pvt_localtime( const time_t *timep, struct tm *result )
166 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
167 tm_ptr = localtime( timep );
168 if ( tm_ptr == NULL ) {
174 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
178 #endif /* !USE_LOCALTIME_R */
180 /* return a broken out time, with microseconds
181 * Must be mutex-protected.
184 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
185 * also need to use a high resolution timer to get microseconds.
186 * This is pretty clunky.
189 ldap_pvt_gettime( struct lutil_tm *tm )
191 static LARGE_INTEGER cFreq;
192 static LARGE_INTEGER prevCount;
198 GetSystemTime( &st );
199 QueryPerformanceCounter( &count );
201 /* It shouldn't ever go backwards, but multiple CPUs might
202 * be able to hit in the same tick.
204 if ( count.QuadPart <= prevCount.QuadPart ) {
211 /* We assume Windows has at least a vague idea of
212 * when a second begins. So we align our microsecond count
213 * with the Windows millisecond count using this offset.
214 * We retain the submillisecond portion of our own count.
216 * Note - this also assumes that the relationship between
217 * the PerformanceCouunter and SystemTime stays constant;
218 * that assumption breaks if the SystemTime is adjusted by
219 * an external action.
221 if ( !cFreq.QuadPart ) {
224 QueryPerformanceFrequency( &cFreq );
226 /* just get sub-second portion of counter */
227 t = count.QuadPart % cFreq.QuadPart;
229 /* convert to microseconds */
231 usec = t / cFreq.QuadPart;
233 offset = usec - st.wMilliseconds * 1000;
238 /* convert to microseconds */
239 count.QuadPart %= cFreq.QuadPart;
240 count.QuadPart *= 1000000;
241 count.QuadPart /= cFreq.QuadPart;
242 count.QuadPart -= offset;
244 tm->tm_usec = count.QuadPart % 1000000;
245 if ( tm->tm_usec < 0 )
246 tm->tm_usec += 1000000;
248 /* any difference larger than microseconds is
249 * already reflected in st
252 tm->tm_sec = st.wSecond;
253 tm->tm_min = st.wMinute;
254 tm->tm_hour = st.wHour;
255 tm->tm_mday = st.wDay;
256 tm->tm_mon = st.wMonth - 1;
257 tm->tm_year = st.wYear - 1900;
261 ldap_pvt_gettime( struct lutil_tm *ltm )
264 static struct timeval prevTv;
270 gettimeofday( &tv, NULL );
273 if ( tv.tv_sec < prevTv.tv_sec
274 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
283 ldap_pvt_gmtime( &t, &tm );
285 ltm->tm_sec = tm.tm_sec;
286 ltm->tm_min = tm.tm_min;
287 ltm->tm_hour = tm.tm_hour;
288 ltm->tm_mday = tm.tm_mday;
289 ltm->tm_mon = tm.tm_mon;
290 ltm->tm_year = tm.tm_year;
291 ltm->tm_usec = tv.tv_usec;
296 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
301 ldap_pvt_gettime( &tm );
303 n = snprintf( buf, len,
304 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
305 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
306 tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
308 if( n < 0 ) return 0;
309 return ( (size_t) n < len ) ? n : 0;
312 #define BUFSTART (1024-32)
313 #define BUFMAX (32*1024-32)
315 #if defined(LDAP_R_COMPILE)
316 static char *safe_realloc( char **buf, int len );
318 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
319 static int copy_hostent( struct hostent *res,
320 char **buf, struct hostent * src );
324 int ldap_pvt_gethostbyname_a(
326 struct hostent *resbuf,
328 struct hostent **result,
331 #if defined( HAVE_GETHOSTBYNAME_R )
333 # define NEED_SAFE_REALLOC 1
337 for(;buflen<BUFMAX;) {
338 if (safe_realloc( buf, buflen )==NULL)
341 #if (GETHOSTBYNAME_R_NARGS < 6)
342 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
343 r = (*result == NULL) ? -1 : 0;
345 r = gethostbyname_r( name, resbuf, *buf,
346 buflen, result, herrno_ptr );
349 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
352 #ifdef NETDB_INTERNAL
354 (*herrno_ptr==NETDB_INTERNAL) &&
364 #elif defined( LDAP_R_COMPILE )
365 # define NEED_COPY_HOSTENT
370 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
372 he = gethostbyname( name );
375 *herrno_ptr = h_errno;
377 } else if (copy_hostent( resbuf, buf, he )<0) {
385 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
390 *result = gethostbyname( name );
396 *herrno_ptr = h_errno;
402 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
404 hp_strerror( int err )
407 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
408 case TRY_AGAIN: return _("Host not found (server fail?)");
409 case NO_RECOVERY: return _("Non-recoverable failure");
410 case NO_DATA: return _("No data of requested type");
411 #ifdef NETDB_INTERNAL
412 case NETDB_INTERNAL: return STRERROR( errno );
415 return _("Unknown resolver error");
419 int ldap_pvt_get_hname(
420 const struct sockaddr *sa,
427 #if defined( HAVE_GETNAMEINFO )
429 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
430 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
431 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
432 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
435 #else /* !HAVE_GETNAMEINFO */
438 struct hostent *hp = NULL;
439 #ifdef HAVE_GETHOSTBYADDR_R
441 int buflen=BUFSTART, h_errno;
446 if (sa->sa_family == AF_INET6) {
447 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
448 addr = (char *)&sin->sin6_addr;
449 alen = sizeof(sin->sin6_addr);
452 if (sa->sa_family == AF_INET) {
453 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
454 addr = (char *)&sin->sin_addr;
455 alen = sizeof(sin->sin_addr);
458 *err = (char *)HSTRERROR( rc );
461 #if defined( HAVE_GETHOSTBYADDR_R )
462 for(;buflen<BUFMAX;) {
463 if (safe_realloc( &buf, buflen )==NULL) {
464 *err = (char *)STRERROR( ENOMEM );
467 #if (GETHOSTBYADDR_R_NARGS < 8)
468 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
469 &hb, buf, buflen, &h_errno );
470 rc = (hp == NULL) ? -1 : 0;
472 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
476 #ifdef NETDB_INTERNAL
478 (h_errno==NETDB_INTERNAL) &&
488 strncpy( name, hp->h_name, namelen );
490 *err = (char *)HSTRERROR( h_errno );
493 #else /* HAVE_GETHOSTBYADDR_R */
495 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
496 hp = gethostbyaddr( addr, alen, sa->sa_family );
498 strncpy( name, hp->h_name, namelen );
502 *err = (char *)HSTRERROR( h_errno );
504 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
506 #endif /* !HAVE_GETHOSTBYADDR_R */
508 #endif /* !HAVE_GETNAMEINFO */
511 int ldap_pvt_gethostbyaddr_a(
515 struct hostent *resbuf,
517 struct hostent **result,
520 #if defined( HAVE_GETHOSTBYADDR_R )
522 # undef NEED_SAFE_REALLOC
523 # define NEED_SAFE_REALLOC
527 for(;buflen<BUFMAX;) {
528 if (safe_realloc( buf, buflen )==NULL)
530 #if (GETHOSTBYADDR_R_NARGS < 8)
531 *result=gethostbyaddr_r( addr, len, type,
532 resbuf, *buf, buflen, herrno_ptr );
533 r = (*result == NULL) ? -1 : 0;
535 r = gethostbyaddr_r( addr, len, type,
536 resbuf, *buf, buflen,
537 result, herrno_ptr );
540 #ifdef NETDB_INTERNAL
542 (*herrno_ptr==NETDB_INTERNAL) &&
552 #elif defined( LDAP_R_COMPILE )
553 # undef NEED_COPY_HOSTENT
554 # define NEED_COPY_HOSTENT
559 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
560 he = gethostbyaddr( addr, len, type );
563 *herrno_ptr = h_errno;
565 } else if (copy_hostent( resbuf, buf, he )<0) {
572 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
576 #else /* gethostbyaddr() */
578 *result = gethostbyaddr( addr, len, type );
587 * ldap_int_utils_init() should be called before any other function.
590 void ldap_int_utils_init( void )
597 #ifdef LDAP_R_COMPILE
598 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
599 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
601 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
602 ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
604 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
606 #ifdef HAVE_CYRUS_SASL
607 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
610 ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
614 /* call other module init functions here... */
617 #if defined( NEED_COPY_HOSTENT )
618 # undef NEED_SAFE_REALLOC
619 #define NEED_SAFE_REALLOC
621 static char *cpy_aliases(
628 for( ; (*src) ; src++ ) {
629 len = strlen( *src ) + 1;
630 AC_MEMCPY( buf, *src, len );
638 static char *cpy_addresses(
645 for( ; (*src) ; src++ ) {
646 AC_MEMCPY( buf, *src, len );
654 static int copy_hostent(
657 struct hostent * src )
664 int total_alias_len=0;
666 int total_addr_len=0;
669 /* calculate the size needed for the buffer */
670 name_len = strlen( src->h_name ) + 1;
672 if( src->h_aliases != NULL ) {
673 for( p = src->h_aliases; (*p) != NULL; p++ ) {
674 total_alias_len += strlen( *p ) + 1;
679 if( src->h_addr_list != NULL ) {
680 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
683 total_addr_len = n_addr * src->h_length;
686 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
687 total_addr_len + total_alias_len + name_len;
689 if (safe_realloc( buf, total_len )) {
691 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
692 AC_MEMCPY( res, src, sizeof( struct hostent ) );
693 /* first the name... */
694 AC_MEMCPY( tbuf, src->h_name, name_len );
695 res->h_name = tbuf; tbuf+=name_len;
696 /* now the aliases */
698 if ( src->h_aliases != NULL ) {
699 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
702 /* finally the addresses */
703 res->h_addr_list = tp;
704 if ( src->h_addr_list != NULL ) {
705 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
714 #if defined( NEED_SAFE_REALLOC )
715 static char *safe_realloc( char **buf, int len )
718 tmpbuf = LDAP_REALLOC( *buf, len );
726 char * ldap_pvt_get_fqdn( char *name )
729 char hostbuf[MAXHOSTNAMELEN+1];
730 struct hostent *hp, he_buf;
731 int rc, local_h_errno;
734 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
735 hostbuf[MAXHOSTNAMELEN] = '\0';
742 rc = ldap_pvt_gethostbyname_a( name,
743 &he_buf, &ha_buf, &hp, &local_h_errno );
745 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
746 fqdn = LDAP_STRDUP( name );
748 fqdn = LDAP_STRDUP( hp->h_name );
755 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
756 && !defined( HAVE_GAI_STRERROR )
757 char *ldap_pvt_gai_strerror (int code) {
762 #ifdef EAI_ADDRFAMILY
763 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
765 { EAI_AGAIN, N_("Temporary failure in name resolution") },
766 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
767 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
768 { EAI_FAMILY, N_("ai_family not supported") },
769 { EAI_MEMORY, N_("Memory allocation failure") },
771 { EAI_NODATA, N_("No address associated with hostname") },
773 { EAI_NONAME, N_("Name or service not known") },
774 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
775 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
776 { EAI_SYSTEM, N_("System error") },
782 for ( i = 0; values[i].msg != NULL; i++ ) {
783 if ( values[i].code == code ) {
784 return (char *) _(values[i].msg);
788 return _("Unknown error");