Kea 3.3.1
user.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2026 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
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::string tmp;
86 bool delim = false;
87 for (auto const& it : id_) {
88 if (delim_char && delim) {
89 tmp.push_back(delim_char);
90 }
91
92 tmp.append(isc::util::str::byteToHex(it));
93 delim = true;
94 }
95
96 return (tmp);
97}
98
99bool
100UserId::operator ==(const UserId & other) const {
101 return ((this->id_type_ == other.id_type_) && (this->id_ == other.id_));
102}
103
104bool
105UserId::operator !=(const UserId & other) const {
106 return (!(*this == other));
107}
108
109bool
110UserId::operator <(const UserId & other) const {
111 return ((this->id_type_ < other.id_type_) ||
112 ((this->id_type_ == other.id_type_) && (this->id_ < other.id_)));
113}
114
115std::string
117 const char* tmp = NULL;
118 switch (type) {
119 case HW_ADDRESS:
120 tmp = HW_ADDRESS_STR;
121 break;
122 case DUID:
123 tmp = DUID_STR;
124 break;
125 default:
126 isc_throw(isc::BadValue, "Invalid UserIdType:" << type);
127 break;
128 }
129
130 return (std::string(tmp));
131}
132
134UserId::lookupType(const std::string& type_str) {
135 if (type_str.compare(HW_ADDRESS_STR) == 0) {
136 return (HW_ADDRESS);
137 } else if (type_str.compare(DUID_STR) == 0) {
138 return (DUID);
139 }
140
141 isc_throw(isc::BadValue, "Invalid UserIdType string:" << type_str);
142}
143
144std::ostream&
145operator<<(std::ostream& os, const UserId& user_id) {
146 std::string tmp = UserId::lookupTypeStr(user_id.getType());
147 os << tmp << "=" << user_id.toText();
148 return (os);
149}
150
151//********************************* User ******************************
152
153User::User(const UserId& user_id) : user_id_(user_id) {
154}
155
156User::User(UserId::UserIdType id_type, const std::vector<uint8_t>& id)
157 : user_id_(id_type, id) {
158}
159
160User::User(UserId::UserIdType id_type, const std::string& id_str)
161 : user_id_(id_type, id_str) {
162}
163
165}
166
167const PropertyMap&
169 return (properties_);
170}
171
172void
174 properties_ = properties;
175}
176
177void User::setProperty(const std::string& name, const std::string& value) {
178 if (name.empty()) {
179 isc_throw (isc::BadValue, "User property name cannot be blank");
180 }
181
182 // Note that if the property exists its value will be updated.
183 properties_[name]=value;
184}
185
186std::string
187User::getProperty(const std::string& name) const {
188 PropertyMap::const_iterator it = properties_.find(name);
189 if (it != properties_.end()) {
190 return ((*it).second);
191 }
192
193 // By returning an empty string rather than throwing, we allow the
194 // flexibility of defaulting to blank if not specified. Let the caller
195 // decide if that is valid or not.
196 return ("");
197}
198
199void
200User::delProperty(const std::string & name) {
201 PropertyMap::iterator it = properties_.find(name);
202 if (it != properties_.end()) {
203 properties_.erase(it);
204 }
205}
206
207const UserId&
209 return (user_id_);
210}
211
212} // 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:105
~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:134
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:116
bool operator==(const UserId &other) const
Compares two UserIds for equality.
Definition user.cc:100
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:110
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:187
User(const UserId &user_id)
Constructor.
Definition user.cc:153
~User()
Destructor.
Definition user.cc:164
void setProperties(const PropertyMap &properties)
Sets the user's properties from a given property map.
Definition user.cc:173
const UserId & getUserId() const
Returns the user's id.
Definition user.cc:208
void setProperty(const std::string &name, const std::string &value)
Sets a given property to the given value.
Definition user.cc:177
const PropertyMap & getProperties() const
Returns a reference to the map of properties.
Definition user.cc:168
void delProperty(const std::string &name)
Removes the given property from the property map.
Definition user.cc:200
#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
const std::string & byteToHex(uint8_t byte)
Converts a byte to a two hex digit string.
Definition str.cc:403
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:145
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
This file defines classes: UserId and User.