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

101 lines
2.2 KiB
PHP
Executable File

<?php
class Account
{
static function sync ($db, $user, $password = NULL)
{
$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;
$conf = $db->getRow (
'SELECT sambaHost, homesHost, sshUser, sshPass
FROM account.accountConfig'
);
$sshPass = base64_decode ($conf['sshPass']);
$samba = new SshConnection ($conf['sambaHost']
,$conf['sshUser']
,$sshPass
);
$homes = new SshConnection ($conf['homesHost']
,$conf['sshUser']
,$sshPass
);
$escUser = SshConnection::escape ($user);
// Creates the Samba user and initializes it's home directory
$userId = $db->getValue (
'SELECT id FROM account.user WHERE name = #', [$user]);
$accConf = $db->getRow (
'SELECT uidBase, domain FROM account.accountConfig');
$escUid = SshConnection::escape ($accConf['uidBase'] + $userId);
$escMail = SshConnection::escape ("$user@{$accConf['domain']}");
$samba->exec (
"/mnt/cluster/scripts/create-user.sh $escUser $escUid $escMail");
$homes->exec (
"/mnt/storage/scripts/create-user.sh $escUser");
// Syncronizes the Samba password
if (empty ($password))
return;
$escPassword = SshConnection::escape ($password);
$samba->exec (
"/mnt/cluster/scripts/set-password.sh $escUser $escPassword");
new SshConnection ($conf['homesHost'], $user, $password);
}
}
class SshConnection
{
var $connection;
/**
* Abrebiated method to make SSH connections.
*/
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.
*/
function exec ($command)
{
return ssh2_exec ($this->connection, $command);
}
/**
* Escapes the double quotes from an string.
*/
static function escape ($str)
{
return '"'. str_replace ('"', '\\"', $str) .'"';
}
}