xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/util-int.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
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/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * Portions Copyright 1998 A. Hartgers.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Bart Hartgers for inclusion in
18  * OpenLDAP Software.
19  */
20 
21 /*
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.
25  */
26 
27 #include "portable.h"
28 
29 #include <ac/stdlib.h>
30 
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>
36 
37 #include "ldap-int.h"
38 
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).
42  */
43 extern int h_errno;
44 #endif
45 
46 #ifdef HAVE_HSTRERROR
47 # define HSTRERROR(e)	hstrerror(e)
48 #else
49 # define HSTRERROR(e)	hp_strerror(e)
50 #endif
51 
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
57 
58 #else
59 # include <ldap_pvt_thread.h>
60   ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
61 
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
68 
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 */
80 
81 char *ldap_pvt_ctime( const time_t *tp, char *buf )
82 {
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
93 
94 #else
95 
96 # ifdef LDAP_R_COMPILE
97 	ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
98 # endif
99 
100 	AC_MEMCPY( buf, ctime(tp), 26 );
101 
102 # ifdef LDAP_R_COMPILE
103 	ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
104 # endif
105 
106 	return buf;
107 #endif
108 }
109 
110 #define BUFSTART (1024-32)
111 #define BUFMAX (32*1024-32)
112 
113 #if defined(LDAP_R_COMPILE)
114 static char *safe_realloc( char **buf, int len );
115 
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
121 
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 )
128 {
129 #if defined( HAVE_GETHOSTBYNAME_R )
130 
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;
138 
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
146 
147 		Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
148 		       name, r, 0 );
149 
150 #ifdef NETDB_INTERNAL
151 		if ((r<0) &&
152 			(*herrno_ptr==NETDB_INTERNAL) &&
153 			(errno==ERANGE))
154 		{
155 			buflen*=2;
156 			continue;
157 	 	}
158 #endif
159 		return r;
160 	}
161 	return -1;
162 #elif defined( LDAP_R_COMPILE )
163 # define NEED_COPY_HOSTENT
164 	struct hostent *he;
165 	int	retval;
166 	*buf = NULL;
167 
168 	ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
169 
170 	he = gethostbyname( name );
171 
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;
181 	}
182 
183 	ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
184 
185 	return retval;
186 #else
187 	*buf = NULL;
188 	*result = gethostbyname( name );
189 
190 	if (*result!=NULL) {
191 		return 0;
192 	}
193 
194 	*herrno_ptr = h_errno;
195 
196 	return -1;
197 #endif
198 }
199 
200 #if !defined( HAVE_GETNAMEINFO ) && !defined( HAVE_HSTRERROR )
201 static const char *
202 hp_strerror( int err )
203 {
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
212 	}
213 	return _("Unknown resolver error");
214 }
215 #endif
216 
217 int ldap_pvt_get_hname(
218 	const struct sockaddr *sa,
219 	int len,
220 	char *name,
221 	int namelen,
222 	char **err )
223 {
224 	int rc;
225 #if defined( HAVE_GETNAMEINFO )
226 
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;
236 
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
246 
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;
262 	}
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;
268 		}
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))
282 		{
283 			buflen*=2;
284 			continue;
285 		}
286 #endif
287 		break;
288 	}
289 	if (hp) {
290 		strncpy( name, hp->h_name, namelen );
291 	} else {
292 		*err = (char *)HSTRERROR( h_errno );
293 	}
294 	LDAP_FREE(buf);
295 #else /* HAVE_GETHOSTBYADDR_R */
296 
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 );
307 	}
308 #if defined( LDAP_R_COMPILE )
309 	ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
310 #endif
311 
312 #endif	/* !HAVE_GETHOSTBYADDR_R */
313 	return rc;
314 #endif	/* !HAVE_GETNAMEINFO */
315 }
316 
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 )
325 {
326 #if defined( HAVE_GETHOSTBYADDR_R )
327 
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
345 
346 #ifdef NETDB_INTERNAL
347 		if ((r<0) &&
348 			(*herrno_ptr==NETDB_INTERNAL) &&
349 			(errno==ERANGE))
350 		{
351 			buflen*=2;
352 			continue;
353 		}
354 #endif
355 		return r;
356 	}
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;
364 
365 	ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
366 
367 	he = gethostbyaddr( addr, len, type );
368 
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;
378 	}
379 
380 	ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
381 
382 	return retval;
383 
384 #else /* gethostbyaddr() */
385 	*buf = NULL;
386 	*result = gethostbyaddr( addr, len, type );
387 
388 	if (*result!=NULL) {
389 		return 0;
390 	}
391 	return -1;
392 #endif
393 }
394 /*
395  * ldap_int_utils_init() should be called before any other function.
396  */
397 
398 void ldap_int_utils_init( void )
399 {
400 	static int done=0;
401 	if (done)
402 	  return;
403 	done=1;
404 
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 );
410 
411 #ifdef HAVE_CYRUS_SASL
412 	ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
413 #endif
414 #endif
415 
416 	/* call other module init functions here... */
417 }
418 
419 #if defined( NEED_COPY_HOSTENT )
420 # undef NEED_SAFE_REALLOC
421 #define NEED_SAFE_REALLOC
422 
423 static char *cpy_aliases(
424 	char ***tgtio,
425 	char *buf,
426 	char **src )
427 {
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;
435 	}
436 	*tgtio=tgt;
437 	return buf;
438 }
439 
440 static char *cpy_addresses(
441 	char ***tgtio,
442 	char *buf,
443 	char **src,
444 	int len )
445 {
446    	char **tgt=*tgtio;
447 	for( ; (*src) ; src++ ) {
448 		AC_MEMCPY( buf, *src, len );
449 		*tgt++=buf;
450 		buf+=len;
451 	}
452 	*tgtio=tgt;
453 	return buf;
454 }
455 
456 static int copy_hostent(
457 	struct hostent *res,
458 	char **buf,
459 	struct hostent * src )
460 {
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;
470 
471 	/* calculate the size needed for the buffer */
472 	name_len = strlen( src->h_name ) + 1;
473 
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++;
478 		}
479 	}
480 
481 	if( src->h_addr_list != NULL ) {
482 		for( p = src->h_addr_list; (*p) != NULL; p++ ) {
483 			n_addr++;
484 		}
485 		total_addr_len = n_addr * src->h_length;
486 	}
487 
488 	total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
489 		total_addr_len + total_alias_len + name_len;
490 
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 );
502 		}
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 );
508 		}
509 		*tp++=NULL;
510 		return 0;
511 	}
512 	return -1;
513 }
514 #endif
515 
516 #if defined( NEED_SAFE_REALLOC )
517 static char *safe_realloc( char **buf, int len )
518 {
519 	char *tmpbuf;
520 	tmpbuf = LDAP_REALLOC( *buf, len );
521 	if (tmpbuf) {
522 		*buf=tmpbuf;
523 	}
524 	return tmpbuf;
525 }
526 #endif
527 
528 char * ldap_pvt_get_fqdn( char *name )
529 {
530 	char *fqdn, *ha_buf;
531 	char hostbuf[MAXHOSTNAMELEN+1];
532 	struct hostent *hp, he_buf;
533 	int rc, local_h_errno;
534 
535 	if( name == NULL ) {
536 		if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
537 			hostbuf[MAXHOSTNAMELEN] = '\0';
538 			name = hostbuf;
539 		} else {
540 			name = "localhost";
541 		}
542 	}
543 
544 	rc = ldap_pvt_gethostbyname_a( name,
545 		&he_buf, &ha_buf, &hp, &local_h_errno );
546 
547 	if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
548 		fqdn = LDAP_STRDUP( name );
549 	} else {
550 		fqdn = LDAP_STRDUP( hp->h_name );
551 	}
552 
553 	LDAP_FREE( ha_buf );
554 	return fqdn;
555 }
556 
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 }
580 	};
581 
582 	int i;
583 
584 	for ( i = 0; values[i].msg != NULL; i++ ) {
585 		if ( values[i].code == code ) {
586 			return (char *) _(values[i].msg);
587 		}
588 	}
589 
590 	return _("Unknown error");
591 }
592 #endif
593