Ticket #3734: Convert remaining xmlrpc_client.inc consumers to use XML_RPC2

This commit is contained in:
Renato Botelho 2016-08-16 19:12:50 -03:00
parent 80f38d81f0
commit 200728c95f
2 changed files with 163 additions and 119 deletions

View File

@ -31,21 +31,22 @@ if (!function_exists('captiveportal_syslog')) {
function xmlrpc_sync_voucher_expire($vouchers, $syncip, $port, $password, $username) {
global $g, $config, $cpzone;
require_once("xmlrpc.inc");
require_once("XML/RPC2/Client.php");
$protocol = "http";
if (is_array($config['system']) && is_array($config['system']['webgui']) && !empty($config['system']['webgui']['protocol']) &&
if (is_array($config['system']) &&
is_array($config['system']['webgui']) &&
!empty($config['system']['webgui']['protocol']) &&
$config['system']['webgui']['protocol'] == "https") {
$protocol = "https";
}
if ($protocol == "https" || $port == "443") {
$url = "https://{$syncip}";
$url = "https://{$syncip}:{$port}";
} else {
$url = "http://{$syncip}";
$url = "http://{$syncip}:{$port}";
}
/* Construct code that is run on remote machine */
$method = 'pfsense.exec_php';
$execcmd = <<<EOF
global \$cpzone;
require_once('/etc/inc/captiveportal.inc');
@ -55,39 +56,52 @@ function xmlrpc_sync_voucher_expire($vouchers, $syncip, $port, $password, $usern
EOF;
/* assemble xmlrpc payload */
$params = array(
XML_RPC_encode($password),
XML_RPC_encode($execcmd)
$options = array(
'prefix' => 'pfsense.',
'sslverify' => false,
'connectionTimeout' => 240
);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url . ":" . $port));
$msg = new XML_RPC_Message($method, $params);
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$cli->setCredentials($username, $password);
$resp = $cli->send($msg, "250");
if (!is_object($resp)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url . ":" . $port);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url));
$cli = XML_RPC2_Client::create($url, $options);
if (!is_object($cli)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url);
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return false;
}
try {
$resp = $cli->exec_php($password, $execcmd);
} catch (XML_RPC2_FaultException $e) {
// The XMLRPC server returns a XMLRPC error
$error = 'Exception calling XMLRPC method exec_php #' . $e->getFaultCode() . ' : ' . $e->getFaultString();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
return false;
} elseif ($resp->faultCode()) {
$error = sprintf(gettext('An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with %1$s - Code %2$s'), $url . ":" . $port, $resp->faultCode() . ": " . $resp->faultString());
} catch (Exception $e) {
// Other errors (HTTP or networking problems...)
$error = 'Exception calling XMLRPC method exec_php #' . $e->getMessage();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
file_notice("CaptivePortalVoucherSync", $error, gettext("Error code received"), "");
return false;
} else {
log_error(sprintf(gettext("CaptivePortalVoucherSync XMLRPC reload data success with %s (pfsense.exec_php)."), $url . ":" . $port));
}
$toreturn = XML_RPC_Decode($resp->value());
if (!is_array($resp) && trim($resp) == "Authentication failed") {
$error = "An authentication failure occurred while trying to access {$url} (exec_php).";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return false;
}
return $toreturn;
log_error(sprintf(gettext("CaptivePortalVoucherSync XMLRPC reload data success with %s (pfsense.exec_php)."), $url));
return $resp;
}
function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $username, $term_cause = 1, $stop_time = null) {
global $g, $config, $cpzone;
require_once("xmlrpc.inc");
require_once("XML/RPC2/Client.php");
$protocol = "http";
if (is_array($config['system']) && is_array($config['system']['webgui']) && !empty($config['system']['webgui']['protocol']) &&
@ -95,15 +109,14 @@ function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $user
$protocol = "https";
}
if ($protocol == "https" || $port == "443") {
$url = "https://{$syncip}";
$url = "https://{$syncip}:{$port}";
} else {
$url = "http://{$syncip}";
$url = "http://{$syncip}:{$port}";
}
/* Construct code that is run on remote machine */
$dbent_str = serialize($dbent);
$tmp_stop_time = (isset($stop_time)) ? $stop_time : "null";
$method = 'pfsense.exec_php';
$execcmd = <<<EOF
global \$cpzone;
require_once('/etc/inc/captiveportal.inc');
@ -115,39 +128,52 @@ function xmlrpc_sync_voucher_disconnect($dbent, $syncip, $port, $password, $user
EOF;
/* assemble xmlrpc payload */
$params = array(
XML_RPC_encode($password),
XML_RPC_encode($execcmd)
$options = array(
'prefix' => 'pfsense.',
'sslverify' => false,
'connectionTimeout' => 240
);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url . ":" . $port));
$msg = new XML_RPC_Message($method, $params);
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$cli->setCredentials($username, $password);
$resp = $cli->send($msg, "250");
if (!is_object($resp)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url . ":" . $port);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url));
$cli = XML_RPC2_Client::create($url, $options);
if (!is_object($cli)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url);
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return false;
}
try {
$resp = $cli->exec_php($password, $execcmd);
} catch (XML_RPC2_FaultException $e) {
// The XMLRPC server returns a XMLRPC error
$error = 'Exception calling XMLRPC method exec_php #' . $e->getFaultCode() . ' : ' . $e->getFaultString();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
return false;
} elseif ($resp->faultCode()) {
$error = sprintf(gettext('An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with %1$s - Code %2$s'), $url . ":" . $port, $resp->faultCode() . ": " . $resp->faultString());
} catch (Exception $e) {
// Other errors (HTTP or networking problems...)
$error = 'Exception calling XMLRPC method exec_php #' . $e->getMessage();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
file_notice("CaptivePortalVoucherSync", $error, gettext("Error code received"), "");
return false;
} else {
log_error(sprintf(gettext("CaptivePortalVoucherSync XMLRPC reload data success with %s (pfsense.exec_php)."), $url . ":" . $port));
}
$toreturn = XML_RPC_Decode($resp->value());
if (!is_array($resp) && trim($resp) == "Authentication failed") {
$error = "An authentication failure occurred while trying to access {$url} (exec_php).";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return false;
}
return $toreturn;
log_error(sprintf(gettext("CaptivePortalVoucherSync XMLRPC reload data success with %s (pfsense.exec_php)."), $url));
return $resp;
}
function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password, $username) {
global $g, $config, $cpzone;
require_once("xmlrpc.inc");
require_once("XML/RPC2/Client.php");
$protocol = "http";
if (is_array($config['system']) && is_array($config['system']['webgui']) && !empty($config['system']['webgui']['protocol']) &&
@ -155,13 +181,12 @@ function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password,
$protocol = "https";
}
if ($protocol == "https" || $port == "443") {
$url = "https://{$syncip}";
$url = "https://{$syncip}:{$port}";
} else {
$url = "http://{$syncip}";
$url = "http://{$syncip}:{$port}";
}
/* Construct code that is run on remote machine */
$method = 'pfsense.exec_php';
$execcmd = <<<EOF
global \$cpzone;
require_once('/etc/inc/voucher.inc');
@ -174,45 +199,58 @@ function xmlrpc_sync_used_voucher($voucher_received, $syncip, $port, $password,
EOF;
/* assemble xmlrpc payload */
$params = array(
XML_RPC_encode($password),
XML_RPC_encode($execcmd)
$options = array(
'prefix' => 'pfsense.',
'sslverify' => false,
'connectionTimeout' => 240
);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url . ":" . $port));
$msg = new XML_RPC_Message($method, $params);
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$cli->setCredentials($username, $password);
$resp = $cli->send($msg, "250");
if (!is_object($resp)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url . ":" . $port);
log_error(sprintf(gettext("Captive Portal Voucher XMLRPC sync data %s."), $url));
$cli = XML_RPC2_Client::create($url, $options);
if (!is_object($cli)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url);
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return null;
}
try {
$resp = $cli->exec_php($password, $execcmd);
} catch (XML_RPC2_FaultException $e) {
// The XMLRPC server returns a XMLRPC error
$error = 'Exception calling XMLRPC method exec_php #' . $e->getFaultCode() . ' : ' . $e->getFaultString();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
return null; // $timeleft
} elseif ($resp->faultCode()) {
$error = sprintf(gettext('An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with %1$s - Code %2$s'), $url . ":" . $port, $resp->faultCode() . ": " . $resp->faultString());
return null;
} catch (Exception $e) {
// Other errors (HTTP or networking problems...)
$error = 'Exception calling XMLRPC method exec_php #' . $e->getMessage();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Error code received", "");
return null; // $timeleft
} else {
log_error(sprintf(gettext("CaptivePortalVoucherSync XMLRPC reload data success with %s (pfsense.exec_php)."), $url . ":" . $port));
file_notice("CaptivePortalVoucherSync", $error, gettext("Error code received"), "");
return null;
}
$toreturn = XML_RPC_Decode($resp->value());
if (!is_array($resp) && trim($resp) == "Authentication failed") {
$error = "An authentication failure occurred while trying to access {$url} (exec_php).";
log_error($error);
file_notice("sync_settings", $error, "Settings Sync", "");
return null;
}
if (!is_array($config['voucher'])) {
$config['voucher'] = array();
}
if (is_array($toreturn['voucher']) && is_array($toreturn['voucher']['roll'])) {
$config['voucher'][$cpzone]['roll'] = $toreturn['voucher']['roll'];
if (is_array($resp['voucher']['roll'])) {
$config['voucher'][$cpzone]['roll'] = $resp['voucher']['roll'];
write_config(sprintf(gettext("Captive Portal Voucher database synchronized with %s"), $url));
voucher_configure_zone(true);
unset($toreturn['voucher']);
} else if (!isset($toreturn['timeleft'])) {
unset($resp['voucher']);
} else if (!isset($resp['timeleft'])) {
return null;
}
return $toreturn['timeleft'];
return $resp['timeleft'];
}
function voucher_expire($voucher_received) {

View File

@ -291,7 +291,7 @@ if ($_POST) {
if ($newvoucher['vouchersyncpass'] && $newvoucher['vouchersyncusername'] &&
$newvoucher['vouchersyncport'] && $newvoucher['vouchersyncdbip']) {
// Synchronize the voucher DB from the master node
require_once("xmlrpc.inc");
require_once("XML/RPC2/Client.php");
$protocol = "http";
if (is_array($config['system']) && is_array($config['system']['webgui']) && !empty($config['system']['webgui']['protocol']) &&
@ -303,6 +303,7 @@ if ($_POST) {
} else {
$url = "http://{$newvoucher['vouchersyncdbip']}";
}
$url .= ":{$newvoucher['vouchersyncport']}/xmlrpc.php";
$execcmd = <<<EOF
\$toreturn = array();
@ -311,71 +312,76 @@ if ($_POST) {
EOF;
/* assemble xmlrpc payload */
$params = array(
XML_RPC_encode($newvoucher['vouchersyncpass']),
XML_RPC_encode($execcmd)
$options = array(
'prefix' => 'pfsense.',
'sslverify' => false,
'connectionTimeout' => 240
);
$port = $newvoucher['vouchersyncport'];
log_error(sprintf(gettext("voucher XMLRPC sync data %s:%d"), $url, $port));
$msg = new XML_RPC_Message('pfsense.exec_php', $params);
$cli = new XML_RPC_Client('/xmlrpc.php', $url, $port);
$cli->setCredentials($newvoucher['vouchersyncusername'], $newvoucher['vouchersyncpass']);
$resp = $cli->send($msg, "250");
if (!is_object($resp)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s:%d (pfsense.exec_php)."), $url, $port);
log_error(sprintf(gettext("voucher XMLRPC sync data %s"), $url));
$cli = XML_RPC2_Client::create($url, $options);
if (!is_object($cli)) {
$error = sprintf(gettext("A communications error occurred while attempting CaptivePortalVoucherSync XMLRPC sync with %s (pfsense.exec_php)."), $url);
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, gettext("Communications error occurred"), "");
$input_errors[] = $error;
} elseif ($resp->faultCode()) {
$cli->setDebug(1);
$resp = $cli->send($msg, "250");
$error = sprintf(gettext("An error code was received while attempting CaptivePortalVoucherSync XMLRPC sync with %s:%d - Code %d: %s"), $url, $port, $resp->faultCode(), $resp->faultString());
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, gettext("Error code received"), "");
file_notice("sync_settings", $error, "Settings Sync", "");
$input_errors[] = $error;
} else {
log_error(sprintf(gettext("The Captive Portal voucher database has been synchronized with %s:%d (pfsense.exec_php)."), $url, $port));
try {
$resp = $cli->exec_php($newvoucher['vouchersyncpass'], $execcmd);
} catch (XML_RPC2_FaultException $e) {
// The XMLRPC server returns a XMLRPC error
$error = 'Exception calling XMLRPC method exec_php #' . $e->getFaultCode() . ' : ' . $e->getFaultString();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, "Communications error occurred", "");
$input_errors[] = $error;
} catch (Exception $e) {
// Other errors (HTTP or networking problems...)
$error = 'Exception calling XMLRPC method exec_php #' . $e->getMessage();
log_error($error);
file_notice("CaptivePortalVoucherSync", $error, gettext("Error code received"), "");
$input_errors[] = $error;
}
}
if (!$input_errors) {
$toreturn = XML_RPC_Decode($resp->value());
if (!is_array($toreturn)) {
if ($toreturn == "Authentication failed") {
if (!is_array($resp)) {
if ($resp == "Authentication failed") {
$input_errors[] = gettext("Could not synchronize the voucher database: Authentication Failed.");
}
} else {
log_error(sprintf(gettext("The Captive Portal voucher database has been synchronized with %s (pfsense.exec_php)."), $url));
// If we received back the voucher roll and other information then store it.
if ($toreturn['voucher']['roll']) {
$newvoucher['roll'] = $toreturn['voucher']['roll'];
if ($resp['voucher']['roll']) {
$newvoucher['roll'] = $resp['voucher']['roll'];
}
if ($toreturn['voucher']['rollbits']) {
$newvoucher['rollbits'] = $toreturn['voucher']['rollbits'];
if ($resp['voucher']['rollbits']) {
$newvoucher['rollbits'] = $resp['voucher']['rollbits'];
}
if ($toreturn['voucher']['ticketbits']) {
$newvoucher['ticketbits'] = $toreturn['voucher']['ticketbits'];
if ($resp['voucher']['ticketbits']) {
$newvoucher['ticketbits'] = $resp['voucher']['ticketbits'];
}
if ($toreturn['voucher']['checksumbits']) {
$newvoucher['checksumbits'] = $toreturn['voucher']['checksumbits'];
if ($resp['voucher']['checksumbits']) {
$newvoucher['checksumbits'] = $resp['voucher']['checksumbits'];
}
if ($toreturn['voucher']['magic']) {
$newvoucher['magic'] = $toreturn['voucher']['magic'];
if ($resp['voucher']['magic']) {
$newvoucher['magic'] = $resp['voucher']['magic'];
}
if ($toreturn['voucher']['exponent']) {
$newvoucher['exponent'] = $toreturn['voucher']['exponent'];
if ($resp['voucher']['exponent']) {
$newvoucher['exponent'] = $resp['voucher']['exponent'];
}
if ($toreturn['voucher']['publickey']) {
$newvoucher['publickey'] = $toreturn['voucher']['publickey'];
if ($resp['voucher']['publickey']) {
$newvoucher['publickey'] = $resp['voucher']['publickey'];
}
if ($toreturn['voucher']['privatekey']) {
$newvoucher['privatekey'] = $toreturn['voucher']['privatekey'];
if ($resp['voucher']['privatekey']) {
$newvoucher['privatekey'] = $resp['voucher']['privatekey'];
}
if ($toreturn['voucher']['descrmsgnoaccess']) {
$newvoucher['descrmsgnoaccess'] = $toreturn['voucher']['descrmsgnoaccess'];
if ($resp['voucher']['descrmsgnoaccess']) {
$newvoucher['descrmsgnoaccess'] = $resp['voucher']['descrmsgnoaccess'];
}
if ($toreturn['voucher']['descrmsgexpired']) {
$newvoucher['descrmsgexpired'] = $toreturn['voucher']['descrmsgexpired'];
if ($resp['voucher']['descrmsgexpired']) {
$newvoucher['descrmsgexpired'] = $resp['voucher']['descrmsgexpired'];
}
$savemsg = sprintf(gettext('Voucher database has been synchronized from %1$s:%2$s'), $url, $port);
$savemsg = sprintf(gettext('Voucher database has been synchronized from %1$s'), $url);
$config['voucher'][$cpzone] = $newvoucher;
write_config();