Project

General

Profile

Download (6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlrpc_client.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2016 Electric Sheep Fencing
7
 * Copyright (c) 2016-2024 Rubicon Communications, LLC (Netgate)
8
 * 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
class pfsense_xmlrpc_client {
26

    
27
	private $username, $password, $url, $logurl, $filenotice, $error;
28

    
29
	public function __construct() {
30
		global $config;
31
		$hasync = config_get_path('hasync', []);
32
		/* XMLRPC sync is not configured, nothing to do.
33
		 * https://redmine.pfsense.org/issues/14182 */
34
		if (empty($hasync)) {
35
			$this->error = "XMLRPC Synchronization is not configured.";
36
			return;
37
		}
38

    
39
		if (empty($hasync['username'])) {
40
			$username = "admin";
41
		} else {
42
			$username = $hasync['username'];
43
		}
44
		/* if port is empty lets rely on the protocol selection */
45
		$port = config_get_path('system/webgui/port');
46
		if (empty($port)) {
47
			if ($config['system']['webgui']['protocol'] == "http") {
48
				$port = "80";
49
			} else {
50
				$port = "443";
51
			}
52
		}
53
		$this->setConnectionData($hasync['synchronizetoip'], $port, $username, $hasync['password']);
54
	}
55

    
56
	public function setConnectionData($syncip, $port, $username, $password, $scheme = "") {
57
		global $config;
58
		$this->username = $username;
59
		$this->password = $password;
60
		$this->filenotice = "sync_settings";
61
		if (empty($scheme)) {
62
			$scheme = "http";
63
			if ($port == "443") {
64
				$scheme = "https";
65
			} else if (is_array($config['system']) &&
66
				is_array($config['system']['webgui']) &&
67
				!empty($config['system']['webgui']['protocol']) &&
68
				$config['system']['webgui']['protocol'] == "https") {
69
				$scheme = "https";
70
			}
71
		}
72
		if (is_ipaddrv6($syncip)) {
73
			$syncip = "[{$syncip}]";
74
		}
75
		$user = rawurlencode($this->username);
76
		$pass = rawurlencode($this->password);
77

    
78
		$this->logurl = "{$scheme}://{$syncip}:{$port}/xmlrpc.php";
79
		$this->url = "{$scheme}://{$user}:{$pass}@{$syncip}:{$port}/xmlrpc.php";
80
	}
81

    
82
	public function set_noticefile($noticefile) {
83
		$this->filenotice = $noticefile;
84
	}
85

    
86
	private function xmlrpc_internal($method, $parameter, $timeout = 240) {
87
		/* XMLRPC sync is not configured, nothing to do.
88
		 * https://redmine.pfsense.org/issues/14182 */
89
		if (empty($this->url) ||
90
		    empty($this->logurl)) {
91
			$this->error = "XMLRPC Synchronization is not configured.";
92
			return;
93
		}
94
		$options = array(
95
			'prefix' => 'pfsense.',
96
			'sslverify' => false,
97
			'connectionTimeout' => $timeout
98
		);
99

    
100
		$max_attempts = 4;
101
		$numberofruns = 0;
102
		while ($numberofruns < $max_attempts) {
103
			$numberofruns++;
104
			$this->error = null;
105

    
106
			log_error(sprintf(gettext("Beginning XMLRPC sync data to %s."), $this->logurl));
107
			$cli = XML_RPC2_Client::create($this->url, $options);
108
			if (!is_object($cli)) {
109
				$this->error = sprintf(gettext("A communications error occurred while attempting XMLRPC sync with %s (pfsense.%s)."), $this->log, $method);
110
			}
111
			try {//restore_config_section
112
				$REQUEST_URI = $_SERVER['REQUEST_URI'];
113
				unset($_SERVER['REQUEST_URI']); // force use of 'toText()' when setting XML_RPC2_CurlException message
114
				$resp = $cli->$method($parameter, $timeout);
115
			} catch (XML_RPC2_FaultException $e) {
116
				// The XMLRPC server returns a XMLRPC error
117
				$this->error = "Exception calling XMLRPC method {$method} #" . $e->getFaultCode() . ' : ' . $e->getFaultString();
118
				log_error($this->error);
119
				file_notice($this->filenotice, $this->error, "Communications error occurred", "");
120
			} catch (XML_RPC2_CurlException $e) {
121
				$previouserror = $e->getPrevious();// HTTP_Request2_ConnectionException
122
				if ($previouserror == null) {
123
					// CurlException doesnt get filled with PreviousError,
124
					// however we dont want to show the stacktrace included in the 'message' to non sysadmin users
125
					preg_match("/HTTP_Request2_ConnectionException: (.*) in \/.*/", $e->getMessage(), $errormsg);
126
					if (empty($errormsg) || (is_array($errormsg) && empty($errormsg[1]))) {
127
						$errormsg = $e->getMessage();
128
					}
129
					$this->error = "A communications error occurred while attempting to call XMLRPC method {$method}: {$errormsg}";
130
				} else {
131
					$this->error = "CurlException calling XMLRPC method {$method} #" . $previouserror->getMessage();
132
				}
133
			} catch (Exception $e) {
134
				// Other errors (HTTP or networking problems...)
135
				$this->error = "Exception calling XMLRPC method {$method} # " . $e->getMessage();
136
			} finally {
137
				if (isset($REQUEST_URI)) {
138
					// restore the unset variable to its previous state.
139
					$_SERVER['REQUEST_URI'] = $REQUEST_URI;
140
				}
141
			}
142

    
143
			if (!is_array($resp) && trim($resp) == "Authentication failed") {
144
				$this->error = "An authentication failure occurred while trying to access {$this->logurl} ({$method}).";
145
			}
146
			if (empty($this->error)) {
147
				log_error(sprintf(gettext("XMLRPC reload data success with %s (pfsense.{$method})."), $this->logurl));
148
				return $resp;
149
			} elseif ($numberofruns < $max_attempts) {
150
				log_error(sprintf(gettext("Retrying XMLRPC Request due to error: %s"), $this->error));
151
				sleep(1);
152
			} else {
153
				log_error($this->error);
154
				file_notice($this->filenotice, $this->error, "XMLRPC Error", "");
155
			}
156
		}
157
		return null;
158
	}
159

    
160
	public function xmlrpc_exec_php($execcmd, $timeout = 240) {
161
		$resp = $this->xmlrpc_internal("exec_php", $execcmd, $timeout);
162
		return $resp;
163
	}
164

    
165
	public function xmlrpc_method($method, $parameter = "", $timeout = 240) {
166
		$resp = $this->xmlrpc_internal($method, $parameter, $timeout);
167
		return $resp;
168
	}
169

    
170
	public function get_error() {
171
		return $this->error;
172
	}
173

    
174
	public function getUrl() {
175
		return $this->logurl;
176
	}
177
}
(61-61/61)