captive.whump.shanti-portal/tools/manage_client.py

79 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2017-02-22 13:41:58 +00:00
# Python helper tool to add IPtables rule using the iptc library. This must
# of course run as root for iptc to work.
2017-03-06 15:03:57 +00:00
from sys import exit
2017-03-03 00:04:12 +00:00
from argparse import ArgumentParser, FileType
from pprint import pprint as pp
2017-03-03 00:04:12 +00:00
from configparser import RawConfigParser
2017-03-06 15:03:57 +00:00
import errors
from storage import StoragePostgres
2017-03-03 00:04:12 +00:00
from client import Client
parser = ArgumentParser((
'Handle clients in the captive portal. Default mode of operation is to'
' create new clients and enable them. Other mode is to --disable the '
'client. And last mode is to --delete the client completely.'
))
parser.add_argument(
'--disable',
default=False,
2017-03-07 16:21:52 +00:00
action='store_true',
help='Disable the client in the DB and delete from firewall'
)
parser.add_argument(
'--delete',
default=False,
2017-03-07 16:21:52 +00:00
action='store_true',
help='Delete the client from DB and firewall'
)
2017-03-03 00:04:12 +00:00
parser.add_argument(
'--protocol',
required=True,
choices=['tcp', 'udp'],
help='Protocol for client'
)
parser.add_argument(
'--config',
type=FileType('r'),
required=True,
help='Configuration file'
)
parser.add_argument(
'src_ip',
help='Client source IP to add'
)
args = parser.parse_args()
2017-03-03 00:04:12 +00:00
config = RawConfigParser()
config.readfp(args.config)
2017-03-06 15:03:57 +00:00
sr = StoragePostgres(config=config)
try:
client = Client(
storage=sr,
ip_address=args.src_ip,
protocol=args.protocol,
chain=config.get('iptables', 'chain')
)
except errors.StorageNotFound:
print('Client not found')
2017-03-06 15:03:57 +00:00
exit(1)
if args.disable:
# For non-existing clients this actually creates them in disabled mode.
client.enabled = False
client.commit()
elif args.delete:
client.delete()
else:
client.enabled = True
client.commit()