Avoid to end SQL commands with a trailing ';'. MDL-9058
[moodle.git] / mnet / environment.php
CommitLineData
71558f85 1<?php
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() {
25 global $CFG;
26
36e6379e 27 if (empty($CFG->mnet_dispatcher_mode)) {
28 set_config('mnet_dispatcher_mode', 'off');
29 }
30
71558f85 31 // Bootstrap the object data on first load.
32 if (empty($CFG->mnet_localhost_id) ) {
71558f85 33
f32689ad 34 $this->wwwroot = $CFG->wwwroot;
35 $this->ip_address = $_SERVER['SERVER_ADDR'];
36 $this->id = insert_record('mnet_host', $this, true);
71558f85 37
38 set_config('mnet_localhost_id', $this->id);
f32689ad 39 $this->get_keypair();
71558f85 40 } else {
41 $hostobject = get_record('mnet_host','id', $CFG->mnet_localhost_id);
f32689ad 42 if(is_object($hostobject)) {
43 $temparr = get_object_vars($hostobject);
8c99567e 44 foreach($temparr as $key => $value) {
f32689ad 45 $this->$key = $value;
46 }
47 unset($hostobject, $temparr);
48 } else {
49 return false;
71558f85 50 }
51
f32689ad 52 // Unless this is an install/upgrade, generate the SSL keys.
53 if(empty($this->public_key)) {
54 $this->get_keypair();
55 }
71558f85 56 }
57
58 // We need to set up a record that represents 'all hosts'. Any rights
59 // granted to this host will be conferred on all hosts.
60 if (empty($CFG->mnet_all_hosts_id) ) {
61 $hostobject = new stdClass();
62 $hostobject->wwwroot = '';
63 $hostobject->ip_address = '';
64 $hostobject->public_key = '';
65 $hostobject->public_key_expires = '';
66 $hostobject->last_connect_time = '0';
67 $hostobject->last_log_id = '0';
68 $hostobject->deleted = 0;
69 $hostobject->name = 'All Hosts';
70
71 $hostobject->id = insert_record('mnet_host',$hostobject, true);
72 set_config('mnet_all_hosts_id', $hostobject->id);
73 $CFG->mnet_all_hosts_id = $hostobject->id;
74 unset($hostobject);
75 }
76 }
77
78 function get_keypair() {
735c7beb 79 // We don't generate keys on install/upgrade because we want the USER
80 // record to have an email address, city and country already.
81 if (!empty($_SESSION['upgraderunning'])) return true;
722f3f05 82 if (!extension_loaded("openssl")) return true;
71558f85 83 if (!empty($this->keypair)) return true;
735c7beb 84
85 $this->keypair = array();
86 $keypair = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl');
87
88 if (!empty($keypair)) {
89 // Explode/Implode is faster than Unserialize/Serialize
8c99567e 90 list($this->keypair['certificate'], $this->keypair['keypair_PEM']) = explode('@@@@@@@@', $keypair);
735c7beb 91 }
92
93 if ($this->public_key_expires > time()) {
08cb427a 94 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
95 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 96 } else {
735c7beb 97 // Key generation/rotation
98
99 // 1. Archive the current key (if there is one).
100 $result = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_history');
101 if(empty($result)) {
102 set_config('openssl_history', serialize(array()), 'mnet');
103 $openssl_history = array();
104 } else {
105 $openssl_history = unserialize($result);
106 }
107
108 if(count($this->keypair)) {
109 $this->keypair['expires'] = $this->public_key_expires;
110 array_unshift($openssl_history, $this->keypair);
111 }
112
113 // 2. How many old keys do we want to keep? Use array_slice to get
114 // rid of any we don't want
115 $openssl_generations = get_field('config_plugins', 'value', 'plugin', 'mnet', 'name', 'openssl_generations');
116 if(empty($openssl_generations)) {
117 set_config('openssl_generations', 3, 'mnet');
118 $openssl_generations = 3;
119 }
120
121 if(count($openssl_history) > $openssl_generations) {
122 $openssl_history = array_slice($openssl_history, 0, $openssl_generations);
123 }
124
125 set_config('openssl_history', serialize($openssl_history), 'mnet');
126
127 // 3. Generate fresh keys
e2d4f75c 128 $this->replace_keys();
71558f85 129 }
130 return true;
131 }
132
e2d4f75c 133 function replace_keys() {
134 $this->keypair = array();
135 $this->keypair = mnet_generate_keypair();
136 $this->public_key = $this->keypair['certificate'];
08cb427a 137 $details = openssl_x509_parse($this->public_key);
e2d4f75c 138 $this->public_key_expires = $details['validTo_time_t'];
139
140 set_config('openssl', implode('@@@@@@@@', $this->keypair), 'mnet');
141
142 update_record('mnet_host', $this);
143 }
144
71558f85 145 function get_private_key() {
146 if (empty($this->keypair)) $this->get_keypair();
147 if (isset($this->keypair['privatekey'])) return $this->keypair['privatekey'];
08cb427a 148 $this->keypair['privatekey'] = openssl_pkey_get_private($this->keypair['keypair_PEM']);
71558f85 149 return $this->keypair['privatekey'];
150 }
151
152 function get_public_key() {
153 if (!isset($this->keypair)) $this->get_keypair();
154 if (isset($this->keypair['publickey'])) return $this->keypair['publickey'];
08cb427a 155 $this->keypair['publickey'] = openssl_pkey_get_public($this->keypair['certificate']);
71558f85 156 return $this->keypair['publickey'];
157 }
158
159 /**
160 * Note that the openssl_sign function computes the sha1 hash, and then
161 * signs the hash.
162 */
163 function sign_message($message) {
08cb427a 164 $bool = openssl_sign($message, $signature, $this->get_private_key());
71558f85 165 return $signature;
166 }
167}
168
169?>