diff --git a/adapters/stormshield/connect.php b/adapters/stormshield/connect.php index 20a887f3..b5b3533b 100644 --- a/adapters/stormshield/connect.php +++ b/adapters/stormshield/connect.php @@ -5,6 +5,9 @@ require_once 'smsd/sms_common.php'; require_once 'smsd/expect.php'; require_once 'smsd/generic_connection.php'; + +require_once load_once('stormshield', 'nsrpc.php'); + require_once "$db_objects"; class connect extends GenericConnection { @@ -21,6 +24,7 @@ class connect extends GenericConnection { private $response; private $cookie; private $session_id; + private $curl_cmd; public function __construct($ip = null, $login = null, $passwd = null, $admin_password = null, $port = null) { @@ -117,15 +121,6 @@ public function send($origin, $rest_cmd) { $url = "https://{$this->sd_ip_config}/{$rest_path}"; - $headers = ''; - foreach ($this->http_header_list as $header) { - $H = trim($header); - $headers .= " -H '{$H}'"; - } - - // for debug - $curl_cmd = "curl -X {$http_op} {$headers} --connect-timeout {$this->conn_timeout} --max-time {$this->conn_timeout} -k '{$url}'"; - if (count($cmd_list) > 2) { if (isset($this->session_id)) { $payload = $cmd_list[2]; @@ -157,14 +152,25 @@ public function send($origin, $rest_cmd) { $rest_payload = ''; } - $curl_cmd .= " --data-raw '{$rest_payload}'"; + $this->execute_curl_command($origin, $http_op, $url, $rest_payload); - debug_dump($curl_cmd, "HTTP REQUEST:\n"); - $this->execute_curl_command($origin, $http_op, $url, $rest_payload, $curl_cmd); - debug_dump($this->response, "HTTP RESPONSE:\n"); + $ret = is_error_xml($this->response); + if ($ret !== false) + { + throw new SmsException("Response to API {$this->curl_cmd} Failed: \n$ret", ERR_SD_CMDFAILED, $origin); + } } - private function execute_curl_command($origin, $http_op, $url, $rest_payload, $curl_cmd) { + private function execute_curl_command($origin, $http_op, $url, $rest_payload) { + + // for debug + $headers = ''; + foreach ($this->http_header_list as $h) { + $H = trim($h); + $headers .= " -H '{$H}'"; + } + $this->curl_cmd = "curl -X {$http_op} {$headers} --connect-timeout {$this->conn_timeout} --max-time {$this->conn_timeout} -k '{$url}' --data-raw '{$rest_payload}'"; + debug_dump($this->curl_cmd, "HTTP REQUEST:\n"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); @@ -206,7 +212,7 @@ private function execute_curl_command($origin, $http_op, $url, $rest_payload, $c if ($http_code < 200 || $http_code > 209) { $cmd_quote = str_replace("\"", "'", $body); $cmd_return = str_replace("\n", "", $cmd_quote); - throw new SmsException("Call to API {$curl_cmd} Failed, header = $header, $cmd_return error", ERR_SD_CMDFAILED, $origin); + throw new SmsException("Call to API {$this->curl_cmd} Failed, header = $header, $cmd_return error", ERR_SD_CMDFAILED, $origin); } if (!isset($this->cookie)) { @@ -221,9 +227,10 @@ private function execute_curl_command($origin, $http_op, $url, $rest_payload, $c else { if ($http_code != 204) { - throw new SmsException ("$origin: Response to API {$curl_cmd} Failed, expected json received empty response, header $header", ERR_SD_CMDFAILED); + throw new SmsException ("$origin: Response to API {$this->curl_cmd} Failed, expected json received empty response, header $header", ERR_SD_CMDFAILED); } } + debug_dump($this->response, "HTTP RESPONSE:\n"); } } diff --git a/adapters/stormshield/nsrpc.php b/adapters/stormshield/nsrpc.php index f2e805ee..78fcfd08 100644 --- a/adapters/stormshield/nsrpc.php +++ b/adapters/stormshield/nsrpc.php @@ -27,6 +27,22 @@ 206 licence restriction */ +// return code considered as ok +$ok_return_code = array ( + "100" => true, + "103" => true, + "104" => true, + "110" => true, + "111" => true, +); + +// intermediate return code, it is normally followed by another return code +$not_a_return_code = array ( + "101" => true, + "102" => true, +); + + function get_return_codes(&$nsrpc_output) { define("RC_LEN", 3); @@ -73,20 +89,8 @@ function get_return_codes(&$nsrpc_output) */ function is_error(&$nsrpc_output) { - // return code considered as ok - $ok_return_code = array ( - "100" => true, - "103" => true, - "104" => true, - "110" => true, - "111" => true, - ); - - // intermediate return code, it is normaly followed by another return code - $not_a_return_code = array ( - "101" => true, - "102" => true, - ); + global $ok_return_code; + global $not_a_return_code; $rc_list = get_return_codes($nsrpc_output); @@ -105,6 +109,53 @@ function is_error(&$nsrpc_output) return false; } +/* + * Get return codes of a SimpleXMLElement response from the managed entity + * and check if there is an error, stops at the first error + * return an error message if any, false otherwise + */ +function is_error_xml($xml) +{ + global $ok_return_code; + global $not_a_return_code; + + if (empty($xml)) + { + return false; + } + + $return_list = array(); + + if (isset($xml->command)) + { + foreach ($xml->command as $command) + { + if (isset($command->serverd)) + { + foreach ($command->serverd as $serverd) + { + if (isset($serverd['ret'])) + { + $return_list[] = $serverd->attributes(); + $rc = (string)$serverd['ret']; + if (empty($ok_return_code[$rc]) && empty($not_a_return_code[$rc])) + { + $err = ''; + foreach ($return_list as $ret) + { + $err .= "ret={$ret['ret']}, code={$ret['code']}, msg={$ret['msg']} \n"; + } + return $err; + } + } + } + } + } + } + + return false; +} + /* * Get the return code from $nsrpc_output for the command $cmd to see if a reboot is needed * Assume no error, this means is_error() is called before