- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

# Account.h

    00001 /*
    00002  *  Phusion Passenger - http://www.modrails.com/
    00003  *  Copyright (c) 2010 Phusion
    00004  *
    00005  *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
    00006  *
    00007  *  Permission is hereby granted, free of charge, to any person obtaining a copy
    00008  *  of this software and associated documentation files (the "Software"), to deal
    00009  *  in the Software without restriction, including without limitation the rights
    00010  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00011  *  copies of the Software, and to permit persons to whom the Software is
    00012  *  furnished to do so, subject to the following conditions:
    00013  *
    00014  *  The above copyright notice and this permission notice shall be included in
    00015  *  all copies or substantial portions of the Software.
    00016  *
    00017  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00018  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00019  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00020  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00021  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00022  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00023  *  THE SOFTWARE.
    00024  */
    00025 #ifndef _PASSENGER_ACCOUNT_H_
    00026 #define _PASSENGER_ACCOUNT_H_
    00027
    00028 #include <string>
    00029 #include <vector>
    00030 #include <boost/shared_ptr.hpp>
    00031 #include "StaticString.h"
    00032 #include "Exceptions.h"
    00033 #include "Utils/StrIntUtils.h"
    00034
    00035 namespace Passenger {
    00036
    00037 using namespace boost;
    00038 using namespace std;
    00039
    00040 /*
    00041  * SECURITY NOTES
    00042  *
    00043  * We want to avoid storing plain text passwords in memory, because attackers may be able
    00044  * to read this process's memory, e.g. through core dumps or debuggers. So in this source
    00045  * file, as well as in several others, we follow these guidelines:
    00046  *
    00047  * - Variables and arguments named userSuppliedPassword represent passwords
    00048  *   supplied by a human, i.e. user input. These variables and arguments have the type
    00049  *   'StaticString' instead of 'string', because we want to avoid accidentally copying
    00050  *   the password values.
    00051  * - Variables and arguments named passwordOrHash might also represent passwords. However,
    00052  *   if it is a password, then it is guaranteed NOT to be supplied by a human, e.g. it's
    00053  *   randomly generated. Therefore it's okay for passwordOrHash to be of type 'string'.
    00054  * - If there is a need to copy the password for whatever reason, then it must be cleared
    00055  *   with ZeroMemoryGuard as soon as possible. Do not use memset(), the code
    00056  *   for ZeroMemoryGuard explains why.
    00057  */
    00058
    00059 class Account {
    00060 public:
    00061         enum Rights {
    00062                 ALL                       = ~0,
    00063                 NONE                      = 0,
    00064
    00065                 // ApplicationPool::Server rights.
    00066                 GET                       = 1 << 0,
    00067                 CLEAR                     = 1 << 1,
    00068                 DETACH                    = 1 << 2,
    00069                 GET_PARAMETERS            = 1 << 3,
    00070                 SET_PARAMETERS            = 1 << 4,
    00071                 INSPECT_BASIC_INFO        = 1 << 5,
    00072                 INSPECT_SENSITIVE_INFO    = 1 << 6,
    00073                 //INSPECT_BACKEND_ADDRESSES = 1 << 6,
    00074                 //INSPECT_DETACH_KEYS       = 1 << 7,
    00075
    00076                 // BacktracesServer rights.
    00077                 INSPECT_BACKTRACES        = 1 << 8,
    00078
    00079                 // Other rights.
    00080                 EXIT                      = 1 << 31
    00081         };
    00082
    00083 private:
    00084         string username;
    00085         string passwordOrHash;
    00086         bool hashGiven;
    00087         Rights rights;
    00088
    00089 public:
    00090         // Urgh, I can't use 'Rights' here as type because apparently bitwise
    00091         // ORing two enums results in an int type.
    00092
    00093         static Rights parseRightsString(const string &str, int defaultValue = NONE) {
    00094                 vector<string> rights_vec;
    00095                 vector<string>::const_iterator it;
    00096                 int result = defaultValue;
    00097
    00098                 split(str, ',', rights_vec);
    00099                 for (it = rights_vec.begin(); it != rights_vec.end(); it++) {
    00100                         if (*it == "all") {
    00101                                 result = ALL;
    00102                         } else if (*it == "none") {
    00103                                 result = NONE;
    00104
    00105                         } else if (*it == "get") {
    00106                                 result |= GET;
    00107                         } else if (*it == "clear") {
    00108                                 result |= CLEAR;
    00109                         } else if (*it == "detach") {
    00110                                 result |= DETACH;
    00111                         } else if (*it == "get_parameters") {
    00112                                 result |= GET_PARAMETERS;
    00113                         } else if (*it == "set_parameters") {
    00114                                 result |= SET_PARAMETERS;
    00115                         } else if (*it == "inspect_basic_info") {
    00116                                 result |= INSPECT_BASIC_INFO;
    00117                         } else if (*it == "inspect_sensitive_info") {
    00118                                 result |= INSPECT_SENSITIVE_INFO;
    00119
    00120                         } else if (*it == "inspect_backtraces") {
    00121                                 result |= INSPECT_BACKTRACES;
    00122
    00123                         } else if (*it == "exit") {
    00124                                 result |= EXIT;
    00125
    00126                         } else if (*it != "") {
    00127                                 throw ArgumentException("Unknown right '" + *it + "'.");
    00128                         }
    00129                 }
    00130
    00131                 return (Rights) result;
    00132         }
    00133
    00134         Account(const string &username, const string &passwordOrHash, bool hashGiven, int rights = ALL) {
    00135                 this->username       = username;
    00136                 this->passwordOrHash = passwordOrHash;
    00137                 this->hashGiven      = hashGiven;
    00138                 this->rights         = (Rights) rights;
    00139         }
    00140
    00141         bool checkPasswordOrHash(const StaticString &userSuppliedPassword) const {
    00142                 if (hashGiven) {
    00143                         return passwordOrHash == createHash(userSuppliedPassword);
    00144                 } else {
    00145                         return userSuppliedPassword == passwordOrHash;
    00146                 }
    00147         }
    00148
    00149         bool hasRights(int rights) const {
    00150                 return this->rights & rights;
    00151         }
    00152
    00153         void setRights(int rights) {
    00154                 this->rights = (Rights) rights;
    00155         }
    00156
    00157         string getUsername() const {
    00158                 return username;
    00159         }
    00160
    00161         string getRawPassword() const {
    00162                 return passwordOrHash;
    00163         }
    00164
    00165         static string createHash(const StaticString &userSuppliedPassword) {
    00166                 // TODO: use bcrypt or something
    00167                 return userSuppliedPassword;
    00168         }
    00169 };
    00170
    00171 typedef shared_ptr<Account> AccountPtr;
    00172
    00173 } // namespace Passenger
    00174
    00175 #endif /* _PASSENGER_ACCOUNT_H_ */