Squidalyzer blacklists update

Version 1.0
Software used in Version 1.0.
GPL License
Software distributed under
GNU GPL license.

Information on SquidGuard Blacklists avaiable on SquidGuard/Blacklists.


Two scripts are needed to fill the database with new blacklists.

Script started by the script getting the new blacklists

#!/bin/sh
#
# Copyright @2006 Savoir-faire Linux, http://www.savoirfairelinux.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# get the database name
database=`grep -e "^dbname" /usr/local/squidalyser/squidalyser.conf | cut -d' ' -f2`

# generate the sql statement
php blacklist.php

# mysql importation
echo "Loading mysql database..."
mysql $database < blacklist.sql


Script to generate SQL queries

<?php
#
# Copyright @2006 Savoir-faire Linux, http://www.savoirfairelinux.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Import blacklists info DB

class SquidGuard
{
  /* configuration file name */
  var $_configname = '/etc/squid/squidguard.conf';
  /* configuration properties */
  var $_config = array();

  function squidguard()
  {
    $this->_config['dbhome'] = ''; // default init
  }

  /**
   * Parse configfile and stock into $_config
   * Use getConfig($key) to retreive
   */
  function parseConfig()
  {
    $file = file($this->_configname);
    foreach($file as $line) {
      #get the line: dbhome /var/lib/squidguard/db
      if ( strpos($line, 'dbhome') === 0 ) {
        list($key,$value) = explode(' ',$line);
        $this->_config[$key] = trim($value);

        break; // we only need that
      }
    }
  }

  /**
   * Get one configuration variable
   * @param $key
   * @return value or '' if the key don't exist
   */
  function getConfig($key)
  {
    return ($this->_config[$key] ? $this->_config[$key] : '');
  }
}

$sg = new SquidGuard();
$sg->parseConfig();

$sgpath = $sg->getConfig('dbhome');
if ($sgpath == '') { die('Error when reading configuration file, didn\'t find dbhome variable'); }
$blacklistpath = $sgpath . '/blacklists';

// get categories
$categories = array();
$dir = opendir($blacklistpath);
while( $file=readdir($dir) ) {
  if ($file != '.' && $file != '..' && is_dir($blacklistpath.'/'.$file)) {
    $categories[] = $file;
  }
}

$fp = fopen('blacklist.sql','wt');
fwrite($fp, 'DROP TABLE IF EXISTS tblDomain;
CREATE TABLE `tblDomain` (`catDomain` varchar(16), `urlDomain` varchar(128), KEY `urlDomainIndex` (`urlDomain`) );
DROP TABLE IF EXISTS tblUrl;
CREATE TABLE `tblUrl` (`catUrl` varchar(16), `urlUrl` varchar(128), KEY `urlUrlIndex` (`urlUrl`) );
');
foreach($categories as $category) {
  $domainsFilename = $blacklistpath.'/'.$category.'/domains';

  $file = file($domainsFilename);
  echo $domainsFilename."\n";
  foreach($file as $line) {
    $line = trim($line);
    if (strlen($line) && $line[0]!='#') {
     fwrite($fp, 'INSERT INTO tblDomain (catDomain,urlDomain)
                  VALUES(\''.addslashes($category).'\',\''.addslashes($line).'\');'."\n");
    }
  }

  $urlsFilename    = $blacklistpath.'/'.$category.'/urls';
  echo $urlsFilename."\n";
  $file = file($urlsFilename);
  foreach($file as $line) {
    if ($line != '' && $line[0]!='#') {
      fwrite($fp, 'INSERT INTO tblUrl (catUrl,urlUrl)
                   VALUES(\''.addslashes($category).'\',\''.addslashes(trim($line)).'\');'."\n");
    }
  }
}
fclose($fp);

?>