File indexing completed on 2023-09-24 11:39:22

0001 /*
0002     A temporary copy to break dependency to KLDAP
0003 
0004     This file is part of libkldap.
0005     SPDX-FileCopyrightText: 2004-2006 Szombathelyi Gy├Ârgy <gyurco@freemail.hu>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef LDIF_P_H
0011 #define LDIF_P_H
0012 
0013 #include <QByteArray>
0014 #include <QString>
0015 
0016 /**
0017  * Ldif
0018  *
0019  * Ldif implements an RFC 2849 compliant Ldif parser. Ldif files are used to
0020  * represent directory information on LDAP-based servers, or to describe a set
0021  * of changes which are to be applied to a directory.
0022  */
0023 class Ldif
0024 {
0025 public:
0026     typedef enum { None, NewEntry, EndEntry, Item, Control, Err, MoreData } ParseValue;
0027 
0028     typedef enum { Entry_None, Entry_Add, Entry_Del, Entry_Mod, Entry_Modrdn } EntryType;
0029 
0030     typedef enum { Mod_None, Mod_Add, Mod_Replace, Mod_Del } ModType;
0031 
0032     Ldif();
0033 
0034     Ldif(const Ldif &that);
0035     Ldif &operator=(const Ldif &that);
0036 
0037     virtual ~Ldif();
0038 
0039     /**
0040      * Assembles fieldname and value into a valid Ldif line, BASE64 encodes the
0041      * value if necessary and optionally splits into more lines.
0042      * @param fieldname The name of the entry.
0043      * @param value The value of the entry.
0044      * @param linelen Maximum length of the lines in the result.
0045      * @param url If true, encode value as url ( use :< ).
0046      */
0047     static QByteArray assembleLine(const QString &fieldname, const QByteArray &value, uint linelen = 0, bool url = false);
0048     /**
0049      * This is the same as the above function, the only difference that
0050      * this accepts QString as the value.
0051      */
0052     static QByteArray assembleLine(const QString &fieldname, const QString &value, uint linelen = 0, bool url = false);
0053 
0054     /**
0055      * Splits one line from an Ldif file to attribute and value components.
0056      * @return true if value is an URL, false otherwise
0057      */
0058     static bool splitLine(const QByteArray &line, QString &fieldname, QByteArray &value);
0059 
0060     /**
0061      * Splits a control specification (without the "control:" directive)
0062      * @param line is the control directive
0063      * @param oid will contain the OID
0064      * @param critical will contain the criticality of control
0065      * @param value is the control value
0066      */
0067     static bool splitControl(const QByteArray &line, QString &oid, bool &critical, QByteArray &value);
0068 
0069     /**
0070      * Starts the parsing of a new Ldif
0071      */
0072     void startParsing();
0073 
0074     /**
0075      * Process one Ldif line
0076      */
0077     ParseValue processLine();
0078 
0079     /**
0080      * Process the Ldif until a complete item can be returned
0081      * @return NewEntry if a new DN encountered, Item if a new item returned,
0082      * Err if the Ldif contains error, EndEntry if the parser reached the end
0083      * of the current entry and MoreData if the parser encountered the end of
0084      * the current chunk of the Ldif.
0085      *
0086      * If you want to finish the parsing after receiving MoreData, then call
0087      * endLdif(), so the parser can safely flush the current entry.
0088      */
0089     ParseValue nextItem();
0090 
0091     /**
0092      * Sets a chunk of Ldif. Call before startParsing(), or if nextItem()
0093      * returned MoreData.
0094      */
0095     void setLdif(const QByteArray &ldif);
0096 
0097     /**
0098      * Indicates the end of the Ldif file/stream. Call if nextItem() returned
0099      * MoreData, but actually you don't have more data.
0100      */
0101     void endLdif();
0102 
0103     /**
0104      * Returns the requested LDAP operation extracted from the current entry.
0105      */
0106     EntryType entryType() const;
0107 
0108     /**
0109      * Returns the LDAP modify request type if entryType() returned Entry_Mod.
0110      */
0111     int modType() const;
0112 
0113     /**
0114      * Returns the new Relative Distinguished Name if modType() returned
0115      * Entry_Modrdn.
0116      */
0117     QString newRdn() const;
0118 
0119     /**
0120      * Returns the new parent of the entry if modType() returned Entry_Modrdn.
0121      */
0122     QString newSuperior() const;
0123 
0124     /**
0125      * Returns if the delete of the old RDN is required.
0126      */
0127     bool delOldRdn() const;
0128 
0129     /**
0130      * Returns the attribute name.
0131      */
0132     QString attr() const;
0133 
0134     /**
0135      * Returns the attribute value.
0136      */
0137     QByteArray value() const;
0138 
0139     /**
0140      * Returns if val() is an url
0141      */
0142     bool isUrl() const;
0143 
0144     /**
0145      * Returns the criticality level when modType() returned Control.
0146      */
0147     bool isCritical() const;
0148 
0149     /**
0150      * Returns the OID when modType() returned Control.
0151      */
0152     QString oid() const;
0153 
0154     /**
0155      * Returns the line number which the parser processes.
0156      */
0157     uint lineNumber() const;
0158 
0159 private:
0160     class LdifPrivate;
0161     LdifPrivate *const d;
0162 };
0163 
0164 #endif