Kea: user.cc Source File

| Kea 2.2.0

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
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
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
70UserId::~UserId() {
71}
72
73const std::vector<uint8_t>&
74UserId::getId() const {
75 return (id_);
76}
77
78 UserId::UserIdType
79UserId::getType() const {
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 (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
102bool
103UserId::operator ==(const UserId & other) const {
104 return ((this->id_type_ == other.id_type_) && (this->id_ == other.id_));
105}
106
107bool
108UserId::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
118std::string
119UserId::lookupTypeStr(UserIdType type) {
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
136 UserId::UserIdType
137UserId::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
147std::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
156User::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
167User::~User() {
168}
169
170const PropertyMap&
171User::getProperties() const {
172 return (properties_);
173}
174
175void
176User::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
189std::string
190User::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
202void
203User::delProperty(const std::string & name) {
204 PropertyMap::iterator it = properties_.find(name);
205 if (it != properties_.end()) {
206 properties_.erase(it);
207 }
208}
209
210const UserId&
211User::getUserId() const {
212 return (user_id_);
213}
214
215} // namespace user_chk
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::dhcp::DUID
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
user_chk::UserId
Encapsulates a unique identifier for a DHCP client.
Definition: user.h:27
user_chk::UserId::getId
const std::vector< uint8_t > & getId() const
Returns a const reference to the actual id value.
Definition: user.cc:74
user_chk::UserId::operator!=
bool operator!=(const UserId &other) const
Compares two UserIds for inequality.
Definition: user.cc:108
user_chk::UserId::~UserId
~UserId()
Destructor.
Definition: user.cc:70
user_chk::UserId::UserId
UserId(UserIdType id_type, const std::vector< uint8_t > &id)
Constructor.
Definition: user.cc:26
user_chk::UserId::lookupType
static UserIdType lookupType(const std::string &type_str)
Returns the id type for a given text label.
Definition: user.cc:137
user_chk::UserId::toText
std::string toText(char delim_char=0x0) const
Returns textual representation of the id.
Definition: user.cc:84
user_chk::UserId::DUID_STR
static const char * DUID_STR
Define the text label DUID id type.
Definition: user.h:42
user_chk::UserId::lookupTypeStr
static std::string lookupTypeStr(UserIdType type)
Returns the text label for a given id type.
Definition: user.cc:119
user_chk::UserId::operator==
bool operator==(const UserId &other) const
Compares two UserIds for equality.
Definition: user.cc:103
user_chk::UserId::getType
UserIdType getType() const
Returns the user id type.
Definition: user.cc:79
user_chk::UserId::operator<
bool operator<(const UserId &other) const
Performs less than comparison of two UserIds.
Definition: user.cc:113
user_chk::UserId::HW_ADDRESS_STR
static const char * HW_ADDRESS_STR
Defines the text label hardware address id type.
Definition: user.h:40
user_chk::UserId::UserIdType
UserIdType
Defines the supported types of user ids.
Definition: user.h:32
user_chk::UserId::HW_ADDRESS
@ HW_ADDRESS
Hardware addresses (MAC) are used for IPv4 clients.
Definition: user.h:34
user_chk::UserId::DUID
@ DUID
DUIDs are used for IPv6 clients.
Definition: user.h:36
user_chk::User::getProperty
std::string getProperty(const std::string &name) const
Fetches the string value for a given property name.
Definition: user.cc:190
user_chk::User::User
User(const UserId &user_id)
Constructor.
Definition: user.cc:156
user_chk::User::~User
~User()
Destructor.
Definition: user.cc:167
user_chk::User::setProperties
void setProperties(const PropertyMap &properties)
Sets the user's properties from a given property map.
Definition: user.cc:176
user_chk::User::getUserId
const UserId & getUserId() const
Returns the user's id.
Definition: user.cc:211
user_chk::User::setProperty
void setProperty(const std::string &name, const std::string &value)
Sets a given property to the given value.
Definition: user.cc:180
user_chk::User::getProperties
const PropertyMap & getProperties() const
Returns a reference to the map of properties.
Definition: user.cc:171
user_chk::User::delProperty
void delProperty(const std::string &name)
Removes the given property from the property map.
Definition: user.cc:203
duid.h
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
hex.h
hwaddr.h
isc::dhcp::HTYPE_ETHER
@ HTYPE_ETHER
Ethernet 10Mbps.
Definition: dhcp4.h:56
isc::util::encode::decodeHex
void decodeHex(const string &input, vector< uint8_t > &result)
Decode a text encoded in the base16 ('hex') format into the original data.
Definition: base_n.cc:474
user_chk
Defines the logger used by the user check hooks library.
Definition: user.cc:19
user_chk::PropertyMap
std::map< std::string, std::string > PropertyMap
Defines a map of string values keyed by string labels.
Definition: user.h:145
user_chk::operator<<
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
isc::dhcp::HWAddr
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
user.h
This file defines classes: UserId and User.