/usr/include/pion/user.hpp is in libpion-dev 5.0.6+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | // ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_USER_HEADER__
#define __PION_USER_HEADER__
#include <map>
#include <string>
#include <cstdio>
#include <cstring>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <pion/config.hpp>
#include <pion/error.hpp>
#ifdef PION_HAVE_SSL
#if defined(__APPLE__)
// suppress warnings about OpenSSL being deprecated in OSX
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <openssl/sha.h>
#endif
namespace pion { // begin namespace pion
///
/// user: base class to store user credentials
///
class user :
private boost::noncopyable
{
public:
/// construct a new user object
user(std::string const &username) :
m_username(username)
{}
/// construct a new user object
user(std::string const &username, std::string const &password) :
m_username(username)
{
set_password(password);
}
/// virtual destructor
virtual ~user() {}
/// returns user name as a string
std::string const & get_username() const { return m_username; }
/// returns password for the user (encrypted if SSL is enabled)
std::string const & get_password() const { return m_password; }
/**
* matches password credential for given user
*
* @param password password credentials
*/
virtual bool match_password(const std::string& password) const {
#ifdef PION_HAVE_SSL
unsigned char sha1_hash[SHA_DIGEST_LENGTH];
SHA1(reinterpret_cast<const unsigned char *>(password.data()), password.size(), sha1_hash);
return (memcmp(sha1_hash, m_password_hash, SHA_DIGEST_LENGTH) == 0);
#else
return m_password == password;
#endif
}
/// sets password credentials for given user
virtual void set_password(const std::string& password) {
#ifdef PION_HAVE_SSL
// store encrypted hash value
SHA1((const unsigned char *)password.data(), password.size(), m_password_hash);
// update password string (convert binary to hex)
m_password.clear();
char buf[3];
for (unsigned int n = 0; n < SHA_DIGEST_LENGTH; ++n) {
sprintf(buf, "%.2x", static_cast<unsigned int>(m_password_hash[n]));
m_password += buf;
}
#else
m_password = password;
#endif
}
#ifdef PION_HAVE_SSL
/// sets encrypted password credentials for given user
virtual void set_password_hash(const std::string& password_hash) {
// update password string representation
if (password_hash.size() != SHA_DIGEST_LENGTH*2)
BOOST_THROW_EXCEPTION( error::bad_password_hash() );
m_password = password_hash;
// convert string from hex to binary value
char buf[3];
buf[2] = '\0';
unsigned int hash_pos = 0;
std::string::iterator str_it = m_password.begin();
while (str_it != m_password.end()) {
buf[0] = *str_it;
++str_it;
buf[1] = *str_it;
++str_it;
m_password_hash[hash_pos++] = boost::numeric_cast<unsigned char>(strtoul(buf, 0, 16));
}
}
#endif
protected:
/// username string
const std::string m_username;
/// password string (actual contents depends on implementation)
std::string m_password;
#ifdef PION_HAVE_SSL
/// SHA1 hash of the password
unsigned char m_password_hash[SHA_DIGEST_LENGTH];
#endif
};
/// data type for a user pointer
typedef boost::shared_ptr<user> user_ptr;
///
/// user_manager base class for user container/manager
///
class user_manager :
private boost::noncopyable
{
public:
/// construct a new user_manager object
user_manager(void) {}
/// virtual destructor
virtual ~user_manager() {}
/// returns true if no users are defined
inline bool empty(void) const {
boost::mutex::scoped_lock lock(m_mutex);
return m_users.empty();
}
/**
* used to add a new user with plaintext password
*
* @param username name or identifier of the user to add
* @param password plaintext password of the user to add
*
* @return false if user with such a name already exists
*/
virtual bool add_user(const std::string &username,
const std::string &password)
{
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::iterator i = m_users.find(username);
if (i!=m_users.end())
return false;
user_ptr user_ptr(new user(username, password));
m_users.insert(std::make_pair(username, user_ptr));
return true;
}
/**
* update password for given user
*
* @param username name or identifier of the user to update
* @param password plaintext password of the user to update
*
* @return false if user with such a name doesn't exist
*/
virtual bool update_user(const std::string &username,
const std::string &password)
{
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::iterator i = m_users.find(username);
if (i==m_users.end())
return false;
i->second->set_password(password);
return true;
}
#ifdef PION_HAVE_SSL
/**
* used to add a new user with encrypted password
*
* @param username name or identifier of the user to add
* @param password_hash encrypted password of the user to add
*
* @return false if user with such a name already exists
*/
virtual bool add_user_hash(const std::string &username,
const std::string &password_hash)
{
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::iterator i = m_users.find(username);
if (i!=m_users.end())
return false;
user_ptr user_ptr(new user(username));
user_ptr->set_password_hash(password_hash);
m_users.insert(std::make_pair(username, user_ptr));
return true;
}
/**
* update password for given user with encrypted password
*
* @param username name or identifier of the user to update
* @param password_hash encrypted password of the user to update
*
* @return false if user with such a name doesn't exist
*/
virtual bool update_user_hash(const std::string &username,
const std::string &password_hash)
{
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::iterator i = m_users.find(username);
if (i==m_users.end())
return false;
i->second->set_password_hash(password_hash);
return true;
}
#endif
/**
* used to remove given user
*
* @return false if no user with such username
*/
virtual bool remove_user(const std::string &username) {
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::iterator i = m_users.find(username);
if (i==m_users.end())
return false;
m_users.erase(i);
return true;
}
/**
* Used to locate user object by username
*/
virtual user_ptr get_user(const std::string &username) {
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::const_iterator i = m_users.find(username);
if (i==m_users.end())
return user_ptr();
else
return i->second;
}
/**
* Used to locate user object by username and password
*/
virtual user_ptr get_user(const std::string& username, const std::string& password) {
boost::mutex::scoped_lock lock(m_mutex);
user_map_t::const_iterator i = m_users.find(username);
if (i==m_users.end() || !i->second->match_password(password))
return user_ptr();
else
return i->second;
}
protected:
/// data type for a map of usernames to user objects
typedef std::map<std::string, user_ptr> user_map_t;
/// mutex used to protect access to the user list
mutable boost::mutex m_mutex;
/// user records container
user_map_t m_users;
};
/// data type for a user_manager pointer
typedef boost::shared_ptr<user_manager> user_manager_ptr;
} // end namespace pion
#endif
|