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

initial work domain support

parent a7f17fb7
Loading
Loading
Loading
Loading

data/authdb.js

0 → 100644
+10 −0
Changes for data/authdb.js: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
const selectElement = document.querySelector("#domain_selector");
const urlresult = window.location.toString();

selectElement.addEventListener("change", (event) => {
    var url = urlresult;
    url += "?domain=";
    url += selectElement.value;
    window.location.replace(url);
});
+11 −2
Changes for data/index.html: 11 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@
                color:#ffffff;
            }

            #content{
            #maincell{
                display: table-cell;
                border:solid 10px #222528;
                width:100%;
@@ -79,6 +79,10 @@
                margin:1em;
            }

            input.domaintext {
                min-width:30vw;
            }

        </style>
    </head>
    <body>
@@ -88,7 +92,7 @@
        <div id="maintable">
            <div id="navbar">
                <ul>
                    <li><a href="#" >Domains</a></li>
                    <li><a href="/admin/listdomains" >Domains</a></li>
                    <li><a href="/admin/listusers" >Users</a></li>
                    <li><a href="#" >Groups</a></li>
                    <li><a href="#" >Clients</a></li>
@@ -96,8 +100,13 @@
                    <li><a href="#" >Sessions</a></li>
                </ul>
            </div>
            <div id="maincell">
                <div id="domain">
                </div>
                <script src="authdb.js"></script>
                <div id="content">
                </div>
            </div>
        </div>
    </body>
</html>
+1 −0
Changes for src/CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ list(APPEND authdbSrcs
    authdb.cpp
    backend.cpp
    user.cpp
    domain.cpp
    hash.cpp
    hash/sha512.cpp
)
+84 −15
Changes for src/authdb.cpp: 84 added lines, 15 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <cstring>
#include <sstream>
#include <map>
#include <fstream>

#include <uuid/uuid.h>
#include <cmdplus/cmdplus.h>
@@ -52,18 +53,25 @@ namespace authdb {
    public:
        AuthDB(authdb::AuthBackend &backend,netplus::socket *ssock) : HttpEvent(ssock), _Backend(backend){
            _Indexpage.loadFile(_IndexElement,"../data/index.html");

            std::ifstream authfs("../data/authdb.js", std::ios::ate);

            size_t asize = authfs.tellg();
            _AuthJs.resize(asize);
            authfs.seekg(0);
            authfs.read(_AuthJs.data(),asize);

        };

        void listUsers(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            _Backend.lock();

            size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end();

            libhtmlpp::HtmlString userlist,out;
            libhtmlpp::HtmlString userlist,out,dform;

            libhtmlpp::HtmlElement root=_IndexElement;

            libhtmlpp::HtmlElement *content=root.getElementbyID("content");
            libhtmlpp::HtmlElement *domain=root.getElementbyID("domain");

            User user;

@@ -108,10 +116,17 @@ namespace authdb {
                }
            }

            _Backend.unlock();
            dform << "<form>"
                  << "<label for=\"domain_selector\">Select Domain:</label>"
                  << "<select id=\"domain_selector\" name=\"domain_selector\">"
                  << "<option value=\"admin.local\" >admin.local</option>"
                  << "<option value=\"tuxist.de\" >tuxist.de</option>"
                  << "</select>"
                  << "</form>";

            userlist << "</table></div><div><ul class=\"usertoolbar\" ><li><a class=\"button\" href=\"/admin/createuser\">CreateUser</a></li></ul></div>";
            domain->insertChild(dform.parse());

            userlist << "</table></div><div><ul class=\"usertoolbar\" ><li><a class=\"button\" href=\"/admin/createuser\">CreateUser</a></li></ul></div>";

            content->insertChild(userlist.parse());

@@ -200,10 +215,8 @@ namespace authdb {
                    udat.Data->append(cudat);
                    delete[] cudat.data;
                }
                _Backend.lock();
                User user;
                user.create(_Backend,&udat);
                _Backend.unlock();
                std::cout << "user created!" << std::endl;
            }

