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