0
1
Fork 0
hedera-web-mindshore/rest/core/account.php

101 lines
2.2 KiB
PHP
Raw Normal View History

2016-10-27 11:22:04 +00:00
<?php
class Account
{
2017-05-02 12:33:48 +00:00
static function sync ($db, $user, $password = NULL)
2016-10-27 11:22:04 +00:00
{
$hasAccount = $db->getValue (
'SELECT COUNT(*) > 0
FROM account.user u
JOIN account.account a ON u.id = a.id
WHERE u.name = #',
[$user]
);
if (!$hasAccount)
return;
2017-05-02 12:33:48 +00:00
$conf = $db->getRow (
'SELECT sambaHost, homesHost, sshUser, sshPass
FROM account.accountConfig'
);
$sshPass = base64_decode ($conf['sshPass']);
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
$samba = new SshConnection ($conf['sambaHost']
2016-10-27 11:22:04 +00:00
,$conf['sshUser']
,$sshPass
);
2017-05-02 12:33:48 +00:00
$homes = new SshConnection ($conf['homesHost']
,$conf['sshUser']
,$sshPass
);
$escUser = SshConnection::escape ($user);
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
// Creates the Samba user and initializes it's home directory
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
$userId = $db->getValue (
'SELECT id FROM account.user WHERE name = #', [$user]);
$accConf = $db->getRow (
'SELECT uidBase, domain FROM account.accountConfig');
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
$escUid = SshConnection::escape ($accConf['uidBase'] + $userId);
$escMail = SshConnection::escape ("$user@{$accConf['domain']}");
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
$samba->exec (
"/mnt/cluster/scripts/create-user.sh $escUser $escUid $escMail");
$homes->exec (
"/mnt/storage/scripts/create-user.sh $escUser");
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
// Syncronizes the Samba password
2016-10-27 11:22:04 +00:00
2017-05-02 12:33:48 +00:00
if (empty ($password))
return;
$escPassword = SshConnection::escape ($password);
$samba->exec (
"/mnt/cluster/scripts/set-password.sh $escUser $escPassword");
new SshConnection ($conf['homesHost'], $user, $password);
2016-10-27 11:22:04 +00:00
}
}
class SshConnection
{
var $connection;
/**
* Abrebiated method to make SSH connections.
2016-12-20 09:32:17 +00:00
*/
2016-10-27 11:22:04 +00:00
function __construct ($host, $user, $password)
{
$this->connection = $connection = ssh2_connect ($host);
if (!$connection)
throw new Exception ("Can't connect to SSH server $host");
$authOk = ssh2_auth_password ($connection, $user, $password);
if (!$authOk)
throw new Exception ("SSH authentication failed on server $host");
return $connection;
}
/**
* Executes a command on the host.
2016-12-20 09:32:17 +00:00
*/
2016-10-27 11:22:04 +00:00
function exec ($command)
{
return ssh2_exec ($this->connection, $command);
}
/**
* Escapes the double quotes from an string.
2016-12-20 09:32:17 +00:00
*/
2016-10-27 11:22:04 +00:00
static function escape ($str)
{
return '"'. str_replace ('"', '\\"', $str) .'"';
}
}