Newer
Older
/*******************************************************************************
* Copyright (c) 2025, Jan Koester jan.koester@gmx.net
* 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 "backend.h"
#include "group.h"
#include "types.h"
authdb::SessionData::SessionData(uuid_t sessionid, uuid_t userid, uuid_t domainid,std::vector<uuid> &members){
uuid_copy(_sid,sessionid);
uuid_copy(_uid,userid);
uuid_copy(_did,domainid);
_next=nullptr;
}
authdb::SessionData::~SessionData(){
}
void authdb::SessionData::getDid(uuid_t& dest) const{
uuid_copy(dest,_did);
}
const char * authdb::SessionData::getUsername() const{
return _username.c_str();
}
authdb::Session::Session(){
_firstData=nullptr;
_lastData=nullptr;
}
authdb::Session::~Session(){
}
const authdb::SessionData * authdb::Session::addSession(AuthBackend &backend,uuid_t domainid,uuid_t userid){
size_t rd=sizeof(authdb::AuthHeader),end=backend.end();
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
&& uuid_compare(cur.uuid,userid)==0){
char *tmp=new char[cur.datasize];
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 == GroupData && strcmp(cur.fieldname,"groupname")==0){
class GroupData grpi(cur.uuid);
size_t upos=sizeof(authdb::AuthHeader);
group.info(backend,grpi,upos);
if(grpi.isMember(cur.uuid)){
mbs.push_back(uuid(cur.uuid));
}
_firstData=std::shared_ptr<SessionData>(new SessionData(sid,userid,domainid,mbs));
_lastData->_next=std::shared_ptr<SessionData>(new SessionData(sid,userid,domainid,mbs));
const authdb::SessionData * authdb::Session::getSession(uuid_t sessionid) const{
for(const SessionData *cur=firstData(); cur; cur=cur->next()){
if(uuid_compare(cur->_sid,sessionid)==0)
for(std::shared_ptr<SessionData> cur=_firstData; cur; cur=cur->_next){
if(prev)
prev->_next=cur->_next;
if(cur==_firstData)
_firstData=cur->_next;
if(cur==_lastData)
_lastData=prev;
cur->_next=nullptr;
void authdb::Session::relSession(AuthBackend &backend,uuid_t sessionid){
Group group;
std::string username;
for(std::shared_ptr<SessionData> cur=_firstData; cur; cur=cur->_next){
if(uuid_compare(cur->_sid,sessionid)==0){
cursess=cur;
break;
}
}
if(!cursess)
throw AuthBackendError("reload session: sessionid not found !");
size_t rd=sizeof(authdb::AuthHeader),end=backend.end();
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
&& uuid_compare(cur.uuid,cursess->_uid)==0){
char *tmp=new char[cur.datasize];
backend.read(reinterpret_cast<unsigned char*>(tmp),cur.datasize);
username=tmp;
delete[] tmp;
}
if(cur.type == GroupData && strcmp(cur.fieldname,"groupname")==0){
class GroupData grpi(cur.uuid);
size_t upos=sizeof(authdb::AuthHeader);
group.info(backend,grpi,upos);
if(grpi.isMember(cursess->_uid))