Newer
Older
/*******************************************************************************
* 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 <iostream>
class AuthDB : public libhttppp::HttpEvent{
public:
AuthDB(authdb::AuthBackend &backend,netplus::socket *ssock) : HttpEvent(ssock), _Backend(backend){
_Indexpage.loadFile(_IndexElement,"../data/index.html");
void listUsers(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
_Backend.lock();
size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end();
_Backend.setPos(rd);
libhtmlpp::HtmlElement *content=root.getElementbyID("content");
userlist <<"<div><p>UserList:</p><table class=\"userlist\">";
cur->data = new char[cur->datasize];
_Backend.read((unsigned char*)cur->data,cur->datasize);
try {
size_t upos=sizeof(authdb::AuthHeader);
user.info(_Backend,&uid,udat,upos);
userlist << "<tr><td class=\"list_username\">";
std::copy(cur->data,cur->data+cur->datasize,std::back_inserter(userlist));
userlist << "</td></tr>"
<< "<tr><td><img class=\"list_picture\" style=\"height:100px;\" src=\"/admin/getavatar/" << cur->uuid << ".jpg\" alt=\"avatar\">"
<< "</td><td><ul><li>"
<< "uid: " << cur->uuid
<< "</li>"
<< "<li><span>Username:</span><span>" << udat.getUsername() << "</span></li>"
<< "<li><span>Firstname:</span><span>" << udat.getFirstname() << "</span></li>"
<< "<li><span>Lastname:</span><span>" << udat.getLastename() << "</span></li>"
<< "<li><span>Email:</span><span>" << udat.getMail() << "</span></li>"
<< "</ul></td><td><ul class=\"usertoolbar\" >"
<< "<li><a class=\"button\" href=\"/admin/edituser/" << cur->uuid << "\">EditUser</a></li>"
<< "<li><a class=\"button\" href=\"/admin/removeuser/" << cur->uuid << "\">RemoveUser</a></li>"
<< "</ul></td></tr>";
}catch(AuthBackendError &e){
std::cerr << e.what() << std::endl;
userlist << "</table></div><div><ul class=\"usertoolbar\" ><li><a class=\"button\" href=\"/admin/createuser\">CreateUser</a></li></ul></div>";
content->insertChild(userlist.parse());
libhtmlpp::print(&root,out,true);
rep.setContentType("text/html");
rep.send(curreq,out.c_str(),out.size());
}
void createUser(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
libhttppp::HttpResponse rep;
libhttppp::HttpForm curform;
curform.parse(curreq);
if (curform.getBoundary()) {
uuid_t uid;
uuid_generate(uid);
for (libhttppp::HttpForm::MultipartForm::Data* curformdat = curform.MultipartFormData.getFormData();
curformdat; curformdat = curformdat->nextData()) {
for(libhttppp::HttpForm::MultipartForm::Data::ContentDisposition *curdispo=curformdat->getDisposition();
curdispo; curdispo=curdispo->nextContentDisposition()
){
if(curformdat->Value.empty() || !curdispo->getValue())
continue;
std::vector<char> tmp;
if(strcmp(curdispo->getValue(),"avatar")!=0){
std:copy(curformdat->Value.begin(),curformdat->Value.end(),std::back_inserter(tmp));
tmp.push_back('\0');
}
}else if(strcmp(curdispo->getValue(),"avatar")==0 && !curformdat->Value.empty()){
libhtmlpp::HtmlString form,out;
form << "<form method=\"post\" enctype=\"multipart/form-data\">"
<< "<table>"
<< "<tr><td><label for=\"username\">username:</label></td>"
<< "<td><input type=\"text\" name=\"username\"></td></tr>"
<< "<tr><td><label for=\"firstname\">firstname:</label></td>"
<< "<td><input type=\"text\" name=\"firstname\"></td></tr>"
<< "<tr><td><label for=\"lastname\">lastname:</label></td>"
<< "<td><input type=\"text\" name=\"lastname\"></td></tr>"
<< "<tr><td><label for=\"mail\">mail:</label></td>"
<< "<td><input type=\"text\" name=\"mail\"></td></tr>"
<< "<tr><td><label for=\"avatar\">ProfilBild:</label></td>"
<< "<td><input type=\"file\" name=\"avatar\"></td></tr>"
<< "<tr><td></td><td><input type=\"submit\"></td></tr>"
<< "</table>"
<< "</form";
libhtmlpp::HtmlElement *content=root.getElementbyID("content");
content->insertChild(form.parse());
libhtmlpp::print(&root,out,true);
rep.setContentType("text/html");
rep.setContentLength(out.size());
rep.send(curreq,out.c_str(),out.size());
}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
void getAvatar(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
libhttppp::HttpResponse rep;
char uid[255],ext[16];
sscanf(curreq->getRequestURL(),"/admin/getavatar/%[^.].%s",uid,ext);
_Backend.lock();
authdb::AuthData *cur;
size_t rd=sizeof(authdb::AuthHeader),end=_Backend.end();
while(rd<end){
cur=new AuthData;
cur->data=nullptr;
_Backend.ReadAuthData(cur,rd);
rd=_Backend.getPos()+cur->datasize;
if(cur && strcmp(cur->uuid,uid)==0 && strcmp(cur->fieldname,"avatar")==0){
cur->data = new char[cur->datasize];
_Backend.read((unsigned char*)cur->data,cur->datasize);
char ctype[255];
snprintf(ctype,255,"image/%s",ext);
rep.setContentType(ctype);
rep.send(curreq,cur->data,cur->datasize);
end=0;
delete[] cur->data;
}
delete cur;
}
if(end!=0){
rep.setState(HTTP404);
rep.send(curreq,nullptr,0);
}
_Backend.unlock();
};
void AdminController(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
if(strncmp(curreq->getRequestURL(),"/admin/listusers",16)==0){
listUsers(curreq,tid,args);
}else if(strncmp(curreq->getRequestURL(),"/admin/createuser",17)==0){
createUser(curreq,tid,args);
}else if(strncmp(curreq->getRequestURL(),"/admin/getavatar/",17)==0){
getAvatar(curreq,tid,args);
libhtmlpp::HtmlString out;
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 RequestEvent(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
if(strncmp(curreq->getRequestURL(),"/admin",6)==0){
AdminController(curreq,tid,args);
}else if(strncmp(curreq->getRequestURL(),"/api",4)==0){
}else{
libhttppp::HttpResponse rep;
rep.setState(HTTP307);
rep.setVersion(HTTPVERSION(1.1));
rep.setHeaderData("Location")->push_back("/admin");
rep.setContentType("text/html");
rep.send(curreq,nullptr,0);
authdb::AuthBackend &_Backend;
libhtmlpp::HtmlElement _IndexElement;
libhtmlpp::HtmlPage _Indexpage;
};
int searchValue(authdb::AuthBackend &backend,const char*fieldname,const char *value){
int rd=sizeof(authdb::AuthHeader),brd=rd;
while(rd>backend.end()){
backend.ReadAuthData(user,rd);
rd=backend.getPos();
user->data = new char[user->datasize];
backend.read((unsigned char*)user->data,user->datasize);
_next=nullptr;
}
AuthDataRecord::AuthDataRecord(const AuthDataRecord &src){
_next=nullptr;
}
AuthDataRecord::~AuthDataRecord(){
};
bool getRecord(authdb::AuthBackend &backend,AuthDataRecord *dest,uuid_t id,int type){
bool found=false;
int rd=sizeof(authdb::AuthHeader),end=backend.end();
char plainid[255];
uuid_unparse(id,plainid);
AuthDataRecord *curec=dest;
while(rd<end){
backend.ReadAuthData(cur,rd);
memcpy(curec->Data,cur,sizeof(AuthHeader));
curec->Data->data=new char[cur->datasize];
backend.read((unsigned char*)curec->Data->data,cur->datasize);
if(rd<backend.end()){
curec->_next=new AuthDataRecord;
curec=curec->_next;
}
found=true;
}
}
int main(int argc, char *argv[]){
cmdplus::CmdController &cmd=cmdplus::CmdController::getInstance();
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"
);
libhttppp::HttpD httpd(
config.getValue(config.getKey("/AUTHDB/BIND"),0),
config.getIntValue(config.getKey("/AUTHDB/PORT"),0),
config.getIntValue(config.getKey("/AUTHDB/MAXCONN"),0),
nullptr,
nullptr
);
authdb::AuthDB authdb(backend,httpd.getServerSocket());
}catch(authdb::AuthBackendError &e){
std::cerr << e.what() << std::endl;
}