Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]
1 <?php 2 /** 3 * An object to represent lots of information about an RPC-peer machine 4 * 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 */ 10 11 require_once($CFG->libdir . '/filelib.php'); // download_file_content() used here 12 13 class mnet_peer { 14 15 /** No SSL verification. */ 16 const SSL_NONE = 0; 17 18 /** SSL verification for host. */ 19 const SSL_HOST = 1; 20 21 /** SSL verification for host and peer. */ 22 const SSL_HOST_AND_PEER = 2; 23 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 38 39 /** @var int $sslverification The level of SSL verification to apply. */ 40 public $sslverification = self::SSL_HOST_AND_PEER; 41 42 /* 43 * Fetch information about a peer identified by wwwroot 44 * If information does not preexist in db, collect it together based on 45 * supplied information 46 * 47 * @param string $wwwroot - address of peer whose details we want 48 * @param string $pubkey - to use if we add a record to db for new peer 49 * @param int $application - table id - what kind of peer are we talking to 50 * @return bool - indication of success or failure 51 */ 52 function bootstrap($wwwroot, $pubkey, $application) { 53 global $DB; 54 55 if (substr($wwwroot, -1, 1) == '/') { 56 $wwwroot = substr($wwwroot, 0, -1); 57 } 58 59 // If a peer record already exists for this address, 60 // load that info and return 61 if ($this->set_wwwroot($wwwroot)) { 62 return true; 63 } 64 65 $hostname = mnet_get_hostname_from_uri($wwwroot); 66 // Get the IP address for that host - if this fails, it will return the hostname string 67 $ip_address = gethostbyname($hostname); 68 69 // Couldn't find the IP address? 70 if ($ip_address === $hostname && !preg_match('/^\d+\.\d+\.\d+.\d+$/',$hostname)) { 71 throw new moodle_exception('noaddressforhost', 'mnet', '', $hostname); 72 } 73 74 $this->name = $wwwroot; 75 76 // TODO: In reality, this will be prohibitively slow... need another 77 // default - maybe blank string 78 $homepage = download_file_content($wwwroot); 79 if (!empty($homepage)) { 80 $count = preg_match("@<title>(.*)</title>@siU", $homepage, $matches); 81 if ($count > 0) { 82 $this->name = $matches[1]; 83 } 84 } 85 86 $this->wwwroot = $wwwroot; 87 $this->ip_address = $ip_address; 88 $this->deleted = 0; 89 90 $this->application = $DB->get_record('mnet_application', array('name'=>$application)); 91 if (empty($this->application)) { 92 $this->application = $DB->get_record('mnet_application', array('name'=>'moodle')); 93 } 94 95 $this->applicationid = $this->application->id; 96 97 if(empty($pubkey)) { 98 $this->public_key = clean_param(mnet_get_public_key($this->wwwroot, $this->application), PARAM_PEM); 99 } else { 100 $this->public_key = clean_param($pubkey, PARAM_PEM); 101 } 102 $this->public_key_expires = $this->check_common_name($this->public_key); 103 $this->last_connect_time = 0; 104 $this->last_log_id = 0; 105 if ($this->public_key_expires == false) { 106 $this->public_key == ''; 107 return false; 108 } 109 $this->bootstrapped = true; 110 return true; 111 } 112 113 /* 114 * Delete mnet peer 115 * the peer is marked as deleted in the database 116 * we delete current sessions. 117 * @return bool - success 118 */ 119 function delete() { 120 global $DB; 121 122 if ($this->deleted) { 123 return true; 124 } 125 126 $this->delete_all_sessions(); 127 128 $this->deleted = 1; 129 return $this->commit(); 130 } 131 132 function count_live_sessions() { 133 global $DB; 134 $obj = $this->delete_expired_sessions(); 135 return $DB->count_records('mnet_session', array('mnethostid'=>$this->id)); 136 } 137 138 function delete_expired_sessions() { 139 global $DB; 140 $now = time(); 141 return $DB->delete_records_select('mnet_session', " mnethostid = ? AND expires < ? ", array($this->id, $now)); 142 } 143 144 function delete_all_sessions() { 145 global $CFG, $DB; 146 // TODO: Expires each PHP session individually 147 $sessions = $DB->get_records('mnet_session', array('mnethostid'=>$this->id)); 148 149 if (count($sessions) > 0 && file_exists($CFG->dirroot.'/auth/mnet/auth.php')) { 150 require_once($CFG->dirroot.'/auth/mnet/auth.php'); 151 $auth = new auth_plugin_mnet(); 152 $auth->end_local_sessions($sessions); 153 } 154 155 $deletereturn = $DB->delete_records('mnet_session', array('mnethostid'=>$this->id)); 156 return true; 157 } 158 159 function check_common_name($key) { 160 $credentials = $this->check_credentials($key); 161 return $credentials['validTo_time_t']; 162 } 163 164 function check_credentials($key) { 165 $credentials = openssl_x509_parse($key); 166 if ($credentials == false) { 167 $this->error[] = array('code' => 3, 'text' => get_string("nonmatchingcert", 'mnet', array('subject' => '','host' => ''))); 168 return false; 169 } elseif (array_key_exists('subjectAltName', $credentials['subject']) && $credentials['subject']['subjectAltName'] != $this->wwwroot) { 170 $a['subject'] = $credentials['subject']['subjectAltName']; 171 $a['host'] = $this->wwwroot; 172 $this->error[] = array('code' => 5, 'text' => get_string("nonmatchingcert", 'mnet', $a)); 173 return false; 174 } else if ($credentials['subject']['CN'] !== substr($this->wwwroot, 0, 64)) { 175 $a['subject'] = $credentials['subject']['CN']; 176 $a['host'] = $this->wwwroot; 177 $this->error[] = array('code' => 4, 'text' => get_string("nonmatchingcert", 'mnet', $a)); 178 return false; 179 } else { 180 if (array_key_exists('subjectAltName', $credentials['subject'])) { 181 $credentials['wwwroot'] = $credentials['subject']['subjectAltName']; 182 } else { 183 $credentials['wwwroot'] = $credentials['subject']['CN']; 184 } 185 return $credentials; 186 } 187 } 188 189 function commit() { 190 global $DB; 191 $obj = new stdClass(); 192 193 $obj->wwwroot = $this->wwwroot; 194 $obj->ip_address = $this->ip_address; 195 $obj->name = $this->name; 196 $obj->public_key = $this->public_key; 197 $obj->public_key_expires = $this->public_key_expires; 198 $obj->deleted = $this->deleted; 199 $obj->last_connect_time = $this->last_connect_time; 200 $obj->last_log_id = $this->last_log_id; 201 $obj->force_theme = $this->force_theme; 202 $obj->theme = $this->theme; 203 $obj->applicationid = $this->applicationid; 204 $obj->sslverification = $this->sslverification; 205 206 if (isset($this->id) && $this->id > 0) { 207 $obj->id = $this->id; 208 return $DB->update_record('mnet_host', $obj); 209 } else { 210 $this->id = $DB->insert_record('mnet_host', $obj); 211 return $this->id > 0; 212 } 213 } 214 215 function touch() { 216 $this->last_connect_time = time(); 217 $this->commit(); 218 } 219 220 function set_name($newname) { 221 if (is_string($newname) && strlen($newname <= 80)) { 222 $this->name = $newname; 223 return true; 224 } 225 return false; 226 } 227 228 function set_applicationid($applicationid) { 229 if (is_numeric($applicationid) && $applicationid == intval($applicationid)) { 230 $this->applicationid = $applicationid; 231 return true; 232 } 233 return false; 234 } 235 236 /** 237 * Load information from db about an mnet peer into this object's properties 238 * 239 * @param string $wwwroot - address of peer whose details we want to load 240 * @return bool - indication of success or failure 241 */ 242 function set_wwwroot($wwwroot) { 243 global $CFG, $DB; 244 245 $hostinfo = $DB->get_record('mnet_host', array('wwwroot'=>$wwwroot)); 246 247 if ($hostinfo != false) { 248 $this->populate($hostinfo); 249 return true; 250 } 251 return false; 252 } 253 254 function set_id($id) { 255 global $CFG, $DB; 256 257 if (clean_param($id, PARAM_INT) != $id) { 258 $this->errno[] = 1; 259 $this->errmsg[] = 'Your id ('.$id.') is not legal'; 260 return false; 261 } 262 263 $sql = " 264 SELECT 265 h.* 266 FROM 267 {mnet_host} h 268 WHERE 269 h.id = ?"; 270 271 if ($hostinfo = $DB->get_record_sql($sql, array($id))) { 272 $this->populate($hostinfo); 273 return true; 274 } 275 return false; 276 } 277 278 /** 279 * Several methods can be used to get an 'mnet_host' record. They all then 280 * send it to this private method to populate this object's attributes. 281 * 282 * @param object $hostinfo A database record from the mnet_host table 283 * @return void 284 */ 285 function populate($hostinfo) { 286 global $DB; 287 $this->id = $hostinfo->id; 288 $this->wwwroot = $hostinfo->wwwroot; 289 $this->ip_address = $hostinfo->ip_address; 290 $this->name = $hostinfo->name; 291 $this->deleted = $hostinfo->deleted; 292 $this->public_key = $hostinfo->public_key; 293 $this->public_key_expires = $hostinfo->public_key_expires; 294 $this->last_connect_time = $hostinfo->last_connect_time; 295 $this->last_log_id = $hostinfo->last_log_id; 296 $this->force_theme = $hostinfo->force_theme; 297 $this->theme = $hostinfo->theme; 298 $this->applicationid = $hostinfo->applicationid; 299 $this->sslverification = $hostinfo->sslverification; 300 $this->application = $DB->get_record('mnet_application', array('id'=>$this->applicationid)); 301 $this->bootstrapped = true; 302 } 303 304 function get_public_key() { 305 if (isset($this->public_key_ref)) return $this->public_key_ref; 306 $this->public_key_ref = openssl_pkey_get_public($this->public_key); 307 return $this->public_key_ref; 308 } 309 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body