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