Project

General

Profile

Download (5.84 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 a68f7a3d Luiz Otavio O Souza
 * Copyright (c) 2016-2024 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 7fb2954d jim-p
		$hasync = config_get_path('hasync', []);
31
		/* XMLRPC sync is not configured, nothing to do.
32
		 * https://redmine.pfsense.org/issues/14182 */
33
		if (empty($hasync)) {
34
			$this->error = "XMLRPC Synchronization is not configured.";
35
			return;
36
		}
37 e3b0eeb2 PiBa-NL
38
		if (empty($hasync['username'])) {
39
			$username = "admin";
40
		} else {
41
			$username = $hasync['username'];
42
		}
43
		/* if port is empty lets rely on the protocol selection */
44 1e45d13f Christian McDonald
		$port = config_get_path('system/webgui/port');
45 e3b0eeb2 PiBa-NL
		if (empty($port)) {
46 63d6bb4f Marcos Mendoza
			if (config_get_path('system/webgui/protocol') == "http") {
47 e3b0eeb2 PiBa-NL
				$port = "80";
48
			} else {
49
				$port = "443";
50
			}
51
		}
52
		$this->setConnectionData($hasync['synchronizetoip'], $port, $username, $hasync['password']);
53
	}
54 dfbd0052 PiBa-NL
55
	public function setConnectionData($syncip, $port, $username, $password, $scheme = "") {
56 a8620841 PiBa-NL
		$this->username = $username;
57
		$this->password = $password;
58
		$this->filenotice = "sync_settings";
59 dfbd0052 PiBa-NL
		if (empty($scheme)) {
60
			$scheme = "http";
61
			if ($port == "443") {
62
				$scheme = "https";
63 63d6bb4f Marcos Mendoza
			} elseif (config_get_path('system/webgui/protocol') == "https") {
64 dfbd0052 PiBa-NL
				$scheme = "https";
65
			}
66 a8620841 PiBa-NL
		}
67
		if (is_ipaddrv6($syncip)) {
68
			$syncip = "[{$syncip}]";
69
		}
70 2ec76321 PiBa-NL
		$user = rawurlencode($this->username);
71
		$pass = rawurlencode($this->password);
72 179377b0 robjarsen
73 dc5f639f PiBa-NL
		$this->logurl = "{$scheme}://{$syncip}:{$port}/xmlrpc.php";
74
		$this->url = "{$scheme}://{$user}:{$pass}@{$syncip}:{$port}/xmlrpc.php";
75 a8620841 PiBa-NL
	}
76 dfbd0052 PiBa-NL
77
	public function set_noticefile($noticefile) {
78 a8620841 PiBa-NL
		$this->filenotice = $noticefile;
79
	}
