Commit 1ab5360f authored by Ralf Haferkamp's avatar Ralf Haferkamp
Browse files

- some new Classes for sync. LDAP operations

- Classes for handling Controls
parent cdc0a665
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "debug.h"
#include "LDAPResult.h"
#include "LDAPException.h"
#include "LDAPReferralException.h"
#include "LDAPUrlList.h"
#include "LDAPConnection.h"
const int LDAPConnection::SEARCH_BASE = LDAPAsynConnection::SEARCH_BASE;
const int LDAPConnection::SEARCH_ONE = LDAPAsynConnection::SEARCH_ONE;
const int LDAPConnection::SEARCH_SUB = LDAPAsynConnection::SEARCH_SUB;
LDAPConnection::LDAPConnection(const string& hostname, int port,
LDAPConstraints* cons) :
LDAPAsynConnection(hostname, port, cons){
}
LDAPConnection::~LDAPConnection(){
}
void LDAPConnection::bind(const string& dn, const string& passwd,
LDAPConstraints* cons){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::bind" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::bind(dn,passwd,cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
if(resCode != LDAPResult::SUCCESS) {
if(resCode == LDAPResult::REFERRAL){
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}else{
delete res;
delete msg;
throw LDAPException(resCode);
}
}
delete res;
}
void LDAPConnection::unbind(){
LDAPAsynConnection::unbind();
}
bool LDAPConnection::compare(const string& dn, const LDAPAttribute& attr,
LDAPConstraints* cons){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::compare" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::compare(dn,attr,cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::COMPARE_TRUE :
delete res;
delete msg;
return true;
break;
case LDAPResult::COMPARE_FALSE :
delete res;
delete msg;
return false;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msg;
throw LDAPException(resCode);
}
}
void LDAPConnection::del(const string& dn, const LDAPConstraints* cons){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::del" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::del(dn,cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::SUCCESS :
delete res;
delete msg;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msg;
throw LDAPException(resCode);
}
}
void LDAPConnection::add(const LDAPEntry* le, const LDAPConstraints* cons=0){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::add" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::add(le,cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::SUCCESS :
delete res;
delete msg;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msg;
throw LDAPException(resCode);
}
}
void LDAPConnection::modify(const string& dn, const LDAPModList* mods,
const LDAPConstraints* cons){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::modify" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::modify(dn,mods,cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::SUCCESS :
delete res;
delete msg;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msg;
throw LDAPException(resCode);
}
}
void LDAPConnection::rename(const string& dn, const string& newRDN,
bool delOldRDN, const string& newParentDN,
const LDAPConstraints* cons=0){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::rename" << endl);
LDAPMessageQueue* msg=0;
LDAPResult* res=0;
try{
msg = LDAPAsynConnection::rename(dn,newRDN,delOldRDN, newParentDN,
cons);
res = (LDAPResult*)msg->getNext();
}catch(LDAPException e){
delete msg;
delete res;
throw;
}
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::SUCCESS :
delete res;
delete msg;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msg;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msg;
throw LDAPException(resCode);
}
}
LDAPSearchResults* LDAPConnection::search(const string& base, int scope,
const string& filter, const StringList& attrs, bool attrsOnly,
const LDAPConstraints* cons){
DEBUG(LDAP_DEBUG_TRACE,"LDAPConnection::search" << endl);
LDAPMessageQueue* msgq=0;
LDAPResult* res=0;
LDAPSearchResults* results= 0;
try{
msgq = LDAPAsynConnection::search(base,scope, filter, attrs, attrsOnly,
cons);
results = new LDAPSearchResults();
res = results->readMessageQueue(msgq);
}catch(LDAPException e){
delete msgq;
throw;
}
if(res != 0){
int resCode=res->getResultCode();
switch (resCode){
case LDAPResult::SUCCESS :
delete res;
delete msgq;
return results;
break;
case LDAPResult::REFERRAL :
{
LDAPUrlList urls = res->getReferralUrls();
delete res;
delete msgq;
throw LDAPReferralException(urls);
}
break;
default :
delete res;
delete msgq;
throw LDAPException(resCode);
}
}
return 0;
}
const string& LDAPConnection::getHost() const{
return LDAPAsynConnection::getHost();
}
int LDAPConnection::getPort() const{
return LDAPAsynConnection::getPort();
}
void LDAPConnection::setConstraints(LDAPConstraints* cons){
LDAPAsynConnection::setConstraints(cons);
}
const LDAPConstraints* LDAPConnection::getConstraints() const{
return LDAPAsynConnection::getConstraints();
}
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#ifndef LDAP_CONNECTION_H
#define LDAP_CONNECTION_H
#include "LDAPSearchResults.h"
#include "LDAPAsynConnection.h"
class LDAPConnection : private LDAPAsynConnection {
public :
static const int SEARCH_BASE;
static const int SEARCH_ONE;
static const int SEARCH_SUB;
LDAPConnection(const string& hostname="localhost", int port=389,
LDAPConstraints* cons=0);
~LDAPConnection();
void init(const string& hostname, int port);
void bind(const string& dn="", const string& passwd="",
LDAPConstraints* cons=0);
void unbind();
bool compare(const string&, const LDAPAttribute& attr,
LDAPConstraints* cons=0);
void del(const string& dn, const LDAPConstraints* cons=0);
void add(const LDAPEntry* le, const LDAPConstraints* cons=0);
void modify(const string& dn, const LDAPModList* mods,
const LDAPConstraints* cons=0);
void rename(const string& dn, const string& newRDN,
bool delOldRDN=false, const string& newParentDN="",
const LDAPConstraints* cons=0);
LDAPSearchResults* search(const string& base, int scope=0,
const string& filter="objectClass=*",
const StringList& attrs=StringList(), bool attrsOnly=false,
const LDAPConstraints* cons=0);
const string& getHost() const;
int getPort() const;
void setConstraints(LDAPConstraints *cons);
const LDAPConstraints* getConstraints() const ;
};
#endif //LDAP_CONNECTION_H
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "debug.h"
#include "LDAPControlSet.h"
LDAPControlSet::LDAPControlSet(){
}
LDAPControlSet::LDAPControlSet(const LDAPControlSet& cs){
DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPControlSet::LDAPControlSet(&)" << endl);
data=cs.data;
}
LDAPControlSet::LDAPControlSet(LDAPControl** controls){
DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPControlSet::LDAPControlSet()" << endl);
if(controls != 0){
LDAPControl** i;
for( i=controls; *i!=0;i++) {
add(LDAPCtrl(*i));
}
}
}
LDAPControlSet::~LDAPControlSet(){
DEBUG(LDAP_DEBUG_DESTROY,"LDAPControlSet::~LDAPControlSet()" << endl);
}
size_t LDAPControlSet::size() const {
DEBUG(LDAP_DEBUG_TRACE,"LDAPControlSet::size()" << endl);
return data.size();
}
LDAPControlSet::const_iterator LDAPControlSet::begin() const{
DEBUG(LDAP_DEBUG_TRACE,"LDAPControlSet::begin()" << endl);
return data.begin();
}
LDAPControlSet::const_iterator LDAPControlSet::end() const{
DEBUG(LDAP_DEBUG_TRACE,"LDAPControlSet::end()" << endl);
return data.end ();
}
void LDAPControlSet::add(const LDAPCtrl& ctrl){
DEBUG(LDAP_DEBUG_TRACE,"LDAPControlSet::add()" << endl);
data.push_back(ctrl);
}
LDAPControl** LDAPControlSet::toLDAPControlArray() const{
DEBUG(LDAP_DEBUG_TRACE, "LDAPControlSet::toLDAPControlArray()" << endl);
if(data.size() == 0){
return 0;
}else{
LDAPControl** ret= new LDAPControl*[data.size()+1];
CtrlList::const_iterator i;
int j=0;
for(i=data.begin(); i!=data.end(); i++,j++){
ret[j] = i->getControlStruct();
}
ret[data.size()]=0;
return ret;
}
}
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#ifndef LDAP_CONTROL_SET_H
#define LDAP_CONTROL_SET_H
#include <list>
#include <ldap.h>
#include "LDAPControl.h"
typedef list<LDAPCtrl> CtrlList;
class LDAPControlSet {
typedef CtrlList::const_iterator const_iterator;
public :
LDAPControlSet();
LDAPControlSet(const LDAPControlSet& cs);
//!for internal use only
//untested til now. Due to lack of server that return Controls
LDAPControlSet(LDAPControl** controls);
~LDAPControlSet();
size_t size() const ;
const_iterator begin() const;
const_iterator end() const;
void add(const LDAPCtrl& ctrl);
LDAPControl** toLDAPControlArray()const ;
private :
CtrlList data;
} ;
#endif //LDAP_CONTROL_SET_H
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "LDAPEntryList.h"
#include "LDAPEntry.h"
LDAPEntryList::LDAPEntryList(){
}
LDAPEntryList::LDAPEntryList(const LDAPEntryList& e){
m_entries = e.m_entries;
}
LDAPEntryList::~LDAPEntryList(){
}
size_t LDAPEntryList::size() const{
return m_entries.size();
}
LDAPEntryList::const_iterator LDAPEntryList::begin() const{
return m_entries.begin();
}
LDAPEntryList::const_iterator LDAPEntryList::end() const{
return m_entries.end();
}
void LDAPEntryList::addEntry(const LDAPEntry& e){
m_entries.push_back(e);
}
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#ifndef LDAP_ENTRY_LIST_H
#define LDAP_ENTRY_LIST_H
#include <list>
class LDAPEntry;
typedef list<LDAPEntry> EntryList;
class LDAPEntryList{
typedef EntryList::const_iterator const_iterator;
private:
EntryList m_entries;
public:
LDAPEntryList(const LDAPEntryList& el);
LDAPEntryList();
~LDAPEntryList();
size_t size() const;
const_iterator begin() const;
const_iterator end() const;
void addEntry(const LDAPEntry& e);
};
#endif // LDAP_ENTRY_LIST_H
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "debug.h"
#include <lber.h>
#include "LDAPRequest.h"
#include "LDAPException.h"
#include "LDAPResult.h"
#include "LDAPExtResult.h"
LDAPExtResult::LDAPExtResult(const LDAPRequest* req, LDAPMessage* msg) :
LDAPResult(req, msg){
DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPExtResult::LDAPExtResult()" << endl);
char* oid = 0;
BerValue* data = 0;
LDAP* lc = req->getConnection()->getSessionHandle();
int err=ldap_parse_extended_result(lc, msg, &oid, &data, 0);
if(err != LDAP_SUCCESS){
ber_bvfree(data);
ldap_memfree(oid);
throw LDAPException(err);
}else{
m_oid=string(oid);
ldap_memfree(oid);
if(data){
m_data=string(data->bv_val, data->bv_len);
ber_bvfree(data);
}
}
}
LDAPExtResult::~LDAPExtResult(){
DEBUG(LDAP_DEBUG_DESTROY,"LDAPExtResult::~LDAPExtResult()" << endl);
}
const string& LDAPExtResult::getResponseOid() const{
return m_oid;
}
const string& LDAPExtResult::getResponse() const{
return m_data;
}
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#ifndef LDAP_EXT_RESULT_H
#define LDAP_EXT_RESULT_H
#include <ldap.h>
class LDAPResult;
class LDAPRequest;
class LDAPExtResult : public LDAPResult {
public :
LDAPExtResult(const LDAPRequest* req, LDAPMessage* msg);
virtual ~LDAPExtResult();
const string& getResponseOid() const;
const string& getResponse() const;
private:
string m_oid;
string m_data;
};
#endif // LDAP_EXT_RESULT_H
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
#include "LDAPRebind.h"
/*
* Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/