Code

removed images from templates
[gosa.git] / gosa-plugins / squid / contrib / goSquid.pl
1 #!/usr/bin/perl -w
2 #
3 # Squid redirect programm for GOsa project
4 #
5 # Igor Muratov <migor@altlinux.org>
6 #
7 # $Id: goSquid.pl,v 1.3 2005/04/03 00:46:14 migor-guest Exp $
8 #
10 use strict;
11 use POSIX qw(strftime);
12 use Time::Local;
13 use DB_File;
15 my $debug = 0;
16 $|=1;
18 my $DEFAULT_URL = "http://www.squid-cache.org/Squidlogo2.gif";
19 my $black_list = '/var/spool/squid/domains.db';
20 my $cache_file = '/var/spool/squid/quota.db';
21 my $format = "A16 A5 S S L A5 L L L";
23 my %cache;
24 my %blacklist;
26 sub timestamp
27 {
28         return strftime("%a %b %X goSquid[$$]: ", localtime);
29 }
31 # Check url in our blacklist
32 sub unwanted_content
33 {
34         my $url = shift;
35         my $host = (split(/\//, $url))[2];
37         return 1 if exists($blacklist{$host}) and $blacklist{$host} > 0;
38         return undef;
39 }
41 # Check work time limit
42 sub work_time
43 {
44         my $user = shift;
45         my ($min,$hour) = (localtime)[1,2];
46         my $time = $hour * 60 + $min;
48         return 1 if $user->{gosaProxyWorkingStart} < $time and $user->{gosaProxyWorkingStop} > $time;
49         return undef;
50 }
52 sub quota_exceed
53 {
54         my $user = shift;
56         return 1 if $user->{trafficUsage} > $user->{gosaProxyQuota};
57         return undef;
58 }
60 sub check_access
61 {
62         my ($user, $url) = @_;
64         $user->{timed} = 0;
65         $user->{quoted} = 0;
66         $user->{filtered} = 0;
68         if($user->{gosaProxyAcctFlags} =~ m/[F]/)
69         {
70                 # Filter unwanted content
71                 $user->{filtered} = 1 if unwanted_content($url);
72         }
73         if($user->{gosaProxyAcctFlags} =~ m/[T]/)
74         {
75                 # Filter unwanted content during working hours only
76                 $user->{timed} = 1 if work_time($user);
77         }
78         if($user->{gosaProxyAcctFlags} =~ m/B/)
79         {
80                 $user->{quoted} = 1 if quota_exceed($user);
81         }
82 }
84 #--------------------------------------
85 while (<>) {
86         my ($url, $addr, $uid, $method) = split;
87         my $time = timelocal(localtime);
88         tie(%blacklist, 'DB_File', $black_list, O_RDONLY);
89         tie(%cache, 'DB_File', $cache_file, O_RDONLY);
91         if( exists($cache{$uid}) )
92         {
93                 my $user;
94                 $user->{uid} = $uid;
95                 (
96                         $user->{modifyTimestamp},
97                         $user->{gosaProxyAcctFlags},
98                         $user->{gosaProxyWorkingStart},
99                         $user->{gosaProxyWorkingStop},
100                         $user->{gosaProxyQuota},
101                         $user->{gosaProxyQuotaPeriod},
102                         $user->{trafficUsage},
103                         $user->{firstRequest},
104                         $user->{lastRequest}
105                 ) = unpack($format, $cache{$uid});
107                 check_access($user, $url);
109                 if($user->{'disabled'})
110                 {
111                         warn timestamp, "Access denied for unknown user $uid\n";
112                 }
113                 elsif($user->{'timed'})
114                 {
115                         warn timestamp, "Access denied by worktime for $uid\n";
116                 }
117                 elsif($user->{'quoted'})
118                 {
119                         warn timestamp, "Access denied by quota for $uid\n";
120                 }
121                 elsif($user->{'filtered'})
122                 {
123                         warn timestamp, "Content $url filtered for $uid\n";
124                 }
125                 else
126                 {
127                         print "$url\n";
128                         next;
129                 }
130         }
132         untie %blacklist;
133         untie %cache;
135         print "$DEFAULT_URL\n";