]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
72d5be2ef88977e4b12fe9192116bd6bf3e367a6
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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 #endif
403
404         /* call other module init functions here... */
405 }
406
407 #if defined( NEED_COPY_HOSTENT )
408 # undef NEED_SAFE_REALLOC
409 #define NEED_SAFE_REALLOC
410
411 static char *cpy_aliases(
412         char ***tgtio,
413         char *buf,
414         char **src )
415 {
416         int len;
417         char **tgt=*tgtio;
418         for( ; (*src) ; src++ ) {
419                 len = strlen( *src ) + 1;
420                 AC_MEMCPY( buf, *src, len );
421                 *tgt++=buf;
422                 buf+=len;
423         }
424         *tgtio=tgt;   
425         return buf;
426 }
427
428 static char *cpy_addresses(
429         char ***tgtio,
430         char *buf,
431         char **src,
432         int len )
433 {
434         char **tgt=*tgtio;
435         for( ; (*src) ; src++ ) {
436                 AC_MEMCPY( buf, *src, len );
437                 *tgt++=buf;
438                 buf+=len;
439         }
440         *tgtio=tgt;      
441         return buf;
442 }
443
444 static int copy_hostent(
445         struct hostent *res,
446         char **buf,
447         struct hostent * src )
448 {
449         char    **p;
450         char    **tp;
451         char    *tbuf;
452         int     name_len;
453         int     n_alias=0;
454         int     total_alias_len=0;
455         int     n_addr=0;
456         int     total_addr_len;
457         int     total_len;
458           
459         /* calculate the size needed for the buffer */
460         name_len = strlen( src->h_name ) + 1;
461         
462         if( src->h_aliases != NULL ) {
463                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
464                         total_alias_len += strlen( *p ) + 1;
465                         n_alias++; 
466                 }
467         }
468
469         if( src->h_addr_list != NULL ) {
470                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
471                         n_addr++;
472                 }
473                 total_addr_len = n_addr * src->h_length;
474         }
475         
476         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
477                 total_addr_len + total_alias_len + name_len;
478         
479         if (safe_realloc( buf, total_len )) {                    
480                 tp = (char **) *buf;
481                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
482                 AC_MEMCPY( res, src, sizeof( struct hostent ) );
483                 /* first the name... */
484                 AC_MEMCPY( tbuf, src->h_name, name_len );
485                 res->h_name = tbuf; tbuf+=name_len;
486                 /* now the aliases */
487                 res->h_aliases = tp;
488                 if ( src->h_aliases != NULL ) {
489                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
490                 }
491                 *tp++=NULL;
492                 /* finally the addresses */
493                 res->h_addr_list = tp;
494                 if ( src->h_addr_list != NULL ) {
495                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
496                 }
497                 *tp++=NULL;
498                 return 0;
499         }
500         return -1;
501 }
502 #endif
503
504 #if defined( NEED_SAFE_REALLOC )
505 static char *safe_realloc( char **buf, int len )
506 {
507         char *tmpbuf;
508         tmpbuf = LDAP_REALLOC( *buf, len );
509         if (tmpbuf) {
510                 *buf=tmpbuf;
511         } 
512         return tmpbuf;
513 }
514 #endif
515
516 char * ldap_pvt_get_fqdn( char *name )
517 {
518         char *fqdn, *ha_buf;
519         char hostbuf[MAXHOSTNAMELEN+1];
520         struct hostent *hp, he_buf;
521         int rc, local_h_errno;
522
523         if( name == NULL ) {
524                 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
525                         hostbuf[MAXHOSTNAMELEN] = '\0';
526                         name = hostbuf;
527                 } else {
528                         name = "localhost";
529                 }
530         }
531
532         rc = ldap_pvt_gethostbyname_a( name,
533                 &he_buf, &ha_buf, &hp, &local_h_errno );
534
535         if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
536                 fqdn = LDAP_STRDUP( name );
537         } else {
538                 fqdn = LDAP_STRDUP( hp->h_name );
539         }
540
541         LDAP_FREE( ha_buf );
542         return fqdn;
543 }
544
545 #if ( defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) ) \
546         && !defined( HAVE_GAI_STRERROR )
547 char *ldap_pvt_gai_strerror (int code) {
548         static struct {
549                 int code;
550                 const char *msg;
551         } values[] = {
552 #ifdef EAI_ADDRFAMILY
553                 { EAI_ADDRFAMILY, N_("Address family for hostname not supported") },
554 #endif
555                 { EAI_AGAIN, N_("Temporary failure in name resolution") },
556                 { EAI_BADFLAGS, N_("Bad value for ai_flags") },
557                 { EAI_FAIL, N_("Non-recoverable failure in name resolution") },
558                 { EAI_FAMILY, N_("ai_family not supported") },
559                 { EAI_MEMORY, N_("Memory allocation failure") },
560 #ifdef EAI_NODATA
561                 { EAI_NODATA, N_("No address associated with hostname") },
562 #endif    
563                 { EAI_NONAME, N_("Name or service not known") },
564                 { EAI_SERVICE, N_("Servname not supported for ai_socktype") },
565                 { EAI_SOCKTYPE, N_("ai_socktype not supported") },
566                 { EAI_SYSTEM, N_("System error") },
567                 { 0, NULL }
568         };
569
570         int i;
571
572         for ( i = 0; values[i].msg != NULL; i++ ) {
573                 if ( values[i].code == code ) {
574                         return (char *) _(values[i].msg);
575                 }
576         }
577         
578         return _("Unknown error");
579 }
580 #endif