File indexing completed on 2024-12-15 04:50:15

0001 /*
0002   This file is part of libkldap.
0003   SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QString>
0011 #include <memory>
0012 
0013 #include "kldap_core_export.h"
0014 #include "ldapserver.h"
0015 #include "ldapurl.h"
0016 
0017 namespace KLDAPCore
0018 {
0019 /**
0020  * @brief
0021  * This class represents a connection to an LDAP server.
0022  */
0023 class KLDAP_CORE_EXPORT LdapConnection
0024 {
0025 public:
0026     enum SASL_Fields { SASL_Authname = 0x1, SASL_Authzid = 0x2, SASL_Realm = 0x4, SASL_Password = 0x8 };
0027 
0028     /** Constructs an LdapConnection object */
0029     LdapConnection();
0030     /** Constructs an LdapConnection with the parameters given in url */
0031     explicit LdapConnection(const LdapUrl &url);
0032     /** Constructs an LdapConnection with the parameters given in server */
0033     explicit LdapConnection(const LdapServer &server);
0034 
0035     ~LdapConnection();
0036 
0037     /**
0038      * Sets the connection parameters via the specified url. After this,
0039      * you need to call connect() to connect with the new parameters.
0040      * @param url the URL containing the connection parameters
0041      */
0042     void setUrl(const LdapUrl &url);
0043     /**
0044      * Returns the connection parameters which was specified with an LDAP Url
0045      * or a LdapServer structure.
0046      */
0047     const LdapServer &server() const;
0048     /**
0049      * Sets the connection parameters via the specified server structure. After
0050      * this, you need to call connect() to connect with the new parameters.
0051      * @param server the server object containing the connection parameters
0052      */
0053     void setServer(const LdapServer &server);
0054 
0055     /**
0056      * Sets up the connection parameters with creating a handle to the LDAP server.
0057      * Also sets sizelimit and timelimit and starts TLS if it is requested.
0058      * Returns 0 if successful, else returns an LDAP error code, and an error
0059      * string which is available via connectionError().
0060      */
0061     int connect();
0062     /**
0063      * Returns a translated error string if connect() failed.
0064      */
0065     [[nodiscard]] QString connectionError() const;
0066     /**
0067      *  Closes the LDAP connection.
0068      */
0069     void close();
0070 
0071     /** Sets the size limit for the connection.
0072      *  @param sizelimit the connection size limit to set
0073      */
0074     [[nodiscard]] bool setSizeLimit(int sizelimit);
0075     /** Returns the current size limit. */
0076     [[nodiscard]] int sizeLimit() const;
0077 
0078     /** Sets the time limit for the connection.
0079      *  @param timelimit the connection time limit to set
0080      */
0081     [[nodiscard]] bool setTimeLimit(int timelimit);
0082     /** Returns the current time limit. */
0083     [[nodiscard]] int timeLimit() const;
0084 
0085     /** Gets an option from the connection. The option value can be client
0086      * library specific, so avoid this function if possible
0087      * @param option the connection option to return
0088      * @param value the value of option to get
0089      */
0090     int getOption(int option, void *value) const;
0091     /** Sets an option in the connection. The option value can be client
0092      * library specific, so avoid this function if possible */
0093     int setOption(int option, void *value);
0094 
0095     /** Returns the LDAP error code from the last operation */
0096     [[nodiscard]] int ldapErrorCode() const;
0097     /** Returns the LDAP error string from the last operation */
0098     [[nodiscard]] QString ldapErrorString() const;
0099     /** Returns a translated error message from the specified LDAP error code */
0100     [[nodiscard]] static QString errorString(int code);
0101 
0102     /** Returns the SASL error string from the last SASL operation */
0103     [[nodiscard]] QString saslErrorString() const;
0104 
0105     /**
0106      * Returns the opaqe client-library specific LDAP object.
0107      * Avoid its usage if you can.
0108      */
0109     void *handle() const;
0110 
0111     /**
0112      * Returns the opaqe sasl-library specific SASL object.
0113      * Avoid its usage if you can.
0114      */
0115     void *saslHandle() const;
0116 
0117 private:
0118     class LdapConnectionPrivate;
0119     std::unique_ptr<LdapConnectionPrivate> const d;
0120 
0121     Q_DISABLE_COPY(LdapConnection)
0122 };
0123 }