Kea 2.6.0
user.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2024 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>
12#include <util/encode/encode.h>
13
14#include <user.h>
15
16#include <iomanip>
17#include <sstream>
18
19namespace user_chk {
20
21//********************************* UserId ******************************
22
23const char* UserId::HW_ADDRESS_STR = "HW_ADDR";
24const char* UserId::DUID_STR = "DUID";
25
26UserId::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
33UserId::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
73const std::vector<uint8_t>&
75 return (id_);
76}
77
80 return (id_type_);
81}
82
83std::string
84UserId::toText(char delim_char) const {
85 std::stringstream tmp;
86 tmp << std::hex;
87 bool delim = false;
88 for (auto const& it : id_) {
89 if (delim_char && delim) {
90 tmp << delim_char;
91 }
92
93 tmp << std::setw(2) << std::setfill('0')
94 << static_cast<unsigned int>(it);
95 delim = true;
96 }
97
98 return (tmp.str());
99}
100
101bool
102UserId::operator ==(const UserId & other) const {
103 return ((this->id_type_ == other.id_type_) && (this->id_ == other.id_));
104}
105
106bool
107UserId::operator !=(const UserId & other) const {
108 return (!(*this == other));
109}
110
111bool
112UserId::operator <(const UserId & other) const {
113 return ((this->id_type_ < other.id_type_) ||
114 ((this->id_type_ == other.id_type_) && (this->id_ < other.id_)));
115}
116
117std::string
119 const char* tmp = NULL;
120 switch (type) {
121 case HW_ADDRESS:
122 tmp = HW_ADDRESS_STR;
123 break;
124 case DUID:
125 tmp = DUID_STR;
126 break;
127 default:
128 isc_throw(isc::BadValue, "Invalid UserIdType:" << type);
129 break;
130 }
131
132 return (std::string(tmp));
133}
134
136UserId::lookupType(const std::string& type_str) {
137 if (type_str.compare(HW_ADDRESS_STR) == 0) {
138 return (HW_ADDRESS);
139 } else if (type_str.compare(DUID_STR) == 0) {
140 return (DUID);
141 }
142
143 isc_throw(isc::BadValue, "Invalid UserIdType string:" << type_str);
144}
145
146std::ostream&
147operator<<(std::ostream& os, const UserId& user_id) {
148 std::string tmp = UserId::lookupTypeStr(user_id.getType());
149 os << tmp << "=" << user_id.toText();
150 return (os);
151}
152
153//********************************* User ******************************
154
155User::User(const UserId& user_id) : user_id_(user_id) {
156}
157
158User::User(UserId::UserIdType id_type, const std::vector<uint8_t>& id)
159 : user_id_(id_type, id) {
160}
161
162User::User(UserId::UserIdType id_type, const std::string& id_str)
163 : user_id_(id_type, id_str) {
164}
165
167}
168
169const PropertyMap&
171 return (properties_);
172}
173
174void
176 properties_ = properties;
177}
178
179void User::setProperty(const std::string& name, const std::string& value) {
180 if (name.empty()) {
181 isc_throw (isc::BadValue, "User property name cannot be blank");
182 }
183
184 // Note that if the property exists its value will be updated.
185 properties_[name]=value;
186}
187
188std::string
189User::getProperty(const std::string& name) const {
190 PropertyMap::const_iterator it = properties_.find(name);
191 if (it != properties_.end()) {
192 return ((*it).second);
193 }
194
195 // By returning an empty string rather than throwing, we allow the
196 // flexibility of defaulting to blank if not specified. Let the caller
197 // decide if that is valid or not.
198 return ("");
199}
200
201void
202User::delProperty(const std::string & name) {
203 PropertyMap::iterator it = properties_.find(name);
204 if (it != properties_.end()) {
205 properties_.erase(it);
206 }
207}
208
209const UserId&
211 return (user_id_);
212}
213
214} // namespace user_chk
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:142
Encapsulates a unique identifier for a DHCP client.
Definition: user.h:27
const std::vector< uint8_t > & getId() const
Returns a const reference to the actual id value.
Definition: user.cc:74
bool operator!=(const UserId &other) const
Compares two UserIds for inequality.
Definition: user.cc:107
~UserId()
Destructor.
Definition: user.cc:70
UserId(UserIdType id_type, const std::vector< uint8_t > &id)
Constructor.
Definition: user.cc:26
static UserIdType lookupType(const std::string &type_str)
Returns the id type for a given text label.
Definition: user.cc:136
std::string toText(char delim_char=0x0) const
Returns textual representation of the id.
Definition: user.cc:84
static const char * DUID_STR
Define the text label DUID id type.
Definition: user.h:42
static std::string lookupTypeStr(UserIdType type)
Returns the text label for a given id type.
Definition: user.cc:118
bool operator==(const UserId &other) const
Compares two UserIds for equality.
Definition: user.cc:102
UserIdType getType() const
Returns the user id type.
Definition: user.cc:79
bool operator<(const UserId &other) const
Performs less than comparison of two UserIds.
Definition: user.cc:112
static const char * HW_ADDRESS_STR
Defines the text label hardware address id type.
Definition: user.h:40
UserIdType
Defines the supported types of user ids.
Definition: user.h:32
@ HW_ADDRESS
Hardware addresses (MAC) are used for IPv4 clients.
Definition: user.h:34
@ DUID
DUIDs are used for IPv6 clients.
Definition: user.h:36
std::string getProperty(const std::string &name) const
Fetches the string value for a given property name.
Definition: user.cc:189
User(const UserId &user_id)
Constructor.
Definition: user.cc:155
~User()
Destructor.
Definition: user.cc:166
void setProperties(const PropertyMap &properties)
Sets the user's properties from a given property map.
Definition: user.cc:175
const UserId & getUserId() const
Returns the user's id.
Definition: user.cc:210
void setProperty(const std::string &name, const std::string &value)
Sets a given property to the given value.
Definition: user.cc:179
const PropertyMap & getProperties() const
Returns a reference to the map of properties.
Definition: user.cc:170
void delProperty(const std::string &name)
Removes the given property from the property map.
Definition: user.cc:202
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition: dhcp4.h:56
void decodeHex(const string &encoded_str, vector< uint8_t > &output)
Decode a base16 encoded string into binary data.
Definition: encode.cc:367
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
std::ostream & operator<<(std::ostream &os, const UserId &user_id)
Outputs the UserId contents in a string to the given stream.
Definition: user.cc:147
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
This file defines classes: UserId and User.