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