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
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef ArmageTron_NKRAWALL_H
00035 #define ArmageTron_NKRAWALL_H
00036
00037
00038
00039
00040 #include "defs.h"
00041 #include "aa_config.h"
00042 #include "md5.h"
00043 #include "tSafePTR.h"
00044 #include "tString.h"
00045
00046 #include "tConfiguration.h"
00047
00048 #include <deque>
00049
00050 class nNetObject;
00051
00052 #ifndef _IOSFWD_
00053 #endif
00054
00055
00056
00057 class tString;
00058 class nMessage;
00059
00061 class nKrawall
00062 {
00063 public:
00064
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;
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
00110 static tString SupportedMethods();
00111
00112
00113 static tString BestMethod( tString const & a, tString const & b );
00114
00115
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
00136
00137 nMethod( char const * method_, std::istream & properties );
00138
00139 nMethod( char const * method_, char const * prefix_ = "", char const * suffix_ = "");
00140 };
00141
00142
00143 struct nPasswordRequest: public nMethod
00144 {
00145 tString message;
00146 bool failureOnLastTry;
00147
00148 nPasswordRequest()
00149 : failureOnLastTry ( false ){}
00150 };
00151
00152
00153 struct nPasswordAnswer
00154 {
00155 tString serverAddress;
00156 tString username;
00157 nScrambledPassword scrambled;
00158 bool aborted;
00159 bool automatic;
00160
00161 nPasswordAnswer()
00162 : aborted( false ), automatic( false ){}
00163 };
00164
00165 struct nPasswordCheckData
00166 {
00167 tString fullAuthority;
00168 nMethod method;
00169 nSalt salt;
00170 nScrambledPassword hash;
00171 tString serverAddress;
00172 };
00173
00174
00175 struct nCheckResultBase
00176 {
00177 tString username;
00178 tString authority;
00179 bool success;
00180 tString error;
00181 std::deque< tString > blurb;
00182
00183 tAccessLevel accessLevel;
00184
00185 nCheckResultBase()
00186 : success( false ), accessLevel( tAccessLevel_Authenticated ){}
00187 };
00188
00189 struct nCheckResult: public nCheckResultBase
00190 {
00191 tJUST_CONTROLLED_PTR< nNetObject > user;
00192 bool aborted;
00193 bool automatic;
00194
00195 nCheckResult();
00196 ~nCheckResult();
00197 nCheckResult( nCheckResult const & other );
00198 };
00199
00200
00201 static tString EncodeScrambledPassword( nScrambledPassword const & scrambled );
00202
00203
00204 static tString EncodeString( tString const & original );
00205
00206
00207 static void WriteScrambledPassword(const nScrambledPassword& scrambled,
00208 nMessage &m);
00209
00210 static void ReadScrambledPassword( nMessage &m,
00211 nScrambledPassword& scrambled);
00212
00213
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
00234 static bool ArePasswordsEqual(const nScrambledPassword& a,
00235 const nScrambledPassword& b);
00236
00237
00238
00239 static void ScramblePassword(const tString& password,
00240 nScrambledPassword &scrambled);
00241
00242
00243 static void BrokenScramblePassword(const tString& password,
00244 nScrambledPassword &scrambled);
00245
00246
00247 static void ScrambleWithSalt2(const nScrambledPassword& source,
00248 const nSalt& salt,
00249 nScrambledPassword& dest);
00250
00251
00252
00253 static void RandomSalt(nSalt& salt);
00254 #ifdef KRAWALL_SERVER
00256 static void SplitUserName( tString const & original, tString & username, tString & authority );
00257
00258
00259 static void CheckScrambledPassword( nCheckResultBase & result,
00260 nPasswordCheckData const & data );
00261
00262
00263 static int FetchURL( tString const & authority, char const * query, std::ostream & target, int maxlen = 10000 );
00264
00265 #ifdef KRAWALL_SERVER_LEAGUE
00266
00267 static const nScrambledPassword& SecretLeagueKey();
00268
00269
00270 static void SendLeagueMessage(const tString& message = *reinterpret_cast<tString *>(0));
00271
00272
00273 static void ReceiveLeagueMessage(const tString& message);
00274
00275
00276
00277
00278
00279 static void Frag(const tString &killer, const tString& victim, tString& message);
00280
00281
00282
00283 static void RoundEnd(const tString* players, int numPlayers, tString& message);
00284
00285
00286 static void ServerFrag(const tString &killer, const tString& victim);
00287
00288
00289
00290 static void ServerRoundEnd(const tString* players, int numPlayers);
00291
00292
00293
00294 static void MasterFrag(const tString &killer, const tString& victim);
00295
00296
00297
00298 static void MasterRoundEnd(const tString* players, int numPlayers);
00299
00300
00301
00302
00303 static bool IsFromKrawall(tString& adress, unsigned int port);
00304
00305
00306
00307 static bool RequireMasterLogin(tString& adress, unsigned int port);
00308
00309
00310
00311 static bool MayRequirePassword(tString& adress, unsigned int port);
00312 #endif
00313 #endif
00314 };
00315
00316 #endif