00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 "Utils.h"
00033
00034 namespace Passenger {
00035
00036 using namespace boost;
00037 using namespace std;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 class Account {
00059 public:
00060 enum Rights {
00061 ALL = ~0,
00062 NONE = 0,
00063
00064
00065 GET = 1 << 0,
00066 CLEAR = 1 << 1,
00067 DETACH = 1 << 2,
00068 GET_PARAMETERS = 1 << 3,
00069 SET_PARAMETERS = 1 << 4,
00070 INSPECT_BASIC_INFO = 1 << 5,
00071 INSPECT_SENSITIVE_INFO = 1 << 6,
00072
00073
00074
00075
00076 INSPECT_BACKTRACES = 1 << 8,
00077
00078
00079 EXIT = 1 << 9
00080 };
00081
00082 private:
00083 string username;
00084 string passwordOrHash;
00085 bool hashGiven;
00086 Rights rights;
00087
00088 public:
00089
00090
00091
00092 static Rights parseRightsString(const string &str, int defaultValue = NONE) {
00093 vector<string> rights_vec;
00094 vector<string>::const_iterator it;
00095 int result = defaultValue;
00096
00097 split(str, ',', rights_vec);
00098 for (it = rights_vec.begin(); it != rights_vec.end(); it++) {
00099 if (*it == "all") {
00100 result = ALL;
00101 } else if (*it == "none") {
00102 result = NONE;
00103
00104 } else if (*it == "get") {
00105 result |= GET;
00106 } else if (*it == "clear") {
00107 result |= CLEAR;
00108 } else if (*it == "detach") {
00109 result |= DETACH;
00110 } else if (*it == "get_parameters") {
00111 result |= GET_PARAMETERS;
00112 } else if (*it == "set_parameters") {
00113 result |= SET_PARAMETERS;
00114 } else if (*it == "inspect_basic_info") {
00115 result |= INSPECT_BASIC_INFO;
00116 } else if (*it == "inspect_sensitive_info") {
00117 result |= INSPECT_SENSITIVE_INFO;
00118
00119 } else if (*it == "inspect_backtraces") {
00120 result |= INSPECT_BACKTRACES;
00121
00122 } else if (*it == "exit") {
00123 result |= EXIT;
00124
00125 } else if (*it != "") {
00126 throw ArgumentException("Unknown right '" + *it + "'.");
00127 }
00128 }
00129
00130 return (Rights) result;
00131 }
00132
00133 Account(const string &username, const string &passwordOrHash, bool hashGiven, int rights = ALL) {
00134 this->username = username;
00135 this->passwordOrHash = passwordOrHash;
00136 this->hashGiven = hashGiven;
00137 this->rights = (Rights) rights;
00138 }
00139
00140 bool checkPasswordOrHash(const StaticString &userSuppliedPassword) const {
00141 if (hashGiven) {
00142 return passwordOrHash == createHash(userSuppliedPassword);
00143 } else {
00144 return userSuppliedPassword == passwordOrHash;
00145 }
00146 }
00147
00148 bool hasRights(int rights) const {
00149 return this->rights & rights;
00150 }
00151
00152 void setRights(int rights) {
00153 this->rights = (Rights) rights;
00154 }
00155
00156 string getUsername() const {
00157 return username;
00158 }
00159
00160 string getRawPassword() const {
00161 return passwordOrHash;
00162 }
00163
00164 static string createHash(const StaticString &userSuppliedPassword) {
00165
00166 return userSuppliedPassword;
00167 }
00168 };
00169
00170 typedef shared_ptr<Account> AccountPtr;
00171
00172 }
00173
00174 #endif