getRow ( 'SELECT sambaHost, homesHost, sshUser, sshPass FROM account.accountConfig'); $sshPass = base64_decode ($conf['sshPass']); $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; $sambaSsh = new SshConnection ($conf['sambaHost'] ,$conf['sshUser'] ,$sshPass ); if ($sync & self::USER) { $userId = $db->getValue ('SELECT id FROM account.user WHERE name = #', [$user]); $accConf = $db->getRow ('SELECT uidBase, domain FROM account.accountConfig'); $escUser = SshConnection::escape ($user); $escUid = SshConnection::escape ($accConf['uidBase'] + $userId); $escMail = SshConnection::escape ("$user@{$accConf['domain']}"); $sambaSsh->exec ( "/mnt/cluster/scripts/create-user.sh $escUser $escUid $escMail"); $homesSsh = new SshConnection ($conf['homesHost'] ,$conf['sshUser'] ,$sshPass ); $homesSsh->exec ( "/mnt/storage/scripts/create-user.sh $escUser"); } if ($sync & self::PASS && !empty ($password)) { $escUser = SshConnection::escape ($user); $escPassword = SshConnection::escape ($password); $sambaSsh->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) .'"'; } }