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.

283 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.5.3 |
  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 if config files had errors
  48. if ($err_str = $RCMAIL->config->get_error()) {
  49. rcmail::raise_error(['code' => 601, 'message' => $err_str], false, true);
  50. }
  51. // check DB connections and exit on failure
  52. if ($err_str = $RCMAIL->db->is_error()) {
  53. rcmail::raise_error(['code' => 603, 'type' => 'db', 'message' => $err_str], false, true);
  54. }
  55. // error steps
  56. if ($RCMAIL->action == 'error' && !empty($_GET['_code'])) {
  57. rcmail::raise_error(['code' => hexdec($_GET['_code'])], false, true);
  58. }
  59. // check if https is required (for login) and redirect if necessary
  60. if (empty($_SESSION['user_id']) && ($force_https = $RCMAIL->config->get('force_https', false))) {
  61. // force_https can be true, <hostname>, <hostname>:<port>, <port>
  62. if (!is_bool($force_https)) {
  63. list($host, $port) = explode(':', $force_https);
  64. if (is_numeric($host) && empty($port)) {
  65. $port = $host;
  66. $host = '';
  67. }
  68. }
  69. if (empty($port)) {
  70. $port = 443;
  71. }
  72. if (!rcube_utils::https_check($port)) {
  73. if (empty($host)) {
  74. $host = preg_replace('/:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
  75. }
  76. if ($port != 443) {
  77. $host .= ':' . $port;
  78. }
  79. header('Location: https://' . $host . $_SERVER['REQUEST_URI']);
  80. exit;
  81. }
  82. }
  83. // trigger startup plugin hook
  84. $startup = $RCMAIL->plugins->exec_hook('startup', ['task' => $RCMAIL->task, 'action' => $RCMAIL->action]);
  85. $RCMAIL->set_task($startup['task']);
  86. $RCMAIL->action = $startup['action'];
  87. $session_error = null;
  88. // try to log in
  89. if ($RCMAIL->task == 'login' && $RCMAIL->action == 'login') {
  90. $request_valid = !empty($_SESSION['temp']) && $RCMAIL->check_request();
  91. $pass_charset = $RCMAIL->config->get('password_charset', 'UTF-8');
  92. // purge the session in case of new login when a session already exists
  93. if ($request_valid) {
  94. $RCMAIL->kill_session();
  95. }
  96. $auth = $RCMAIL->plugins->exec_hook('authenticate', [
  97. 'host' => $RCMAIL->autoselect_host(),
  98. 'user' => trim(rcube_utils::get_input_string('_user', rcube_utils::INPUT_POST)),
  99. 'pass' => rcube_utils::get_input_string('_pass', rcube_utils::INPUT_POST, true, $pass_charset),
  100. 'valid' => $request_valid,
  101. 'error' => null,
  102. 'cookiecheck' => true,
  103. ]);
  104. // Login
  105. if ($auth['valid'] && !$auth['abort']
  106. && $RCMAIL->login($auth['user'], $auth['pass'], $auth['host'], $auth['cookiecheck'])
  107. ) {
  108. // create new session ID, don't destroy the current session
  109. // it was destroyed already by $RCMAIL->kill_session() above
  110. $RCMAIL->session->remove('temp');
  111. $RCMAIL->session->regenerate_id(false);
  112. // send auth cookie if necessary
  113. $RCMAIL->session->set_auth_cookie();
  114. // log successful login
  115. $RCMAIL->log_login();
  116. // restore original request parameters
  117. $query = [];
  118. if ($url = rcube_utils::get_input_string('_url', rcube_utils::INPUT_POST)) {
  119. parse_str($url, $query);
  120. // prevent endless looping on login page
  121. if (!empty($query['_task']) && $query['_task'] == 'login') {
  122. unset($query['_task']);
  123. }
  124. // prevent redirect to compose with specified ID (#1488226)
  125. if (!empty($query['_action']) && $query['_action'] == 'compose' && !empty($query['_id'])) {
  126. $query = ['_action' => 'compose'];
  127. }
  128. }
  129. // allow plugins to control the redirect url after login success
  130. $redir = $RCMAIL->plugins->exec_hook('login_after', $query + ['_task' => 'mail']);
  131. unset($redir['abort'], $redir['_err']);
  132. // send redirect
  133. $RCMAIL->output->redirect($redir, 0, true);
  134. }
  135. else {
  136. if (!$auth['valid']) {
  137. $error_code = rcmail::ERROR_INVALID_REQUEST;
  138. }
  139. else {
  140. $error_code = is_numeric($auth['error']) ? $auth['error'] : $RCMAIL->login_error();
  141. }
  142. $error_labels = [
  143. rcmail::ERROR_STORAGE => 'storageerror',
  144. rcmail::ERROR_COOKIES_DISABLED => 'cookiesdisabled',
  145. rcmail::ERROR_INVALID_REQUEST => 'invalidrequest',
  146. rcmail::ERROR_INVALID_HOST => 'invalidhost',
  147. rcmail::ERROR_RATE_LIMIT => 'accountlocked',
  148. ];
  149. if (!empty($auth['error']) && !is_numeric($auth['error'])) {
  150. $error_message = $auth['error'];
  151. }
  152. else {
  153. $error_message = !empty($error_labels[$error_code]) ? $error_labels[$error_code] : 'loginfailed';
  154. }
  155. $RCMAIL->output->show_message($error_message, 'warning');
  156. // log failed login
  157. $RCMAIL->log_login($auth['user'], true, $error_code);
  158. $RCMAIL->plugins->exec_hook('login_failed', [
  159. 'code' => $error_code,
  160. 'host' => $auth['host'],
  161. 'user' => $auth['user'],
  162. ]);
  163. if (!isset($_SESSION['user_id'])) {
  164. $RCMAIL->kill_session();
  165. }
  166. }
  167. }
  168. // handle oauth login requests
  169. else if ($RCMAIL->task == 'login' && $RCMAIL->action == 'oauth' && $RCMAIL->oauth->is_enabled()) {
  170. $oauth_handler = new rcmail_action_login_oauth();
  171. $oauth_handler->run();
  172. }
  173. // end session
  174. else if ($RCMAIL->task == 'logout' && isset($_SESSION['user_id'])) {
  175. $RCMAIL->request_security_check(rcube_utils::INPUT_GET | rcube_utils::INPUT_POST);
  176. $userdata = array(
  177. 'user' => $_SESSION['username'],
  178. 'host' => $_SESSION['storage_host'],
  179. 'lang' => $RCMAIL->user->language,
  180. );
  181. $RCMAIL->output->show_message('loggedout');
  182. $RCMAIL->logout_actions();
  183. $RCMAIL->kill_session();
  184. $RCMAIL->plugins->exec_hook('logout_after', $userdata);
  185. }
  186. // check session and auth cookie
  187. else if ($RCMAIL->task != 'login' && $_SESSION['user_id']) {
  188. if (!$RCMAIL->session->check_auth()) {
  189. $RCMAIL->kill_session();
  190. $session_error = 'sessionerror';
  191. }
  192. }
  193. // not logged in -> show login page
  194. if (empty($RCMAIL->user->ID)) {
  195. if (
  196. $session_error
  197. || (!empty($_REQUEST['_err']) && $_REQUEST['_err'] === 'session')
  198. || ($session_error = $RCMAIL->session_error())
  199. ) {
  200. $RCMAIL->output->show_message($session_error ?: 'sessionerror', 'error', null, true, -1);
  201. }
  202. if ($RCMAIL->output->ajax_call || $RCMAIL->output->get_env('framed')) {
  203. $RCMAIL->output->command('session_error', $RCMAIL->url(['_err' => 'session']));
  204. $RCMAIL->output->send('iframe');
  205. }
  206. // check if installer is still active
  207. if ($RCMAIL->config->get('enable_installer') && is_readable('./installer/index.php')) {
  208. $RCMAIL->output->add_footer(html::div(['id' => 'login-addon', 'style' => "background:#ef9398; border:2px solid #dc5757; padding:0.5em; margin:2em auto; width:50em"],
  209. html::tag('h2', array('style' => "margin-top:0.2em"), "Installer script is still accessible") .
  210. html::p(null, "The install script of your Roundcube installation is still stored in its default location!") .
  211. html::p(null, "Please <b>remove</b> the whole <tt>installer</tt> folder from the Roundcube directory because
  212. these files may expose sensitive configuration data like server passwords and encryption keys
  213. to the public. Make sure you cannot access the <a href=\"./installer/\">installer script</a> from your browser.")
  214. ));
  215. }
  216. $plugin = $RCMAIL->plugins->exec_hook('unauthenticated', [
  217. 'task' => 'login',
  218. 'error' => $session_error,
  219. // Return 401 only on failed logins (#7010)
  220. 'http_code' => empty($session_error) && !empty($error_message) ? 401 : 200
  221. ]);
  222. $RCMAIL->set_task($plugin['task']);
  223. if ($plugin['http_code'] == 401) {
  224. header('HTTP/1.0 401 Unauthorized');
  225. }
  226. $RCMAIL->output->send($plugin['task']);
  227. }
  228. else {
  229. // CSRF prevention
  230. $RCMAIL->request_security_check();
  231. // check access to disabled actions
  232. $disabled_actions = (array) $RCMAIL->config->get('disabled_actions');
  233. if (in_array($RCMAIL->task . '.' . ($RCMAIL->action ?: 'index'), $disabled_actions)) {
  234. rcube::raise_error(['code' => 404, 'message' => "Action disabled"], true, true);
  235. }
  236. }
  237. $RCMAIL->action_handler();