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>
namespace authdb {
class AuthDB : public libhttppp::HttpEvent{
public:
AuthDB(authdb::AuthBackend &backend,netplus::socket *ssock) : HttpEvent(ssock), _Backend(backend){
};
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);
std::string userlist="UserList:\r\n";
while(rd<end){
_Backend.ReadAuthData(cur,rd);
rd=_Backend.getPos();
_Backend.setPos(sizeof(AuthHeader));
getRecord(_Backend,&rec,uid,1);
userlist+="User: ";
userlist+= "\r\n";
for(AuthDataRecord *curec=&rec; curec; curec=curec->next()){
libhttppp::HttpResponse rep;
rep.setContentType("text/txt");
rep.setContentLength(userlist.length());
rep.send(curreq,userlist.c_str(),userlist.length());
}
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);
UserData udat(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;
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
if(strcmp(curdispo->getValue(),"username")==0){
udat.setUserName(curformdat->Value.data());
}else if(strcmp(curdispo->getValue(),"firstname")==0){
udat.setFirstName(curformdat->Value.data());
}else if(strcmp(curdispo->getValue(),"lastname")==0){
udat.setLastName(curformdat->Value.data());
}else if(strcmp(curdispo->getValue(),"mail")==0){
udat.setMail(curformdat->Value.data());
}else if(strcmp(curdispo->getValue(),"avatar")==0){
udat.setAvatar(curformdat->Value);
}
}
}
User user;
user.create(_Backend,&udat);
std::cout << "user created!" << std::endl;
}
libhtmlpp::HtmlElement *root;
libhtmlpp::HtmlPage page;
libhtmlpp::HtmlString content,out;
content << "<!DOCTYPE HTML>\r\n"
<< "<html>"
<< "<body>"
<< "<form method=\"post\" enctype=\"multipart/form-data\"><ul>"
<< "<li><label for=\"username\">username:</label>"
<< "<input type=\"text\" name=\"username\"></li>"
<< "<li><label for=\"firstname\">firstname:</label>"
<< "<input type=\"text\" name=\"firstname\"></li>"
<< "<li><label for=\"lastname\">lastname:</label>"
<< "<input type=\"text\" name=\"lastname\"></li>"
<< "<li><label for=\"mail\">mail:</label>"
<< "<input type=\"text\" name=\"mail\"></li>"
<< "<li><label for=\"avatar\">ProfilBild:</label>"
<< "<input type=\"file\" name=\"avatar\"></li>"
<< "<li><input type=\"submit\"></li>"
<< "<ul></form>"
<< "</body>";
root=(libhtmlpp::HtmlElement*)content.parse();
libhtmlpp::print(root,out,true);
rep.setContentType("text/html");
rep.setContentLength(out.size());
rep.send(curreq,out.c_str(),out.size());
}
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);
}
}
void RequestEvent(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
if(strncmp(curreq->getRequestURL(),"/admin",6)==0){
AdminController(curreq,tid,args);
}
};
private:
authdb::AuthBackend &_Backend;
};
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();
if(strcmp(user->fieldname,fieldname) == 0){
if(strcmp(user->data,value) == 0){
delete[] user->data;
delete user;
return -1;
}
AuthDataRecord::AuthDataRecord(){
_next=nullptr;
}
AuthDataRecord::AuthDataRecord(const AuthDataRecord &src){
_next=nullptr;
}
AuthDataRecord::~AuthDataRecord(){
}
AuthDataRecord *AuthDataRecord::next(){
return _next;
};
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);
rd=backend.getPos();
if( strcmp(cur->uuid,plainid) == 0 && cur->type == type){
memcpy(&curec->Data,cur,sizeof(AuthData));
curec->Data->data=new char(cur->datasize);
memcpy(curec->Data->data,cur->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"
);
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=new authdb::AuthData,*pw=new authdb::AuthData;
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());
int rd=sizeof(authdb::AuthHeader);
while(rd<backend.end()){
if(strcmp(euser->fieldname,"username") == 0 && strcmp(euser->data,user->data)==0 ){
delete[] euser->data;
delete euser;
delete user;
delete pw;
throw authdb::AuthBackendError("Username already exits");
}
backend.WriteAuthData(user,backend.end());
backend.WriteAuthData(pw,backend.end());
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;
}