Commit 837058f9 authored by jan.koester's avatar jan.koester
Browse files

needs testing

parent 6ff2cd19
Loading
Loading
Loading
Loading
+2 −2
Changes for CMakeLists.txt: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -9,19 +9,19 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
find_package(libnetplus REQUIRED)
find_package(libhtmlpp REQUIRED)
find_package(libhttppp REQUIRED)
find_package(OpenLDAP)
find_package(Hiredis REQUIRED)
find_package(libsecureid REQUIRED)
find_package(Brotli REQUIRED)
find_package(libcmdplus REQUIRED)
find_package(libconfplus REQUIRED)
find_package(libdbpp REQUIRED)
find_package(authdb REQUIRED)
find_package(json-c REQUIRED)

include_directories(
    ${NETPLUS_INCLUDE_DIRS}
    ${HTTPPP_INCLUDE_DIRS}
    ${CRYPTPLUS_INCLUDE_DIRS}
    ${OPENLDAP_INCLUDE_DIRS}
    ${CMAKE_SOURCE_DIR}/src
    ${SECUREID_INCLUDE_DIRS}
    ${DBPP_INCLUDE_DIR}
+2 −1
Changes for src/CMakeLists.txt: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -30,10 +30,11 @@ target_link_libraries(blogidev PUBLIC
    cmdplus::cmdplus
    ${PostgreSQL_LIBRARIES}
    ${SQLite3_LIBRARIES}
    ${OPENLDAP_LIBRARIES}
    ${BROTLI_LIBRARIES}
    ${SMTPCLIENT_LIBRARY}
    json-c::json-c
    confplus::confplus
    authdb::client
)

target_link_libraries(blogidev PRIVATE
+20 −158
Changes for src/auth.cpp: 20 added lines, 158 removed lines.
Original line number Diff line number Diff line
@@ -54,180 +54,42 @@ blogi::Auth::Auth(dbpp::Database **pcon,blogi::Config *cfg){
        <<      "password character varying(255)"
        << ");";
    _dbconn[0]->exec(sql,res);
}

blogi::Auth::~Auth(){

}

bool blogi::Auth::login(const int tid,const char* username, const char* password, const char *ssid){
#ifndef LDAPSUPPORT
    return locallogin(username,password,ssid);
#else
    bool ret=ldapLogin(tid,username,password,ssid);
    if(!ret)
        ret =locallogin(tid,username,password,ssid);
    return ret;
#endif
}

bool blogi::Auth::locallogin(const int tid,const char* username, const char* password, const char *ssid){
    dbpp::SQL sql;
    dbpp::DBResult res;

    std::vector<char> sbuf;

    sql << "SELECT sid,id,username FROM users WHERE username='" << dbpp::SQL::escaped(sbuf,username) << "' LIMIT 1;";
    return false;
}

#ifdef LDAPSUPPORT
bool blogi::Auth::ldapLogin(const int tid,const char *username,const char *password,const char *ssid){
    libhttppp::HTTPException excep;

    LDAP* userldap;
    LDAPControl* userserverctls = nullptr;
    LDAPControl* userclientctls = nullptr;

    int ulerr = ldap_initialize(&userldap, _config->getlphost());

    if (ulerr != LDAP_SUCCESS) {
        excep[libhttppp::HTTPException::Critical] << "User Ldap init failed: "
        << ldap_err2string(ulerr);
        throw excep;
    }

    int ldap_vers = LDAP_VERSION3;
    ldap_set_option(userldap, LDAP_OPT_PROTOCOL_VERSION, &ldap_vers);

    ulerr = ldap_start_tls_s(userldap, &userserverctls, &userclientctls);

    if (ulerr != LDAP_SUCCESS) {
         excep[libhttppp::HTTPException::Critical] << "User Ldap Tls failed: "
         << ldap_err2string(ulerr);
         throw excep;
    }
    _AuthCon.setUrl(_config->getAuthUrl());
    _AuthCon.setClientName(_config->getClientName());
    _AuthCon.setClientSecret(_config->getClientSecret());

    berval cred, * servercred;
    cred.bv_val = (char*)password;
    cred.bv_len = strlen(password);
    ulerr = ldap_sasl_bind_s(userldap, username, nullptr,
                             &cred, &userserverctls, &userclientctls, &servercred);
    authdb::client::Client clt(_AuthCon);
    clt.ClientAuth();

    if (ulerr != LDAP_SUCCESS) {
        excep[libhttppp::HTTPException::Note] << "User Ldap Login failed: "
        << ldap_err2string(ulerr);
        throw excep;
}

    char* attrs[] = { (char*)"objectSid",(char*)"userPrincipalName",(char*)"mail",(char*)"gecos",nullptr };
    LDAPMessage* answer, * entry;
    struct timeval timeout;

    timeout.tv_sec = 1;
    timeout.tv_usec =0;

    ulerr = ldap_search_ext_s(userldap, _config->getlpbasedn(), LDAP_SCOPE_SUBTREE, _config->getlpfilter(), attrs, 0, &userserverctls, &userclientctls, &timeout, 0, &answer);

    if (ulerr != LDAP_SUCCESS) {
        excep[libhttppp::HTTPException::Note] << "<span>User can'T find uid !</span>";
        throw excep;
    }

    size_t entries_found = ldap_count_entries(userldap, answer);

    if (entries_found <= 0) {
        excep[libhttppp::HTTPException::Note] << "<span>User can'T find uid !</span>";
        throw excep;
    }

    char sid[512],email[255],displayname[512];
    BerElement* ber;
    char* dn;
    char* attribute;

    for (entry = ldap_first_entry(userldap, answer);
         entry != NULL; entry = ldap_next_entry(userldap, entry)) {

        dn = ldap_get_dn(userldap, entry);
        berval** values;

blogi::Auth::~Auth(){

        for (attribute = ldap_first_attribute(userldap, entry, &ber);
         attribute != nullptr; attribute = ldap_next_attribute(userldap, entry, ber)) {
            if ((values = ldap_get_values_len(userldap, entry, attribute))) {
                if (strcmp(attribute, "userPrincipalName") == 0) {
                    for (int i = 0; values[i]; i++) {
                        if (strncmp(username, values[i]->bv_val, values[i]->bv_len) == 0) {
                            ldap_value_free_len(values);
                            goto LDAPLOGINUSERFOUND;
                        }
                    }
                }
            }
            ldap_value_free_len(values);
        }
}

    ldap_memfree(dn);
    ldap_msgfree(answer);
    ldap_unbind_ext_s(userldap, &userserverctls, &userclientctls);

    return false;


LDAPLOGINUSERFOUND:
         SID* mysid;
         initSID(&mysid);
         berval** values;
         for (attribute = ldap_first_attribute(userldap, entry, &ber);
              attribute != nullptr; attribute = ldap_next_attribute(userldap, entry, ber)) {
             if ((values = ldap_get_values_len(userldap, entry, attribute))) {
                 if (strcmp(attribute, "objectSid") == 0) {
                     SIDcpy(mysid,(SID*)values[0]->bv_val);
                 }else if (strcmp(attribute, "mail") == 0) {
                     memcpy(&email,values[0]->bv_val,values[0]->bv_len);
                     email[values[0]->bv_len]='\0';
                 }else if (strcmp(attribute, "gecos") == 0) {
                     memcpy(&displayname,values[0]->bv_val,values[0]->bv_len);
                     displayname[values[0]->bv_len]='\0';
                 }
             }
bool blogi::Auth::login(const int tid,const char* username, const char* password, const char *ssid){
    authdb::client::Client clt(_AuthCon);
    _AuthCon.setUsername(username);
    _AuthCon.setUserPassword(password);
    bool ret = clt.UserAuth();
    Session sess(_dbconn[tid]);
    ret=clt.GPOcheck("e7d3b8b3-4825-11f0-ae2b-3cecefce9cb6");
    if(ret){
        sess.addSessionData(ssid,"authid",_AuthCon.getSessionID().c_str());
    }
        ldap_value_free_len(values);

        printSID(mysid,sid,512);
        destroySID(mysid);

        dbpp::SQL sql;
        dbpp::DBResult res;
        sql = "SELECT sid,id FROM users WHERE sid='"; sql << sid << "' LIMIT 1;";

        if (_dbconn[tid]->exec(sql,res) < 1) {
           sql <<  "INSERT INTO users (sid,username,email,authprovider,displayname) VALUES ('" << sid <<"','" << username << "','" << email <<"','1','" << displayname << "');";
           _dbconn[tid]->exec(sql,res);
        };

        Session sess(*_dbconn);

        sess.addSessionData(ssid,"uid",sid);
        sess.addSessionData(ssid,"username",username);

        std::cout << "User : " << sid << " are logged in" << std::endl;

        ldap_memfree(dn);
        ldap_msgfree(answer);
        ldap_unbind_ext_s(userldap, &userserverctls, &userclientctls);
        return true;
    return ret;
}
#endif

bool blogi::Auth::isLoggedIn(const int tid,libhttppp::HttpRequest *curreq,const char *ssid){
        try{
            std::string storeid;
            Session sess(_dbconn[tid]);
            sess.getSessionData(ssid,"uid",storeid);
            if(!storeid.empty()){
            sess.getSessionData(ssid,"authid",storeid);
            _AuthCon.setSessionID(storeid.c_str());
           authdb::client::Client clt(_AuthCon);
            if(clt.GPOcheck("e7d3b8b3-4825-11f0-ae2b-3cecefce9cb6")){
                return true;
            }
        }catch(...){};
+7 −16
Changes for src/auth.h: 7 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -25,6 +25,11 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <cstdio>

#include <authdb/client.h>
#include <authdb/uuid.h>

#include <httppp/http.h>

#include <dbpp/database.h>
@@ -34,13 +39,6 @@

#pragma once

extern "C" {
    #include <stdio.h>
#ifdef LDAPSUPPORT
    #include <ldap.h>
#endif
}

namespace blogi {

    class Auth{
@@ -50,16 +48,9 @@ namespace blogi {
        bool login(const int tid,const char *username,const char *password,const char *ssid);
        bool isLoggedIn(const int tid,libhttppp::HttpRequest *curreq,const char *ssid);
    private:
#ifdef LDAPSUPPORT
        bool ldapLogin(const int tid,const char *username,const char *password,const char *ssid);
#endif
        bool locallogin(const int tid,const char *username,const char *password,const char *ssid);

        authdb::client::ClientConnection _AuthCon;
        dbpp::Database                        **_dbconn;
        blogi::Config                               *_config;
#ifdef LDAPSUPPORT
        LDAPControl      *_serverctrls;
        LDAPControl      *_clientctrls;
#endif

    };
};
+15 −24
Changes for src/conf.cpp: 15 added lines, 24 removed lines.
Original line number Diff line number Diff line
@@ -42,17 +42,14 @@ blogi::Config::Config(const char *path) : confplus::Config(path){
    if(getKey("/BLOGI/DATABASE/CONNECTION"))
        _DBConnection=getValue(getKey("/BLOGI/DATABASE/CONNECTION"),0);

    if(getKey("/BLOGI/LDAP/DOMAIN"))
        _LDAPDomain=getValue(getKey("/BLOGI/LDAP/DOMAIN"),0);
    if(getKey("/BLOGI/AUTHDB/URL"))
        _AuthUrl=getValue(getKey("/BLOGI/AUTHDB/URL"),0);

    if(getKey("/BLOGI/LDAP/HOST"))
        _LDAPHost=getValue(getKey("/BLOGI/LDAP/HOST"),0);
    if(getKey("/BLOGI/AUTHDB/CLIENTNAME"))
        _ClientName=getValue(getKey("/BLOGI/AUTHDB/CLIENTNAME"),0);

    if(getKey("/BLOGI/LDAP/BASEDN"))
        _LDAPBase=getValue(getKey("/BLOGI/LDAP/BASEDN"),0);

    if(getKey("/BLOGI/LDAP/LOGINFILTER"))
        _LDAPFilter=getValue(getKey("/BLOGI/LDAP/LOGINFILTER"),0);
    if(getKey("/BLOGI/AUTHDB/CLIENTSECRET"))
        _ClientSecret=getValue(getKey("/BLOGI/AUTHDB/CLIENTSECRET"),0);

    if(getKey("/BLOGI/HTTP/BIND"))
        _HttpBind=getValue(getKey("/BLOGI/HTTP/BIND"),0);
@@ -147,28 +144,22 @@ const char * blogi::Config::getdbopts(){
    return _DBConnection.c_str();
}

const char * blogi::Config::getlpdomain(){
    if(_LDAPDomain.empty())
        return nullptr;
    return _LDAPDomain.c_str();
}

const char *blogi::Config::getlphost(){
    if(_LDAPHost.empty())
const char * blogi::Config::getAuthUrl(){
    if(_AuthUrl.empty())
        return nullptr;
    return _LDAPHost.c_str();
    return _AuthUrl.c_str();
}

const char * blogi::Config::getlpbasedn(){
    if(_LDAPBase.empty())
const char *blogi::Config::getClientName(){
    if(_ClientName.empty())
        return nullptr;
    return _LDAPBase.c_str();
    return _ClientName.c_str();
}

const char * blogi::Config::getlpfilter(){
    if(_LDAPFilter.empty())
const char * blogi::Config::getClientSecret(){
    if(_ClientSecret.empty())
        return nullptr;
    return _LDAPFilter.c_str();
    return _ClientSecret.c_str();
}

const char * blogi::Config::gethttpaddr(){
Loading