@@ -253,9 +266,7 @@ namespace authdb {

            uuid_parse(uid,uuid);

            _Backend.lock();
            user.remove(_Backend,uuid);
            _Backend.unlock();

            libhttppp::HttpResponse rep;
            rep.setState(HTTP307);
@@ -273,8 +284,6 @@ namespace authdb {
            uuid_t uid;
            uuid_parse(cuid,uid);

            _Backend.lock();

            size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end();

            while(rd<end){
@@ -300,11 +309,9 @@ namespace authdb {
                rep.send(curreq,nullptr,0);
            }

            _Backend.unlock();
        };

        void editUser(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            _Backend.lock();
            size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end();
            char cuid[255];

@@ -387,7 +394,6 @@ namespace authdb {

            if(data){
                editRecord(_Backend,editrec,UserData);
                _Backend.unlock();
                libhttppp::HttpResponse rep;
                rep.setState(HTTP307);
                rep.setVersion(HTTPVERSION(1.1));
@@ -423,7 +429,6 @@ namespace authdb {
                    delete[] cur.data;
                }
            }
            _Backend.unlock();

            form  << "<tr><td><label for=\"avatar\">ProfilBild:</label></td>"
                  << "<td><input type=\"file\" name=\"avatar\"></td></tr>"
@@ -444,7 +449,61 @@ namespace authdb {
            rep.send(curreq,out.c_str(),out.size());
        }

        void listDomains(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            libhtmlpp::HtmlString list,out;

            libhtmlpp::HtmlElement root=_IndexElement;
            libhtmlpp::HtmlElement *content=root.getElementbyID("content");

            list  << "<div><ul class=\"usertoolbar\" >"
                  << "<li><a class=\"button\" href=\"/admin/createdomain\">CreateDomain</a>"
                  << "</li></ul></div>";

            content->insertChild(list.parse());

            libhtmlpp::print(&root,out,true);

            libhttppp::HttpResponse rep;
            rep.setContentType("text/html");
            rep.setContentLength(out.size());
            rep.send(curreq,out.c_str(),out.size());
        }

        void createDomain(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            libhtmlpp::HtmlString form,out;

            libhtmlpp::HtmlElement root=_IndexElement;
            libhtmlpp::HtmlElement *content=root.getElementbyID("content");

            form  << "<form method=\"post\" enctype=\"multipart/form-data\">"
                  << "<table>"

                  << "<tr><td><label for=\"domainname\">Domain Name:</label></td>"
                  << "<td><input class=\"domaintext\" type=\"text\" name=\"domainname\" value=\"tuxist.de\"></td></tr>"

                  << "<tr><td><label for=\"domainstorage\">Domain Storage:</label></td>"
                  << "<td><select name=\"domainstorage\">"
                  << "<option value=\"file\">File</option>"
                  << "</select></td></tr>"

                  << "<tr><td><label for=\"domainname\">Domain Options:</label></td>"
                  << "<td><input class=\"domaintext\" type=\"text\" name=\"options\" value=\"/var/lib/authdb/tuxist.de.db\"></td></tr>"

                  << "<tr><td></td><td><input type=\"submit\" value=\"save\" ></td></tr>"
                  << "</table>";

            content->insertChild(form.parse());

            libhtmlpp::print(&root,out,true);

            libhttppp::HttpResponse rep;
            rep.setContentType("text/html");
            rep.setContentLength(out.size());
            rep.send(curreq,out.c_str(),out.size());
        }

        void AdminController(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
            _Backend.lock();
            try {
                if(strncmp(curreq->getRequestURL(),"/admin/listusers",16)==0){
                    listUsers(curreq,tid,args);
@@ -456,6 +515,14 @@ namespace authdb {
                    editUser(curreq,tid,args);
                }else if(strncmp(curreq->getRequestURL(),"/admin/getavatar/",17)==0){
                    getAvatar(curreq,tid,args);
                }else if(strncmp(curreq->getRequestURL(),"/admin/listdomains/",18)==0){
                    listDomains(curreq,tid,args);
                }else if(strncmp(curreq->getRequestURL(),"/admin/createdomain/",19)==0){
                    createDomain(curreq,tid,args);
                }else if(strncmp(curreq->getRequestURL(),"/admin/authdb.js",16)==0){
                    libhttppp::HttpResponse rep;
                    rep.setContentType("text/javascript");
                    rep.send(curreq,_AuthJs.data(),_AuthJs.size());
                }else{
                    libhtmlpp::HtmlElement root=_IndexElement;
                    libhtmlpp::HtmlString out;
@@ -472,6 +539,7 @@ namespace authdb {
                    rep.setContentType("text/html");
                    rep.send(curreq,e.what(),strlen(e.what()));
            }
            _Backend.unlock();
        }

        void RequestEvent(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
@@ -492,6 +560,7 @@ namespace authdb {
        authdb::AuthBackend     _Backend;
        libhtmlpp::HtmlElement  _IndexElement;
        libhtmlpp::HtmlPage     _Indexpage;
        std::vector<char>       _AuthJs;
    };

    int searchValue(authdb::AuthBackend &backend,const char*fieldname,const char *value){
+235 −0
Changes for src/domain.cpp: 235 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 <cstring>

#include "domain.h"
#include "types.h"

namespace authdb {
    inline void setDomainData(AuthData *dat,AuthData::Record **varclass,const char *varname,const char *value){
        AuthData::Record *target=*varclass;

        AuthData *curec=dat;

        if(!target){
            while(curec){
                if(curec->Data->type == authdb::DomainData &&
                    uuid_compare(curec->Data->uuid,dat->Data->uuid)==0 &&
                    strcmp(curec->Data->fieldname,varname)==0
                ){

                    target=curec->Data;
                    target->datasize=0;
                    delete[] target->data;
                    target->data=nullptr;
                    break;
                }
                curec=curec->next();
            }
        }

        if(!target){
            target=new AuthData::Record;
        }

        target->start=0xFE;
        uuid_copy(target->uuid,dat->Data->uuid);
        target->type=DataType::DomainData;
        target->storage=StorageType::TextStorage;

        snprintf(target->fieldname,255,"%s",varname);
        target->datasize=strlen(value)+1;
        target->data= new char[target->datasize];
        strcpy(target->data,value);

        *varclass=dat->append(*target)->Data;
    }

    inline const char *getDomainData(const AuthData *dat,AuthData::Record **varclass,const char *varname){
        if(*varclass && (*varclass)->data ){
            return (*varclass)->data;
        }else{
            for(const AuthData *curdat=dat; curdat; curdat=curdat->next()){
                if(curdat->Data && curdat->Data->storage == StorageType::TextStorage &&
                    curdat->Data->type==DataType::DomainData
                    && strcmp(curdat->Data->fieldname,varname)==0){
                    *varclass=curdat->Data;
                    return curdat->Data->data;
                }
            }
        }
        std::string err;
        err=varname;
        err+=" data not found !";
        throw(authdb::AuthBackendError(err.c_str()));
    }

    inline void setDomainData(AuthData *dat,AuthData::Record **varclass,const char *varname,int value){
        AuthData::Record *target=*varclass;

        AuthData *curec=dat;

        if(!target){
            while(curec){
                if(curec->Data->type == authdb::DomainData &&
                    uuid_compare(curec->Data->uuid,dat->Data->uuid)==0 &&
                    strcmp(curec->Data->fieldname,varname)==0
                ){

                    target=curec->Data;
                    target->datasize=0;
                    delete[] target->data;
                    target->data=nullptr;
                    break;
                }
                curec=curec->next();
            }
        }

        if(!target){
            target=new AuthData::Record;
        }

        target->start=0xFE;
        uuid_copy(target->uuid,dat->Data->uuid);
        target->type=DataType::DomainData;
        target->storage=StorageType::IntStorage;

        snprintf(target->fieldname,255,"%s",varname);
        target->datasize=sizeof(int);
        target->data= new char[target->datasize];
        memcpy(target->data,&value,target->datasize);

        *varclass=dat->append(*target)->Data;
    }

    inline int getIntDomainData(const AuthData *dat,AuthData::Record **varclass,const char *varname){
        if(*varclass && (*varclass)->data ){
            return *(*varclass)->data;
        }else{
            for(const AuthData *curdat=dat; curdat; curdat=curdat->next()){
                if(curdat->Data && curdat->Data->storage == StorageType::TextStorage &&
                    curdat->Data->type==DataType::DomainData
                    && strcmp(curdat->Data->fieldname,varname)==0){
                    *varclass=curdat->Data;
                    return *curdat->Data->data;
                }
            }
        }
        std::string err;
        err=varname;
        err+=" data not found !";
        throw(authdb::AuthBackendError(err.c_str()));
    }
};

authdb::DomainData::DomainData(){
    uuid_generate(did);
    Data=new AuthData(did);
}

authdb::DomainData::~DomainData(){
    uuid_clear(did);
    delete Data;
}

void authdb::DomainData::setDomainName(const char* name){
    setDomainData(this->Data,&_name,"domainName",name);
}

const char * authdb::DomainData::getDomainName(){
    return getDomainData(this->Data,&_name,"domainName");
}


void authdb::DomainData::setStorageType(int type){
    setDomainData(this->Data,&_storageType,"domainStorage",type);
}

int authdb::DomainData::getStorageType(){
    return getIntDomainData(this->Data,&_storageType,"domainStorage");
}

void authdb::DomainData::setStorageOptions(const char *opts){
    setDomainData(this->Data,&_storageOptions,"domainOptions",opts);
}

const char * authdb::DomainData::getStorageOptions(){
    return getDomainData(this->Data,&_storageOptions,"domainOptions");
}

void authdb::Domain::create(authdb::AuthBackend& backend,class DomainData *data){
    int exi=0;

    if(exits(backend,&data->did,data->getDomainName(),exi)){
       switch(exi){
           case 1:
                throw authdb::AuthBackendError("Create User: Username already Exists aborting!");
           default:
                throw authdb::AuthBackendError("Create User: UUID already Exists aborting!");
       }
    }

    size_t wd=backend.end();

    for(AuthData *curec=data->Data; curec; curec=curec->next()){
         curec->Data->type=DataType::DomainData;
         backend.setPos(wd);
         backend.write((unsigned char*)curec->Data,sizeof(struct AuthData::Record));
         backend.write((unsigned char*)curec->Data->data,curec->Data->datasize);
         wd=backend.end();
    }
}

bool authdb::Domain::exits(AuthBackend& backend, uuid_t* did, const char* name,int &ret){
    authdb::AuthData::Record cur;

    size_t rd=sizeof(authdb::AuthHeader),end=backend.end();

    while(rd<end){
        backend.setPos(rd);
        backend.read((unsigned char*)&cur,sizeof(AuthData::Record));
        rd=backend.getPos()+cur.datasize;
        if(strcmp(cur.fieldname,"domainname")==0 ){
            cur.data=new char[cur.datasize];
            backend.read((unsigned char*)cur.data,cur.datasize);
            if(name && strcmp(cur.data,name) == 0){
                delete[] cur.data;
                ret=1;
                return true;

            }else if(did && uuid_compare(cur.uuid,*did)==0){
                delete[] cur.data;
                ret=2;
                return true;
            }
            delete[] cur.data;
        }
    }
    ret=-1;
    return false;
}
Loading