/******************************************************************************* * 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 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 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 #include #include #include #include #include #include #include "backend.h" #include "authdb.h" namespace authdb { class AuthDB : public libhttppp::HttpEvent{ public: AuthDB(authdb::AuthBackend &backend,netplus::socket *ssock) : HttpEvent(ssock), _Backend(backend){ }; void RequestEvent(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){ _Backend.lock(); authdb::AuthData cur; size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end(); _Backend.setPos(rd); std::string userlist="UserList:\r\n"; while(rdnext()){ if(strcmp(curec->Data.fieldname,"username")!=0){ userlist+=" "; userlist+=curec->Data.fieldname; userlist+=": "; std::copy(curec->Data.data,curec->Data.data+curec->Data.datasize, std::back_inserter(userlist)); userlist+= "\r\n"; } } } } _Backend.unlock(); libhttppp::HttpResponse rep; rep.setContentType("text/txt"); rep.setContentLength(userlist.length()); rep.send(curreq,userlist.c_str(),userlist.length()); }; private: authdb::AuthBackend &_Backend; }; int searchValue(authdb::AuthBackend &backend,const char*fieldname,const char *value){ authdb::AuthData user,attr; int rd=sizeof(authdb::AuthHeader),brd=rd; while(rd>backend.end()){ backend.ReadAuthData(user,rd); rd=backend.getPos(); if(strcmp(user.fieldname,fieldname) == 0){ if(strcmp(user.data,value) == 0){ delete[] user.data; return brd; } } brd=rd; delete[] user.data; } return -1; } AuthDataRecord::AuthDataRecord(){ _next=nullptr; } AuthDataRecord::AuthDataRecord(const AuthDataRecord &src){ memcpy(&Data,&src.Data,sizeof(src.Data)); memcpy(Data.data,src.Data.data,src.Data.datasize); _next=nullptr; } AuthDataRecord::~AuthDataRecord(){ } AuthDataRecord *AuthDataRecord::next(){ return _next; }; bool getRecord(authdb::AuthBackend &backend,AuthDataRecord *dest,uuid_t id,int type){ bool found=false; authdb::AuthData cur; int rd=sizeof(authdb::AuthHeader),end=backend.end(); char plainid[255]; uuid_unparse(id,plainid); AuthDataRecord *curec=dest; while(rdData,&cur,sizeof(AuthData)); curec->Data.data=new char(cur.datasize); memcpy(curec->Data.data,cur.data,cur.datasize); if(rd_next=new AuthDataRecord; curec=curec->_next; } found=true; } delete[] cur.data; } return found; } }; int main(int argc, char *argv[]){ cmdplus::CmdController &cmd=cmdplus::CmdController::getInstance(); cmd.registerCmd("config",'c',true,nullptr,"Config Path"); cmd.parseCmd(argc,argv); if(!cmd.checkRequired()){ std::cerr << "Config Path required !" << std::endl; cmd.printHelp(); return -1; } confplus::Config config(cmd.getCmdbyKey("config")->getValue()); authdb::AuthBackend backend(authdb::AuthBackendType::File, config.getValue(config.getKey("/AUTHDB/ADMINDB/PATH"),0), "admin.local" ); try { if(backend.end()<=sizeof(authdb::AuthHeader)){ std::string username,password; std::cerr << "Initial User not found !" << std::endl; std::cout << "Please Enter Username: "; std::cin >> username; std::cout << "Please Enter Password: "; std::cin >> password; std::cout << "Creating User !"; authdb::AuthData user,pw; uuid_t id; uuid_generate(id); uuid_unparse(id,user.uuid); uuid_unparse(id,pw.uuid); user.type=1; snprintf(user.fieldname,255,"%s","username"); pw.type=1; snprintf(pw.fieldname,255,"%s","password"); user.datasize=username.length()+1; user.data = new char[user.datasize]; snprintf(user.data,user.datasize,"%s",username.c_str()); pw.datasize=password.length()+1; pw.data = new char[pw.datasize]; snprintf(pw.data,pw.datasize,"%s",password.c_str()); authdb::AuthData euser; int rd=sizeof(authdb::AuthHeader); while(rd