Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
AuthBackend *backend=nullptr;
if(strcmp(cdid,"admin")!=0){
if(uuid_parse(cdid,did)<0)
throw AuthBackendError("createGroups: wrong domain id !");
backend=dbackend.data(did);
}else{
backend=&_AdminBackend;
}
if(!backend)
throw AuthBackendError("createGroups: no backend seleted!");
libhttppp::HttpForm curform;
curform.parse(curreq);
if (curform.getBoundary()) {
size_t gcount=0;
std::string gname;
uuid_t gid;
uuid_generate(gid);
class GroupData grp(gid);
for (libhttppp::HttpForm::MultipartForm::Data* curformdat = curform.MultipartFormData.getFormData();
curformdat; curformdat = curformdat->nextData()) {
if(curformdat->Value.empty())
continue;
for(libhttppp::HttpForm::MultipartForm::Data::ContentDisposition *curdispo=curformdat->getDisposition();
curdispo; curdispo=curdispo->nextContentDisposition()
){
std::string tmp=curdispo->getKey();
std::transform(tmp.begin(), tmp.end(), tmp.begin(),
[](unsigned char c){ return std::tolower(c); });
if(tmp=="name" && strcmp(curdispo->getValue(),"members")==0){
++gcount;
}
if(tmp=="name" && strcmp(curdispo->getValue(),"groupname")==0){
std::copy(curformdat->Value.begin(),curformdat->Value.end(),std::back_inserter(gname));
}
}
}
if(gname.empty())
throw AuthBackendError("createGroups: no doamin name given");
grp.Members->count=gcount;
grp.Members->uid = new uuid_t[gcount];
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
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()
){
std::string tmp=curdispo->getKey();
std::transform(tmp.begin(), tmp.end(), tmp.begin(),
[](unsigned char c){ return std::tolower(c); });
if(tmp=="name" && strcmp(curdispo->getValue(),"members")==0){
uuid_parse(curformdat->Value.data(),grp.Members->uid[git]);
++git;
}
}
}
delete[] grp.Members->uid;
}
size_t rd=sizeof(authdb::AuthHeader),end=backend->end();
form << "<form method=\"post\" enctype=\"multipart/form-data\">"
<< "<table>"
<< "<tr><th>Create Group:</th></tr>"
<< "<tr><td><label for=\"groupname\">groupname:</label></td>"
<< "<td><input type=\"text\" name=\"groupname\"></td></tr>"
<< "<tr><td>Members:</td>"
<< "<td><select name=\"members\" multiple>";
while(rd<end){
authdb::AuthData::Record cur;
cur.type=EmptyData;
backend->setPos(rd);
backend->read((unsigned char*)&cur,sizeof(AuthData::Record));
rd=backend->getPos()+cur.datasize;
if(cur.type == UserData && strcmp(cur.fieldname,"username")==0){
cur.data=new char[cur.datasize];
backend->read((unsigned char*)cur.data,cur.datasize);
char cuid[255];
uuid_unparse(cur.uuid,cuid);
form << "<option value=\"" << cuid << "\">" << cur.data << "</option>";
delete[] cur.data;
}
}
form << "</select></td></tr>"
<< "<tr><td></td><td><input type=\"submit\" value=\"Create group\"></td></tr>"
<< "</table>"
<< "</form";
content->appendChild(form.parse());
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 SettingsController(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
_AdminBackend.lock();
char domain[255],url[255];
if(sscanf(curreq->getRequestURL(),"/settings/%[^/]/%s",domain,url)<0)
}else if(strncmp(url,"listgroups",10)==0){
listGroups(curreq,tid,args,"listgroups");
}else if(strncmp(url,"creategroup",11)==0){
createGroup(curreq,tid,args,"creategroup");
}else{
libhtmlpp::HtmlElement root=_IndexElement;
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());
}
}catch(AuthBackendError &e){
libhttppp::HttpResponse rep;
rep.setState(HTTP500);
rep.setContentType("text/html");
rep.send(curreq,e.what(),strlen(e.what()));
}
void RequestEvent(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
if(strncmp(curreq->getRequestURL(),"/settings",9)==0){
SettingsController(curreq,tid,args);
}if(strncmp(curreq->getRequestURL(),"/getavatar/",11)==0){
getAvatar(curreq,tid,args);
}else if(strncmp(curreq->getRequestURL(),"/authdb.js",10)==0){
libhttppp::HttpResponse rep;
rep.setContentType("text/javascript");
rep.send(curreq,_AuthJs.data(),_AuthJs.size());
}else{
libhttppp::HttpResponse rep;
rep.setState(HTTP307);
rep.setVersion(HTTPVERSION(1.1));
rep.setHeaderData("Location")->push_back("/settings/admin/index");
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.setPos(rd);
backend.read((unsigned char*)user,sizeof(AuthData::Record));
user->data = new char[user->datasize];
backend.read((unsigned char*)user->data,user->datasize);
Data->type=DataType::EmptyData;
Data->fieldname[0]='\0';
Data->data=nullptr;
Data->datasize=0;
AuthData *dest=this;
for(const AuthData *cursrc=&src; cursrc; cursrc=cursrc->next()){
memcpy(dest->Data,cursrc->Data,sizeof(AuthData::Record));
dest->Data->datasize=cursrc->Data->datasize;
dest->Data->data=new char[dest->Data->datasize];
memcpy(dest->Data->data,cursrc->Data->data,cursrc->Data->datasize);
if(cursrc->next()){
bool authdb::AuthData::empty(){
if(!Data->data)
return true;
return false;
};
AuthData *curec=this,*next=this;
if(!curec->_next && curec->Data->datasize==0){
for(const AuthData *cursrc=&src; cursrc; cursrc=cursrc->next()){
next->append(*src.Data);
if(cursrc->next()){
next->_next=new AuthData(Data->uuid);
next=next->_next;
}
}
return curec;
}
curec=curec->_next;
break;
}
curec=curec->_next;
return curec;
}
AuthData *AuthData::append(const AuthData::Record &src){
AuthData *curec=this;
if(!curec->_next && curec->Data->datasize==0){
memcpy(curec->Data,&src,sizeof(AuthData::Record));
curec->Data->data=new char[curec->Data->datasize];
memcpy(curec->Data->data,src.data,curec->Data->datasize);
return curec;
}
while(curec){
bool getRecord(authdb::AuthBackend &backend,AuthData &rec,int type){
bool found=false;
int rd=sizeof(authdb::AuthHeader),end=backend.end();
AuthData::Record cur;
memset(&cur,0,sizeof(AuthData::Record));
backend.setPos(rd);
backend.read((unsigned char*)&cur,sizeof(AuthData::Record));
rd=backend.getPos()+cur.datasize;
if(uuid_compare(cur.uuid,rec.Data->uuid) == 0 && cur.type == type){
cur.data=new char[cur.datasize];
backend.read((unsigned char*)cur.data,cur.datasize);
rec.append(cur);
delete[] cur.data;
bool editRecord(AuthBackend &backend,AuthData &rec,int type){
AuthData old(rec.Data->uuid);
std::vector<AuthData::Record*> changemap; //use fieldname;
std::vector<AuthData::Record*> newmap;
for(const AuthData *curdat=&rec; curdat; curdat=curdat->_next){
for(const AuthData *olddat=&old; olddat; olddat=olddat->_next){
if(uuid_compare(olddat->Data->uuid,curdat->Data->uuid)==0
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
&& strcmp(olddat->Data->fieldname,curdat->Data->fieldname)==0
){
size_t end = olddat->Data->datasize;
if(end!=curdat->Data->datasize){
changemap.push_back(curdat->Data);
}else{
for(size_t i=0; i<end; ++i){
if(olddat->Data->data[i]!=curdat->Data->data[i]){
changemap.push_back(curdat->Data);
break;
}
}
}
newentry=false;
}
}
if(newentry){
newmap.push_back(curdat->Data);
}
}
for(auto newel : newmap){
backend.setPos(backend.end());
backend.write((unsigned char*)newel,sizeof(AuthData::Record));;
backend.write((unsigned char*)newel->data,newel->datasize);
}
const unsigned char zero = 0;
for(auto chel : changemap){
size_t rd=sizeof(authdb::AuthHeader),end=backend.end();
while(rd<end){
authdb::AuthData::Record *rdrec=new AuthData::Record;
backend.setPos(rd);
backend.read((unsigned char*)rdrec,sizeof(AuthData::Record));
if(uuid_compare(rdrec->uuid,rec.Data->uuid)==0 && strcmp(rdrec->fieldname,chel->fieldname)==0){
backend.setPos(backend.end());
backend.write((const unsigned char*)chel,sizeof(AuthData::Record));
backend.write((const unsigned char*)chel->data,chel->datasize);
size_t dsize=rdrec->datasize;
backend.setPos(rd);
backend.write((const unsigned char*)rdrec,sizeof(AuthData::Record));
for(size_t i =0; i<rdrec->datasize; ++i){
backend.write(&zero,1);
}
rd=backend.getPos();
}else{
rd=backend.getPos()+rdrec->datasize;
}
delete rdrec;
}
}
if(!changemap.empty() || !newmap.empty())
return true;
return false;
}
size_t end=backend.end(),rd=sizeof(AuthHeader);
if(!uid)
throw AuthBackendError("user info uid required!");
while(rd<end){
authdb::AuthData::Record *cur=new AuthData::Record;
backend.setPos(rd);
backend.read((unsigned char*)cur,sizeof(AuthData::Record));
backend.setPos(wr);
backend.write((unsigned char*)cur,sizeof(AuthData::Record));
const unsigned char zero=0;
for(size_t i=0; i<dsize; ++i){
backend.write(&zero,1);
}
}
delete cur;
}
}
int main(int argc, char *argv[]){
cmdplus::CmdController &cmd=cmdplus::CmdController::getInstance();
cmd.registerCmd("config",'c',true,nullptr,"Config Path");
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;
}