dansguardian 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,5 @@
1
+
2
+ 0.0.1
3
+
4
+ * first release
5
+
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ ===DansGuardian
2
+
3
+ A Ruby library to manage DansGuardian configuration. Based on ConfigFiles by
4
+ the same author.
5
+
6
+ require 'rubygems'
7
+ require 'pp'
8
+ require 'configfiles'
9
+ require 'dansguardian'
10
+
11
+ file = '/etc/dansguardian/dansguardian.conf'
12
+
13
+ #dgm = DansGuardian::Config::Main.new
14
+ #dgp = DansGuardian::Parser.read_file file
15
+ #dgm.load dgp
16
+ #pp dgm
17
+
18
+ dgconf = DansGuardian::Config.new(:mainfile => file)
19
+
20
+ dgconf.main
21
+
22
+ dgconf.filtergroup(1, :cached => true)
23
+
24
+ wfl = dgconf.filtergroup(1)[:weightedphraselist]
25
+
26
+ p wfl
27
+
28
+ pp DansGuardian::Inclusion.get(wfl)
29
+
30
+ #dg.config.filtergroup(1).inclusiontree('weightedphraselist')
31
+
32
+ == License
33
+
34
+ Same of Ruby
35
+
36
+ Copyright (c) 2010 Guido De Rosa <guido.derosa at vemarsas.it>
37
+
38
+
data/example.rb ADDED
@@ -0,0 +1,38 @@
1
+ $dg_library_path = File.expand_path File.join File.dirname(__FILE__), 'lib'
2
+
3
+ unless $LOAD_PATH.include? $dg_library_path
4
+ $LOAD_PATH.unshift $dg_library_path
5
+ end
6
+
7
+ require 'rubygems'
8
+ require 'pp'
9
+ require 'configfiles'
10
+ require 'dansguardian'
11
+
12
+ file = '/etc/dansguardian/dansguardian.conf'
13
+
14
+ #dgm = DansGuardian::Config::Main.new
15
+ #dgp = DansGuardian::Parser.read_file file
16
+ #dgm.load dgp
17
+ #pp dgm
18
+
19
+ dgconf = DansGuardian::Config.new(:mainfile => file)
20
+
21
+ dgconf.main
22
+
23
+ dgconf.filtergroup(1, :cached => true)
24
+
25
+ wfl = dgconf.filtergroup(1)[:weightedphraselist]
26
+
27
+ p wfl
28
+
29
+ pp DansGuardian::Inclusion.get(wfl)
30
+
31
+
32
+
33
+
34
+
35
+ #dg.config.filtergroup(1).inclusiontree('weightedphraselist')
36
+
37
+
38
+
@@ -0,0 +1,13 @@
1
+ autoload :IPAddr, 'ipaddr'
2
+ autoload :URI, 'uri'
3
+ autoload :Set, 'set'
4
+
5
+ require 'configfiles'
6
+
7
+ module DansGuardian
8
+ VERSION = '0.0.2'
9
+
10
+ autoload :Config, 'dansguardian/config'
11
+ autoload :Parser, 'dansguardian/parser'
12
+ autoload :Inclusion, 'dansguardian/inclusion'
13
+ end
@@ -0,0 +1,69 @@
1
+ require 'dansguardian/config/main'
2
+ require 'dansguardian/config/filtergroup'
3
+
4
+ module DansGuardian
5
+ class Config
6
+ # +arg+ May be +String+-like (the main config file name) or +Hash+-like :
7
+ #
8
+ # DansGuardian::Config.new('path/to/dansguardian.conf')
9
+ #
10
+ # DansGuardian::Config.new(:mainfile => '/path/to/dansguardian.conf')
11
+ #
12
+ def initialize(arg='/etc/dansguardian/dansguardian.conf')
13
+ h = {}
14
+ if arg.respond_to? :[]
15
+ h = arg
16
+ else # String-like assumed
17
+ h = {:mainfile => arg}
18
+ end
19
+ @mainfile = h[:mainfile]
20
+ @mainconfig = nil
21
+ @filtergroups = []
22
+ end
23
+
24
+ # get data from the main configuration file
25
+ def main(opt_h={:cached => false})
26
+ if opt_h[:cached]
27
+ if @mainconfig
28
+ return @mainconfig
29
+ else
30
+ get_main
31
+ end
32
+ else
33
+ get_main
34
+ end
35
+ end
36
+
37
+ # get filtergroup configuration data
38
+ def filtergroup(n=1, opt_h={:cached => false})
39
+ if opt_h[:cached]
40
+ if @filtergroups[n]
41
+ return @filtergroups[n]
42
+ else
43
+ get_filtergroup n
44
+ end
45
+ else
46
+ get_filtergroup n
47
+ end
48
+ end
49
+
50
+ def filtergroupfile(n=1)
51
+ File.join File.dirname(@mainfile), "dansguardianf#{n}.conf"
52
+ end
53
+
54
+ protected
55
+
56
+ def get_main
57
+ dgmain = DansGuardian::Config::Main.new
58
+ parsedata = DansGuardian::Parser.read_file @mainfile
59
+ @mainconfig = dgmain.load parsedata
60
+ end
61
+
62
+ def get_filtergroup(n)
63
+ dgf = DansGuardian::Config::FilterGroup.new
64
+ parsedata = DansGuardian::Parser.read_file filtergroupfile(n)
65
+ @filtergroups[n] = dgf.load parsedata
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,27 @@
1
+ require 'configfiles'
2
+
3
+ module DansGuardian
4
+ class Config
5
+
6
+ # Useful constants
7
+ INFINITY = ::Float::INFINITY
8
+
9
+ # Recurring converters
10
+ BOOL = {
11
+ 'on' => true,
12
+ 'off' => false
13
+ }
14
+ REPORTING_LEVEL = {
15
+ '-1' => :log_only,
16
+ '0' => :access_denied_only,
17
+ '1' => :why_but_not_what,
18
+ '2' => :full,
19
+ '3' => :html_template
20
+ }
21
+
22
+ # Exceptions
23
+ class ValidationFailed < ::ConfigFiles::Base::ValidationFailed; end
24
+
25
+ end
26
+ end
27
+
@@ -0,0 +1,181 @@
1
+ autoload :URI, 'uri'
2
+
3
+ require 'dansguardian/config/constants'
4
+
5
+ module DansGuardian
6
+ class Config
7
+ class FilterGroup < ::ConfigFiles::Base
8
+
9
+ on :unknown_parameter do |s|
10
+ "FIXME: unknown parameter: value = #{s}"
11
+ end
12
+
13
+ parameter :groupmode,
14
+ '0' => :banned,
15
+ '1' => :filtered,
16
+ '2' => :unfiltered
17
+
18
+ parameter :groupname
19
+
20
+ [
21
+ :bannedphraselist,
22
+ :bannedsitelist,
23
+ :bannedurllist,
24
+ :bannedregexpurllist,
25
+ :bannedextensionlist,
26
+ :bannedregexpheaderlist,
27
+ :bannedmimetypelist,
28
+ :headerregexplist, # ban
29
+
30
+ :weightedphraselist,
31
+
32
+ :greysitelist,
33
+ :greyurllist,
34
+
35
+ :contentregexplist, # modify
36
+ :urlregexplist, # modify
37
+
38
+ :exceptionextensionlist,
39
+ :exceptionmimetypelist,
40
+ :exceptionfilesitelist,
41
+ :exceptionfileurllist,
42
+ :exceptionphraselist,
43
+ :exceptionsitelist,
44
+ :exceptionurllist,
45
+ :exceptionregexpurllist,
46
+
47
+ :logsitelist,
48
+ :logurllist,
49
+ :logregexpurllist,
50
+
51
+ :picsfile,
52
+
53
+ ].each do |name|
54
+ parameter name
55
+ end
56
+
57
+ parameter :naughtynesslimit, :to_i
58
+
59
+ virtual :naughtyness do |confdata|
60
+ case confdata[:naughtynesslimit]
61
+ when 0..50
62
+ :young_children
63
+ when 51..100
64
+ :old_children
65
+ when 101..150 # sure of 150 ?
66
+ :young_adults
67
+ when 150..INFINITY
68
+ :adults
69
+ end
70
+ end
71
+
72
+ parameter :categorydisplaythreshold do |str|
73
+ case str
74
+ when '-1'
75
+ :highest_only
76
+ when '0'
77
+ :all
78
+ else
79
+ n = str.to_i
80
+ if n > 0
81
+ n
82
+ else
83
+ # 'eager' validation ;-)
84
+ raise ValidationFailed, "found an invalid value '#{str}' for parameter :categorydisplaythreshold"
85
+ end
86
+ end
87
+ end
88
+ default :categorydisplaythreshold, :all
89
+
90
+ parameter :embeddedurlweight, :to_i
91
+ default :embeddedurlweight, 0
92
+
93
+ parameter :blockdownloads, BOOL
94
+ default :blockdownloads, false
95
+
96
+ parameter :enablepics, BOOL
97
+ default :blockdownloads, false
98
+
99
+ [:bypass, :infectionbypass].each do |name|
100
+ parameter name do |str|
101
+ case str
102
+ when '-1'
103
+ :require_separate_program
104
+ when '0'
105
+ :disable
106
+ else
107
+ seconds = str.to_i
108
+ if seconds > 0
109
+ seconds
110
+ else
111
+ # 'eager' validation...
112
+ raise ValidationFailed, "found an invalid value '#{str}' for parameter '#{name}'"
113
+ end
114
+ end
115
+ end
116
+ default name, :disable
117
+ end
118
+
119
+ [:bypasskey, :infectionbypasskey].each do |name|
120
+ parameter name do |str|
121
+ case str
122
+ when /^\s*$/
123
+ :generate_random
124
+ else
125
+ str
126
+ end
127
+ end
128
+ default name, :generate_random
129
+ end
130
+
131
+ parameter :infectionbypasserrorsonly, BOOL
132
+ default :infectionbypasserrorsonly, true
133
+
134
+ parameter :disablecontentscan, BOOL
135
+ default :disablecontentscan, false
136
+ virtual(:contentscan) {|conf| !conf[:disablecontentscan]}
137
+
138
+ parameter :deepurlanalysis, BOOL
139
+ default :deepurlanalysis, false
140
+
141
+ parameter :reportinglevel, REPORTING_LEVEL
142
+
143
+ parameter(:accessdeniedaddress) {|s| URI.parse s}
144
+
145
+ parameter :htmltemplate
146
+
147
+ parameter :usesmtp, BOOL
148
+ default :usesmtp, false
149
+
150
+ [:mailfrom, :avadmin, :contentadmin, :avsubject, :contentsubject].each do |name|
151
+ parameter name do |str|
152
+ case str
153
+ when /^\s*$/
154
+ nil
155
+ else
156
+ str
157
+ end
158
+ end
159
+ end
160
+
161
+ [:notifyav, :notifycontent, :thresholdbyuser].each do |name|
162
+ parameter name, BOOL
163
+ end
164
+
165
+ parameter :violations, :to_i
166
+ virtual :violations_before_notification do |cfdata|
167
+ case cfdata[:violations]
168
+ when 0
169
+ :never
170
+ else
171
+ cfdata[:violations]
172
+ end
173
+ end
174
+
175
+ parameter :threshold, :to_i
176
+
177
+ end
178
+ end
179
+ end
180
+
181
+
@@ -0,0 +1,268 @@
1
+ autoload :IPAddr, 'ipaddr'
2
+ autoload :URI, 'uri'
3
+ autoload :Set, 'set'
4
+
5
+ require 'dansguardian/extensions/float'
6
+ require 'dansguardian/config/constants'
7
+
8
+ module DansGuardian
9
+ class Config
10
+ class Main < ::ConfigFiles::Base
11
+
12
+ # Useful constants
13
+ INFINITY = ::Float::INFINITY
14
+
15
+ # Recurring converters
16
+ BOOL = {'on' => true, 'off' => false}
17
+
18
+ # Exceptions
19
+ class ValidationFailed < ValidationFailed; end
20
+
21
+ on :unknown_parameter do |s|
22
+ "FIXME: unknown parameter: value = #{s}"
23
+ end
24
+
25
+ parameter :reportinglevel, REPORTING_LEVEL
26
+
27
+ parameter :languagedir
28
+
29
+ parameter :language
30
+
31
+ parameter :loglevel,
32
+ '0' => :none,
33
+ '1' => :just_denied,
34
+ '2' => :all_text_based,
35
+ '3' => :all_requests
36
+
37
+ parameter :logexceptionhits,
38
+ '0' => :never,
39
+ '1' => :log,
40
+ '2' => :log_and_mark
41
+ default :logexceptionhits, :log_and_mark
42
+
43
+ parameter :logfileformat,
44
+ '1' => :dansguardian,
45
+ '2' => :csv,
46
+ '3' => :squid,
47
+ '4' => :tabs
48
+
49
+ parameter :maxlogitemlength, :to_i
50
+
51
+ parameter :anonymizelogs, BOOL
52
+
53
+ parameter :syslog, BOOL
54
+
55
+ parameter :loglocation
56
+
57
+ parameter :statlocation
58
+
59
+ parameter :filterip do |str|
60
+ if str == ''
61
+ :any
62
+ else
63
+ IPAddr.new str # in case of invalid str, rely on IPAddr exceptions
64
+ end
65
+ end
66
+
67
+ parameter :filterport, :to_i
68
+
69
+ parameter :proxyip do |str|
70
+ IPAddr.new str
71
+ end
72
+ default :proxyip, IPAddr.new('127.0.0.1')
73
+
74
+ parameter :proxyport, :to_i
75
+
76
+ parameter :accessdeniedaddress do |str|
77
+ URI.parse str
78
+ end
79
+
80
+ parameter :nonstandarddelimiter, BOOL
81
+ default :nonstandarddelimiter, true
82
+
83
+ parameter :usecustombannedimage, BOOL
84
+ default :usecustombannedimage, true
85
+
86
+ parameter :custombannedimagefile
87
+
88
+ parameter :filtergroups, :to_i
89
+
90
+ parameter :filtergroupslist
91
+
92
+ parameter :bannediplist
93
+
94
+ parameter :exceptioniplist
95
+
96
+ parameter :showweightedfound, BOOL
97
+
98
+ parameter :weightedphrasemode,
99
+ '0' => false,
100
+ '1' => :normal,
101
+ '2' => :singular
102
+
103
+ parameter :urlcachenumber, :to_i
104
+
105
+ parameter :urlcacheage, :to_i
106
+ # seconds, TODO: class Time::Interval ?
107
+
108
+ parameter :scancleancache, BOOL
109
+ default :scancleancache, true
110
+
111
+ parameter :phrasefiltermode,
112
+ '0' => Set.new([:meta, :title, :raw]),
113
+ '1' => Set.new([:meta, :title, :smart ]),
114
+ '2' => Set.new([:meta, :title, :smart, :raw]),
115
+ '3' => Set.new([:meta, :title ])
116
+ default :phrasefiltermode,
117
+ Set.new([:meta, :title, :smart, :raw])
118
+
119
+ parameter :preservecase,
120
+ '0' => Set.new([:lower ]),
121
+ '1' => Set.new([ :original ]),
122
+ '2' => Set.new([:lower, :original ])
123
+ default :preservecase,
124
+ Set.new([:lower ])
125
+
126
+ parameter :hexdecodecontent, BOOL
127
+ default :hexdecodecontent, false
128
+
129
+ parameter :forcequicksearch, BOOL
130
+ default :forcequicksearch, false
131
+
132
+ parameter :reverseaddresslookups, BOOL
133
+
134
+ parameter :reverseclientiplookups, BOOL
135
+
136
+ parameter :logclienthostnames, BOOL
137
+
138
+ parameter :createlistcachefiles, BOOL
139
+
140
+ parameter :maxuploadsize do |str|
141
+ case str
142
+ when '-1'
143
+ INFINITY
144
+ else
145
+ str.to_i * 1024
146
+ end
147
+ end
148
+
149
+ parameter :maxcontentfiltersize do |str|
150
+ case str
151
+ when '0'
152
+ lambda {|confdata| confdata[:maxcontentramcachescansize]}
153
+ else
154
+ str.to_i * 1024
155
+ end
156
+ end
157
+
158
+ parameter :maxcontentramcachescansize do |str|
159
+ case str
160
+ when '0'
161
+ lambda {|confdata| confdata[:maxcontentfilecachescansize]}
162
+ else
163
+ str.to_i * 1024
164
+ end
165
+ end
166
+
167
+ parameter :maxcontentfilecachescansize do |str|
168
+ str.to_i * 1024
169
+ end
170
+
171
+ parameter :filecachedir
172
+
173
+ parameter :deletedownloadedtempfiles, BOOL
174
+ default :deletedownloadedtempfiles, true
175
+
176
+ parameter :initialtrickledelay, :to_i
177
+
178
+ parameter :trickledelay, :to_i
179
+
180
+ # You don't really need to lazy-evaluate (short) Arrays
181
+ #
182
+ parameter :downloadmanager # Array
183
+ parameter :contentscanner # Array
184
+ parameter :authplugin # Array
185
+
186
+ parameter :contentscannertimeout, :to_i
187
+ default :contentscannertimeout, 60
188
+
189
+ parameter :contentscanexceptions, BOOL
190
+ default :contentscanexceptions, false
191
+
192
+ parameter :recheckreplacedurls, BOOL
193
+ default :recheckreplacedurls, false
194
+
195
+ [
196
+ :forwardedfor,
197
+ :usexforwardedfor,
198
+ :logconnectionhandlingerrors
199
+ ].each do |par|
200
+ parameter par, BOOL
201
+ end
202
+
203
+ parameter :logchildprocesshandling, BOOL
204
+
205
+ [
206
+ :maxchildren,
207
+ :minchildren,
208
+ :minsparechildren,
209
+ :preforkchildren,
210
+ :maxsparechildren,
211
+ :maxagechildren
212
+ ].each do |par|
213
+ parameter par, :to_i
214
+ end
215
+
216
+ parameter :maxips do |str|
217
+ if str == '0'
218
+ INFINITY
219
+ else
220
+ str.to_i
221
+ end
222
+ end
223
+
224
+ [:ipcfilename, :urlipcfilename, :ipipcfilename, :pidfilename].each do |p|
225
+ parameter p
226
+ end
227
+ default :pidfilename, '/var/run/dansguardian.pid'
228
+
229
+ [:nodaemon, :nologger, :logadblocks, :loguseragent].each do |p|
230
+ parameter p, BOOL
231
+ default p, false
232
+ end
233
+
234
+ [:daemon, :logger].each do |name|
235
+ virtual name do |confdata|
236
+ not(confdata["no#{name}".to_sym])
237
+ end
238
+ end
239
+
240
+ [:daemonuser, :daemongroup].each do |name|
241
+ parameter name
242
+ default name, :set_at_compile_time
243
+ end
244
+
245
+ parameter :softrestart, BOOL
246
+ default :softrestart, false
247
+
248
+ parameter :mailer
249
+
250
+ validate do |conf|
251
+ raise ValidationFailed, "maxcontentfiltersize must be not higher than maxcontentramcachescansize" unless
252
+ conf[:maxcontentfiltersize] <=
253
+ conf[:maxcontentramcachescansize]
254
+
255
+ raise ValidationFailed, "maxcontentramcachescansize must be not higher than maxcontentfilecachescansize" unless
256
+ conf[:maxcontentramcachescansize] <=
257
+ conf[:maxcontentfilecachescansize]
258
+
259
+ raise ValidationFailed, "maxcontentfilecachescansize must be greater or equal to maxcontentramcachescansize" unless
260
+ conf[:maxcontentfilecachescansize] >=
261
+ conf[:maxcontentramcachescansize]
262
+ end
263
+
264
+ end
265
+ end
266
+ end
267
+
268
+
@@ -0,0 +1,3 @@
1
+ class Float
2
+ INFINITY ||= 1.0/0.0 # Introduced in Ruby 1.9.2
3
+ end
@@ -0,0 +1,16 @@
1
+ module DansGuardian
2
+ module Inclusion
3
+ # no distinction between text parser and ConfigFile conversion here ...
4
+ def self.get(filename)
5
+ all = {}
6
+ File.foreach filename do |line|
7
+ if line =~ /^\s*\.Include\s*<(.*)>/
8
+ all[$1] = {:active => true}
9
+ elsif line =~ /^\s*#+\s*\.Include\s*<(.*)>/
10
+ all[$1] = {:active => false}
11
+ end
12
+ end
13
+ return all
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,43 @@
1
+ module DansGuardian
2
+ module Parser
3
+
4
+ def self.read(io)
5
+ h = {}
6
+ may_appear_multiple_times = [
7
+ :downloadmanager, :contentscanner, :authplugin]
8
+ io.each_line do |line|
9
+ line.sub! /#.*$/, ''
10
+ line.strip!
11
+ match = false
12
+ case line
13
+ when /^([^=\s]+)\s*=\s*([^=\s']*)$/
14
+ key = $1.to_sym
15
+ value = $2
16
+ match = true
17
+ when /^([^=\s]+)\s*=\s*'(.*)'$/
18
+ key = $1.to_sym
19
+ value = $2.gsub(/\\'/, "'")
20
+ match = true
21
+ end
22
+ if match
23
+ if may_appear_multiple_times.include? key
24
+ h[key] ||= []
25
+ h[key] << value
26
+ else
27
+ h[key] = value
28
+ end
29
+ end
30
+ end
31
+ return h
32
+ end
33
+
34
+ def self.read_file(filepath)
35
+ File.open filepath, 'r' do |f|
36
+ read f
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dansguardian
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Guido De Rosa
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-03 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: configfiles
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: Manage DansGuardian configuration from Ruby -- http:/dansguardian.org/
34
+ email: guido.derosa@vemarsas.it
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README.rdoc
41
+ files:
42
+ - Changelog
43
+ - README.rdoc
44
+ - example.rb
45
+ - lib/dansguardian.rb
46
+ - lib/dansguardian/config/constants.rb
47
+ - lib/dansguardian/config/filtergroup.rb
48
+ - lib/dansguardian/config/main.rb
49
+ - lib/dansguardian/extensions/float.rb
50
+ - lib/dansguardian/config.rb
51
+ - lib/dansguardian/inclusion.rb
52
+ - lib/dansguardian/parser.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/gderosa/dansguardian.rb
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --main
60
+ - README.rdoc
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Manage DansGuardian configuration from Ruby -- http:/dansguardian.org/
86
+ test_files: []
87
+