openldap/contrib/ldapc++/examples/main.cpp

135 lines
4.6 KiB
C++
Raw Normal View History

2008-03-28 19:27:24 +08:00
// $OpenLDAP$
/*
2021-01-12 03:25:53 +08:00
* Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
2008-03-08 00:46:23 +08:00
#include <iostream>
#include <sstream>
#include "LDAPConnection.h"
#include "LDAPConstraints.h"
#include "LDAPSearchReference.h"
#include "LDAPSearchResults.h"
#include "LDAPAttribute.h"
#include "LDAPAttributeList.h"
#include "LDAPEntry.h"
#include "LDAPException.h"
#include "LDAPModification.h"
2008-03-08 00:46:23 +08:00
#include "debug.h"
int main(){
LDAPConstraints* cons=new LDAPConstraints;
LDAPControlSet* ctrls=new LDAPControlSet;
ctrls->add(LDAPCtrl(LDAP_CONTROL_MANAGEDSAIT));
cons->setServerControls(ctrls);
LDAPConnection *lc=new LDAPConnection("localhost",9009);
lc->setConstraints(cons);
2007-09-21 19:27:23 +08:00
std::cout << "----------------------doing bind...." << std::endl;
try{
lc->bind("cn=Manager,o=Organisation,c=DE" , "secret",cons);
2007-09-21 19:27:23 +08:00
std::cout << lc->getHost() << std::endl;
bool result = lc->compare("cn=Manager,o=Organisation,c=DE",
ITS#8605 - spelling fixes * javascript * kernel * ldap * length * macros * maintained * manager * matching * maximum * mechanism * memory * method * mimic * minimum * modifiable * modifiers * modifying * multiple * necessary * normalized * objectclass * occurrence * occurring * offered * operation * original * overridden * parameter * permanent * preemptively * printable * protocol * provider * really * redistribution * referenced * refresh * regardless * registered * request * reserved * resource * response * sanity * separated * setconcurrency * should * specially * specifies * structure * structures * subordinates * substitution * succeed * successful * successfully * sudoers * sufficient * superiors * supported * synchronization * terminated * they're * through * traffic * transparent * unsigned * unsupported * version * absence * achieves * adamson * additional * address * against * appropriate * architecture * associated * async * attribute * authentication * authorized * auxiliary * available * begin * beginning * buffered * canonical * certificate * charray * check * class * compatibility * compilation * component * configurable * configuration * configure * conjunction * constraints * constructor * contained * containing * continued * control * convenience * correspond * credentials * cyrillic * database * definitions * deloldrdn * dereferencing * destroy * distinguish * documentation * emmanuel * enabled * entry * enumerated * everything * exhaustive * existence * existing * explicitly * extract * fallthru * fashion * february * finally * function * generically * groupname * happened * implementation * including * initialization * initializes * insensitive * instantiated * instantiation * integral * internal * iterate
2017-02-26 15:49:31 +08:00
LDAPAttribute("cn","Manager"));
2007-09-21 19:27:23 +08:00
std::cout << "Compare: " << result << std::endl;
LDAPAttributeList* attrs=new LDAPAttributeList();
StringList values;
StringList s2;
values.add("top");
values.add("Person");
attrs->addAttribute(LDAPAttribute("objectClass",values));
attrs->addAttribute(LDAPAttribute("cn","Peter"));
attrs->addAttribute(LDAPAttribute("sn","Peter,hallo"));
LDAPEntry* entry=new LDAPEntry(
"cn=Peter , o=Organisation, c=DE", attrs);
// lc->add(entry);
// lc->del("ou=Groups,o=Organisation,c=DE");
LDAPSearchResults* entries = lc->search("o=Organisation,c=DE",
LDAPConnection::SEARCH_ONE);
if (entries != 0){
LDAPEntry* entry = entries->getNext();
if(entry != 0){
2007-09-21 19:27:23 +08:00
std::cout << *(entry) << std::endl;
}
while(entry){
try{
entry = entries->getNext();
if(entry != 0){
2007-09-21 19:27:23 +08:00
std::cout << *(entry) << std::endl;
}
delete entry;
}catch(LDAPReferralException e){
2007-09-21 19:27:23 +08:00
std::cout << "Caught Referral" << std::endl;
}
}
}
lc->unbind();
delete lc;
2008-03-28 19:27:24 +08:00
}catch (LDAPException &e){
2007-09-21 19:27:23 +08:00
std::cout << "-------------- caught Exception ---------"<< std::endl;
std::cout << e << std::endl;
}
/*
2007-09-21 19:27:23 +08:00
std::cout << "--------------------starting search" << std::endl;
LDAPAttributeList* attrs=new LDAPAttributeList();
StringList values;
values.add("top");
values.add("organizationalUnit");
attrs->addAttribute(LDAPAttribute("objectClass",values));
attrs->addAttribute(LDAPAttribute("ou","Groups"));
LDAPEntry* entry=new LDAPEntry(
"ou=Groups, o=Organisation, c=DE", attrs);
LDAPAttribute newattr("description");
LDAPModification::mod_op op = LDAPModification::OP_DELETE;
LDAPModList *mod=new LDAPModList();
mod->addModification(LDAPModification(newattr,op));
LDAPMessageQueue* q=0;
try{
q=lc->search("o=Organisation,c=de",LDAPAsynConnection::SEARCH_SUB,
"objectClass=*",StringList());
// q=lc->add(entry);
// q=lc->modify("cn=Manager,o=Organisation,c=DE",
// mod);
LDAPMsg *res=q->getNext();
bool cont=true;
while( cont ) {
switch(res->getMessageType()){
LDAPSearchResult *res2;
const LDAPEntry *entry;
case LDAP_RES_SEARCH_ENTRY :
res2= (LDAPSearchResult*)res;
entry= res2->getEntry();
2007-09-21 19:27:23 +08:00
std::cout << "Entry: " << *entry << std::endl;
delete res;
res=q->getNext();
break;
case LDAP_RES_SEARCH_REFERENCE :
2007-09-21 19:27:23 +08:00
std::cout << "Reference: " << std::endl;
delete res;
res=q->getNext();
break;
default :
2007-09-21 19:27:23 +08:00
std::cout << ( *(LDAPResult*) res) << std::endl;
delete res;
2007-09-21 19:27:23 +08:00
std::cout << "-----------------search done" << std::endl;
cont=false;
break;
}
}
delete q;
}catch (LDAPException e){
2007-09-21 19:27:23 +08:00
std::cout << "----------------error during search" << std::endl;
delete q;
2007-09-21 19:27:23 +08:00
std::cout << e << std::endl;
}
lc->unbind();
*/
}