PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/cacti-0.8.8a/auth_login.php

#
PHP | 364 lines | 268 code | 29 blank | 67 comment | 65 complexity | a5402dd38e74238e3287710e95a2a759 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. +-------------------------------------------------------------------------+
  4. | Copyright (C) 2004-2012 The Cacti Group |
  5. | |
  6. | This program is free software; you can redistribute it and/or |
  7. | modify it under the terms of the GNU General Public License |
  8. | as published by the Free Software Foundation; either version 2 |
  9. | of the License, or (at your option) any later version. |
  10. | |
  11. | This program is distributed in the hope that it will be useful, |
  12. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  14. | GNU General Public License for more details. |
  15. +-------------------------------------------------------------------------+
  16. | Cacti: The Complete RRDTool-based Graphing Solution |
  17. +-------------------------------------------------------------------------+
  18. | This code is designed, written, and maintained by the Cacti Group. See |
  19. | about.php and/or the AUTHORS file for specific developer information. |
  20. +-------------------------------------------------------------------------+
  21. | http://www.cacti.net/ |
  22. +-------------------------------------------------------------------------+
  23. */
  24. /* set default action */
  25. if (isset($_REQUEST["action"])) {
  26. $action = $_REQUEST["action"];
  27. }else{
  28. $action = "";
  29. }
  30. /* Get the username */
  31. if (read_config_option("auth_method") == "2") {
  32. /* Get the Web Basic Auth username and set action so we login right away */
  33. $action = "login";
  34. if (isset($_SERVER["PHP_AUTH_USER"])) {
  35. $username = str_replace("\\", "\\\\", $_SERVER["PHP_AUTH_USER"]);
  36. }elseif (isset($_SERVER["REMOTE_USER"])) {
  37. $username = str_replace("\\", "\\\\", $_SERVER["REMOTE_USER"]);
  38. }elseif (isset($_SERVER["REDIRECT_REMOTE_USER"])) {
  39. $username = str_replace("\\", "\\\\", $_SERVER["REDIRECT_REMOTE_USER"]);
  40. }elseif (isset($_SERVER["HTTP_PHP_AUTH_USER"])) {
  41. $username = str_replace("\\", "\\\\", $_SERVER["HTTP_PHP_AUTH_USER"]);
  42. }elseif (isset($_SERVER["HTTP_REMOTE_USER"])) {
  43. $username = str_replace("\\", "\\\\", $_SERVER["HTTP_REMOTE_USER"]);
  44. }elseif (isset($_SERVER["HTTP_REDIRECT_REMOTE_USER"])) {
  45. $username = str_replace("\\", "\\\\", $_SERVER["HTTP_REDIRECT_REMOTE_USER"]);
  46. }else{
  47. /* No user - Bad juju! */
  48. $username = "";
  49. cacti_log("ERROR: No username passed with Web Basic Authentication enabled.", false, "AUTH");
  50. auth_display_custom_error_message("Web Basic Authentication configured, but no username was passed from the web server. Please make sure you have authentication enabled on the web server.");
  51. exit;
  52. }
  53. }else{
  54. if ($action == "login") {
  55. /* LDAP and Builtin get username from Form */
  56. $username = get_request_var_post("login_username");
  57. }else{
  58. $username = "";
  59. }
  60. }
  61. $username = sanitize_search_string($username);
  62. /* process login */
  63. $copy_user = false;
  64. $user_auth = false;
  65. $user_enabled = 1;
  66. $ldap_error = false;
  67. $ldap_error_message = "";
  68. $realm = 0;
  69. if ($action == 'login') {
  70. switch (read_config_option("auth_method")) {
  71. case "0":
  72. /* No auth, no action, also shouldn't get here */
  73. exit;
  74. break;
  75. case "2":
  76. /* Web Basic Auth */
  77. $copy_user = true;
  78. $user_auth = true;
  79. $realm = 2;
  80. /* Locate user in database */
  81. $user = db_fetch_row("SELECT * FROM user_auth WHERE username = " . $cnn_id->qstr($username) . " AND realm = 2");
  82. break;
  83. case "3":
  84. /* LDAP Auth */
  85. if ((get_request_var_post("realm") == "ldap") && (strlen(get_request_var_post("login_password")) > 0)) {
  86. /* include LDAP lib */
  87. include_once("./lib/ldap.php");
  88. /* get user DN */
  89. $ldap_dn_search_response = cacti_ldap_search_dn($username);
  90. if ($ldap_dn_search_response["error_num"] == "0") {
  91. $ldap_dn = $ldap_dn_search_response["dn"];
  92. }else{
  93. /* Error searching */
  94. cacti_log("LOGIN: LDAP Error: " . $ldap_dn_search_response["error_text"], false, "AUTH");
  95. $ldap_error = true;
  96. $ldap_error_message = "LDAP Search Error: " . $ldap_dn_search_response["error_text"];
  97. $user_auth = false;
  98. $user = array();
  99. }
  100. if (!$ldap_error) {
  101. /* auth user with LDAP */
  102. $ldap_auth_response = cacti_ldap_auth($username,stripslashes(get_request_var_post("login_password")),$ldap_dn);
  103. if ($ldap_auth_response["error_num"] == "0") {
  104. /* User ok */
  105. $user_auth = true;
  106. $copy_user = true;
  107. $realm = 1;
  108. /* Locate user in database */
  109. cacti_log("LOGIN: LDAP User '" . $username . "' Authenticated", false, "AUTH");
  110. $user = db_fetch_row("SELECT * FROM user_auth WHERE username = " . $cnn_id->qstr($username) . " AND realm = 1");
  111. }else{
  112. /* error */
  113. cacti_log("LOGIN: LDAP Error: " . $ldap_auth_response["error_text"], false, "AUTH");
  114. $ldap_error = true;
  115. $ldap_error_message = "LDAP Error: " . $ldap_auth_response["error_text"];
  116. $user_auth = false;
  117. $user = array();
  118. }
  119. }
  120. }
  121. default:
  122. if (!api_plugin_hook_function('login_process', false)) {
  123. /* Builtin Auth */
  124. if ((!$user_auth) && (!$ldap_error)) {
  125. /* if auth has not occured process for builtin - AKA Ldap fall through */
  126. $user = db_fetch_row("SELECT * FROM user_auth WHERE username = " . $cnn_id->qstr($username) . " AND password = '" . md5(get_request_var_post("login_password")) . "' AND realm = 0");
  127. }
  128. }
  129. }
  130. /* end of switch */
  131. /* Create user from template if requested */
  132. if ((!sizeof($user)) && ($copy_user) && (read_config_option("user_template") != "0") && (strlen($username) > 0)) {
  133. cacti_log("WARN: User '" . $username . "' does not exist, copying template user", false, "AUTH");
  134. /* check that template user exists */
  135. if (db_fetch_row("SELECT id FROM user_auth WHERE username = '" . read_config_option("user_template") . "' AND realm = 0")) {
  136. /* template user found */
  137. user_copy(read_config_option("user_template"), $username, 0, $realm);
  138. /* requery newly created user */
  139. $user = db_fetch_row("SELECT * FROM user_auth WHERE username = " . $cnn_id->qstr($username) . " AND realm = " . $realm);
  140. }else{
  141. /* error */
  142. cacti_log("LOGIN: Template user '" . read_config_option("user_template") . "' does not exist.", false, "AUTH");
  143. auth_display_custom_error_message("Template user '" . read_config_option("user_template") . "' does not exist.");
  144. exit;
  145. }
  146. }
  147. /* Guest account checking - Not for builtin */
  148. $guest_user = false;
  149. if ((sizeof($user) < 1) && ($user_auth) && (read_config_option("guest_user") != "0")) {
  150. /* Locate guest user record */
  151. $user = db_fetch_row("SELECT * FROM user_auth WHERE username = '" . read_config_option("guest_user") . "'");
  152. if ($user) {
  153. cacti_log("LOGIN: Authenicated user '" . $username . "' using guest account '" . $user["username"] . "'", false, "AUTH");
  154. $guest_user = true;
  155. }else{
  156. /* error */
  157. auth_display_custom_error_message("Guest user \"" . read_config_option("guest_user") . "\" does not exist.");
  158. cacti_log("LOGIN: Unable to locate guest user '" . read_config_option("guest_user") . "'", false, "AUTH");
  159. exit;
  160. }
  161. }
  162. /* Process the user */
  163. if (sizeof($user) > 0) {
  164. cacti_log("LOGIN: User '" . $user["username"] . "' Authenticated", false, "AUTH");
  165. db_execute("INSERT INTO user_log (username,user_id,result,ip,time) VALUES (" . $cnn_id->qstr($username) . "," . $user["id"] . ",1,'" . $_SERVER["REMOTE_ADDR"] . "',NOW())");
  166. /* is user enabled */
  167. $user_enabled = $user["enabled"];
  168. if ($user_enabled != "on") {
  169. /* Display error */
  170. auth_display_custom_error_message("Access Denied, user account disabled.");
  171. exit;
  172. }
  173. /* set the php session */
  174. $_SESSION["sess_user_id"] = $user["id"];
  175. /* handle "force change password" */
  176. if (($user["must_change_password"] == "on") && (read_config_option("auth_method") == 1)) {
  177. $_SESSION["sess_change_password"] = true;
  178. }
  179. /* ok, at the point the user has been sucessfully authenticated; so we must
  180. decide what to do next */
  181. switch ($user["login_opts"]) {
  182. case '1': /* referer */
  183. /* because we use plugins, we can't redirect back to graph_view.php if they don't
  184. * have console access
  185. */
  186. if (isset($_SERVER["HTTP_REFERER"])) {
  187. $referer = $_SERVER["HTTP_REFERER"];
  188. if (basename($referer) == "logout.php") {
  189. $referer = $config['url_path'] . "index.php";
  190. }
  191. } else if (isset($_SERVER["REQUEST_URI"])) {
  192. $referer = $_SERVER["REQUEST_URI"];
  193. if (basename($referer) == "logout.php") {
  194. $referer = $config['url_path'] . "index.php";
  195. }
  196. } else {
  197. $referer = $config['url_path'] . "index.php";
  198. }
  199. if (substr_count($referer, "plugins")) {
  200. header("Location: " . $referer);
  201. } elseif (sizeof(db_fetch_assoc("SELECT realm_id FROM user_auth_realm WHERE realm_id = 8 AND user_id = " . $_SESSION["sess_user_id"])) == 0) {
  202. header("Location: graph_view.php");
  203. } else {
  204. header("Location: $referer");
  205. }
  206. break;
  207. case '2': /* default console page */
  208. header("Location: " . $config['url_path'] . "index.php");
  209. break;
  210. case '3': /* default graph page */
  211. header("Location: " . $config['url_path'] . "graph_view.php");
  212. break;
  213. default:
  214. api_plugin_hook_function('login_options_navigate', $user['login_opts']);
  215. }
  216. exit;
  217. }else{
  218. if ((!$guest_user) && ($user_auth)) {
  219. /* No guest account defined */
  220. auth_display_custom_error_message("Access Denied, please contact you Cacti Administrator.");
  221. cacti_log("LOGIN: Access Denied, No guest enabled or template user to copy", false, "AUTH");
  222. exit;
  223. }else{
  224. /* BAD username/password builtin and LDAP */
  225. db_execute("INSERT INTO user_log (username,user_id,result,ip,time) VALUES (" . $cnn_id->qstr($username) . ",0,0,'" . $_SERVER["REMOTE_ADDR"] . "',NOW())");
  226. }
  227. }
  228. }
  229. /* auth_display_custom_error_message - displays a custom error message to the browser that looks like
  230. the pre-defined error messages
  231. @arg $message - the actual text of the error message to display */
  232. function auth_display_custom_error_message($message) {
  233. /* kill the session */
  234. setcookie(session_name(),"",time() - 3600,"/");
  235. /* print error */
  236. print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
  237. print "<html>\n<head>\n";
  238. print " <title>" . "Cacti" . "</title>\n";
  239. print " <meta http-equiv='Content-Type' content='text/html;charset=utf-8'>";
  240. print " <link href=\"include/main.css\" type=\"text/css\" rel=\"stylesheet\">";
  241. print "</head>\n";
  242. print "<body>\n<br><br>\n";
  243. display_custom_error_message($message);
  244. print "</body>\n</html>\n";
  245. }
  246. if (api_plugin_hook_function('custom_login', OPER_MODE_NATIVE) == OPER_MODE_RESKIN) {
  247. return;
  248. }
  249. ?>
  250. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  251. <html>
  252. <head>
  253. <title><?php print api_plugin_hook_function("login_title", "Login to Cacti");?></title>
  254. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  255. <STYLE TYPE="text/css">
  256. <!--
  257. BODY, TABLE, TR, TD {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;}
  258. A {text-decoration: none;}
  259. A:active { text-decoration: none;}
  260. A:hover {text-decoration: underline; color: #333333;}
  261. A:visited {color: Blue;}
  262. -->
  263. </style>
  264. </head>
  265. <body bgcolor="#FFFFFF" onload="document.login.login_username.focus()">
  266. <form name="login" method="post" action="<?php print basename($_SERVER["PHP_SELF"]);?>">
  267. <input type="hidden" name="action" value="login">
  268. <?php
  269. api_plugin_hook_function("login_before", array('ldap_error' => $ldap_error, 'ldap_error_message' => $ldap_error_message, 'username' => $username, 'user_enabled' => $user_enabled, 'action' => $action));
  270. $cacti_logo = $config['url_path'] . 'images/auth_login.gif';
  271. $cacti_logo = api_plugin_hook_function('cacti_image', $cacti_logo);
  272. ?>
  273. <table id="login" align="center">
  274. <tr>
  275. <td colspan="2"><center><?php if ($cacti_logo != '') { ?><img src="<?php echo $cacti_logo; ?>" border="0" alt=""><?php } ?></center></td>
  276. </tr>
  277. <?php
  278. if ($ldap_error) {?>
  279. <tr style="height:10px;"><td></td></tr>
  280. <tr>
  281. <td id="error" colspan="2"><font color="#FF0000"><strong><?php print $ldap_error_message; ?></strong></font></td>
  282. </tr>
  283. <?php }else{
  284. if ($action == "login") {?>
  285. <tr style="height:10px;"><td></td></tr>
  286. <tr>
  287. <td id="error" colspan="2"><font color="#FF0000"><strong>Invalid User Name/Password Please Retype</strong></font></td>
  288. </tr>
  289. <?php }
  290. if ($user_enabled == "0") {?>
  291. <tr style="height:10px;"><td></td></tr>
  292. <tr>
  293. <td id="error" colspan="2"><font color="#FF0000"><strong>User Account Disabled</strong></font></td>
  294. </tr>
  295. <?php } } ?>
  296. <tr style="height:10px;"><td></td></tr>
  297. <tr id="login_row">
  298. <td colspan="2">Please enter your Cacti user name and password below:</td>
  299. </tr>
  300. <tr style="height:10px;"><td></td></tr>
  301. <tr id="user_row">
  302. <td>User Name:</td>
  303. <td><input type="text" name="login_username" size="40" style="width: 295px;" value="<?php print htmlspecialchars($username); ?>"></td>
  304. </tr>
  305. <tr id="password_row">
  306. <td>Password:</td>
  307. <td><input type="password" name="login_password" size="40" style="width: 295px;"></td>
  308. </tr>
  309. <?php
  310. if (read_config_option("auth_method") == "3" || api_plugin_hook_function('login_realms_exist')) {
  311. $realms = api_plugin_hook_function('login_realms', array("local" => array("name" => "Local", "selected" => false), "ldap" => array("name" => "LDAP", "selected" => true)));
  312. ?>
  313. <tr id="realm_row">
  314. <td>Realm:</td>
  315. <td>
  316. <select name="realm" style="width: 295px;"><?php
  317. if (sizeof($realms)) {
  318. foreach($realms as $name => $realm) {
  319. print "\t\t\t\t\t<option value='" . $name . "'" . ($realm["selected"] ? " selected":"") . ">" . htmlspecialchars($realm["name"]) . "</option>\n";
  320. }
  321. }
  322. ?>
  323. </select>
  324. </td>
  325. </tr>
  326. <?php }?>
  327. <tr style="height:10px;"><td></td></tr>
  328. <tr>
  329. <td><input type="submit" value="Login"></td>
  330. </tr>
  331. </table>
  332. <?php api_plugin_hook('login_after'); ?>
  333. </form>
  334. </body>
  335. </html>