/** * Connect to an IMAP server * * @param string $host Host to connect * @param string $user Username for IMAP account * @param string $pass Password for IMAP account * @param integer $port Port to connect to * @param string $use_ssl SSL schema (either ssl or tls) or null if plain connection * * @return boolean True on success, False on failure */ public function connect($host, $user, $pass, $port = 143, $use_ssl = null) { // check for OpenSSL support in PHP build if ($use_ssl && extension_loaded('openssl')) { $this->options['ssl_mode'] = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; } else { if ($use_ssl) { rcube::raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => "OpenSSL not available"), true, false); $port = 143; } } $this->options['port'] = $port; if ($this->options['debug']) { $this->set_debug(true); $this->options['ident'] = array('name' => 'Roundcube', 'version' => RCUBE_VERSION, 'php' => PHP_VERSION, 'os' => PHP_OS, 'command' => $_SERVER['REQUEST_URI']); } $attempt = 0; do { $data = $this->plugins->exec_hook('storage_connect', array_merge($this->options, array('host' => $host, 'user' => $user, 'attempt' => ++$attempt))); if (!empty($data['pass'])) { $pass = $data['pass']; } // Handle per-host socket options rcube_utils::parse_socket_options($data['socket_options'], $data['host']); $this->conn->connect($data['host'], $data['user'], $pass, $data); } while (!$this->conn->connected() && $data['retry']); $config = array('host' => $data['host'], 'user' => $data['user'], 'password' => $pass, 'port' => $port, 'ssl' => $use_ssl); $this->options = array_merge($this->options, $config); $this->connect_done = true; if ($this->conn->connected()) { // check for session identifier $session = null; if (preg_match('/\\s+SESSIONID=([^=\\s]+)/', $this->conn->result, $m)) { $session = $m[1]; } // get namespace and delimiter $this->set_env(); // trigger post-connect hook $this->plugins->exec_hook('storage_connected', array('host' => $host, 'user' => $user, 'session' => $session)); return true; } else { if ($this->conn->error) { if ($pass && $user) { $message = sprintf("Login failed for %s from %s. %s", $user, rcube_utils::remote_ip(), $this->conn->error); rcube::raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, 'line' => __LINE__, 'message' => $message), true, false); } } } return false; }
/** * SMTP Connection and authentication * * @param string Server host * @param string Server port * @param string User name * @param string Password * * @return bool Returns true on success, or false on error */ public function connect($host = null, $port = null, $user = null, $pass = null) { $rcube = rcube::get_instance(); // disconnect/destroy $this->conn $this->disconnect(); // reset error/response var $this->error = $this->response = null; // let plugins alter smtp connection config $CONFIG = $rcube->plugins->exec_hook('smtp_connect', array('smtp_server' => $host ?: $rcube->config->get('smtp_server'), 'smtp_port' => $port ?: $rcube->config->get('smtp_port', 25), 'smtp_user' => $user !== null ? $user : $rcube->config->get('smtp_user'), 'smtp_pass' => $pass !== null ? $pass : $rcube->config->get('smtp_pass'), 'smtp_auth_cid' => $rcube->config->get('smtp_auth_cid'), 'smtp_auth_pw' => $rcube->config->get('smtp_auth_pw'), 'smtp_auth_type' => $rcube->config->get('smtp_auth_type'), 'smtp_helo_host' => $rcube->config->get('smtp_helo_host'), 'smtp_timeout' => $rcube->config->get('smtp_timeout'), 'smtp_conn_options' => $rcube->config->get('smtp_conn_options'), 'smtp_auth_callbacks' => array())); $smtp_host = rcube_utils::parse_host($CONFIG['smtp_server']); // when called from Installer it's possible to have empty $smtp_host here if (!$smtp_host) { $smtp_host = 'localhost'; } $smtp_port = is_numeric($CONFIG['smtp_port']) ? $CONFIG['smtp_port'] : 25; $smtp_host_url = parse_url($smtp_host); // overwrite port if (isset($smtp_host_url['host']) && isset($smtp_host_url['port'])) { $smtp_host = $smtp_host_url['host']; $smtp_port = $smtp_host_url['port']; } // re-write smtp host if (isset($smtp_host_url['host']) && isset($smtp_host_url['scheme'])) { $smtp_host = sprintf('%s://%s', $smtp_host_url['scheme'], $smtp_host_url['host']); } // remove TLS prefix and set flag for use in Net_SMTP::auth() if (preg_match('#^tls://#i', $smtp_host)) { $smtp_host = preg_replace('#^tls://#i', '', $smtp_host); $use_tls = true; } // Handle per-host socket options rcube_utils::parse_socket_options($CONFIG['smtp_conn_options'], $smtp_host); if (!empty($CONFIG['smtp_helo_host'])) { $helo_host = $CONFIG['smtp_helo_host']; } else { if (!empty($_SERVER['SERVER_NAME'])) { $helo_host = preg_replace('/:\\d+$/', '', $_SERVER['SERVER_NAME']); } else { $helo_host = 'localhost'; } } // IDNA Support $smtp_host = rcube_utils::idn_to_ascii($smtp_host); $this->conn = new Net_SMTP($smtp_host, $smtp_port, $helo_host, false, 0, $CONFIG['smtp_conn_options']); if ($rcube->config->get('smtp_debug')) { $this->conn->setDebug(true, array($this, 'debug_handler')); $this->anonymize_log = 0; } // register authentication methods if (!empty($CONFIG['smtp_auth_callbacks']) && method_exists($this->conn, 'setAuthMethod')) { foreach ($CONFIG['smtp_auth_callbacks'] as $callback) { $this->conn->setAuthMethod($callback['name'], $callback['function'], isset($callback['prepend']) ? $callback['prepend'] : true); } } // try to connect to server and exit on failure $result = $this->conn->connect($CONFIG['smtp_timeout']); if (is_a($result, 'PEAR_Error')) { $this->response[] = "Connection failed: " . $result->getMessage(); list($code, ) = $this->conn->getResponse(); $this->error = array('label' => 'smtpconnerror', 'vars' => array('code' => $code)); $this->conn = null; return false; } // workaround for timeout bug in Net_SMTP 1.5.[0-1] (#1487843) if (method_exists($this->conn, 'setTimeout') && ($timeout = ini_get('default_socket_timeout'))) { $this->conn->setTimeout($timeout); } $smtp_user = str_replace('%u', $rcube->get_user_name(), $CONFIG['smtp_user']); $smtp_pass = str_replace('%p', $rcube->get_user_password(), $CONFIG['smtp_pass']); $smtp_auth_type = $CONFIG['smtp_auth_type'] ?: null; if (!empty($CONFIG['smtp_auth_cid'])) { $smtp_authz = $smtp_user; $smtp_user = $CONFIG['smtp_auth_cid']; $smtp_pass = $CONFIG['smtp_auth_pw']; } // attempt to authenticate to the SMTP server if ($smtp_user && $smtp_pass) { // IDNA Support if (strpos($smtp_user, '@')) { $smtp_user = rcube_utils::idn_to_ascii($smtp_user); } $result = $this->conn->auth($smtp_user, $smtp_pass, $smtp_auth_type, $use_tls, $smtp_authz); if (is_a($result, 'PEAR_Error')) { list($code, ) = $this->conn->getResponse(); $this->error = array('label' => 'smtpautherror', 'vars' => array('code' => $code)); $this->response[] = 'Authentication failure: ' . $result->getMessage() . ' (Code: ' . $result->getCode() . ')'; $this->reset(); $this->disconnect(); return false; } } return true; }
/** * Connect to configured managesieve server * * @param string $username User login * @param string $password User password * * @return int Connection status: 0 on success, >0 on failure */ public function connect($username, $password) { // Get connection parameters $host = $this->rc->config->get('managesieve_host', 'localhost'); $port = $this->rc->config->get('managesieve_port'); $tls = $this->rc->config->get('managesieve_usetls', false); $host = rcube_utils::parse_host($host); $host = rcube_utils::idn_to_ascii($host); // remove tls:// prefix, set TLS flag if (($host = preg_replace('|^tls://|i', '', $host, 1, $cnt)) && $cnt) { $tls = true; } if (empty($port)) { $port = getservbyname('sieve', 'tcp'); if (empty($port)) { $port = self::PORT; } } $plugin = $this->rc->plugins->exec_hook('managesieve_connect', array('user' => $username, 'password' => $password, 'host' => $host, 'port' => $port, 'usetls' => $tls, 'auth_type' => $this->rc->config->get('managesieve_auth_type'), 'disabled' => $this->rc->config->get('managesieve_disabled_extensions'), 'debug' => $this->rc->config->get('managesieve_debug', false), 'auth_cid' => $this->rc->config->get('managesieve_auth_cid'), 'auth_pw' => $this->rc->config->get('managesieve_auth_pw'), 'socket_options' => $this->rc->config->get('managesieve_conn_options'))); // Handle per-host socket options rcube_utils::parse_socket_options($plugin['socket_options'], $plugin['host']); // try to connect to managesieve server and to fetch the script $this->sieve = new rcube_sieve($plugin['user'], $plugin['password'], $plugin['host'], $plugin['port'], $plugin['auth_type'], $plugin['usetls'], $plugin['disabled'], $plugin['debug'], $plugin['auth_cid'], $plugin['auth_pw'], $plugin['socket_options']); $error = $this->sieve->error(); if ($error) { rcube::raise_error(array('code' => 403, 'file' => __FILE__, 'line' => __LINE__, 'message' => "Unable to connect to managesieve on {$host}:{$port}"), true, false); } return $error; }