]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/examples/main.cpp
Happy New Year
[openldap] / contrib / ldapc++ / examples / main.cpp
1 // $OpenLDAP$
2 /*
3  * Copyright 2000-2018 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include <iostream>
8 #include <sstream>
9 #include "LDAPConnection.h"
10 #include "LDAPConstraints.h"
11 #include "LDAPSearchReference.h"
12 #include "LDAPSearchResults.h"
13 #include "LDAPAttribute.h"
14 #include "LDAPAttributeList.h"
15 #include "LDAPEntry.h"
16 #include "LDAPException.h"
17 #include "LDAPModification.h"
18
19 #include "debug.h"
20
21 int main(){
22     LDAPConstraints* cons=new LDAPConstraints;
23     LDAPControlSet* ctrls=new LDAPControlSet;
24     ctrls->add(LDAPCtrl(LDAP_CONTROL_MANAGEDSAIT));
25     cons->setServerControls(ctrls);
26     LDAPConnection *lc=new LDAPConnection("localhost",9009);
27     lc->setConstraints(cons);
28     std::cout << "----------------------doing bind...." << std::endl;
29     try{
30         lc->bind("cn=Manager,o=Organisation,c=DE" , "secret",cons);
31         std::cout << lc->getHost() << std::endl;    
32         bool result = lc->compare("cn=Manager,o=Organisation,c=DE", 
33                 LDAPAttribute("cn","Manager"));
34         std::cout << "Compare: " << result << std::endl;
35     
36         LDAPAttributeList* attrs=new LDAPAttributeList();
37         StringList values;
38         StringList s2;
39         values.add("top");
40         values.add("Person");
41         attrs->addAttribute(LDAPAttribute("objectClass",values));
42         attrs->addAttribute(LDAPAttribute("cn","Peter"));
43         attrs->addAttribute(LDAPAttribute("sn","Peter,hallo"));
44         LDAPEntry* entry=new LDAPEntry(
45                 "cn=Peter , o=Organisation, c=DE", attrs);    
46 //        lc->add(entry);
47         
48 //        lc->del("ou=Groups,o=Organisation,c=DE");
49
50         LDAPSearchResults* entries = lc->search("o=Organisation,c=DE",
51                 LDAPConnection::SEARCH_ONE);
52         if (entries != 0){
53             LDAPEntry* entry = entries->getNext();
54             if(entry != 0){
55                 std::cout << *(entry) << std::endl;
56             }
57             while(entry){
58                 try{
59                     entry = entries->getNext();
60                     if(entry != 0){
61                         std::cout << *(entry) << std::endl;
62                     }
63                     delete entry;
64                 }catch(LDAPReferralException e){
65                     std::cout << "Caught Referral" << std::endl;
66                 }
67             }
68         }
69         
70         lc->unbind();
71         delete lc;
72    }catch (LDAPException &e){
73         std::cout << "-------------- caught Exception ---------"<< std::endl;
74         std::cout << e << std::endl;
75     }
76
77     /*
78     std::cout << "--------------------starting search" << std::endl;
79     LDAPAttributeList* attrs=new LDAPAttributeList();
80     StringList values;
81     values.add("top");
82     values.add("organizationalUnit");
83     attrs->addAttribute(LDAPAttribute("objectClass",values));
84     attrs->addAttribute(LDAPAttribute("ou","Groups"));
85     LDAPEntry* entry=new LDAPEntry(
86             "ou=Groups, o=Organisation, c=DE", attrs);    
87
88     LDAPAttribute newattr("description");
89     LDAPModification::mod_op op = LDAPModification::OP_DELETE;
90     LDAPModList *mod=new LDAPModList();
91     mod->addModification(LDAPModification(newattr,op));
92     LDAPMessageQueue* q=0;
93     try{
94         q=lc->search("o=Organisation,c=de",LDAPAsynConnection::SEARCH_SUB,
95          "objectClass=*",StringList());
96 //        q=lc->add(entry);
97 //        q=lc->modify("cn=Manager,o=Organisation,c=DE",
98 //                mod);
99         LDAPMsg *res=q->getNext();
100         bool cont=true;
101         while( cont  ) {
102             switch(res->getMessageType()){
103                 LDAPSearchResult *res2;
104                 const LDAPEntry *entry;
105                 case LDAP_RES_SEARCH_ENTRY :
106                     res2= (LDAPSearchResult*)res;
107                     entry=  res2->getEntry();
108                     std::cout << "Entry:            " << *entry << std::endl; 
109                     delete res;
110                     res=q->getNext();
111                 break;
112                 case LDAP_RES_SEARCH_REFERENCE :
113                     std::cout << "Reference:         "  << std::endl;
114                     delete res;
115                     res=q->getNext();
116                 break;
117                 default :
118                     std::cout << ( *(LDAPResult*) res) << std::endl;
119                     delete res;
120                     std::cout  << "-----------------search done" << std::endl;
121                     cont=false;
122                 break;
123             }
124         }
125         delete q;
126     }catch (LDAPException e){
127         std::cout << "----------------error during search" << std::endl;
128         delete q;
129         std::cout << e << std::endl;
130     }
131     lc->unbind();
132     */
133 }
134