Jelle_Timmer
Basic Pleskian
Hi,
To automate some tasks for some projects I'm working on, I need to be able to automatically create or delete ftp-users.
Creating and listing ftp-accounts is working great, but when it comes to deleting them I run into an internal server error. Before my script gets the response it's waiting for, the script crashes.
- When live watching the Apache error-log with 'tail -f', the terminal-session is exited with 'Killed (core dumped)'
- Apache error-log shows:
- Webbrowser shows default '500 Internal Server Error'
I have checked and changed time-out settings, but it didn't help. Error occurs a few seconds after executing, while the timeouts are set to 60 to 90 seconds.
Packet send to Plesk:
As far as I know, my xml-packet is correctly formatted (API manual), which could be verified by the fact that the user I was trying to delete is actually deleted. But the overall behavior is far from satisfying.
Using secret key or username/password combination makes no difference, both result in error.
Running the same packet through the python-script referred to on this page also kills the terminal-session.
My files:
PleskApiClient.php
ftp.php (only for testing-purposes, will be integrated in existing product when working)
Did I miss something?
I'm working with PHP 5.3 (FastCGI) on Plesk 12 on CentOS 6.6 x64.
To automate some tasks for some projects I'm working on, I need to be able to automatically create or delete ftp-users.
Creating and listing ftp-accounts is working great, but when it comes to deleting them I run into an internal server error. Before my script gets the response it's waiting for, the script crashes.
- When live watching the Apache error-log with 'tail -f', the terminal-session is exited with 'Killed (core dumped)'
- Apache error-log shows:
Code:
(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server, referer: http://example.com/ftp.php?action=delete
Premature end of script headers: ftp.php, referer: http://example.com/ftp.php?action=delete
I have checked and changed time-out settings, but it didn't help. Error occurs a few seconds after executing, while the timeouts are set to 60 to 90 seconds.
Packet send to Plesk:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<packet version="1.6.6.0">
<ftp-user>
<del>
<filter>
<name>test</name>
</filter>
</del>
</ftp-user>
</packet>
As far as I know, my xml-packet is correctly formatted (API manual), which could be verified by the fact that the user I was trying to delete is actually deleted. But the overall behavior is far from satisfying.
Using secret key or username/password combination makes no difference, both result in error.
Running the same packet through the python-script referred to on this page also kills the terminal-session.
My files:
PleskApiClient.php
PHP:
<?php
/**
* @copyright 1999-2015. Parallels IP Holdings GmbH. All Rights Reserved.
*/
class PleskApiClient
{
private $_host;
private $_port;
private $_protocol;
private $_login;
private $_password;
private $_secretKey;
private $result;
/**
* Create client
*
* @param Logging $log
* @param string $host
* @param int $port
* @param string $protocol
*/
public function __construct($host = 'localhost', $port = 8443, $protocol = 'https')
{
$this->_host = $host;
$this->_port = $port;
$this->_protocol = $protocol;
}
/**
* @ignore
*/
final function __set($var, $val) {
$this->$var = $val;
}
/**
* @ignore
*/
final function __get($var) {
return $this->$var;
}
/**
* Setup credentials for authentication
*
* @param string $login
* @param string $password
*/
public function setCredentials($login, $password)
{
$this->_login = $login;
$this->_password = $password;
}
/**
* Define secret key for alternative authentication
*
* @param string $secretKey
*/
public function setSecretKey($secretKey)
{
$this->_secretKey = $secretKey;
}
/**
* Perform API request
*
* @param string $request
*/
public function request($request)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$this->_protocol://$this->_host:$this->_port/enterprise/control/agent.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->_getHeaders());
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
/**
* Retrieve list of headers needed for request
*
* @return array
*/
private function _getHeaders()
{
$headers = array(
"Content-Type: text/xml",
"HTTP_PRETTY_PRINT: TRUE",
);
if ($this->_secretKey) {
$headers[] = "KEY: $this->_secretKey";
} else {
$headers[] = "HTTP_AUTH_LOGIN: $this->_login";
$headers[] = "HTTP_AUTH_PASSWD: $this->_password";
}
return $headers;
}
}
ftp.php (only for testing-purposes, will be integrated in existing product when working)
PHP:
<?php
require_once("PleskApiClient.php");
$_plesk = new PleskApiClient();
$_plesk->setSecretKey("79af145c-fd2d-dbe8-ecc7-33324d079de8");
function listFtpUsers() {
$xml = new DomDocument("1.0", "UTF-8");
$xml->formatOutput = true;
$packet = $xml->createElement("packet");
$packet->setAttribute("version", "1.6.6.0");
$xml->appendChild($packet);
$ftp = $xml->createElement("ftp-user");
$packet->appendChild($ftp);
$get = $xml->createElement("get");
$ftp->appendChild($get);
$get->appendChild($xml->createElement("filter"));
return $xml;
}
function createFtpUser($user, $password, $domain) {
$xml = new DomDocument("1.0", "UTF-8");
$xml->formatOutput = true;
$packet = $xml->createElement("packet");
$packet->setAttribute("version", "1.6.6.0");
$xml->appendChild($packet);
$ftp = $xml->createElement("ftp-user");
$packet->appendChild($ftp);
$add = $xml->createElement("add");
$ftp->appendChild($add);
$add->appendChild($xml->createElement("name", $user));
$add->appendChild($xml->createElement("password", $password));
$add->appendChild($xml->createElement("webspace-name", $domain));
return $xml;
}
function deleteFtpUser($user) {
$xml = new DomDocument("1.0", "UTF-8");
$xml->formatOutput = true;
$packet = $xml->createElement("packet");
$packet->setAttribute("version", "1.6.6.0");
$xml->appendChild($packet);
$ftp = $xml->createElement("ftp-user");
$packet->appendChild($ftp);
$del = $xml->createElement("del");
$ftp->appendChild($del);
$filter = $xml->createElement("filter");
$del->appendChild($filter);
$name = $xml->createElement("name", $user);
$filter->appendChild($name);
return $xml;
}
?>
<html>
<head>
<title>Plesk API FTP-users tests</title>
<body>
<input type="button" name="create" id="user_list" value="List users" />
<input type="button" name="create" id="user_create" value="Create user" />
<input type="button" name="delete" id="user_delete" value="Delete user" />
<?php
if(isset($_GET["action"]) && $_GET["action"] != "") {
if($_GET["action"] == "create") {
$createUserPacket = createFtpUser("test", "password", "example.com");
echo($createUserPacket->saveXML());
echo $_plesk->request($createUserPacket->saveXML());
} elseif($_GET["action"] == "delete") {
$deleteUserPacket = deleteFtpUser("test");
echo($deleteUserPacket->saveXML());
echo $_plesk->request($deleteUserPacket->saveXML());
} elseif($_GET["action"] == "list") {
$listUserPacket = listFtpUsers();
echo($listUserPacket->saveXML());
echo $_plesk->request($listUserPacket->saveXML());
}
}
?>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#user_list").on("click", function() {
location.href = "<?php echo($_SERVER["PHP_SELF"]); ?>?action=list";
});
$("#user_create").on("click", function() {
location.href = "<?php echo($_SERVER["PHP_SELF"]); ?>?action=create";
});
$("#user_delete").on("click", function() {
location.href = "<?php echo($_SERVER["PHP_SELF"]); ?>?action=delete";
});
});
</script>
</body>
</html>
Did I miss something?
I'm working with PHP 5.3 (FastCGI) on Plesk 12 on CentOS 6.6 x64.