Newer
Older
<< "/editgroup/" << cruid << "\">Edit group</a></li>"
<< "</ul></td></tr>";
}catch(AuthBackendError &e){
std::cerr << e.what() << std::endl;
}
}
}
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){
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 ){
backend=&_AdminBackend;
}else if(uuid_parse(cdid,did) == 0 ){
throw AuthBackendError("createGroups: could not parse uuid!");
}
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;
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
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;
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
1166
1167
1168
1169
}
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());
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
1206
1207
1208
1209
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);
}
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
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
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
void editGroup(libhttppp::HttpRequest *curreq, const int tid, ULONG_PTR args){
char cdid[255],cgid[255];
uuid_t did;
if(sscanf(curreq->getRequestURL(),"/settings/%[^/]/editgroup/%s",cdid,cgid)<0){
throw AuthBackendError("listUsers: 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("listUsers: could not parse uuid!");
}
size_t rd=sizeof(authdb::AuthHeader),end=backend->end();
uuid_t gid;
uuid_parse(cgid,gid);
AuthData editrec(gid);
bool data = false;
libhttppp::HttpForm curform;
curform.parse(curreq);
for (libhttppp::HttpForm::MultipartForm::Data* curformdat = curform.MultipartFormData.getFormData();
curformdat; curformdat = curformdat->nextData()) {
AuthData curec(gid);
curec.Data->type=GroupData;
curec.Data->storage=EmptyStorage;
for(libhttppp::HttpForm::MultipartForm::Data::ContentDisposition *curdispo=curformdat->getDisposition();
curdispo; curdispo=curdispo->nextContentDisposition()
){
if(curformdat->Value.empty() || !curdispo->getValue())
continue;
data = true;
std::string key;
if(curdispo->getKey())
key=curdispo->getKey();
else
continue;
std::transform(key.begin(), key.end(), key.begin(),
[](unsigned char c){ return std::tolower(c); });
if(key=="name"){
strcpy(curec.Data->fieldname,curdispo->getValue());
}
if(key=="filename"){
curec.Data->storage=BinaryStorage;
}
}
if(curec.Data->storage==EmptyStorage){
bool numeric=true;
for(auto t : curformdat->Value){
if(!std::isdigit(t)){
numeric=false;
break;
}
}
if(numeric)
curec.Data->storage=IntStorage;
else
curec.Data->storage=TextStorage;
}
if(curec.Data->storage==TextStorage) {
curformdat->Value.push_back('\0');
curec.Data->datasize=curformdat->Value.size();
curec.Data->data=new char[curec.Data->datasize];
std::copy(curformdat->Value.begin(),
curformdat->Value.end(),curec.Data->data);
editrec.append(curec);
delete[] curec.Data->data;
}else if(curec.Data->storage==BinaryStorage){
curec.Data->datasize=curformdat->Value.size();
curec.Data->data=new char[curec.Data->datasize];
memcpy(curec.Data->data,curformdat->Value.data(),
curformdat->Value.size());
editrec.append(curec);
delete[] curec.Data->data;
}else if(curec.Data->storage==IntStorage){
curformdat->Value.push_back('\0');
curec.Data->datasize=sizeof(int);
curec.Data->data=(char*)new int;
int val=atoi(curformdat->Value.data());
memcpy(curec.Data->data,&val,
curec.Data->datasize);
editrec.append(curec);
delete[] (int*)curec.Data->data;
}
}
if(data){
editRecord(*backend,editrec,GroupData);
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);
return;
}
libhtmlpp::HtmlPage page;
libhtmlpp::HtmlString form,out;
form << "<form method=\"post\" enctype=\"multipart/form-data\">"
<< "<table>";
while(rd<end){
authdb::AuthData::Record cur;
backend->setPos(rd);
backend->read((unsigned char*)&cur,sizeof(AuthData::Record));
rd=backend->getPos()+cur.datasize;
if(uuid_compare(cur.uuid,gid)==0 && cur.type==GroupData && cur.storage==TextStorage){
cur.data = new char[cur.datasize];
backend->read((unsigned char*)cur.data,cur.datasize);
try {
form << "<tr><td><label for=\""<< cur.fieldname <<"\">" << cur.fieldname << "</label></td>"
<< "<td><input type=\"text\" name=\"" << cur.fieldname << "\" value=\""
<< cur.data <<"\"></td></tr>";
}catch(AuthBackendError &e){
std::cerr << e.what() << std::endl;
}
delete[] cur.data;
} else if(uuid_compare(cur.uuid,gid)==0 && cur.type==GroupData && cur.storage==BinaryStorage
&& strcmp(cur.fieldname ,"groupmembers")==0
){
cur.data = new char[cur.datasize];
backend->read((unsigned char*)cur.data,cur.datasize);
GroupData::MemberUids mrec;
memcpy(&mrec,cur.data,sizeof(GroupData::MemberUids));
mrec.uid=new uuid_t[mrec.count];
memcpy(mrec.uid,cur.data+sizeof(GroupData::MemberUids),
(sizeof(uuid_t)*mrec.count));
for(size_t i=0; i<mrec.count; ++i ){
size_t urd=sizeof(AuthHeader),uend=backend->end();
while(urd<uend){
authdb::AuthData::Record curu;
backend->setPos(urd);
backend->read((unsigned char*)&curu,sizeof(AuthData::Record));
urd=backend->getPos()+curu.datasize;
if(curu.type == UserData && uuid_compare(mrec.uid[i],curu.uuid) == 0 &&
curu.storage==TextStorage && strcmp(curu.fieldname,"username")==0){
curu.data = new char[curu.datasize];
backend->read((unsigned char*)curu.data,curu.datasize);
std::cout << curu.data << std::endl;
delete[] curu.data;
break;
}
}
}
delete[] mrec.uid;
}
}
form << "<tr><td></td><td><input type=\"submit\"></td></tr>"
<< "</table>"
<< "</form";
libhtmlpp::HtmlElement root=_IndexElement;
libhtmlpp::HtmlElement *content=root.getElementbyID("content");
content->insertChild(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,"removegroup",10)==0){
removeGroup(curreq,tid,args,"removegroup");
}else if(strncmp(url,"editgroup",9)==0){
editGroup(curreq,tid,args);
}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
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
&& 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;
}