00001 #ifndef _ots_Utilities_WebUsers_h_
00002 #define _ots_Utilities_WebUsers_h_
00003
00004 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00005 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00006 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
00007
00008 #include <string>
00009 #include <vector>
00010 #include <iostream>
00011
00012 #define WEB_LOGIN_DB_PATH std::string(getenv("SERVICE_DATA_PATH")) + "/LoginData/"
00013 #define HASHES_DB_PATH "HashesData/"
00014 #define USERS_DB_PATH "UsersData/"
00015 #define USERS_LOGIN_HISTORY_PATH USERS_DB_PATH + "UserLoginHistoryData/"
00016 #define USERS_PREFERENCES_PATH USERS_DB_PATH + "UserPreferencesData/"
00017 #define TOOLTIP_DB_PATH USERS_DB_PATH + "/TooltipData/"
00018
00019
00020
00021
00022
00023 namespace ots
00024 {
00025
00026 class HttpXmlDocument;
00027
00028 class WebUsers
00029 {
00030 public:
00031 WebUsers();
00032
00033 enum {
00034 SESSION_ID_LENGTH = 512,
00035 COOKIE_CODE_LENGTH = 512,
00036 NOT_FOUND_IN_DATABASE = uint64_t(-1),
00037 USERNAME_LENGTH = 4,
00038 DISPLAY_NAME_LENGTH = 4,
00039 };
00040
00041 enum {
00042 DB_SAVE_OPEN_AND_CLOSE,
00043 DB_SAVE_OPEN,
00044 DB_SAVE_CLOSE
00045 };
00046
00047 enum {
00048 DB_USERS,
00049 DB_HASHES
00050 };
00051
00052 enum {
00053 MOD_TYPE_UPDATE,
00054 MOD_TYPE_ADD,
00055 MOD_TYPE_DELETE
00056 };
00057
00058 static const std::string DEFAULT_ADMIN_USERNAME;
00059 static const std::string DEFAULT_ADMIN_DISPLAY_NAME;
00060
00061 static const std::string REQ_NO_LOGIN_RESPONSE;
00062 static const std::string REQ_NO_PERMISSION_RESPONSE;
00063 static const std::string REQ_USER_LOCKOUT_RESPONSE;
00064
00065 static const std::string SECURITY_TYPE_NONE;
00066 static const std::string SECURITY_TYPE_DIGEST_ACCESS;
00067 static const std::string SECURITY_TYPE_KERBEROS;
00068
00069 bool createNewAccount (std::string username, std::string displayName);
00070 void cleanupExpiredEntries (std::vector<std::string> *loggedOutUsernames = 0);
00071 std::string createNewLoginSession (std::string uuid, std::string ip = "0");
00072
00073 uint64_t attemptActiveSession (std::string uuid, std::string &jumbledUser, std::string jumbledPw, std::string &newAccountCode);
00074 uint64_t isCookieCodeActiveForLogin (std::string uuid, std::string &cookieCode,std::string &username);
00075 bool cookieCodeIsActiveForRequest (std::string &cookieCode, uint8_t *userPermissions = 0, uint64_t *uid = 0, std::string ip = "0", bool refresh = true, std::string *userWithLock = 0);
00076 uint64_t cookieCodeLogout (std::string cookieCode, bool logoutOtherUserSessions, uint64_t *uid = 0, std::string ip = "0");
00077
00078 std::string getUsersDisplayName (uint64_t uid);
00079 std::string getUsersUsername (uint64_t uid);
00080 uint64_t getActiveSessionCountForUser (uint64_t uid);
00081 uint8_t getPermissionsForUser (uint64_t uid);
00082 void insertSettingsForUser (uint64_t uid, HttpXmlDocument *xmldoc,bool includeAccounts=false);
00083 std::string getGenericPreference (uint64_t uid, const std::string &preferenceName, HttpXmlDocument *xmldoc = 0) const;
00084
00085 void changeSettingsForUser (uint64_t uid, const std::string &bgcolor, const std::string &dbcolor, const std::string &wincolor, const std::string &layout, const std::string &syslayout);
00086 void setGenericPreference (uint64_t uid, const std::string &preferenceName, const std::string &preferenceValue);
00087 static void tooltipCheckForUsername (const std::string& username, HttpXmlDocument *xmldoc, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId);
00088 static void tooltipSetNeverShowForUsername (const std::string& username, HttpXmlDocument *xmldoc, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId, bool doNeverShow, bool temporarySilence);
00089
00090 void modifyAccountSettings (uint64_t uid_master, uint8_t cmd_type, std::string username, std::string displayname, std::string permissions);
00091 bool setUserWithLock (uint64_t uid_master, bool lock, std::string username);
00092 std::string getUserWithLock () { return usersUsernameWithLock_; }
00093
00094 std::string getActiveUsersString ();
00095
00096 bool getUserInfoForCookie (std::string &cookieCode, std::string *userName, std::string *displayName = 0, uint64_t *activeSessionIndex = 0);
00097
00098 bool isUsernameActive (std::string username) const;
00099 bool isUserIdActive (uint64_t uid) const;
00100 uint64_t getAdminUserID ();
00101 std::string getSecurity ();
00102
00103 static void deleteUserData ();
00104 static void resetAllUserTooltips (const std::string &userNeedle = "*");
00105
00106 static void NACDisplayThread (std::string nac, std::string user);
00107
00108 private:
00109 void loadSecuritySelection ();
00110 void loadUserWithLock ();
00111 unsigned int hexByteStrToInt (const char *h);
00112 void intToHexStr (uint8_t i, char *h);
00113 std::string sha512 (std::string user, std::string password, std::string &salt);
00114 std::string dejumble (std::string jumbledUser, std::string sessionId);
00115 std::string createNewActiveSession (uint64_t uid,std::string ip = "0", uint64_t asIndex = 0);
00116 bool addToHashesDatabase (std::string hash);
00117 std::string genCookieCode ();
00118 std::string refreshCookieCode (unsigned int i, bool enableRefresh = true);
00119 void removeActiveSessionEntry (unsigned int i);
00120 void removeLoginSessionEntry (unsigned int i);
00121 bool deleteAccount (std::string username, std::string displayName);
00122
00123 void saveToDatabase (FILE * fp, std::string field, std::string value, uint8_t type = DB_SAVE_OPEN_AND_CLOSE, bool addNewLine = true);
00124 bool saveDatabaseToFile (uint8_t db);
00125 bool loadDatabases ();
00126
00127 uint64_t searchUsersDatabaseForUsername (std::string username) const;
00128 uint64_t searchUsersDatabaseForUserId (uint64_t uid) const;
00129 uint64_t searchLoginSessionDatabaseForUUID (std::string uuid) const;
00130 uint64_t searchHashesDatabaseForHash (std::string hash);
00131 uint64_t searchActiveSessionDatabaseForCookie (std::string cookieCode) const;
00132
00133 static std::string getTooltipFilename (const std::string& username, const std::string &srcFile, const std::string &srcFunc, const std::string &srcId);
00134
00135
00136
00137 std::vector<std::string> UsersDatabaseEntryFields,HashesDatabaseEntryFields;
00138 bool CareAboutCookieCodes_;
00139 std::string securityType_;
00140
00141
00142
00143
00144
00145
00146
00147
00148 std::vector<std::string> LoginSessionIdVector, LoginSessionUUIDVector, LoginSessionIpVector;
00149 std::vector<time_t> LoginSessionStartTimeVector;
00150 std::vector<uint8_t> LoginSessionAttemptsVector;
00151 enum {
00152 LOGIN_SESSION_EXPIRATION_TIME = 5*60,
00153 LOGIN_SESSION_ATTEMPTS_MAX = 5,
00154 };
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 std::vector<std::string> ActiveSessionCookieCodeVector, ActiveSessionIpVector;
00166 std::vector<uint64_t> ActiveSessionUserIdVector, ActiveSessionIndex;
00167 std::vector<time_t> ActiveSessionStartTimeVector;
00168 enum {
00169 ACTIVE_SESSION_EXPIRATION_TIME = 120*60,
00170 ACTIVE_SESSION_COOKIE_OVERLAP_TIME = 10*60,
00171 ACTIVE_SESSION_STALE_COOKIE_LIMIT = 10,
00172 };
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 std::vector<std::string> UsersUsernameVector, UsersDisplayNameVector, UsersSaltVector, UsersLastModifierUsernameVector;
00187 std::vector<uint8_t> UsersPermissionsVector;
00188 std::vector<uint64_t> UsersUserIdVector;
00189 std::vector<time_t> UsersLastLoginAttemptVector, UsersAccountCreatedTimeVector, UsersLastModifiedTimeVector;
00190 std::vector<uint8_t> UsersLoginFailureCountVector;
00191 uint64_t usersNextUserId_;
00192 enum {
00193 USERS_LOGIN_HISTORY_SIZE = 20,
00194 USERS_GLOBAL_HISTORY_SIZE = 1000,
00195 USERS_MAX_LOGIN_FAILURES = 20,
00196 };
00197 std::string usersUsernameWithLock_;
00198
00199 std::vector<std::string> UsersLoggedOutUsernames_;
00200
00201
00202
00203 std::vector<std::string> HashesVector;
00204 std::vector<time_t> HashesAccessTimeVector;
00205 };
00206
00207 const std::string WebUsers::REQ_NO_LOGIN_RESPONSE = "NoLogin";
00208 const std::string WebUsers::REQ_NO_PERMISSION_RESPONSE = "NoPermission";
00209 const std::string WebUsers::REQ_USER_LOCKOUT_RESPONSE = "UserLockout";
00210
00211 const std::string WebUsers::SECURITY_TYPE_NONE = "NoSecurity";
00212 const std::string WebUsers::SECURITY_TYPE_DIGEST_ACCESS = "DigestAccessAuthentication";
00213 const std::string WebUsers::SECURITY_TYPE_KERBEROS = "Kerberos";
00214
00215
00216 void WebUsers::deleteUserData ()
00217 {
00218
00219 std::system(("rm -f " + (std::string)WEB_LOGIN_DB_PATH + HASHES_DB_PATH + "/*").c_str());
00220 std::system(("rm -f " + (std::string)WEB_LOGIN_DB_PATH + USERS_DB_PATH + "/*").c_str());
00221 std::system(("rm -f " + (std::string)WEB_LOGIN_DB_PATH + USERS_LOGIN_HISTORY_PATH + "/*").c_str());
00222 std::system(("rm -f " + (std::string)WEB_LOGIN_DB_PATH + USERS_PREFERENCES_PATH + "/*").c_str());
00223 std::system(("rm -rf " + (std::string)WEB_LOGIN_DB_PATH + TOOLTIP_DB_PATH).c_str());
00224
00225 std::string serviceDataPath = getenv("SERVICE_DATA_PATH");
00226
00227 std::system(("rm -rf " + std::string(serviceDataPath) + "/MacroData/").c_str());
00228 std::system(("rm -rf " + std::string(serviceDataPath) + "/MacroHistory/").c_str());
00229 std::system(("rm -rf " + std::string(serviceDataPath) + "/MacroExport/").c_str());
00230
00231
00232 std::system(("rm -rf " + std::string(serviceDataPath) + "/ConsolePreferences/").c_str());
00233
00234
00235 std::system(("rm -rf " + std::string(serviceDataPath) + "/OtsWizardData/").c_str());
00236
00237
00238 std::system(("rm -rf " + std::string(serviceDataPath) + "/ProgressBarData/").c_str());
00239
00240
00241 std::system(("rm -rf " + std::string(serviceDataPath) + "/RunNumber/").c_str());
00242 std::system(("rm -rf " + std::string(serviceDataPath) + "/RunControlData/").c_str());
00243
00244
00245 std::system(("rm -rf " + std::string(serviceDataPath) + "/VisualizerData/").c_str());
00246
00247
00248 std::system(("rm -f " + std::string(serviceDataPath) + "/ActiveConfigurationGroups.cfg").c_str());
00249
00250
00251 std::system(("rm -rf " + std::string(getenv("LOGBOOK_DATA_PATH")) + "/").c_str());
00252
00253 std::cout << __COUT_HDR_FL__ << "$$$$$$$$$$$$$$ Successfully deleted ALL service user data $$$$$$$$$$$$" << std::endl;
00254 }
00255 }
00256
00257 #endif