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
a9b57a31
Commit
a9b57a31
authored
Oct 14, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(captiveportal, new) add some db functions
parent
7ef601b2
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
18 deletions
+50
-18
db.py
src/opnsense/scripts/OPNsense/CaptivePortal/lib/db.py
+50
-18
No files found.
src/opnsense/scripts/OPNsense/CaptivePortal/lib/db.py
View file @
a9b57a31
...
...
@@ -34,17 +34,27 @@ class DB(object):
database_filename
=
'/var/captiveportal/captiveportal.sqlite'
def
__init__
(
self
):
""" construct new database connection
""" construct new database connection
, open and make sure the sqlite file exists
:return:
"""
self
.
_connection
=
sqlite3
.
connect
(
self
.
database_filename
)
self
.
open
(
)
self
.
create
()
def
__del__
(
self
):
""" destruct, close database handle
"""
self
.
close
()
def
close
(
self
):
""" close database
"""
self
.
_connection
.
close
()
def
open
(
self
):
""" open database
"""
self
.
_connection
=
sqlite3
.
connect
(
self
.
database_filename
)
def
create
(
self
,
force_recreate
=
False
):
""" create/initialize new database
:param force_recreate: if database already exists, remove old one first
...
...
@@ -63,26 +73,34 @@ class DB(object):
cur
.
executescript
(
open
(
init_script_filename
,
'rb'
)
.
read
())
cur
.
close
()
def
address_active
(
self
,
zoneid
,
ip_address
):
"""
check if address / network is active
def
sessions_per_address
(
self
,
zoneid
,
ip_address
=
None
,
mac_address
=
None
):
"""
fetch session(s) per (mac) address
:param zoneid: cp zone number
:param ip_address: ip address
(to unlock)
:param ip_address: ip address
:return: active status (boolean)
"""
cur
=
self
.
_connection
.
cursor
()
request
=
{
'zoneid'
:
zoneid
,
'ip_address'
:
ip_address
}
cur
.
execute
(
"""select count(*)
from cp_clients
where deleted = 0 and zoneid = :zoneid and ip_address = :ip_address"""
,
request
)
request
=
{
'zoneid'
:
zoneid
,
'ip_address'
:
ip_address
,
'mac_address'
:
mac_address
}
cur
.
execute
(
"""select cc.sessionid sessionId
, cc.authenticated_via authenticated_via
from cp_clients cc
where cc.deleted = 0
and cc.zoneid = :zoneid
and (
cc.ip_address = :ip_address
or
cc.mac_address = :mac_address
)"""
,
request
)
if
cur
.
fetchall
()[
0
][
0
]
>
0
:
return
True
else
:
return
False
result
=
[]
for
row
in
cur
.
fetchall
():
result
.
append
({
'sessionId'
:
row
[
0
],
'authenticated_via'
:
row
[
1
]})
return
result
def
add_client
(
self
,
zoneid
,
authenticated_via
,
username
,
ip_address
,
mac_address
):
""" add a new client to the captive portal administration
:param zoneid: cp zone number
:param authenticated_via: name/id of the authenticator or ---ip--- / ---mac--- for authentication by address
:param username: username, maybe empty
:param ip_address: ip address (to unlock)
:param mac_address: physical address of this ip
...
...
@@ -99,6 +117,7 @@ class DB(object):
cur
=
self
.
_connection
.
cursor
()
# set cp_client as deleted in case there's already a user logged-in at this ip address.
if
ip_address
is
not
None
and
ip_address
!=
''
:
cur
.
execute
(
"""UPDATE cp_clients
SET deleted = 1
WHERE zoneid = :zoneid
...
...
@@ -113,6 +132,19 @@ class DB(object):
self
.
_connection
.
commit
()
return
response
def
update_client_ip
(
self
,
zoneid
,
sessionid
,
ip_address
):
""" change client ip address
"""
cur
=
self
.
_connection
.
cursor
()
cur
.
execute
(
"""UPDATE cp_clients
SET ip_address = :ip_address
WHERE deleted = 0
and zoneid = :zoneid
AND sessionid = :sessionid
"""
,
{
'zoneid'
:
zoneid
,
'sessionid'
:
sessionid
,
'ip_address'
:
ip_address
})
self
.
_connection
.
commit
()
def
del_client
(
self
,
zoneid
,
sessionid
):
""" mark (administrative) client for removal
:param zoneid: zone id
...
...
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