src/network/nKrawall.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 Although it is already included in the GPL, let me clarify:
00027 anybody, especially the Krawall Gaming network, is allowed
00028 to run a server/master server build from modified versions of
00029 this file WHITHOUT releasing the source to the public (provided
00030 the executable is not distributed).
00031 
00032 */
00033 
00034 #ifndef ArmageTron_NKRAWALL_H
00035 #define ArmageTron_NKRAWALL_H
00036 
00037 //#include <iosfwd>
00038 //#include <iostream>
00039 
00040 #include "defs.h"
00041 #include "aa_config.h"
00042 #include "md5.h"
00043 #include "tSafePTR.h"
00044 #include "tString.h"
00045 // #include "nNetObject.h"
00046 #include "tConfiguration.h"
00047 
00048 #include <deque>
00049 
00050 class nNetObject;
00051 
00052 #ifndef _IOSFWD_
00053 #endif
00054 
00055 //#error x
00056 
00057 class tString;
00058 class nMessage;
00059 
00061 class nKrawall
00062 {
00063 public:
00064     // the scrambled password data types
00065     class nScrambledPassword
00066     {
00067         friend class nKrawall;
00068 
00069         md5_byte_t content[16];
00070     public:
00071         md5_byte_t operator[]( int i ) const
00072         {
00073             tASSERT( i >= 0 && i < 16 );
00074             return content[i];
00075         }
00076 
00077         md5_byte_t & operator[]( int i )
00078         {
00079             tASSERT( i >= 0 && i < 16 );
00080             return content[i];
00081         }
00082 
00083         void Clear()
00084         {
00085             memset( &content, 0, sizeof(content));
00086         }
00087     };
00088 
00089     typedef nScrambledPassword nSalt;          // (freely changable)
00090 
00092     struct nScrambleInfo
00093     {
00094         tString username;           
00095 
00096         nScrambleInfo( tString const & username_ )
00097         : username( username_)
00098         {
00099         }
00100     };
00101 
00103     struct nMethod
00104     {
00105         tString method;          
00106         tString prefix;          
00107         tString suffix;          
00108 
00109         // returns a comma separated list of supported methods
00110         static tString SupportedMethods();
00111 
00112         // from two strings of supported-method-lists, select the best one
00113         static tString BestMethod( tString const & a, tString const & b );
00114 
00115         // scramble a password using this
00116         void ScramblePassword( nScrambleInfo const & info, tString const & password, nScrambledPassword & scrambled ) const;
00117 
00119         void ScrambleSalt( nSalt & salt, tString const & serverIP ) const;
00120 
00122         void ScrambleWithSalt( nScrambleInfo const & info, nScrambledPassword const & scrambled, nSalt const & salt, nScrambledPassword & result ) const;
00123 
00125         static nMethod const * const * LocalMethods();
00126 
00128         static bool BestLocalMethod( tString const & supportedOnClient, nMethod & result );
00129 
00131         static bool Equal( nMethod const & a, nMethod const & b );
00132 
00133         nMethod(){}
00134 
00135         // construct a method from the type and a stream with properties
00136         // the stream is supposed to consist of lines of the "property_name property_value" form.
00137         nMethod( char const * method_, std::istream & properties );
00138 
00139         nMethod( char const * method_, char const * prefix_ = "", char const * suffix_ = "");
00140     };
00141 
00142     // structure for a password request to the user
00143     struct nPasswordRequest: public nMethod
00144     {   
00145         tString message;         // message to show to the user
00146         bool failureOnLastTry;   // did the last attempt fail?
00147 
00148         nPasswordRequest()
00149             : failureOnLastTry ( false ){}
00150     };
00151 
00152     // structure for read-write data for password requests
00153     struct nPasswordAnswer
00154     {
00155         tString serverAddress;         // the address of the server
00156         tString username;              // username, read-write property
00157         nScrambledPassword scrambled;  // the scrambled password
00158         bool aborted;                  // did the user abort the operation?
00159         bool automatic;                // was the answer provided automatically without user interaction?
00160 
00161         nPasswordAnswer()
00162             : aborted( false ), automatic( false ){}
00163     };
00164 
00165     struct nPasswordCheckData
00166     {
00167         tString fullAuthority;          // authority (no shorthand version); authenticated name is username@authority
00168         nMethod method;                 
00169         nSalt salt;                     
00170         nScrambledPassword hash;        
00171         tString serverAddress;          
00172     };
00173 
00174     // return structure for an authentication request
00175     struct nCheckResultBase
00176     {   
00177         tString username;        // username as sent from client
00178         tString authority;       // authority (shorthand version allowed); authenticated name is username@authority
00179         bool success;            // was the operation successful?
00180         tString error;           // potential error message
00181         std::deque< tString > blurb; // additional blurb data the authority may give on successful login
00182 
00183         tAccessLevel accessLevel;// access level of user
00184 
00185         nCheckResultBase()
00186             : success( false ), accessLevel( tAccessLevel_Authenticated ){}
00187     };
00188 
00189     struct nCheckResult: public nCheckResultBase
00190     {   
00191         tJUST_CONTROLLED_PTR< nNetObject > user; // net object identifying the user (will be ePlayerNetID, but we don't know about that here)
00192         bool aborted;            // did the user abort the operation?
00193         bool automatic;          // was the answer provided automatically without user interaction?
00194 
00195         nCheckResult();
00196         ~nCheckResult();
00197         nCheckResult( nCheckResult const & other );
00198     };
00199 
00200     // encode scrambled passwords and salts as hexcode strings
00201     static tString EncodeScrambledPassword( nScrambledPassword const & scrambled );
00202 
00203     // encode a string for safe inclusion into an URL
00204     static tString EncodeString( tString const & original );
00205 
00206     // network read/write operations of these data types
00207     static void WriteScrambledPassword(const nScrambledPassword& scrambled,
00208                                        nMessage &m);
00209 
00210     static void ReadScrambledPassword( nMessage &m,
00211                                        nScrambledPassword& scrambled);
00212 
00213     // file read/write operations of these data types
00214     static void WriteScrambledPassword(const nScrambledPassword& scrambled,
00215                                        std::ostream &s);
00216 
00217     static void ReadScrambledPassword( std::istream &s,
00218                                        nScrambledPassword& scrambled);
00219 
00220 
00221     static void WriteSalt(const nSalt& salt,
00222                           nMessage &m)
00223     {
00224         WriteScrambledPassword(salt, m);
00225     }
00226 
00227     static void ReadSalt( nMessage &m,
00228                           nSalt& salt)
00229     {
00230         ReadScrambledPassword(m, salt);
00231     }
00232 
00233     // compare two passwords
00234     static bool ArePasswordsEqual(const nScrambledPassword& a,
00235                                   const nScrambledPassword& b);
00236 
00237 
00238     // scramble a password locally (so it does not have to be stored on disk)
00239     static void ScramblePassword(const tString& password,
00240                                  nScrambledPassword &scrambled);
00241 
00242     // scramble a password locally (so it does not have to be stored on disk), old broken method that includes the trailing \0.
00243     static void BrokenScramblePassword(const tString& password,
00244                                        nScrambledPassword &scrambled);
00245 
00246     // scramble it again before transfering it over the network
00247     static void ScrambleWithSalt2(const nScrambledPassword& source,
00248                                  const nSalt& salt,
00249                                  nScrambledPassword& dest);
00250 
00251 
00252     // get a random salt value
00253     static void RandomSalt(nSalt& salt);
00254 #ifdef KRAWALL_SERVER
00256     static void SplitUserName( tString const & original, tString & username, tString & authority );
00257 
00258     // check whether username's password, when run through ScrambleWithSalt( ScramblePassword(password), salt ), equals scrambledRemote. result.userName and result.authority need to be set by the caller, success and error are filled by this function, and authority may be modified.
00259     static void CheckScrambledPassword( nCheckResultBase & result,
00260                                         nPasswordCheckData const & data );
00261 
00262     // fetches an URL content, return http return code (-1 if total failure), fill result stream.
00263     static int FetchURL( tString const & authority, char const * query, std::ostream & target, int maxlen = 10000 );
00264 
00265 #ifdef KRAWALL_SERVER_LEAGUE
00266     // secret key to encrypt server->master server league transfer
00267     static const nScrambledPassword& SecretLeagueKey();
00268 
00269     // called on the servers to create a league message
00270     static void SendLeagueMessage(const tString& message = *reinterpret_cast<tString *>(0));
00271 
00272     // called on the master server when the league message is received
00273     static void ReceiveLeagueMessage(const tString& message);
00274 
00275     // league message generation functions: fill message with the message to be
00276     // sent
00277 
00278     // called when victim drives against killer's wall
00279     static void Frag(const tString &killer, const tString& victim, tString& message);
00280 
00281     // called at the end of a round; the last survivor is stored in
00282     // players[numPlayers-1], the first death in players[0]
00283     static void RoundEnd(const tString* players, int numPlayers, tString& message);
00284 
00285     // called ON THE SERVER when victim drives against killer's wall
00286     static void ServerFrag(const tString &killer, const tString& victim);
00287 
00288     // called ON THE SERVER at the end of a round; the last survivor is stored in
00289     // players[numPlayers-1], the first death in players[0]
00290     static void ServerRoundEnd(const tString* players, int numPlayers);
00291 
00292 
00293     // called ON THE MASTER when victim drives against killer's wall
00294     static void MasterFrag(const tString &killer, const tString& victim);
00295 
00296     // called ON THE MASTER at the end of a round; the last survivor is stored in
00297     // players[numPlayers-1], the first death in players[0]
00298     static void MasterRoundEnd(const tString* players, int numPlayers);
00299 
00300     // Adress checking functions
00301 
00302     // first validity check for the league messages
00303     static bool IsFromKrawall(tString& adress, unsigned int port);
00304 
00305     // check if a user is from germany (so the master server will require
00306     // a password check)
00307     static bool RequireMasterLogin(tString& adress, unsigned int port);
00308 
00309     // only servers acknowledged by this funktion are from Krawall and
00310     // are allowed to request logins
00311     static bool MayRequirePassword(tString& adress, unsigned int port);
00312 #endif
00313 #endif
00314 };
00315 
00316 #endif

Generated on Sat Mar 15 22:55:49 2008 for Armagetron Advanced by  doxygen 1.5.4