1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1990, 1994 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* cldap.c - synchronous, retrying interface to the cldap protocol
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
#include <ac/unistd.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include "ldap-int.h"
|
|
|
|
|
|
|
|
#define DEF_CLDAP_TIMEOUT 3
|
|
|
|
#define DEF_CLDAP_TRIES 4
|
|
|
|
|
|
|
|
|
|
|
|
struct cldap_retinfo {
|
|
|
|
int cri_maxtries;
|
|
|
|
int cri_try;
|
|
|
|
int cri_useaddr;
|
|
|
|
long cri_timeout;
|
|
|
|
};
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
static int add_addr LDAP_P((
|
|
|
|
LDAP *ld, struct sockaddr *sap ));
|
|
|
|
static int cldap_result LDAP_P((
|
|
|
|
LDAP *ld, int msgid, LDAPMessage **res,
|
|
|
|
struct cldap_retinfo *crip, char *base ));
|
|
|
|
static int cldap_parsemsg LDAP_P((
|
|
|
|
LDAP *ld, int msgid, BerElement *ber,
|
|
|
|
LDAPMessage **res, char *base ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cldap_open - initialize and connect to an ldap server. A magic cookie to
|
|
|
|
* be used for future communication is returned on success, NULL on failure.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* LDAP *ld;
|
|
|
|
* ld = cldap_open( hostname, port );
|
|
|
|
*/
|
|
|
|
|
|
|
|
LDAP *
|
|
|
|
cldap_open( char *host, int port )
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
unsigned long address;
|
|
|
|
struct sockaddr_in sock;
|
|
|
|
struct hostent *hp;
|
|
|
|
LDAP *ld;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
1999-01-15 22:49:03 +08:00
|
|
|
/* buffers for ldap_pvt_gethostbyname_a ... */
|
1998-12-19 09:27:20 +08:00
|
|
|
struct hostent he_buf;
|
|
|
|
int local_h_errno;
|
|
|
|
char *ha_buf=NULL;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
#define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
|
1998-12-19 09:27:20 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( (s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
sock.sin_addr.s_addr = 0;
|
|
|
|
sock.sin_family = AF_INET;
|
|
|
|
sock.sin_port = 0;
|
|
|
|
if ( bind(s, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
|
1999-03-29 13:15:59 +08:00
|
|
|
tcp_close( s );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
if (( ld = ldap_init( host, port )) == NULL ) {
|
1999-03-29 13:15:59 +08:00
|
|
|
tcp_close( s );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
1999-03-03 04:23:14 +08:00
|
|
|
|
|
|
|
ld->ld_cldapnaddr = 0;
|
|
|
|
ld->ld_cldapaddrs = NULL;
|
1998-11-11 07:37:30 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
if (ber_pvt_sb_set_io( &(ld->ld_sb), &ber_pvt_sb_io_udp, NULL )<0) {
|
1999-03-03 04:23:14 +08:00
|
|
|
ldap_ld_free(ld, 1 );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-08 02:45:52 +08:00
|
|
|
ld->ld_version = LDAP_VERSION2;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sock.sin_family = AF_INET;
|
|
|
|
sock.sin_port = htons( port );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'host' may be a space-separated list.
|
|
|
|
*/
|
|
|
|
if ( host != NULL ) {
|
|
|
|
for ( ; host != NULL; host = p ) {
|
|
|
|
if (( p = strchr( host, ' ' )) != NULL ) {
|
|
|
|
for (*p++ = '\0'; *p == ' '; p++) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-12 10:24:43 +08:00
|
|
|
address = inet_addr( host );
|
|
|
|
/* This was just a test for -1 until OSF1 let inet_addr return
|
|
|
|
unsigned int, which is narrower than 'unsigned long address' */
|
|
|
|
if ( address == 0xffffffff || address == (unsigned long) -1 ) {
|
1999-01-15 22:49:03 +08:00
|
|
|
if ((ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
|
1998-12-19 09:27:20 +08:00
|
|
|
&hp,&local_h_errno)<0) ||
|
|
|
|
(hp==NULL)) {
|
|
|
|
errno = EHOSTUNREACH;
|
|
|
|
continue;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0; hp->h_addr_list[ i ] != 0; ++i ) {
|
|
|
|
SAFEMEMCPY( (char *)&sock.sin_addr.s_addr,
|
|
|
|
(char *)hp->h_addr_list[ i ],
|
|
|
|
sizeof(sock.sin_addr.s_addr));
|
|
|
|
if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
|
1998-11-11 07:37:30 +08:00
|
|
|
ldap_ld_free( ld, 1 );
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sock.sin_addr.s_addr = address;
|
|
|
|
if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
|
1998-11-11 07:37:30 +08:00
|
|
|
ldap_ld_free( ld, 1 );
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ld->ld_host == NULL ) {
|
1999-06-03 06:28:22 +08:00
|
|
|
ld->ld_host = LDAP_STRDUP( host );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
address = INADDR_LOOPBACK;
|
|
|
|
sock.sin_addr.s_addr = htonl( address );
|
|
|
|
if ( add_addr( ld, (struct sockaddr *)&sock ) < 0 ) {
|
1998-11-11 07:37:30 +08:00
|
|
|
ldap_ld_free( ld, 1 );
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-03 04:23:14 +08:00
|
|
|
if ( ld->ld_cldapaddrs == NULL
|
1998-08-09 11:04:12 +08:00
|
|
|
|| ( ld->ld_defconn = ldap_new_connection( ld, NULL, 1,0,0 )) == NULL
|
1998-08-09 08:43:13 +08:00
|
|
|
) {
|
1998-11-11 07:37:30 +08:00
|
|
|
ldap_ld_free( ld, 0 );
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_pvt_sb_udp_set_dst( &ld->ld_sb, ld->ld_cldapaddrs[0] );
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
cldap_setretryinfo( ld, 0, 0 );
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
putchar( '\n' );
|
1999-03-03 04:23:14 +08:00
|
|
|
for ( i = 0; i < ld->ld_cldapnaddr; ++i ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "end of cldap_open address %d is %s\n",
|
|
|
|
i, inet_ntoa( ((struct sockaddr_in *)
|
1999-03-03 04:23:14 +08:00
|
|
|
ld->ld_cldapaddrs[ i ])->sin_addr ), 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( ld );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
#undef DO_RETURN
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
cldap_close( LDAP *ld )
|
|
|
|
{
|
|
|
|
ldap_ld_free( ld, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
cldap_setretryinfo( LDAP *ld, int tries, int timeout )
|
|
|
|
{
|
|
|
|
ld->ld_cldaptries = ( tries <= 0 ) ? DEF_CLDAP_TRIES : tries;
|
|
|
|
ld->ld_cldaptimeout = ( timeout <= 0 ) ? DEF_CLDAP_TIMEOUT : timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
cldap_search_s( LDAP *ld, char *base, int scope, char *filter, char **attrs,
|
|
|
|
int attrsonly, LDAPMessage **res, char *logdn )
|
|
|
|
{
|
|
|
|
int ret, msgid;
|
|
|
|
struct cldap_retinfo cri;
|
|
|
|
|
|
|
|
*res = NULLMSG;
|
|
|
|
|
|
|
|
(void) memset( &cri, 0, sizeof( cri ));
|
|
|
|
|
|
|
|
if ( logdn != NULL ) {
|
|
|
|
ld->ld_cldapdn = logdn;
|
|
|
|
} else if ( ld->ld_cldapdn == NULL ) {
|
|
|
|
ld->ld_cldapdn = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
if ( cri.cri_try != 0 ) {
|
|
|
|
--ld->ld_msgid; /* use same id as before */
|
|
|
|
}
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_pvt_sb_udp_set_dst( &(ld->ld_sb),
|
1999-03-03 04:23:14 +08:00
|
|
|
ld->ld_cldapaddrs[ cri.cri_useaddr ] );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_search_s try %d (to %s)\n",
|
|
|
|
cri.cri_try, inet_ntoa( ((struct sockaddr_in *)
|
1999-03-03 04:23:14 +08:00
|
|
|
ld->ld_cldapaddrs[ cri.cri_useaddr ])->sin_addr), 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( (msgid = ldap_search( ld, base, scope, filter, attrs,
|
|
|
|
attrsonly )) == -1 ) {
|
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifndef LDAP_NOCACHE
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ld->ld_cache != NULL && ld->ld_responses != NULL ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_search_s res from cache\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
*res = ld->ld_responses;
|
|
|
|
ld->ld_responses = ld->ld_responses->lm_next;
|
|
|
|
return( ldap_result2error( ld, *res, 0 ));
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* LDAP_NOCACHE */
|
1998-08-09 08:43:13 +08:00
|
|
|
ret = cldap_result( ld, msgid, res, &cri, base );
|
|
|
|
} while (ret == -1);
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_addr( LDAP *ld, struct sockaddr *sap )
|
|
|
|
{
|
|
|
|
struct sockaddr *newsap, **addrs;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( newsap = (struct sockaddr *)LDAP_MALLOC( sizeof( struct sockaddr )))
|
1998-08-09 08:43:13 +08:00
|
|
|
== NULL ) {
|
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
addrs = (struct sockaddr **)LDAP_REALLOC( ld->ld_cldapaddrs,
|
1999-03-03 04:23:14 +08:00
|
|
|
( ld->ld_cldapnaddr + 1 ) * sizeof(struct sockaddr *));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( addrs == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( newsap );
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_NO_MEMORY;
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
SAFEMEMCPY( (char *)newsap, (char *)sap, sizeof( struct sockaddr ));
|
1999-03-03 04:23:14 +08:00
|
|
|
addrs[ ld->ld_cldapnaddr++ ] = newsap;
|
|
|
|
ld->ld_cldapaddrs = (void **)addrs;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cldap_result( LDAP *ld, int msgid, LDAPMessage **res,
|
|
|
|
struct cldap_retinfo *crip, char *base )
|
|
|
|
{
|
|
|
|
Sockbuf *sb = &ld->ld_sb;
|
|
|
|
BerElement ber;
|
|
|
|
char *logdn;
|
1999-06-19 07:53:05 +08:00
|
|
|
int ret, fromaddr, i;
|
|
|
|
ber_int_t id;
|
1998-08-09 08:43:13 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
fromaddr = -1;
|
|
|
|
|
|
|
|
if ( crip->cri_try == 0 ) {
|
1999-03-03 04:23:14 +08:00
|
|
|
crip->cri_maxtries = ld->ld_cldaptries * ld->ld_cldapnaddr;
|
1998-08-09 08:43:13 +08:00
|
|
|
crip->cri_timeout = ld->ld_cldaptimeout;
|
|
|
|
crip->cri_useaddr = 0;
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_result tries %d timeout %d\n",
|
|
|
|
ld->ld_cldaptries, ld->ld_cldaptimeout, 0 );
|
|
|
|
}
|
|
|
|
|
1999-03-03 04:23:14 +08:00
|
|
|
if ((tv.tv_sec = crip->cri_timeout / ld->ld_cldapnaddr) < 1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
}
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
"cldap_result waiting up to %ld seconds for a response\n",
|
|
|
|
(long) tv.tv_sec, 0, 0 );
|
1998-10-26 10:38:39 +08:00
|
|
|
ber_init_w_nullc( &ber, 0 );
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_set_ber_options( ld, &ber );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( cldap_getmsg( ld, &tv, &ber ) == -1 ) {
|
|
|
|
ret = ld->ld_errno;
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_getmsg returned -1 (%d)\n",
|
|
|
|
ret, 0, 0 );
|
|
|
|
} else if ( ld->ld_errno == LDAP_TIMEOUT ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"cldap_result timed out\n", 0, 0, 0 );
|
|
|
|
/*
|
|
|
|
* It timed out; is it time to give up?
|
|
|
|
*/
|
|
|
|
if ( ++crip->cri_try >= crip->cri_maxtries ) {
|
|
|
|
ret = LDAP_TIMEOUT;
|
|
|
|
--crip->cri_try;
|
|
|
|
} else {
|
1999-03-03 04:23:14 +08:00
|
|
|
if ( ++crip->cri_useaddr >= ld->ld_cldapnaddr ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* new round: reset address to first one and
|
|
|
|
* double the timeout
|
|
|
|
*/
|
|
|
|
crip->cri_useaddr = 0;
|
|
|
|
crip->cri_timeout <<= 1;
|
|
|
|
}
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Got a response. It should look like:
|
|
|
|
* { msgid, logdn, { searchresponse...}}
|
|
|
|
*/
|
|
|
|
logdn = NULL;
|
|
|
|
|
|
|
|
if ( ber_scanf( &ber, "ia", &id, &logdn ) == LBER_ERROR ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ber.ber_buf ); /* gack! */
|
1998-08-09 08:43:13 +08:00
|
|
|
ret = LDAP_DECODING_ERROR;
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"cldap_result: ber_scanf returned LBER_ERROR (%d)\n",
|
|
|
|
ret, 0, 0 );
|
|
|
|
} else if ( id != msgid ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ber.ber_buf ); /* gack! */
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"cldap_result: looking for msgid %d; got %d\n",
|
|
|
|
msgid, id, 0 );
|
|
|
|
ret = -1; /* ignore and keep looking */
|
|
|
|
} else {
|
1999-03-03 04:23:14 +08:00
|
|
|
struct sockaddr_in * src;
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* got a result: determine which server it came from
|
|
|
|
* decode into ldap message chain
|
|
|
|
*/
|
1999-05-19 09:12:33 +08:00
|
|
|
src = (struct sockaddr_in *) ber_pvt_sb_udp_get_src( sb );
|
1999-03-03 04:23:14 +08:00
|
|
|
|
|
|
|
for ( fromaddr = 0; fromaddr < ld->ld_cldapnaddr; ++fromaddr ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( memcmp( &((struct sockaddr_in *)
|
1999-03-03 04:23:14 +08:00
|
|
|
ld->ld_cldapaddrs[ fromaddr ])->sin_addr,
|
|
|
|
&(src->sin_addr),
|
1998-08-09 08:43:13 +08:00
|
|
|
sizeof( struct in_addr )) == 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = cldap_parsemsg( ld, msgid, &ber, res, base );
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ber.ber_buf ); /* gack! */
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"cldap_result got result (%d)\n", ret, 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( logdn != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( logdn );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we are giving up (successfully or otherwise) then
|
|
|
|
* abandon any outstanding requests.
|
|
|
|
*/
|
|
|
|
if ( ret != -1 ) {
|
|
|
|
i = crip->cri_try;
|
1999-03-03 04:23:14 +08:00
|
|
|
if ( i >= ld->ld_cldapnaddr ) {
|
|
|
|
i = ld->ld_cldapnaddr - 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( ; i >= 0; --i ) {
|
|
|
|
if ( i == fromaddr ) {
|
|
|
|
continue;
|
|
|
|
}
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_pvt_sb_udp_set_dst( sb, ld->ld_cldapaddrs[i] );
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_result abandoning id %d (to %s)\n",
|
|
|
|
msgid, inet_ntoa( ((struct sockaddr_in *)
|
1999-03-03 04:23:14 +08:00
|
|
|
ld->ld_cldapaddrs[i])->sin_addr ), 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
(void) ldap_abandon( ld, msgid );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ld->ld_errno = ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
cldap_parsemsg( LDAP *ld, int msgid, BerElement *ber,
|
|
|
|
LDAPMessage **res, char *base )
|
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
|
|
|
int baselen, slen;
|
|
|
|
ber_tag_t rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
char *dn, *p, *cookie;
|
|
|
|
LDAPMessage *chain, *prev, *ldm;
|
|
|
|
struct berval *bv;
|
|
|
|
|
|
|
|
rc = LDAP_DECODING_ERROR; /* pessimistic */
|
|
|
|
ldm = chain = prev = NULLMSG;
|
|
|
|
baselen = ( base == NULL ) ? 0 : strlen( base );
|
|
|
|
bv = NULL;
|
|
|
|
|
|
|
|
for ( tag = ber_first_element( ber, &len, &cookie );
|
|
|
|
tag != LBER_DEFAULT && rc != LDAP_SUCCESS;
|
|
|
|
tag = ber_next_element( ber, &len, cookie )) {
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( ldm = (LDAPMessage *)LDAP_CALLOC( 1, sizeof(LDAPMessage)))
|
1998-08-09 11:04:12 +08:00
|
|
|
== NULL || ( ldm->lm_ber = ldap_alloc_ber_with_options( ld ))
|
1999-06-19 07:53:05 +08:00
|
|
|
== NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = LDAP_NO_MEMORY;
|
|
|
|
break; /* return w/error*/
|
|
|
|
}
|
|
|
|
ldm->lm_msgid = msgid;
|
|
|
|
ldm->lm_msgtype = tag;
|
|
|
|
|
|
|
|
if ( tag == LDAP_RES_SEARCH_RESULT ) {
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got search result\n",
|
|
|
|
0, 0, 0 );
|
|
|
|
|
|
|
|
if ( ber_get_stringal( ber, &bv ) == LBER_DEFAULT ) {
|
|
|
|
break; /* return w/error */
|
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( ber_printf( ldm->lm_ber, "tO", tag, bv ) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
break; /* return w/error */
|
|
|
|
}
|
|
|
|
ber_bvfree( bv );
|
|
|
|
bv = NULL;
|
|
|
|
rc = LDAP_SUCCESS;
|
|
|
|
|
|
|
|
} else if ( tag == LDAP_RES_SEARCH_ENTRY ) {
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( ber_scanf( ber, "{aO" /*}*/, &dn, &bv ) == LBER_ERROR ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
break; /* return w/error */
|
|
|
|
}
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg entry %s\n", dn, 0, 0 );
|
|
|
|
if ( dn != NULL && *(dn + ( slen = strlen(dn)) - 1) == '*' &&
|
|
|
|
baselen > 0 ) {
|
|
|
|
/*
|
|
|
|
* substitute original searchbase for trailing '*'
|
|
|
|
*/
|
1999-05-29 09:19:14 +08:00
|
|
|
if (( p = (char *)LDAP_MALLOC( slen + baselen )) == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = LDAP_NO_MEMORY;
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
break; /* return w/error */
|
|
|
|
}
|
|
|
|
strcpy( p, dn );
|
|
|
|
strcpy( p + slen - 1, base );
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
dn = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ber_printf( ldm->lm_ber, "t{so}", tag, dn, bv->bv_val,
|
|
|
|
bv->bv_len ) == -1 ) {
|
|
|
|
break; /* return w/error */
|
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( dn );
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_bvfree( bv );
|
|
|
|
bv = NULL;
|
|
|
|
|
1998-12-24 14:00:53 +08:00
|
|
|
#ifdef notyet
|
|
|
|
} else if ( tag == LDAP_RES_SEARCH_REFERENCE ) {
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "cldap_parsemsg got unknown tag %lu\n",
|
1998-08-09 08:43:13 +08:00
|
|
|
tag, 0, 0 );
|
|
|
|
rc = LDAP_PROTOCOL_ERROR;
|
|
|
|
break; /* return w/error */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset message ber so we can read from it later. Gack! */
|
|
|
|
ldm->lm_ber->ber_end = ldm->lm_ber->ber_ptr;
|
|
|
|
ldm->lm_ber->ber_ptr = ldm->lm_ber->ber_buf;
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
|
|
|
|
fprintf( stderr, "cldap_parsemsg add message id %d type %d:\n",
|
|
|
|
ldm->lm_msgid, ldm->lm_msgtype );
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_dump( LDAP_DEBUG_BER, ldap_debug, ldm->lm_ber, 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifndef LDAP_NOCACHE
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ld->ld_cache != NULL ) {
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_add_result_to_cache( ld, ldm );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* LDAP_NOCACHE */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( chain == NULL ) {
|
|
|
|
chain = ldm;
|
|
|
|
} else {
|
|
|
|
prev->lm_chain = ldm;
|
|
|
|
}
|
|
|
|
prev = ldm;
|
|
|
|
ldm = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dispose of any leftovers */
|
|
|
|
if ( ldm != NULL ) {
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( ldm->lm_ber != NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_free( ldm->lm_ber, 1 );
|
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( ldm );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
if ( bv != NULL ) {
|
|
|
|
ber_bvfree( bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return chain, calling result2error if we got anything at all */
|
|
|
|
*res = chain;
|
|
|
|
return(( *res == NULLMSG ) ? rc : ldap_result2error( ld, *res, 0 ));
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
#endif /* LDAP_CONNECTIONLESS */
|