Kea  2.1.7-git
user_registry.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 <user_registry.h>
10 #include <user.h>
11 
12 namespace user_chk {
13 
15 }
16 
18 }
19 
20 void
22  if (!user) {
23  isc_throw (UserRegistryError, "UserRegistry cannot add blank user");
24  }
25 
26  UserPtr found_user;
27  if ((found_user = findUser(user->getUserId()))) {
28  isc_throw (UserRegistryError, "UserRegistry duplicate user: "
29  << user->getUserId());
30  }
31 
32  users_[user->getUserId()] = user;
33 }
34 
35 const UserPtr&
36 UserRegistry::findUser(const UserId& id) const {
37  static UserPtr empty;
38  UserMap::const_iterator it = users_.find(id);
39  if (it != users_.end()) {
40  const UserPtr tmp = (*it).second;
41  return ((*it).second);
42  }
43 
44  return empty;
45 }
46 
47 void
49  static UserPtr empty;
50  UserMap::iterator it = users_.find(id);
51  if (it != users_.end()) {
52  users_.erase(it);
53  }
54 }
55 
56 const UserPtr&
58  UserId id(UserId::HW_ADDRESS, hwaddr.hwaddr_);
59  return (findUser(id));
60 }
61 
62 const UserPtr&
64  UserId id(UserId::DUID, duid.getDuid());
65  return (findUser(id));
66 }
67 
69  if (!source_) {
71  "UserRegistry: cannot refresh, no data source");
72  }
73 
74  // If the source isn't open, open it.
75  if (!source_->isOpen()) {
76  source_->open();
77  }
78 
79  // Make a copy in case something goes wrong midstream.
80  UserMap backup(users_);
81 
82  // Empty the registry then read users from source until source is empty.
83  clearall();
84  try {
85  UserPtr user;
86  while ((user = source_->readNextUser())) {
87  addUser(user);
88  }
89  } catch (const std::exception& ex) {
90  // Source was compromised so restore registry from backup.
91  users_ = backup;
92  // Close the source.
93  source_->close();
94  isc_throw (UserRegistryError, "UserRegistry: refresh failed during read"
95  << ex.what());
96  }
97 
98  // Close the source.
99  source_->close();
100 }
101 
103  users_.clear();
104 }
105 
107  if (!source) {
109  "UserRegistry: data source cannot be set to null");
110  }
111 
112  source_ = source;
113 }
114 
116  return (source_);
117 }
118 
119 } // namespace user_chk
void setSource(UserDataSourcePtr &source)
Sets the data source to the given value.
void refresh()
Updates the registry from its data source.
const UserDataSourcePtr & getSource()
Returns a reference to the data source.
Encapsulates a unique identifier for a DHCP client.
Definition: user.h:27
std::vector< uint8_t > hwaddr_
Definition: hwaddr.h:98
void addUser(UserPtr &user)
Adds a given user to the registry.
boost::shared_ptr< User > UserPtr
Defines a smart pointer to a User.
Definition: user.h:241
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Holds DUID (DHCPv6 Unique Identifier)
Definition: duid.h:27
const UserPtr & findUser(const UserId &id) const
Finds a user in the registry by user id.
Defines the logger used by the user check hooks library.
Definition: user.cc:19
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
This file defines classes: UserId and User.
UserRegistry()
Constructor.
void clearall()
Removes all entries from the registry.
std::map< UserId, UserPtr > UserMap
Defines a map of unique Users keyed by UserId.
Definition: user_registry.h:30
const std::vector< uint8_t > & getDuid() const
Returns a const reference to the actual DUID value.
Definition: duid.cc:46
~UserRegistry()
Destructor.
void removeUser(const UserId &id)
Removes a user from the registry by user id.
boost::shared_ptr< UserDataSource > UserDataSourcePtr
Defines a smart pointer to a UserDataSource.
Hardware addresses (MAC) are used for IPv4 clients.
Definition: user.h:34
Hardware type that represents information from DHCPv4 packet.
Definition: hwaddr.h:20
Defines the class, UserRegistry.
Thrown UserRegistry encounters an error.
Definition: user_registry.h:22
DUIDs are used for IPv6 clients.
Definition: user.h:36