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