Skip to content

Commit

Permalink
[Sec] Make XrdSecEntity const correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
abh3 committed Jun 26, 2020
1 parent 2761c01 commit 01edbda
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 304 deletions.
5 changes: 3 additions & 2 deletions src/XrdAcc/XrdAccEntity.cc
Expand Up @@ -33,6 +33,7 @@
#include "XrdAcc/XrdAccEntity.hh"
#include "XrdOuc/XrdOucTokenizer.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSec/XrdSecEntityAttr.hh"
#include "XrdSys/XrdSysError.hh"

/******************************************************************************/
Expand Down Expand Up @@ -128,7 +129,7 @@ XrdAccEntity *XrdAccEntity::GetEntity(const XrdSecEntity *secP, bool &isNew)

// If we already compiled the identity informaion, reuse it.
//
if ((seP = secP->Get(&accSig)))
if ((seP = secP->eaAPI->Get(&accSig)))
{isNew = false;
return static_cast<XrdAccEntity *>(seP);
}
Expand Down Expand Up @@ -193,7 +194,7 @@ void XrdAccEntity::PutEntity(const XrdSecEntity *secP)
// already if some other thread beat us to the punch (unlike). If there is
// we simply delete ourselves to avoid a memory leak.
//
if (!(const_cast<XrdSecEntity*>(secP)->Add(*this))) delete this;
if (!secP->eaAPI->Add(*this)) delete this;
}

/******************************************************************************/
Expand Down
2 changes: 2 additions & 0 deletions src/XrdHeaders.cmake
Expand Up @@ -53,7 +53,9 @@ set( XROOTD_PUBLIC_HEADERS
XrdOuc/XrdOuca2x.hh
XrdOuc/XrdOucEnum.hh
XrdOuc/XrdOucCompiler.hh
XrdSec/XrdSecAttr.hh
XrdSec/XrdSecEntity.hh
XrdSec/XrdSecEntityAttr.hh
XrdSec/XrdSecEntityPin.hh
XrdSec/XrdSecInterface.hh
XrdSys/XrdSysAtomics.hh
Expand Down
4 changes: 2 additions & 2 deletions src/XrdSec/XrdSecAttr.hh
Expand Up @@ -60,10 +60,10 @@ class XrdSecEntity;
class XrdSecAttr
{
public:
friend class XrdSecEntity;
friend class XrdSecEntityAttr;

//------------------------------------------------------------------------------
//! Delete this object (may be over-ridden for cusom action).
//! Delete this object (may be over-ridden for custom action).
//------------------------------------------------------------------------------

virtual void Delete() {delete this;}
Expand Down
189 changes: 18 additions & 171 deletions src/XrdSec/XrdSecEntity.cc
Expand Up @@ -27,76 +27,28 @@
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/

#include <iostream>
#include <map>
#include <string.h>
#include <vector>

#include "XrdSec/XrdSecAttr.hh"
#include "XrdSec/XrdSecEntity.hh"
#include "XrdSec/XrdSecEntityXtra.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysPthread.hh"

/******************************************************************************/
/* L o c a l C l a s s e s */
/* C o n s t r u c t o r */
/******************************************************************************/

class XrdSecEntityXtra
{
public:

XrdSysMutex xMutex;

std::vector<XrdSecAttr *> attrVec;

std::map<std::string, std::string> attrMap;

XrdSecEntityXtra() {}
~XrdSecEntityXtra() {}
};

/******************************************************************************/
/* A d d */
/******************************************************************************/

bool XrdSecEntity::Add(XrdSecAttr &attr)
XrdSecEntity::XrdSecEntity(const char *spName) : eaAPI(new XrdSecEntityXtra)
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::vector<XrdSecAttr*>::iterator it;

// Check if this attribute already exists
//
for (it = entXtra->attrVec.begin(); it != entXtra->attrVec.end(); it++)
if ((*it)->Signature == attr.Signature) return false;

// Add the attribute object to our list of objects
//
entXtra->attrVec.push_back(&attr);
return true;
Init(spName);
}


/******************************************************************************/
/* D e s t r u c t o r */
/******************************************************************************/

bool XrdSecEntity::Add(const std::string &key,
const std::string &val, bool replace)
XrdSecEntity::~XrdSecEntity()
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::map<std::string, std::string>::iterator it;
bool found = false;

// Check if this attribute already exists
//
it = entXtra->attrMap.find(key);
if (it != entXtra->attrMap.end())
{if (!replace) return false;
found = true;
}

// Add or replace the value
//
if (found) it->second = val;
else entXtra->attrMap.insert(std::make_pair(key, val));
return true;
delete eaAPI->entXtra;
}

