]> git.sur5r.net Git - openldap/blob - libraries/libldap/cldap.c
Check if inet_addr() returns 0xffffffff as well as -1.
[openldap] / libraries / libldap / cldap.c
1 /*
2  *  Copyright (c) 1990, 1994 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  cldap.c - synchronous, retrying interface to the cldap protocol
6  */
7
8 #include "portable.h"
9
10 #ifdef LDAP_CONNECTIONLESS
11
12 #ifndef lint 
13 static char copyright[] = "@(#) Copyright (c) 1990, 1994 Regents of the University of Michigan.\nAll rights reserved.\n";
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <ac/errno.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23
24 #include "ldap-int.h"
25
26 #define DEF_CLDAP_TIMEOUT       3
27 #define DEF_CLDAP_TRIES         4
28
29
30 struct cldap_retinfo {
31         int             cri_maxtries;
32         int             cri_try;
33         int             cri_useaddr;
34         long            cri_timeout;
35 };
36
37 static int add_addr LDAP_P((
38         LDAP *ld, struct sockaddr *sap ));
39 static int cldap_result LDAP_P((
40         LDAP *ld, int msgid, LDAPMessage **res,
41         struct cldap_retinfo *crip, char *base ));
42 static int cldap_parsemsg LDAP_P((
43         LDAP *ld, int msgid, BerElement *ber,
44         LDAPMessage **res, char *base ));
45
46 /*
47  * cldap_open - initialize and connect to an ldap server.  A magic cookie to
48  * be used for future communication is returned on success, NULL on failure.
49  *
50  * Example:
51  *      LDAP    *ld;
52  *      ld = cldap_open( hostname, port );
53  */
54
55 LDAP *
56 cldap_open( char *host, int port )
57 {
58     int                 s;
59     unsigned long       address;
60     struct sockaddr_in  sock;
61     struct hostent      *hp;
62     LDAP                *ld;
63     char                *p;
64     int                 i;
65
66     Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
67
68     if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
69         return( NULL );
70     }
71
72     sock.sin_addr.s_addr = 0;
73     sock.sin_family = AF_INET;
74     sock.sin_port = 0;
75     if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0)  {
76         close( s );
77         return( NULL );
78     }
79
80     if (( ld = ldap_init( host, port )) == NULL ) {
81         close( s );
82         return( NULL );
83     }
84     if ( (ld->ld_sb.sb_fromaddr = (void *) calloc( 1,
85             sizeof( struct sockaddr ))) == NULL ) {
86
87         ldap_ld_free(ld, 1);
88         return( NULL );
89     }   
90     ld->ld_sb.sb_sd = s;
91     ld->ld_sb.sb_naddr = 0;
92     ld->ld_version = LDAP_VERSION;
93
94     sock.sin_family = AF_INET;
95     sock.sin_port = htons( port );
96
97     /*
98      * 'host' may be a space-separated list.
99      */
100     if ( host != NULL ) {
101         for ( ; host != NULL; host = p ) {
102             if (( p = strchr( host, ' ' )) != NULL ) {
103                 for (*p++ = '\0'; *p == ' '; p++) {
104                     ;
105                 }
106             }
107
108             address = inet_addr( host );
109             /* This was just a test for -1 until OSF1 let inet_addr return
110                unsigned int, which is narrower than 'unsigned long address' */
111             if ( address == 0xffffffff || address == (unsigned long) -1 ) {
112                 if ( (hp = gethostbyname( host )) == NULL ) {
113                     errno = EHOSTUNREACH;
114                     continue;
115                 }
116
117                 for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
118                     SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
119                             (char *)hp->h_addr_list[ i ],
120                             sizeof(sock.sin_addr.s_addr));
121                     if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
122                         ldap_ld_free( ld, 1 );
123                         return( NULL );
124                     }
125                 }
126
127             } else {
128                 sock.sin_addr.s_addr = address;
129                 if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
130                     ldap_ld_free( ld, 1 );
131                     return( NULL );
132                 }
133             }
134
135             if ( ld->ld_host == NULL ) {
136                     ld->ld_host = strdup( host );
137             }
138         }
139
140     } else {
141         address = INADDR_LOOPBACK;
142         sock.sin_addr.s_addr = htonl( address );
143         if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
144             ldap_ld_free( ld, 1 );
145             return( NULL );
146         }
147     }
148
149     if ( ld->ld_sb.sb_addrs == NULL
150 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
151             || ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
152 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
153             ) {
154         ldap_ld_free( ld, 0 );
155         return( NULL );
156     }
157
158     ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ 0 ];
159     cldap_setretryinfo( ld, 0, 0 );
160
161 #ifdef LDAP_DEBUG
162     putchar( '\n' );
163     for ( i = 0; i < ld->ld_sb.sb_naddr; ++i ) {
164         Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
165                 i, inet_ntoa( ((struct sockaddr_in *)
166                 ld->ld_sb.sb_addrs[ i ])->sin_addr ), 0 );
167     }
168 #endif
169
170     return( ld );
171 }
172
173
174
175 void
176 cldap_close( LDAP *ld )
177 {
178         ldap_ld_free( ld, 0 );
179 }
180
181
182 void
183 cldap_setretryinfo( LDAP *ld, int tries, int timeout )
184 {
185     ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
186     ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
187 }
188
189
190 int
191 cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
192         int attrsonly, LDAPMessage **res, char *logdn )
193 {
194     int                         ret, msgid;
195     struct cldap_retinfo        cri;
196
197     *res = NULLMSG;
198
199     (void) memset( &cri, 0, sizeof( cri ));
200
201     if ( logdn != NULL ) {
202         ld->ld_cldapdn = logdn;
203     } else if ( ld->ld_cldapdn == NULL ) {
204         ld->ld_cldapdn = "";
205     }
206
207     do {
208         if ( cri.cri_try != 0 ) {
209                 --ld->ld_msgid; /* use same id as before */
210         }
211         ld->ld_sb.sb_useaddr = ld->ld_sb.sb_addrs[ cri.cri_useaddr ];
212
213         Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
214             cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
215             ld->ld_sb.sb_useaddr)->sin_addr ), 0 );
216
217             if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
218                 attrsonly )) == -1 ) {
219                     return( ld->ld_errno );
220             }
221 #ifndef LDAP_NOCACHE
222             if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
223                 Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
224                         0, 0, 0 );
225                 *res = ld->ld_responses;
226                 ld->ld_responses = ld->ld_responses->lm_next;
227                 return( ldap_result2error( ld, *res, 0 ));
228             }
229 #endif /* LDAP_NOCACHE */
230             ret = cldap_result( ld, msgid, res, &cri, base );
231         } while (ret == -1);
232
233         return( ret );
234 }
235
236
237 static int
238 add_addr( LDAP *ld, struct sockaddr *sap )
239 {
240     struct sockaddr     *newsap, **addrs;
241
242     if (( newsap = (struct sockaddr *)malloc( sizeof( struct sockaddr )))
243             == NULL ) {
244         ld->ld_errno = LDAP_NO_MEMORY;
245         return( -1 );
246     }
247
248     if ( ld->ld_sb.sb_naddr == 0 ) {
249         addrs = (struct sockaddr **)malloc( sizeof(struct sockaddr *));
250     } else {
251         addrs = (struct sockaddr **)realloc( ld->ld_sb.sb_addrs,
252                 ( ld->ld_sb.sb_naddr + 1 ) * sizeof(struct sockaddr *));
253     }
254
255     if ( addrs == NULL ) {
256         free( newsap );
257         ld->ld_errno = LDAP_NO_MEMORY;
258         return( -1 );
259     }
260
261     SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
262     addrs[ ld->ld_sb.sb_naddr++ ] = newsap;
263     ld->ld_sb.sb_addrs = (void **)addrs;
264     return( 0 );
265 }
266
267
268 static int
269 cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
270         struct cldap_retinfo *crip, char *base )
271 {
272     Sockbuf             *sb = &ld->ld_sb;
273     BerElement          ber;
274     char                *logdn;
275     int                 ret, id, fromaddr, i;
276     struct timeval      tv;
277
278     fromaddr = -1;
279
280     if ( crip->cri_try == 0 ) {
281         crip->cri_maxtries = ld->ld_cldaptries * sb->sb_naddr;
282         crip->cri_timeout = ld->ld_cldaptimeout;
283         crip->cri_useaddr = 0;
284         Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
285                 ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
286     }
287
288     if ((tv.tv_sec = crip->cri_timeout / sb->sb_naddr) < 1 ) {
289         tv.tv_sec = 1;
290     }
291     tv.tv_usec = 0;
292
293     Debug( LDAP_DEBUG_TRACE,
294             "cldap_result waiting up to %d seconds for a response\n",
295             tv.tv_sec, 0, 0 );
296     ber_init_w_nullc( &ber, 0 );
297     ldap_set_ber_options( ld, &ber );
298
299     if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
300         ret = ld->ld_errno;
301         Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
302                 ret, 0, 0 );
303     } else if ( ld->ld_errno == LDAP_TIMEOUT ) {
304         Debug( LDAP_DEBUG_TRACE,
305             "cldap_result timed out\n", 0, 0, 0 );
306         /*
307          * It timed out; is it time to give up?
308          */
309         if ( ++crip->cri_try >= crip->cri_maxtries ) {
310             ret = LDAP_TIMEOUT;
311             --crip->cri_try;
312         } else {
313             if ( ++crip->cri_useaddr >= sb->sb_naddr ) {
314                 /*
315                  * new round: reset address to first one and
316                  * double the timeout
317                  */
318                 crip->cri_useaddr = 0;
319                 crip->cri_timeout <<= 1;
320             }
321             ret = -1;
322         }
323
324     } else {
325         /*
326          * Got a response.  It should look like:
327          * { msgid, logdn, { searchresponse...}}
328          */
329         logdn = NULL;
330
331         if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
332             free( ber.ber_buf );        /* gack! */
333             ret = LDAP_DECODING_ERROR;
334             Debug( LDAP_DEBUG_TRACE,
335                     "cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
336                     ret, 0, 0 );
337         } else if ( id != msgid ) {
338             free( ber.ber_buf );        /* gack! */
339             Debug( LDAP_DEBUG_TRACE,
340                     "cldap_result: looking for msgid %d; got %d\n",
341                     msgid, id, 0 );
342             ret = -1;   /* ignore and keep looking */
343         } else {
344             /*
345              * got a result: determine which server it came from
346              * decode into ldap message chain
347              */
348             for ( fromaddr = 0; fromaddr < sb->sb_naddr; ++fromaddr ) {
349                     if ( memcmp( &((struct sockaddr_in *)
350                             sb->sb_addrs[ fromaddr ])->sin_addr,
351                             &((struct sockaddr_in *)sb->sb_fromaddr)->sin_addr,
352                             sizeof( struct in_addr )) == 0 ) {
353                         break;
354                     }
355             }
356             ret = cldap_parsemsg( ld, msgid, &ber, res, base );
357             free( ber.ber_buf );        /* gack! */
358             Debug( LDAP_DEBUG_TRACE,
359                 "cldap_result got result (%d)\n", ret, 0, 0 );
360         }
361
362         if ( logdn != NULL ) {
363                 free( logdn );
364         }
365     }
366     
367
368     /*
369      * If we are giving up (successfully or otherwise) then 
370      * abandon any outstanding requests.
371      */
372     if ( ret != -1 ) {
373         i = crip->cri_try;
374         if ( i >= sb->sb_naddr ) {
375             i = sb->sb_naddr - 1;
376         }
377
378         for ( ; i >= 0; --i ) {
379             if ( i == fromaddr ) {
380                 continue;
381             }
382             sb->sb_useaddr = sb->sb_addrs[ i ];
383             Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
384                 msgid, inet_ntoa( ((struct sockaddr_in *)
385                 sb->sb_useaddr)->sin_addr ), 0 );
386             (void) ldap_abandon( ld, msgid );
387         }
388     }
389
390     return( ld->ld_errno = ret );
391 }
392
393
394 static int
395 cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
396         LDAPMessage **res, char *base )
397 {
398     unsigned long       tag, len;
399     int                 baselen, slen, rc;
400     char                *dn, *p, *cookie;
401     LDAPMessage         *chain, *prev, *ldm;
402     struct berval       *bv;
403
404     rc = LDAP_DECODING_ERROR;   /* pessimistic */
405     ldm = chain = prev = NULLMSG;
406     baselen = ( base == NULL ) ? 0 : strlen( base );
407     bv = NULL;
408
409     for ( tag = ber_first_element( ber, &len, &cookie );
410             tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
411             tag = ber_next_element( ber, &len, cookie )) {
412         if (( ldm = (LDAPMessage *)calloc( 1, sizeof(LDAPMessage)))
413                 == NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
414                 == NULLBER ) {
415             rc = LDAP_NO_MEMORY;
416             break;      /* return w/error*/
417         }
418         ldm->lm_msgid = msgid;
419         ldm->lm_msgtype = tag;
420
421         if ( tag == LDAP_RES_SEARCH_RESULT ) {
422             Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
423                     0, 0, 0 );
424
425             if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
426                 break;  /* return w/error */
427             }
428
429             if ( ber_printf( ldm->lm_ber, "to", tag, bv->bv_val,
430                     bv->bv_len ) == -1 ) {
431                 break;  /* return w/error */
432             }
433             ber_bvfree( bv );
434             bv = NULL;
435             rc = LDAP_SUCCESS;
436
437         } else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
438             if ( ber_scanf( ber, "{aO", &dn, &bv ) == LBER_ERROR ) {
439                 break;  /* return w/error */
440             }
441             Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
442             if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
443                     baselen > 0 ) {
444                 /*
445                  * substitute original searchbase for trailing '*'
446                  */
447                 if (( p = (char *)malloc( slen + baselen )) == NULL ) {
448                     rc = LDAP_NO_MEMORY;
449                     free( dn );
450                     break;      /* return w/error */
451                 }
452                 strcpy( p, dn );
453                 strcpy( p + slen - 1, base );
454                 free( dn );
455                 dn = p;
456             }
457
458             if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
459                     bv->bv_len ) == -1 ) {
460                 break;  /* return w/error */
461             }
462             free( dn );
463             ber_bvfree( bv );
464             bv = NULL;
465                 
466         } else {
467             Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %d\n",
468                     tag, 0, 0 );
469             rc = LDAP_PROTOCOL_ERROR;
470             break;      /* return w/error */
471         }
472
473         /* Reset message ber so we can read from it later.  Gack! */
474         ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
475         ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
476
477 #ifdef LDAP_DEBUG
478         if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
479             fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
480                     ldm->lm_msgid, ldm->lm_msgtype  );
481             ber_dump( ldm->lm_ber, 1 );
482         }
483 #endif /* LDAP_DEBUG */
484
485 #ifndef LDAP_NOCACHE
486             if ( ld->ld_cache != NULL ) {
487                 ldap_add_result_to_cache( ld, ldm );
488             }
489 #endif /* LDAP_NOCACHE */
490
491         if ( chain == NULL ) {
492             chain = ldm;
493         } else {
494             prev->lm_chain = ldm;
495         }
496         prev = ldm;
497         ldm = NULL;
498     }
499
500     /* dispose of any leftovers */
501     if ( ldm != NULL ) {
502         if ( ldm->lm_ber != NULLBER ) {
503             ber_free( ldm->lm_ber, 1 );
504         }
505         free( ldm );
506     }
507     if ( bv != NULL ) {
508         ber_bvfree( bv );
509     }
510
511     /* return chain, calling result2error if we got anything at all */
512     *res = chain;
513     return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
514 }
515 #endif /* LDAP_CONNECTIONLESS */