]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
00faaca7d511a2b327447b25a005d6356c27963d
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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 #if defined( LDAP_R_COMPILE )
37 # include <ldap_pvt_thread.h>
38 #else
39 # undef HAVE_REENTRANT_FUNCTIONS
40 #endif
41
42 #if (defined( HAVE_CTIME_R ) || defined( HAVE_REENTRANT_FUNCTIONS)) \
43         && defined( CTIME_R_NARGS )
44 #       define USE_CTIME_R
45 #endif
46
47 #if defined(HAVE_GETHOSTBYNAME_R) && \
48     (GETHOSTBYNAME_R_NARGS > 6 || GETHOSTBYNAME_R_NARGS < 5)
49         /* Don't know how to handle this version, pretend it's not there */
50 #       undef HAVE_GETHOSTBYNAME_R
51 #endif
52 #if defined(HAVE_GETHOSTBYADDR_R) && \
53     (GETHOSTBYADDR_R_NARGS > 8 || GETHOSTBYADDR_R_NARGS < 7)
54         /* Don't know how to handle this version, pretend it's not there */
55 #       undef HAVE_GETHOSTBYADDR_R
56 #endif
57
58 #ifdef LDAP_R_COMPILE
59 # ifndef USE_CTIME_R
60         static ldap_pvt_thread_mutex_t ldap_int_ctime_mutex;
61 # endif
62 # if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
63         static ldap_pvt_thread_mutex_t ldap_int_gethostby_mutex;
64 # endif
65 # ifdef HAVE_RES_QUERY
66         ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
67 # endif
68 #endif /* LDAP_R_COMPILE */
69
70 char *ldap_pvt_ctime( const time_t *tp, char *buf )
71 {
72 #ifdef USE_CTIME_R
73 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
74 #       error "CTIME_R_NARGS should be 2 or 3"
75 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
76         return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
77 # elif CTIME_R_NARGS > 2
78         return ctime_r(tp,buf,26);
79 # else
80         return ctime_r(tp,buf);
81 # endif   
82
83 #else
84 # ifdef LDAP_R_COMPILE
85         ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
86 # endif
87         AC_MEMCPY( buf, ctime(tp), 26 );
88 # ifdef LDAP_R_COMPILE
89         ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
90 # endif
91         return buf;
92 #endif  
93 }
94
95 #define BUFSTART 1024
96 #define BUFMAX (32*1024)
97
98 #if defined( NEED_SAFE_REALLOC )
99 static char *safe_realloc( char **buf, int len );
100 #endif
101
102 #if !defined(HAVE_GETHOSTBYNAME_R) && defined(LDAP_R_COMPILE)
103 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
104 #endif
105
106 int ldap_pvt_gethostbyname_a(
107         const char *name, 
108         struct hostent *resbuf,
109         char **buf,
110         struct hostent **result,
111         int *herrno_ptr )
112 {
113 #if defined( HAVE_GETHOSTBYNAME_R )
114
115 # define NEED_SAFE_REALLOC 1   
116         int r=-1;
117         int buflen=BUFSTART;
118         *buf = NULL;
119         for(;buflen<BUFMAX;) {
120                 if (safe_realloc( buf, buflen )==NULL)
121                         return r;
122
123 #if (GETHOSTBYNAME_R_NARGS < 6)
124                 r = ((*result=gethostbyname_r( name, resbuf, *buf,
125                                                buflen, herrno_ptr ))== NULL) ?
126                     -1 : 0;
127 #else
128                 r = gethostbyname_r( name, resbuf, *buf,
129                         buflen, result, herrno_ptr );
130 #endif
131
132                 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
133                        name, r, 0 );
134
135 #ifdef NETDB_INTERNAL
136                 if ((r<0) &&
137                         (*herrno_ptr==NETDB_INTERNAL) &&
138                         (errno==ERANGE))
139                 {
140                         buflen*=2;
141                         continue;
142                 }
143 #endif
144                 return r;
145         }
146         return -1;
147 #elif defined( LDAP_R_COMPILE )
148 # define NEED_COPY_HOSTENT   
149         struct hostent *he;
150         int     retval;
151         *buf = NULL;
152         
153         ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
154         
155         he = gethostbyname( name );
156         
157         if (he==NULL) {
158                 *herrno_ptr = h_errno;
159                 retval = -1;
160         } else if (copy_hostent( resbuf, buf, he )<0) {
161                 *herrno_ptr = -1;
162                 retval = -1;
163         } else {
164                 *result = resbuf;
165                 retval = 0;
166         }
167         
168         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
169         
170         return retval;
171 #else   
172         *buf = NULL;
173         *result = gethostbyname( name );
174
175         if (*result!=NULL) {
176                 return 0;
177         }
178
179         *herrno_ptr = h_errno;
180         
181         return -1;
182 #endif  
183 }
184          
185 int ldap_pvt_gethostbyaddr_a(
186         const char *addr,
187         int len,
188         int type,
189         struct hostent *resbuf,
190         char **buf,
191         struct hostent **result,
192         int *herrno_ptr )
193 {
194 #if defined( HAVE_GETHOSTBYADDR_R )
195
196 # undef NEED_SAFE_REALLOC
197 # define NEED_SAFE_REALLOC   
198         int r=-1;
199         int buflen=BUFSTART;
200         *buf = NULL;   
201         for(;buflen<BUFMAX;) {
202                 if (safe_realloc( buf, buflen )==NULL)
203                         return r;
204 #if (GETHOSTBYADDR_R_NARGS < 8)
205                 r = ((*result=gethostbyaddr_r( addr, len, type,
206                                                resbuf, *buf, buflen, 
207                                                herrno_ptr )) == NULL) ?
208                     -1 : 0;
209 #else
210                 r = gethostbyaddr_r( addr, len, type,
211                         resbuf, *buf, buflen, 
212                         result, herrno_ptr );
213 #endif
214
215 #ifdef NETDB_INTERNAL
216                 if ((r<0) &&
217                         (*herrno_ptr==NETDB_INTERNAL) &&
218                         (errno==ERANGE))
219                 {
220                         buflen*=2;
221                         continue;
222                 }
223 #endif
224                 return r;
225         }
226         return -1;
227 #elif defined( LDAP_R_COMPILE )
228 # undef NEED_COPY_HOSTENT
229 # define NEED_COPY_HOSTENT   
230         struct hostent *he;
231         int     retval;
232         *buf = NULL;   
233         
234         ldap_pvt_thread_mutex_lock( &ldap_int_gethostby_mutex );
235         
236         he = gethostbyaddr( addr, len, type );
237         
238         if (he==NULL) {
239                 *herrno_ptr = h_errno;
240                 retval = -1;
241         } else if (copy_hostent( resbuf, buf, he )<0) {
242                 *herrno_ptr = -1;
243                 retval = -1;
244         } else {
245                 *result = resbuf;
246                 retval = 0;
247         }
248         
249         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostby_mutex );
250         
251         return retval;   
252 #else /* gethostbyaddr() */
253         *buf = NULL;   
254         *result = gethostbyaddr( addr, len, type );
255
256         if (*result!=NULL) {
257                 return 0;
258         }
259         return -1;
260 #endif  
261 }
262 /* 
263  * ldap_int_utils_init() should be called before any other function.
264  */
265
266 void ldap_int_utils_init( void )
267 {
268         static int done=0;
269         if (done)
270           return;
271         done=1;
272
273 #ifdef LDAP_R_COMPILE
274
275 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
276         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
277 #endif
278
279 #if !defined( HAVE_GETHOSTBYNAME_R ) || !defined( HAVE_GETHOSTBYADDR_R )
280         ldap_pvt_thread_mutex_init( &ldap_int_gethostby_mutex );
281 #endif
282
283 #ifdef HAVE_RES_QUERY
284         ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
285 #endif
286
287         /* call other module init functions here... */
288 #endif
289 }
290
291 #if defined( NEED_COPY_HOSTENT )
292 # undef NEED_SAFE_REALLOC
293 #define NEED_SAFE_REALLOC
294
295 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
296 {
297         int len;
298         char **tgt=*tgtio;
299         for( ; (*src) ; src++ ) {
300                 len = strlen( *src ) + 1;
301                 AC_MEMCPY( buf, *src, len );
302                 *tgt++=buf;
303                 buf+=len;
304         }
305         *tgtio=tgt;   
306         return buf;
307 }
308
309 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
310 {
311         char **tgt=*tgtio;
312         for( ; (*src) ; src++ ) {
313                 AC_MEMCPY( buf, *src, len );
314                 *tgt++=buf;
315                 buf+=len;
316         }
317         *tgtio=tgt;      
318         return buf;
319 }
320
321 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
322 {
323         char    **p;
324         char    **tp;
325         char    *tbuf;
326         int     name_len;
327         int     n_alias=0;
328         int     total_alias_len=0;
329         int     n_addr=0;
330         int     total_addr_len;
331         int     total_len;
332           
333         /* calculate the size needed for the buffer */
334         name_len = strlen( src->h_name ) + 1;
335         
336         if( src->h_aliases != NULL ) {
337                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
338                         total_alias_len += strlen( *p ) + 1;
339                         n_alias++; 
340                 }
341         }
342
343         if( src->h_addr_list != NULL ) {
344                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
345                         n_addr++;
346                 }
347                 total_addr_len = n_addr * src->h_length;
348         }
349         
350         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
351                 total_addr_len + total_alias_len + name_len;
352         
353         if (safe_realloc( buf, total_len )) {                    
354                 tp = (char **) *buf;
355                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
356                 AC_MEMCPY( res, src, sizeof( struct hostent ) );
357                 /* first the name... */
358                 AC_MEMCPY( tbuf, src->h_name, name_len );
359                 res->h_name = tbuf; tbuf+=name_len;
360                 /* now the aliases */
361                 res->h_aliases = tp;
362                 if ( src->h_aliases != NULL ) {
363                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
364                 }
365                 *tp++=NULL;
366                 /* finally the addresses */
367                 res->h_addr_list = tp;
368                 if ( src->h_addr_list != NULL ) {
369                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
370                 }
371                 *tp++=NULL;
372                 return 0;
373         }
374         return -1;
375 }
376 #endif
377
378 #if defined( NEED_SAFE_REALLOC )
379 static char *safe_realloc( char **buf, int len )
380 {
381         char *tmpbuf;
382         tmpbuf = LDAP_REALLOC( *buf, len );
383         if (tmpbuf) {
384                 *buf=tmpbuf;
385         } 
386         return tmpbuf;
387 }
388 #endif
389
390 char * ldap_pvt_get_fqdn( char *name )
391 {
392         char *fqdn, *ha_buf;
393         char hostbuf[MAXHOSTNAMELEN+1];
394         struct hostent *hp, he_buf;
395         int rc, local_h_errno;
396
397         if( name == NULL ) {
398                 if( gethostname( hostbuf, MAXHOSTNAMELEN ) == 0 ) {
399                         hostbuf[MAXHOSTNAMELEN] = '\0';
400                         name = hostbuf;
401                 } else {
402                         name = "localhost";
403                 }
404         }
405
406         rc = ldap_pvt_gethostbyname_a( name,
407                 &he_buf, &ha_buf, &hp, &local_h_errno );
408
409         if( rc < 0 || hp == NULL || hp->h_name == NULL ) {
410                 fqdn = LDAP_STRDUP( name );
411         } else {
412                 fqdn = LDAP_STRDUP( hp->h_name );
413         }
414
415         LDAP_FREE( ha_buf );
416         return fqdn;
417 }
418
419 #if defined( HAVE_GETADDRINFO ) && !defined( HAVE_GAI_STRERROR )
420 char *ldap_pvt_gai_strerror (int code) {
421         static struct {
422                 int code;
423                 const char *msg;
424         } values[] = {
425 #ifdef EAI_ADDRFAMILY
426                 { EAI_ADDRFAMILY, "Address family for hostname not supported" },
427 #endif
428                 { EAI_AGAIN, "Temporary failure in name resolution" },
429                 { EAI_BADFLAGS, "Bad value for ai_flags" },
430                 { EAI_FAIL, "Non-recoverable failure in name resolution" },
431                 { EAI_FAMILY, "ai_family not supported" },
432                 { EAI_MEMORY, "Memory allocation failure" },
433 #ifdef EAI_NODATA
434                 { EAI_NODATA, "No address associated with hostname" },
435 #endif    
436                 { EAI_NONAME, "Name or service not known" },
437                 { EAI_SERVICE, "Servname not supported for ai_socktype" },
438                 { EAI_SOCKTYPE, "ai_socktype not supported" },
439                 { EAI_SYSTEM, "System error" },
440                 { 0, NULL }
441         };
442
443         int i;
444
445         for ( i = 0; values[i].msg != NULL; i++ ) {
446                 if ( values[i].code == code ) {
447                         return (char *) values[i].msg;
448                 }
449         }
450         
451         return "Unknown error";
452 }
453 #endif