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