Newer
Older
grouplist << "</table></div><div><ul class=\"grouptoolbar\" ><li><a class=\"button\" href=\"/settings/"<< cdid << "/creategroup\">CreateGroup</a></li></ul></div>";
content->appendChild(grouplist.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 createGroup(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args,const char *url){
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
libhtmlpp::HtmlString form,out;
libhtmlpp::HtmlElement root=_IndexElement,*content=root.getElementbyID("content");
char cdid[255];
uuid_t did;
if(sscanf(curreq->getRequestURL(),"/settings/%[^/]",cdid)<0){
throw AuthBackendError("createGroups: wrong url!");
}
DomainBackend dbackend(_AdminBackend);
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 grpdat(gid);
struct GroupData::MemberUids member;
1059
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
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");
Group grp;
grpdat.setName(gname.c_str());
member.count=gcount;
member.uid = new uuid_t[gcount];
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){
grpdat.addMember(member);
grp.create(*backend,&grpdat);
delete[] member.uid;
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
}
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());
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
void removeGroup(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args,const char *url){
char cdid[255],gid[255];
uuid_t did;
if(sscanf(curreq->getRequestURL(),"/settings/%[^/]/removegroup/%s",cdid,gid)<0){
throw AuthBackendError("removeGroup: wrong url!");
}
DomainBackend dbackend(_AdminBackend);
AuthBackend *backend=nullptr;
if(strcmp(cdid,"admin") == 0 ){
backend=&_AdminBackend;
}else if(uuid_parse(cdid,did) == 0 ){
backend=dbackend.data(did);
}else{
throw AuthBackendError("removeGroup: could not parse uuid!");
}
Group group;
uuid_t uuid;
uuid_parse(gid,uuid);
group.remove(*backend,uuid);
libhttppp::HttpResponse rep;
rep.setState(HTTP307);
rep.setVersion(HTTPVERSION(1.1));
char rurl[255];
snprintf(rurl,255,"/settings/%s/listgroups",cdid);
rep.setHeaderData("Location")->push_back(rurl);
rep.setContentType("text/html");
rep.send(curreq,nullptr,0);
}
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,"removegroup",10)==0){
removeGroup(curreq,tid,args,"removegroup");
}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
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
&& 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;
}