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