[Gcblue-commits] gcb_wx/include/database md5.h,NONE,1.1 md5class.h,NONE,1.1 tcAccountDatabase.h,NONE
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2005-05-06 23:57:26
|
Update of /cvsroot/gcblue/gcb_wx/include/database In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11073/include/database Added Files: md5.h md5class.h tcAccountDatabase.h tcPlayerDatabase.h Log Message: added player name to connection status, update rates are slower for non-controlled objects, non-controlled objects now display as green, added chat text popup --- NEW FILE: md5.h --- /* MD5.H - header file for MD5C.C */ //See internet RFC 1321, "The MD5 Message-Digest Algorithm" /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved. License to copy and use this software is granted provided that it is identified as the "RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing this software or this function. License is also granted to make and use derivative works provided that such works are identified as "derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm" in all material mentioning or referencing the derived work. RSA Data Security, Inc. makes no representations concerning either the merchantability of this software or the suitability of this software for any particular purpose. It is provided "as is" without express or implied warranty of any kind. These notices must be retained in any copies of any part of this documentation and/or software. */ /* MD5 context. */ #ifndef _MD5CONTEXT_H #define _MD5CONTEXT_H #ifndef POINTER /* POINTER defines a generic pointer type */ typedef unsigned char *POINTER; #endif #ifndef UINT2 /* UINT2 defines a two byte word */ typedef unsigned short int UINT2; #endif #ifndef UINT4 /* UINT4 defines a four byte word */ typedef unsigned long int UINT4; #endif #define PROTO_LIST(list) list typedef struct { UINT4 state[4]; /* state (ABCD) */ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ unsigned char buffer[64]; /* input buffer */ } MD5_CTX; #ifdef __cplusplus //added by Jim Howard so that these functions can be called from c++ extern "C" { #endif void MD5Init PROTO_LIST ((MD5_CTX *)); void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int)); void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); #ifdef __cplusplus } #endif #endif --- NEW FILE: tcPlayerDatabase.h --- #ifndef _PLAYERDATABASE_H_ #define _PLAYERDATABASE_H_ /** name alliance funds owned entities score */ /** * Interface to player database. The player database has state info for the * current game. */ class tcPlayerDatabase { public: enum { SUCCESS = 0, PLAYER_NOT_FOUND = 1, INVALID_ENTITY = 2, INSUFFICIENT_FUNDS = 3, DUPLICATE_PLAYER = 4 }; /// Add new player to database int AddPlayer(const std::string& player); int AdjustScore(const std::string& player, float amount); int GetPlayerAlliance(const std::string& player); std::vector<long>& GetPlayerEntities(const std::string& player); float GetPlayerFunds(const std::string& player); float GetPlayerScore(const std::string& player); /// @return true if player is in database bool IsPlayerValid(const std::string& player); int RemovePlayer(const std::string& player); int SetPlayerAlliance(const std::string& player, int alliance); /// use empty string to add or delete without transferring int TransferEntity(long entity, const std::string& from, const std::string& to); /// use empty string to add or delete without transferring int TransferMultipleEntities(std::vector<long>& entityList, const std::string& from, const std::string& to); /// use empty string to add or remove without transferring int TransferFunds(float amount, const std::string& from, const std::string& to); private: std::vector<long> temporaryEntityList; }; #endif --- NEW FILE: md5class.h --- // md5class.h: interface for the CMD5 class. // ////////////////////////////////////////////////////////////////////// #ifndef _MD5CLASS_H_ #define _MD5CLASS_H_ #if _MSC_VER > 1000 #pragma once #endif #include <string> /*************************************************************************** This class is a utility wrapper for 'C' code contained in internet RFC 1321, "The MD5 Message-Digest Algorithm". It calculates a cryptological hash value, called a "digest" from a character string. For every unique character string the MD5 hash is guaranteed to be unique. The MD5 hash has the property that given the digest, it's thought to be impossible to get back to the plain text string with existing technology. In this implementation the digest is always a 32 digit hex number, regardless of the length of the input plaintext. This class is helpful for programs which store passwords. Rather than storing the password directly, the programmer should store the MD5 digest of the password. Then when the user enters a password, compute the MD5 digest of the input password. If it is identical to the stored digest, then the user has entered the correct password. It doesn't matter if an evil person sees the digest, since he or she can't get from the digest to the password. At least not unless the user enters a word out of the dictionary, since the evil person could hash the whole dictionary. One way to defeat a dictionary attack is to append a non-text character onto the password, so that even if the user enters a dumb password like "password", you just append some non alpha character to the entered password, i.e. password = "password" + "$". By always appending a nonalpha character, your stored digest isn't in the attacker's dictionary. You can then safely post the digest of the password on a highway billboard. Example pseudocode: { std::string storedPasswordDigest = GetPasswordDigestFromStorage(); std::string passwordEnteredbyUser; cout << "Enter password:" ; cin >> passwordEnteredbyUser; CMD5 md5(passwordEnteredbyUser.c_str()); //note c_str() returns a pointer to the std::string's character buffer, just like CString's "GetBuffer" member function. if(md5.getMD5Digest != storedPasswordDigest) { //user has entered an invalid password cout << "Incorrect password!"; exit(1); } //if we get here, then the user entered a valid password } ************************************************************************** Use this code as you see fit. It is provided "as is" without express or implied warranty of any kind. Jim Howard, jn...@ju... ***************************************************************************/ class CMD5 { public: CMD5(); //default ctor CMD5(const char* plainText); //set plaintext in ctor CMD5(const std::string& plainText); void setPlainText(const std::string& plainText); void setPlainText(const char* plainText); //set plaintext with a mutator, it's ok to //to call this multiple times, the digest is recalculated after each call. const char* getMD5Digest() const; //access message digest (aka hash), return 0 if plaintext has not been set bool isDigestValid() const; bool operator==(const CMD5& rhs); virtual ~CMD5(); private: bool calcDigest(); //this function computes the digest by calling the RFC 1321 'C' code bool m_digestValid; //false until the plaintext has been set and digest computed unsigned char m_digest[16]; //the numerical value of the digest char m_digestString[33]; //Null terminated string value of the digest expressed in hex digits std::string m_plainText; //char* m_plainText; //a pointer to the plain text. If casting away the const-ness //worries you, you could either make a local copy of the plain //text string instead of just pointing at the user's string or //modify the RFC 1321 code to take 'const' plaintext. }; #endif --- NEW FILE: tcAccountDatabase.h --- /** ** @file tcAccountDatabase.h */ /* Copyright (C) 2005 Dewitt Colclough (de...@tw...) ** All rights reserved. ** This file is part of the Global Conflict Blue (GCB) program. ** GCB is free software; you can redistribute it and/or modify ** it under the terms of version 2 of the GNU General Public License as ** published by the Free Software Foundation. ** GCB is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** You should have received a copy of the GNU General Public License ** along with GCB; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _ACCOUNTDATABASE_H_ #define _ACCOUNTDATABASE_H_ #include <map> #include <string> class CMD5; namespace sqlite { class connection; } /** * Interface to account database. Currently this mixes the database interface with * game-related use of data. Eventually may want to separate this. */ class tcAccountDatabase { public: /// status codes enum { SUCCESS = 0, USER_NOT_FOUND = 1, PASSWORD_INVALID = 2, DUPLICATE_USER = 3, DUPLICATE_LOGIN = 4, INSUFFICIENT_PERMISSION = 5, UNKNOWN_ERROR = 99 }; struct UserData { /// permission flags enum { ALLIANCE_SWITCH = 1, ///< allows user to switch alliance PLATFORM_CONTROL = 2, ///< allows user to take control of entities }; std::string username; std::string password_hash; std::string email; std::string last_ip; unsigned int login_count; unsigned long last_login; ///< seconds since Jan 1, 1970 unsigned long last_logout; ///< seconds since Jan 1, 1970 unsigned int alliance; ///< 0 for no alliance selected float score; int flags; ///< permission flags bool CanControlPlatforms() const {return (flags & PLATFORM_CONTROL) != 0;} bool CanSwitchAlliance() const {return (alliance==0) || ((flags & ALLIANCE_SWITCH) != 0);} }; /// Add new user to account database int AddUser(const std::string& user, const std::string& hash, const std::string& email); /// @return SUCCESS if user is authenticated, otherwise error code int AuthenticateUser(const std::string& user, const std::string& hash); std::string ErrorCodeToString(int code); bool GetUserData(const std::string& user, UserData& userData); /// loads user to cachedUserData (if nessessary) and returns reference UserData& GetCachedUserData(const std::string& user); bool IsUserLoggedIn(const std::string& user); /// Call once at login to update database with login info for user int LogIn(const std::string& user, const std::string ipAddress); /// Call once to log out user int LogOut(const std::string& user); int LookupFieldIndex(const std::string& field); /// @return SUCCESS if alliance changed, otherwise error code int SetUserAlliance(const std::string& user, unsigned int alliance); /// Updates all fields of database entry (user name cannot be changed this way) int UpdateDatabaseEntry(UserData& userData); const char* GetMD5digest(const char* plainText); const char* GetMD5digest(const std::string& plainText); static tcAccountDatabase* Get(); ///< singleton instance accessor private: sqlite::connection* accountDatabase; std::map<std::string, unsigned int> columnLookup; // for faster access of data CMD5* md5; UserData cachedUserData; ///< all columns from last read are stored in this structure const std::string databasePath; const std::string tableName; void Initialize(); ///< open connection to database and initialize columnLookup void TestUpdate(); tcAccountDatabase(); ~tcAccountDatabase(); }; #endif |