Stellarcore.net
Search: Google
Whois
 Links of the Moment: Nekochan  Apple Computers

Here is a sample of a script that is used to make a LDAP lookup against a Active Directory Domain in order to build groups filters for Dansguardian. To cut down on traffic we run this every 30mins to build a static map of users to groups.

#!/usr/bin/perl

##################################
# dansgroup.pl by Mike Tremaine  Copyright (C) 2006
#
# 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.
####################################

#This Script uses ldapsearch to get valid sAMAccountNames from specific ou's in the Active Domain.
#These usernames are converted to lowercase and listed for use by dansguardian filtergrouplist.

#Use
use File::Temp qw/ tempdir /;

#Globals
$global::tmp = "/tmp";
$global::last_update = "/usr/local/etc/dansguardian/dansguardian/lastupdate.filtergrouplist";
$global::dansguardconf = "/usr/local/etc/dansguardian/dansguardian/filtergroupslist";
$global::template = "/usr/local/etc/dansguardian/dansguardian/template.filtergroupslist";
@global::results;
$global::ldapsearch = "/usr/bin/ldapsearch";
$global::ldapbind_options = "-D \"cn=user,cn=Users,dc=group,dc=com\" -w \"password\" -h ADSERVER -x -s sub \"CN=*\"";
$global::groupmap = {
   filter1 => "First Group",
   filter2 => "Second Group",
   filter3 => "Third Group",
   filter4 => "Fourth Group"
};

#Make Temp Dir
my $TempDir = tempdir( 'dansgroups.XXXXXXXX', DIR => $global::tmp, CLEANUP => 1 );

#Main
foreach my $filtername ( sort keys %{ $global::groupmap } ) {
   my $ldapgroup_target = "ou=$global::groupmap->{$filtername},dc=group,dc=com";   
   #Now remove subgroups -mgt
   $filtername =~ s/(filter\d)[a-z]/$1/;

   #Open Pipe from Ldapsearch with grep filter
   open(LDAP, "$global::ldapsearch -b \"$ldapgroup_target\" $global::ldapbind_options | grep 'sAMAccountName: ' |") ||
      die "Unable to open ldapsearch - $!\n";
   my @search_results = ;
   close LDAP;

   #Loop over results cleaning it up
   foreach my $line (sort {uc($a) cmp uc($b)} @search_results) {
      chomp $line;
      $line =~ s/^sAMAccountName\:\s+//;
      $line = lc "$line";
      push @global::results, "$line=$filtername\n";
   }
}

#Writeout tmp
open(RESULTS, ">$TempDir/ldapdata.txt") || die "Unable to open $TempDir/ldapdata.txt - $!\n";
for my $outline (@global::results) { print RESULTS "$outline"; };
close RESULTS;

#Now diff
system("/usr/bin/diff -b $TempDir/ldapdata.txt $global::last_update");

if ($? == 0) {
   system("/bin/rm -f $TempDir/ldapdata.txt");
   exit;
} elsif ($? == 256) {
   #They differ lets update
   system("/bin/cat $global::template $TempDir/ldapdata.txt > $global::dansguardconf");
   system("/etc/init.d/dansguardian restart");
   system("/bin/cp $TempDir/ldapdata.txt $global::last_update");
   system("/bin/rm -f $TempDir/ldapdata.txt");
} else {
   #Must be an error on the diff log it
   system("/usr/bin/logger -p daemon.err 'Active Domain update failed diff test for Dansguardian! Needs helps.");
}

exit;
# vi: shiftwidth=3 tabstop=3 et