@@ -232,32 +232,63 @@ function syslog_partition_manage() {
232232 return $ syslog_deleted ;
233233}
234234
235+ /**
236+ * syslog_partition_table_allowed - validate that the table being partitioned
237+ * is in our approved list.
238+ *
239+ * @param (string) The table name
240+ *
241+ * @return (bool) True if allowed, False otherwise
242+ */
243+ function syslog_partition_table_allowed ($ table ) {
244+ if (in_array ($ table , array ('syslog ' , 'syslog_removed ' ), true )) {
245+ return (bool )preg_match ('/^[a-z_]+$/ ' , $ table );
246+ }
247+
248+ return false ;
249+ }
250+
235251/**
236252 * This function will create a new partition for the specified table.
237253 */
238254function syslog_partition_create ($ table ) {
239255 global $ syslogdb_default ;
240256
257+ if (!syslog_partition_table_allowed ($ table )) {
258+ return false ;
259+ }
260+
241261 /* determine the format of the table name */
242262 $ time = time ();
243263 $ cformat = 'd ' . date ('Ymd ' , $ time );
244264 $ lnow = date ('Y-m-d ' , $ time +86400 );
245265
246- $ exists = syslog_db_fetch_row ("SELECT *
266+ $ exists = syslog_db_fetch_row_prepared ("SELECT *
247267 FROM `information_schema`.`partitions`
248- WHERE table_schema=' " . $ syslogdb_default . "'
249- AND partition_name=' " . $ cformat . "'
250- AND table_name='syslog'
251- ORDER BY partition_ordinal_position " );
268+ WHERE table_schema = ?
269+ AND partition_name = ?
270+ AND table_name = ?
271+ ORDER BY partition_ordinal_position " ,
272+ array ($ syslogdb_default , $ cformat , $ table )
273+ );
252274
253275 if (!cacti_sizeof ($ exists )) {
254- cacti_log ("SYSLOG: Creating new partition ' $ cformat' " , false , 'SYSTEM ' );
276+ $ lock_name = hash ('sha256 ' , $ syslogdb_default . 'syslog_partition_create. ' . $ table );
277+
278+ try {
279+ syslog_db_fetch_cell_prepared ('SELECT GET_LOCK(?, 10) ' , array ($ lock_name ));
280+
281+ cacti_log ("SYSLOG: Creating new partition ' $ cformat' for table ' $ table' " , false , 'SYSTEM ' );
255282
256- syslog_debug ("Creating new partition ' $ cformat' " );
283+ syslog_debug ("Creating new partition ' $ cformat' for table ' $ table ' " );
257284
258- syslog_db_execute ("ALTER TABLE ` " . $ syslogdb_default . "`.` $ table` REORGANIZE PARTITION dMaxValue INTO (
259- PARTITION $ cformat VALUES LESS THAN (TO_DAYS(' $ lnow')),
260- PARTITION dMaxValue VALUES LESS THAN MAXVALUE) " );
285+ /* MySQL does not support parameter binding for DDL statements */
286+ syslog_db_execute ("ALTER TABLE ` " . $ syslogdb_default . "`.` $ table` REORGANIZE PARTITION dMaxValue INTO (
287+ PARTITION $ cformat VALUES LESS THAN (TO_DAYS(' $ lnow')),
288+ PARTITION dMaxValue VALUES LESS THAN MAXVALUE) " );
289+ } finally {
290+ syslog_db_fetch_cell_prepared ('SELECT RELEASE_LOCK(?) ' , array ($ lock_name ));
291+ }
261292 }
262293}
263294
@@ -267,32 +298,48 @@ function syslog_partition_create($table) {
267298function syslog_partition_remove ($ table ) {
268299 global $ syslogdb_default ;
269300
301+ if (!syslog_partition_table_allowed ($ table )) {
302+ cacti_log ("SYSLOG ERROR: Attempt to remove partitions from disallowed table ' $ table' " , false , 'SYSTEM ' );
303+ return 0 ;
304+ }
305+
270306 $ syslog_deleted = 0 ;
271- $ number_of_partitions = syslog_db_fetch_assoc ("SELECT *
307+ $ number_of_partitions = syslog_db_fetch_assoc_prepared ("SELECT *
272308 FROM `information_schema`.`partitions`
273- WHERE table_schema=' " . $ syslogdb_default . "' AND table_name='syslog'
274- ORDER BY partition_ordinal_position " );
309+ WHERE table_schema = ?
310+ AND table_name = ?
311+ ORDER BY partition_ordinal_position " ,
312+ array ($ syslogdb_default , $ table )
313+ );
275314
276315 $ days = read_config_option ('syslog_retention ' );
277316
278- syslog_debug ("There are currently ' " . sizeof ($ number_of_partitions ) . "' Syslog Partitions, We will keep ' $ days' of them. " );
317+ syslog_debug ("There are currently ' " . sizeof ($ number_of_partitions ) . "' Syslog Partitions for ' $ table ' , We will keep '$ days' of them. " );
279318
280319 if ($ days > 0 ) {
281320 $ user_partitions = sizeof ($ number_of_partitions ) - 1 ;
282321 if ($ user_partitions >= $ days ) {
283- $ i = 0 ;
284- while ($ user_partitions > $ days ) {
285- $ oldest = $ number_of_partitions [$ i ];
322+ $ lock_name = hash ('sha256 ' , $ syslogdb_default . 'syslog_partition_remove. ' . $ table );
286323
287- cacti_log ("SYSLOG: Removing old partition ' " . $ oldest ['PARTITION_NAME ' ] . "' " , false , 'SYSTEM ' );
324+ try {
325+ syslog_db_fetch_cell_prepared ('SELECT GET_LOCK(?, 10) ' , array ($ lock_name ));
288326
289- syslog_debug ("Removing partition ' " . $ oldest ['PARTITION_NAME ' ] . "' " );
327+ $ i = 0 ;
328+ while ($ user_partitions > $ days ) {
329+ $ oldest = $ number_of_partitions [$ i ];
290330
291- syslog_db_execute ( " ALTER TABLE ` " . $ syslogdb_default . "`.` $ table ` DROP PARTITION " . $ oldest [ ' PARTITION_NAME ' ] );
331+ cacti_log ( " SYSLOG: Removing old partition ' " . $ oldest [ ' PARTITION_NAME ' ] . "' from table ' $ table ' " , false , ' SYSTEM ' );
292332
293- $ i ++;
294- $ user_partitions --;
295- $ syslog_deleted ++;
333+ syslog_debug ("Removing partition ' " . $ oldest ['PARTITION_NAME ' ] . "' from table ' $ table' " );
334+
335+ syslog_db_execute ("ALTER TABLE ` " . $ syslogdb_default . "`.` $ table` DROP PARTITION " . $ oldest ['PARTITION_NAME ' ]);
336+
337+ $ i ++;
338+ $ user_partitions --;
339+ $ syslog_deleted ++;
340+ }
341+ } finally {
342+ syslog_db_fetch_cell_prepared ('SELECT RELEASE_LOCK(?) ' , array ($ lock_name ));
296343 }
297344 }
298345 }
@@ -303,21 +350,29 @@ function syslog_partition_remove($table) {
303350function syslog_partition_check ($ table ) {
304351 global $ syslogdb_default ;
305352
353+ if (!syslog_partition_table_allowed ($ table )) {
354+ return false ;
355+ }
356+
306357 if (defined ('SYSLOG_CONFIG ' )) {
307358 include (SYSLOG_CONFIG );
308359 }
309360
310361 /* find date of last partition */
311- $ last_part = syslog_db_fetch_cell ("SELECT PARTITION_NAME
362+ $ last_part = syslog_db_fetch_cell_prepared ("SELECT PARTITION_NAME
312363 FROM `information_schema`.`partitions`
313- WHERE table_schema=' " . $ syslogdb_default . "' AND table_name='syslog'
364+ WHERE table_schema = ?
365+ AND table_name = ?
314366 ORDER BY partition_ordinal_position DESC
315- LIMIT 1,1; " );
367+ LIMIT 1,1 " ,
368+ array ($ syslogdb_default , $ table )
369+ );
316370
317371 $ lformat = str_replace ('d ' , '' , $ last_part );
318372 $ cformat = date ('Ymd ' );
319373
320374 if ($ cformat > $ lformat ) {
375+
321376 return true ;
322377 } else {
323378 return false ;
@@ -339,16 +394,9 @@ function syslog_remove_items($table, $uniqueID) {
339394 syslog_debug ('------------------------------------------------------------------------------------- ' );
340395 syslog_debug ('Processing Removal Rules... ' );
341396
342- if ($ table == 'syslog ' ) {
343- $ rows = syslog_db_fetch_assoc ("SELECT *
344- FROM ` " . $ syslogdb_default . "`.`syslog_remove`
345- WHERE enabled = 'on'
346- AND id = $ uniqueID " );
347- } else {
348- $ rows = syslog_db_fetch_assoc ('SELECT *
349- FROM ` ' . $ syslogdb_default . '`.`syslog_remove`
350- WHERE enabled="on" ' );
351- }
397+ $ rows = syslog_db_fetch_assoc ('SELECT *
398+ FROM ` ' . $ syslogdb_default . '`.`syslog_remove`
399+ WHERE enabled="on" ' );
352400
353401 syslog_debug (sprintf ('Found %5s - Removal Rule(s) to process ' , cacti_sizeof ($ rows )));
354402
0 commit comments