80 179377b0 robjarsen
81 dfbd0052 PiBa-NL
	private function xmlrpc_internal($method, $parameter, $timeout = 240) {
82 7fb2954d jim-p
		/* XMLRPC sync is not configured, nothing to do.
83
		 * https://redmine.pfsense.org/issues/14182 */
84
		if (empty($this->url) ||
85
		    empty($this->logurl)) {
86
			$this->error = "XMLRPC Synchronization is not configured.";
87
			return;
88
		}
89 a8620841 PiBa-NL
		$options = array(
90
			'prefix' => 'pfsense.',
91
			'sslverify' => false,
92
			'connectionTimeout' => $timeout
93
		);
94 179377b0 robjarsen
95 9455c6ef jim-p
		$max_attempts = 4;
96 a8620841 PiBa-NL
		$numberofruns = 0;
97 9455c6ef jim-p
		while ($numberofruns < $max_attempts) {
98 a8620841 PiBa-NL
			$numberofruns++;
99 9455c6ef jim-p
			$this->error = null;
100 a8620841 PiBa-NL
101 bf335b2b Renato Botelho do Couto
			log_error(sprintf(gettext("Beginning XMLRPC sync data to %s."), $this->logurl));
102 a8620841 PiBa-NL
			$cli = XML_RPC2_Client::create($this->url, $options);
103
			if (!is_object($cli)) {
104 1ec82c30 Marcos Mendoza
				$this->error = sprintf(gettext("A communications error occurred while attempting XMLRPC sync with %s (pfsense.%s)."), $this->logurl, $method);
105 a8620841 PiBa-NL
			}
106
			try {//restore_config_section
107 dfbd0052 PiBa-NL
				$REQUEST_URI = $_SERVER['REQUEST_URI'];
108
				unset($_SERVER['REQUEST_URI']); // force use of 'toText()' when setting XML_RPC2_CurlException message
109 4f26f187 Viktor G
				$resp = $cli->$method($parameter, $timeout);
110 a8620841 PiBa-NL
			} catch (XML_RPC2_FaultException $e) {
111
				// The XMLRPC server returns a XMLRPC error
112 4d7522bf PiBa-NL
				$this->error = "Exception calling XMLRPC method {$method} #" . $e->getFaultCode() . ' : ' . $e->getFaultString();
113 a8620841 PiBa-NL
				log_error($this->error);
114
				file_notice($this->filenotice, $this->error, "Communications error occurred", "");
115 9455c6ef jim-p
			} catch (XML_RPC2_CurlException $e) {
116 dfbd0052 PiBa-NL
				$previouserror = $e->getPrevious();// HTTP_Request2_ConnectionException
117
				if ($previouserror == null) {
118
					// CurlException doesnt get filled with PreviousError,
119
					// however we dont want to show the stacktrace included in the 'message' to non sysadmin users
120 593f0521 jim-p
					preg_match("/HTTP_Request2_ConnectionException: (.*) in \/.*/", $e->getMessage(), $errormsg);
121 9455c6ef jim-p
					if (empty($errormsg) || (is_array($errormsg) && empty($errormsg[1]))) {
122
						$errormsg = $e->getMessage();
123
					}
124
					$this->error = "A communications error occurred while attempting to call XMLRPC method {$method}: {$errormsg}";
125 dfbd0052 PiBa-NL
				} else {
126
					$this->error = "CurlException calling XMLRPC method {$method} #" . $previouserror->getMessage();
127
				}
128 a8620841 PiBa-NL
			} catch (Exception $e) {
129
				// Other errors (HTTP or networking problems...)
130 4d7522bf PiBa-NL
				$this->error = "Exception calling XMLRPC method {$method} # " . $e->getMessage();
131 dfbd0052 PiBa-NL
			} finally {
132
				if (isset($REQUEST_URI)) {
133
					// restore the unset variable to its previous state.
134
					$_SERVER['REQUEST_URI'] = $REQUEST_URI;
135
				}
136 a8620841 PiBa-NL
			}
137
138
			if (!is_array($resp) && trim($resp) == "Authentication failed") {
139 dc5f639f PiBa-NL
				$this->error = "An authentication failure occurred while trying to access {$this->logurl} ({$method}).";
140 9455c6ef jim-p
			}
141
			if (empty($this->error)) {
142
				log_error(sprintf(gettext("XMLRPC reload data success with %s (pfsense.{$method})."), $this->logurl));
143
				return $resp;
144
			} elseif ($numberofruns < $max_attempts) {
145
				log_error(sprintf(gettext("Retrying XMLRPC Request due to error: %s"), $this->error));
146
				sleep(1);
147
			} else {
148 a8620841 PiBa-NL
				log_error($this->error);
149 9455c6ef jim-p
				file_notice($this->filenotice, $this->error, "XMLRPC Error", "");
150 a8620841 PiBa-NL
			}
151
		}
152
		return null;
153
	}
154 179377b0 robjarsen
155 dfbd0052 PiBa-NL
	public function xmlrpc_exec_php($execcmd, $timeout = 240) {
156 a8620841 PiBa-NL
		$resp = $this->xmlrpc_internal("exec_php", $execcmd, $timeout);
157
		return $resp;
158
	}
159 179377b0 robjarsen
160 dfbd0052 PiBa-NL
	public function xmlrpc_method($method, $parameter = "", $timeout = 240) {
161 a8620841 PiBa-NL
		$resp = $this->xmlrpc_internal($method, $parameter, $timeout);
162
		return $resp;
163
	}
164 179377b0 robjarsen
165 dfbd0052 PiBa-NL
	public function get_error() {
166 a8620841 PiBa-NL
		return $this->error;
167
	}
168 179377b0 robjarsen
169 dfbd0052 PiBa-NL
	public function getUrl() {
170 dc5f639f PiBa-NL
		return $this->logurl;
171 dfbd0052 PiBa-NL
	}
172 b8f91b7c Luiz Souza
}