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