PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/www/login.php

http://scalr.googlecode.com/
PHP | 211 lines | 209 code | 2 blank | 0 comment | 0 complexity | 65a998a8dc443fcea15349e773539865 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0
  1. <?
  2. require("src/prepend.inc.php");
  3. CONTEXTS::$APPCONTEXT = APPCONTEXT::ORDER_WIZARD;
  4. $display['title'] = _("Self-Scaling Hosting Environment utilizing Amazon's EC2.");
  5. $display['meta_descr'] = _("Scalr is fully redundant, self-curing and self-scaling hosting environment utilizing Amazon's EC2. It is open source, allowing you to create server farms through a web-based interface using pre-built AMI's.");
  6. $display['meta_keywords'] = _("Amazon EC2, scalability, AWS, hosting, scaling, self-scaling, hosting environment, cloud computing, open source, web-based interface");
  7. if (isset($req_logout))
  8. {
  9. @session_destroy();
  10. setcookie("scalr_sault", "0", time()-86400);
  11. setcookie("scalr_hash", "0", time()-86400);
  12. setcookie("scalr_uid", "0", time()-86400);
  13. setcookie("scalr_signature", "0", time()-86400);
  14. $mess = _("Succesfully logged out");
  15. UI::Redirect("/login.php");
  16. }
  17. if ($req_action == "pwdrecovery")
  18. {
  19. if ($_POST)
  20. {
  21. $clientinfo = $db->GetRow("SELECT * FROM clients WHERE email=?", array($post_email));
  22. if ($clientinfo)
  23. {
  24. if ($clientinfo["isactive"] == 1)
  25. {
  26. $password = $Crypto->Sault(10);
  27. $db->Execute("UPDATE clients SET password=? WHERE id=?",
  28. array($Crypto->Hash($password), $clientinfo["id"])
  29. );
  30. $clientinfo["password"] = $password;
  31. // Send welcome E-mail
  32. $Mailer->ClearAddresses();
  33. $res = $Mailer->Send("emails/welcome.eml",
  34. array("client" => $clientinfo, "site_url" => "http://{$_SERVER['HTTP_HOST']}"),
  35. $clientinfo['email'],
  36. $clientinfo['fullname']
  37. );
  38. $display["okmsg"] = "Your password has been reset and emailed<br> to you";
  39. $_POST = false;
  40. $template_name = "login.tpl";
  41. }
  42. else
  43. $err[] = "Your account is not active yet";
  44. }
  45. else
  46. $err[] = "Specified e-mail not found in our database";
  47. }
  48. if (!$template_name)
  49. $template_name = "pwdrecovery.tpl";
  50. }
  51. if ($_POST || $req_isadmin == 1)
  52. {
  53. if (($post_login == CONFIG::$ADMIN_LOGIN) && ($Crypto->Hash($post_pass) == CONFIG::$ADMIN_PASSWORD))
  54. {
  55. if (CheckIPAcceess())
  56. {
  57. $sault = $Crypto->Sault();
  58. $_SESSION["sault"] = $sault;
  59. $_SESSION["hash"] = $Crypto->Hash("{$post_login}:".$Crypto->Hash($post_pass).":{$sault}");
  60. $_SESSION["uid"] = 0;
  61. $_SESSION["cpwd"] = $post_pass;
  62. $rpath = ($_SESSION["REQUEST_URI"]) ? $_SESSION["REQUEST_URI"] : "index.php";
  63. unset($_SESSION["REQUEST_URI"]);
  64. UI::Redirect("{$rpath}");
  65. }
  66. else
  67. $err[] = "Incorrect login or password";
  68. }
  69. else
  70. {
  71. if($req_isadmin && CheckIPAcceess())
  72. {
  73. $hash = $Crypto->Hash(CONFIG::$ADMIN_LOGIN.":".CONFIG::$ADMIN_PASSWORD.":".$_SESSION["sault"]);
  74. $valid_hash = ($newhash == $_SESSION["hash"] && !empty($_SESSION["hash"]));
  75. if ($hash == $valid_hash)
  76. {
  77. $user = $db->GetRow("SELECT * FROM clients WHERE id=?", array($req_id));
  78. $valid_admin = true;
  79. }
  80. else
  81. $err[] = "Your session expired. Please log in again";
  82. }
  83. else
  84. $user = $db->GetRow("SELECT * FROM clients WHERE email=?", array($post_login));
  85. if ($user)
  86. {
  87. if ($user["isactive"] == 0)
  88. $err[] = "Your account has been stopped by service administrator. Please <a href='mailto:".CONFIG::$EMAIL_ADDRESS."'>contact us</a> for more information.";
  89. else
  90. {
  91. $bruteforce = false;
  92. if ($user['login_attempts'] >= 3 && strtotime($user['dtlastloginattempt'])+600 > time())
  93. {
  94. $err[] = _("Bruteforce Protection!<br>You must wait 10 minutes before trying again.");
  95. $bruteforce = true;
  96. }
  97. elseif ($user['login_attempts'] >= 3)
  98. {
  99. $db->Execute("UPDATE clients SET login_attempts='0' WHERE id=?", array($user["id"]));
  100. }
  101. if (!$bruteforce)
  102. {
  103. if ($user["password"] == $Crypto->Hash($post_pass) || $valid_admin)
  104. {
  105. $sault = $Crypto->Sault();
  106. $_SESSION["sault"] = $sault;
  107. $_SESSION["hash"] = $Crypto->Hash("{$user['email']}:{$user["password"]}:{$sault}");
  108. $_SESSION["uid"] = $user["id"];
  109. $_SESSION["cpwd"] = $Crypto->Decrypt(@file_get_contents(dirname(__FILE__)."/../etc/.passwd"));
  110. $_SESSION["aws_accesskey"] = $Crypto->Decrypt($user["aws_accesskey"], $_SESSION["cpwd"]);
  111. $_SESSION["aws_accesskeyid"] = $Crypto->Decrypt($user["aws_accesskeyid"], $_SESSION["cpwd"]);
  112. $_SESSION["aws_accountid"] = $user["aws_accountid"];
  113. if ($user["aws_private_key_enc"])
  114. $_SESSION["aws_private_key"] = $Crypto->Decrypt($user["aws_private_key_enc"], $_SESSION["cpwd"]);
  115. if ($user["aws_certificate_enc"])
  116. $_SESSION["aws_certificate"] = $Crypto->Decrypt($user["aws_certificate_enc"], $_SESSION["cpwd"]);
  117. $rpath = ($_SESSION["REQUEST_URI"]) ? $_SESSION["REQUEST_URI"] : "index.php";
  118. unset($_SESSION["REQUEST_URI"]);
  119. $errmsg = false;
  120. $err = false;
  121. $db->Execute("UPDATE clients SET `login_attempts`=0, dtlastloginattempt=NOW() WHERE id=?", array($user["id"]));
  122. if ($post_keep_session)
  123. {
  124. setcookie("scalr_sault", $_SESSION["sault"], time()+86400*2);
  125. setcookie("scalr_hash", $_SESSION["hash"], time()+86400*2);
  126. setcookie("scalr_uid", $_SESSION["uid"], time()+86400*2);
  127. setcookie("scalr_signature", $Crypto->Hash("{$_SESSION["sault"]}:{$_SESSION["hash"]}:{$_SESSION["uid"]}:{$_SERVER['REMOTE_ADDR']}:{$_SESSION["cpwd"]}"), time()+43200);
  128. }
  129. UI::Redirect("{$rpath}");
  130. }
  131. else
  132. {
  133. $db->Execute("UPDATE clients SET `login_attempts`=`login_attempts` + 1, dtlastloginattempt=NOW() WHERE id=?", array($user["id"]));
  134. $err[] = _("Incorrect login or password");
  135. }
  136. }
  137. }
  138. }
  139. else
  140. $err[] = _("Incorrect login or password");
  141. }
  142. }
  143. function CheckIPAcceess()
  144. {
  145. global $db;
  146. $current_ip = $_SERVER["REMOTE_ADDR"];
  147. $current_ip_parts = explode(".", $current_ip);
  148. $ipaccesstable = $db->Execute("SELECT * FROM ipaccess");
  149. while ($row = $ipaccesstable->fetchRow())
  150. {
  151. $allowedhost = $row["ipaddress"];
  152. if (preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/si", $allowedhost))
  153. {
  154. if (ip2long($allowedhost) == ip2long($current_ip))
  155. return true;
  156. }
  157. elseif (stristr($allowedhost, "*"))
  158. {
  159. $ip_parts = explode(".", trim($allowedhost));
  160. if (
  161. ($ip_parts[0] == "*" || $ip_parts[0] == $current_ip_parts[0]) &&
  162. ($ip_parts[1] == "*" || $ip_parts[1] == $current_ip_parts[1]) &&
  163. ($ip_parts[2] == "*" || $ip_parts[2] == $current_ip_parts[2]) &&
  164. ($ip_parts[3] == "*" || $ip_parts[3] == $current_ip_parts[3])
  165. )
  166. return true;
  167. }
  168. else
  169. {
  170. $ip = @gethostbyname($allowedhost);
  171. if ($ip != $allowedhost)
  172. {
  173. if (ip2long($ip) == ip2long($current_ip))
  174. return true;
  175. }
  176. }
  177. }
  178. return false;
  179. }
  180. require("src/append.inc.php");
  181. ?>