Project

General

Profile

Download (5.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * xmlrpc_client.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2016 Electric Sheep Fencing
7
 * Copyright (c) 2016-2019 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['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

    
50
	public function setConnectionData($syncip, $port, $username, $password, $scheme = "") {
51
		global $config;
52
		$this->username = $username;
53
		$this->password = $password;
54
		$this->filenotice = "sync_settings";
55
		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
		}
66
		if (is_ipaddrv6($syncip)) {
67
			$syncip = "[{$syncip}]";
68
		}
69
		$user = rawurlencode($this->username);
70
		$pass = rawurlencode($this->password);
71

    
72
		$this->logurl = "{$scheme}://{$syncip}:{$port}/xmlrpc.php";
73
		$this->url = "{$scheme}://{$user}:{$pass}@{$syncip}:{$port}/xmlrpc.php";
74
	}
75

    
76
	public function set_noticefile($noticefile) {
77
		$this->filenotice = $noticefile;
78
	}
79

    
80
	private function xmlrpc_internal($method, $parameter, $timeout = 240) {
81
		$this->error = null;
82
		$options = array(
83
			'prefix' => 'pfsense.',
84
			'sslverify' => false,
85
			'connectionTimeout' => $timeout
86
		);
87

    
88
		$numberofruns = 0;
89
		while ($numberofruns < 2) {
90
			$numberofruns++;
91

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

    
136
			if (!is_array($resp) && trim($resp) == "Authentication failed") {
137
				$this->error = "An authentication failure occurred while trying to access {$this->logurl} ({$method}).";
138
				log_error($this->error);
139
				file_notice($this->filenotice, $this->error, "Settings Sync", "");
140
				continue;
141
			}
142
			log_error(sprintf(gettext("XMLRPC reload data success with %s (pfsense.{$method})."), $this->logurl));
143
			return $resp;
144
		}
145
		return null;
146
	}
147

    
148
	public function xmlrpc_exec_php($execcmd, $timeout = 240) {
149
		$resp = $this->xmlrpc_internal("exec_php", $execcmd, $timeout);
150
		return $resp;
151
	}
152

    
153
	public function xmlrpc_method($method, $parameter = "", $timeout = 240) {
154
		$resp = $this->xmlrpc_internal($method, $parameter, $timeout);
155
		return $resp;
156
	}
157

    
158
	public function get_error() {
159
		return $this->error;
160
	}
161

    
162
	public function getUrl() {
163
		return $this->logurl;
164
	}
165
}
(59-59/59)