MDL-16094 File storage conversion Quiz and Questions
[moodle.git] / mnet / environment.php
CommitLineData
1d422980 1<?php
71558f85 2/**
3 * Info about the local environment, wrt RPC
4 *
5 * This should really be a singleton. A PHP5 Todo I guess.
6 */
7
8class mnet_environment {
9
10 var $id = 0;
11 var $wwwroot = '';
12 var $ip_address = '';
13 var $public_key = '';
14 var $public_key_expires = 0;
15 var $last_connect_time = 0;
16 var $last_log_id = 0;
17 var $keypair = array();
f32689ad 18 var $deleted = 0;
71558f85 19
20 function mnet_environment() {
21 return true;
22 }
23
24 function init() {
cc38ff5d 25 global $CFG, $DB;
71558f85 26
27 // Bootstrap the object data on first load.
1caea91e 28 if (!$hostobject = $DB->get_record('mnet_host', array('id'=>$CFG->mnet_localhost_id))) {
29 return false;
30 }
31 $temparr = get_object_vars($hostobject);
32 foreach($temparr as $key => $value) {
33 $this->$key = $value;
34 }
35 unset($hostobject, $temparr);
71558f85 36
1caea91e 37 // Unless this is an install/upgrade, generate the SSL keys.
38 if (empty($this->public_key)) {
39 $this->get_keypair();
71558f85 40 }
41
42 // We need to set up a record that represents 'all hosts'. Any rights
43 // granted to this host will be conferred on all hosts.
44 if (empty($CFG->mnet_all_hosts_id) ) {
45 $hostobject = new stdClass();
46 $hostobject->wwwroot = '';
47 $hostobject->ip_address = '';
48 $hostobject->public_key = '';
4ab65b56 49 $hostobject->public_key_expires = 0;
50 $hostobject->last_connect_time = 0;
51 $hostobject->last_log_id = 0;
71558f85 52 $hostobject->deleted = 0;
53 $hostobject->name = 'All Hosts';
54
cc38ff5d 55 $hostobject->id = $DB->insert_record('mnet_host',$hostobject);
71558f85 56 set_config('mnet_all_hosts_id', $hostobject->id);
57 $CFG->mnet_all_hosts_id = $hostobject->id;
58 unset($hostobject);
59 }
60 }
61
62 function get_keypair() {
1caea91e 63 global $DB, $CFG;
a5d424df 64
735c7beb 65 // We don't generate keys on install/upgrade because we want the USER
66 // record to have an email address, city and country already.
31a99877 67 if (during_initial_install()) return true;
1caea91e 68 if ($CFG->mnet_dispatcher_mode == 'off') return true;
722f3f05 69 if (!extension_loaded("openssl")) return true;
71558f85 70 if (!empty($this->keypair)) return true;
735c7beb 71
72 $this->keypair = array();
bac44e6d 73 $keypair = get_config('mnet', 'openssl');
735c7beb 74
75 if (!empty($keypair)) {
76 // Explode/Implode is faster than Unserialize/Serialize
8c99567e 77 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
735c7beb 78 }
79
80 if ($this->public_key_expires > time()) {
08cb427a 81 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
82 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 83 } else {
735c7beb 84 // Key generation/rotation
85
86 // 1. Archive the current key (if there is one).
bac44e6d 87 $result = get_config('mnet', 'openssl_history');
735c7beb 88 if(empty($result)) {
89 set_config('openssl_history', serialize(array()), 'mnet');
90 $openssl_history = array();
91 } else {
92 $openssl_history = unserialize($result);
93 }
94
95 if(count($this->keypair)) {
96 $this->keypair['expires'] = $this->public_key_expires;
97 array_unshift($openssl_history, $this->keypair);
98 }
99
1d422980 100 // 2. How many old keys do we want to keep? Use array_slice to get
735c7beb 101 // rid of any we don't want
bac44e6d 102 $openssl_generations = get_config('mnet', 'openssl_generations');
735c7beb 103 if(empty($openssl_generations)) {
104 set_config('openssl_generations', 3, 'mnet');
105 $openssl_generations = 3;
106 }
107
108 if(count($openssl_history) > $openssl_generations) {
109 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
110 }
111
112 set_config('openssl_history', serialize($openssl_history), 'mnet');
113
114 // 3. Generate fresh keys
e2d4f75c 115 $this->replace_keys();
71558f85 116 }
117 return true;
118 }
119
e2d4f75c 120 function replace_keys() {
e47ac681 121 global $DB, $CFG;
cc38ff5d 122
eb7f89bc 123 $keypair = mnet_generate_keypair();
124 if (empty($keypair)) {
125 error_log('Can not generate keypair, sorry');
126 return;
127 }
128
e2d4f75c 129 $this->keypair = array();
eb7f89bc 130 $this->keypair = $keypair;
e2d4f75c 131 $this->public_key = $this->keypair['certificate'];
08cb427a 132 $details = openssl_x509_parse($this->public_key);
e2d4f75c 133 $this->public_key_expires = $details['validTo_time_t'];
134
e47ac681 135 $this->wwwroot = $CFG->wwwroot;
136 if (empty($_SERVER['SERVER_ADDR'])) {
137 // SERVER_ADDR is only returned by Apache-like webservers
138 $my_hostname = mnet_get_hostname_from_uri($CFG->wwwroot);
139 $my_ip = gethostbyname($my_hostname); // Returns unmodified hostname on failure. DOH!
140 if ($my_ip == $my_hostname) {
141 $this->ip_address = 'UNKNOWN';
142 } else {
143 $this->ip_address = $my_ip;
144 }
145 } else {
146 $this->ip_address = $_SERVER['SERVER_ADDR'];
147 }
148
e2d4f75c 149 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
150
cc38ff5d 151 $DB->update_record('mnet_host', $this);
309eb040 152 error_log('New public key has been generated. It expires ' . date('Y/m/d h:i:s', $this->public_key_expires));
e2d4f75c 153 }
154
71558f85 155 function get_private_key() {
156 if (empty($this->keypair)) $this->get_keypair();
157 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
08cb427a 158 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
71558f85 159 return $this->keypair['privatekey'];
160 }
161
162 function get_public_key() {
163 if (!isset($this->keypair)) $this->get_keypair();
164 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
08cb427a 165 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 166 return $this->keypair['publickey'];
167 }
71558f85 168}