]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
Import ITS#3278 from HEAD
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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 #ifndef LDAP_R_COMPILE
47 # undef HAVE_REENTRANT_FUNCTIONS
48 # undef HAVE_CTIME_R
49 # undef HAVE_GETHOSTBYNAME_R
50 # undef HAVE_GETHOSTBYADDR_R
51
52 #else
53 # include <ldap_pvt_thread.h>
54   ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
55
56 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
57         && defined( CTIME_R_NARGS )
58 # define USE_CTIME_R
59 # else
60         static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
61 # endif
62
63 # if defined(HAVE_GETHOSTBYNAME_R) && \
64         (GETHOSTBYNAME_R_NARGS < 5) || (6 < GETHOSTBYNAME_R_NARGS)
65         /* Don't know how to handle this version, pretend it's not there */
66 #       undef HAVE_GETHOSTBYNAME_R
67 # endif
68 # if defined(HAVE_GETHOSTBYADDR_R) && \
69         (GETHOSTBYADDR_R_NARGS < 7) || (8 < GETHOSTBYADDR_R_NARGS)
70         /* Don't know how to handle this version, pretend it's not there */
71 #       undef HAVE_GETHOSTBYADDR_R
72 # endif
73 #endif /* LDAP_R_COMPILE */
74
75 char *ldap_pvt_ctime( const time_t *tp, char *buf )
76 {
77 #ifdef USE_CTIME_R
78 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
79 #       error "CTIME_R_NARGS should be 2 or 3"
80 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
81         return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
82 # elif CTIME_R_NARGS > 2
83         return ctime_r(tp,buf,26);
84 # else
85         return ctime_r(tp,buf);
86 # endif   
87
88 #else
89
90 # ifdef LDAP_R_COMPILE
91         ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
92 # endif
93
94         AC_MEMCPY( buf, ctime(tp), 26 );
95
96 # ifdef LDAP_R_COMPILE
97         ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
98 # endif
99
100         return buf;
101 #endif  
102 }
103
104 #define BUFSTART (1024-32)
105 #define BUFMAX (32*1024-32)
106
107 static char *safe_realloc( char **buf, int len );
108
109 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
110 static int copy_hostent( struct hostent *res,
111         char **buf, struct hostent * src );
112 #endif
113
114 int ldap_pvt_gethostbyname_a(
115         const char *name, 
116         struct hostent *resbuf,
117         char **buf,
118         struct hostent **result,
119         int *herrno_ptr )
120 {
121 #if defined( HAVE_GETHOSTBYNAME_R )
122
123 # define NEED_SAFE_REALLOC 1   
124         int r=-1;
125         int buflen=BUFSTART;
126         *buf = NULL;
127         for(;buflen<BUFMAX;) {
128                 if (safe_realloc( buf, buflen )==NULL)
129                         return r;
130
131 #if (GETHOSTBYNAME_R_NARGS < 6)
132                 *result=gethostbyname_r( name, resbuf, *buf, buflen, herrno_ptr );
133                 r = (*result == NULL) ?  -1 : 0;
134 #else
135                 r = gethostbyname_r( name, resbuf, *buf,
136                         buflen, result, herrno_ptr );
137 #endif
138
139                 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
140                        name, r, 0 );
141
142 #ifdef NETDB_INTERNAL
143                 if ((r<0) &&
144                         (*herrno_ptr==NETDB_INTERNAL) &&
145                         (errno==ERANGE))
146                 {
147                         buflen*=2;
148                         continue;
149                 }
150 #endif
151                 return r;
152         }
153         return -1;
154 #elif defined( LDAP_R_COMPILE )
155 # define NEED_COPY_HOSTENT   
156         struct hostent *he;
157         int     retval;
158         *buf = NULL;
159         
160         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
161         
162         he = gethostbyname( name );
163         
164         if (he==NULL) {
165                 *herrno_ptr = h_errno;
166                 retval = -1;
167         } else if (copy_hostent( resbuf, buf, he )<0) {
168                 *herrno_ptr = -1;
169                 retval = -1;
170         } else {
171                 *result = resbuf;
172                 retval = 0;
173         }
174         
175         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
176         
177         return retval;
178 #else   
179         *buf = NULL;
180         *result = gethostbyname( name );
181
182         if (*result!=NULL) {
183                 return 0;
184         }
185
186         *herrno_ptr = h_errno;
187         
188         return -1;
189 #endif  
190 }
191
192 #ifndef GETNAMEINFO
193 static const char *
194 hp_strerror( int err )
195 {
196         switch (err) {
197         case HOST_NOT_FOUND:    return _("Host not found (authoritative)");
198         case TRY_AGAIN:                 return _("Host not found (server fail?)");
199         case NO_RECOVERY:               return _("Non-recoverable failure");
200         case NO_DATA:                   return _("No data of requested type");
201 #ifdef NETDB_INTERNAL
202         case NETDB_INTERNAL:    return STRERROR( errno );
203 #endif
204         }
205         return _("Unknown resolver error");
206 }
207 #endif
208
209 int ldap_pvt_get_hname(
210         const struct sockaddr *sa,
211         int len,
212         char *name,
213         int namelen,
214         char **err )
215 {
216         int rc;
217 #if defined( HAVE_GETNAMEINFO )
218
219 #if defined( LDAP_R_COMPILE )
220         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
221 #endif
222         rc = getnameinfo( sa, len, name, namelen, NULL, 0, 0 );
223 #if defined( LDAP_R_COMPILE )
224         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
225 #endif
226         if ( rc ) *err = (char *)AC_GAI_STRERROR( rc );
227         return rc;
228
229 #else /* !HAVE_GETNAMEINFO */
230         char *addr;
231         int alen;
232         struct hostent *hp = NULL;
233 #ifdef HAVE_GETHOSTBYADDR_R
234         struct hostent hb;
235         int buflen=BUFSTART, h_errno;
236         char *buf=NULL;
237 #endif
238
239 #ifdef LDAP_PF_INET6
240         if (sa->sa_family == AF_INET6) {
241                 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
242                 addr = (char *)&sin->sin6_addr;
243                 alen = sizeof(sin->sin6_addr);
244         } else
245 #endif
246         if (sa->sa_family == AF_INET) {
247                 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
248                 addr = (char *)&sin->sin_addr;
249                 alen = sizeof(sin->sin_addr);
250         } else {
251                 rc = NO_RECOVERY;
252                 *err = (char *)hp_strerror( rc );
253                 return rc;
254         }
255 #if defined( HAVE_GETHOSTBYADDR_R )
256         for(;buflen<BUFMAX;) {
257                 if (safe_realloc( &buf, buflen )==NULL) {
258                         *err = (char *)STRERROR( ENOMEM );
259                         return ENOMEM;
260                 }
261 #if (GETHOSTBYADDR_R_NARGS < 8)
262                 hp=gethostbyaddr_r( addr, alen, sa->sa_family,
263                         &hb, buf, buflen, &h_errno );
264                 rc = (hp == NULL) ? -1 : 0;
265 #else
266                 rc = gethostbyaddr_r( addr, alen, sa->sa_family,
267                         &hb, buf, buflen, 
268                         &hp, &h_errno );
269 #endif
270 #ifdef NETDB_INTERNAL
271                 if ((rc<0) &&
272                         (h_errno==NETDB_INTERNAL) &&
273                         (errno==ERANGE))
274                 {
275                         buflen*=2;
276                         continue;
277                 }
278 #endif
279                 break;
280         }
281         if (hp) {
282                 strncpy( name, hp->h_name, namelen );
283         } else {
284                 *err = (char *)hp_strerror( h_errno );
285         }
286         LDAP_FREE(buf);
287 #else /* HAVE_GETHOSTBYADDR_R */
288
289 #if defined( LDAP_R_COMPILE )
290         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
291 #endif
292         hp = gethostbyaddr( addr, alen, sa->sa_family );
293         if (hp) {
294                 strncpy( name, hp->h_name, namelen );
295                 rc = 0;
296         } else {
297                 rc = h_errno;
298                 *err = (char *)hp_strerror( h_errno );
299         }
300 #if defined( LDAP_R_COMPILE )
301         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
302 #endif
303
304 #endif  /* !HAVE_GETHOSTBYADDR_R */
305         return rc;
306 #endif  /* !HAVE_GETNAMEINFO */
307 }
308
309 int ldap_pvt_gethostbyaddr_a(
310         const char *addr,
311         int len,
312         int type,
313         struct hostent *resbuf,
314         char **buf,
315         struct hostent **result,
316         int *herrno_ptr )
317 {
318 #if defined( HAVE_GETHOSTBYADDR_R )
319
320 # undef NEED_SAFE_REALLOC
321 # define NEED_SAFE_REALLOC   
322         int r=-1;
323         int buflen=BUFSTART;
324         *buf = NULL;   
325         for(;buflen<BUFMAX;) {
326                 if (safe_realloc( buf, buflen )==NULL)
327                         return r;
328 #if (GETHOSTBYADDR_R_NARGS < 8)
329                 *result=gethostbyaddr_r( addr, len, type,
330                         resbuf, *buf, buflen, herrno_ptr );
331                 r = (*result == NULL) ? -1 : 0;
332 #else
333                 r = gethostbyaddr_r( addr, len, type,
334                         resbuf, *buf, buflen, 
335                         result, herrno_ptr );
336 #endif
337
338 #ifdef NETDB_INTERNAL
339                 if ((r<0) &&
340                         (*herrno_ptr==NETDB_INTERNAL) &&
341                         (errno==ERANGE))
342                 {
343                         buflen*=2;
344                         continue;
345                 }
346 #endif
347                 return r;
348         }
349         return -1;
350 #elif defined( LDAP_R_COMPILE )
351 # undef NEED_COPY_HOSTENT
352 # define NEED_COPY_HOSTENT   
353         struct hostent *he;
354         int     retval;
355         *buf = NULL;   
356         
357         ldap_pvt_thread_mutex_lock( &ldap_int_resolv_mutex );
358         
359         he = gethostbyaddr( addr, len, type );
360         
361         if (he==NULL) {
362                 *herrno_ptr = h_errno;
363                 retval = -1;
364         } else if (copy_hostent( resbuf, buf, he )<0) {
365                 *herrno_ptr = -1;
366                 retval = -1;
367         } else {
368                 *result = resbuf;
369                 retval = 0;
370         }
371         
372         ldap_pvt_thread_mutex_unlock( &ldap_int_resolv_mutex );
373         
374         return retval;
375
376 #else /* gethostbyaddr() */
377         *buf = NULL;   
378         *result = gethostbyaddr( addr, len, type );
379
380         if (*result!=NULL) {
381                 return 0;
382         }
383         return -1;
384 #endif  
385 }
386 /* 
387  * ldap_int_utils_init() should be called before any other function.
388  */
389
390 void ldap_int_utils_init( void )
391 {
392         static int done=0;
393         if (done)
394           return;
395         done=1;
396
397 #ifdef LDAP_R_COMPILE
398 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
399         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
400 #endif
401         ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
402
403 #ifdef HAVE_CYRUS_SASL
404         ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
405 #endif
406 #endif
407
408         /* call other module init functions here... */
409 }
410
411 #if defined( NEED_COPY_HOSTENT )
412 # undef NEED_SAFE_REALLOC
413 #define NEED_SAFE_REALLOC
414
415 static char *cpy_aliases(
416         char ***tgtio,
417         char *buf,
418         char **src )
419 {
420         int len;
421         char **tgt=*tgtio;
422         for( ; (*src) ; src++ ) {
423                 len = strlen( *src ) + 1;
424                 AC_MEMCPY( buf, *src, len );
425                 *tgt++=buf;
426                 buf+=len;
427         }
428         *tgtio=tgt;   
429         return buf;
430 }
431
432 static char *cpy_addresses(
433         char ***tgtio,
434         char *buf,
435         char **src,
436         int len )
437 {
438         char **tgt=*tgtio;
439         for( ; (*src) ; src++ ) {
440                 AC_MEMCPY( buf, *src, len );
441                 *tgt++=buf;
442                 buf+=len;
443         }
444         *tgtio=tgt;      
445         return buf;
446 }
447
448 static int copy_hostent(
449         struct hostent *res,
450         char **buf,
451         struct hostent * src )
452 {
453         char    **p;
454         char    **tp;
455         char    *tbuf;
456         int     name_len;
457         int     n_alias=0;
458         int     total_alias_len=0;
459         int     n_addr=0;
460         int     total_addr_len;
461         int     total_len;
462           
463         /* calculate the size needed for the buffer */
464         name_len = strlen( src->h_name ) + 1;
465         
466         if( src->h_aliases != NULL ) {
467                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
468                         total_alias_len += strlen( *p ) + 1;
469                         n_alias++; 
470                 }
471         }
472
473         if( src->h_addr_list != NULL ) {
474                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
475                         n_addr++;
476                 }
477                 total_addr_len = n_addr * src->h_length;
478         }
479         
480         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
481                 total_addr_len + total_alias_len + name_len;
482         
483         if (safe_realloc( buf, total_len )) {                    
484                 tp = (char **) *buf;
485                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
486                 AC_MEMCPY( res, src, sizeof( struct hostent ) );
487                 /* first the name... */
488                 AC_MEMCPY( tbuf, src->h_name, name_len );
489                 res->h_name = tbuf; tbuf+=name_len;
490                 /* now the aliases */
491                 res->h_aliases = tp;
492                 if ( src->h_aliases != NULL ) {
493                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
494                 }
495                 *tp++=NULL;
496                 /* finally the addresses */
497                 res->h_addr_list = tp;
498                 if ( src->h_addr_list != NULL ) {
499                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
500                 }
501                 *tp++=NULL;
502                 return 0;
503         }
504         return -1;
505 }
506 #endif
507
508 #if defined( NEED_SAFE_REALLOC )
509 static char *safe_realloc( char **buf, int len )
510 {
511         char *tmpbuf;
512         tmpbuf = LDAP_REALLOC( *buf, len );
513         if (tmpbuf) {
514                 *buf=tmpbuf;
515         } 
516         return tmpbuf;
517 }
518 #endif
519
520 char * ldap_pvt_get_fqdn( char *name )
521 {
522         char *fqdn, *ha_buf;
523         char hostbuf[MAXHOSTNAMELEN+1];
524         struct hostent *hp, he_buf;
525         int rc, local_h_errno;
526
527         if( name == NULL ) {
528                 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
529                         hostbuf[MAXHOSTNAMELEN] = '\0';
530                         name = hostbuf;
531                 } else {
532                         name = "localhost";
533                 }
534         }
535
536         rc = ldap_pvt_gethostbyname_a( name,
537                 &he_buf, &ha_buf, &hp, &local_h_errno );
538
539         if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
540                 fqdn = LDAP_STRDUP( name );
541         } else {
542                 fqdn = LDAP_STRDUP( hp->h_name );
543         }
544
545         LDAP_FREE( ha_buf );
546         return fqdn;
547 }
548
549 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
550         && !defined( HAVE_GAI_STRERROR )
551 char *ldap_pvt_gai_strerror (int code) {
552         static struct {
553                 int code;
554                 const char *msg;
555         } values[] = {
556 #ifdef EAI_ADDRFAMILY
557                 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
558 #endif
559                 { EAI_AGAIN, N_("Temporary failure in name resolution") },
560                 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
561                 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
562                 { EAI_FAMILY, N_("ai_family not supported") },
563                 { EAI_MEMORY, N_("Memory allocation failure") },
564 #ifdef EAI_NODATA
565                 { EAI_NODATA, N_("No address associated with hostname") },
566 #endif    
567                 { EAI_NONAME, N_("Name or service not known") },
568                 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
569                 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
570                 { EAI_SYSTEM, N_("System error") },
571                 { 0, NULL }
572         };
573
574         int i;
575
576         for ( i = 0; values[i].msg != NULL; i++ ) {
577                 if ( values[i].code == code ) {
578                         return (char *) _(values[i].msg);
579                 }
580         }
581         
582         return _("Unknown error");
583 }
584 #endif