3 * An object to represent lots of information about an RPC-peer machine
5 * @author Donal McMullan donal@catalyst.net.nz
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->libdir
. '/filelib.php'); // download_file_content() used here
15 /** No SSL verification. */
18 /** SSL verification for host. */
21 /** SSL verification for host and peer. */
22 const SSL_HOST_AND_PEER
= 2;
29 var $public_key_expires = 0;
30 var $last_connect_time = 0;
34 var $applicationid = 1; // Default of 1 == Moodle
35 var $keypair = 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. */
45 /** @var stdClass data from mnet_application table in DB. */
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
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) {
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)) {
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);
99 $this->name
= $matches[1];
103 $this->wwwroot
= $wwwroot;
104 $this->ip_address
= $ip_address;
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
;
115 $this->public_key
= clean_param(mnet_get_public_key($this->wwwroot
, $this->application
), PARAM_PEM
);
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
== '';
126 $this->bootstrapped
= true;
132 * the peer is marked as deleted in the database
133 * we delete current sessions.
134 * @return bool - success
139 if ($this->deleted
) {
143 $this->delete_all_sessions();
146 return $this->commit();
149 function count_live_sessions() {
151 $obj = $this->delete_expired_sessions();
152 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id
));
155 function delete_expired_sessions() {
158 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id
, $now));
161 function delete_all_sessions() {
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
));
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' => '')));
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));
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));
197 if (array_key_exists('subjectAltName', $credentials['subject'])) {
198 $credentials['wwwroot'] = $credentials['subject']['subjectAltName'];
200 $credentials['wwwroot'] = $credentials['subject']['CN'];
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);
227 $this->id
= $DB->insert_record('mnet_host', $obj);
228 return $this->id
> 0;
233 $this->last_connect_time
= time();
237 function set_name($newname) {
238 if (is_string($newname) && strlen($newname <= 80)) {
239 $this->name
= $newname;
245 function set_applicationid($applicationid) {
246 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) {
247 $this->applicationid
= $applicationid;
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) {
262 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot));
264 if ($hostinfo != false) {
265 $this->populate($hostinfo);
271 function set_id($id) {
274 if (clean_param($id, PARAM_INT
) != $id) {
275 $this->error
[] = ['code' => 1, 'text' => 'Your id ('.$id.') is not legal'];
287 if ($hostinfo = $DB->get_record_sql($sql, array($id))) {
288 $this->populate($hostinfo);
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
301 function populate($hostinfo) {
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;
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
;