Kea 3.3.3
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/duid.h>
10#include <dhcp/hwaddr.h>
12#include <user_chk/user.h>
13#include <util/encode/encode.h>
14
15#include <iomanip>
16#include <sstream>
17
18namespace user_chk {
19
20//********************************* UserId ******************************
21
22const char* UserId::HW_ADDRESS_STR = "HW_ADDR";
23const char* UserId::DUID_STR = "DUID";
24
25UserId::UserId(UserIdType id_type, const std::vector<uint8_t>& id)
26 : id_type_(id_type), id_(id) {
27 if (id.size() == 0) {
28 isc_throw(isc::BadValue, "UserId id may not be blank");
29 }
30}
31
32UserId::UserId(UserIdType id_type, const std::string & id_str) :
33 id_type_(id_type) {
34 if (id_str.empty()) {
35 isc_throw(isc::BadValue, "UserId id string may not be blank");
36 }
37
38 // Convert the id string to vector.
39 // Input is expected to be 2-digits per bytes, no delimiters.
40 std::vector<uint8_t> addr_bytes;
41
42 // Strip out colon delimiters, decodeHex doesn't like them.
43 std::string clean_id_str = id_str;
44 std::string::iterator end_pos = std::remove(clean_id_str.begin(),
45 clean_id_str.end(), ':');
46 clean_id_str.erase(end_pos, clean_id_str.end());
47
48 isc::util::encode::decodeHex(clean_id_str, addr_bytes);
49
50 // Attempt to instantiate the appropriate id class to leverage validation.
51 switch (id_type) {
52 case HW_ADDRESS: {
53 isc::dhcp::HWAddr hwaddr(addr_bytes, isc::dhcp::HTYPE_ETHER);
54 break;
55 }
56 case DUID: {
57 isc::dhcp::DUID duid(addr_bytes);
58 break;
59 }
60 default:
61 isc_throw (isc::BadValue, "Invalid id_type: " << id_type);
62 break;
63 }
64
65 // It's a valid id.
66 id_ = addr_bytes;
67}
68
71
72const std::vector<uint8_t>&
74 return (id_);
75}
76
79 return (id_type_);
80}
81
82std::string
83UserId::toText(char delim_char) const {
84 std::string tmp;
85 bool delim = false;
86 for (auto const& it : id_) {
87 if (delim_char && delim) {
88 tmp.push_back(delim_char);
89 }
90
91 tmp.append(isc::util::str::byteToHex(it));
92 delim = true;
93 }
94
95 return (tmp);
96}
97
98bool
99UserId::operator ==(const UserId & other) const {
100 return ((this->id_type_ == other.id_type_) && (this->id_ == other.id_));
101}
102
103bool
104UserId::operator !=(const UserId & other) const {
105 return (!(*this == other));
106}
107
108bool
109UserId::operator <(const UserId & other) const {
110 return ((this->id_type_ < other.id_type_) ||
111 ((this->id_type_ == other.id_type_) && (this->id_ < other.id_)));
112}
113
114std::string
116 const char* tmp = NULL;
117 switch (type) {
118 case HW_ADDRESS:
119 tmp = HW_ADDRESS_STR;
120 break;
121 case DUID:
122 tmp = DUID_STR;
123 break;
124 default:
125 isc_throw(isc::BadValue, "Invalid UserIdType:" << type);
126 break;
127 }
128
129 return (std::string(tmp));
130}
131
133UserId::lookupType(const std::string& type_str) {
134 if (type_str.compare(HW_ADDRESS_STR) == 0) {
135 return (HW_ADDRESS);
136 } else if (type_str.compare(DUID_STR) == 0) {
137 return (DUID);
138 }
139
140 isc_throw(isc::BadValue, "Invalid UserIdType string:" << type_str);
141}
142
143std::ostream&
144operator<<(std::ostream& os, const UserId& user_id) {
145 std::string tmp = UserId::lookupTypeStr(user_id.getType());
146 os << tmp << "=" << user_id.toText();
147 return (os);
148}
149
150//********************************* User ******************************
151
152User::User(const UserId& user_id) : user_id_(user_id) {
153}
154
155User::User(UserId::UserIdType id_type, const std::vector<uint8_t>& id)
156 : user_id_(id_type, id) {
157}
158
159User::User(UserId::UserIdType id_type, const std::string& id_str)
160 : user_id_(id_type, id_str) {
161}
162
164}
165
166const PropertyMap&
168 return (properties_);
169}
170
171void
173 properties_ = properties;
174}
175
176void User::setProperty(const std::string& name, const std::string& value) {
177 if (name.empty()) {
178 isc_throw (isc::BadValue, "User property name cannot be blank");
179 }
180
181 // Note that if the property exists its value will be updated.
182 properties_[name]=value;
183}
184
185std::string
186User::getProperty(const std::string& name) const {
187 PropertyMap::const_iterator it = properties_.find(name);
188 if (it != properties_.end()) {
189 return ((*it).second);
190 }
191
192 // By returning an empty string rather than throwing, we allow the
193 // flexibility of defaulting to blank if not specified. Let the caller
194 // decide if that is valid or not.
195 return ("");
196}
197
198void
199User::delProperty(const std::string & name) {
200 PropertyMap::iterator it = properties_.find(name);
201 if (it != properties_.end()) {
202 properties_.erase(it);
203 }
204}
205
206const UserId&
208 return (user_id_);
209}
210
211} // 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
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
const std::vector< uint8_t > & getId() const
Returns a const reference to the actual id value.
Definition user.cc:73
bool operator!=(const UserId &other) const
Compares two UserIds for inequality.
Definition user.cc:104
~UserId()
Destructor.
Definition user.cc:69
UserId(UserIdType id_type, const std::vector< uint8_t > &id)
Constructor.
Definition user.cc:25
static UserIdType lookupType(const std::string &type_str)
Returns the id type for a given text label.
Definition user.cc:133
std::string toText(char delim_char=0x0) const
Returns textual representation of the id.
Definition user.cc:83
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:115
bool operator==(const UserId &other) const
Compares two UserIds for equality.
Definition user.cc:99
UserIdType getType() const
Returns the user id type.
Definition user.cc:78
bool operator<(const UserId &other) const
Performs less than comparison of two UserIds.
Definition user.cc:109
static const char * HW_ADDRESS_STR
Defines the text label hardware address id type.
Definition user.h:40
std::string getProperty(const std::string &name) const
Fetches the string value for a given property name.
Definition user.cc:186
User(const UserId &user_id)
Constructor.
Definition user.cc:152
~User()
Destructor.
Definition user.cc:163
void setProperties(const PropertyMap &properties)
Sets the user's properties from a given property map.
Definition user.cc:172
const UserId & getUserId() const
Returns the user's id.
Definition user.cc:207
void setProperty(const std::string &name, const std::string &value)
Sets a given property to the given value.
Definition user.cc:176
const PropertyMap & getProperties() const
Returns a reference to the map of properties.
Definition user.cc:167
void delProperty(const std::string &name)
Removes the given property from the property map.
Definition user.cc:199
#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:18
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:144
Hardware type that represents information from DHCPv4 packet.
Definition hwaddr.h:20
This file defines classes: UserId and User.