/usr/include/pion/net/PionUser.hpp is in libpion-net-dev 4.0.7+dfsg-3.1ubuntu2.
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 300 301 302 303 304 | // ------------------------------------------------------------------
// pion-net: a C++ framework for building lightweight HTTP interfaces
// ------------------------------------------------------------------
// Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_PIONUSER_HEADER__
#define __PION_PIONUSER_HEADER__
#include <map>
#include <string>
#include <cstring>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <pion/PionConfig.hpp>
#include <pion/PionException.hpp>
#ifdef PION_HAVE_SSL
#include <openssl/sha.h>
#endif
namespace pion { // begin namespace pion
namespace net { // begin namespace net (Pion Network Library)
///
/// PionUser: base class to store user credentials
///
class PionUser :
private boost::noncopyable
{
public:
/// exception thrown if a bad password hash is given to setPasswordHash()
class BadPasswordHash : public std::exception {
public:
virtual const char* what() const throw() {
return "Invalid password hash provided";
}
};
/// construct a new PionUser object
PionUser(std::string const &username) :
m_username(username)
{}
/// construct a new PionUser object
PionUser(std::string const &username, std::string const &password) :
m_username(username)
{
setPassword(password);
}
/// virtual destructor
virtual ~PionUser() {}
/// returns user name as a string
std::string const & getUsername() const { return m_username; }
/// returns password for the user (encrypted if SSL is enabled)
std::string const & getPassword() const { return m_password; }
/**
* matches password credential for given user
*
* @param password password credentials
*/
virtual bool matchPassword(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 setPassword(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 setPasswordHash(const std::string& password_hash) {
// update password string representation
if (password_hash.size() != SHA_DIGEST_LENGTH*2)
throw BadPasswordHash();
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 PionUser pointer
typedef boost::shared_ptr<PionUser> PionUserPtr;
///
/// PionUserManager base class for PionUser container/manager
///
class PionUserManager :
private boost::noncopyable
{
public:
/// construct a new PionUserManager object
PionUserManager(void) {}
/// virtual destructor
virtual ~PionUserManager() {}
/// 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 addUser(const std::string &username,
const std::string &password)
{
boost::mutex::scoped_lock lock(m_mutex);
UserMap::iterator i = m_users.find(username);
if (i!=m_users.end())
return false;
PionUserPtr user(new PionUser(username, password));
m_users.insert(std::make_pair(username, user));
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 updateUser(const std::string &username,
const std::string &password)
{
boost::mutex::scoped_lock lock(m_mutex);
UserMap::iterator i = m_users.find(username);
if (i==m_users.end())
return false;
i->second->setPassword(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 addUserHash(const std::string &username,
const std::string &password_hash)
{
boost::mutex::scoped_lock lock(m_mutex);
UserMap::iterator i = m_users.find(username);
if (i!=m_users.end())
return false;
PionUserPtr user(new PionUser(username));
user->setPasswordHash(password_hash);
m_users.insert(std::make_pair(username, user));
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 updateUserHash(const std::string &username,
const std::string &password_hash)
{
boost::mutex::scoped_lock lock(m_mutex);
UserMap::iterator i = m_users.find(username);
if (i==m_users.end())
return false;
i->second->setPasswordHash(password_hash);
return true;
}
#endif
/**
* used to remove given user
*
* @return false if no user with such username
*/
virtual bool removeUser(const std::string &username) {
boost::mutex::scoped_lock lock(m_mutex);
UserMap::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 PionUserPtr getUser(const std::string &username) {
boost::mutex::scoped_lock lock(m_mutex);
UserMap::const_iterator i = m_users.find(username);
if (i==m_users.end())
return PionUserPtr();
else
return i->second;
}
/**
* Used to locate user object by username and password
*/
virtual PionUserPtr getUser(const std::string& username, const std::string& password) {
boost::mutex::scoped_lock lock(m_mutex);
UserMap::const_iterator i = m_users.find(username);
if (i==m_users.end() || !i->second->matchPassword(password))
return PionUserPtr();
else
return i->second;
}
protected:
/// data type for a map of usernames to user objects
typedef std::map<std::string, PionUserPtr> UserMap;
/// mutex used to protect access to the user list
mutable boost::mutex m_mutex;
/// user records container
UserMap m_users;
};
/// data type for a PionUserManager pointer
typedef boost::shared_ptr<PionUserManager> PionUserManagerPtr;
} // end namespace net
} // end namespace pion
#endif
|