Back to index

openldap  2.4.31
main.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include <iostream>
00008 #include <sstream>
00009 #include "LDAPConnection.h"
00010 #include "LDAPConstraints.h"
00011 #include "LDAPSearchReference.h"
00012 #include "LDAPSearchResults.h"
00013 #include "LDAPAttribute.h"
00014 #include "LDAPAttributeList.h"
00015 #include "LDAPEntry.h"
00016 #include "LDAPException.h"
00017 #include "LDAPModification.h"
00018 
00019 #include "debug.h"
00020 
00021 int main(){
00022     LDAPConstraints* cons=new LDAPConstraints;
00023     LDAPControlSet* ctrls=new LDAPControlSet;
00024     ctrls->add(LDAPCtrl(LDAP_CONTROL_MANAGEDSAIT));
00025     cons->setServerControls(ctrls);
00026     LDAPConnection *lc=new LDAPConnection("localhost",9009);
00027     lc->setConstraints(cons);
00028     std::cout << "----------------------doing bind...." << std::endl;
00029     try{
00030         lc->bind("cn=Manager,o=Organisation,c=DE" , "secret",cons);
00031         std::cout << lc->getHost() << std::endl;    
00032         bool result = lc->compare("cn=Manager,o=Organisation,c=DE", 
00033                 LDAPAttribute("cn","Manaer"));
00034         std::cout << "Compare: " << result << std::endl;
00035     
00036         LDAPAttributeList* attrs=new LDAPAttributeList();
00037         StringList values;
00038         StringList s2;
00039         values.add("top");
00040         values.add("Person");
00041         attrs->addAttribute(LDAPAttribute("objectClass",values));
00042         attrs->addAttribute(LDAPAttribute("cn","Peter"));
00043         attrs->addAttribute(LDAPAttribute("sn","Peter,hallo"));
00044         LDAPEntry* entry=new LDAPEntry(
00045                 "cn=Peter , o=Organisation, c=DE", attrs);    
00046 //        lc->add(entry);
00047         
00048 //        lc->del("ou=Groups,o=Organisation,c=DE");
00049 
00050         LDAPSearchResults* entries = lc->search("o=Organisation,c=DE",
00051                 LDAPConnection::SEARCH_ONE);
00052         if (entries != 0){
00053             LDAPEntry* entry = entries->getNext();
00054             if(entry != 0){
00055                 std::cout << *(entry) << std::endl;
00056             }
00057             while(entry){
00058                 try{
00059                     entry = entries->getNext();
00060                     if(entry != 0){
00061                         std::cout << *(entry) << std::endl;
00062                     }
00063                     delete entry;
00064                 }catch(LDAPReferralException e){
00065                     std::cout << "Caught Referral" << std::endl;
00066                 }
00067             }
00068         }
00069         
00070         lc->unbind();
00071         delete lc;
00072    }catch (LDAPException &e){
00073         std::cout << "-------------- caught Exception ---------"<< std::endl;
00074         std::cout << e << std::endl;
00075     }
00076 
00077     /*
00078     std::cout << "--------------------starting search" << std::endl;
00079     LDAPAttributeList* attrs=new LDAPAttributeList();
00080     StringList values;
00081     values.add("top");
00082     values.add("organizationalUnit");
00083     attrs->addAttribute(LDAPAttribute("objectClass",values));
00084     attrs->addAttribute(LDAPAttribute("ou","Groups"));
00085     LDAPEntry* entry=new LDAPEntry(
00086             "ou=Groups, o=Organisation, c=DE", attrs);    
00087 
00088     LDAPAttribute newattr("description");
00089     LDAPModification::mod_op op = LDAPModification::OP_DELETE;
00090     LDAPModList *mod=new LDAPModList();
00091     mod->addModification(LDAPModification(newattr,op));
00092     LDAPMessageQueue* q=0;
00093     try{
00094         q=lc->search("o=Organisation,c=de",LDAPAsynConnection::SEARCH_SUB,
00095          "objectClass=*",StringList());
00096 //        q=lc->add(entry);
00097 //        q=lc->modify("cn=Manager,o=Organisation,c=DE",
00098 //                mod);
00099         LDAPMsg *res=q->getNext();
00100         bool cont=true;
00101         while( cont  ) {
00102             switch(res->getMessageType()){
00103                 LDAPSearchResult *res2;
00104                 const LDAPEntry *entry;
00105                 case LDAP_RES_SEARCH_ENTRY :
00106                     res2= (LDAPSearchResult*)res;
00107                     entry=  res2->getEntry();
00108                     std::cout << "Entry:            " << *entry << std::endl; 
00109                     delete res;
00110                     res=q->getNext();
00111                 break;
00112                 case LDAP_RES_SEARCH_REFERENCE :
00113                     std::cout << "Reference:         "  << std::endl;
00114                     delete res;
00115                     res=q->getNext();
00116                 break;
00117                 default :
00118                     std::cout << ( *(LDAPResult*) res) << std::endl;
00119                     delete res;
00120                     std::cout  << "-----------------search done" << std::endl;
00121                     cont=false;
00122                 break;
00123             }
00124         }
00125         delete q;
00126     }catch (LDAPException e){
00127         std::cout << "----------------error during search" << std::endl;
00128         delete q;
00129         std::cout << e << std::endl;
00130     }
00131     lc->unbind();
00132     */
00133 }
00134