* request.c - sending of ldap requests; handling of referrals
*/
+#include "portable.h"
+
#ifndef lint
static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
#endif
#include <stdio.h>
-#include <string.h>
-#ifdef MACOS
#include <stdlib.h>
-#include <time.h>
-#include "macos.h"
-#else /* MACOS */
-#if defined( DOS ) || defined( _WIN32 )
-#include "msdos.h"
-#include <time.h>
-#include <stdlib.h>
-#ifdef PCNFS
-#include <tklib.h>
-#include <tk_errno.h>
-#include <bios.h>
-#endif /* PCNFS */
-#ifdef NCSA
-#include "externs.h"
-#endif /* NCSA */
-#else /* DOS */
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <errno.h>
-#ifdef _AIX
-#include <sys/select.h>
-#endif /* _AIX */
-#include "portable.h"
-#endif /* DOS */
-#endif /* MACOS */
-#ifdef VMS
-#include "ucx_select.h"
-#endif
-#include "lber.h"
-#include "ldap.h"
+
+#include <ac/errno.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
#include "ldap-int.h"
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
-
-
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
-#ifdef NEEDPROTOS
-static LDAPConn *find_connection( LDAP *ld, LDAPServer *srv, int any );
-static void use_connection( LDAP *ld, LDAPConn *lc );
-static void free_servers( LDAPServer *srvlist );
-#else /* NEEDPROTOS */
-static LDAPConn *find_connection();
-static void use_connection();
-static void free_servers();
-#endif /* NEEDPROTOS */
-#endif /* LDAP_REFERRALS || LDAP_DNS */
-
-
-#ifdef LDAP_DNS
-#ifdef NEEDPROTOS
-static LDAPServer *dn2servers( LDAP *ld, char *dn );
-#else /* NEEDPROTOS */
-static LDAPServer *dn2servers();
-#endif /* NEEDPROTOS */
-#endif /* LDAP_DNS */
-
-#ifdef LDAP_REFERRALS
-#ifdef NEEDPROTOS
-static BerElement *re_encode_request( LDAP *ld, BerElement *origber,
- int msgid, char **dnp );
-#else /* NEEDPROTOS */
-static BerElement *re_encode_request();
-#endif /* NEEDPROTOS */
-#endif /* LDAP_REFERRALS */
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
+static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
+static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
+static void free_servers LDAP_P(( LDAPServer *srvlist ));
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+
+
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
+
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
+static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
+ int msgid, char **dnp ));
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
BerElement *
-alloc_ber_with_options( LDAP *ld )
+ldap_alloc_ber_with_options( LDAP *ld )
{
BerElement *ber;
ld->ld_errno = LDAP_NO_MEMORY;
#ifdef STR_TRANSLATION
} else {
- set_ber_options( ld, ber );
+ ldap_set_ber_options( ld, ber );
#endif /* STR_TRANSLATION */
}
void
-set_ber_options( LDAP *ld, BerElement *ber )
+ldap_set_ber_options( LDAP *ld, BerElement *ber )
{
ber->ber_options = ld->ld_lberoptions;
#ifdef STR_TRANSLATION
int
-send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
+ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
BerElement *ber )
{
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
LDAPServer *servers;
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
- Debug( LDAP_DEBUG_TRACE, "send_initial_request\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );
-#if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
+#if !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) && !defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
ld->ld_errno = LDAP_SERVER_DOWN;
return( -1 );
ld->ld_errno = LDAP_SUCCESS;
return( ld->ld_msgid );
-#else /* !LDAP_REFERRALS && !LDAP_DNS */
+#else /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_DNS
- if (( ld->ld_options & LDAP_OPT_DNS ) != 0 && ldap_is_dns_dn( dn )) {
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
+ if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
+ && ldap_is_dns_dn( dn ) )
+ {
if (( servers = dn2servers( ld, dn )) == NULL ) {
ber_free( ber, 1 );
return( -1 );
}
#endif /* LDAP_DEBUG */
} else {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
/*
* use of DNS is turned off or this is an X.500 DN...
* use our default connection
*/
servers = NULL;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
- return( send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
+ return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
NULL, 0 ));
-#endif /* !LDAP_REFERRALS && !LDAP_DNS */
+#endif /* !LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS && !LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
}
-#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS ) || defined( LDAP_API_FEATURE_X_OPENLDAP_V2_DNS )
int
-send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
+ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
*parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
{
LDAPRequest *lr;
+ int incparent;
- Debug( LDAP_DEBUG_TRACE, "send_server_request\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
+ incparent = 0;
ld->ld_errno = LDAP_SUCCESS; /* optimistic */
if ( lc == NULL ) {
} else {
if (( lc = find_connection( ld, srvlist, 1 )) ==
NULL ) {
- lc = new_connection( ld, &srvlist, 0, 1, bind );
+ if ( bind && (parentreq != NULL) ) {
+ /* Remember the bind in the parent */
+ incparent = 1;
+ ++parentreq->lr_outrefcnt;
+ }
+ lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
}
free_servers( srvlist );
}
if ( ld->ld_errno == LDAP_SUCCESS ) {
ld->ld_errno = LDAP_SERVER_DOWN;
}
+ if ( incparent ) {
+ /* Forget about the bind */
+ --parentreq->lr_outrefcnt;
+ }
return( -1 );
}
if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
NULL ) {
ld->ld_errno = LDAP_NO_MEMORY;
- free_connection( ld, lc, 0, 0 );
+ ldap_free_connection( ld, lc, 0, 0 );
ber_free( ber, 1 );
+ if ( incparent ) {
+ /* Forget about the bind */
+ --parentreq->lr_outrefcnt;
+ }
return( -1 );
}
lr->lr_msgid = msgid;
lr->lr_ber = ber;
lr->lr_conn = lc;
if ( parentreq != NULL ) { /* sub-request */
- ++parentreq->lr_outrefcnt;
+ if ( !incparent ) {
+ /* Increment if we didn't do it before the bind */
+ ++parentreq->lr_outrefcnt;
+ }
lr->lr_origid = parentreq->lr_origid;
lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
lr->lr_parent = parentreq;
if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
#ifdef notyet
- extern int errno;
-
if ( errno == EWOULDBLOCK ) {
/* need to continue write later */
lr->lr_status = LDAP_REQST_WRITING;
- mark_select_write( ld, lc->lconn_sb );
+ ldap_mark_select_write( ld, lc->lconn_sb );
} else {
#else /* notyet */
ld->ld_errno = LDAP_SERVER_DOWN;
- free_request( ld, lr );
- free_connection( ld, lc, 0, 0 );
+ ldap_free_request( ld, lr );
+ ldap_free_connection( ld, lc, 0, 0 );
return( -1 );
#endif /* notyet */
#ifdef notyet
}
/* sent -- waiting for a response */
- mark_select_read( ld, lc->lconn_sb );
+ ldap_mark_select_read( ld, lc->lconn_sb );
}
ld->ld_errno = LDAP_SUCCESS;
LDAPConn *
-new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
+ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
int connect, int bind )
{
LDAPConn *lc;
}
if ( err != 0 ) {
- free_connection( ld, lc, 1, 0 );
+ ldap_free_connection( ld, lc, 1, 0 );
lc = NULL;
}
}
void
-free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
+ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
{
LDAPConn *tmplc, *prevlc;
- Debug( LDAP_DEBUG_TRACE, "free_connection\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
if ( force || --lc->lconn_refcnt <= 0 ) {
if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
- mark_select_clear( ld, lc->lconn_sb );
+ ldap_mark_select_clear( ld, lc->lconn_sb );
if ( unbind ) {
- send_unbind( ld, lc->lconn_sb );
+ ldap_send_unbind( ld, lc->lconn_sb );
}
- close_connection( lc->lconn_sb );
+ ldap_close_connection( lc->lconn_sb );
if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
free( lc->lconn_sb->sb_ber.ber_buf );
}
}
break;
}
+ prevlc = tmplc;
}
free_servers( lc->lconn_server );
if ( lc->lconn_krbinstance != NULL ) {
free( (char *)lc->lconn_sb );
}
free( lc );
- Debug( LDAP_DEBUG_TRACE, "free_connection: actually freed\n",
+ Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
0, 0, 0 );
} else {
lc->lconn_lastused = time( 0 );
- Debug( LDAP_DEBUG_TRACE, "free_connection: refcnt %d\n",
+ Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
lc->lconn_refcnt, 0, 0 );
}
}
#ifdef LDAP_DEBUG
void
-dump_connection( LDAP *ld, LDAPConn *lconns, int all )
+ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
{
LDAPConn *lc;
+ char timebuf[32];
fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
"NeedSocket" : ( lc->lconn_status ==
LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
fprintf( stderr, " last used: %s\n",
- ctime( &lc->lconn_lastused ));
+ ldap_int_ctime( &lc->lconn_lastused, timebuf ));
if ( !all ) {
break;
}
void
-dump_requests_and_responses( LDAP *ld )
+ldap_dump_requests_and_responses( LDAP *ld )
{
LDAPRequest *lr;
LDAPMessage *lm, *l;
void
-free_request( LDAP *ld, LDAPRequest *lr )
+ldap_free_request( LDAP *ld, LDAPRequest *lr )
{
LDAPRequest *tmplr, *nextlr;
- Debug( LDAP_DEBUG_TRACE, "free_request (origid %d, msgid %d)\n",
+ Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
lr->lr_origid, lr->lr_msgid, 0 );
if ( lr->lr_parent != NULL ) {
/* free all referrals (child requests) */
for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
nextlr = tmplr->lr_refnext;
- free_request( ld, tmplr );
+ ldap_free_request( ld, tmplr );
}
}
srvlist = nextsrv;
}
}
-#endif /* LDAP_REFERRALS || LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS || LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
-#ifdef LDAP_REFERRALS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
/*
* XXX merging of errors in this routine needs to be improved
*/
int
-chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
+ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
{
int rc, count, len, newdn;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
int ldapref;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
char *p, *ports, *ref, *tmpref, *refdn, *unfollowed;
LDAPRequest *origreq;
LDAPServer *srv;
BerElement *ber;
- Debug( LDAP_DEBUG_TRACE, "chase_referrals\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
ld->ld_errno = LDAP_SUCCESS; /* optimistic */
*hadrefp = 0;
/* parse out & follow referrals */
for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
ldapref = 0;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
if (( p = strchr( ref, '\n' )) != NULL ) {
*p++ = '\0';
LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
Debug( LDAP_DEBUG_TRACE,
"chasing LDAP referral: <%s>\n", ref, 0, 0 );
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
ldapref = 1;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
tmpref = ref + LDAP_LDAP_REF_STR_LEN;
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
} else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
Debug( LDAP_DEBUG_TRACE,
"chasing DX referral: <%s>\n", ref, 0, 0 );
tmpref = ref + LDAP_DX_REF_STR_LEN;
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
} else {
Debug( LDAP_DEBUG_TRACE,
"ignoring unknown referral <%s>\n", ref, 0, 0 );
- rc = append_referral( ld, &unfollowed, ref );
+ rc = ldap_append_referral( ld, &unfollowed, ref );
*hadrefp = 1;
continue;
}
return( -1 );
}
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
if ( ldapref ) {
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
if (( srv = (LDAPServer *)calloc( 1,
sizeof( LDAPServer ))) == NULL ) {
ber_free( ber, 1 );
return( -1 );
}
- if (( srv->lsrv_host = strdup( tmpref )) == NULL ) {
+ if (( srv->lsrv_host = ldap_strdup( tmpref )) == NULL ) {
free( (char *)srv );
ber_free( ber, 1 );
ld->ld_errno = LDAP_NO_MEMORY;
*ports++ = '\0';
srv->lsrv_port = atoi( ports );
} else {
- srv->lsrv_port = LDAP_PORT;
+ srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
}
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
} else {
srv = dn2servers( ld, tmpref );
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
- if ( srv != NULL && send_server_request( ld, ber, ld->ld_msgid,
+ if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
lr, srv, NULL, 1 ) >= 0 ) {
++count;
} else {
Debug( LDAP_DEBUG_ANY,
"Unable to chase referral (%s)\n",
ldap_err2string( ld->ld_errno ), 0, 0 );
- rc = append_referral( ld, &unfollowed, ref );
+ rc = ldap_append_referral( ld, &unfollowed, ref );
}
if ( !newdn && refdn != NULL ) {
int
-append_referral( LDAP *ld, char **referralsp, char *s )
+ldap_append_referral( LDAP *ld, char **referralsp, char *s )
{
int first;
return( NULL );
}
- if (( ber = alloc_ber_with_options( ld )) == NULLBER ) {
+ if (( ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
return( NULL );
}
LDAPRequest *
-find_request_by_msgid( LDAP *ld, int msgid )
+ldap_find_request_by_msgid( LDAP *ld, int msgid )
{
LDAPRequest *lr;
return( lr );
}
-#endif /* LDAP_REFERRALS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
-#ifdef LDAP_DNS
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
static LDAPServer *
dn2servers( LDAP *ld, char *dn ) /* dn can also be a domain.... */
{
domain = dn;
}
- if (( dxs = getdxbyname( domain )) == NULL ) {
+ if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
ld->ld_errno = LDAP_NO_MEMORY;
return( NULL );
}
srvlist = NULL;
for ( i = 0; dxs[ i ] != NULL; ++i ) {
- port = LDAP_PORT;
+ port = openldap_ldap_global_options.ldo_defport;
server_dn = NULL;
if ( strchr( dxs[ i ], ':' ) == NULL ) {
host = dxs[ i ];
prevsrv = srv;
/* copy in info. */
- if (( srv->lsrv_host = strdup( host )) == NULL ||
+ if (( srv->lsrv_host = ldap_strdup( host )) == NULL ||
( server_dn != NULL && ( srv->lsrv_dn =
- strdup( server_dn )) == NULL )) {
+ ldap_strdup( server_dn )) == NULL )) {
free_servers( srvlist );
srvlist = NULL;
break; /* exit loop & return */
return( srvlist );
}
-#endif /* LDAP_DNS */
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */