RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
11 KiB

20 years ago
10 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
12 years ago
17 years ago
20 years ago
17 years ago
20 years ago
20 years ago
12 years ago
12 years ago
20 years ago
9 years ago
9 years ago
9 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
12 years ago
20 years ago
12 years ago
12 years ago
20 years ago
12 years ago
12 years ago
20 years ago
12 years ago
12 years ago
9 years ago
12 years ago
12 years ago
20 years ago
  1. <?php
  2. /**
  3. +-------------------------------------------------------------------------+
  4. | Roundcube Webmail IMAP Client |
  5. | Version 1.6.0 |
  6. | |
  7. | Copyright (C) The Roundcube Dev Team |
  8. | |
  9. | This program is free software: you can redistribute it and/or modify |
  10. | it under the terms of the GNU General Public License (with exceptions |
  11. | for skins & plugins) as published by the Free Software Foundation, |
  12. | either version 3 of the License, or (at your option) any later version. |
  13. | |
  14. | This file forms part of the Roundcube Webmail Software for which the |
  15. | following exception is added: Plugins and Skins which merely make |
  16. | function calls to the Roundcube Webmail Software, and for that purpose |
  17. | include it by reference shall not be considered modifications of |
  18. | the software. |
  19. | |
  20. | If you wish to use this file in another project or create a modified |
  21. | version that will not be part of the Roundcube Webmail Software, you |
  22. | may remove the exception above and use this source code under the |
  23. | original version of the license. |
  24. | |
  25. | This program is distributed in the hope that it will be useful, |
  26. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  27. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  28. | GNU General Public License for more details. |
  29. | |
  30. | You should have received a copy of the GNU General Public License |
  31. | along with this program. If not, see http://www.gnu.org/licenses/. |
  32. | |
  33. +-------------------------------------------------------------------------+
  34. | Author: Thomas Bruederli <roundcube@gmail.com> |
  35. | Author: Aleksander Machniak <alec@alec.pl> |
  36. +-------------------------------------------------------------------------+
  37. */
  38. // include environment
  39. require_once 'program/include/iniset.php';
  40. // init application, start session, init output class, etc.
  41. $RCMAIL = rcmail::get_instance(0, isset($GLOBALS['env']) ? $GLOBALS['env'] : null);
  42. // Make the whole PHP output non-cacheable (#1487797)
  43. $RCMAIL->output->nocacheing_headers();
  44. $RCMAIL->output->common_headers(!empty($_SESSION['user_id']));
  45. // turn on output buffering
  46. ob_start();
  47. // check the initial error state
  48. if ($RCMAIL->config->get_error() || $RCMAIL->db->is_error()) {
  49. rcmail_fatal_error();
  50. }
  51. // error steps
  52. if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) {
  53. rcmail::raise_error(['code' => hexdec($_GET['_code'])], false, true);
  54. }
  55. // check if https is required (for login) and redirect if necessary
  56. if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
  57. // force_https can be true, <hostname>, <hostname>:<port>, <port>
  58. if (!is_bool($force_https)) {
  59. list($host, $port) = explode(':', $force_https);
  60. if (is_numeric($host) && empty($port)) {
  61. $port = $host;
  62. $host = '';
  63. }
  64. }
  65. if (empty($port)) {
  66. $port = 443;
  67. }
  68. if (!rcube_utils::https_check($port)) {
  69. if (empty($host)) {
  70. $host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  71. }
  72. if ($port != 443) {
  73. $host .= ':' . $port;
  74. }
  75. header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
  76. exit;
  77. }
  78. }
  79. // trigger startup plugin hook
  80. $startup = $RCMAIL->plugins->exec_hook('startup', ['task' => $RCMAIL->task, 'action' => $RCMAIL->action]);
  81. $RCMAIL->set_task($startup['task']);
  82. $RCMAIL->action = $startup['action'];
  83. $session_error = null;
  84. // try to log in
  85. if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
  86. $request_valid = !empty($_SESSION['temp']) && $RCMAIL->check_request();
  87. $pass_charset = $RCMAIL->config->get('password_charset', 'UTF-8');
  88. // purge the session in case of new login when a session already exists
  89. if ($request_valid) {
  90. $RCMAIL->kill_session();
  91. }
  92. $auth = $RCMAIL->plugins->exec_hook('authenticate', [
  93. 'host' => $RCMAIL->autoselect_host(),
  94. 'user' => trim(rcube_utils::get_input_string('_user', rcube_utils::INPUT_POST)),
  95. 'pass' => rcube_utils::get_input_string('_pass', rcube_utils::INPUT_POST, true, $pass_charset),
  96. 'valid' => $request_valid,
  97. 'error' => null,
  98. 'cookiecheck' => true,
  99. ]);
  100. // Login
  101. if ($auth['valid'] && !$auth['abort']
  102. && $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck'])
  103. ) {
  104. // create new session ID, don't destroy the current session
  105. // it was destroyed already by $RCMAIL->kill_session() above
  106. $RCMAIL->session->remove('temp');
  107. $RCMAIL->session->regenerate_id(false);
  108. // send auth cookie if necessary
  109. $RCMAIL->session->set_auth_cookie();
  110. // log successful login
  111. $RCMAIL->log_login();
  112. // restore original request parameters
  113. $query = [];
  114. if ($url = rcube_utils::get_input_string('_url', rcube_utils::INPUT_POST)) {
  115. parse_str($url, $query);
  116. // prevent endless looping on login page
  117. if (!empty($query['_task']) && $query['_task'] == 'login') {
  118. unset($query['_task']);
  119. }
  120. // prevent redirect to compose with specified ID (#1488226)
  121. if (!empty($query['_action']) && $query['_action'] == 'compose' && !empty($query['_id'])) {
  122. $query = ['_action' => 'compose'];
  123. }
  124. }
  125. // allow plugins to control the redirect url after login success
  126. $redir = $RCMAIL->plugins->exec_hook('login_after', $query + ['_task' => 'mail']);
  127. unset($redir['abort'], $redir['_err']);
  128. // send redirect
  129. $RCMAIL->output->redirect($redir, 0, true);
  130. }
  131. else {
  132. if (!$auth['valid']) {
  133. $error_code = rcmail::ERROR_INVALID_REQUEST;
  134. }
  135. else {
  136. $error_code = is_numeric($auth['error']) ? $auth['error'] : $RCMAIL->login_error();
  137. }
  138. $error_labels = [
  139. rcmail::ERROR_STORAGE => 'storageerror',
  140. rcmail::ERROR_COOKIES_DISABLED => 'cookiesdisabled',
  141. rcmail::ERROR_INVALID_REQUEST => 'invalidrequest',
  142. rcmail::ERROR_INVALID_HOST => 'invalidhost',
  143. rcmail::ERROR_RATE_LIMIT => 'accountlocked',
  144. ];
  145. if (!empty($auth['error']) && !is_numeric($auth['error'])) {
  146. $error_message = $auth['error'];
  147. }
  148. else {
  149. $error_message = !empty($error_labels[$error_code]) ? $error_labels[$error_code] : 'loginfailed';
  150. }
  151. $RCMAIL->output->show_message($error_message, 'warning');
  152. // log failed login
  153. $RCMAIL->log_login($auth['user'], true, $error_code);
  154. $RCMAIL->plugins->exec_hook('login_failed', [
  155. 'code' => $error_code,
  156. 'host' => $auth['host'],
  157. 'user' => $auth['user'],
  158. ]);
  159. if (!isset($_SESSION['user_id'])) {
  160. $RCMAIL->kill_session();
  161. }
  162. }
  163. }
  164. // handle oauth login requests
  165. else if ($RCMAIL->task == 'login' && $RCMAIL->action == 'oauth' && $RCMAIL->oauth->is_enabled()) {
  166. $oauth_handler = new rcmail_action_login_oauth();
  167. $oauth_handler->run();
  168. }
  169. // end session
  170. else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id'])) {
  171. $RCMAIL->request_security_check(rcube_utils::INPUT_GET | rcube_utils::INPUT_POST);
  172. $userdata = array(
  173. 'user' => $_SESSION['username'],
  174. 'host' => $_SESSION['storage_host'],
  175. 'lang' => $RCMAIL->user->language,
  176. );
  177. $RCMAIL->output->show_message('loggedout');
  178. $RCMAIL->logout_actions();
  179. $RCMAIL->kill_session();
  180. $RCMAIL->plugins->exec_hook('logout_after', $userdata);
  181. }
  182. // check session and auth cookie
  183. else if ($RCMAIL->task != 'login' && $_SESSION['user_id']) {
  184. if (!$RCMAIL->session->check_auth()) {
  185. $RCMAIL->kill_session();
  186. $session_error = 'sessionerror';
  187. }
  188. }
  189. // not logged in -> show login page
  190. if (empty($RCMAIL->user->ID)) {
  191. if (
  192. $session_error
  193. || (!empty($_REQUEST['_err']) && $_REQUEST['_err'] === 'session')
  194. || ($session_error = $RCMAIL->session_error())
  195. ) {
  196. $RCMAIL->output->show_message($session_error ?: 'sessionerror', 'error', null, true, -1);
  197. }
  198. if ($RCMAIL->output->ajax_call || $RCMAIL->output->get_env('framed')) {
  199. $RCMAIL->output->command('session_error', $RCMAIL->url(['_err' => 'session']));
  200. $RCMAIL->output->send('iframe');
  201. }
  202. // check if installer is still active
  203. if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
  204. $RCMAIL->output->add_footer(html::div(['id' => 'login-addon', 'style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"],
  205. html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
  206. html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
  207. html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because
  208. these files may expose sensitive configuration data like server passwords and encryption keys
  209. to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
  210. ));
  211. }
  212. $plugin = $RCMAIL->plugins->exec_hook('unauthenticated', [
  213. 'task' => 'login',
  214. 'error' => $session_error,
  215. // Return 401 only on failed logins (#7010)
  216. 'http_code' => empty($session_error) && !empty($error_message) ? 401 : 200
  217. ]);
  218. $RCMAIL->set_task($plugin['task']);
  219. if ($plugin['http_code'] == 401) {
  220. header('HTTP/1.0 401 Unauthorized');
  221. }
  222. $RCMAIL->output->send($plugin['task']);
  223. }
  224. else {
  225. // CSRF prevention
  226. $RCMAIL->request_security_check();
  227. // check access to disabled actions
  228. $disabled_actions = (array) $RCMAIL->config->get('disabled_actions');
  229. if (in_array($RCMAIL->task . '.' . ($RCMAIL->action ?: 'index'), $disabled_actions)) {
  230. rcube::raise_error(['code' => 404, 'message' => "Action disabled"], true, true);
  231. }
  232. }
  233. $RCMAIL->action_handler();