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