X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Fopen.c;h=e8785387f5507c4cb7b6f3328e1953b70d51ef4e;hb=403f4479bc9f9a864122d4aeecf7284408918302;hp=e42c95cd0ee650c825359a8cd9ab2c6f5d08c4a0;hpb=dc07e765f263ef459dcd2afd1ece01cfc85a0edd;p=openldap diff --git a/libraries/libldap/open.c b/libraries/libldap/open.c index e42c95cd0e..e8785387f5 100644 --- a/libraries/libldap/open.c +++ b/libraries/libldap/open.c @@ -1,3 +1,4 @@ +/* $OpenLDAP$ */ /* * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. * COPYING RESTRICTIONS APPLY, see COPYRIGHT file @@ -12,7 +13,8 @@ #include "portable.h" #include -#include + +#include #include #include @@ -39,9 +41,7 @@ LDAP * ldap_open( LDAP_CONST char *host, int port ) { LDAP *ld; -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS LDAPServer *srv; -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 ); @@ -49,32 +49,23 @@ ldap_open( LDAP_CONST char *host, int port ) return( NULL ); } -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS - if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) == + if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) == NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host = - strdup( ld->ld_defhost )) == NULL )) { - if(srv != NULL) free( (char*) srv ); + LDAP_STRDUP( ld->ld_defhost )) == NULL )) { + if(srv != NULL) LDAP_FREE( (char*) srv ); ldap_ld_free( ld, 0, NULL, NULL ); return( NULL ); } srv->lsrv_port = ld->ld_defport; if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) { - if ( ld->ld_defhost != NULL ) free( srv->lsrv_host ); - free( (char *)srv ); + if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host ); + LDAP_FREE( (char *)srv ); ldap_ld_free( ld, 0, NULL, NULL ); return( NULL ); } ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */ -#else /* LDAP_API_FEATURE_X_OPENLDAP_V2_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, NULL, NULL ); - return( NULL ); - } -#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */ - Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n", ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 ); @@ -96,8 +87,8 @@ ldap_init( LDAP_CONST char *defhost, int defport ) { LDAP *ld; - if(!openldap_ldap_initialized) { - openldap_ldap_initialize(); + if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) { + ldap_int_initialize(); } Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 ); @@ -140,48 +131,50 @@ ldap_init( LDAP_CONST char *defhost, int defport ) } #endif - if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) { + if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) { WSACleanup( ); return( NULL ); } /* copy the global options */ - memcpy(&ld->ld_options, &openldap_ldap_global_options, + memcpy(&ld->ld_options, &ldap_int_global_options, sizeof(ld->ld_options)); - /* but not pointers to malloc'ed strings */ + ld->ld_valid = LDAP_VALID_SESSION; + + /* but not pointers to malloc'ed items */ ld->ld_options.ldo_defbase = NULL; ld->ld_options.ldo_defhost = NULL; + ld->ld_options.ldo_sctrls = NULL; + ld->ld_options.ldo_cctrls = NULL; if ( defhost != NULL ) { - ld->ld_options.ldo_defhost = strdup( defhost ); + ld->ld_options.ldo_defhost = LDAP_STRDUP( defhost ); } else { - ld->ld_options.ldo_defhost = strdup( - openldap_ldap_global_options.ldo_defhost); + ld->ld_options.ldo_defhost = LDAP_STRDUP( + ldap_int_global_options.ldo_defhost); } if ( ld->ld_options.ldo_defhost == NULL ) { - free( (char*)ld ); + LDAP_FREE( (char*)ld ); WSACleanup( ); return( NULL ); } - if ( openldap_ldap_global_options.ldo_defbase != NULL ) { - ld->ld_options.ldo_defbase = strdup( - openldap_ldap_global_options.ldo_defbase); + if ( ldap_int_global_options.ldo_defbase != NULL ) { + ld->ld_options.ldo_defbase = LDAP_STRDUP( + ldap_int_global_options.ldo_defbase); } -#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) { - free( (char*) ld->ld_options.ldo_defhost ); + LDAP_FREE( (char*) ld->ld_options.ldo_defhost ); if ( ld->ld_options.ldo_defbase == NULL ) { - free( (char*) ld->ld_options.ldo_defbase ); + LDAP_FREE( (char*) ld->ld_options.ldo_defbase ); } - free( (char*) ld ); + LDAP_FREE( (char*) ld ); WSACleanup( ); return( NULL ); } -#endif if(defport != 0) { ld->ld_defport = defport; @@ -244,13 +237,13 @@ open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport, port = defport; } - if (( rc = ldap_connect_to_host( sb, curhost, 0L, + if (( rc = ldap_connect_to_host( ld, sb, curhost, 0L, port, async )) != -1 ) { break; } } } else { - rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ), + rc = ldap_connect_to_host( ld, sb, 0, htonl( INADDR_LOOPBACK ), defport, async ); } @@ -260,11 +253,26 @@ open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport, ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL ); +#ifdef HAVE_TLS + if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ) { + /* + * Fortunately, the lib uses blocking io... + */ + if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) < + 0 ) { + return -1; + } + /* FIXME: hostname of server must be compared with name in + * certificate.... + */ + } +#endif if ( krbinstancep != NULL ) { #ifdef HAVE_KERBEROS + char *c; if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL && - ( p = strchr( *krbinstancep, '.' )) != NULL ) { - *p = '\0'; + ( c = strchr( *krbinstancep, '.' )) != NULL ) { + *c = '\0'; } #else /* HAVE_KERBEROS */ krbinstancep = NULL;