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;
61 ldap_pvt_thread_mutex_t ldap_int_hostname_mutex;
63 # if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
64 && defined( CTIME_R_NARGS )
67 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
70 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
73 /* to be released with 2.5 */
74 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
75 /* we use the same mutex for gmtime(3) and localtime(3)
76 * because implementations may use the same buffer
77 * for both functions */
78 static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
80 #else /* ! LDAP_DEVEL */
81 ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
82 #endif /* ! LDAP_DEVEL */
84 # if defined(HAVE_GETHOSTBYNAME_R) && \
85 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
86 /* Don't know how to handle this version, pretend it's not there */
87 # undef HAVE_GETHOSTBYNAME_R
89 # if defined(HAVE_GETHOSTBYADDR_R) && \
90 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
91 /* Don't know how to handle this version, pretend it's not there */
92 # undef HAVE_GETHOSTBYADDR_R
94 #endif /* LDAP_R_COMPILE */
96 char *ldap_pvt_ctime( const time_t *tp, char *buf )
99 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
100 # error "CTIME_R_NARGS should be 2 or 3"
101 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
102 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
103 # elif CTIME_R_NARGS > 2
104 return ctime_r(tp,buf,26);
106 return ctime_r(tp,buf);
111 LDAP_MUTEX_LOCK( &ldap_int_ctime_mutex );
112 AC_MEMCPY( buf, ctime(tp), 26 );
113 LDAP_MUTEX_UNLOCK( &ldap_int_ctime_mutex );
119 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
121 ldap_pvt_gmtime_lock( void )
123 # ifndef LDAP_R_COMPILE
125 # else /* LDAP_R_COMPILE */
126 return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
127 # endif /* LDAP_R_COMPILE */
131 ldap_pvt_gmtime_unlock( void )
133 # ifndef LDAP_R_COMPILE
135 # else /* LDAP_R_COMPILE */
136 return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
137 # endif /* LDAP_R_COMPILE */
139 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
143 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
147 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
148 tm_ptr = gmtime( timep );
149 if ( tm_ptr == NULL ) {
155 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
159 #endif /* !USE_GMTIME_R */
161 #ifndef USE_LOCALTIME_R
163 ldap_pvt_localtime( const time_t *timep, struct tm *result )
167 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
168 tm_ptr = localtime( timep );
169 if ( tm_ptr == NULL ) {
175 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
179 #endif /* !USE_LOCALTIME_R */
181 /* return a broken out time, with microseconds
182 * Must be mutex-protected.
185 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
186 * also need to use a high resolution timer to get microseconds.
187 * This is pretty clunky.
190 ldap_pvt_gettime( struct lutil_tm *tm )
192 static LARGE_INTEGER cFreq;
193 static LARGE_INTEGER prevCount;
199 GetSystemTime( &st );
200 QueryPerformanceCounter( &count );
202 /* It shouldn't ever go backwards, but multiple CPUs might
203 * be able to hit in the same tick.
205 if ( count.QuadPart <= prevCount.QuadPart ) {
212 /* We assume Windows has at least a vague idea of
213 * when a second begins. So we align our microsecond count
214 * with the Windows millisecond count using this offset.
215 * We retain the submillisecond portion of our own count.
217 * Note - this also assumes that the relationship between
218 * the PerformanceCouunter and SystemTime stays constant;
219 * that assumption breaks if the SystemTime is adjusted by
220 * an external action.
222 if ( !cFreq.QuadPart ) {
225 QueryPerformanceFrequency( &cFreq );
227 /* just get sub-second portion of counter */
228 t = count.QuadPart % cFreq.QuadPart;
230 /* convert to microseconds */
232 usec = t / cFreq.QuadPart;
234 offset = usec - st.wMilliseconds * 1000;
239 /* convert to microseconds */
240 count.QuadPart %= cFreq.QuadPart;
241 count.QuadPart *= 1000000;
242 count.QuadPart /= cFreq.QuadPart;
243 count.QuadPart -= offset;
245 tm->tm_usec = count.QuadPart % 1000000;
246 if ( tm->tm_usec < 0 )
247 tm->tm_usec += 1000000;
249 /* any difference larger than microseconds is
250 * already reflected in st
253 tm->tm_sec = st.wSecond;
254 tm->tm_min = st.wMinute;
255 tm->tm_hour = st.wHour;
256 tm->tm_mday = st.wDay;
257 tm->tm_mon = st.wMonth - 1;
258 tm->tm_year = st.wYear - 1900;
262 ldap_pvt_gettime( struct lutil_tm *ltm )
265 static struct timeval prevTv;
271 gettimeofday( &tv, NULL );
274 if ( tv.tv_sec < prevTv.tv_sec
275 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
284 ldap_pvt_gmtime( &t, &tm );
286 ltm->tm_sec = tm.tm_sec;
287 ltm->tm_min = tm.tm_min;
288 ltm->tm_hour = tm.tm_hour;
289 ltm->tm_mday = tm.tm_mday;
290 ltm->tm_mon = tm.tm_mon;
291 ltm->tm_year = tm.tm_year;
292 ltm->tm_usec = tv.tv_usec;
297 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
302 ldap_pvt_gettime( &tm );
304 n = snprintf( buf, len,
305 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
306 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
307 tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
309 if( n < 0 ) return 0;
310 return ( (size_t) n < len ) ? n : 0;
313 #define BUFSTART (1024-32)
314 #define BUFMAX (32*1024-32)
316 #if defined(LDAP_R_COMPILE)
317 static char *safe_realloc( char **buf, int len );
319 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
320 static int copy_hostent( struct hostent *res,
321 char **buf, struct hostent * src );
325 int ldap_pvt_gethostbyname_a(
327 struct hostent *resbuf,
329 struct hostent **result,
332 #if defined( HAVE_GETHOSTBYNAME_R )
334 # define NEED_SAFE_REALLOC 1
338 for(;buflen<BUFMAX;) {
339 if (safe_realloc( buf, buflen )==NULL)
342 #if (GETHOSTBYNAME_R_NARGS < 6)
343 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
344 r = (*result == NULL) ? -1 : 0;
346 r = gethostbyname_r( name, resbuf, *buf,
347 buflen, result, herrno_ptr );
350 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
353 #ifdef NETDB_INTERNAL
355 (*herrno_ptr==NETDB_INTERNAL) &&
365 #elif defined( LDAP_R_COMPILE )
366 # define NEED_COPY_HOSTENT
371 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
373 he = gethostbyname( name );
376 *herrno_ptr = h_errno;
378 } else if (copy_hostent( resbuf, buf, he )<0) {
386 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
391 *result = gethostbyname( name );
397 *herrno_ptr = h_errno;
403 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
405 hp_strerror( int err )
408 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
409 case TRY_AGAIN: return _("Host not found (server fail?)");
410 case NO_RECOVERY: return _("Non-recoverable failure");
411 case NO_DATA: return _("No data of requested type");
412 #ifdef NETDB_INTERNAL
413 case NETDB_INTERNAL: return STRERROR( errno );
416 return _("Unknown resolver error");
420 int ldap_pvt_get_hname(
421 const struct sockaddr *sa,
428 #if defined( HAVE_GETNAMEINFO )
430 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
431 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
432 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
433 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
436 #else /* !HAVE_GETNAMEINFO */
439 struct hostent *hp = NULL;
440 #ifdef HAVE_GETHOSTBYADDR_R
442 int buflen=BUFSTART, h_errno;
447 if (sa->sa_family == AF_INET6) {
448 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
449 addr = (char *)&sin->sin6_addr;
450 alen = sizeof(sin->sin6_addr);
453 if (sa->sa_family == AF_INET) {
454 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
455 addr = (char *)&sin->sin_addr;
456 alen = sizeof(sin->sin_addr);
459 *err = (char *)HSTRERROR( rc );
462 #if defined( HAVE_GETHOSTBYADDR_R )
463 for(;buflen<BUFMAX;) {
464 if (safe_realloc( &buf, buflen )==NULL) {
465 *err = (char *)STRERROR( ENOMEM );
468 #if (GETHOSTBYADDR_R_NARGS < 8)
469 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
470 &hb, buf, buflen, &h_errno );
471 rc = (hp == NULL) ? -1 : 0;
473 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
477 #ifdef NETDB_INTERNAL
479 (h_errno==NETDB_INTERNAL) &&
489 strncpy( name, hp->h_name, namelen );
491 *err = (char *)HSTRERROR( h_errno );
494 #else /* HAVE_GETHOSTBYADDR_R */
496 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
497 hp = gethostbyaddr( addr, alen, sa->sa_family );
499 strncpy( name, hp->h_name, namelen );
503 *err = (char *)HSTRERROR( h_errno );
505 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
507 #endif /* !HAVE_GETHOSTBYADDR_R */
509 #endif /* !HAVE_GETNAMEINFO */
512 int ldap_pvt_gethostbyaddr_a(
516 struct hostent *resbuf,
518 struct hostent **result,
521 #if defined( HAVE_GETHOSTBYADDR_R )
523 # undef NEED_SAFE_REALLOC
524 # define NEED_SAFE_REALLOC
528 for(;buflen<BUFMAX;) {
529 if (safe_realloc( buf, buflen )==NULL)
531 #if (GETHOSTBYADDR_R_NARGS < 8)
532 *result=gethostbyaddr_r( addr, len, type,
533 resbuf, *buf, buflen, herrno_ptr );
534 r = (*result == NULL) ? -1 : 0;
536 r = gethostbyaddr_r( addr, len, type,
537 resbuf, *buf, buflen,
538 result, herrno_ptr );
541 #ifdef NETDB_INTERNAL
543 (*herrno_ptr==NETDB_INTERNAL) &&
553 #elif defined( LDAP_R_COMPILE )
554 # undef NEED_COPY_HOSTENT
555 # define NEED_COPY_HOSTENT
560 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
561 he = gethostbyaddr( addr, len, type );
564 *herrno_ptr = h_errno;
566 } else if (copy_hostent( resbuf, buf, he )<0) {
573 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
577 #else /* gethostbyaddr() */
579 *result = gethostbyaddr( addr, len, type );
588 * ldap_int_utils_init() should be called before any other function.
591 void ldap_int_utils_init( void )
598 #ifdef LDAP_R_COMPILE
599 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
600 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
602 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
603 ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
605 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
607 ldap_pvt_thread_mutex_init( &ldap_int_hostname_mutex );
609 #ifdef HAVE_CYRUS_SASL
610 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
613 ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
617 /* call other module init functions here... */
620 #if defined( NEED_COPY_HOSTENT )
621 # undef NEED_SAFE_REALLOC
622 #define NEED_SAFE_REALLOC
624 static char *cpy_aliases(
631 for( ; (*src) ; src++ ) {
632 len = strlen( *src ) + 1;
633 AC_MEMCPY( buf, *src, len );
641 static char *cpy_addresses(
648 for( ; (*src) ; src++ ) {
649 AC_MEMCPY( buf, *src, len );
657 static int copy_hostent(
660 struct hostent * src )
667 int total_alias_len=0;
669 int total_addr_len=0;
672 /* calculate the size needed for the buffer */
673 name_len = strlen( src->h_name ) + 1;
675 if( src->h_aliases != NULL ) {
676 for( p = src->h_aliases; (*p) != NULL; p++ ) {
677 total_alias_len += strlen( *p ) + 1;
682 if( src->h_addr_list != NULL ) {
683 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
686 total_addr_len = n_addr * src->h_length;
689 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
690 total_addr_len + total_alias_len + name_len;
692 if (safe_realloc( buf, total_len )) {
694 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
695 AC_MEMCPY( res, src, sizeof( struct hostent ) );
696 /* first the name... */
697 AC_MEMCPY( tbuf, src->h_name, name_len );
698 res->h_name = tbuf; tbuf+=name_len;
699 /* now the aliases */
701 if ( src->h_aliases != NULL ) {
702 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
705 /* finally the addresses */
706 res->h_addr_list = tp;
707 if ( src->h_addr_list != NULL ) {
708 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
717 #if defined( NEED_SAFE_REALLOC )
718 static char *safe_realloc( char **buf, int len )
721 tmpbuf = LDAP_REALLOC( *buf, len );
729 char * ldap_pvt_get_fqdn( char *name )
732 char hostbuf[MAXHOSTNAMELEN+1];
733 struct hostent *hp, he_buf;
734 int rc, local_h_errno;
737 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
738 hostbuf[MAXHOSTNAMELEN] = '\0';
745 rc = ldap_pvt_gethostbyname_a( name,
746 &he_buf, &ha_buf, &hp, &local_h_errno );
748 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
749 fqdn = LDAP_STRDUP( name );
751 fqdn = LDAP_STRDUP( hp->h_name );
758 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
759 && !defined( HAVE_GAI_STRERROR )
760 char *ldap_pvt_gai_strerror (int code) {
765 #ifdef EAI_ADDRFAMILY
766 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
768 { EAI_AGAIN, N_("Temporary failure in name resolution") },
769 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
770 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
771 { EAI_FAMILY, N_("ai_family not supported") },
772 { EAI_MEMORY, N_("Memory allocation failure") },
774 { EAI_NODATA, N_("No address associated with hostname") },
776 { EAI_NONAME, N_("Name or service not known") },
777 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
778 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
779 { EAI_SYSTEM, N_("System error") },
785 for ( i = 0; values[i].msg != NULL; i++ ) {
786 if ( values[i].code == code ) {
787 return (char *) _(values[i].msg);
791 return _("Unknown error");