| 1 |
<?php |
| 2 |
|
| 3 |
//////////////////////////////////////////////////// |
| 4 |
// |
| 5 |
// Authentication server, reference implementation 0.1 |
| 6 |
// |
| 7 |
// To run authority X, this script needs to be |
| 8 |
// adapted and be callable from a web browser as |
| 9 |
// http://X/armaauth/0.1 |
| 10 |
// |
| 11 |
// the easiest way to do so is to name the script index.php |
| 12 |
// and place it into an appropriate armaauth/0.1 |
| 13 |
// directory on a web server that supports php. |
| 14 |
// |
| 15 |
//////////////////////////////////////////////////// |
| 16 |
|
| 17 |
// however, before they are used, %u is replaced by the username. |
| 18 |
|
| 19 |
function substitutions( $fix, $user ) |
| 20 |
{ |
| 21 |
return str_replace( "%u", $user, $fix ); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
// this function reports the result of the check back to the |
| 26 |
// qerying AA server. |
| 27 |
|
| 28 |
function conclude($statusCode, $msg) |
| 29 |
{ |
| 30 |
// if you do want to abuse return error codes (the servers |
| 31 |
// work fine with parsing the text response), uncomment |
| 32 |
// the next line. The request is technically always successful. |
| 33 |
$statusCode = 200; |
| 34 |
|
| 35 |
// the status codes passed in have been chosen somewhat |
| 36 |
// meaningfully (200: ok, 404: something not found, |
| 37 |
// 401: password failure) but still violate the standard. |
| 38 |
|
| 39 |
// report error code in header |
| 40 |
header("Status: $statusCode", true, $statusCode); |
| 41 |
header("Content-Type: text/plain"); |
| 42 |
|
| 43 |
// print message |
| 44 |
die("$msg\n"); |
| 45 |
} |
| 46 |
|
| 47 |
// read authority from gloval variables |
| 48 |
$authority = $_SERVER['HTTP_HOST']; |
| 49 |
|
| 50 |
//////////////////////////////////////////////////// |
| 51 |
// Bits you need to change follow. |
| 52 |
//////////////////////////////////////////////////// |
| 53 |
|
| 54 |
// You should definitely check that the hostname the game |
| 55 |
// server used to contact you is the one you intend it |
| 56 |
// to use; otherwise, there may be problems with web |
| 57 |
// servers known under different names. You should uncomment |
| 58 |
// this and add your real authority hostname. |
| 59 |
|
| 60 |
/* |
| 61 |
if ( $authority != "authority" ) |
| 62 |
conclude( 404, "WRONG_HOST" ); |
| 63 |
*/ |
| 64 |
|
| 65 |
// The user "database": fetch the password for a user. |
| 66 |
// If you are fine with storing plaintext passwords, |
| 67 |
// adapt this function; otherwise, adapt getPasswordHash(). |
| 68 |
|
| 69 |
function getPassword( $user ) |
| 70 |
{ |
| 71 |
// the user/password database. For very small groups |
| 72 |
// of users, you may just get away with expanding this |
| 73 |
// array. |
| 74 |
$passwords= array ( |
| 75 |
'test' => 'password', // clever choice there, test |
| 76 |
'z-män' => 'passwörd' // test for utf8 usernames and passwords |
| 77 |
); |
| 78 |
|
| 79 |
$password = $passwords[ $user ]; |
| 80 |
if ( NULL == $password ) |
| 81 |
return NULL; |
| 82 |
|
| 83 |
// return a pair of username and password. |
| 84 |
// it is important that you return the username |
| 85 |
// exactly as it appeared in the database. |
| 86 |
// If the username lookup is case insensitive, |
| 87 |
// the rest of the script and the game servers |
| 88 |
// need to know what the correct form of the name |
| 89 |
// is. |
| 90 |
return array( $user, $password ); |
| 91 |
} |
| 92 |
|
| 93 |
// these two functions return prefix and suffix for the md5 |
| 94 |
// hash method. They are prepended/appended to the password |
| 95 |
// before md5 is applied on it. Adapt them to the way your |
| 96 |
// md5 password hash is stored in your database. |
| 97 |
|
| 98 |
// IMPORTANT: if you want to keep the %u (a good idea for |
| 99 |
// security, prevents precomputation attacks on the passwords) |
| 100 |
// user name lookup needs to be case sensitive, or there will |
| 101 |
// be unexplainable password failures. |
| 102 |
|
| 103 |
function getPrefix() |
| 104 |
{ |
| 105 |
return "%u:aaauth:"; |
| 106 |
} |
| 107 |
|
| 108 |
function getSuffix() |
| 109 |
{ |
| 110 |
global $authority; |
| 111 |
return ":$authority"; |
| 112 |
} |
| 113 |
|
| 114 |
// You do not need plain text passwords. The |
| 115 |
// checks on the passwords are done to a hash function |
| 116 |
// thereof. You can just as well precompute the |
| 117 |
// work of this function and store the result, |
| 118 |
// and maybe your user database already contains |
| 119 |
// the one or other hash. phpBB, for example, |
| 120 |
// stores md5( $password ). |
| 121 |
|
| 122 |
function getPasswordHash( $user, $method ) |
| 123 |
{ |
| 124 |
// fetch the plaintext password |
| 125 |
$userInfo = getPassword( $user ); |
| 126 |
if ( NULL == $userInfo ) |
| 127 |
return NULL; |
| 128 |
|
| 129 |
// unpack the data |
| 130 |
$trueUser = $userInfo[0]; |
| 131 |
$password = $userInfo[1]; |
| 132 |
|
| 133 |
// check that neither prefix nor suffix conain %u if $trueUser != $user |
| 134 |
if ( $trueUser != $user && ( strpos( getPrefix(), '%u' ) !== FALSE || strpos( getPrefix(), '%u' ) !== FALSE ) ) |
| 135 |
{ |
| 136 |
conclude(404, 'UNKNOWN_USER ' . $user . ' Do not use %u in pre/suffix if your user database is making case-insensitive lookups.'); |
| 137 |
} |
| 138 |
|
| 139 |
// two methods are currently supported |
| 140 |
// by server and client, bmd5 (broken md5) |
| 141 |
// and md5. Both use the md5 hash algorithm. |
| 142 |
|
| 143 |
switch( $method ) |
| 144 |
{ |
| 145 |
case 'bmd5': |
| 146 |
// bmd5 quirk: the hash is computed for password + a trailing 0 |
| 147 |
$password = "$password" . chr(0); |
| 148 |
break; |
| 149 |
|
| 150 |
case "md5": |
| 151 |
// md5 adds the prefix and suffix before calculating |
| 152 |
// the hash. The example values are chosen so that the |
| 153 |
// resulting hash is the same one you find in .htdigest |
| 154 |
// files and that is used in the digest http authentication |
| 155 |
// method. If you set the prefix and suffix to empty |
| 156 |
// strings, the resulting hash will be the one found |
| 157 |
// in phpBB user databases. |
| 158 |
$password = substitutions( getPrefix(), $trueUser ) . "$password" . substitutions( getSuffix(), $trueUser ); |
| 159 |
break; |
| 160 |
} |
| 161 |
|
| 162 |
// after that, both methods just calculate the md5 hash |
| 163 |
// and return that. |
| 164 |
return array( $trueUser, md5( $password ) ); |
| 165 |
} |
| 166 |
|
| 167 |
// comma separated lists of methods you support. If, for example, |
| 168 |
// you cannot generate the hash for the bmd5 method, just |
| 169 |
// remove it from this list. |
| 170 |
|
| 171 |
function getMethods() |
| 172 |
{ |
| 173 |
return "md5, bmd5"; |
| 174 |
} |
| 175 |
|
| 176 |
//////////////////////////////////////////////////// |
| 177 |
// The rest of the script should not be changed, |
| 178 |
//////////////////////////////////////////////////// |
| 179 |
|
| 180 |
// Of course, the AA password query passes in some variables. |
| 181 |
// The first is the query type; it can either be |
| 182 |
// 'methods' for a list of supported methods, |
| 183 |
// 'params' for a list of method parameters, or |
| 184 |
// 'check' to check a user's identity. |
| 185 |
|
| 186 |
$query = @strtolower($_REQUEST['query'] . ''); |
| 187 |
|
| 188 |
switch ( $query ) |
| 189 |
{ |
| 190 |
case "methods": |
| 191 |
// give lists of supported methods. It's supposed to be "methods", |
| 192 |
// followed by a comma and/or whitespace separated list of the methods. |
| 193 |
conclude(200, 'METHODS '. getMethods() ); |
| 194 |
break; |
| 195 |
|
| 196 |
// the other two queries pass in more parameters, both have |
| 197 |
// the 'method' parameter that tells you which method should |
| 198 |
// be used. Well behaved servers will pick one of the methods |
| 199 |
// you support. |
| 200 |
|
| 201 |
case "params": |
| 202 |
$method = @strtolower($_REQUEST['method'] . ''); |
| 203 |
|
| 204 |
// give method parameter info |
| 205 |
if ( $method == 'md5' ) |
| 206 |
{ |
| 207 |
// give prefix and suffix |
| 208 |
conclude( 200, "PREFIX " . getPrefix() . "\nSUFFIX " . getSuffix() ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( $method == 'bmd5' ) |
| 212 |
{ |
| 213 |
// bmd5 does not support or need extra parameters. |
| 214 |
conclude(200 , '' ); |
| 215 |
} |
| 216 |
|
| 217 |
// we know nothing about this. |
| 218 |
conclude(404 , 'UNKNOWN_METHOD' ); |
| 219 |
break; |
| 220 |
|
| 221 |
case "check": |
| 222 |
$method = @strtolower($_REQUEST['method'] . ''); |
| 223 |
|
| 224 |
// the last query passes in the username to check, |
| 225 |
$user = @$_REQUEST['user'] . ''; |
| 226 |
|
| 227 |
// a salt value (128 bit hexcoded) |
| 228 |
$salt = @$_REQUEST['salt'] . ''; |
| 229 |
|
| 230 |
// and a hash value the AA client has computed. |
| 231 |
$hash = @$_REQUEST['hash'] . ''; |
| 232 |
|
| 233 |
// we're supposed to check whether that hash is correct; |
| 234 |
// to do so, we need to do the same operations to the |
| 235 |
// password the client has done. |
| 236 |
|
| 237 |
// first, the client computed a hash of the password |
| 238 |
// with method-specific rules. <TV cook mode> we have |
| 239 |
// already prepared that here. </TV cook mode>. |
| 240 |
$userInfo = getPasswordHash( $user, $method ); |
| 241 |
|
| 242 |
// check if user exists in the first place. |
| 243 |
if ( $userInfo == NULL ) |
| 244 |
{ |
| 245 |
conclude(404, 'UNKNOWN_USER ' . $user ); |
| 246 |
} |
| 247 |
|
| 248 |
// unpack user info |
| 249 |
$trueUser = $userInfo[0]; |
| 250 |
$correctPasswordHash = $userInfo[1]; |
| 251 |
|
| 252 |
// the operations the AA client did were not on the hex-encoded |
| 253 |
// hashes we have so far, but on binary packed variants thereof: |
| 254 |
$packedSalt = pack("H*", $salt); |
| 255 |
$correctPackedPasswordHash = pack('H*', $correctPasswordHash); |
| 256 |
|
| 257 |
// the client then simply calculated the MD5 sum of the two |
| 258 |
// concatenated packed values. |
| 259 |
$correctHash = md5($correctPackedPasswordHash . $packedSalt); |
| 260 |
|
| 261 |
// well, let's see if the client got it right! |
| 262 |
if (strcasecmp($hash, $correctHash) === 0) |
| 263 |
{ |
| 264 |
// he did! Return OK, followed by the user's full name. |
| 265 |
// You can also append "blurb" messages in subsequent lines. |
| 266 |
// Those messages will be transformed a little (the first word gets |
| 267 |
// AUTHORITY_BLURB_ prepended to it and the user's authenticated name |
| 268 |
// is added as a second token) and written to the server's ladder log. |
| 269 |
conclude(200, 'PASSWORD_OK ' . $trueUser . '@' . $authority . "\nFOO baz" ); |
| 270 |
|
| 271 |
// A useful blurb example would be a line containing: |
| 272 |
// ALIAS oldusername@oldauthority |
| 273 |
// It would get logged as |
| 274 |
// AUTHORITY_BLURB_ALIAS user@authority oldusername@oldauthority |
| 275 |
// and nice ladder log parsing scripts can choose to believe your |
| 276 |
// authority and assume user@authority and oldusername@oldauthority |
| 277 |
// are the same person. It is a matter between ladderlog parser writers |
| 278 |
// and authority implementers to define more useful standard blurb messages. |
| 279 |
} |
| 280 |
|
| 281 |
// he didn't. |
| 282 |
conclude(401, 'PASSWORD_FAIL' ); |
| 283 |
|
| 284 |
// That's it. Note that any response other that a text beginning |
| 285 |
// with "PASSWORD_OK" and a 200 return code will be interpreted |
| 286 |
// by the server as an error and authentication will fail. |
| 287 |
|
| 288 |
// String comparisons for the control words (METHODS, PASSWORD_OK) |
| 289 |
// are case-insensitive on the server. We just like to return them |
| 290 |
// as caps becasue that looks more like CONTOL_CODES. |
| 291 |
break; |
| 292 |
|
| 293 |
default: |
| 294 |
// we don't know what the server wants from us |
| 295 |
// if execution ends up here. |
| 296 |
conclude(404, 'UNKNOWN_QUERY'); |
| 297 |
} |
| 298 |
|
| 299 |
?> |