Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / mnet / peer.php
blob158f57791e26424b0fab42636d2fc8fc2a0cd2ba
1 <?php
2 /**
3 * An object to represent lots of information about an RPC-peer machine
5 * @author Donal McMullan donal@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package mnet
9 */
11 require_once($CFG->libdir . '/filelib.php'); // download_file_content() used here
13 class mnet_peer {
15 /** No SSL verification. */
16 const SSL_NONE = 0;
18 /** SSL verification for host. */
19 const SSL_HOST = 1;
21 /** SSL verification for host and peer. */
22 const SSL_HOST_AND_PEER = 2;
24 var $id = 0;
25 var $wwwroot = '';
26 var $ip_address = '';
27 var $name = '';
28 var $public_key = '';
29 var $public_key_expires = 0;
30 var $last_connect_time = 0;
31 var $last_log_id = 0;
32 var $force_theme = 0;
33 var $theme = '';
34 var $applicationid = 1; // Default of 1 == Moodle
35 var $keypair = array();
36 var $error = array();
37 var $bootstrapped = false; // set when the object is populated
39 /** @var int $sslverification The level of SSL verification to apply. */
40 public $sslverification = self::SSL_HOST_AND_PEER;
42 /** @var int deleted status. */
43 public $deleted;
45 /** @var stdClass data from mnet_application table in DB. */
46 public $application;
48 /**
49 * Current SSL public key
51 * MNet need to compare the remote machine's SSL Cert and the public key to warn users of any mismatch.
52 * The property is the remote machine's SSL Cert.
54 * @see admin/mnet/peers.php
55 * @var string
57 public $currentkey;
60 * Fetch information about a peer identified by wwwroot
61 * If information does not preexist in db, collect it together based on
62 * supplied information
64 * @param string $wwwroot - address of peer whose details we want
65 * @param string $pubkey - to use if we add a record to db for new peer
66 * @param int $application - table id - what kind of peer are we talking to
67 * @return bool - indication of success or failure
69 function bootstrap($wwwroot, $pubkey, $application) {
70 global $DB;
72 if (substr($wwwroot, -1, 1) == '/') {
73 $wwwroot = substr($wwwroot, 0, -1);
76 // If a peer record already exists for this address,
77 // load that info and return
78 if ($this->set_wwwroot($wwwroot)) {
79 return true;
82 $hostname = mnet_get_hostname_from_uri($wwwroot);
83 // Get the IP address for that host - if this fails, it will return the hostname string
84 $ip_address = gethostbyname($hostname);
86 // Couldn't find the IP address?
87 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) {
88 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname);
91 $this->name = $wwwroot;
93 // TODO: In reality, this will be prohibitively slow... need another
94 // default - maybe blank string
95 $homepage = download_file_content($wwwroot);
96 if (!empty($homepage)) {
97 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches);
98 if ($count > 0) {
99 $this->name = $matches[1];
103 $this->wwwroot = $wwwroot;
104 $this->ip_address = $ip_address;
105 $this->deleted = 0;
107 $this->application = $DB->get_record('mnet_application', array('name'=>$application));
108 if (empty($this->application)) {
109 $this->application = $DB->get_record('mnet_application', array('name'=>'moodle'));
112 $this->applicationid = $this->application->id;
114 if(empty($pubkey)) {
115 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM);
116 } else {
117 $this->public_key = clean_param($pubkey, PARAM_PEM);
119 $this->public_key_expires = $this->check_common_name($this->public_key);
120 $this->last_connect_time = 0;
121 $this->last_log_id = 0;
122 if ($this->public_key_expires == false) {
123 $this->public_key == '';
124 return false;
126 $this->bootstrapped = true;
127 return true;
131 * Delete mnet peer
132 * the peer is marked as deleted in the database
133 * we delete current sessions.
134 * @return bool - success
136 function delete() {
137 global $DB;
139 if ($this->deleted) {
140 return true;
143 $this->delete_all_sessions();
145 $this->deleted = 1;
146 return $this->commit();
149 function count_live_sessions() {
150 global $DB;
151 $obj = $this->delete_expired_sessions();
152 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id));
155 function delete_expired_sessions() {
156 global $DB;
157 $now = time();
158 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now));
161 function delete_all_sessions() {
162 global $CFG, $DB;
163 // TODO: Expires each PHP session individually
164 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id));
166 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) {
167 require_once($CFG->dirroot.'/auth/mnet/auth.php');
168 $auth = new auth_plugin_mnet();
169 $auth->end_local_sessions($sessions);
172 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id));
173 return true;
176 function check_common_name($key) {
177 $credentials = $this->check_credentials($key);
178 return $credentials['validTo_time_t'];
181 function check_credentials($key) {
182 $credentials = openssl_x509_parse($key);
183 if ($credentials == false) {
184 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => '')));
185 return false;
186 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) {
187 $a['subject'] = $credentials['subject']['subjectAltName'];
188 $a['host'] = $this->wwwroot;
189 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a));
190 return false;
191 } else if ($credentials['subject']['CN'] !== substr($this->wwwroot, 0, 64)) {
192 $a['subject'] = $credentials['subject']['CN'];
193 $a['host'] = $this->wwwroot;
194 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a));
195 return false;
196 } else {
197 if (array_key_exists('subjectAltName', $credentials['subject'])) {
198 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
199 } else {
200 $credentials['wwwroot'] = $credentials['subject']['CN'];
202 return $credentials;
206 function commit() {
207 global $DB;
208 $obj = new stdClass();
210 $obj->wwwroot = $this->wwwroot;
211 $obj->ip_address = $this->ip_address;
212 $obj->name = $this->name;
213 $obj->public_key = $this->public_key;
214 $obj->public_key_expires = $this->public_key_expires;
215 $obj->deleted = $this->deleted;
216 $obj->last_connect_time = $this->last_connect_time;
217 $obj->last_log_id = $this->last_log_id;
218 $obj->force_theme = $this->force_theme;
219 $obj->theme = $this->theme;
220 $obj->applicationid = $this->applicationid;
221 $obj->sslverification = $this->sslverification;
223 if (isset($this->id) && $this->id > 0) {
224 $obj->id = $this->id;
225 return $DB->update_record('mnet_host', $obj);
226 } else {
227 $this->id = $DB->insert_record('mnet_host', $obj);
228 return $this->id > 0;
232 function touch() {
233 $this->last_connect_time = time();
234 $this->commit();
237 function set_name($newname) {
238 if (is_string($newname) && strlen($newname <= 80)) {
239 $this->name = $newname;
240 return true;
242 return false;
245 function set_applicationid($applicationid) {
246 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
247 $this->applicationid = $applicationid;
248 return true;
250 return false;
254 * Load information from db about an mnet peer into this object's properties
256 * @param string $wwwroot - address of peer whose details we want to load
257 * @return bool - indication of success or failure
259 function set_wwwroot($wwwroot) {
260 global $CFG, $DB;
262 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
264 if ($hostinfo != false) {
265 $this->populate($hostinfo);
266 return true;
268 return false;
271 function set_id($id) {
272 global $CFG, $DB;
274 if (clean_param($id, PARAM_INT) != $id) {
275 $this->error[] = ['code' => 1, 'text' => 'Your id ('.$id.') is not legal'];
276 return false;
279 $sql = "
280 SELECT
282 FROM
283 {mnet_host} h
284 WHERE
285 h.id = ?";
287 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
288 $this->populate($hostinfo);
289 return true;
291 return false;
295 * Several methods can be used to get an 'mnet_host' record. They all then
296 * send it to this private method to populate this object's attributes.
298 * @param object $hostinfo A database record from the mnet_host table
299 * @return void
301 function populate($hostinfo) {
302 global $DB;
303 $this->id = $hostinfo->id;
304 $this->wwwroot = $hostinfo->wwwroot;
305 $this->ip_address = $hostinfo->ip_address;
306 $this->name = $hostinfo->name;
307 $this->deleted = $hostinfo->deleted;
308 $this->public_key = $hostinfo->public_key;
309 $this->public_key_expires = $hostinfo->public_key_expires;
310 $this->last_connect_time = $hostinfo->last_connect_time;
311 $this->last_log_id = $hostinfo->last_log_id;
312 $this->force_theme = $hostinfo->force_theme;
313 $this->theme = $hostinfo->theme;
314 $this->applicationid = $hostinfo->applicationid;
315 $this->sslverification = $hostinfo->sslverification;
316 $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid));
317 $this->bootstrapped = true;
321 * Get public key.
323 * @deprecated since Moodle 4.3
324 * @todo MDL-78304 Final deprecation.
326 function get_public_key() {
327 debugging('Function get_public_key() is deprecated.', DEBUG_DEVELOPER);
328 if (isset($this->public_key_ref)) return $this->public_key_ref;
329 $this->public_key_ref = openssl_pkey_get_public($this->public_key);
330 return $this->public_key_ref;