#!/usr/bin/env python
"""
Script to enable or disable rules specified on commandline.
Examples:
Enable the shaping rule named "Shape All":
ruleset_disable_enable_rules.py --host 192.168.1.25 --shaping --enable "Shape All"
Enable all shaping or filtering rules matching "Global *"
ruleset_disable_enable_rules.py --host 192.168.1.25 --use-fnmatch --shaping --filtering --enable "Global *"
Enable filtering rules named "Spam", "Egg" or "Ham"
ruleset_disable_enable_rules.py --host 192.168.1.25 --filtering --enable "Spam" --enable "Egg" --enable "Ham"
Enable filtering rules named "Spam" and "Egg", disable rule named "Ham"
ruleset_disable_enable_rules.py --host 192.168.1.25 --filtering --enable "Spam" --enable "Egg" --disable "Ham"
Disable all rules
ruleset_disable_enable_rules.py --host 192.168.1.25 --filtering --shaping --statistics --use-fnmatch --disable "*"
"""
###############################################################################
#
# NO WARRANTY
#
# BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
# FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
# TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
#
# IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
# WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
# REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
# INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
# TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
# PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################
import packetlogic2
import optparse
import fnmatch
import operator
import sys
#
# Parse options
#
parser = optparse.OptionParser()
grp_host = optparse.OptionGroup(parser, "PacketLogic host options", "Options determining how to connect to the PacketLogic system")
grp_host.add_option("--host", dest="host", default="192.168.1.25", action="store",
help="PacketLogic host to connect to")
grp_host.add_option("--user", dest="user", default="admin", action="store",
help="PacketLogic user to authenticate with")
grp_host.add_option("--pass", dest="passwd", default="pldemo00", action="store",
help="PacketLogic passwd to authenticate with")
parser.add_option_group(grp_host)
grp_act = optparse.OptionGroup(parser, "Rule actions", "Options determining how to change the ruleset")
grp_act.add_option("--shaping", dest="shaping", default=False, action="store_true",
help="Enable/disable shaping rules")
grp_act.add_option("--filtering", dest="filtering", default=False, action="store_true",
help="Enable/disable filtering rules")
grp_act.add_option("--statistics", dest="statistics", default=False, action="store_true",
help="Enable/disable statistics rules")
grp_act.add_option("--enable", dest="enable", default=[], action="append",
help="enable rule named RULE", metavar="RULE")
grp_act.add_option("--disable", dest="disable", default=[], action="append",
help="disable rule named RULE", metavar="RULE")
grp_act.add_option("--use-fnmatch", dest="usefnmatch", default=False, action="store_true",
help="use file name matching wildcards instead of exact rule names")
parser.add_option_group(grp_act)
(options, args) = parser.parse_args()
#
# Validate options
#
if len(args) != 0:
parser.error("Unexpected extra arguments")
if len(options.enable) == 0 and len(options.disable) == 0:
parser.error("Nothing to do (specify --enable or --disable arguments)")
if not (options.filtering or options.shaping or options.statistics):
parser.error("Nothing to do (no rule type specified)")
#
# Connect to PacketLogic system
#
try:
pl = packetlogic2.connect(options.host, options.user, options.passwd)
except:
t, v, tb = sys.exc_info()
print "Error: Couldn't connect: %s" % v
sys.exit(1)
#
# Get "Rules and Objects" resource
#
rs = pl.Ruleset()
#
# Define function to do requested changes
#
def handle(options, rulelist):
if options.usefnmatch:
matchfunc = fnmatch.fnmatch
else:
matchfunc = operator.eq
for r in rulelist:
for e in options.enable:
if matchfunc(r.name, e) and not r.active:
r.enable()
print "Enabling rule %s" % r.name
for d in options.disable:
if matchfunc(r.name, d) and r.active:
r.disable()
print "Disabling rule %s" % r.name
#
# List and handle specified rule types
#
if options.filtering:
handle(options, rs.fwrule_list())
if options.shaping:
handle(options, rs.shapingrule_list())
if options.statistics:
handle(options, rs.statisticsrule_list())
#
# Commit changes
#
rs.commit()