2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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 # ifdef LDAP_R_COMPILE
111 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
114 AC_MEMCPY( buf, ctime(tp), 26 );
116 # ifdef LDAP_R_COMPILE
117 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
124 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
126 ldap_pvt_gmtime_lock( void )
128 # ifndef LDAP_R_COMPILE
130 # else /* LDAP_R_COMPILE */
131 return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
132 # endif /* LDAP_R_COMPILE */
136 ldap_pvt_gmtime_unlock( void )
138 # ifndef LDAP_R_COMPILE
140 # else /* LDAP_R_COMPILE */
141 return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
142 # endif /* LDAP_R_COMPILE */
144 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
148 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
152 # ifdef LDAP_R_COMPILE
153 ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
154 # endif /* LDAP_R_COMPILE */
156 tm_ptr = gmtime( timep );
157 if ( tm_ptr == NULL ) {
164 # ifdef LDAP_R_COMPILE
165 ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
166 # endif /* LDAP_R_COMPILE */
170 #endif /* !USE_GMTIME_R */
172 #ifndef USE_LOCALTIME_R
174 ldap_pvt_localtime( const time_t *timep, struct tm *result )
178 # ifdef LDAP_R_COMPILE
179 ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
180 # endif /* LDAP_R_COMPILE */
182 tm_ptr = localtime( timep );
183 if ( tm_ptr == NULL ) {
190 # ifdef LDAP_R_COMPILE
191 ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
192 # endif /* LDAP_R_COMPILE */
196 #endif /* !USE_LOCALTIME_R */
198 /* return a broken out time, with microseconds
199 * Must be mutex-protected.
202 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
203 * also need to use a high resolution timer to get microseconds.
204 * This is pretty clunky.
207 ldap_pvt_gettime( struct lutil_tm *tm )
209 static LARGE_INTEGER cFreq;
210 static LARGE_INTEGER prevCount;
216 GetSystemTime( &st );
217 QueryPerformanceCounter( &count );
219 /* It shouldn't ever go backwards, but multiple CPUs might
220 * be able to hit in the same tick.
222 if ( count.QuadPart <= prevCount.QuadPart ) {
229 /* We assume Windows has at least a vague idea of
230 * when a second begins. So we align our microsecond count
231 * with the Windows millisecond count using this offset.
232 * We retain the submillisecond portion of our own count.
234 * Note - this also assumes that the relationship between
235 * the PerformanceCouunter and SystemTime stays constant;
236 * that assumption breaks if the SystemTime is adjusted by
237 * an external action.
239 if ( !cFreq.QuadPart ) {
242 QueryPerformanceFrequency( &cFreq );
244 /* just get sub-second portion of counter */
245 t = count.QuadPart % cFreq.QuadPart;
247 /* convert to microseconds */
249 usec = t / cFreq.QuadPart;
251 offset = usec - st.wMilliseconds * 1000;
256 /* convert to microseconds */
257 count.QuadPart %= cFreq.QuadPart;
258 count.QuadPart *= 1000000;
259 count.QuadPart /= cFreq.QuadPart;
260 count.QuadPart -= offset;
262 tm->tm_usec = count.QuadPart % 1000000;
263 if ( tm->tm_usec < 0 )
264 tm->tm_usec += 1000000;
266 /* any difference larger than microseconds is
267 * already reflected in st
270 tm->tm_sec = st.wSecond;
271 tm->tm_min = st.wMinute;
272 tm->tm_hour = st.wHour;
273 tm->tm_mday = st.wDay;
274 tm->tm_mon = st.wMonth - 1;
275 tm->tm_year = st.wYear - 1900;
279 ldap_pvt_gettime( struct lutil_tm *ltm )
282 static struct timeval prevTv;
288 gettimeofday( &tv, NULL );
291 if ( tv.tv_sec < prevTv.tv_sec
292 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
301 ldap_pvt_gmtime( &t, &tm );
303 ltm->tm_sec = tm.tm_sec;
304 ltm->tm_min = tm.tm_min;
305 ltm->tm_hour = tm.tm_hour;
306 ltm->tm_mday = tm.tm_mday;
307 ltm->tm_mon = tm.tm_mon;
308 ltm->tm_year = tm.tm_year;
309 ltm->tm_usec = tv.tv_usec;
314 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
319 ldap_pvt_gettime( &tm );
321 n = snprintf( buf, len,
322 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
323 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
324 tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
326 if( n < 0 ) return 0;
327 return ( (size_t) n < len ) ? n : 0;
330 #define BUFSTART (1024-32)
331 #define BUFMAX (32*1024-32)
333 #if defined(LDAP_R_COMPILE)
334 static char *safe_realloc( char **buf, int len );
336 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
337 static int copy_hostent( struct hostent *res,
338 char **buf, struct hostent * src );
342 int ldap_pvt_gethostbyname_a(
344 struct hostent *resbuf,
346 struct hostent **result,
349 #if defined( HAVE_GETHOSTBYNAME_R )
351 # define NEED_SAFE_REALLOC 1
355 for(;buflen<BUFMAX;) {
356 if (safe_realloc( buf, buflen )==NULL)
359 #if (GETHOSTBYNAME_R_NARGS < 6)
360 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
361 r = (*result == NULL) ? -1 : 0;
363 r = gethostbyname_r( name, resbuf, *buf,
364 buflen, result, herrno_ptr );
367 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
370 #ifdef NETDB_INTERNAL
372 (*herrno_ptr==NETDB_INTERNAL) &&
382 #elif defined( LDAP_R_COMPILE )
383 # define NEED_COPY_HOSTENT
388 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
390 he = gethostbyname( name );
393 *herrno_ptr = h_errno;
395 } else if (copy_hostent( resbuf, buf, he )<0) {
403 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
408 *result = gethostbyname( name );
414 *herrno_ptr = h_errno;
420 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
422 hp_strerror( int err )
425 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
426 case TRY_AGAIN: return _("Host not found (server fail?)");
427 case NO_RECOVERY: return _("Non-recoverable failure");
428 case NO_DATA: return _("No data of requested type");
429 #ifdef NETDB_INTERNAL
430 case NETDB_INTERNAL: return STRERROR( errno );
433 return _("Unknown resolver error");
437 int ldap_pvt_get_hname(
438 const struct sockaddr *sa,
445 #if defined( HAVE_GETNAMEINFO )
447 #if defined( LDAP_R_COMPILE )
448 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
450 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
451 #if defined( LDAP_R_COMPILE )
452 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
454 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
457 #else /* !HAVE_GETNAMEINFO */
460 struct hostent *hp = NULL;
461 #ifdef HAVE_GETHOSTBYADDR_R
463 int buflen=BUFSTART, h_errno;
468 if (sa->sa_family == AF_INET6) {
469 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
470 addr = (char *)&sin->sin6_addr;
471 alen = sizeof(sin->sin6_addr);
474 if (sa->sa_family == AF_INET) {
475 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
476 addr = (char *)&sin->sin_addr;
477 alen = sizeof(sin->sin_addr);
480 *err = (char *)HSTRERROR( rc );
483 #if defined( HAVE_GETHOSTBYADDR_R )
484 for(;buflen<BUFMAX;) {
485 if (safe_realloc( &buf, buflen )==NULL) {
486 *err = (char *)STRERROR( ENOMEM );
489 #if (GETHOSTBYADDR_R_NARGS < 8)
490 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
491 &hb, buf, buflen, &h_errno );
492 rc = (hp == NULL) ? -1 : 0;
494 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
498 #ifdef NETDB_INTERNAL
500 (h_errno==NETDB_INTERNAL) &&
510 strncpy( name, hp->h_name, namelen );
512 *err = (char *)HSTRERROR( h_errno );
515 #else /* HAVE_GETHOSTBYADDR_R */
517 #if defined( LDAP_R_COMPILE )
518 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
520 hp = gethostbyaddr( addr, alen, sa->sa_family );
522 strncpy( name, hp->h_name, namelen );
526 *err = (char *)HSTRERROR( h_errno );
528 #if defined( LDAP_R_COMPILE )
529 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
532 #endif /* !HAVE_GETHOSTBYADDR_R */
534 #endif /* !HAVE_GETNAMEINFO */
537 int ldap_pvt_gethostbyaddr_a(
541 struct hostent *resbuf,
543 struct hostent **result,
546 #if defined( HAVE_GETHOSTBYADDR_R )
548 # undef NEED_SAFE_REALLOC
549 # define NEED_SAFE_REALLOC
553 for(;buflen<BUFMAX;) {
554 if (safe_realloc( buf, buflen )==NULL)
556 #if (GETHOSTBYADDR_R_NARGS < 8)
557 *result=gethostbyaddr_r( addr, len, type,
558 resbuf, *buf, buflen, herrno_ptr );
559 r = (*result == NULL) ? -1 : 0;
561 r = gethostbyaddr_r( addr, len, type,
562 resbuf, *buf, buflen,
563 result, herrno_ptr );
566 #ifdef NETDB_INTERNAL
568 (*herrno_ptr==NETDB_INTERNAL) &&
578 #elif defined( LDAP_R_COMPILE )
579 # undef NEED_COPY_HOSTENT
580 # define NEED_COPY_HOSTENT
585 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
587 he = gethostbyaddr( addr, len, type );
590 *herrno_ptr = h_errno;
592 } else if (copy_hostent( resbuf, buf, he )<0) {
600 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
604 #else /* gethostbyaddr() */
606 *result = gethostbyaddr( addr, len, type );
615 * ldap_int_utils_init() should be called before any other function.
618 void ldap_int_utils_init( void )
625 #ifdef LDAP_R_COMPILE
626 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
627 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
629 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
630 ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
632 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
634 #ifdef HAVE_CYRUS_SASL
635 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
638 ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
642 /* call other module init functions here... */
645 #if defined( NEED_COPY_HOSTENT )
646 # undef NEED_SAFE_REALLOC
647 #define NEED_SAFE_REALLOC
649 static char *cpy_aliases(
656 for( ; (*src) ; src++ ) {
657 len = strlen( *src ) + 1;
658 AC_MEMCPY( buf, *src, len );
666 static char *cpy_addresses(
673 for( ; (*src) ; src++ ) {
674 AC_MEMCPY( buf, *src, len );
682 static int copy_hostent(
685 struct hostent * src )
692 int total_alias_len=0;
694 int total_addr_len=0;
697 /* calculate the size needed for the buffer */
698 name_len = strlen( src->h_name ) + 1;
700 if( src->h_aliases != NULL ) {
701 for( p = src->h_aliases; (*p) != NULL; p++ ) {
702 total_alias_len += strlen( *p ) + 1;
707 if( src->h_addr_list != NULL ) {
708 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
711 total_addr_len = n_addr * src->h_length;
714 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
715 total_addr_len + total_alias_len + name_len;
717 if (safe_realloc( buf, total_len )) {
719 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
720 AC_MEMCPY( res, src, sizeof( struct hostent ) );
721 /* first the name... */
722 AC_MEMCPY( tbuf, src->h_name, name_len );
723 res->h_name = tbuf; tbuf+=name_len;
724 /* now the aliases */
726 if ( src->h_aliases != NULL ) {
727 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
730 /* finally the addresses */
731 res->h_addr_list = tp;
732 if ( src->h_addr_list != NULL ) {
733 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
742 #if defined( NEED_SAFE_REALLOC )
743 static char *safe_realloc( char **buf, int len )
746 tmpbuf = LDAP_REALLOC( *buf, len );
754 char * ldap_pvt_get_fqdn( char *name )
757 char hostbuf[MAXHOSTNAMELEN+1];
758 struct hostent *hp, he_buf;
759 int rc, local_h_errno;
762 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
763 hostbuf[MAXHOSTNAMELEN] = '\0';
770 rc = ldap_pvt_gethostbyname_a( name,
771 &he_buf, &ha_buf, &hp, &local_h_errno );
773 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
774 fqdn = LDAP_STRDUP( name );
776 fqdn = LDAP_STRDUP( hp->h_name );
783 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
784 && !defined( HAVE_GAI_STRERROR )
785 char *ldap_pvt_gai_strerror (int code) {
790 #ifdef EAI_ADDRFAMILY
791 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
793 { EAI_AGAIN, N_("Temporary failure in name resolution") },
794 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
795 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
796 { EAI_FAMILY, N_("ai_family not supported") },
797 { EAI_MEMORY, N_("Memory allocation failure") },
799 { EAI_NODATA, N_("No address associated with hostname") },
801 { EAI_NONAME, N_("Name or service not known") },
802 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
803 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
804 { EAI_SYSTEM, N_("System error") },
810 for ( i = 0; values[i].msg != NULL; i++ ) {
811 if ( values[i].code == code ) {
812 return (char *) _(values[i].msg);
816 return _("Unknown error");