+/* $OpenLDAP$ */
/*
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/* Portions
* Copyright (c) 1995 Regents of the University of Michigan.
* All rights reserved.
*
* open.c
*/
-#ifndef lint
-static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
#include <stdio.h>
-#include <string.h>
-
-#ifdef MACOS
-#include <stdlib.h>
-#include "macos.h"
-#endif /* MACOS */
-
-#if defined( DOS ) || defined( _WIN32 )
-#include "msdos.h"
-#include <stdlib.h>
-#endif /* DOS */
-
-#if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#ifndef VMS
-#include <sys/param.h>
-#endif
-#include <netinet/in.h>
-#endif
-#include "lber.h"
-#include "ldap.h"
-#include "ldap-int.h"
+#include <limits.h>
-#ifdef LDAP_DEBUG
-int ldap_debug;
-#endif
+#include <ac/stdlib.h>
-#ifndef INADDR_LOOPBACK
-#define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
-#endif
+#include <ac/param.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
-#ifndef MAXHOSTNAMELEN
-#define MAXHOSTNAMELEN 64
-#endif
+#include "ldap-int.h"
+
+int ldap_open_defconn( LDAP *ld )
+{
+ ld->ld_defconn = ldap_new_connection( ld,
+ ld->ld_options.ldo_defludp, 1, 1, NULL );
+ if( ld->ld_defconn == NULL ) {
+ ld->ld_errno = LDAP_SERVER_DOWN;
+ return -1;
+ }
+
+ ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
+ return 0;
+}
/*
* ldap_open - initialize and connect to an ldap server. A magic cookie to
*/
LDAP *
-ldap_open( char *host, int port )
+ldap_open( LDAP_CONST char *host, int port )
{
+ int rc;
LDAP *ld;
-#ifdef LDAP_REFERRALS
- LDAPServer *srv;
-#endif /* LDAP_REFERRALS */
- Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
+ host, port, 0 );
- if (( ld = ldap_init( host, port )) == NULL ) {
+ ld = ldap_init( host, port );
+ if ( ld == NULL ) {
return( NULL );
}
-#ifdef LDAP_REFERRALS
- if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
- NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
- strdup( ld->ld_defhost )) == NULL )) {
- ldap_ld_free( ld, 0 );
- return( NULL );
+ rc = ldap_open_defconn( ld );
+
+ if( rc < 0 ) {
+ ldap_ld_free( ld, 0, NULL, NULL );
+ ld = NULL;
}
- srv->lsrv_port = ld->ld_defport;
- if (( ld->ld_defconn = new_connection( ld, &srv, 1,1,0 )) == NULL ) {
- if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
- free( (char *)srv );
- ldap_ld_free( ld, 0 );
- return( NULL );
+ Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
+ ld == NULL ? "succeeded" : "failed", 0, 0 );
+
+ return ld;
+}
+
+
+
+int
+ldap_create( LDAP **ldp )
+{
+ LDAP *ld;
+ struct ldapoptions *gopts;
+
+ *ldp = NULL;
+ /* Get pointer to global option structure */
+ if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
+ return LDAP_NO_MEMORY;
}
- ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
-#else /* LDAP_REFERRALS */
- if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
- ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
- ldap_ld_free( ld, 0 );
- return( NULL );
+ /* Initialize the global options, if not already done. */
+ if( gopts->ldo_valid != LDAP_INITIALIZED ) {
+ ldap_int_initialize(gopts, NULL);
+ if ( gopts->ldo_valid != LDAP_INITIALIZED )
+ return LDAP_LOCAL_ERROR;
}
-#endif /* LDAP_REFERRALS */
- Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
- ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
- return( ld );
-}
+ if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
+ return( LDAP_NO_MEMORY );
+ }
+
+ /* copy the global options */
+ AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
+
+ ld->ld_valid = LDAP_VALID_SESSION;
+
+ /* but not pointers to malloc'ed items */
+ ld->ld_options.ldo_sctrls = NULL;
+ ld->ld_options.ldo_cctrls = NULL;
+
+#ifdef HAVE_CYRUS_SASL
+ ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
+ ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
+ ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
+ ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
+ ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
+ ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
+ ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
+ ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
+#endif
+ ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
+
+ if ( ld->ld_options.ldo_defludp == NULL ) {
+ LDAP_FREE( (char*)ld );
+ return LDAP_NO_MEMORY;
+ }
+
+ if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
+ ldap_free_urllist( ld->ld_options.ldo_defludp );
+ LDAP_FREE( (char*) ld );
+ return LDAP_NO_MEMORY;
+ }
+
+ ld->ld_lberoptions = LBER_USE_DER;
+
+ ld->ld_sb = ber_sockbuf_alloc( );
+ if ( ld->ld_sb == NULL ) {
+ ldap_free_urllist( ld->ld_options.ldo_defludp );
+ LDAP_FREE( (char*) ld );
+ return LDAP_NO_MEMORY;
+ }
+
+ *ldp = ld;
+ return LDAP_SUCCESS;
+}
/*
* ldap_init - initialize the LDAP library. A magic cookie to be used for
* future communication is returned on success, NULL on failure.
- * "defhost" may be a space-separated list of hosts or IP addresses
+ * "host" may be a space-separated list of hosts or IP addresses
*
* Example:
* LDAP *ld;
- * ld = ldap_open( default_hostname, default_port );
+ * ld = ldap_init( host, port );
*/
LDAP *
-ldap_init( char *defhost, int defport )
+ldap_init( LDAP_CONST char *defhost, int defport )
{
- LDAP *ld;
+ LDAP *ld;
+ int rc;
- Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
+ rc = ldap_create(&ld);
+ if ( rc != LDAP_SUCCESS )
+ return NULL;
+ if (defport != 0)
+ ld->ld_options.ldo_defport = defport;
- if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
- return( NULL );
+ if (defhost != NULL) {
+ rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
+ if ( rc != LDAP_SUCCESS ) {
+ ldap_ld_free(ld, 1, NULL, NULL);
+ return NULL;
+ }
}
-#ifdef LDAP_REFERRALS
- if (( ld->ld_selectinfo = new_select_info()) == NULL ) {
- free( (char*)ld );
- return( NULL );
- }
- ld->ld_options = LDAP_OPT_REFERRALS;
-#endif /* LDAP_REFERRALS */
+ return( ld );
+}
- if ( defhost != NULL &&
- ( ld->ld_defhost = strdup( defhost )) == NULL ) {
-#ifdef LDAP_REFERRALS
- free_select_info( ld->ld_selectinfo );
-#endif /* LDAP_REFERRALS */
- free( (char*)ld );
- return( NULL );
+
+int
+ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
+{
+ int rc;
+ LDAP *ld;
+
+ *ldp = NULL;
+ rc = ldap_create(&ld);
+ if ( rc != LDAP_SUCCESS )
+ return rc;
+
+ if (url != NULL) {
+ rc = ldap_set_option(ld, LDAP_OPT_URI, url);
+ if ( rc != LDAP_SUCCESS ) {
+ ldap_ld_free(ld, 1, NULL, NULL);
+ return rc;
+ }
+#ifdef LDAP_CONNECTIONLESS
+ if (ldap_is_ldapc_url(url))
+ LDAP_IS_UDP(ld) = 1;
+#endif
}
+ *ldp = ld;
+ return LDAP_SUCCESS;
+}
- ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
- ld->ld_version = LDAP_VERSION;
- ld->ld_lberoptions = LBER_USE_DER;
- ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
+int
+ldap_int_open_connection(
+ LDAP *ld,
+ LDAPConn *conn,
+ LDAPURLDesc *srv,
+ int async )
+{
+ int rc = -1;
+#ifdef HAVE_CYRUS_SASL
+ char *sasl_host = NULL;
+ int sasl_ssf = 0;
+#endif
+ char *host;
+ int port, proto;
+ long addr;
-#if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
- ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
-#if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
- ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
-#endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
-#endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
+ Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
- return( ld );
-}
+ switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
+ case LDAP_PROTO_TCP:
+ port = srv->lud_port;
+ addr = 0;
+ if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
+ host = NULL;
+ addr = htonl( INADDR_LOOPBACK );
+ } else {
+ host = srv->lud_host;
+ }
-int
-open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
- char **krbinstancep, int async )
-{
- int rc, port;
- char *p, *q, *r;
- char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
-
- Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
-
- defport = htons( defport );
-
- if ( host != NULL ) {
- for ( p = host; p != NULL && *p != '\0'; p = q ) {
- if (( q = strchr( p, ' ' )) != NULL ) {
- strncpy( hostname, p, q - p );
- hostname[ q - p ] = '\0';
- curhost = hostname;
- while ( *q == ' ' ) {
- ++q;
+ if( !port ) {
+ if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
+ port = LDAPS_PORT;
+ } else {
+ port = LDAP_PORT;
}
- } else {
- curhost = p; /* avoid copy if possible */
- q = NULL;
}
- if (( r = strchr( curhost, ':' )) != NULL ) {
- if ( curhost != hostname ) {
- strcpy( hostname, curhost ); /* now copy */
- r = hostname + ( r - curhost );
- curhost = hostname;
- }
- *r++ = '\0';
- port = htons( (short)atoi( r ));
+ rc = ldap_connect_to_host( ld, conn->lconn_sb,
+ proto, host, addr, port, async );
+
+ if ( rc == -1 ) return rc;
+
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
+#endif
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+
+#ifdef HAVE_CYRUS_SASL
+ sasl_host = ldap_host_connected_to( conn->lconn_sb );
+#endif
+ break;
+#ifdef LDAP_CONNECTIONLESS
+
+ case LDAP_PROTO_UDP:
+ port = srv->lud_port;
+
+ addr = 0;
+ if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
+ host = NULL;
+ addr = htonl( INADDR_LOOPBACK );
} else {
- port = defport;
+ host = srv->lud_host;
}
- if (( rc = connect_to_host( sb, curhost, 0L,
- port, async )) != -1 ) {
- break;
- }
- }
- } else {
- rc = connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
- defport, async );
+ if( !port ) port = LDAP_PORT;
+
+ LDAP_IS_UDP(ld) = 1;
+ rc = ldap_connect_to_host( ld, conn->lconn_sb,
+ proto, host, addr, port, async );
+
+ if ( rc == -1 ) return rc;
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
+#endif
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+ break;
+#endif
+ case LDAP_PROTO_IPC:
+#ifdef LDAP_PF_LOCAL
+ /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
+ rc = ldap_connect_to_path( ld, conn->lconn_sb,
+ srv->lud_host, async );
+ if ( rc == -1 ) return rc;
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
+#endif
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+
+#ifdef HAVE_CYRUS_SASL
+ sasl_host = ldap_host_connected_to( conn->lconn_sb );
+ sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
+#endif
+ break;
+#endif /* LDAP_PF_LOCAL */
+ default:
+ return -1;
+ break;
}
- if ( rc == -1 ) {
- return( rc );
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
+ INT_MAX, (void *)"ldap_" );
+#endif
+
+#ifdef LDAP_CONNECTIONLESS
+ if( proto == LDAP_PROTO_UDP )
+ return 0;
+#endif
+
+#ifdef HAVE_CYRUS_SASL
+ /* establish Cyrus SASL context prior to starting TLS so
+ that SASL EXTERNAL might be used */
+ if( sasl_host != NULL ) {
+ ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
+ LDAP_FREE( sasl_host );
}
+#endif
+
+#ifdef HAVE_TLS
+ if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
+ strcmp( srv->lud_scheme, "ldaps" ) == 0 )
+ {
+ ++conn->lconn_refcnt; /* avoid premature free */
+
+ rc = ldap_int_tls_start( ld, conn, srv );
+
+ --conn->lconn_refcnt;
+
+ if (rc != LDAP_SUCCESS) {
+ return -1;
+ }
+ }
+#endif
+
+#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
+ if ( conn->lconn_krbinstance == NULL ) {
+ char *c;
+ conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
- if ( krbinstancep != NULL ) {
-#ifdef KERBEROS
- if (( *krbinstancep = host_connected_to( sb )) != NULL &&
- ( p = strchr( *krbinstancep, '.' )) != NULL ) {
- *p = '\0';
+ if( conn->lconn_krbinstance != NULL &&
+ ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
+ *c = '\0';
}
-#else /* KERBEROS */
- krbinstancep = NULL;
-#endif /* KERBEROS */
}
+#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
return( 0 );
}
+
+
+int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
+{
+ int rc;
+ LDAPConn *c;
+ LDAPRequest *lr;
+
+ rc = ldap_create( ldp );
+ if( rc != LDAP_SUCCESS ) {
+ *ldp = NULL;
+ return( rc );
+ }
+
+ /* Make it appear that a search request, msgid 0, was sent */
+ lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
+ if( lr == NULL ) {
+ ldap_unbind( *ldp );
+ *ldp = NULL;
+ return( LDAP_NO_MEMORY );
+ }
+ memset(lr, 0, sizeof( LDAPRequest ));
+ lr->lr_msgid = 0;
+ lr->lr_status = LDAP_REQST_INPROGRESS;
+ lr->lr_res_errno = LDAP_SUCCESS;
+ (*ldp)->ld_requests = lr;
+
+ /* Attach the passed socket as the *LDAP's connection */
+ c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
+ if( c == NULL ) {
+ ldap_unbind( *ldp );
+ *ldp = NULL;
+ return( LDAP_NO_MEMORY );
+ }
+ ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
+#ifdef LDAP_DEBUG
+ ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
+ LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
+#endif
+ ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
+ LBER_SBIOD_LEVEL_PROVIDER, NULL );
+ (*ldp)->ld_defconn = c;
+
+ /* Add the connection to the *LDAP's select pool */
+ ldap_mark_select_read( *ldp, c->lconn_sb );
+ ldap_mark_select_write( *ldp, c->lconn_sb );
+
+ /* Make this connection an LDAP V3 protocol connection */
+ rc = LDAP_VERSION3;
+ ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
+
+ return( LDAP_SUCCESS );
+}
+/* $OpenLDAP$ */
/*
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
+/* Portions
* Copyright (c) 1995 Regents of the University of Michigan.
* All rights reserved.
*
* os-ip.c -- platform-specific TCP & UDP related code
*/
-#ifndef lint
-static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
-#endif
+#include "portable.h"
#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#ifdef _WIN32
+#include <ac/stdlib.h>
+
+#include <ac/errno.h>
+#include <ac/socket.h>
+#include <ac/string.h>
+#include <ac/time.h>
+#include <ac/unistd.h>
+
+#ifdef HAVE_IO_H
#include <io.h>
-#include "msdos.h"
-#else /* _WIN32 */
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#endif /* _WIN32 */
-#ifdef _AIX
-#include <sys/select.h>
-#endif /* _AIX */
-#ifdef VMS
-#include "ucx_select.h"
-#endif /* VMS */
-#include "portable.h"
-#include "lber.h"
-#include "ldap.h"
+#endif /* HAVE_IO_H */
-#ifdef LDAP_REFERRALS
-#ifdef USE_SYSCONF
-#include <unistd.h>
-#endif /* USE_SYSCONF */
-#ifdef notyet
-#ifdef NEED_FILIO
-#include <sys/filio.h>
-#else /* NEED_FILIO */
-#include <sys/ioctl.h>
-#endif /* NEED_FILIO */
-#endif /* notyet */
-#endif /* LDAP_REFERRALS */
-
-#ifdef MACOS
-#define tcp_close( s ) tcpclose( s )
-#else /* MACOS */
-#ifdef DOS
-#ifdef PCNFS
-#define tcp_close( s ) close( s )
-#endif /* PCNFS */
-#ifdef NCSA
-#define tcp_close( s ) netclose( s ); netshut()
-#endif /* NCSA */
-#ifdef WINSOCK
-#define tcp_close( s ) closesocket( s ); WSACleanup();
-#endif /* WINSOCK */
-#else /* DOS */
-#define tcp_close( s ) close( s )
-#endif /* DOS */
-#endif /* MACOS */
+#include "ldap-int.h"
+int ldap_int_tblsize = 0;
-int
-connect_to_host( Sockbuf *sb, char *host, unsigned long address,
- int port, int async )
/*
- * if host == NULL, connect using address
- * "address" and "port" must be in network byte order
- * zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
- * async is only used ifdef LDAP_REFERRALS (non-0 means don't wait for connect)
- * XXX async is not used yet!
+ * nonblock connect code
+ * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
+ *
+ * Copyright 1999, Lars Uffmann, All rights reserved.
+ * This software is not subject to any license of my employer
+ * mediaWays GmbH.
+ *
+ * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ *
+ * Read about the rationale in ldap_connect_timeout:
+ * ftp://koobera.math.uic.edu/www/docs/connect.html.
*/
+
+#define osip_debug(ld,fmt,arg1,arg2,arg3) \
+do { \
+ ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
+} while(0)
+
+static void
+ldap_pvt_set_errno(int err)
{
- int rc, i, s, connected, use_hp;
- struct sockaddr_in sin;
- struct hostent *hp;
-#ifdef notyet
-#ifdef LDAP_REFERRALS
- int status; /* for ioctl call */
-#endif /* LDAP_REFERRALS */
-#endif /* notyet */
+ errno = err;
+}
- Debug( LDAP_DEBUG_TRACE, "connect_to_host: %s:%d\n",
- ( host == NULL ) ? "(by address)" : host, ntohs( port ), 0 );
+int
+ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
+{
+ struct timeval *new;
- connected = use_hp = 0;
+ assert( dest != NULL );
- if ( host != NULL && ( address = inet_addr( host )) == -1 ) {
- if ( (hp = gethostbyname( host )) == NULL ) {
- errno = EHOSTUNREACH; /* not exactly right, but... */
- return( -1 );
- }
- use_hp = 1;
+ if (src == NULL) {
+ *dest = NULL;
+ return 0;
+ }
+
+ new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
+
+ if( new == NULL ) {
+ *dest = NULL;
+ return 1;
}
- rc = -1;
- for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
- if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
- return( -1 );
+ AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
+
+ *dest = new;
+ return 0;
+}
+
+static int
+ldap_pvt_ndelay_on(LDAP *ld, int fd)
+{
+ osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
+ return ber_pvt_socket_set_nonblock( fd, 1 );
+}
+
+static int
+ldap_pvt_ndelay_off(LDAP *ld, int fd)
+{
+ osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
+ return ber_pvt_socket_set_nonblock( fd, 0 );
+}
+
+static ber_socket_t
+ldap_int_socket(LDAP *ld, int family, int type )
+{
+ ber_socket_t s = socket(family, type, 0);
+ osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
+ return ( s );
+}
+
+static int
+ldap_pvt_close_socket(LDAP *ld, int s)
+{
+ osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
+ return tcp_close(s);
+}
+
+static int
+ldap_int_prepare_socket(LDAP *ld, int s, int proto )
+{
+ osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
+
+#ifdef TCP_NODELAY
+ if( proto == LDAP_PROTO_TCP ) {
+ int dummy = 1;
+ if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
+ (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
+ {
+ osip_debug(ld, "ldap_prepare_socket: "
+ "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
+ s, 0, 0);
}
+ }
+#endif
+
+ return 0;
+}
+
+#ifndef HAVE_WINSOCK
+
+#undef TRACE
+#define TRACE do { \
+ osip_debug(ld, \
+ "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
+ s, \
+ errno, \
+ sock_errstr(errno) ); \
+} while( 0 )
+
+/*
+ * check the socket for errors after select returned.
+ */
+static int
+ldap_pvt_is_socket_ready(LDAP *ld, int s)
+{
+ osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
+
+#if defined( notyet ) /* && defined( SO_ERROR ) */
+{
+ int so_errno;
+ int dummy = sizeof(so_errno);
+ if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
+ == AC_SOCKET_ERROR )
+ {
+ return -1;
+ }
+ if ( so_errno ) {
+ ldap_pvt_set_errno(so_errno);
+ TRACE;
+ return -1;
+ }
+ return 0;
+}
+#else
+{
+ /* error slippery */
+ struct sockaddr_in sin;
+ char ch;
+ int dummy = sizeof(sin);
+ if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
+ == AC_SOCKET_ERROR )
+ {
+ /* XXX: needs to be replace with ber_stream_read() */
+ read(s, &ch, 1);
+ TRACE;
+ return -1;
+ }
+ return 0;
+}
+#endif
+ return -1;
+}
+#undef TRACE
+
+#endif /* HAVE_WINSOCK */
+
+static int
+ldap_pvt_connect(LDAP *ld, ber_socket_t s,
+ struct sockaddr *sin, socklen_t addrlen,
+ int async)
+{
+ struct timeval tv, *opt_tv=NULL;
+ fd_set wfds, *z=NULL;
+#ifdef HAVE_WINSOCK
+ fd_set efds;
+#endif
+
+#ifdef LDAP_CONNECTIONLESS
+ /* We could do a connect() but that would interfere with
+ * attempts to poll a broadcast address
+ */
+ if (LDAP_IS_UDP(ld)) {
+ if (ld->ld_options.ldo_peer)
+ ldap_memfree(ld->ld_options.ldo_peer);
+ ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
+ AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
+ return ( 0 );
+ }
+#endif
+ if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
+ tv.tv_usec = opt_tv->tv_usec;
+ tv.tv_sec = opt_tv->tv_sec;
+ }
+
+ osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
+ s, opt_tv ? tv.tv_sec : -1L, async);
+
+ if ( ldap_pvt_ndelay_on(ld, s) == -1 )
+ return ( -1 );
+
+ if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
+ {
+ if ( ldap_pvt_ndelay_off(ld, s) == -1 )
+ return ( -1 );
+ return ( 0 );
+ }
+
+#ifdef HAVE_WINSOCK
+ ldap_pvt_set_errno( WSAGetLastError() );
+#endif
+
+ if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
+ return ( -1 );
+ }
+
#ifdef notyet
-#ifdef LDAP_REFERRALS
- status = 1;
- if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
- Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
- s, 0, 0 );
+ if ( async ) return ( -2 );
+#endif
+
+ FD_ZERO(&wfds);
+ FD_SET(s, &wfds );
+
+#ifdef HAVE_WINSOCK
+ FD_ZERO(&efds);
+ FD_SET(s, &efds );
+#endif
+
+ if ( select(ldap_int_tblsize, z, &wfds,
+#ifdef HAVE_WINSOCK
+ &efds,
+#else
+ z,
+#endif
+ opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
+ {
+ return ( -1 );
+ }
+
+#ifdef HAVE_WINSOCK
+ /* This means the connection failed */
+ if ( FD_ISSET(s, &efds) ) {
+ int so_errno;
+ int dummy = sizeof(so_errno);
+ if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
+ (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
+ {
+ /* impossible */
+ so_errno = WSAGetLastError();
+ }
+ ldap_pvt_set_errno(so_errno);
+ osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
+ "errno: %d (%s)\n", s, errno, sock_errstr(errno));
+ return -1;
+ }
+#endif
+ if ( FD_ISSET(s, &wfds) ) {
+#ifndef HAVE_WINSOCK
+ if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
+ return ( -1 );
+#endif
+ if ( ldap_pvt_ndelay_off(ld, s) == -1 )
+ return ( -1 );
+ return ( 0 );
+ }
+ osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
+ ldap_pvt_set_errno( ETIMEDOUT );
+ return ( -1 );
+}
+
+#ifndef HAVE_INET_ATON
+int
+ldap_pvt_inet_aton( const char *host, struct in_addr *in)
+{
+ unsigned long u = inet_addr( host );
+ if ( u != 0xffffffff || u != (unsigned long) -1 ) {
+ in->s_addr = u;
+ return 1;
+ }
+ return 0;
+}
+#endif
+
+
+int
+ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
+ int proto,
+ const char *host,
+ unsigned long address, int port, int async )
+{
+ struct sockaddr_in sin;
+ ber_socket_t s = AC_SOCKET_INVALID;
+ int rc, i, use_hp = 0;
+ struct hostent *hp = NULL;
+ char *ha_buf=NULL, *p, *q;
+ int socktype;
+
+
+ switch(proto) {
+ case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
+ osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
+ break;
+ case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
+ osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
+ break;
+
+ default:
+ osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
+ proto, 0, 0);
+ return -1;
+ }
+
+ if (host != NULL) {
+#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
+ char serv[7];
+ int err;
+ struct addrinfo hints, *res, *sai;
+
+ memset( &hints, '\0', sizeof(hints) );
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = socktype;
+
+ snprintf(serv, sizeof serv, "%d", port );
+ if ( err = getaddrinfo(host, serv, &hints, &res) ) {
+ osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
+ AC_GAI_STRERROR(err), 0, 0);
+ return -1;
}
-#endif /* LDAP_REFERRALS */
-#endif /* notyet */
- (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
- sin.sin_family = AF_INET;
- sin.sin_port = port;
- SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
- ( use_hp ? (char *) hp->h_addr_list[ i ] :
- (char *) &address ), sizeof( sin.sin_addr.s_addr) );
-
- if ( connect( s, (struct sockaddr *)&sin,
- sizeof( struct sockaddr_in )) >= 0 ) {
- connected = 1;
- rc = 0;
- break;
- } else {
-#ifdef notyet
-#ifdef LDAP_REFERRALS
-#ifdef EAGAIN
- if ( errno == EINPROGRESS || errno == EAGAIN ) {
-#else /* EAGAIN */
- if ( errno == EINPROGRESS ) {
-#endif /* EAGAIN */
- Debug( LDAP_DEBUG_TRACE,
- "connect would block...\n", 0, 0, 0 );
- rc = -2;
- break;
+ rc = -1;
+
+ for( sai=res; sai != NULL; sai=sai->ai_next) {
+ if( sai->ai_addr == NULL ) {
+ osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
+ "ai_addr is NULL?\n", 0, 0, 0);
+ continue;
}
-#endif /* LDAP_REFERRALS */
-#endif /* notyet */
-#ifdef LDAP_DEBUG
- if ( ldap_debug & LDAP_DEBUG_TRACE ) {
- perror( (char *)inet_ntoa( sin.sin_addr ));
+ /* we assume AF_x and PF_x are equal for all x */
+ s = ldap_int_socket( ld, sai->ai_family, socktype );
+ if ( s == AC_SOCKET_INVALID ) {
+ continue;
}
+
+ if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
+ ldap_pvt_close_socket(ld, s);
+ break;
+ }
+
+ switch (sai->ai_family) {
+#ifdef LDAP_PF_INET6
+ case AF_INET6: {
+ char addr[INET6_ADDRSTRLEN];
+ inet_ntop( AF_INET6,
+ &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
+ addr, sizeof addr);
+ osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
+ addr, serv, 0);
+ } break;
#endif
- close( s );
- if ( !use_hp ) {
+ case AF_INET: {
+ char addr[INET_ADDRSTRLEN];
+ inet_ntop( AF_INET,
+ &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
+ addr, sizeof addr);
+ osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
+ addr, serv, 0);
+ } break;
+ }
+
+ rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
+ if ( (rc == 0) || (rc == -2) ) {
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
break;
}
+ ldap_pvt_close_socket(ld, s);
}
+ freeaddrinfo(res);
+ return rc;
+
+#else
+ struct in_addr in;
+ if (! inet_aton( host, &in) ) {
+ int local_h_errno;
+ struct hostent he_buf;
+ rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
+ &hp, &local_h_errno);
+
+ if ( (rc < 0) || (hp == NULL) ) {
+#ifdef HAVE_WINSOCK
+ ldap_pvt_set_errno( WSAGetLastError() );
+#else
+ /* not exactly right, but... */
+ ldap_pvt_set_errno( EHOSTUNREACH );
+#endif
+ if (ha_buf) LDAP_FREE(ha_buf);
+ return -1;
+ }
+ use_hp = 1;
+ }
+ address = in.s_addr;
+#endif
}
- sb->sb_sd = s;
-
- if ( connected ) {
-#ifdef notyet
-#ifdef LDAP_REFERRALS
- status = 0;
- if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
- Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
- s, 0, 0 );
+ rc = s = -1;
+ for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
+ s = ldap_int_socket( ld, PF_INET, socktype );
+ if ( s == AC_SOCKET_INVALID ) {
+ /* use_hp ? continue : break; */
+ break;
+ }
+
+ if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
+ ldap_pvt_close_socket(ld, s);
+ break;
}
-#endif /* LDAP_REFERRALS */
-#endif /* notyet */
-
- Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
- s, inet_ntoa( sin.sin_addr ), 0 );
- }
- return( rc );
-}
+ (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons((short) port);
+ p = (char *)&sin.sin_addr;
+ q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
+ AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
+
+ osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
+ inet_ntoa(sin.sin_addr),port,0);
+
+ rc = ldap_pvt_connect(ld, s,
+ (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
+ async);
+
+ if ( (rc == 0) || (rc == -2) ) {
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
+ break;
+ }
+ ldap_pvt_close_socket(ld, s);
-void
-close_connection( Sockbuf *sb )
-{
- tcp_close( sb->sb_sd );
+ if (!use_hp)
+ break;
+ }
+ if (ha_buf) LDAP_FREE(ha_buf);
+ return rc;
}
-
-#ifdef KERBEROS
+#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
+ defined( HAVE_CYRUS_SASL )
char *
-host_connected_to( Sockbuf *sb )
+ldap_host_connected_to( Sockbuf *sb )
{
- struct hostent *hp;
- char *p;
- int len;
- struct sockaddr_in sin;
-
- (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
- len = sizeof( sin );
- if ( getpeername( sb->sb_sd, (struct sockaddr *)&sin, &len ) == -1 ) {
+ struct hostent *hp;
+ socklen_t len;
+ struct sockaddr sa;
+ char *addr;
+ char *host;
+
+ /* buffers for gethostbyaddr_r */
+ struct hostent he_buf;
+ int local_h_errno;
+ char *ha_buf=NULL;
+ ber_socket_t sd;
+
+ (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
+ len = sizeof( sa );
+
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ if ( getpeername( sd, &sa, &len ) == -1 ) {
return( NULL );
}
* this is necessary for kerberos to work right, since the official
* hostname is used as the kerberos instance.
*/
- if (( hp = gethostbyaddr( (char *) &sin.sin_addr,
- sizeof( sin.sin_addr ), AF_INET )) != NULL ) {
- if ( hp->h_name != NULL ) {
- return( strdup( hp->h_name ));
+
+ switch (sa.sa_family) {
+#ifdef LDAP_PF_LOCAL
+ case AF_LOCAL:
+ return LDAP_STRDUP( ldap_int_hostname );
+#endif
+#ifdef LDAP_PF_INET6
+ case AF_INET6:
+ addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
+ len = sizeof( struct in6_addr );
+ break;
+#endif
+ case AF_INET:
+ addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
+ len = sizeof( struct in_addr );
+
+ {
+ struct sockaddr_in localhost;
+ localhost.sin_addr.s_addr = htonl( INADDR_ANY );
+
+ if( memcmp ( &localhost.sin_addr,
+ &((struct sockaddr_in *)&sa)->sin_addr,
+ sizeof(localhost.sin_addr) ) == 0 )
+ {
+ return LDAP_STRDUP( ldap_int_hostname );
+ }
+
+#ifdef INADDR_LOOPBACK
+ localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
+
+ if( memcmp ( &localhost.sin_addr,
+ &((struct sockaddr_in *)&sa)->sin_addr,
+ sizeof(localhost.sin_addr) ) == 0 )
+ {
+ return LDAP_STRDUP( ldap_int_hostname );
+ }
+#endif
}
+ break;
+
+ default:
+ return( NULL );
+ break;
+ }
+
+ host = NULL;
+ if ((ldap_pvt_gethostbyaddr_a( addr, len,
+ sa.sa_family, &he_buf, &ha_buf,
+ &hp,&local_h_errno ) == 0 ) &&
+ (hp != NULL) && ( hp->h_name != NULL ) )
+ {
+ host = LDAP_STRDUP( hp->h_name );
}
- return( NULL );
+ LDAP_FREE( ha_buf );
+ return host;
}
-#endif /* KERBEROS */
+#endif
-#ifdef LDAP_REFERRALS
/* for UNIX */
struct selectinfo {
fd_set si_readfds;
void
-mark_select_write( LDAP *ld, Sockbuf *sb )
+ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
{
struct selectinfo *sip;
+ ber_socket_t sd;
sip = (struct selectinfo *)ld->ld_selectinfo;
-
- if ( !FD_ISSET( sb->sb_sd, &sip->si_writefds )) {
- FD_SET( sb->sb_sd, &sip->si_writefds );
+
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ if ( !FD_ISSET( sd, &sip->si_writefds )) {
+ FD_SET( sd, &sip->si_writefds );
}
}
void
-mark_select_read( LDAP *ld, Sockbuf *sb )
+ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
{
struct selectinfo *sip;
+ ber_socket_t sd;
sip = (struct selectinfo *)ld->ld_selectinfo;
- if ( !FD_ISSET( sb->sb_sd, &sip->si_readfds )) {
- FD_SET( sb->sb_sd, &sip->si_readfds );
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ if ( !FD_ISSET( sd, &sip->si_readfds )) {
+ FD_SET( sd, &sip->si_readfds );
}
}
void
-mark_select_clear( LDAP *ld, Sockbuf *sb )
+ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
{
struct selectinfo *sip;
+ ber_socket_t sd;
sip = (struct selectinfo *)ld->ld_selectinfo;
- FD_CLR( sb->sb_sd, &sip->si_writefds );
- FD_CLR( sb->sb_sd, &sip->si_readfds );
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ FD_CLR( sd, &sip->si_writefds );
+ FD_CLR( sd, &sip->si_readfds );
}
int
-is_write_ready( LDAP *ld, Sockbuf *sb )
+ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
{
struct selectinfo *sip;
+ ber_socket_t sd;
sip = (struct selectinfo *)ld->ld_selectinfo;
- return( FD_ISSET( sb->sb_sd, &sip->si_use_writefds ));
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ return( FD_ISSET( sd, &sip->si_use_writefds ));
}
int
-is_read_ready( LDAP *ld, Sockbuf *sb )
+ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
{
struct selectinfo *sip;
+ ber_socket_t sd;
sip = (struct selectinfo *)ld->ld_selectinfo;
- return( FD_ISSET( sb->sb_sd, &sip->si_use_readfds ));
+ ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
+ return( FD_ISSET( sd, &sip->si_use_readfds ));
}
void *
-new_select_info()
+ldap_new_select_info( void )
{
struct selectinfo *sip;
- if (( sip = (struct selectinfo *)calloc( 1,
+ if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
sizeof( struct selectinfo ))) != NULL ) {
FD_ZERO( &sip->si_readfds );
FD_ZERO( &sip->si_writefds );
void
-free_select_info( void *sip )
+ldap_free_select_info( void *sip )
{
- free( sip );
+ LDAP_FREE( sip );
+}
+
+
+void
+ldap_int_ip_init( void )
+{
+ int tblsize;
+#if defined( HAVE_SYSCONF )
+ tblsize = sysconf( _SC_OPEN_MAX );
+#elif defined( HAVE_GETDTABLESIZE )
+ tblsize = getdtablesize();
+#else
+ tblsize = FD_SETSIZE;
+#endif /* !USE_SYSCONF */
+
+#ifdef FD_SETSIZE
+ if( tblsize > FD_SETSIZE )
+ tblsize = FD_SETSIZE;
+#endif /* FD_SETSIZE*/
+ ldap_int_tblsize = tblsize;
}
int
-do_ldap_select( LDAP *ld, struct timeval *timeout )
+ldap_int_select( LDAP *ld, struct timeval *timeout )
{
struct selectinfo *sip;
- static int tblsize;
- Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
+ Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
- if ( tblsize == 0 ) {
-#ifdef USE_SYSCONF
- tblsize = sysconf( _SC_OPEN_MAX );
-#else /* USE_SYSCONF */
- tblsize = getdtablesize();
-#endif /* USE_SYSCONF */
- }
+ if ( ldap_int_tblsize == 0 )
+ ldap_int_ip_init();
sip = (struct selectinfo *)ld->ld_selectinfo;
sip->si_use_readfds = sip->si_readfds;
sip->si_use_writefds = sip->si_writefds;
- return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
- NULL, timeout ));
+ return( select( ldap_int_tblsize,
+ &sip->si_use_readfds, &sip->si_use_writefds,
+ NULL, timeout ));
}
-#endif /* LDAP_REFERRALS */
--- /dev/null
+/*
+ * Copyright 2000-2002 The OpenLDAP Foundation
+ * COPYING RESTRICTIONS APPLY. See COPYRIGHT File in top level directory
+ * of this package for details.
+ */
+
+#include "portable.h"
+
+#include <ac/ctype.h>
+#include <ac/string.h>
+#include <ac/stdlib.h>
+
+#include <lber.h>
+
+#define malloc(x) ber_memalloc(x)
+#define realloc(x,y) ber_memrealloc(x,y)
+#define free(x) ber_memfree(x)
+
+#include <ldap_utf8.h>
+#include <ldap_pvt_uc.h>
+
+
+int ucstrncmp(
+ const ldap_unicode_t *u1,
+ const ldap_unicode_t *u2,
+ ber_len_t n )
+{
+ for(; 0 < n; ++u1, ++u2, --n ) {
+ if( *u1 != *u2 ) {
+ return *u1 < *u2 ? -1 : +1;
+ }
+ if ( *u1 == 0 ) {
+ return 0;
+ }
+ }
+ return 0;
+}
+
+int ucstrncasecmp(
+ const ldap_unicode_t *u1,
+ const ldap_unicode_t *u2,
+ ber_len_t n )
+{
+ for(; 0 < n; ++u1, ++u2, --n ) {
+ ldap_unicode_t uu1 = uctoupper( *u1 );
+ ldap_unicode_t uu2 = uctoupper( *u2 );
+
+ if( uu1 != uu2 ) {
+ return uu1 < uu2 ? -1 : +1;
+ }
+ if ( uu1 == 0 ) {
+ return 0;
+ }
+ }
+ return 0;
+}
+
+ldap_unicode_t * ucstrnchr(
+ const ldap_unicode_t *u,
+ ber_len_t n,
+ ldap_unicode_t c )
+{
+ for(; 0 < n; ++u, --n ) {
+ if( *u == c ) {
+ return (ldap_unicode_t *) u;
+ }
+ }
+
+ return NULL;
+}
+
+ldap_unicode_t * ucstrncasechr(
+ const ldap_unicode_t *u,
+ ber_len_t n,
+ ldap_unicode_t c )
+{
+ c = uctoupper( c );
+ for(; 0 < n; ++u, --n ) {
+ if( uctoupper( *u ) == c ) {
+ return (ldap_unicode_t *) u;
+ }
+ }
+
+ return NULL;
+}
+
+void ucstr2upper(
+ ldap_unicode_t *u,
+ ber_len_t n )
+{
+ for(; 0 < n; ++u, --n ) {
+ *u = uctoupper( *u );
+ }
+}
+
+char * UTF8normalize(
+ struct berval *bv,
+ unsigned casefold )
+{
+ int i, j, len, clen, outpos, ucsoutlen, outsize, last;
+ char *out, *s;
+ unsigned long *ucs, *p, *ucsout;
+
+ static unsigned char mask[] = {
+ 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
+
+ if ( bv == NULL ) {
+ return NULL;
+ }
+
+ s = bv->bv_val;
+ len = bv->bv_len;
+
+ /* See if the string is pure ASCII so we can shortcut */
+ for ( i=0; i<len; i++ ) {
+ if ( s[i] & 0x80 ) /* non-ASCII */
+ break;
+ }
+
+ /* It's pure ASCII or zero-len */
+ if ( i == len ) {
+ out = malloc( len + 1 );
+ if ( i && !casefold ) {
+ strncpy( out, bv->bv_val, len );
+ } else {
+ for ( j=0; j<i; j++ )
+ out[j] = TOUPPER( s[j] );
+ }
+ out[len] = '\0';
+ return out;
+ }
+
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+
+ /* FIXME: Should first check to see if string is already in
+ * proper normalized form.
+ */
+
+ outpos = 0;
+
+ /* finish off everything up to character before first non-ascii */
+ if ( LDAP_UTF8_ISASCII( s ) ) {
+ for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
+ }
+ if ( i == len ) {
+ out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
+ out[outpos] = '\0';
+ return out;
+ }
+ } else {
+ i = 0;
+ }
+
+ p = ucs = (long *) malloc( len * sizeof(*ucs) );
+ if ( ucs == NULL ) {
+ free(out);
+ return NULL;
+ }
+
+ /* convert character before first non-ascii to ucs-4 */
+ if ( i > 0 ) {
+ *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+ p++;
+ }
+
+ /* s[i] is now first non-ascii character */
+ for (;;) {
+ /* s[i] is non-ascii */
+ /* convert everything up to next ascii to ucs-4 */
+ while ( i < len ) {
+ clen = LDAP_UTF8_CHARLEN2( s + i, clen );
+ if ( clen == 0 ) {
+ free( ucs );
+ free( out );
+ return NULL;
+ }
+ if ( clen == 1 ) {
+ /* ascii */
+ break;
+ }
+ *p = s[i] & mask[clen];
+ i++;
+ for( j = 1; j < clen; j++ ) {
+ if ( (s[i] & 0xc0) != 0x80 ) {
+ free( ucs );
+ free( out );
+ return NULL;
+ }
+ *p <<= 6;
+ *p |= s[i] & 0x3f;
+ i++;
+ }
+ if ( casefold ) {
+ *p = uctoupper( *p );
+ }
+ p++;
+ }
+ /* normalize ucs of length p - ucs */
+ uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );
+ ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
+ /* convert ucs to utf-8 and store in out */
+ for ( j = 0; j < ucsoutlen; j++ ) {
+ /* allocate more space if not enough room for
+ 6 bytes and terminator */
+ if ( outsize - outpos < 7 ) {
+ outsize = ucsoutlen - j + outpos + 6;
+ out = (char *) realloc( out, outsize );
+ if ( out == NULL ) {
+ free( ucs );
+ return NULL;
+ }
+ }
+ outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
+ }
+
+ if ( i == len ) {
+ break;
+ }
+
+ last = i;
+
+ /* s[i] is ascii */
+ /* finish off everything up to char before next non-ascii */
+ for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
+ }
+ if ( i == len ) {
+ out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
+ break;
+ }
+
+ /* convert character before next non-ascii to ucs-4 */
+ *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+ p = ucs + 1;
+ }
+ free( ucs );
+ out[outpos] = '\0';
+ return out;
+}
+
+struct berval * UTF8bvnormalize(
+ struct berval *bv,
+ struct berval *newbv,
+ unsigned casefold )
+{
+ int i, j, len, clen, outpos, ucsoutlen, outsize, last;
+ char *out, *s;
+ unsigned long *ucs, *p, *ucsout;
+
+ static unsigned char mask[] = {
+ 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
+
+ if ( bv == NULL ) {
+ return NULL;
+ }
+
+ s = bv->bv_val;
+ len = bv->bv_len;
+
+ if ( len == 0 ) {
+ return ber_dupbv( newbv, bv );
+ }
+
+ /* FIXME: Should first check to see if string is already in
+ * proper normalized form. This is almost as time consuming
+ * as the normalization though.
+ */
+
+ /* finish off everything up to character before first non-ascii */
+ if ( LDAP_UTF8_ISASCII( s ) ) {
+ if ( casefold ) {
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+ outpos = 0;
+
+ for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ out[outpos++] = TOUPPER( s[i-1] );
+ }
+ if ( i == len ) {
+ out[outpos++] = TOUPPER( s[len - 1] );
+ out[outpos] = '\0';
+ return ber_str2bv( out, outpos, 0, newbv);
+ }
+ } else {
+ for ( i = 1; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ /* empty */
+ }
+
+ if ( i == len ) {
+ return ber_str2bv( s, len, 1, newbv );
+ }
+
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+ outpos = i - 1;
+ memcpy(out, s, outpos);
+ }
+ } else {
+ outsize = len + 7;
+ out = (char *) malloc( outsize );
+ if ( out == NULL ) {
+ return NULL;
+ }
+ outpos = 0;
+ i = 0;
+ }
+
+ p = ucs = (long *) malloc( len * sizeof(*ucs) );
+ if ( ucs == NULL ) {
+ free(out);
+ return NULL;
+ }
+
+ /* convert character before first non-ascii to ucs-4 */
+ if ( i > 0 ) {
+ *p = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+ p++;
+ }
+
+ /* s[i] is now first non-ascii character */
+ for (;;) {
+ /* s[i] is non-ascii */
+ /* convert everything up to next ascii to ucs-4 */
+ while ( i < len ) {
+ clen = LDAP_UTF8_CHARLEN2( s + i, clen );
+ if ( clen == 0 ) {
+ free( ucs );
+ free( out );
+ return NULL;
+ }
+ if ( clen == 1 ) {
+ /* ascii */
+ break;
+ }
+ *p = s[i] & mask[clen];
+ i++;
+ for( j = 1; j < clen; j++ ) {
+ if ( (s[i] & 0xc0) != 0x80 ) {
+ free( ucs );
+ free( out );
+ return NULL;
+ }
+ *p <<= 6;
+ *p |= s[i] & 0x3f;
+ i++;
+ }
+ if ( casefold ) {
+ *p = uctoupper( *p );
+ }
+ p++;
+ }
+ /* normalize ucs of length p - ucs */
+ uccanondecomp( ucs, p - ucs, &ucsout, &ucsoutlen );
+ ucsoutlen = uccanoncomp( ucsout, ucsoutlen );
+ /* convert ucs to utf-8 and store in out */
+ for ( j = 0; j < ucsoutlen; j++ ) {
+ /* allocate more space if not enough room for
+ 6 bytes and terminator */
+ if ( outsize - outpos < 7 ) {
+ outsize = ucsoutlen - j + outpos + 6;
+ out = (char *) realloc( out, outsize );
+ if ( out == NULL ) {
+ free( ucs );
+ return NULL;
+ }
+ }
+ outpos += ldap_x_ucs4_to_utf8( ucsout[j], &out[outpos] );
+ }
+
+ if ( i == len ) {
+ break;
+ }
+
+ last = i;
+
+ /* s[i] is ascii */
+ /* finish off everything up to char before next non-ascii */
+ for ( i++; (i < len) && LDAP_UTF8_ISASCII(s + i); i++ ) {
+ out[outpos++] = casefold ? TOUPPER( s[i-1] ) : s[i-1];
+ }
+ if ( i == len ) {
+ out[outpos++] = casefold ? TOUPPER( s[len - 1] ) : s[len - 1];
+ break;
+ }
+
+ /* convert character before next non-ascii to ucs-4 */
+ *ucs = casefold ? TOUPPER( s[i - 1] ) : s[i - 1];
+ p = ucs + 1;
+ }
+ free( ucs );
+ out[outpos] = '\0';
+ return ber_str2bv( out, outpos, 0, newbv );
+}
+
+/* compare UTF8-strings, optionally ignore casing, string pointers must not be NULL */
+/* slow, should be optimized */
+int UTF8normcmp(
+ const char *s1,
+ const char *s2,
+ unsigned casefold )
+{
+ int i, l1, l2, len, ulen, res;
+ unsigned long *ucs, *ucsout1, *ucsout2;
+
+ l1 = strlen( s1 );
+ l2 = strlen( s2 );
+
+ if ( ( l1 == 0 ) || ( l2 == 0 ) ) {
+ if ( l1 == l2 ) {
+ return 0;
+ }
+ return *s1 - *s2 > 0 ? 1 : -1;
+ }
+
+ /* See if we can get away with a straight ASCII compare */
+ len = (l1 < l2) ? l1 : l2;
+ for ( i = 0; i<len; i++ ) {
+ /* Is either char non-ASCII? */
+ if ((s1[i] & 0x80) || (s2[i] & 0x80))
+ break;
+ if (casefold) {
+ char c1 = TOUPPER(s1[i]);
+ char c2 = TOUPPER(s2[i]);
+ res = c1 - c2;
+ } else {
+ res = s1[i] - s2[i];
+ }
+ if (res)
+ return res;
+ }
+ /* Strings were ASCII, equal up to minlen */
+ if (i == len)
+ return l1 - l2;
+
+ /* FIXME: Should first check to see if strings are already in
+ * proper normalized form.
+ */
+
+ ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
+ if ( ucs == NULL ) {
+ return l1 > l2 ? 1 : -1; /* what to do??? */
+ }
+
+ /*
+ * XXYYZ: we convert to ucs4 even though -llunicode
+ * expects ucs2 in an unsigned long
+ */
+
+ /* convert and normalize 1st string */
+ for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
+ ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
+ if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+ free( ucs );
+ return -1; /* what to do??? */
+ }
+ len = LDAP_UTF8_CHARLEN( s1 + i );
+ }
+ uccanondecomp( ucs, ulen, &ucsout1, &l1 );
+ l1 = uccanoncomp( ucsout1, l1 );
+
+ /* convert and normalize 2nd string */
+ for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
+ ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
+ if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+ free( ucsout1 );
+ free( ucs );
+ return 1; /* what to do??? */
+ }
+ len = LDAP_UTF8_CHARLEN( s2 + i );
+ }
+ uccanondecomp( ucs, ulen, &ucsout2, &l2 );
+ l2 = uccanoncomp( ucsout2, l2 );
+
+ free( ucs );
+
+ res = casefold
+ ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
+ : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
+ free( ucsout1 );
+ free( ucsout2 );
+
+ if ( res != 0 ) {
+ return res;
+ }
+ if ( l1 == l2 ) {
+ return 0;
+ }
+ return l1 > l2 ? 1 : -1;
+}
+
+/* compare UTF8-strings, optionally ignore casing */
+/* slow, should be optimized */
+int UTF8bvnormcmp(
+ struct berval *bv1,
+ struct berval *bv2,
+ unsigned casefold )
+{
+ int i, l1, l2, len, ulen, res;
+ char *s1, *s2, *done;
+ unsigned long *ucs, *ucsout1, *ucsout2;
+
+ if (bv1 == NULL) {
+ return bv2 == NULL ? 0 : -1;
+ } else if (bv2 == NULL) {
+ return 1;
+ }
+
+ l1 = bv1->bv_len;
+ l2 = bv2->bv_len;
+
+ len = (l1 < l2) ? l1 : l2;
+ if (len == 0) {
+ return l1 == 0 ? (l2 == 0 ? 0 : -1) : 1;
+ }
+
+ s1 = bv1->bv_val;
+ s2 = bv2->bv_val;
+ done = s1 + len;
+
+ while ( (s1 < done) && LDAP_UTF8_ISASCII(s1) && LDAP_UTF8_ISASCII(s2) ) {
+ if (casefold) {
+ char c1 = TOUPPER(*s1);
+ char c2 = TOUPPER(*s2);
+ res = c1 - c2;
+ } else {
+ res = *s1 - *s2;
+ }
+ s1++;
+ s2++;
+ if (res) {
+ /* done unless next character in s1 or s2 is non-ascii */
+ if (s1 < done) {
+ if (!LDAP_UTF8_ISASCII(s1) || !LDAP_UTF8_ISASCII(s2)) {
+ break;
+ }
+ } else if ((len < l1) && !LDAP_UTF8_ISASCII(s1) ||
+ (len < l2) && !LDAP_UTF8_ISASCII(s2)) {
+ break;
+ }
+ return res;
+ }
+ }
+
+ /* We have encountered non-ascii or strings equal up to len */
+
+ /* set i to number of iterations */
+ i = s1 - done + len;
+ /* passed through loop at least once? */
+ if (i > 0) {
+ if (!res && (s1 == done) &&
+ ((len == l1) || LDAP_UTF8_ISASCII(s1)) &&
+ ((len == l2) || LDAP_UTF8_ISASCII(s2))) {
+ /* all ascii and equal up to len */
+ return l1 - l2;
+ }
+
+ /* rewind one char, and do normalized compare from there */
+ s1--;
+ s2--;
+ l1 -= i - 1;
+ l2 -= i - 1;
+ }
+
+ /* FIXME: Should first check to see if strings are already in
+ * proper normalized form.
+ */
+
+ ucs = (long *) malloc( ( l1 > l2 ? l1 : l2 ) * sizeof(*ucs) );
+ if ( ucs == NULL ) {
+ return l1 > l2 ? 1 : -1; /* what to do??? */
+ }
+
+ /*
+ * XXYYZ: we convert to ucs4 even though -llunicode
+ * expects ucs2 in an unsigned long
+ */
+
+ /* convert and normalize 1st string */
+ for ( i = 0, ulen = 0; i < l1; i += len, ulen++ ) {
+ ucs[ulen] = ldap_x_utf8_to_ucs4( s1 + i );
+ if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+ free( ucs );
+ return -1; /* what to do??? */
+ }
+ len = LDAP_UTF8_CHARLEN( s1 + i );
+ }
+ uccanondecomp( ucs, ulen, &ucsout1, &l1 );
+ l1 = uccanoncomp( ucsout1, l1 );
+
+ /* convert and normalize 2nd string */
+ for ( i = 0, ulen = 0; i < l2; i += len, ulen++ ) {
+ ucs[ulen] = ldap_x_utf8_to_ucs4( s2 + i );
+ if ( ucs[ulen] == LDAP_UCS4_INVALID ) {
+ free( ucsout1 );
+ free( ucs );
+ return 1; /* what to do??? */
+ }
+ len = LDAP_UTF8_CHARLEN( s2 + i );
+ }
+ uccanondecomp( ucs, ulen, &ucsout2, &l2 );
+ l2 = uccanoncomp( ucsout2, l2 );
+
+ free( ucs );
+
+ res = casefold
+ ? ucstrncasecmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 )
+ : ucstrncmp( ucsout1, ucsout2, l1 < l2 ? l1 : l2 );
+ free( ucsout1 );
+ free( ucsout2 );
+
+ if ( res != 0 ) {
+ return res;
+ }
+ if ( l1 == l2 ) {
+ return 0;
+ }
+ return l1 > l2 ? 1 : -1;
+}