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