/******************************************************************************* * 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 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 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 #include "session.h" #include "backend.h" #include "group.h" #include "types.h" #include "authdb.h" #include "uuid.h" authdb::SessionData::SessionData(uuid_t sessionid, uuid_t userid, uuid_t domainid,std::vector &members){ uuid_copy(_sid,sessionid); uuid_copy(_uid,userid); uuid_copy(_did,domainid); _members=members;; _next=nullptr; } authdb::SessionData::~SessionData(){ } void authdb::SessionData::getUid(uuid_t& dest) const{ uuid_copy(dest,_uid); } void authdb::SessionData::getSid(uuid_t& dest) const{ uuid_copy(dest,_sid); } void authdb::SessionData::getDid(uuid_t& dest) const{ uuid_copy(dest,_did); } const char * authdb::SessionData::getUsername() const{ return _username.c_str(); } bool authdb::SessionData::isMemberOf(const uuid_t& gid){ for(const auto &member : _members){ if(member==gid) return true; } return false; } const authdb::SessionData * authdb::SessionData::next() const{ return _next.get(); } authdb::Session::Session(){ _firstData=nullptr; _lastData=nullptr; } authdb::Session::~Session(){ } const authdb::SessionData * authdb::Session::addSession(AuthBackend &backend,uuid_t domainid,uuid_t userid){ Group group; std::string username; size_t rd=sizeof(authdb::AuthHeader),end=backend.end(); while(rd tmp(new unsigned char[cur.datasize]); backend.read(tmp.get(),cur.datasize); username=reinterpret_cast(tmp.get()); } } size_t count=0; std::vector mbs; size_t mbs_pos=0; while(rd(new SessionData(sid,userid,domainid,mbs)); _lastData=_firstData; }else{ _lastData->_next=std::shared_ptr(new SessionData(sid,userid,domainid,mbs)); _lastData=_lastData->_next; } _lastData->_username=username; return _lastData.get(); } 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) return cur; } return nullptr; } void authdb::Session::remSession(uuid_t sessionid){ std::shared_ptr prev; for(std::shared_ptr cur=_firstData; cur; cur=cur->_next){ if(uuid_compare(cur->_sid,sessionid)==0){ if(prev) prev->_next=cur->_next; if(cur==_firstData) _firstData=cur->_next; if(cur==_lastData) _lastData=prev; cur->_next=nullptr; break; } prev=cur; } } void authdb::Session::relSession(AuthBackend &backend,uuid_t sessionid){ Group group; std::string username; std::shared_ptr cursess=nullptr; for(std::shared_ptr 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 !"); cursess->_members.clear(); size_t rd=sizeof(authdb::AuthHeader),end=backend.end(); while(rd_uid)==0){ std::shared_ptrtmp(new unsigned char[cur.datasize]); backend.read(tmp.get(),cur.datasize); username=reinterpret_cast(tmp.get()); } 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)) cursess->_members.emplace_back(uuid(cursess->_uid)); } } } const authdb::SessionData * authdb::Session::firstData() const{ return _firstData.get(); }