Skip to content

Commit 94b44c6

Browse files
author
Till Brehm
committed
Improved checks in openvz plugin and DB library.
1 parent 4c3fcd9 commit 94b44c6

File tree

2 files changed

+73
-40
lines changed

2 files changed

+73
-40
lines changed

interface/lib/classes/db_mysql.inc.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,18 @@ public function diffrec($record_old, $record_new) {
262262
public function datalogSave($db_table, $action, $primary_field, $primary_id, $record_old, $record_new, $force_update = false) {
263263
global $app, $conf;
264264

265-
// Insert backticks only for incomplete table names.
266-
if(stristr($db_table, '.')) {
267-
$escape = '';
265+
// Check fields
266+
if(!preg_match('/^[a-zA-Z0-9\.\-]{1,64}$/',$db_table)) $app->error('Invalid table name '.$db_table);
267+
if(!preg_match('/^[a-zA-Z0-9\-]{1,64}$/',$primary_field)) $app->error('Invalid primary field '.$primary_field.' in table '.$db_table);
268+
269+
if(strpos($db_table, '.') !== false) {
270+
$db_table = preg_replace('/^(.+)\.(.+)$/', '`$1`.`$2`', $db_table);
268271
} else {
269-
$escape = '`';
272+
$db_table = '`' . $db_table . '`';
270273
}
274+
275+
$primary_field = $this->quote($primary_field);
276+
$primary_id = intval($primary_id);
271277

272278
if($force_update == true) {
273279
//* We force a update even if no record has changed
@@ -307,7 +313,16 @@ public function datalogSave($db_table, $action, $primary_field, $primary_id, $re
307313
public function datalogInsert($tablename, $insert_data, $index_field) {
308314
global $app;
309315

310-
$tablename = $this->quote($tablename);
316+
// Check fields
317+
if(!preg_match('/^[a-zA-Z0-9\.\-]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename);
318+
if(!preg_match('/^[a-zA-Z0-9\-]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename);
319+
320+
if(strpos($tablename, '.') !== false) {
321+
$tablename = preg_replace('/^(.+)\.(.+)$/', '`$1`.`$2`', $tablename);
322+
} else {
323+
$tablename = '`' . $tablename . '`';
324+
}
325+
311326
$index_field = $this->quote($index_field);
312327

313328
if(is_array($insert_data)) {
@@ -337,7 +352,16 @@ public function datalogInsert($tablename, $insert_data, $index_field) {
337352
public function datalogUpdate($tablename, $update_data, $index_field, $index_value, $force_update = false) {
338353
global $app;
339354

340-
$tablename = $this->quote($tablename);
355+
// Check fields
356+
if(!preg_match('/^[a-zA-Z0-9\.\-]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename);
357+
if(!preg_match('/^[a-zA-Z0-9\-]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename);
358+
359+
if(strpos($tablename, '.') !== false) {
360+
$tablename = preg_replace('/^(.+)\.(.+)$/', '`$1`.`$2`', $tablename);
361+
} else {
362+
$tablename = '`' . $tablename . '`';
363+
}
364+
341365
$index_field = $this->quote($index_field);
342366
$index_value = $this->quote($index_value);
343367

@@ -364,7 +388,16 @@ public function datalogUpdate($tablename, $update_data, $index_field, $index_val
364388
public function datalogDelete($tablename, $index_field, $index_value) {
365389
global $app;
366390

367-
$tablename = $this->quote($tablename);
391+
// Check fields
392+
if(!preg_match('/^[a-zA-Z0-9\.\-]{1,64}$/',$tablename)) $app->error('Invalid table name '.$tablename);
393+
if(!preg_match('/^[a-zA-Z0-9\-]{1,64}$/',$index_field)) $app->error('Invalid index field '.$index_field.' in table '.$tablename);
394+
395+
if(strpos($tablename, '.') !== false) {
396+
$tablename = preg_replace('/^(.+)\.(.+)$/', '`$1`.`$2`', $tablename);
397+
} else {
398+
$tablename = '`' . $tablename . '`';
399+
}
400+
368401
$index_field = $this->quote($index_field);
369402
$index_value = $this->quote($index_value);
370403

interface/lib/plugins/vm_openvz_plugin.inc.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function onLoad() {
3333
function openvz_vm_insert($event_name, $page_form) {
3434
global $app, $conf;
3535

36-
$this->id = $page_form->id;
36+
$this->id = $app->functions->intval($page_form->id);
3737
$this->dataRecord = $page_form->dataRecord;
3838
$this->oldDataRecord = $page_form->oldDataRecord;
3939

@@ -58,7 +58,7 @@ function openvz_vm_insert($event_name, $page_form) {
5858
$this->applyTemplate();
5959

6060
// Set the IP address
61-
$app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
61+
$app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$app->db->quote($this->dataRecord['ip_address'])."'");
6262

6363
// Create the OpenVZ config file and store it in config field
6464
$this->makeOpenVZConfig();
@@ -74,7 +74,7 @@ function openvz_vm_insert($event_name, $page_form) {
7474
function openvz_vm_update($event_name, $page_form) {
7575
global $app, $conf;
7676

77-
$this->id = $page_form->id;
77+
$this->id = $app->functions->intval($page_form->id);
7878
$this->dataRecord = $page_form->dataRecord;
7979
$this->oldDataRecord = $page_form->oldDataRecord;
8080

@@ -94,7 +94,7 @@ function openvz_vm_update($event_name, $page_form) {
9494
}
9595

9696
// Set the IP address
97-
if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$this->dataRecord['ip_address']."'");
97+
if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ".$this->id." WHERE ip_address = '".$app->db->quote($this->dataRecord['ip_address'])."'");
9898

9999
// Create the OpenVZ config file and store it in config field
100100
$this->makeOpenVZConfig();
@@ -111,7 +111,7 @@ function openvz_vm_delete($event_name, $page_form) {
111111
global $app, $conf;
112112

113113
//* Free the IP address
114-
$tmp = $app->db->queryOneRecord("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ".$page_form->id);
114+
$tmp = $app->db->queryOneRecord("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ".$app->functions->intval($page_form->id));
115115
$app->db->datalogUpdate('openvz_ip', 'vm_id = 0', 'ip_address_id', $tmp['ip_address_id']);
116116
unset($tmp);
117117

@@ -120,29 +120,29 @@ function openvz_vm_delete($event_name, $page_form) {
120120
private function applyTemplate() {
121121
global $app, $conf;
122122

123-
$tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$this->dataRecord["template_id"]);
123+
$tpl = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$app->functions->intval($this->dataRecord["template_id"]));
124124

125125
$sql = "UPDATE openvz_vm SET ";
126-
$sql .= "diskspace = '".$tpl['diskspace']."', ";
127-
$sql .= "ram = '".$tpl['ram']."', ";
128-
$sql .= "ram_burst = '".$tpl['ram_burst']."', ";
129-
$sql .= "cpu_units = '".$tpl['cpu_units']."', ";
130-
$sql .= "cpu_num = '".$tpl['cpu_num']."', ";
131-
$sql .= "cpu_limit = '".$tpl['cpu_limit']."', ";
132-
$sql .= "io_priority = '".$tpl['io_priority']."', ";
133-
$sql .= "nameserver = '".$tpl['nameserver']."', ";
134-
$sql .= "create_dns = '".$tpl['create_dns']."', ";
135-
$sql .= "capability = '".$tpl['capability']."' ";
136-
$sql .= "WHERE vm_id = ".$this->id;
126+
$sql .= "diskspace = '".$app->db->quote($tpl['diskspace'])."', ";
127+
$sql .= "ram = '".$app->db->quote($tpl['ram'])."', ";
128+
$sql .= "ram_burst = '".$app->db->quote($tpl['ram_burst'])."', ";
129+
$sql .= "cpu_units = '".$app->db->quote($tpl['cpu_units'])."', ";
130+
$sql .= "cpu_num = '".$app->db->quote($tpl['cpu_num'])."', ";
131+
$sql .= "cpu_limit = '".$app->db->quote($tpl['cpu_limit'])."', ";
132+
$sql .= "io_priority = '".$app->db->quote($tpl['io_priority'])."', ";
133+
$sql .= "nameserver = '".$app->db->quote($tpl['nameserver'])."', ";
134+
$sql .= "create_dns = '".$app->db->quote($tpl['create_dns'])."', ";
135+
$sql .= "capability = '".$app->db->quote($tpl['capability'])."' ";
136+
$sql .= "WHERE vm_id = ".$app->functions->intval($this->id);
137137
$app->db->query($sql);
138138

139139
}
140140

141141
private function makeOpenVZConfig() {
142142
global $app, $conf;
143143

144-
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$this->id);
145-
$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$vm['template_id']);
144+
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$app->functions->intval($this->id));
145+
$vm_template = $app->db->queryOneRecord("SELECT * FROM openvz_template WHERE template_id = ".$app->functions->intval($vm['template_id']));
146146
$burst_ram = $vm['ram_burst']*256;
147147
$guar_ram = $vm['ram']*256;
148148

@@ -194,12 +194,12 @@ private function makeOpenVZConfig() {
194194
$tpl->setVar('nameserver', $vm['nameserver']);
195195
$tpl->setVar('capability', $vm['capability']);
196196

197-
$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ".$vm['ostemplate_id']);
197+
$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ".$app->functions->intval($vm['ostemplate_id']));
198198
$tpl->setVar('ostemplate', $tmp['template_file']);
199199
unset($tmp);
200200

201201
$openvz_config = $app->db->quote($tpl->grab());
202-
$app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$this->id);
202+
$app->db->query("UPDATE openvz_vm SET config = '".$openvz_config."' WHERE vm_id = ".$app->functions->intval($this->id));
203203

204204
unset($tpl);
205205

@@ -208,33 +208,33 @@ private function makeOpenVZConfig() {
208208
private function createDNS() {
209209
global $app, $conf;
210210

211-
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$this->id);
211+
$vm = $app->db->queryOneRecord("SELECT * FROM openvz_vm WHERE vm_id = ".$app->functions->intval($this->id));
212212

213213
if($vm['create_dns'] != 'y') return;
214214

215215
$full_hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']);
216216
$hostname_parts = explode('.', $full_hostname);
217-
$hostname = $hostname_parts[0];
217+
$hostname = $app->db->quote($hostname_parts[0]);
218218
unset($hostname_parts[0]);
219-
$zone = implode('.', $hostname_parts);
219+
$zone = $app->db->quote((implode('.', $hostname_parts));
220220
unset($hostname_parts);
221221

222222
// Find the dns zone
223-
$zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '$zone.'");
224-
$rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$zone_rec['id']."' AND name = '$hostname'");
223+
$zone_rec = $app->db->queryOneRecord("SELECT * FROM dns_soa WHERE origin = '".$app->db->quote($zone).".'");
224+
$rr_rec = $app->db->queryOneRecord("SELECT * FROM dns_rr WHERE zone = '".$app->functions->intval($zone_rec['id'])."' AND name = '".$app->db->quote($hostname)."'");
225225

226226
if($zone_rec['id'] > 0) {
227-
$ip_address = $vm['ip_address'];
228-
$sys_userid = $zone_rec['sys_userid'];
229-
$sys_groupid = $zone_rec['sys_groupid'];
230-
$server_id = $zone_rec['server_id'];
231-
$dns_soa_id = $zone_rec['id'];
227+
$ip_address = $app->db->quote($vm['ip_address']);
228+
$sys_userid = $app->functions->intval($zone_rec['sys_userid']);
229+
$sys_groupid = $app->functions->intval($zone_rec['sys_groupid']);
230+
$server_id = $app->functions->intval($zone_rec['server_id']);
231+
$dns_soa_id = $app->functions->intval($zone_rec['id']);
232232

233233
if($rr_rec['id'] > 0) {
234234
$app->uses('validate_dns');
235-
$app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $rr_rec['id']);
235+
$app->db->datalogUpdate('dns_rr', "data = '$ip_address'", 'id', $app->functions->intval($rr_rec['id']));
236236
$serial = $app->validate_dns->increase_serial($zone_rec['serial']);
237-
$app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $zone_rec['id']);
237+
$app->db->datalogUpdate('dns_soa', "serial = '$serial'", 'id', $app->functions->intval($zone_rec['id']));
238238
} else {
239239
$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
240240
('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$hostname', 'A', '$ip_address', '0', '3600', 'Y')";

0 commit comments

Comments
 (0)