Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
162 lines (158 sloc) 6.11 KB
* This file is part of OpenMediaVault.
* @license GPL Version 3
* @author Volker Theile <>
* @copyright Copyright (c) 2009-2018 Volker Theile
* OpenMediaVault is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* OpenMediaVault is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with OpenMediaVault. If not, see <>.
class OMVRpcServiceSession extends \OMV\Rpc\ServiceAbstract {
* Get the RPC service name.
final public function getName() {
return "Session";
* Initialize the RPC service.
final public function initialize() {
* Login user.
* @param params The method parameters containing the following fields:
* \em username The name of the user.
* \em password The password.
* @param context The context of the caller.
* @return An array containing the fields \em authenticated which is TRUE
* if authentication was successful, otherwise FALSE. The name of the
* user is in \em username.
final public function login($params, $context) {
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.session.login");
$authenticated = FALSE;
// Authenticate the given user. Note, the OMV engine RPC is
// executed in another context which allows reading the shadow
// file, otherwise pam_auth() will fail.
$object = \OMV\Rpc\Rpc::call("UserMgmt", "authUser", $params,
$this->getAdminContext(), \OMV\Rpc\Rpc::MODE_REMOTE, TRUE);
if (!is_null($object) && (TRUE === $object['authenticated'])) {
$session = &\OMV\Session::getInstance();
if ($session->isAuthenticated()) {
// Is the current session registered to the user to be
// authenticated?
if ($session->getUsername() !== $params['username']) {
throw new \OMV\ErrorMsgException(
} else {
// Initialize session.
$role = ($params['username'] === "admin") ?
$session->initialize($params['username'], $role);
$authenticated = $session->isAuthenticated();
// Get the product information to customize the syslog identity
// and cookie name.
$prd = new \OMV\ProductInfo();
$ident = mb_strtolower(sprintf("%s-webgui", $prd->getName()));
$prefix = mb_strtoupper(sprintf("X-%s-LOGIN", $prd->getName()));
// Open connection to system logger.
openlog($ident, LOG_PID | LOG_PERROR, LOG_AUTH);
if (TRUE === $authenticated) {
// Log the successful login attempt via syslog.
syslog(LOG_ALERT, sprintf("Authorized login from %s ".
"[username=%s, user-agent=%s]", $_SERVER['REMOTE_ADDR'],
$params['username'], $_SERVER['HTTP_USER_AGENT']));
// Send an email to the user if a new web browser is used to log
// in to the control panel. If no special cookie is detected,
// then it is assumed that the web browser has not been used to
// log in until now. Note, the cookie expires after 60 days.
// The salted user name hash is used as identifier.
$firstLogIn = TRUE;
foreach ($_COOKIE as $cookiek => $cookiev) {
// Extract the hashed username. Note, the hash is encoded
// to meet RFC 2616.
$regex = sprintf('/^%s-(.+)$/', $prefix);
if (1 !== preg_match($regex, $cookiek, $matches))
$hashedUsername = urldecode($matches[1]);
if (hash_equals($hashedUsername, crypt($params['username'],
$hashedUsername))) {
$firstLogIn = FALSE;
if (TRUE === $firstLogIn) {
// Send the notification email to the user.
$subject = sprintf("Your user account was used to log in to ".
"the %s control panel via a web browser.", $prd->getName());
$message = sprintf("Dear %s,\n\n".
"your user account was used to log in to %s (%s) via a web browser.\n\n".
"Date and time: %s\n".
"Remote address: %s\n".
"User agent: %s\n\n".
"If you recently logged in to the control panel, you can disregard this email. ".
"If you have not logged in recently and believe someone may have accessed ".
"your account, you should reset your password.\n\n".
"your %s system",
$params['username'], \OMV\System\Net\Dns::getFqdn(),
$_SERVER['HTTP_USER_AGENT'], $prd->getName());
$mail = new \OMV\Email("root", $params['username'],
$subject, $message);
// Set a cookie. It will expire after 60 days. The user name
// is crypted/hashed to be unable to reverse engineer the
// account names from the cookie. Finally encode the hashed
// user name to meet RFC 2616.
$hashedUsername = password_hash($params['username'],
$key = sprintf("%s-%s", $prefix, urlencode($hashedUsername));
$value = array_rand_value(\OMV\Environment::get(
$expire = time() + 60 * 60 * 24 * 60;
setcookie($key, $value, $expire);
} else {
// Log the unauthorized login attempt via syslog.
syslog(LOG_ALERT, sprintf("Unauthorized login attempt from %s ".
"[username=%s, user-agent=%s]", $_SERVER['REMOTE_ADDR'],
$params['username'], $_SERVER['HTTP_USER_AGENT']));
return [
"authenticated" => $authenticated,
"username" => $params['username']
* Logout user.
final public function logout($params, $context) {
// Check the permissions and destroy the session.
$session = &\OMV\Session::getInstance();