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