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