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;
62 static ldap_pvt_thread_mutex_t ldap_int_gettime_mutex;
64 # if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
65 && defined( CTIME_R_NARGS )
68 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
71 /* USE_GMTIME_R and USE_LOCALTIME_R defined in ldap_pvt.h */
74 /* to be released with 2.5 */
75 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
76 /* we use the same mutex for gmtime(3) and localtime(3)
77 * because implementations may use the same buffer
78 * for both functions */
79 static ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
81 #else /* ! LDAP_DEVEL */
82 ldap_pvt_thread_mutex_t ldap_int_gmtime_mutex;
83 #endif /* ! LDAP_DEVEL */
85 # if defined(HAVE_GETHOSTBYNAME_R) && \
86 (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
87 /* Don't know how to handle this version, pretend it's not there */
88 # undef HAVE_GETHOSTBYNAME_R
90 # if defined(HAVE_GETHOSTBYADDR_R) && \
91 (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
92 /* Don't know how to handle this version, pretend it's not there */
93 # undef HAVE_GETHOSTBYADDR_R
95 #endif /* LDAP_R_COMPILE */
97 char *ldap_pvt_ctime( const time_t *tp, char *buf )
100 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
101 # error "CTIME_R_NARGS should be 2 or 3"
102 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
103 return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
104 # elif CTIME_R_NARGS > 2
105 return ctime_r(tp,buf,26);
107 return ctime_r(tp,buf);
112 LDAP_MUTEX_LOCK( &ldap_int_ctime_mutex );
113 AC_MEMCPY( buf, ctime(tp), 26 );
114 LDAP_MUTEX_UNLOCK( &ldap_int_ctime_mutex );
120 #if !defined( USE_GMTIME_R ) || !defined( USE_LOCALTIME_R )
122 ldap_pvt_gmtime_lock( void )
124 # ifndef LDAP_R_COMPILE
126 # else /* LDAP_R_COMPILE */
127 return ldap_pvt_thread_mutex_lock( &ldap_int_gmtime_mutex );
128 # endif /* LDAP_R_COMPILE */
132 ldap_pvt_gmtime_unlock( void )
134 # ifndef LDAP_R_COMPILE
136 # else /* LDAP_R_COMPILE */
137 return ldap_pvt_thread_mutex_unlock( &ldap_int_gmtime_mutex );
138 # endif /* LDAP_R_COMPILE */
140 #endif /* !USE_GMTIME_R || !USE_LOCALTIME_R */
144 ldap_pvt_gmtime( const time_t *timep, struct tm *result )
148 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
149 tm_ptr = gmtime( timep );
150 if ( tm_ptr == NULL ) {
156 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
160 #endif /* !USE_GMTIME_R */
162 #ifndef USE_LOCALTIME_R
164 ldap_pvt_localtime( const time_t *timep, struct tm *result )
168 LDAP_MUTEX_LOCK( &ldap_int_gmtime_mutex );
169 tm_ptr = localtime( timep );
170 if ( tm_ptr == NULL ) {
176 LDAP_MUTEX_UNLOCK( &ldap_int_gmtime_mutex );
180 #endif /* !USE_LOCALTIME_R */
182 /* return a broken out time, with microseconds
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 LDAP_MUTEX_LOCK( &ldap_int_gettime_mutex );
206 if ( count.QuadPart <= prevCount.QuadPart ) {
212 LDAP_MUTEX_UNLOCK( &ldap_int_gettime_mutex );
214 /* We assume Windows has at least a vague idea of
215 * when a second begins. So we align our microsecond count
216 * with the Windows millisecond count using this offset.
217 * We retain the submillisecond portion of our own count.
219 * Note - this also assumes that the relationship between
220 * the PerformanceCouunter and SystemTime stays constant;
221 * that assumption breaks if the SystemTime is adjusted by
222 * an external action.
224 if ( !cFreq.QuadPart ) {
227 QueryPerformanceFrequency( &cFreq );
229 /* just get sub-second portion of counter */
230 t = count.QuadPart % cFreq.QuadPart;
232 /* convert to microseconds */
234 usec = t / cFreq.QuadPart;
236 offset = usec - st.wMilliseconds * 1000;
241 /* convert to microseconds */
242 count.QuadPart %= cFreq.QuadPart;
243 count.QuadPart *= 1000000;
244 count.QuadPart /= cFreq.QuadPart;
245 count.QuadPart -= offset;
247 tm->tm_usec = count.QuadPart % 1000000;
248 if ( tm->tm_usec < 0 )
249 tm->tm_usec += 1000000;
251 /* any difference larger than microseconds is
252 * already reflected in st
255 tm->tm_sec = st.wSecond;
256 tm->tm_min = st.wMinute;
257 tm->tm_hour = st.wHour;
258 tm->tm_mday = st.wDay;
259 tm->tm_mon = st.wMonth - 1;
260 tm->tm_year = st.wYear - 1900;
264 ldap_pvt_gettime( struct lutil_tm *ltm )
267 static struct timeval prevTv;
273 gettimeofday( &tv, NULL );
276 LDAP_MUTEX_LOCK( &ldap_int_gettime_mutex );
277 if ( tv.tv_sec < prevTv.tv_sec
278 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
284 LDAP_MUTEX_UNLOCK( &ldap_int_gettime_mutex );
288 ldap_pvt_gmtime( &t, &tm );
290 ltm->tm_sec = tm.tm_sec;
291 ltm->tm_min = tm.tm_min;
292 ltm->tm_hour = tm.tm_hour;
293 ltm->tm_mday = tm.tm_mday;
294 ltm->tm_mon = tm.tm_mon;
295 ltm->tm_year = tm.tm_year;
296 ltm->tm_usec = tv.tv_usec;
301 ldap_pvt_csnstr(char *buf, size_t len, unsigned int replica, unsigned int mod)
306 ldap_pvt_gettime( &tm );
308 n = snprintf( buf, len,
309 "%4d%02d%02d%02d%02d%02d.%06dZ#%06x#%03x#%06x",
310 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
311 tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_usub, replica, mod );
313 if( n < 0 ) return 0;
314 return ( (size_t) n < len ) ? n : 0;
317 #define BUFSTART (1024-32)
318 #define BUFMAX (32*1024-32)
320 #if defined(LDAP_R_COMPILE)
321 static char *safe_realloc( char **buf, int len );
323 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
324 static int copy_hostent( struct hostent *res,
325 char **buf, struct hostent * src );
329 int ldap_pvt_gethostbyname_a(
331 struct hostent *resbuf,
333 struct hostent **result,
336 #if defined( HAVE_GETHOSTBYNAME_R )
338 # define NEED_SAFE_REALLOC 1
342 for(;buflen<BUFMAX;) {
343 if (safe_realloc( buf, buflen )==NULL)
346 #if (GETHOSTBYNAME_R_NARGS < 6)
347 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
348 r = (*result == NULL) ? -1 : 0;
350 r = gethostbyname_r( name, resbuf, *buf,
351 buflen, result, herrno_ptr );
354 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
357 #ifdef NETDB_INTERNAL
359 (*herrno_ptr==NETDB_INTERNAL) &&
369 #elif defined( LDAP_R_COMPILE )
370 # define NEED_COPY_HOSTENT
375 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
377 he = gethostbyname( name );
380 *herrno_ptr = h_errno;
382 } else if (copy_hostent( resbuf, buf, he )<0) {
390 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
395 *result = gethostbyname( name );
401 *herrno_ptr = h_errno;
407 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
409 hp_strerror( int err )
412 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
413 case TRY_AGAIN: return _("Host not found (server fail?)");
414 case NO_RECOVERY: return _("Non-recoverable failure");
415 case NO_DATA: return _("No data of requested type");
416 #ifdef NETDB_INTERNAL
417 case NETDB_INTERNAL: return STRERROR( errno );
420 return _("Unknown resolver error");
424 int ldap_pvt_get_hname(
425 const struct sockaddr *sa,
432 #if defined( HAVE_GETNAMEINFO )
434 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
435 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
436 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
437 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
440 #else /* !HAVE_GETNAMEINFO */
443 struct hostent *hp = NULL;
444 #ifdef HAVE_GETHOSTBYADDR_R
446 int buflen=BUFSTART, h_errno;
451 if (sa->sa_family == AF_INET6) {
452 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
453 addr = (char *)&sin->sin6_addr;
454 alen = sizeof(sin->sin6_addr);
457 if (sa->sa_family == AF_INET) {
458 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
459 addr = (char *)&sin->sin_addr;
460 alen = sizeof(sin->sin_addr);
463 *err = (char *)HSTRERROR( rc );
466 #if defined( HAVE_GETHOSTBYADDR_R )
467 for(;buflen<BUFMAX;) {
468 if (safe_realloc( &buf, buflen )==NULL) {
469 *err = (char *)STRERROR( ENOMEM );
472 #if (GETHOSTBYADDR_R_NARGS < 8)
473 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
474 &hb, buf, buflen, &h_errno );
475 rc = (hp == NULL) ? -1 : 0;
477 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
481 #ifdef NETDB_INTERNAL
483 (h_errno==NETDB_INTERNAL) &&
493 strncpy( name, hp->h_name, namelen );
495 *err = (char *)HSTRERROR( h_errno );
498 #else /* HAVE_GETHOSTBYADDR_R */
500 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
501 hp = gethostbyaddr( addr, alen, sa->sa_family );
503 strncpy( name, hp->h_name, namelen );
507 *err = (char *)HSTRERROR( h_errno );
509 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
511 #endif /* !HAVE_GETHOSTBYADDR_R */
513 #endif /* !HAVE_GETNAMEINFO */
516 int ldap_pvt_gethostbyaddr_a(
520 struct hostent *resbuf,
522 struct hostent **result,
525 #if defined( HAVE_GETHOSTBYADDR_R )
527 # undef NEED_SAFE_REALLOC
528 # define NEED_SAFE_REALLOC
532 for(;buflen<BUFMAX;) {
533 if (safe_realloc( buf, buflen )==NULL)
535 #if (GETHOSTBYADDR_R_NARGS < 8)
536 *result=gethostbyaddr_r( addr, len, type,
537 resbuf, *buf, buflen, herrno_ptr );
538 r = (*result == NULL) ? -1 : 0;
540 r = gethostbyaddr_r( addr, len, type,
541 resbuf, *buf, buflen,
542 result, herrno_ptr );
545 #ifdef NETDB_INTERNAL
547 (*herrno_ptr==NETDB_INTERNAL) &&
557 #elif defined( LDAP_R_COMPILE )
558 # undef NEED_COPY_HOSTENT
559 # define NEED_COPY_HOSTENT
564 LDAP_MUTEX_LOCK( &ldap_int_resolv_mutex );
565 he = gethostbyaddr( addr, len, type );
568 *herrno_ptr = h_errno;
570 } else if (copy_hostent( resbuf, buf, he )<0) {
577 LDAP_MUTEX_UNLOCK( &ldap_int_resolv_mutex );
581 #else /* gethostbyaddr() */
583 *result = gethostbyaddr( addr, len, type );
592 * ldap_int_utils_init() should be called before any other function.
595 void ldap_int_utils_init( void )
602 #ifdef LDAP_R_COMPILE
603 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
604 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
606 #if !defined( USE_GMTIME_R ) && !defined( USE_LOCALTIME_R )
607 ldap_pvt_thread_mutex_init( &ldap_int_gmtime_mutex );
609 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
611 ldap_pvt_thread_mutex_init( &ldap_int_hostname_mutex );
613 ldap_pvt_thread_mutex_init( &ldap_int_gettime_mutex );
615 #ifdef HAVE_CYRUS_SASL
616 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
619 ldap_pvt_thread_mutex_init( &ldap_int_gssapi_mutex );
623 /* call other module init functions here... */
626 #if defined( NEED_COPY_HOSTENT )
627 # undef NEED_SAFE_REALLOC
628 #define NEED_SAFE_REALLOC
630 static char *cpy_aliases(
637 for( ; (*src) ; src++ ) {
638 len = strlen( *src ) + 1;
639 AC_MEMCPY( buf, *src, len );
647 static char *cpy_addresses(
654 for( ; (*src) ; src++ ) {
655 AC_MEMCPY( buf, *src, len );
663 static int copy_hostent(
666 struct hostent * src )
673 int total_alias_len=0;
675 int total_addr_len=0;
678 /* calculate the size needed for the buffer */
679 name_len = strlen( src->h_name ) + 1;
681 if( src->h_aliases != NULL ) {
682 for( p = src->h_aliases; (*p) != NULL; p++ ) {
683 total_alias_len += strlen( *p ) + 1;
688 if( src->h_addr_list != NULL ) {
689 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
692 total_addr_len = n_addr * src->h_length;
695 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
696 total_addr_len + total_alias_len + name_len;
698 if (safe_realloc( buf, total_len )) {
700 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
701 AC_MEMCPY( res, src, sizeof( struct hostent ) );
702 /* first the name... */
703 AC_MEMCPY( tbuf, src->h_name, name_len );
704 res->h_name = tbuf; tbuf+=name_len;
705 /* now the aliases */
707 if ( src->h_aliases != NULL ) {
708 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
711 /* finally the addresses */
712 res->h_addr_list = tp;
713 if ( src->h_addr_list != NULL ) {
714 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
723 #if defined( NEED_SAFE_REALLOC )
724 static char *safe_realloc( char **buf, int len )
727 tmpbuf = LDAP_REALLOC( *buf, len );
735 char * ldap_pvt_get_fqdn( char *name )
738 char hostbuf[MAXHOSTNAMELEN+1];
739 struct hostent *hp, he_buf;
740 int rc, local_h_errno;
743 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
744 hostbuf[MAXHOSTNAMELEN] = '\0';
751 rc = ldap_pvt_gethostbyname_a( name,
752 &he_buf, &ha_buf, &hp, &local_h_errno );
754 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
755 fqdn = LDAP_STRDUP( name );
757 fqdn = LDAP_STRDUP( hp->h_name );
764 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
765 && !defined( HAVE_GAI_STRERROR )
766 char *ldap_pvt_gai_strerror (int code) {
771 #ifdef EAI_ADDRFAMILY
772 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
774 { EAI_AGAIN, N_("Temporary failure in name resolution") },
775 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
776 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
777 { EAI_FAMILY, N_("ai_family not supported") },
778 { EAI_MEMORY, N_("Memory allocation failure") },
780 { EAI_NODATA, N_("No address associated with hostname") },
782 { EAI_NONAME, N_("Name or service not known") },
783 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
784 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
786 { EAI_SYSTEM, N_("System error") },
793 for ( i = 0; values[i].msg != NULL; i++ ) {
794 if ( values[i].code == code ) {
795 return (char *) _(values[i].msg);
799 return _("Unknown error");