]> git.sur5r.net Git - openldap/blob - libraries/libldap/util-int.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / libraries / libldap / util-int.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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 #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         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         
146         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyname_mutex );
147         
148         he = gethostbyname( name );
149         
150         if (he==NULL) {
151                 *herrno_ptr = h_errno;
152                 retval = -1;
153         } else if (copy_hostent( resbuf, buf, he )<0) {
154                 *herrno_ptr = -1;
155                 retval = -1;
156         } else {
157                 *result = resbuf;
158                 retval = 0;
159         }
160         
161         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyname_mutex );
162         
163         return retval;
164 #else   
165         *result = gethostbyname( name );
166
167         if (*result!=NULL) {
168                 return 0;
169         }
170
171         *herrno_ptr = h_errno;
172         
173         return -1;
174 #endif  
175 }
176          
177 int ldap_pvt_gethostbyaddr_a(
178         const char *addr,
179         int len,
180         int type,
181         struct hostent *resbuf,
182         char **buf,
183         struct hostent **result,
184         int *herrno_ptr )
185 {
186 #if defined( HAVE_GETHOSTBYADDR_R )
187
188 # undef NEED_SAFE_REALLOC
189 # define NEED_SAFE_REALLOC   
190         int r=-1;
191         int buflen=BUFSTART;
192         *buf = NULL;   
193         for(;buflen<BUFMAX;) {
194                 if (safe_realloc( buf, buflen )==NULL)
195                         return r;
196 #if (GETHOSTBYADDR_R_NARGS < 8)
197                 r = ((*result=gethostbyaddr_r( addr, len, type,
198                                                resbuf, *buf, buflen, 
199                                                herrno_ptr )) == NULL) ?
200                     -1 : 0;
201 #else
202                 r = gethostbyaddr_r( addr, len, type,
203                         resbuf, *buf, buflen, 
204                         result, herrno_ptr );
205 #endif
206
207 #ifdef NETDB_INTERNAL
208                 if ((r<0) &&
209                         (*herrno_ptr==NETDB_INTERNAL) &&
210                         (errno==ERANGE))
211                 {
212                         buflen*=2;
213                         continue;
214                 }
215 #endif
216                 return r;
217         }
218         return -1;
219 #elif defined( LDAP_R_COMPILE )
220 # undef NEED_COPY_HOSTENT
221 # define NEED_COPY_HOSTENT   
222         struct hostent *he;
223         int     retval;
224         
225         ldap_pvt_thread_mutex_lock( &ldap_int_gethostbyaddr_mutex );
226         
227         he = gethostbyaddr( addr, len, type );
228         
229         if (he==NULL) {
230                 *herrno_ptr = h_errno;
231                 retval = -1;
232         } else if (copy_hostent( resbuf, buf, he )<0) {
233                 *herrno_ptr = -1;
234                 retval = -1;
235         } else {
236                 *result = resbuf;
237                 retval = 0;
238         }
239         
240         ldap_pvt_thread_mutex_unlock( &ldap_int_gethostbyaddr_mutex );
241         
242         return retval;   
243 #else /* gethostbyaddr() */
244         *result = gethostbyaddr( addr, len, type );
245
246         if (*result!=NULL) {
247                 return 0;
248         }
249         return -1;
250 #endif  
251 }
252 /* 
253  * ldap_int_utils_init() should be called before any other function.
254  */
255
256 void ldap_int_utils_init( void )
257 {
258         static int done=0;
259         if (done)
260           return;
261         done=1;
262
263 #ifdef LDAP_R_COMPILE
264
265 #if !defined( USE_CTIME_R ) && !defined( HAVE_REENTRANT_FUNCTIONS )
266         ldap_pvt_thread_mutex_init( &ldap_int_ctime_mutex );
267 #endif
268
269 #if !defined( HAVE_GETHOSTBYNAME_R )
270         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyname_mutex );
271 #endif
272
273 #if !defined( HAVE_GETHOSTBYADDR_R )
274         ldap_pvt_thread_mutex_init( &ldap_int_gethostbyaddr_mutex );
275 #endif
276
277         /* call other module init functions here... */
278 #endif
279 }
280
281 #if defined( NEED_COPY_HOSTENT )
282 # undef NEED_SAFE_REALLOC
283 #define NEED_SAFE_REALLOC
284
285 static char *cpy_aliases( char ***tgtio, char *buf, char **src )
286 {
287         int len;
288         char **tgt=*tgtio;
289         for( ; (*src) ; src++ ) {
290                 len = strlen( *src ) + 1;
291                 memcpy( buf, *src, len );
292                 *tgt++=buf;
293                 buf+=len;
294         }
295         *tgtio=tgt;   
296         return buf;
297 }
298
299 static char *cpy_addresses( char ***tgtio, char *buf, char **src, int len )
300 {
301         char **tgt=*tgtio;
302         for( ; (*src) ; src++ ) {
303                 memcpy( buf, *src, len );
304                 *tgt++=buf;
305                 buf+=len;
306         }
307         *tgtio=tgt;      
308         return buf;
309 }
310
311 static int copy_hostent( struct hostent *res, char **buf, struct hostent * src )
312 {
313         char    **p;
314         char    **tp;
315         char    *tbuf;
316         int     name_len;
317         int     n_alias;
318         int     total_alias_len;
319         int     n_addr;
320         int     total_addr_len;
321         int     total_len;
322           
323         /* calculate the size needed for the buffer */
324         name_len = strlen( src->h_name ) + 1;
325         
326         for( n_alias=total_alias_len=0, p=src->h_aliases; (*p) ; p++ ) {
327                 total_alias_len += strlen( *p ) + 1;
328                 n_alias++; 
329         }
330
331         for( n_addr=0, p=src->h_addr_list; (*p) ; p++ ) {
332                 n_addr++;
333         }
334         total_addr_len = n_addr * src->h_length;
335         
336         total_len = (n_alias + n_addr + 2) * sizeof( char * ) +
337                 total_addr_len + total_alias_len + name_len;
338         
339         if (safe_realloc( buf, total_len )) {                    
340                 tp = (char **) *buf;
341                 tbuf = *buf + (n_alias + n_addr + 2) * sizeof( char * );
342                 memcpy( res, src, sizeof( struct hostent ) );
343                 /* first the name... */
344                 memcpy( tbuf, src->h_name, name_len );
345                 res->h_name = tbuf; tbuf+=name_len;
346                 /* now the aliases */
347                 res->h_aliases = tp;
348                 tbuf = cpy_aliases( &tp, tbuf, src->h_aliases );
349                 *tp++=NULL;
350                 /* finally the addresses */
351                 res->h_addr_list = tp;
352                 tbuf = cpy_addresses( &tp, tbuf, src->h_addr_list, src->h_length );
353                 *tp++=NULL;
354                 return 0;
355         }
356         return -1;
357 }
358 #endif
359
360 #if defined( NEED_SAFE_REALLOC )
361 static char *safe_realloc( char **buf, int len )
362 {
363         char *tmpbuf;
364         tmpbuf = LDAP_REALLOC( *buf, len );
365         if (tmpbuf) {
366                 *buf=tmpbuf;
367         } 
368         return tmpbuf;
369 }
370 #endif
371
372