]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
03547263a4fb9c245d3d98fc92954254b3bddc19
[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 # ifndef HAVE_GETHOSTBYNAME_R
62         static ldap_pvt_thread_mutex_t ldap_int_gethostbyname_mutex;
63 # endif
64 # ifndef HAVE_GETHOSTBYADDR_R
65         static ldap_pvt_thread_mutex_t ldap_int_gethostbyaddr_mutex;
66 # endif
67 # ifdef HAVE_RES_SEARCH
68         ldap_pvt_thread_mutex_t ldap_int_resolv_mutex;
69 # endif
70 #endif /* LDAP_R_COMPILE */
71
72 char *ldap_pvt_ctime( const time_t *tp, char *buf )
73 {
74 #ifdef USE_CTIME_R
75 # if (CTIME_R_NARGS > 3) || (CTIME_R_NARGS < 2)
76 #       error "CTIME_R_NARGS should be 2 or 3"
77 # elif CTIME_R_NARGS > 2 && defined(CTIME_R_RETURNS_INT)
78         return( ctime_r(tp,buf,26) < 0 ? 0 : buf );
79 # elif CTIME_R_NARGS > 2
80         return ctime_r(tp,buf,26);
81 # else
82         return ctime_r(tp,buf);
83 # endif   
84
85 #else
86 # ifdef LDAP_R_COMPILE
87         ldap_pvt_thread_mutex_lock( &ldap_int_ctime_mutex );
88 # endif
89         memcpy( buf, ctime(tp), 26 );
90 # ifdef LDAP_R_COMPILE
91         ldap_pvt_thread_mutex_unlock( &ldap_int_ctime_mutex );
92 # endif
93         return buf;
94 #endif  
95 }
96
97 #define BUFSTART 1024
98 #define BUFMAX (32*1024)
99
100 static char *safe_realloc( char **buf, int len );
101 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src );
102
103 int ldap_pvt_gethostbyname_a(
104         const char *name, 
105         struct hostent *resbuf,
106         char **buf,
107         struct hostent **result,
108         int *herrno_ptr )
109 {
110 #if defined( HAVE_GETHOSTBYNAME_R )
111
112 # define NEED_SAFE_REALLOC 1   
113         int r=-1;
114         int buflen=BUFSTART;
115         *buf = NULL;
116         for(;buflen<BUFMAX;) {
117                 if (safe_realloc( buf, buflen )==NULL)
118                         return r;
119
120 #if (GETHOSTBYNAME_R_NARGS < 6)
121                 r = ((*result=gethostbyname_r( name, resbuf, *buf,
122                                                buflen, herrno_ptr ))== NULL) ?
123                     -1 : 0;
124 #else
125                 r = gethostbyname_r( name, resbuf, *buf,
126                         buflen, result, herrno_ptr );
127 #endif
128
129                 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_gethostbyname_a: host=%s, r=%d\n",
130                        name, r, 0 );
131
132 #ifdef NETDB_INTERNAL
133                 if ((r<0) &&
134                         (*herrno_ptr==NETDB_INTERNAL) &&
135                         (errno==ERANGE))
136                 {
137                         buflen*=2;
138                         continue;
139                 }
140 #endif
141                 return r;
142         }
143         return -1;
144 #elif defined( LDAP_R_COMPILE )
145 # define NEED_COPY_HOSTENT   
146         struct hostent *he;
147         int     retval;
148         
149         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
150         
151         he = gethostbyname( name );
152         
153         if (he==NULL) {
154                 *herrno_ptr = h_errno;
155                 retval = -1;
156         } else if (copy_hostent( resbuf, buf, he )<0) {
157                 *herrno_ptr = -1;
158                 retval = -1;
159         } else {
160                 *result = resbuf;
161                 retval = 0;
162         }
163         
164         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
165         
166         return retval;
167 #else   
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         
228         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_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_gethostbyaddr_mutex );
244         
245         return retval;   
246 #else /* gethostbyaddr() */
247         *result = gethostbyaddr( addr, len, type );
248
249         if (*result!=NULL) {
250                 return 0;
251         }
252         return -1;
253 #endif  
254 }
255 /* 
256  * ldap_int_utils_init() should be called before any other function.
257  */
258
259 void ldap_int_utils_init( void )
260 {
261         static int done=0;
262         if (done)
263           return;
264         done=1;
265
266 #ifdef LDAP_R_COMPILE
267
268 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
269         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
270 #endif
271
272 #if !defined( HAVE_GETHOSTBYNAME_R )
273         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
274 #endif
275
276 #if !defined( HAVE_GETHOSTBYADDR_R )
277         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
278 #endif
279
280 #ifdef HAVE_RES_SEARCH
281         ldap_pvt_thread_mutex_init( &ldap_int_resolv_mutex );
282 #endif
283
284         /* call other module init functions here... */
285 #endif
286 }
287
288 #if defined( NEED_COPY_HOSTENT )
289 # undef NEED_SAFE_REALLOC
290 #define NEED_SAFE_REALLOC
291
292 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
293 {
294         int len;
295         char **tgt=*tgtio;
296         for( ; (*src) ; src++ ) {
297                 len = strlen( *src ) + 1;
298                 memcpy( buf, *src, len );
299                 *tgt++=buf;
300                 buf+=len;
301         }
302         *tgtio=tgt;   
303         return buf;
304 }
305
306 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
307 {
308         char **tgt=*tgtio;
309         for( ; (*src) ; src++ ) {
310                 memcpy( buf, *src, len );
311                 *tgt++=buf;
312                 buf+=len;
313         }
314         *tgtio=tgt;      
315         return buf;
316 }
317
318 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
319 {
320         char    **p;
321         char    **tp;
322         char    *tbuf;
323         int     name_len;
324         int     n_alias=0;
325         int     total_alias_len=0;
326         int     n_addr=0;
327         int     total_addr_len;
328         int     total_len;
329           
330         /* calculate the size needed for the buffer */
331         name_len = strlen( src->h_name ) + 1;
332         
333         if( src->h_aliases != NULL ) {
334                 for( p = src->h_aliases; (*p) != NULL; p++ ) {
335                         total_alias_len += strlen( *p ) + 1;
336                         n_alias++; 
337                 }
338         }
339
340         if( src->h_addr_list != NULL ) {
341                 for( p = src->h_addr_list; (*p) != NULL; p++ ) {
342                         n_addr++;
343                 }
344                 total_addr_len = n_addr * src->h_length;
345         }
346         
347         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
348                 total_addr_len + total_alias_len + name_len;
349         
350         if (safe_realloc( buf, total_len )) {                    
351                 tp = (char **) *buf;
352                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
353                 memcpy( res, src, sizeof( struct hostent ) );
354                 /* first the name... */
355                 memcpy( tbuf, src->h_name, name_len );
356                 res->h_name = tbuf; tbuf+=name_len;
357                 /* now the aliases */
358                 res->h_aliases = tp;
359                 if ( src->h_aliases != NULL ) {
360                         tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
361                 }
362                 *tp++=NULL;
363                 /* finally the addresses */
364                 res->h_addr_list = tp;
365                 if ( src->h_addr_list != NULL ) {
366                         tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
367                 }
368                 *tp++=NULL;
369                 return 0;
370         }
371         return -1;
372 }
373 #endif
374
375 #if defined( NEED_SAFE_REALLOC )
376 static char *safe_realloc( char **buf, int len )
377 {
378         char *tmpbuf;
379         tmpbuf = LDAP_REALLOC( *buf, len );
380         if (tmpbuf) {
381                 *buf=tmpbuf;
382         } 
383         return tmpbuf;
384 }
385 #endif
386
387