]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPAsynConnection.h
Added ldif_countlines()
[openldap] / contrib / ldapc++ / src / LDAPAsynConnection.h
1 /*
2  * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6
7 #ifndef LDAP_ASYN_CONNECTION_H
8 #define LDAP_ASYN_CONNECTION_H
9
10 #include<iostream>
11 #include<string>
12
13 #include<ldap.h>
14 #include<lber.h>
15
16 #include <LDAPMessageQueue.h>
17 #include <LDAPConstraints.h>
18 #include <LDAPModification.h>
19 #include <LDAPModList.h>
20 #include <LDAPUrl.h>
21 #include <LDAPUrlList.h>
22
23 class LDAPEntry;
24 class LDAPAttribute;
25
26 //* Main class for an asynchronous LDAP connection 
27 /**
28  * This class represents an asynchronous connection to an LDAP-Server. It 
29  * provides the methods for authentication, and all other LDAP-Operations
30  * (e.g. search, add, delete, etc.)
31  * All of the LDAP-Operations return a pointer to a LDAPMessageQueue-Object,
32  * which can be used to obtain the results of that operation.
33  * A basic example of this class could be like this:  <BR>
34  * 1. Create a new LDAPAsynConnection Object: <BR>
35  * 2. Use the init-method to initialize the connection <BR>
36  * 3. Call the bind-method to authenticate to the directory <BR>
37  * 4. Obtain the bind results from the return LDAPMessageQueue-Object <BR>
38  * 5. Perform on of the operations on the directory (add, delete, search, ..)
39  *    <BR>
40  * 6. Use the return LDAPMessageQueue to obtain the results of the operation 
41  * <BR>
42  * 7. Close the connection (feature not implemented yet :) ) <BR>
43  */
44 class LDAPAsynConnection{
45     public :
46         /** 
47          * Constant for the Search-Operation to indicate a Base-Level
48          * Search
49          */
50         static const int SEARCH_BASE=0;
51         
52         /** 
53          * Constant for the Search-Operation to indicate a One-Level
54          * Search
55          */
56         static const int SEARCH_ONE=1;
57         
58         /** 
59          * Constant for the Search-Operation to indicate a subtree 
60          * Search
61          */
62         static const int SEARCH_SUB=2;
63 //        static const int SEARCH_SUB=LDAP_SCOPE_SUBTREE;
64 //        static const int SEARCH_ONE=LDAP_SCOPE_ONELEVEL;
65 //        static const int SEARCH_SUB=LDAP_SCOPE_SUBTREE;
66
67         /** Construtor that initializes a connection to a server
68          * @param hostname Name (or IP-Adress) of the destination host
69          * @param port Port the LDAP server is running on
70          * @param cons Default constraints to use with operations over 
71          *      this connection
72          */
73         LDAPAsynConnection(const std::string& hostname=std::string("localhost"),
74                 int port=389, LDAPConstraints *cons=new LDAPConstraints() );
75
76         //* Destructor
77         virtual ~LDAPAsynConnection();
78
79         /** 
80          * Initializes a connection to a server. 
81          * 
82          * There actually no
83          * communication to the server. Just the object is initialized
84          * (e.g. this method is called within the 
85          * LDAPAsynConnection(char*,int,LDAPConstraints) constructor.)
86          * @param hostname  The Name or IP-Address of the destination
87          *             LDAP-Server
88          * @param port      The Network Port the server is running on
89          */
90         void init(const std::string& hostname, int port);
91
92         /**
93          * Start TLS on this connection.  This isn't in the constructor,
94          * because it could fail (i.e. server doesn't have SSL cert, client
95          * api wasn't compiled against OpenSSL, etc.).  If you need TLS,
96          * then you should error if this call fails with an error code.
97          */
98         int start_tls();
99
100         /** Simple authentication to a LDAP-Server
101          *
102          * @throws LDAPException If the Request could not be sent to the
103          *      destination server, a LDAPException-object contains the
104          *      error that occured.
105          * This method does a simple (username, password) bind to the server.
106          * Other, saver, authentcation methods are provided later
107          * @param dn the distiguished name to bind as
108          * @param passwd cleartext password to use
109          */
110         LDAPMessageQueue* bind(const std::string& dn="", const std::string& passwd="",
111                 const LDAPConstraints *cons=0);
112
113         /** Performing a search on a directory tree.
114          *
115          * Use the search method to perform a search on the LDAP-Directory
116          * @throws LDAPException If the Request could not be sent to the
117          *      destination server, a LDAPException-object contains the
118          *      error that occured.
119          * @param base The distinguished name of the starting point for the
120          *      search operation
121          * @param scope The scope of the search. Possible values: <BR> 
122          *      LDAPAsynConnection::SEARCH_BASE, <BR> 
123          *      LDAPAsynConnection::SEARCH_ONE, <BR>
124          *      LDAPAsynConnection::SEARCH_SUB
125          * @param filter The std::string representation of a search filter to
126          *      use with this operation
127          * @param attrsOnly true if only the attributes names (no values) 
128          *      should be returned
129          * @param cons A set of constraints that should be used with this
130          *      request
131          */
132         LDAPMessageQueue* search(const std::string& base="", int scope=0, 
133                                  const std::string& filter="objectClass=*", 
134                                  const StringList& attrs=StringList(), 
135                                  bool attrsOnly=false,
136                                  const LDAPConstraints *cons=0);
137         
138         /** Delete an entry from the directory
139          *
140          * This method sends a delete request to the server
141          * @throws LDAPException If the Request could not be sent to the
142          *      destination server, a LDAPException-object contains the
143          *      error that occured.
144          * @param dn    Distinguished name of the entry that should be deleted
145          * @param cons  A set of constraints that should be used with this
146          *              request
147          */
148         LDAPMessageQueue* del(const std::string& dn, const LDAPConstraints *cons=0);
149         
150         /** 
151          * Perform the COMPARE-operation on an attribute 
152          *
153          * @throws LDAPException If the Request could not be sent to the
154          *      destination server, a LDAPException-object contains the
155          *      error that occured.
156          * @param dn    Distinguished name of the entry for which the compare
157          *              should be performed
158          * @param attr  An Attribute (one (!) value) to use for the
159          *      compare operation
160          * @param cons  A set of constraints that should be used with this
161          *              request
162          */
163         LDAPMessageQueue* compare(const std::string& dn, 
164                 const LDAPAttribute& attr, 
165                 const LDAPConstraints *cons=0);
166
167         /** Add an entry to the directory
168          *
169          * @throws LDAPException If the Request could not be sent to the
170          *      destination server, a LDAPException-object contains the
171          *      error that occured.
172          * @param le The entry that will be added to the directory
173          */
174         LDAPMessageQueue* add( const LDAPEntry* le,
175                 const LDAPConstraints *const=0);
176
177         /** Apply modifications to attributes of an entry
178          *
179          * @throws LDAPException If the Request could not be sent to the
180          *      destination server, a LDAPException-object contains the
181          *      error that occured.
182          * @param dn Distiguished Name of the Entry to modify
183          * @param modlist A set of modification that should be applied
184          *      to the Entry
185          * @param cons  A set of constraints that should be used with this
186          *              request
187          */
188         LDAPMessageQueue* modify(const std::string& dn, 
189                 const LDAPModList *modlist,
190                 const LDAPConstraints *cons=0);
191
192         /** modify the DN of an entry
193          *
194          * @throws LDAPException If the Request could not be sent to the
195          *      destination server, a LDAPException-object contains the
196          *      error that occured.
197          * @param dn            DN to modify
198          * @param newRDN        The new relative DN for the entry
199          * @param delOldRDN     true=The old RDN will be removed from the 
200          *                      attributes <BR>
201          *                      false=The old RDN will still be present in the
202          *                      attributes of the entry
203          * @param newParentDN   The DN of the new parent entry of the entry
204          *                      0 to keep the old one
205          */
206         LDAPMessageQueue* rename(const std::string& dn, 
207                 const std::string& newRDN,
208                 bool delOldRDN=false, const std::string& newParentDN="",
209                 const LDAPConstraints* cons=0);
210         
211         /** Perform a LDAP extended Operation
212          *
213          * @throws LDAPException If the Request could not be sent to the
214          *      destination server, a LDAPException-object contains the
215          *      error that occured.
216          * @param oid The dotted decimal representation of the extended 
217          *      Operation that should be performed
218          * @param value The data asociated with this operation
219          * @param cons  A set of constraints that should be used with this
220          *              request
221          */
222         LDAPMessageQueue* extOperation(const std::string& oid, 
223                 const std::string& value="", const LDAPConstraints *cons=0);
224         
225         /** End an outstanding request
226          *
227          * @param q All outstanding request related to this LDAPMessageQueue 
228          *      will be abandoned
229          */
230         void abandon(LDAPMessageQueue *q);
231         
232         /**
233          * Performs the UNBIND-operation on the destination server
234          * 
235          * @throws LDAPException in any case of an error
236          */
237         void unbind();
238
239         /**
240          * @returns The C-APIs LDAP-structure that is associated with the
241          *      current connection 
242          */
243         LDAP* getSessionHandle() const ;
244
245         /**
246          * @returns The Hostname of the destination server of the
247          *      connection. 
248          */
249         const std::string& getHost() const;
250
251         /**
252          * @returns The Port to which this connection is connecting to on
253          *      the remote server. 
254          */
255         int getPort() const;
256         
257         /** Change the default constraints of the connection
258          *
259          * @parameter cons cons New LDAPConstraints to use with the connection
260          */
261         void setConstraints(LDAPConstraints *cons);
262         
263         /** Get the default constraints of the connection
264          *
265          * @return Pointer to the LDAPConstraints-Object that is currently
266          *      used with the Connection
267          */
268         const LDAPConstraints* getConstraints() const;
269
270         /**
271          * This method is used internally for automatic referral chasing.
272          * It tries to bind to a destination server of the URLs of a
273          * referral.
274          *
275          * @throws LDAPException in any case of an error
276          * @param urls Contains a std::list of LDAP-Urls that indicate the
277          *      destinations of a referral
278          * @param usedUrl After this method has successfully bind to one of
279          *      the Destination URLs this parameter contains the URLs 
280          *      which was contacted.
281          * @param cons An LDAPConstraints-Object that should be used for
282          *      the new connection. If this object contains a 
283          *      LDAPRebind-object it is used to bind to the new server
284          */ 
285         LDAPAsynConnection* referralConnect(const LDAPUrlList& urls,
286                 LDAPUrlList::const_iterator& usedUrl,
287                 const LDAPConstraints* cons) const;
288
289     private :
290         /**
291          * Private copy constructor. So nobody can call it.
292          */
293         LDAPAsynConnection(const LDAPAsynConnection& lc){};
294         
295         /**
296          * A pointer to the C-API LDAP-structure that is associated with
297          * this connection
298          */
299         LDAP *cur_session;
300
301         /**
302          * A pointer to the default LDAPConstrains-object that is used when
303          * no LDAPConstraints-parameter is provided with a call for a
304          * LDAP-operation
305          */
306         LDAPConstraints *m_constr;
307
308         /**
309          * The name of the destination host
310          */
311         std::string m_host;
312
313         /**
314          * The port the destination server is running on.
315          */
316         int m_port;
317
318  protected:
319         /**
320          * Is caching enabled?
321          */
322         bool m_cacheEnabled;
323 };
324 #endif //LDAP_ASYN_CONNECTION_H
325
326