Commit 87f9bcea authored by jan.koester's avatar jan.koester
Browse files

test

parent b3fbad32
Loading
Loading
Loading
Loading
Loading
+1 −0
Changes for src/CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ add_executable(authdb
    admin.cpp
    authdb.cpp
    api.cpp
    log.cpp
    $<TARGET_OBJECTS:authobj>
)

+36 −2
Changes for src/authdb.cpp: 36 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@
#include "api.h"
#include "gpo_default.h"
#include "plugin.h"
#include "log.h"

#include "favicon.h"

@@ -193,6 +194,14 @@ namespace authdb {
        }

        void RequestEvent(libhttppp::HttpRequest &curreq, const int tid, ULONG_PTR args){
            std::string clientAddr;
            if(!curreq.slots.empty() && curreq.slots[0].csock)
                curreq.slots[0].csock->getAddress(clientAddr);

            const char *methods[]={"PARSE","GET","POST","DELETE","PUT"};
            int rtype=curreq.getRequestType();
            const char *method=(rtype>=0 && rtype<=4) ? methods[rtype] : "UNKNOWN";

            if(curreq.getRequestURL().compare(0,10,"/authdb.js")==0){
                libhttppp::HttpResponse rep;
                rep.setState(HTTP200);
@@ -228,12 +237,14 @@ namespace authdb {
                }
            }

            AuthBackendLock admlock(_AdminBackend);

            if(curreq.getRequestURL().compare(0,9,"/settings",9)==0){
                AuthBackendLock admlock(_AdminBackend);
                try{
                    _AdminInterface.Request(curreq,tid,args);
                    g_Log.access(clientAddr,method,curreq.getRequestURL(),200);
                }catch(AuthBackendError &e){
                    g_Log.error(clientAddr,e.what());
                    g_Log.access(clientAddr,method,curreq.getRequestURL(),500);
                    libhttppp::HttpResponse rep;
                    rep.setState(HTTP500);
                    rep.setContentType("text/html");
@@ -267,6 +278,8 @@ namespace authdb {
                    return;
                }

                {
                    AuthBackendLock admlock(_AdminBackend);
                    try {
                        apidb=db.data(did.value);
                    }catch(std::exception &e){
@@ -283,26 +296,39 @@ namespace authdb {
                        rep.send(curreq,msg,sizeof(msg));
                        return;
                    }
                }

                if(apidb){
                    apidb->lock();
                    try{
                        ApiController(*apidb,did,curreq,tid,args);
                        g_Log.access(clientAddr,method,curreq.getRequestURL(),200);
                    }catch(std::exception &e){
                        g_Log.error(clientAddr,e.what());
                        g_Log.access(clientAddr,method,curreq.getRequestURL(),500);
                        apidb->unlock();
                        throw;
                    }catch(...){
                        g_Log.access(clientAddr,method,curreq.getRequestURL(),500);
                        apidb->unlock();
                        throw;
                    }
                    apidb->unlock();
                }else{
                    g_Log.access(clientAddr,method,curreq.getRequestURL(),404);
                    libhttppp::HttpResponse rep;
                    rep.setState(HTTP404);
                    rep.setContentType("text/html");
                    rep.send(curreq,"uuid not found!",12);
                }
            }else if(curreq.getRequestURL().compare(0,10,"/getavatar",10)==0){
                AuthBackendLock admlock(_AdminBackend);
                try{
                    getAvatar(curreq,tid,args);
                    g_Log.access(clientAddr,method,curreq.getRequestURL(),200);
                }catch(AuthBackendError &e){
                    g_Log.error(clientAddr,e.what());
                    g_Log.access(clientAddr,method,curreq.getRequestURL(),500);
                    libhttppp::HttpResponse rep;
                    rep.setState(HTTP500);
                    rep.setContentType("text/html");
@@ -450,6 +476,14 @@ int main(int argc,char *argv[]){
        }
    }

#endif

    /* Create log directory and open log files */
#ifndef _WIN32
    mkdir("/var/log/authdb", 0755);
    authdb::g_Log.open("/var/log/authdb/access.log", "/var/log/authdb/error.log");
#else
    authdb::g_Log.open("access.log", "error.log");
#endif

    confplus::Config config(cmd.getCmdbyKey("config")->getValue());

src/log.cpp

0 → 100644
+93 −0
Changes for src/log.cpp: 93 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2026, 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 <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <iostream>

#include "log.h"

namespace authdb {
    Log g_Log;
}

authdb::Log::Log() {
}

authdb::Log::~Log() {
    if (_access.is_open()) _access.close();
    if (_error.is_open())  _error.close();
}

void authdb::Log::open(const std::string &accessPath, const std::string &errorPath) {
    _access.open(accessPath, std::ios::app);
    if (!_access.is_open()) {
        std::cerr << "warning: could not open access log: " << accessPath << std::endl;
    }
    _error.open(errorPath, std::ios::app);
    if (!_error.is_open()) {
        std::cerr << "warning: could not open error log: " << errorPath << std::endl;
    }
}

std::string authdb::Log::timestamp() {
    auto now = std::chrono::system_clock::now();
    auto t   = std::chrono::system_clock::to_time_t(now);
    struct tm buf;
    localtime_r(&t, &buf);
    std::ostringstream os;
    os << std::put_time(&buf, "%Y-%m-%d %H:%M:%S");
    return os.str();
}

void authdb::Log::access(const std::string &addr, const std::string &method,
                          const std::string &url, int status) {
    std::lock_guard<std::mutex> guard(_accessLock);
    if (_access.is_open()) {
        _access << timestamp() << " " << addr << " " << method
                << " " << url << " " << status << "\n";
        _access.flush();
    }
}

void authdb::Log::error(const std::string &msg) {
    std::lock_guard<std::mutex> guard(_errorLock);
    if (_error.is_open()) {
        _error << timestamp() << " [error] " << msg << "\n";
        _error.flush();
    }
}

void authdb::Log::error(const std::string &addr, const std::string &msg) {
    std::lock_guard<std::mutex> guard(_errorLock);
    if (_error.is_open()) {
        _error << timestamp() << " [error] " << addr << " " << msg << "\n";
        _error.flush();
    }
}

src/log.h

0 → 100644
+58 −0
Changes for src/log.h: 58 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2026, 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.
 *******************************************************************************/

#pragma once

#include <string>
#include <fstream>
#include <mutex>

namespace authdb {

    class Log {
    public:
        Log();
        ~Log();

        void open(const std::string &accessPath, const std::string &errorPath);

        void access(const std::string &addr, const std::string &method,
                     const std::string &url, int status);

        void error(const std::string &msg);
        void error(const std::string &addr, const std::string &msg);

    private:
        std::string timestamp();
        std::ofstream _access;
        std::ofstream _error;
        std::mutex    _accessLock;
        std::mutex    _errorLock;
    };

    extern Log g_Log;
};
+21 −16
Changes for src/session.cpp: 21 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -180,9 +180,6 @@ authdb::Session::~Session(){
};

const authdb::SessionData *authdb::Session::addSession(AuthBackend &backend,uuid::uuid domainid,uuid::uuid userid){
    std::lock_guard<std::mutex> guard(_SessionLock);

    cleanupExpired();

    std::string username;
    std::shared_ptr<authdb::AuthData::Record> cur=std::make_shared<authdb::AuthData::Record>();
@@ -230,19 +227,12 @@ const authdb::SessionData *authdb::Session::addSession(AuthBackend &backend,uuid
    uuid::uuid sid;
    sid.generate();

    if(!_firstData){
        _firstData=new SessionData(sid,userid,domainid,*mbs);
        _lastData=_firstData;
    }else{
        _lastData->_next=new SessionData(sid,userid,domainid,*mbs);
        _lastData=_lastData->_next;
    }

    _lastData->_username=username;
    SessionData *newSession=new SessionData(sid,userid,domainid,*mbs);
    newSession->_username=username;

    _lastData->_GPOResult.next=nullptr;
    newSession->_GPOResult.next=nullptr;

    SessionData::GPOResult *gpores=&_lastData->_GPOResult;
    SessionData::GPOResult *gpores=&newSession->_GPOResult;

    Gpo gpo;
    std::vector<uuid::uuid> allow;
@@ -271,7 +261,7 @@ const authdb::SessionData *authdb::Session::addSession(AuthBackend &backend,uuid
            gpodat->getAlowed(allow);

            for(const uuid::uuid &al : allow){
                for(uuid::uuid member : _lastData->_members){
                for(uuid::uuid member : newSession->_members){
                    if(member==al){
                        gpores->GPORes=gpodat->getGpoValue() ? false : true;
                    }
@@ -279,7 +269,22 @@ const authdb::SessionData *authdb::Session::addSession(AuthBackend &backend,uuid
            }
        }
    }
    return _lastData;

    {
        std::lock_guard<std::mutex> guard(_SessionLock);

        cleanupExpired();

        if(!_firstData){
            _firstData=newSession;
            _lastData=_firstData;
        }else{
            _lastData->_next=newSession;
            _lastData=_lastData->_next;
        }
    }

    return newSession;
}

const authdb::SessionData *authdb::Session::getSession(uuid::uuid sessionid){