XRootD
XrdSecEntityAttr.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S e c E n t i t y A t t r . c c */
4 /* */
5 /* (c) 2019 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19 /* License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24 /* */
25 /* The copyright holder's institutional names and contributor's names may not */
26 /* be used to endorse or promote products derived from this software without */
27 /* specific prior written permission of the institution or contributor. */
28 /******************************************************************************/
29 
30 #include <cstring>
31 #include <vector>
32 
33 #include "XrdSec/XrdSecAttr.hh"
35 #include "XrdSys/XrdSysPthread.hh"
36 
37 /******************************************************************************/
38 /* A d d */
39 /******************************************************************************/
40 
42 {
43  XrdSysMutexHelper mHelp(entXtra->xMutex);
44  std::vector<XrdSecAttr*>::iterator it;
45 
46 // Check if this attribute already exists
47 //
48  for (it = entXtra->attrVec.begin(); it != entXtra->attrVec.end(); it++)
49  if ((*it)->Signature == attr.Signature) return false;
50 
51 // Add the attribute object to our list of objects
52 //
53  entXtra->attrVec.push_back(&attr);
54  return true;
55 }
56 
57 /******************************************************************************/
58 
59 bool XrdSecEntityAttr::Add(const std::string &key,
60  const std::string &val, bool replace)
61 {
62  XrdSysMutexHelper mHelp(entXtra->xMutex);
63  std::map<std::string, std::string>::iterator it;
64  bool found = false;
65 
66 // Check if this attribute already exists
67 //
68  it = entXtra->attrMap.find(key);
69  if (it != entXtra->attrMap.end())
70  {if (!replace) return false;
71  found = true;
72  }
73 
74 // Add or replace the value
75 //
76  if (found) it->second = val;
77  else entXtra->attrMap.insert(std::make_pair(key, val));
78  return true;
79 }
80 
81 /******************************************************************************/
82 /* G e t */
83 /******************************************************************************/
84 
85 XrdSecAttr *XrdSecEntityAttr::Get(const void *sigkey)
86 {
87  XrdSysMutexHelper mHelp(entXtra->xMutex);
88  std::vector<XrdSecAttr*>::iterator it;
89 
90 // Return pointer to the attribute if it exists
91 //
92  for (it = entXtra->attrVec.begin(); it != entXtra->attrVec.end(); it++)
93  if ((*it)->Signature == sigkey) return *it;
94 
95 // Attribute not found
96 //
97  return (XrdSecAttr *)0;
98 }
99 
100 /******************************************************************************/
101 
102 bool XrdSecEntityAttr::Get(const std::string &key, std::string &val)
103 {
104  XrdSysMutexHelper mHelp(entXtra->xMutex);
105  std::map<std::string, std::string>::iterator it;
106 
107 // Return pointer to the attribute if it exists
108 //
109  it = entXtra->attrMap.find(key);
110  if (it != entXtra->attrMap.end())
111  {val = it->second;
112  return true;
113  }
114 
115 // The key does not exists
116 //
117  return false;
118 }
119 
120 /******************************************************************************/
121 /* K e y s */
122 /******************************************************************************/
123 
124 std::vector<std::string> XrdSecEntityAttr::Keys()
125 {
126  XrdSysMutexHelper mHelp(entXtra->xMutex);
127  std::map<std::string, std::string>::iterator itM;
128  std::vector<std::string> keyVec;
129 
130  for (itM = entXtra->attrMap.begin();
131  itM != entXtra->attrMap.end(); itM++) keyVec.push_back(itM->first);
132 
133  return keyVec;
134 }
135 
136 /******************************************************************************/
137 /* L i s t */
138 /******************************************************************************/
139 
141 {
142  XrdSysMutexHelper mHelp(entXtra->xMutex);
143  std::map<std::string, std::string>::iterator itM;
144  std::vector<const char *> attrDel;
145  std::vector<const char *>::iterator itV;
146  XrdSecEntityAttrCB::Action rc = XrdSecEntityAttrCB::Action::Stop;
147 
148  for (itM = entXtra->attrMap.begin();
149  itM != entXtra->attrMap.end(); itM++)
150  {rc = attrCB.Attr(itM->first.c_str(), itM->second.c_str());
151  if (rc == XrdSecEntityAttrCB::Stop) break;
152  else if (rc == XrdSecEntityAttrCB::Delete)
153  attrDel.push_back(itM->first.c_str());
154  }
155 
156  if (rc != XrdSecEntityAttrCB::Stop) attrCB.Attr(0, 0);
157 
158  for (itV = attrDel.begin(); itV != attrDel.end(); itV++)
159  entXtra->attrMap.erase(std::string(*itV));
160 }
@ Stop
Stop the iteration.
@ Delete
Delete the key-value and proceed to next one.
virtual Action Attr(const char *key, const char *val)=0
bool Add(XrdSecAttr &attr)
XrdSecAttr * Get(const void *sigkey)
std::vector< std::string > Keys()
void List(XrdSecEntityAttrCB &attrCB)
std::vector< XrdSecAttr * > attrVec
std::map< std::string, std::string > attrMap