Commit f4f7e5ef authored by jan.koester's avatar jan.koester
Browse files

added

parent 3f42300b
Loading
Loading
Loading
Loading

src/backend.cpp

0 → 100644
+96 −0
Changes for src/backend.cpp: 96 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include "backends/file.h"

#include "backend.h"

authdb::AuthBackend::AuthBackend(int type, const char* options,const char *domain) : _Type(type),_Options(options),_Domain(domain) {
    if(_Type==AuthBackendType::File){
        _Api = new class File(_Options,VERSION,_Domain);
    }
}

authdb::AuthBackend::AuthBackend(const AuthBackend &backend){
    if(backend._Type==AuthBackendType::File){
        _Api = new class File(backend._Options,VERSION,backend._Domain);
    }
}

authdb::AuthBackend::~AuthBackend(){
    if(_Type==AuthBackendType::File){
        delete ((class File*)_Api);
    }
}

void authdb::AuthBackend::read(char unsigned *src,size_t srcsize){
   _Api->read(src,srcsize);
}

void authdb::AuthBackend::write(const unsigned char* dest, size_t destsize){
   _Api->write(dest,destsize);
}

void authdb::AuthBackend::ReadAuthData(struct AuthData *adata,int pos){
    setPos(pos);
    read((unsigned char*)adata,sizeof(authdb::AuthData));
}

void authdb::AuthBackend::WriteAuthData(const struct AuthData *adata,int pos){
    newRevesion();
    setPos(pos);
    write((unsigned char*)adata,sizeof(authdb::AuthData));
}

void authdb::AuthBackend::lock(){
    _Api->lock();
}

void authdb::AuthBackend::unlock(){
    _Api->unlock();
}

void authdb::AuthBackend::setPos(size_t pos){
    _Api->setPos(pos);
}

size_t authdb::AuthBackend::getPos(){
    return _Api->getPos();
}


size_t authdb::AuthBackend::end(){
    return _Api->end();
}

size_t authdb::AuthBackend::getRevesion(){
    return _Api->getRevesion();
}

void authdb::AuthBackend::newRevesion(){
    _Api->newRevesion();
}

src/backend.h

0 → 100644
+88 −0
Changes for src/backend.h: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2024, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <stdexcept>

#include <stdbit.h>

#pragma once

namespace authdb{

    enum AuthBackendType{
        File=0,
        Ceph=1
    };

    class AuthBackendError : public std::runtime_error{
    public:
        AuthBackendError(const std::string& msg = "") : runtime_error(msg) {}
    };

    class AuthBackendApi {
    public:
        virtual void write(const unsigned char *dest,size_t destsize)=0;
        virtual void read(unsigned char *src,size_t srcsize)=0;
        virtual void lock()=0;
        virtual void unlock()=0;
        virtual void setPos(size_t pos)=0;
        virtual size_t getPos()=0;
        virtual size_t end()=0;
        virtual size_t getRevesion()=0;
        virtual void newRevesion()=0;
    };

    class AuthBackend {
    public:
        AuthBackend(int type,const char *options,const char *domain);
        AuthBackend(const AuthBackend &backend);
        ~AuthBackend();

        void ReadAuthData(struct AuthData *adata,int pos);
        void WriteAuthData(const struct AuthData *adata,int pos);

        void lock();
        void unlock();

        void setPos(size_t pos);
        size_t getPos();

        size_t getRevesion();
        void   newRevesion();

        size_t end();

        void write(const unsigned char *dest,size_t destsize);
        void read(char unsigned *src,size_t srcsize);
    private:
        int             _Type;
        AuthBackendApi *_Api;
        const char     *_Options;
        const char     *_Domain;
    };

};