/******************************************************************************/
Expand Down Expand Up @@ -148,95 +100,14 @@ void XrdSecEntity::Display(XrdSysError &mDest)

// Display it's attributes, if any
//
List(displayAttr);
eaAPI->List(displayAttr);
}

/******************************************************************************/
/* G e t */
/******************************************************************************/

XrdSecAttr *XrdSecEntity::Get(const void *sigkey) const
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::vector<XrdSecAttr*>::iterator it;

// Return pointer to the attribute if it exists
//
for (it = entXtra->attrVec.begin(); it != entXtra->attrVec.end(); it++)
if ((*it)->Signature == sigkey) return *it;

// Attribute not found
//
return (XrdSecAttr *)0;
}

/* I n i t */
/******************************************************************************/

bool XrdSecEntity::Get(const std::string &key, std::string &val) const
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::map<std::string, std::string>::iterator it;

// Return pointer to the attribute if it exists
//
it = entXtra->attrMap.find(key);
if (it != entXtra->attrMap.end())
{val = it->second;
return true;
}

// The key does not exists
//
return false;
}

/******************************************************************************/
/* K e y s */
/******************************************************************************/

std::vector<std::string> XrdSecEntity::Keys() const
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::map<std::string, std::string>::iterator itM;
std::vector<std::string> keyVec;

for (itM = entXtra->attrMap.begin();
itM != entXtra->attrMap.end(); itM++) keyVec.push_back(itM->first);

return keyVec;
}

/******************************************************************************/
/* L i s t */
/******************************************************************************/

void XrdSecEntity::List(XrdSecEntityAttrCB &attrCB) const
{
XrdSysMutexHelper mHelp(entXtra->xMutex);
std::map<std::string, std::string>::iterator itM;
std::vector<const char *> attrDel;
std::vector<const char *>::iterator itV;
XrdSecEntityAttrCB::Action rc = XrdSecEntityAttrCB::Action::Stop;

for (itM = entXtra->attrMap.begin();
itM != entXtra->attrMap.end(); itM++)
{rc = attrCB.Attr(itM->first.c_str(), itM->second.c_str());
if (rc == XrdSecEntityAttrCB::Stop) break;
else if (rc == XrdSecEntityAttrCB::Delete)
attrDel.push_back(itM->first.c_str());
}

if (rc != XrdSecEntityAttrCB::Stop) attrCB.Attr(0, 0);

for (itV = attrDel.begin(); itV != attrDel.end(); itV++)
entXtra->attrMap.erase(std::string(*itV));
}

/******************************************************************************/
/* R e s e t */
/******************************************************************************/

void XrdSecEntity::Reset(bool isnew, const char *spV)
void XrdSecEntity::Init(const char *spV)
{
memset( prot, 0, sizeof(prot) );
memset( prox, 0, sizeof(prox) );
Expand All @@ -260,38 +131,14 @@ void XrdSecEntity::Reset(bool isnew, const char *spV)
uid = 0;
gid = 0;
memset(future, 0, sizeof(future));

if (isnew) entXtra = new XrdSecEntityXtra;
else ResetXtra();
}

/******************************************************************************/
/* R e s e t X t r a */
/* R e s e t */
/******************************************************************************/

void XrdSecEntity::ResetXtra(bool dodel)
{
XrdSysMutexHelper mHelp(entXtra->xMutex);

// Cleanup the key-value map
//
entXtra->attrMap.clear();

// Run through attribute objects, deleting each one
//
std::vector<XrdSecAttr*>::iterator it;
for (it = entXtra->attrVec.begin(); it != entXtra->attrVec.end(); it++)
{(*it)->Delete();}

// Now clear the whole vector
//
entXtra->attrVec.clear();

// Delete the extension if so wanted
//
if (dodel)
{
mHelp.UnLock(); // we have to unlock the mutex bofere it's destroyed
delete entXtra; entXtra = 0;
}
void XrdSecEntity::Reset(const char *spV)
{
Init(spV);
eaAPI->entXtra->Reset();
}

0 comments on commit 01edbda

Please sign in to comment.