Project

General

Profile

Download (5.56 KB) Statistics
| Branch: | Tag: | Revision:
1 a8620841 PiBa-NL
<?php
2
/*
3 b2a8595c Renato Botelho do Couto
 * xmlrpc_client.inc
4 a8620841 PiBa-NL
 *
5
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2016 Electric Sheep Fencing
7 8f2f85c3 Luiz Otavio O Souza
 * Copyright (c) 2016-2022 Rubicon Communications, LLC (Netgate)
8 a8620841 PiBa-NL
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
require_once("XML/RPC2/Client.php");
24
25 e3b0eeb2 PiBa-NL
class pfsense_xmlrpc_client {
26 179377b0 robjarsen
27 dc5f639f PiBa-NL
	private $username, $password, $url, $logurl, $filenotice, $error;
28 179377b0 robjarsen
29 e3b0eeb2 PiBa-NL
	public function __construct() {
30
		global $config;
31
		$hasync = $config['hasync'];
32
33
		if (empty($hasync['username'])) {
34
			$username = "admin";
35
		} else {
36
			$username = $hasync['username'];
37
		}
38
		/* if port is empty lets rely on the protocol selection */
39
		$port = $config['system']['webgui']['port'];
40
		if (empty($port)) {
41
			if ($config['system']['webgui']['protocol'] == "http") {
42
				$port = "80";
43
			} else {
44
				$port = "443";
45
			}
46
		}
47
		$this->setConnectionData($hasync['synchronizetoip'], $port, $username, $hasync['password']);
48
	}
49 dfbd0052 PiBa-NL
50
	public function setConnectionData($syncip, $port, $username, $password, $scheme = "") {
51 a8620841 PiBa-NL
		global $config;
52
		$this->username = $username;
53
		$this->password = $password;
54
		$this->filenotice = "sync_settings";
55 dfbd0052 PiBa-NL
		if (empty($scheme)) {
56
			$scheme = "http";
57
			if ($port == "443") {
58
				$scheme = "https";
59
			} else if (is_array($config['system']) &&
60
				is_array($config['system']['webgui']) &&
61
				!empty($config['system']['webgui']['protocol']) &&
62
				$config['system']['webgui']['protocol'] == "https") {
63
				$scheme = "https";
64
			}
65 a8620841 PiBa-NL
		}
66
		if (is_ipaddrv6($syncip)) {
67
			$syncip = "[{$syncip}]";
68
		}
69 2ec76321 PiBa-NL
		$user = rawurlencode($this->username);
70
		$pass = rawurlencode($this->password);
71 179377b0 robjarsen
72 dc5f639f PiBa-NL
		$this->logurl = "{$scheme}://{$syncip}:{$port}/xmlrpc.php";
73
		$this->url = "{$scheme}://{$user}:{$pass}@{$syncip}:{$port}/xmlrpc.php";
74 a8620841 PiBa-NL
	}
75 dfbd0052 PiBa-NL
76
	public function set_noticefile($noticefile) {
77 a8620841 PiBa-NL
		$this->filenotice = $noticefile;
78
	}
