Code View

plugin / plugin-2.2.0.0 / src / dso / DSOLoader.hh
// SPDX-License-Identifier: MIT
#pragma once

#include <mutex>
#include <string>
#include <vector>

#include <dso/DSOVisibility.hh>

// Forward declarations — sign/ is optional (DSOLD_WITH_SIGN).
namespace fedem { namespace sign {
    enum class TrustLevel;
    struct VerifyResult;
} }

namespace fedem
{
  namespace dso
  {

#if defined(DSOLD_WITH_SIGN)
    // ── SignedLoadResult — returned by DSOLoader::loadSigned() ───────────────
    struct PLUGIN_EXPORT SignedLoadResult
    {
        bool        loaded  = false;
        std::string soPath;                  ///< resolved path
        sign::TrustLevel trust;              ///< see sign/TrustLevel.hh
        std::string detail;                  ///< human-readable reason
    };
#endif

    /**
     * Thread-safe DSO loader.
     *
     * Loading variants
     * ────────────────
     * load()          — load without any signature check (always available)
     * loadSigned()    — load and report trust level   (requires DSOLD_WITH_SIGN)
     * loadVerified()  — load only when trust >= min   (requires DSOLD_WITH_SIGN)
     */
    class PLUGIN_EXPORT DSOLoader
    {
      public:
        // ── Original API — always available ──────────────────────────────────
        static void load( std::string const& soName );
        static void load( std::string const& soName,
                          std::vector<std::string> const& list );

        static void loadAll( std::string const& extension,
                             std::string const& folder );
        static void loadAll( std::string const& extension );
        static void loadAll( std::string const& extension,
                             std::vector<std::string> const& list );

        static void loadAllByEnvironment( std::string const& extension,
                                          std::string const& env );
        static void loadAllByEnvironment( std::string const& extension );

        static void prefix( std::string const& pre );
        static std::string prefix();

#if defined(DSOLD_WITH_SIGN)
        // ── Signed API — available when plugin-sign + OpenSSL are present ────

        /**
         * Load DSO and verify its signature.  Loading is never blocked —
         * the caller receives the trust level and decides what to do.
         */
        static SignedLoadResult loadSigned(
            std::string const&              soName,
            std::vector<std::string> const& searchDirs,
            std::string const&              trustedKeysDir );

        /**
         * Load DSO only if trust level >= minTrust.
         * Throws fedem::exception::SignatureRejected when trust is insufficient.
         * Throws fedem::exception::FileNotFound when the DSO is not found.
         */
        static void loadVerified(
            std::string const&              soName,
            std::vector<std::string> const& searchDirs,
            std::string const&              trustedKeysDir,
            sign::TrustLevel                minTrust );
#endif

      private:
        DSOLoader()  = delete;
        ~DSOLoader() = delete;
        DSOLoader( DSOLoader const& )            = delete;
        DSOLoader( DSOLoader&& )                 = delete;
        DSOLoader& operator=( DSOLoader const& ) = delete;
        DSOLoader& operator=( DSOLoader&& )      = delete;

        static std::string loadSingle( std::string const& soName );
        static void scanDirectory( std::string const& extension,
                                   std::string const& folder,
                                   std::string& missingFiles );

        static std::string filenamePrefix;
        static std::mutex  mutex_;
    };
  }  // namespace dso
}  // namespace fedem