Kea  2.1.7-git
user.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2015,2017 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <dhcp/hwaddr.h>
10 #include <dhcp/duid.h>
11 #include <exceptions/exceptions.h>
12 #include <util/encode/hex.h>
13 
14 #include <user.h>
15 
16 #include <iomanip>
17 #include <sstream>
18 
19 namespace user_chk {
20 
21 //********************************* UserId ******************************
22 
23 const char* UserId::HW_ADDRESS_STR = "HW_ADDR";
24 const char* UserId::DUID_STR = "DUID";
25 
26 UserId::UserId(UserIdType id_type, const std::vector<uint8_t>& id)
27  : id_type_(id_type), id_(id) {
28  if (id.size() == 0) {
29  isc_throw(isc::BadValue, "UserId id may not be blank");
30  }
31 }
32 
33 UserId::UserId(UserIdType id_type, const std::string & id_str) :
34  id_type_(id_type) {
35  if (id_str.empty()) {
36  isc_throw(isc::BadValue, "UserId id string may not be blank");
37  }
38 
39  // Convert the id string to vector.
40  // Input is expected to be 2-digits per bytes, no delimiters.
41  std::vector<uint8_t> addr_bytes;
42 
43  // Strip out colon delimiters, decodeHex doesn't like them.
44  std::string clean_id_str = id_str;
45  std::string::iterator end_pos = std::remove(clean_id_str.begin(),
46  clean_id_str.end(), ':');
47  clean_id_str.erase(end_pos, clean_id_str.end());
48 
49  isc::util::encode::decodeHex(clean_id_str, addr_bytes);
50 
51  // Attempt to instantiate the appropriate id class to leverage validation.
52  switch (id_type) {
53  case HW_ADDRESS: {
54  isc::dhcp::HWAddr hwaddr(addr_bytes, isc::dhcp::HTYPE_ETHER);
55  break;
56  }
57  case DUID: {
58  isc::dhcp::DUID duid(addr_bytes);
59  break;
60  }
61  default:
62  isc_throw (isc::BadValue, "Invalid id_type: " << id_type);
63  break;
64  }
65 
66  // It's a valid id.
67  id_ = addr_bytes;
68 }
69 
71 }
72 
73 const std::vector<uint8_t>&
74 UserId::getId() const {
75  return (id_);
76 }
77 
79 UserId::getType() const {
80  return (id_type_);
81 }
82 
83 std::string
84 UserId::toText(char delim_char) const {
85  std::stringstream tmp;
86  tmp << std::hex;
87  bool delim = false;
88  for (std::vector<uint8_t>::const_iterator it = id_.begin();
89  it != id_.end(); ++it) {
90  if (delim_char && delim) {
91  tmp << delim_char;
92  }
93 
94  tmp << std::setw(2) << std::setfill('0')
95  << static_cast<unsigned int>(*it);
96  delim = true;
97  }
98 
99  return (tmp.str());
100 }
101 
102 bool
103 UserId::operator ==(const UserId & other) const {
104  return ((this->id_type_ == other.id_type_) && (this->id_ == other.id_));
105 }
106 
107 bool
108 UserId::operator !=(const UserId & other) const {
109  return (!(*this == other));
110 }
111 
112 bool
113 UserId::operator <(const UserId & other) const {
114  return ((this->id_type_ < other.id_type_) ||
115  ((this->id_type_ == other.id_type_) && (this->id_ < other.id_)));
116 }
117 
118 std::string
120  const char* tmp = NULL;
121  switch (type) {
122  case HW_ADDRESS:
123  tmp = HW_ADDRESS_STR;
124  break;
125  case DUID:
126  tmp = DUID_STR;
127  break;
128  default:
129  isc_throw(isc::BadValue, "Invalid UserIdType:" << type);
130  break;
131  }
132 
133  return (std::string(tmp));
134 }
135 
137 UserId::lookupType(const std::string& type_str) {
138  if (type_str.compare(HW_ADDRESS_STR) == 0) {
139  return (HW_ADDRESS);
140  } else if (type_str.compare(DUID_STR) == 0) {
141  return (DUID);
142  }
143 
144  isc_throw(isc::BadValue, "Invalid UserIdType string:" << type_str);
145 }
146 
147 std::ostream&
148 operator<<(std::ostream& os, const UserId& user_id) {
149  std::string tmp = UserId::lookupTypeStr(user_id.getType());
150  os << tmp << "=" << user_id.toText();
151  return (os);
152 }
153 
154 //********************************* User ******************************
155 
156 User::User(const UserId& user_id) : user_id_(user_id) {
157 }
158 
159 User::User(UserId::UserIdType id_type, const std::vector<uint8_t>& id)
160  : user_id_(id_type, id) {
161 }
162 
163 User::User(UserId::UserIdType id_type, const std::string& id_str)
164  : user_id_(id_type, id_str) {
165 }
166 
168 }
169 
170 const PropertyMap&
172  return (properties_);
173 }
174 
175 void
176 User::setProperties(const PropertyMap& properties) {
177  properties_ = properties;
178 }
179 
180 void User::setProperty(const std::string& name, const std::string& value) {
181  if (name.empty()) {
182  isc_throw (isc::BadValue, "User property name cannot be blank");
183  }
184 
185  // Note that if the property exists its value will be updated.
186  properties_[name]=value;
187 }
188 
189 std::string
190 User::getProperty(const std::string& name) const {
191  PropertyMap::const_iterator it = properties_.find(name);
192  if (it != properties_.end()) {
193  return ((*it).second);
194  }
195 
196  // By returning an empty string rather than throwing, we allow the
197  // flexibility of defaulting to blank if not specified. Let the caller
198  // decide if that is valid or not.
199  return ("");
200 }
201 
202 void
203 User::delProperty(const std::string & name) {
204  PropertyMap::iterator it = properties_.find(name);
205  if (it != properties_.end()) {
206  properties_.erase(it);
207  }
208 }
209 
210 const UserId&
212  return (user_id_);
213 }
214 
215 } // namespace user_chk
UserIdType
Defines the supported types of user ids.
Definition: user.h:32
static std::string lookupTypeStr(UserIdType type)
Returns the text label for a given id type.
Definition: user.cc:119
void setProperties(const PropertyMap &properties)
Sets the user&#39;s properties from a given property map.
Definition: user.cc:176
static const char * DUID_STR
Define the text label DUID id type.
Definition: user.h:42
Encapsulates a unique identifier for a DHCP client.
Definition: user.h:27
std::string getProperty(const std::string &name) const
Fetches the string value for a given property name.
Definition: user.cc:190
bool operator<(const UserId &other) const
Performs less than comparison of two UserIds.
Definition: user.cc:113
User(const UserId &user_id)
Constructor.
Definition: user.cc:156
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
void setProperty(const std::string &name, const std::string &value)
Sets a given property to the given value.
Definition: user.cc:180
Defines the logger used by the user check hooks library.
Definition: user.cc:19
std::map< std::string, std::string > PropertyMap
Defines a map of string values keyed by string labels.
Definition: user.h:145
bool operator!=(const UserId &other) const
Compares two UserIds for inequality.
Definition: user.cc:108
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
This file defines classes: UserId and User.
const UserId & getUserId() const
Returns the user&#39;s id.
Definition: user.cc:211
const PropertyMap & getProperties() const
Returns a reference to the map of properties.
Definition: user.cc:171
void decodeHex(const string &input, vector< uint8_t > &result)
Decode a text encoded in the base16 (&#39;hex&#39;) format into the original data.
Definition: base_n.cc:474
~User()
Destructor.
Definition: user.cc:167
void delProperty(const std::string &name)
Removes the given property from the property map.
Definition: user.cc:203
Ethernet 10Mbps.
Definition: dhcp4.h:56
static UserIdType lookupType(const std::string &type_str)
Returns the id type for a given text label.
Definition: user.cc:137
UserId(UserIdType id_type, const std::vector< uint8_t > &id)
Constructor.
Definition: user.cc:26
UserIdType getType() const
Returns the user id type.
Definition: user.cc:79
Hardware addresses (MAC) are used for IPv4 clients.
Definition: user.h:34
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
~UserId()
Destructor.
Definition: user.cc:70
std::ostream & operator<<(std::ostream &os, const UserId &user_id)
Outputs the UserId contents in a string to the given stream.
Definition: user.cc:148
static const char * HW_ADDRESS_STR
Defines the text label hardware address id type.
Definition: user.h:40
bool operator==(const UserId &other) const
Compares two UserIds for equality.
Definition: user.cc:103
const std::vector< uint8_t > & getId() const
Returns a const reference to the actual id value.
Definition: user.cc:74
DUIDs are used for IPv6 clients.
Definition: user.h:36
std::string toText(char delim_char=0x0) const
Returns textual representation of the id.
Definition: user.cc:84