79 179377b0 robjarsen
80 dfbd0052 PiBa-NL
	private function xmlrpc_internal($method, $parameter, $timeout = 240) {
81 a8620841 PiBa-NL
		$options = array(
82
			'prefix' => 'pfsense.',
83
			'sslverify' => false,
84
			'connectionTimeout' => $timeout
85
		);
86 179377b0 robjarsen
87 9455c6ef jim-p
		$max_attempts = 4;
88 a8620841 PiBa-NL
		$numberofruns = 0;
89 9455c6ef jim-p
		while ($numberofruns < $max_attempts) {
90 a8620841 PiBa-NL
			$numberofruns++;
91 9455c6ef jim-p
			$this->error = null;
92 a8620841 PiBa-NL
93 bf335b2b Renato Botelho do Couto
			log_error(sprintf(gettext("Beginning XMLRPC sync data to %s."), $this->logurl));
94 a8620841 PiBa-NL
			$cli = XML_RPC2_Client::create($this->url, $options);
95
			if (!is_object($cli)) {
96 dc5f639f PiBa-NL
				$this->error = sprintf(gettext("A communications error occurred while attempting XMLRPC sync with %s (pfsense.%s)."), $this->log, $method);
97 a8620841 PiBa-NL
			}
98
			try {//restore_config_section
99 dfbd0052 PiBa-NL
				$REQUEST_URI = $_SERVER['REQUEST_URI'];
100
				unset($_SERVER['REQUEST_URI']); // force use of 'toText()' when setting XML_RPC2_CurlException message
101 4f26f187 Viktor G
				$resp = $cli->$method($parameter, $timeout);
102 a8620841 PiBa-NL
			} catch (XML_RPC2_FaultException $e) {
103
				// The XMLRPC server returns a XMLRPC error
104 4d7522bf PiBa-NL
				$this->error = "Exception calling XMLRPC method {$method} #" . $e->getFaultCode() . ' : ' . $e->getFaultString();
105 a8620841 PiBa-NL
				log_error($this->error);
106
				file_notice($this->filenotice, $this->error, "Communications error occurred", "");
107 9455c6ef jim-p
			} catch (XML_RPC2_CurlException $e) {
108 dfbd0052 PiBa-NL
				$previouserror = $e->getPrevious();// HTTP_Request2_ConnectionException
109
				if ($previouserror == null) {
110
					// CurlException doesnt get filled with PreviousError,
111
					// however we dont want to show the stacktrace included in the 'message' to non sysadmin users
112 593f0521 jim-p
					preg_match("/HTTP_Request2_ConnectionException: (.*) in \/.*/", $e->getMessage(), $errormsg);
113 9455c6ef jim-p
					if (empty($errormsg) || (is_array($errormsg) && empty($errormsg[1]))) {
114
						$errormsg = $e->getMessage();
115
					}
116
					$this->error = "A communications error occurred while attempting to call XMLRPC method {$method}: {$errormsg}";
117 dfbd0052 PiBa-NL
				} else {
118
					$this->error = "CurlException calling XMLRPC method {$method} #" . $previouserror->getMessage();
119
				}
120 a8620841 PiBa-NL
			} catch (Exception $e) {
121
				// Other errors (HTTP or networking problems...)
122 4d7522bf PiBa-NL
				$this->error = "Exception calling XMLRPC method {$method} # " . $e->getMessage();
123 dfbd0052 PiBa-NL
			} finally {
124
				if (isset($REQUEST_URI)) {
125
					// restore the unset variable to its previous state.
126
					$_SERVER['REQUEST_URI'] = $REQUEST_URI;
127
				}
128 a8620841 PiBa-NL
			}
129
130
			if (!is_array($resp) && trim($resp) == "Authentication failed") {
131 dc5f639f PiBa-NL
				$this->error = "An authentication failure occurred while trying to access {$this->logurl} ({$method}).";
132 9455c6ef jim-p
			}
133
			if (empty($this->error)) {
134
				log_error(sprintf(gettext("XMLRPC reload data success with %s (pfsense.{$method})."), $this->logurl));
135
				return $resp;
136
			} elseif ($numberofruns < $max_attempts) {
137
				log_error(sprintf(gettext("Retrying XMLRPC Request due to error: %s"), $this->error));
138
				sleep(1);
139
			} else {
140 a8620841 PiBa-NL
				log_error($this->error);
141 9455c6ef jim-p
				file_notice($this->filenotice, $this->error, "XMLRPC Error", "");
142 a8620841 PiBa-NL
			}
143
		}
144
		return null;
145
	}
146 179377b0 robjarsen
147 dfbd0052 PiBa-NL
	public function xmlrpc_exec_php($execcmd, $timeout = 240) {
148 a8620841 PiBa-NL
		$resp = $this->xmlrpc_internal("exec_php", $execcmd, $timeout);
149
		return $resp;
150
	}
151 179377b0 robjarsen
152 dfbd0052 PiBa-NL
	public function xmlrpc_method($method, $parameter = "", $timeout = 240) {
153 a8620841 PiBa-NL
		$resp = $this->xmlrpc_internal($method, $parameter, $timeout);
154
		return $resp;
155
	}
156 179377b0 robjarsen
157 dfbd0052 PiBa-NL
	public function get_error() {
158 a8620841 PiBa-NL
		return $this->error;
159
	}
160 179377b0 robjarsen
161 dfbd0052 PiBa-NL
	public function getUrl() {
162 dc5f639f PiBa-NL
		return $this->logurl;
163 dfbd0052 PiBa-NL
	}
164 b8f91b7c Luiz Souza
}