No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / libldap / util-int.c
blob546026b80c2c9dc2c34edd2e2e58f7cafb6f9e8f
1 /* $OpenLDAP: pkg/ldap/libraries/libldap/util-int.c,v 1.57.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
5 * Portions Copyright 1998 A. Hartgers.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
16 /* ACKNOWLEDGEMENTS:
17 * This work was initially developed by Bart Hartgers for inclusion in
18 * OpenLDAP Software.
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.
27 #include "portable.h"
29 #include <ac/stdlib.h>
31 #include <ac/errno.h>
32 #include <ac/socket.h>
33 #include <ac/string.h>
34 #include <ac/time.h>
35 #include <ac/unistd.h>
37 #include "ldap-int.h"
39 #ifndef h_errno
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).
43 extern int h_errno;
44 #endif
46 #ifdef HAVE_HSTRERROR
47 # define HSTRERROR(e) hstrerror(e)
48 #else
49 # define HSTRERROR(e) hp_strerror(e)
50 #endif
52 #ifndef LDAP_R_COMPILE
53 # undef HAVE_REENTRANT_FUNCTIONS
54 # undef HAVE_CTIME_R
55 # undef HAVE_GETHOSTBYNAME_R
56 # undef HAVE_GETHOSTBYADDR_R
58 #else
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 )
64 # define USE_CTIME_R
65 # else
66 static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
67 # endif
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
73 # endif
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
78 # endif
79 #endif /* LDAP_R_COMPILE */
81 char *ldap_pvt_ctime( const time_t *tp, char *buf )
83 #ifdef USE_CTIME_R
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);
90 # else
91 return ctime_r(tp,buf);
92 # endif
94 #else
96 # ifdef LDAP_R_COMPILE
97 ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
98 # endif
100 AC_MEMCPY( buf, ctime(tp), 26 );
102 # ifdef LDAP_R_COMPILE
103 ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
104 # endif
106 return buf;
107 #endif
110 #define BUFSTART (1024-32)
111 #define BUFMAX (32*1024-32)
113 #if defined(LDAP_R_COMPILE)
114 static char *safe_realloc( char **buf, int len );
116 #if !(defined(HAVE_GETHOSTBYNAME_R) && defined(HAVE_GETHOSTBYADDR_R))
117 static int copy_hostent( struct hostent *res,
118 char **buf, struct hostent * src );
119 #endif
120 #endif
122 int ldap_pvt_gethostbyname_a(
123 const char *name,
124 struct hostent *resbuf,
125 char **buf,
126 struct hostent **result,
127 int *herrno_ptr )
129 #if defined( HAVE_GETHOSTBYNAME_R )
131 # define NEED_SAFE_REALLOC 1
132 int r=-1;
133 int buflen=BUFSTART;
134 *buf = NULL;
135 for(;buflen<BUFMAX;) {
136 if (safe_realloc( buf, buflen )==NULL)
137 return r;
139 #if (GETHOSTBYNAME_R_NARGS < 6)
140 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
141 r = (*result == NULL) ? -1 : 0;
142 #else
143 r = gethostbyname_r( name, resbuf, *buf,
144 buflen, result, herrno_ptr );
145 #endif
147 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
148 name, r, 0 );
150 #ifdef NETDB_INTERNAL
151 if ((r<0) &&
152 (*herrno_ptr==NETDB_INTERNAL) &&
153 (errno==ERANGE))
155 buflen*=2;
156 continue;
158 #endif
159 return r;
161 return -1;
162 #elif defined( LDAP_R_COMPILE )
163 # define NEED_COPY_HOSTENT
164 struct hostent *he;
165 int retval;
166 *buf = NULL;
168 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
170 he = gethostbyname( name );
172 if (he==NULL) {
173 *herrno_ptr = h_errno;
174 retval = -1;
175 } else if (copy_hostent( resbuf, buf, he )<0) {
176 *herrno_ptr = -1;
177 retval = -1;
178 } else {
179 *result = resbuf;
180 retval = 0;
183 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
185 return retval;
186 #else
187 *buf = NULL;
188 *result = gethostbyname( name );
190 if (*result!=NULL) {
191 return 0;
194 *herrno_ptr = h_errno;
196 return -1;
197 #endif
200 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
201 static const char *
202 hp_strerror( int err )
204 switch (err) {
205 case HOST_NOT_FOUND: return _("Host not found (authoritative)");
206 case TRY_AGAIN: return _("Host not found (server fail?)");
207 case NO_RECOVERY: return _("Non-recoverable failure");
208 case NO_DATA: return _("No data of requested type");
209 #ifdef NETDB_INTERNAL
210 case NETDB_INTERNAL: return STRERROR( errno );
211 #endif
213 return _("Unknown resolver error");
215 #endif
217 int ldap_pvt_get_hname(
218 const struct sockaddr *sa,
219 int len,
220 char *name,
221 int namelen,
222 char **err )
224 int rc;
225 #if defined( HAVE_GETNAMEINFO )
227 #if defined( LDAP_R_COMPILE )
228 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
229 #endif
230 rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
231 #if defined( LDAP_R_COMPILE )
232 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
233 #endif
234 if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
235 return rc;
237 #else /* !HAVE_GETNAMEINFO */
238 char *addr;
239 int alen;
240 struct hostent *hp = NULL;
241 #ifdef HAVE_GETHOSTBYADDR_R
242 struct hostent hb;
243 int buflen=BUFSTART, h_errno;
244 char *buf=NULL;
245 #endif
247 #ifdef LDAP_PF_INET6
248 if (sa->sa_family == AF_INET6) {
249 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
250 addr = (char *)&sin->sin6_addr;
251 alen = sizeof(sin->sin6_addr);
252 } else
253 #endif
254 if (sa->sa_family == AF_INET) {
255 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
256 addr = (char *)&sin->sin_addr;
257 alen = sizeof(sin->sin_addr);
258 } else {
259 rc = NO_RECOVERY;
260 *err = (char *)HSTRERROR( rc );
261 return rc;
263 #if defined( HAVE_GETHOSTBYADDR_R )
264 for(;buflen<BUFMAX;) {
265 if (safe_realloc( &buf, buflen )==NULL) {
266 *err = (char *)STRERROR( ENOMEM );
267 return ENOMEM;
269 #if (GETHOSTBYADDR_R_NARGS < 8)
270 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
271 &hb, buf, buflen, &h_errno );
272 rc = (hp == NULL) ? -1 : 0;
273 #else
274 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
275 &hb, buf, buflen,
276 &hp, &h_errno );
277 #endif
278 #ifdef NETDB_INTERNAL
279 if ((rc<0) &&
280 (h_errno==NETDB_INTERNAL) &&
281 (errno==ERANGE))
283 buflen*=2;
284 continue;
286 #endif
287 break;
289 if (hp) {
290 strncpy( name, hp->h_name, namelen );
291 } else {
292 *err = (char *)HSTRERROR( h_errno );
294 LDAP_FREE(buf);
295 #else /* HAVE_GETHOSTBYADDR_R */
297 #if defined( LDAP_R_COMPILE )
298 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
299 #endif
300 hp = gethostbyaddr( addr, alen, sa->sa_family );
301 if (hp) {
302 strncpy( name, hp->h_name, namelen );
303 rc = 0;
304 } else {
305 rc = h_errno;
306 *err = (char *)HSTRERROR( h_errno );
308 #if defined( LDAP_R_COMPILE )
309 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
310 #endif
312 #endif /* !HAVE_GETHOSTBYADDR_R */
313 return rc;
314 #endif /* !HAVE_GETNAMEINFO */
317 int ldap_pvt_gethostbyaddr_a(
318 const char *addr,
319 int len,
320 int type,
321 struct hostent *resbuf,
322 char **buf,
323 struct hostent **result,
324 int *herrno_ptr )
326 #if defined( HAVE_GETHOSTBYADDR_R )
328 # undef NEED_SAFE_REALLOC
329 # define NEED_SAFE_REALLOC
330 int r=-1;
331 int buflen=BUFSTART;
332 *buf = NULL;
333 for(;buflen<BUFMAX;) {
334 if (safe_realloc( buf, buflen )==NULL)
335 return r;
336 #if (GETHOSTBYADDR_R_NARGS < 8)
337 *result=gethostbyaddr_r( addr, len, type,
338 resbuf, *buf, buflen, herrno_ptr );
339 r = (*result == NULL) ? -1 : 0;
340 #else
341 r = gethostbyaddr_r( addr, len, type,
342 resbuf, *buf, buflen,
343 result, herrno_ptr );
344 #endif
346 #ifdef NETDB_INTERNAL
347 if ((r<0) &&
348 (*herrno_ptr==NETDB_INTERNAL) &&
349 (errno==ERANGE))
351 buflen*=2;
352 continue;
354 #endif
355 return r;
357 return -1;
358 #elif defined( LDAP_R_COMPILE )
359 # undef NEED_COPY_HOSTENT
360 # define NEED_COPY_HOSTENT
361 struct hostent *he;
362 int retval;
363 *buf = NULL;
365 ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
367 he = gethostbyaddr( addr, len, type );
369 if (he==NULL) {
370 *herrno_ptr = h_errno;
371 retval = -1;
372 } else if (copy_hostent( resbuf, buf, he )<0) {
373 *herrno_ptr = -1;
374 retval = -1;
375 } else {
376 *result = resbuf;
377 retval = 0;
380 ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
382 return retval;
384 #else /* gethostbyaddr() */
385 *buf = NULL;
386 *result = gethostbyaddr( addr, len, type );
388 if (*result!=NULL) {
389 return 0;
391 return -1;
392 #endif
395 * ldap_int_utils_init() should be called before any other function.
398 void ldap_int_utils_init( void )
400 static int done=0;
401 if (done)
402 return;
403 done=1;
405 #ifdef LDAP_R_COMPILE
406 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
407 ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
408 #endif
409 ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
411 #ifdef HAVE_CYRUS_SASL
412 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
413 #endif
414 #endif
416 /* call other module init functions here... */
419 #if defined( NEED_COPY_HOSTENT )
420 # undef NEED_SAFE_REALLOC
421 #define NEED_SAFE_REALLOC
423 static char *cpy_aliases(
424 char ***tgtio,
425 char *buf,
426 char **src )
428 int len;
429 char **tgt=*tgtio;
430 for( ; (*src) ; src++ ) {
431 len = strlen( *src ) + 1;
432 AC_MEMCPY( buf, *src, len );
433 *tgt++=buf;
434 buf+=len;
436 *tgtio=tgt;
437 return buf;
440 static char *cpy_addresses(
441 char ***tgtio,
442 char *buf,
443 char **src,
444 int len )
446 char **tgt=*tgtio;
447 for( ; (*src) ; src++ ) {
448 AC_MEMCPY( buf, *src, len );
449 *tgt++=buf;
450 buf+=len;
452 *tgtio=tgt;
453 return buf;
456 static int copy_hostent(
457 struct hostent *res,
458 char **buf,
459 struct hostent * src )
461 char **p;
462 char **tp;
463 char *tbuf;
464 int name_len;
465 int n_alias=0;
466 int total_alias_len=0;
467 int n_addr=0;
468 int total_addr_len=0;
469 int total_len;
471 /* calculate the size needed for the buffer */
472 name_len = strlen( src->h_name ) + 1;
474 if( src->h_aliases != NULL ) {
475 for( p = src->h_aliases; (*p) != NULL; p++ ) {
476 total_alias_len += strlen( *p ) + 1;
477 n_alias++;
481 if( src->h_addr_list != NULL ) {
482 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
483 n_addr++;
485 total_addr_len = n_addr * src->h_length;
488 total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
489 total_addr_len + total_alias_len + name_len;
491 if (safe_realloc( buf, total_len )) {
492 tp = (char **) *buf;
493 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
494 AC_MEMCPY( res, src, sizeof( struct hostent ) );
495 /* first the name... */
496 AC_MEMCPY( tbuf, src->h_name, name_len );
497 res->h_name = tbuf; tbuf+=name_len;
498 /* now the aliases */
499 res->h_aliases = tp;
500 if ( src->h_aliases != NULL ) {
501 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
503 *tp++=NULL;
504 /* finally the addresses */
505 res->h_addr_list = tp;
506 if ( src->h_addr_list != NULL ) {
507 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
509 *tp++=NULL;
510 return 0;
512 return -1;
514 #endif
516 #if defined( NEED_SAFE_REALLOC )
517 static char *safe_realloc( char **buf, int len )
519 char *tmpbuf;
520 tmpbuf = LDAP_REALLOC( *buf, len );
521 if (tmpbuf) {
522 *buf=tmpbuf;
524 return tmpbuf;
526 #endif
528 char * ldap_pvt_get_fqdn( char *name )
530 char *fqdn, *ha_buf;
531 char hostbuf[MAXHOSTNAMELEN+1];
532 struct hostent *hp, he_buf;
533 int rc, local_h_errno;
535 if( name == NULL ) {
536 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
537 hostbuf[MAXHOSTNAMELEN] = '\0';
538 name = hostbuf;
539 } else {
540 name = "localhost";
544 rc = ldap_pvt_gethostbyname_a( name,
545 &he_buf, &ha_buf, &hp, &local_h_errno );
547 if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
548 fqdn = LDAP_STRDUP( name );
549 } else {
550 fqdn = LDAP_STRDUP( hp->h_name );
553 LDAP_FREE( ha_buf );
554 return fqdn;
557 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
558 && !defined( HAVE_GAI_STRERROR )
559 char *ldap_pvt_gai_strerror (int code) {
560 static struct {
561 int code;
562 const char *msg;
563 } values[] = {
564 #ifdef EAI_ADDRFAMILY
565 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
566 #endif
567 { EAI_AGAIN, N_("Temporary failure in name resolution") },
568 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
569 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
570 { EAI_FAMILY, N_("ai_family not supported") },
571 { EAI_MEMORY, N_("Memory allocation failure") },
572 #ifdef EAI_NODATA
573 { EAI_NODATA, N_("No address associated with hostname") },
574 #endif
575 { EAI_NONAME, N_("Name or service not known") },
576 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
577 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
578 { EAI_SYSTEM, N_("System error") },
579 { 0, NULL }
582 int i;
584 for ( i = 0; values[i].msg != NULL; i++ ) {
585 if ( values[i].code == code ) {
586 return (char *) _(values[i].msg);
590 return _("Unknown error");
592 #endif