| 1 | /* |
| 2 | A temporary copy to break dependency to KLDAP |
| 3 | |
| 4 | This file is part of libkldap. |
| 5 | SPDX-FileCopyrightText: 2004-2006 Szombathelyi György <gyurco@freemail.hu> |
| 6 | |
| 7 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef LDIF_P_H |
| 11 | #define LDIF_P_H |
| 12 | |
| 13 | #include <QByteArray> |
| 14 | #include <QString> |
| 15 | |
| 16 | /*! |
| 17 | * Ldif |
| 18 | * |
| 19 | * Ldif implements an RFC 2849 compliant Ldif parser. Ldif files are used to |
| 20 | * represent directory information on LDAP-based servers, or to describe a set |
| 21 | * of changes which are to be applied to a directory. |
| 22 | * |
| 23 | * \internal |
| 24 | */ |
| 25 | class Ldif |
| 26 | { |
| 27 | public: |
| 28 | typedef enum { None, NewEntry, EndEntry, Item, Control, Err, MoreData } ParseValue; |
| 29 | |
| 30 | typedef enum { Entry_None, Entry_Add, Entry_Del, Entry_Mod, Entry_Modrdn } EntryType; |
| 31 | |
| 32 | typedef enum { Mod_None, Mod_Add, Mod_Replace, Mod_Del } ModType; |
| 33 | |
| 34 | Ldif(); |
| 35 | |
| 36 | Ldif(const Ldif &that); |
| 37 | Ldif &operator=(const Ldif &that); |
| 38 | |
| 39 | virtual ~Ldif(); |
| 40 | |
| 41 | /*! |
| 42 | * Assembles fieldname and value into a valid Ldif line, BASE64 encodes the |
| 43 | * value if necessary and optionally splits into more lines. |
| 44 | * |
| 45 | * \a fieldname The name of the entry. |
| 46 | * |
| 47 | * \a value The value of the entry. |
| 48 | * |
| 49 | * \a linelen Maximum length of the lines in the result. |
| 50 | * |
| 51 | * \a url If true, encode value as url ( use :< ). |
| 52 | */ |
| 53 | static QByteArray assembleLine(const QString &fieldname, const QByteArray &value, uint linelen = 0, bool url = false); |
| 54 | /*! |
| 55 | * This is the same as the above function, the only difference that |
| 56 | * this accepts QString as the value. |
| 57 | */ |
| 58 | static QByteArray assembleLine(const QString &fieldname, const QString &value, uint linelen = 0, bool url = false); |
| 59 | |
| 60 | /*! |
| 61 | * Splits one line from an Ldif file to attribute and value components. |
| 62 | * Returns true if value is an URL, false otherwise |
| 63 | */ |
| 64 | static bool splitLine(const QByteArray &line, QString &fieldname, QByteArray &value); |
| 65 | |
| 66 | /*! |
| 67 | * Splits a control specification (without the "control:" directive) |
| 68 | * |
| 69 | * \a line is the control directive |
| 70 | * |
| 71 | * \a oid will contain the OID |
| 72 | * |
| 73 | * \a critical will contain the criticality of control |
| 74 | * |
| 75 | * \a value is the control value |
| 76 | */ |
| 77 | static bool splitControl(const QByteArray &line, QString &oid, bool &critical, QByteArray &value); |
| 78 | |
| 79 | /*! |
| 80 | * Starts the parsing of a new Ldif |
| 81 | */ |
| 82 | void startParsing(); |
| 83 | |
| 84 | /*! |
| 85 | * Process one Ldif line |
| 86 | */ |
| 87 | ParseValue processLine(); |
| 88 | |
| 89 | /*! |
| 90 | * Process the Ldif until a complete item can be returned |
| 91 | * Returns NewEntry if a new DN encountered, Item if a new item returned, |
| 92 | * Err if the Ldif contains error, EndEntry if the parser reached the end |
| 93 | * of the current entry and MoreData if the parser encountered the end of |
| 94 | * the current chunk of the Ldif. |
| 95 | * |
| 96 | * If you want to finish the parsing after receiving MoreData, then call |
| 97 | * endLdif(), so the parser can safely flush the current entry. |
| 98 | */ |
| 99 | ParseValue nextItem(); |
| 100 | |
| 101 | /*! |
| 102 | * Sets a chunk of Ldif. Call before startParsing(), or if nextItem() |
| 103 | * returned MoreData. |
| 104 | */ |
| 105 | void setLdif(const QByteArray &ldif); |
| 106 | |
| 107 | /*! |
| 108 | * Indicates the end of the Ldif file/stream. Call if nextItem() returned |
| 109 | * MoreData, but actually you don't have more data. |
| 110 | */ |
| 111 | void endLdif(); |
| 112 | |
| 113 | /*! |
| 114 | * Returns the requested LDAP operation extracted from the current entry. |
| 115 | */ |
| 116 | EntryType entryType() const; |
| 117 | |
| 118 | /*! |
| 119 | * Returns the LDAP modify request type if entryType() returned Entry_Mod. |
| 120 | */ |
| 121 | int modType() const; |
| 122 | |
| 123 | /*! |
| 124 | * Returns the new Relative Distinguished Name if modType() returned |
| 125 | * Entry_Modrdn. |
| 126 | */ |
| 127 | QString newRdn() const; |
| 128 | |
| 129 | /*! |
| 130 | * Returns the new parent of the entry if modType() returned Entry_Modrdn. |
| 131 | */ |
| 132 | QString newSuperior() const; |
| 133 | |
| 134 | /*! |
| 135 | * Returns if the delete of the old RDN is required. |
| 136 | */ |
| 137 | bool delOldRdn() const; |
| 138 | |
| 139 | /*! |
| 140 | * Returns the attribute name. |
| 141 | */ |
| 142 | QString attr() const; |
| 143 | |
| 144 | /*! |
| 145 | * Returns the attribute value. |
| 146 | */ |
| 147 | QByteArray value() const; |
| 148 | |
| 149 | /*! |
| 150 | * Returns if val() is an url |
| 151 | */ |
| 152 | bool isUrl() const; |
| 153 | |
| 154 | /*! |
| 155 | * Returns the criticality level when modType() returned Control. |
| 156 | */ |
| 157 | bool isCritical() const; |
| 158 | |
| 159 | /*! |
| 160 | * Returns the OID when modType() returned Control. |
| 161 | */ |
| 162 | QString oid() const; |
| 163 | |
| 164 | /*! |
| 165 | * Returns the line number which the parser processes. |
| 166 | */ |
| 167 | uint lineNumber() const; |
| 168 | |
| 169 | private: |
| 170 | class LdifPrivate; |
| 171 | LdifPrivate *const d; |
| 172 | }; |
| 173 | |
| 174 | #endif |
| 175 | |