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