Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
c10d9361
Commit
c10d9361
authored
Jul 02, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(ids) check for existing rulecache before read
parent
f08169cd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
61 deletions
+65
-61
rulecache.py
src/opnsense/scripts/suricata/lib/rulecache.py
+65
-61
No files found.
src/opnsense/scripts/suricata/lib/rulecache.py
View file @
c10d9361
...
...
@@ -165,66 +165,67 @@ class RuleCache(object):
:return: dict
"""
result
=
{
'rows'
:[]}
db
=
sqlite3
.
connect
(
self
.
cachefile
)
cur
=
db
.
cursor
()
# construct query including filters
sql
=
'select * from rules '
sql_filters
=
{}
for
filtertag
in
shlex
.
split
(
filter
):
fieldnames
=
filtertag
.
split
(
'/'
)[
0
]
searchcontent
=
'/'
.
join
(
filtertag
.
split
(
'/'
)[
1
:])
if
len
(
sql_filters
)
>
0
:
sql
+=
' and ( '
else
:
sql
+=
' where ( '
for
fieldname
in
map
(
lambda
x
:
x
.
lower
()
.
strip
(),
fieldnames
.
split
(
','
)):
if
fieldname
in
self
.
_rule_fields
:
if
fieldname
!=
fieldnames
.
split
(
','
)[
0
]
.
strip
():
sql
+=
' or '
if
searchcontent
.
find
(
'*'
)
==
-
1
:
sql
+=
'cast('
+
fieldname
+
" as text) like :"
+
fieldname
+
" "
else
:
sql
+=
'cast('
+
fieldname
+
" as text) like '
%
'|| :"
+
fieldname
+
" || '
%
' "
sql_filters
[
fieldname
]
=
searchcontent
.
replace
(
'*'
,
''
)
else
:
# not a valid fieldname, add a tag to make sure our sql statement is valid
sql
+=
' 1 = 1 '
sql
+=
' ) '
# apply sort order (if any)
sql_sort
=
[]
for
sortField
in
sort_by
.
split
(
','
):
if
sortField
.
split
(
' '
)[
0
]
in
self
.
_rule_fields
:
if
sortField
.
split
(
' '
)[
-
1
]
.
lower
()
==
'desc'
:
sql_sort
.
append
(
'
%
s desc'
%
sortField
.
split
()[
0
])
if
os
.
path
.
exists
(
self
.
cachefile
):
db
=
sqlite3
.
connect
(
self
.
cachefile
)
cur
=
db
.
cursor
()
# construct query including filters
sql
=
'select * from rules '
sql_filters
=
{}
for
filtertag
in
shlex
.
split
(
filter
):
fieldnames
=
filtertag
.
split
(
'/'
)[
0
]
searchcontent
=
'/'
.
join
(
filtertag
.
split
(
'/'
)[
1
:])
if
len
(
sql_filters
)
>
0
:
sql
+=
' and ( '
else
:
sql_sort
.
append
(
'
%
s asc'
%
sortField
.
split
()[
0
])
sql
+=
' where ( '
for
fieldname
in
map
(
lambda
x
:
x
.
lower
()
.
strip
(),
fieldnames
.
split
(
','
)):
if
fieldname
in
self
.
_rule_fields
:
if
fieldname
!=
fieldnames
.
split
(
','
)[
0
]
.
strip
():
sql
+=
' or '
if
searchcontent
.
find
(
'*'
)
==
-
1
:
sql
+=
'cast('
+
fieldname
+
" as text) like :"
+
fieldname
+
" "
else
:
sql
+=
'cast('
+
fieldname
+
" as text) like '
%
'|| :"
+
fieldname
+
" || '
%
' "
sql_filters
[
fieldname
]
=
searchcontent
.
replace
(
'*'
,
''
)
else
:
# not a valid fieldname, add a tag to make sure our sql statement is valid
sql
+=
' 1 = 1 '
sql
+=
' ) '
# apply sort order (if any)
sql_sort
=
[]
for
sortField
in
sort_by
.
split
(
','
):
if
sortField
.
split
(
' '
)[
0
]
in
self
.
_rule_fields
:
if
sortField
.
split
(
' '
)[
-
1
]
.
lower
()
==
'desc'
:
sql_sort
.
append
(
'
%
s desc'
%
sortField
.
split
()[
0
])
else
:
sql_sort
.
append
(
'
%
s asc'
%
sortField
.
split
()[
0
])
# count total number of rows
cur
.
execute
(
'select count(*) from (
%
s) a'
%
sql
,
sql_filters
)
result
[
'total_rows'
]
=
cur
.
fetchall
()[
0
][
0
]
# count total number of rows
cur
.
execute
(
'select count(*) from (
%
s) a'
%
sql
,
sql_filters
)
result
[
'total_rows'
]
=
cur
.
fetchall
()[
0
][
0
]
if
len
(
sql_sort
)
>
0
:
sql
+=
' order by
%
s'
%
(
','
.
join
(
sql_sort
))
if
len
(
sql_sort
)
>
0
:
sql
+=
' order by
%
s'
%
(
','
.
join
(
sql_sort
))
if
str
(
limit
)
!=
'0'
and
str
(
limit
)
.
isdigit
():
sql
+=
' limit
%
s'
%
(
limit
)
if
str
(
offset
)
!=
'0'
and
str
(
offset
)
.
isdigit
():
sql
+=
' offset
%
s'
%
(
offset
)
if
str
(
limit
)
!=
'0'
and
str
(
limit
)
.
isdigit
():
sql
+=
' limit
%
s'
%
(
limit
)
if
str
(
offset
)
!=
'0'
and
str
(
offset
)
.
isdigit
():
sql
+=
' offset
%
s'
%
(
offset
)
# fetch results
cur
.
execute
(
sql
,
sql_filters
)
while
True
:
row
=
cur
.
fetchone
()
if
row
is
None
:
break
# fetch results
cur
.
execute
(
sql
,
sql_filters
)
while
True
:
row
=
cur
.
fetchone
()
if
row
is
None
:
break
record
=
{}
for
fieldNum
in
range
(
len
(
cur
.
description
)):
record
[
cur
.
description
[
fieldNum
][
0
]]
=
row
[
fieldNum
]
result
[
'rows'
]
.
append
(
record
)
record
=
{}
for
fieldNum
in
range
(
len
(
cur
.
description
)):
record
[
cur
.
description
[
fieldNum
][
0
]]
=
row
[
fieldNum
]
result
[
'rows'
]
.
append
(
record
)
return
result
...
...
@@ -233,10 +234,13 @@ class RuleCache(object):
:return: list of installed classtypes
"""
result
=
[]
db
=
sqlite3
.
connect
(
self
.
cachefile
)
cur
=
db
.
cursor
()
cur
.
execute
(
'select distinct classtype from rules'
)
for
record
in
cur
.
fetchall
():
result
.
append
(
record
[
0
])
return
sorted
(
result
)
if
os
.
path
.
exists
(
self
.
cachefile
):
db
=
sqlite3
.
connect
(
self
.
cachefile
)
cur
=
db
.
cursor
()
cur
.
execute
(
'select distinct classtype from rules'
)
for
record
in
cur
.
fetchall
():
result
.
append
(
record
[
0
])
return
sorted
(
result
)
else
:
return
result
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment