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