2004-12-09 19:10:57 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
$Id$
|
|
|
|
|
2009-10-27 18:47:12 +00:00
|
|
|
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
|
2010-02-28 18:28:44 +00:00
|
|
|
Copyright (C) 2004 - 2010 Roland Gruber
|
2004-12-09 19:10:57 +00:00
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file includes functions to control lamdaemon.
|
|
|
|
*
|
|
|
|
* @author Tilo Lutz
|
|
|
|
* @author Roland Gruber
|
2007-02-20 17:39:37 +00:00
|
|
|
* @author Thomas Manninger
|
2004-12-09 19:10:57 +00:00
|
|
|
*
|
|
|
|
* @package modules
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends commands to lamdaemon script.
|
|
|
|
*
|
2010-05-14 09:34:11 +00:00
|
|
|
* @param array $command command to execute
|
2007-02-20 17:39:37 +00:00
|
|
|
* @param string $server remote server
|
2004-12-09 19:10:57 +00:00
|
|
|
* @return array Output of lamdaemon
|
|
|
|
*
|
|
|
|
*/
|
2010-05-14 09:34:11 +00:00
|
|
|
function lamdaemon($command, $server) {
|
2010-05-23 13:33:04 +00:00
|
|
|
// remove the following line to restore SSH via PHP SSH2
|
|
|
|
return lamdaemonSeclib($command, $server);
|
2010-02-28 18:28:44 +00:00
|
|
|
if (!function_exists('ssh2_connect')) {
|
|
|
|
return array('ERROR,' . _('This module requires the PHP ssh2 extension.'));
|
|
|
|
}
|
2006-08-27 14:57:50 +00:00
|
|
|
// get username and password of the current lam-admin
|
|
|
|
$credentials = $_SESSION['ldap']->decrypt_login();
|
2008-07-03 18:42:26 +00:00
|
|
|
$serverNameParts = explode(",", $server);
|
|
|
|
if (sizeof($serverNameParts) > 1) {
|
|
|
|
$handle = @ssh2_connect($serverNameParts[0], $serverNameParts[1]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$handle = @ssh2_connect($server);
|
|
|
|
}
|
2006-08-27 14:57:50 +00:00
|
|
|
if ($handle) {
|
2010-05-14 09:34:11 +00:00
|
|
|
$sr = @ldap_read($_SESSION['ldap']->server(), $credentials[0], "objectClass=posixAccount", array('uid'), 0, 0, 0, LDAP_DEREF_NEVER);
|
2006-09-14 20:08:29 +00:00
|
|
|
if (!$sr) {
|
|
|
|
$return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ",");
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
$entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr);
|
2008-07-22 17:30:11 +00:00
|
|
|
if (!isset($entry[0]['uid'][0])) {
|
2006-09-14 20:08:29 +00:00
|
|
|
$return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ",");
|
|
|
|
return $return;
|
|
|
|
}
|
2008-07-22 17:30:11 +00:00
|
|
|
$userName = $entry[0]['uid'][0];
|
2006-09-09 11:45:22 +00:00
|
|
|
if (@ssh2_auth_password($handle, $userName, $credentials[1])) {
|
2010-05-14 10:14:19 +00:00
|
|
|
$shell = ssh2_exec($handle, "sudo " . $_SESSION['config']->get_scriptPath() . ' ' . escapeshellarg($command));
|
2006-09-09 11:45:22 +00:00
|
|
|
$return = array();
|
2010-05-14 09:34:11 +00:00
|
|
|
$time = time() + 30;
|
|
|
|
while (sizeof($return) < 1) {
|
2006-09-09 11:45:22 +00:00
|
|
|
if ($time < time()) {
|
|
|
|
$return = array("ERROR," . _("Timeout while executing lamdaemon commands!") . ",");
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
usleep(100);
|
2009-08-14 20:18:08 +00:00
|
|
|
$read = explode("\n", trim(fread($shell, 100000)));
|
2006-09-09 11:45:22 +00:00
|
|
|
if ((sizeof($read) == 1) && (!isset($read[0]) || ($read[0] == ""))) continue;
|
|
|
|
for ($i = 0; $i < sizeof($read); $i++) {
|
|
|
|
$return[] = $read[$i];
|
|
|
|
}
|
2006-08-27 14:57:50 +00:00
|
|
|
}
|
2006-09-09 11:45:22 +00:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
else {
|
2007-02-20 17:39:37 +00:00
|
|
|
$return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server);
|
2006-09-09 11:45:22 +00:00
|
|
|
return $return;
|
2006-08-27 14:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 18:11:02 +00:00
|
|
|
else {
|
2007-02-20 17:39:37 +00:00
|
|
|
$return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server);
|
2006-10-04 18:11:02 +00:00
|
|
|
return $return;
|
|
|
|
}
|
2006-08-27 14:57:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-23 13:33:04 +00:00
|
|
|
/**
|
|
|
|
* Sends commands to lamdaemon script.
|
|
|
|
*
|
|
|
|
* @param array $command command to execute
|
|
|
|
* @param string $server remote server
|
|
|
|
* @return array Output of lamdaemon
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function lamdaemonSeclib($command, $server) {
|
|
|
|
// add phpseclib to include path
|
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/3rdParty/phpseclib');
|
|
|
|
include_once('Net/SSH2.php');
|
|
|
|
// get username and password of the current lam-admin
|
|
|
|
$credentials = $_SESSION['ldap']->decrypt_login();
|
|
|
|
$serverNameParts = explode(",", $server);
|
|
|
|
if (sizeof($serverNameParts) > 1) {
|
|
|
|
$handle = new Net_SSH2($serverNameParts[0], $serverNameParts[1]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$handle = new Net_SSH2($server);
|
|
|
|
}
|
|
|
|
if ($handle) {
|
|
|
|
$sr = @ldap_read($_SESSION['ldap']->server(), $credentials[0], "objectClass=posixAccount", array('uid'), 0, 0, 0, LDAP_DEREF_NEVER);
|
|
|
|
if (!$sr) {
|
|
|
|
$return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ",");
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
$entry = @ldap_get_entries($_SESSION['ldap']->server(), $sr);
|
|
|
|
if (!isset($entry[0]['uid'][0])) {
|
|
|
|
$return = array("ERROR," . _("Your LAM admin user must be a valid Unix account to work with lamdaemon!") . ",");
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
$userName = $entry[0]['uid'][0];
|
|
|
|
if ($handle->login($userName, $credentials[1])) {
|
|
|
|
$output = $handle->exec("sudo " . $_SESSION['config']->get_scriptPath() . ' ' . escapeshellarg($command));
|
|
|
|
$return = array($output);
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server);
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$return = array("ERROR," . _('Unable to connect to remote server!') . "," . $server);
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-09 19:10:57 +00:00
|
|
|
?>
|