diff --git a/Dockerfile b/Dockerfile index e33488d65b50f02d44361a5d12629ad0900b7465..b46d98bcb1bf6cb697806979b36d2efa8f45080a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,5 +46,6 @@ COPY --from=build /usr/local/lib /usr/local/lib COPY data/docker-config.yaml /etc/authdb/config.yaml +USER authdb CMD ["authdb","-c","yaml://etc/authdb/config.yaml"] diff --git a/src/authdb.cpp b/src/authdb.cpp index fab818f4a9efd4b2d53ea5bb05c2d9d12367e3c9..cc0b83c634ae1d4ae987b648a80636de2341a239 100644 --- a/src/authdb.cpp +++ b/src/authdb.cpp @@ -75,7 +75,94 @@ namespace authdb { cssfs.read(_AuthCss.data(),csssize); }; + void WizzardEvent(libhttppp::HttpRequest * curreq, const int tid, ULONG_PTR args){ + std::lock_guard wlock(g_wizzard); + + libhttppp::HttpForm form; + + form.parse(curreq); + + std::string username,password,pwhash; + + for(libhttppp::HttpForm::UrlcodedForm::Data *curformdat=form.UrlFormData.getFormData(); + curformdat; curformdat=curformdat->nextData()){ + if(strcmp(curformdat->getKey(),"username")==0) + username=curformdat->getValue(); + else if(strcmp(curformdat->getKey(),"password")==0) + password=curformdat->getValue(); + } + + if(!password.empty() && !username.empty()){ + sha512 hash; + hash.hash(password,pwhash); + + authdb::User user; + + uuid_t adminid,admingid; + + uuid_generate(adminid); + + uuid_generate(admingid); + + class authdb::UserData udat(adminid); + + udat.setUserName(username.c_str()); + + authdb::sha512 sha; + sha.hash(password,pwhash); + + udat.setPwHash(pwhash.c_str()); + + user.create(_AdminBackend,&udat); + + authdb::Group group; + + class authdb::GroupData gdat(admingid); + + gdat.setName("SuperAdmin"); + + authdb::GroupData::MemberUids muid; + + muid.count=1; + muid.uid=&adminid; + + gdat.addMember(muid); + + group.create(_AdminBackend,&gdat); + + libhttppp::HTTPException exp; + exp[999] << "wizzard finished"; + throw exp; + } + + libhtmlpp::HtmlElement index; + + libhtmlpp::HtmlPage page; + page.loadFile(index,GETDATA(wizzard.html)); + + libhttppp::HttpResponse rep; + + libhtmlpp::HtmlString sendat; + + libhtmlpp::print((libhtmlpp::Element*)&index,sendat,false); + + rep.setState(HTTP200); + rep.setVersion(HTTPVERSION(1.1)); + rep.setContentType("text/html"); + rep.send(curreq,sendat.c_str(),sendat.size()); + + } + void RequestEvent(libhttppp::HttpRequest * curreq, const int tid, ULONG_PTR args){ + if(_AdminBackend.end()<=sizeof(authdb::AuthHeader)){ + try{ + WizzardEvent(curreq,tid,args); + return; + }catch(libhttppp::HTTPException &e){ + if(e.getErrorType()!=999) + throw e; + } + } if(strncmp("/settings",curreq->getRequestURL(),9)==0){ _AdminBackend.lock(); try{ @@ -446,99 +533,6 @@ VALUEFOUND: DomainBackend::~DomainBackend(){ delete _domainbck; } - - class AuthWizzard : public libhttppp::HttpEvent { - public: - AuthWizzard(netplus::socket *ssock) : HttpEvent(ssock){ - _Page.loadFile(_Index,GETDATA(wizzard.html)); - } - - void RequestEvent(libhttppp::HttpRequest * curreq, const int tid, ULONG_PTR args){ - std::lock_guard wlock(g_wizzard); - - AuthBackend *backend=(AuthBackend*)args; - - if(backend->end()>sizeof(authdb::AuthHeader)){ - libhttppp::HTTPException exp; - exp[999] << "wizzard already done!"; - throw exp; - } - - libhttppp::HttpForm form; - - form.parse(curreq); - - std::string username,password,pwhash; - - for(libhttppp::HttpForm::UrlcodedForm::Data *curformdat=form.UrlFormData.getFormData(); - curformdat; curformdat=curformdat->nextData()){ - if(strcmp(curformdat->getKey(),"username")==0) - username=curformdat->getValue(); - else if(strcmp(curformdat->getKey(),"password")==0) - password=curformdat->getValue(); - } - - if(!password.empty() && !username.empty()){ - sha512 hash; - hash.hash(password,pwhash); - - authdb::User user; - - uuid_t adminid,admingid; - - uuid_generate(adminid); - - uuid_generate(admingid); - - class authdb::UserData udat(adminid); - - udat.setUserName(username.c_str()); - - authdb::sha512 sha; - sha.hash(password,pwhash); - - udat.setPwHash(pwhash.c_str()); - - user.create(*backend,&udat); - - authdb::Group group; - - class authdb::GroupData gdat(admingid); - - gdat.setName("SuperAdmin"); - - authdb::GroupData::MemberUids muid; - - muid.count=1; - muid.uid=&adminid; - - gdat.addMember(muid); - - group.create(*backend,&gdat); - - libhttppp::HTTPException exp; - exp[999] << "wizzard finished"; - throw exp; - } - - libhtmlpp::HtmlElement index=_Index; - - libhttppp::HttpResponse rep; - - libhtmlpp::HtmlString sendat; - - libhtmlpp::print((libhtmlpp::Element*)&index,sendat,false); - - rep.setState(HTTP200); - rep.setVersion(HTTPVERSION(1.1)); - rep.setContentType("text/html"); - rep.send(curreq,sendat.c_str(),sendat.size()); - - } - private: - libhtmlpp::HtmlPage _Page; - libhtmlpp::HtmlElement _Index;; - }; }; int main(int argc, char *argv[]){ @@ -569,19 +563,6 @@ int main(int argc, char *argv[]){ nullptr ); - if(backend.end()<=sizeof(authdb::AuthHeader)){ - try{ - authdb::AuthWizzard wizzard(httpd.getServerSocket()); - wizzard.threads=1; - wizzard.runEventloop((ULONG_PTR)&backend); - }catch(libhttppp::HTTPException &e){ - if(e.getErrorType()==999) - std::cout<< e.what() << std::endl; - else - throw e; - } - } - authdb::AuthDB authdb(backend,httpd.getServerSocket()); // authdb.threads=2;