KLdap

ldapoperation.h
1/*
2 This file is part of libkldap.
3 SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#pragma once
9
10#include "kldap_core_export.h"
11#include "ldapconnection.h"
12#include "ldapcontrol.h"
13#include "ldapdn.h"
14#include "ldapobject.h"
15#include "ldapurl.h"
16
17#include <QByteArray>
18#include <QList>
19#include <QString>
20
21#include <memory>
22
23namespace KLDAPCore
24{
25/**
26 * @brief
27 * This class allows sending an ldap operation
28 * (search, rename, modify, delete, compare, exop) to an LDAP server.
29 */
30class KLDAP_CORE_EXPORT LdapOperation
31{
32public:
33 using ModType = enum {
34 Mod_None,
35 Mod_Add,
36 Mod_Replace,
37 Mod_Del,
38 };
39
40 using ResultType = enum {
41 RES_BIND = 0x61,
42 RES_SEARCH_ENTRY = 0x64,
43 RES_SEARCH_REFERENCE = 0x73,
44 RES_SEARCH_RESULT = 0x65,
45 RES_MODIFY = 0x67,
46 RES_ADD = 0x69,
47 RES_DELETE = 0x69,
48 RES_MODDN = 0x6d,
49 RES_COMPARE = 0x6f,
50 RES_EXTENDED = 0x78,
51 RES_EXTENDED_PARTIAL = 0x79,
52 };
53
54 using ModOp = struct {
55 ModType type;
56 QString attr;
57 QList<QByteArray> values;
58 };
59
60 using ModOps = QList<ModOp>;
61
62 enum SASL_Fields {
63 SASL_Authname = 0x1,
64 SASL_Authzid = 0x2,
65 SASL_Realm = 0x4,
66 SASL_Password = 0x8,
67 };
68
69 struct SASL_Credentials {
70 int fields;
71 QString authname;
72 QString authzid;
73 QString realm;
74 QString password;
75 };
76
77 using SASL_Callback_Proc = int(SASL_Credentials &, void *);
78
79 struct SASL_Data {
80 SASL_Callback_Proc *proc;
81 void *data;
82 SASL_Credentials creds;
83 };
84
85 LdapOperation();
86 explicit LdapOperation(LdapConnection &conn);
87 ~LdapOperation();
88
89 /**
90 * Sets the connection object. Without living connection object,
91 * LDAP operations are not possible.
92 * @param the connection object to set
93 */
94 void setConnection(LdapConnection &conn);
95 /**
96 * Returns the connection object.
97 */
99 /**
100 * Sets the client controls which will sent with each operation.
101 */
102 void setClientControls(const LdapControls &ctrls);
103 /**
104 * Sets the server controls which will sent with each operation.
105 */
106 void setServerControls(const LdapControls &ctrls);
107 /**
108 * Returns the client controls (which set by setClientControls()).
109 */
110 [[nodiscard]] LdapControls clientControls() const;
111 /**
112 * Returns the server controls (which set by setServerControls()).
113 */
114 [[nodiscard]] LdapControls serverControls() const;
115
116 /**
117 * Binds to the server which specified in the connection object.
118 * Can do simple or SASL bind. Returns a message id if successful, negative value if not.
119 */
120 [[nodiscard]] int bind(const QByteArray &creds = QByteArray(), SASL_Callback_Proc *saslproc = nullptr, void *data = nullptr);
121
122 /**
123 * Binds to the server which specified in the connection object.
124 * Can do simple or SASL bind. This is the synchronous version.
125 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
126 */
127 [[nodiscard]] int bind_s(SASL_Callback_Proc *saslproc = nullptr, void *data = nullptr);
128
129 /**
130 * Starts a search operation with the given base DN, scope, filter and
131 * result attributes. Returns a message id if successful, -1 if not.
132 */
133 [[nodiscard]] int search(const LdapDN &base, LdapUrl::Scope scope, const QString &filter, const QStringList &attrs);
134 /**
135 * Starts an addition operation.
136 * Returns a message id if successful, -1 if not.
137 * @param object the additional operation to start
138 */
139 [[nodiscard]] int add(const LdapObject &object);
140 /**
141 * Adds the specified object to the LDAP database.
142 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
143 * @param object the object to add to LDAP database
144 */
145 [[nodiscard]] int add_s(const LdapObject &object);
146 /**
147 * Starts an addition operation. This version accepts ModOps not LdapObject.
148 * Returns a message id if successful, -1 if not.
149 * @param dn the LdapDN operation to start
150 * @param ops the ModOps operation to start
151 */
152 [[nodiscard]] int add(const LdapDN &dn, const ModOps &ops);
153 /**
154 * Adds the specified object to the LDAP database. This version accepts ModOps not LdapObject.
155 * This is the synchronous version.
156 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
157 * @param dn the LdapDN object to add
158 * @param ops the ModOps object to add
159 */
160 [[nodiscard]] int add_s(const LdapDN &dn, const ModOps &ops);
161 /**
162 * Starts a modrdn operation on given DN, changing its RDN to newRdn,
163 * changing its parent to newSuperior (if it's not empty), and deletes
164 * the old dn if deleteold is true.
165 * Returns a message id if successful, -1 if not.
166 */
167 [[nodiscard]] int rename(const LdapDN &dn, const QString &newRdn, const QString &newSuperior, bool deleteold = true);
168 /**
169 * Performs a modrdn operation on given DN, changing its RDN to newRdn,
170 * changing its parent to newSuperior (if it's not empty), and deletes
171 * the old dn if deleteold is true. This is the synchronous version.
172 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
173 */
174 [[nodiscard]] int rename_s(const LdapDN &dn, const QString &newRdn, const QString &newSuperior, bool deleteold = true);
175 /**
176 * Starts a delete operation on the given DN.
177 * Returns a message id if successful, -1 if not.
178 */
179 [[nodiscard]] int del(const LdapDN &dn);
180 /**
181 * Deletes the given DN. This is the synchronous version.
182 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
183 * @param dn the dn to delete
184 */
185 [[nodiscard]] int del_s(const LdapDN &dn);
186 /**
187 * Starts a modify operation on the given DN.
188 * Returns a message id if successful, -1 if not.
189 * @param dn the DN to start modify operation on
190 */
191 [[nodiscard]] int modify(const LdapDN &dn, const ModOps &ops);
192 /**
193 * Performs a modify operation on the given DN.
194 * This is the synchronous version.
195 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
196 */
197 [[nodiscard]] int modify_s(const LdapDN &dn, const ModOps &ops);
198 /**
199 * Starts a compare operation on the given DN, compares the specified
200 * attribute with the given value.
201 * Returns a message id if successful, -1 if not.
202 */
203 [[nodiscard]] int compare(const LdapDN &dn, const QString &attr, const QByteArray &value);
204 /**
205 * Performs a compare operation on the given DN, compares the specified
206 * attribute with the given value. This is the synchronous version.
207 * Returns KLDAP_COMPARE_TRUE if the entry contains the attribute value
208 * and KLDAP_COMPARE_FALSE if it does not. Otherwise, some error code
209 * is returned.
210 */
211 [[nodiscard]] int compare_s(const LdapDN &dn, const QString &attr, const QByteArray &value);
212 /**
213 * Starts an extended operation specified with oid and data.
214 * Returns a message id if successful, -1 if not.
215 */
216 [[nodiscard]] int exop(const QString &oid, const QByteArray &data);
217 /**
218 * Performs an extended operation specified with oid and data.
219 * This is the synchronous version.
220 * Returns KLDAP_SUCCESS id if successful, else an LDAP error code.
221 */
222 [[nodiscard]] int exop_s(const QString &oid, const QByteArray &data);
223 /**
224 * Abandons a long-running operation. Requires the message id.
225 */
226 [[nodiscard]] int abandon(int id);
227 /**
228 * Waits for up to \p msecs milliseconds for a result message from the LDAP
229 * server. If \p msecs is -1, then this function will block indefinitely.
230 * If \p msecs is 0, then this function will return immediately, that is it
231 * will perform a poll for a result message.
232 *
233 * Returns the type of the result LDAP message (RES_XXX constants).
234 * -1 if error occurred, 0 if the timeout value elapsed. Note!
235 * Return code -1 means that fetching the message resulted in error,
236 * not the LDAP operation error. Call connection().ldapErrorCode() to
237 * determine if the operation succeeded.
238 */
239 [[nodiscard]] int waitForResult(int id, int msecs = -1);
240 /**
241 * Returns the result object if result() returned RES_SEARCH_ENTRY.
242 */
243 [[nodiscard]] LdapObject object() const;
244 /**
245 * Returns the server controls from the returned ldap message (grabbed
246 * by result()).
247 */
248 [[nodiscard]] LdapControls controls() const;
249 /**
250 * Returns the OID of the extended operation response (result
251 * returned RES_EXTENDED).
252 */
253 [[nodiscard]] QByteArray extendedOid() const;
254 /**
255 * Returns the data from the extended operation response (result
256 * returned RES_EXTENDED).
257 */
258 [[nodiscard]] QByteArray extendedData() const;
259 /**
260 * The server might supply a matched DN string in the message indicating
261 * how much of a name in a request was recognized. This can be grabbed by
262 * matchedDn().
263 */
264 [[nodiscard]] QString matchedDn() const;
265 /**
266 * This function returns the referral strings from the parsed message
267 * (if any).
268 */
269 [[nodiscard]] QList<QByteArray> referrals() const;
270 /**
271 * Returns the server response for a bind request (result
272 * returned RES_BIND).
273 */
274 [[nodiscard]] QByteArray serverCred() const;
275
276private:
277 class LdapOperationPrivate;
278 std::unique_ptr<LdapOperationPrivate> const d;
279
280 Q_DISABLE_COPY(LdapOperation)
281};
282}
This class represents a connection to an LDAP server.
This class represents an LDAP Object.
Definition ldapobject.h:31
void setClientControls(const LdapControls &ctrls)
Sets the client controls which will sent with each operation.
int bind_s(SASL_Callback_Proc *saslproc=nullptr, void *data=nullptr)
Binds to the server which specified in the connection object.
QByteArray extendedOid() const
Returns the OID of the extended operation response (result returned RES_EXTENDED).
QString matchedDn() const
The server might supply a matched DN string in the message indicating how much of a name in a request...
QList< QByteArray > referrals() const
This function returns the referral strings from the parsed message (if any).
int abandon(int id)
Abandons a long-running operation.
LdapControls clientControls() const
Returns the client controls (which set by setClientControls()).
QByteArray serverCred() const
Returns the server response for a bind request (result returned RES_BIND).
QByteArray extendedData() const
Returns the data from the extended operation response (result returned RES_EXTENDED).
int modify(const LdapDN &dn, const ModOps &ops)
Starts a modify operation on the given DN.
int exop(const QString &oid, const QByteArray &data)
Starts an extended operation specified with oid and data.
int rename(const LdapDN &dn, const QString &newRdn, const QString &newSuperior, bool deleteold=true)
Starts a modrdn operation on given DN, changing its RDN to newRdn, changing its parent to newSuperior...
int rename_s(const LdapDN &dn, const QString &newRdn, const QString &newSuperior, bool deleteold=true)
Performs a modrdn operation on given DN, changing its RDN to newRdn, changing its parent to newSuperi...
void setConnection(LdapConnection &conn)
Sets the connection object.
void setServerControls(const LdapControls &ctrls)
Sets the server controls which will sent with each operation.
int add(const LdapObject &object)
Starts an addition operation.
int compare(const LdapDN &dn, const QString &attr, const QByteArray &value)
Starts a compare operation on the given DN, compares the specified attribute with the given value.
LdapObject object() const
Returns the result object if result() returned RES_SEARCH_ENTRY.
int compare_s(const LdapDN &dn, const QString &attr, const QByteArray &value)
Performs a compare operation on the given DN, compares the specified attribute with the given value.
int del_s(const LdapDN &dn)
Deletes the given DN.
int bind(const QByteArray &creds=QByteArray(), SASL_Callback_Proc *saslproc=nullptr, void *data=nullptr)
Binds to the server which specified in the connection object.
LdapConnection & connection()
Returns the connection object.
int modify_s(const LdapDN &dn, const ModOps &ops)
Performs a modify operation on the given DN.
int del(const LdapDN &dn)
Starts a delete operation on the given DN.
LdapControls controls() const
Returns the server controls from the returned ldap message (grabbed by result()).
LdapControls serverControls() const
Returns the server controls (which set by setServerControls()).
int exop_s(const QString &oid, const QByteArray &data)
Performs an extended operation specified with oid and data.
int waitForResult(int id, int msecs=-1)
Waits for up to msecs milliseconds for a result message from the LDAP server.
int add_s(const LdapObject &object)
Adds the specified object to the LDAP database.
int search(const LdapDN &base, LdapUrl::Scope scope, const QString &filter, const QStringList &attrs)
Starts a search operation with the given base DN, scope, filter and result attributes.
enum { Base, One, Sub } Scope
Describes the scope of the LDAP url.
Definition ldapurl.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:20 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.