aboutsummaryrefslogtreecommitdiffstats
path: root/src/msec/config.py
diff options
context:
space:
mode:
authorEugeni Dodonov <eugeni@mandriva.org>2009-09-09 01:19:49 +0000
committerEugeni Dodonov <eugeni@mandriva.org>2009-09-09 01:19:49 +0000
commite7116c0765fbc33a0dc4d3caf86f918ef0456544 (patch)
tree0f93ed6d4ecf440b95d74c78bfe645359f5327c8 /src/msec/config.py
parent7b6b2a5027615b586ef01b77de1463e85c09bd96 (diff)
downloadmsec-e7116c0765fbc33a0dc4d3caf86f918ef0456544.tar
msec-e7116c0765fbc33a0dc4d3caf86f918ef0456544.tar.gz
msec-e7116c0765fbc33a0dc4d3caf86f918ef0456544.tar.bz2
msec-e7116c0765fbc33a0dc4d3caf86f918ef0456544.tar.xz
msec-e7116c0765fbc33a0dc4d3caf86f918ef0456544.zip
added support for exceptions
Diffstat (limited to 'src/msec/config.py')
-rw-r--r--src/msec/config.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/msec/config.py b/src/msec/config.py
index e30677b..af272bb 100644
--- a/src/msec/config.py
+++ b/src/msec/config.py
@@ -26,6 +26,7 @@ SECURITY_LEVEL="%s/etc/security/msec/level.%s"
# msec configuration file
SECURITYCONF = '/etc/security/msec/security.conf'
+EXCEPTIONSCONF = '/etc/security/msec/exceptions'
# permissions
PERMCONF = '/etc/security/msec/perms.conf'
@@ -307,6 +308,97 @@ class MsecConfig:
return True
# }}}
+# {{{ ExceptionConfig
+class ExceptionConfig:
+ """Exceptions configuration parser"""
+ def __init__(self, log, config=EXCEPTIONSCONF):
+ self.config = config
+ self.options = []
+ self.comments = []
+ self.log = log
+
+ def reset(self):
+ """Resets all configuration"""
+ del self.options
+ self.options = []
+ del self.comments
+ self.comments = []
+
+ def load(self):
+ """Loads and parses configuration file"""
+ if not self.config:
+ # No associated file
+ return True
+ try:
+ fd = open(self.config)
+ except:
+ self.log.error(_("Unable to load configuration file %s: %s") % (self.config, sys.exc_value[1]))
+ return False
+ for line in fd.readlines():
+ line = line.strip()
+ if not line:
+ continue
+ if line[0] == "#":
+ # comment
+ self.comments.append(line)
+ continue
+ try:
+ option, val = line.split(" ", 1)
+ self.options.append((option, val))
+ except:
+ self.log.warn(_("Bad config option: %s") % line)
+ continue
+ fd.close()
+ return True
+
+ def get(self, pos, default=None):
+ """Gets a configuration option, or defines it if not defined"""
+ if pos > len(self.options):
+ return default
+ return self.options[pos]
+
+ def remove(self, pos):
+ """Removes a configuration option."""
+ if pos < len(self.options):
+ del self.options[pos]
+
+ def set(self, pos, value):
+ """Sets a configuration option"""
+ if pos > 0:
+ print "Pos: %d" % pos
+ self.options[pos] = value
+ else:
+ self.options.append(value)
+
+ def list_options(self):
+ """Sorts and returns configuration parameters"""
+ sortedparams = self.options
+ if sortedparams:
+ sortedparams.sort()
+ return sortedparams
+
+ def save(self):
+ """Saves configuration. Comments go on top"""
+ if not self.config:
+ # No associated file
+ return True
+ try:
+ fd = open(self.config, "w")
+ except:
+ self.log.error(_("Unable to save %s: %s") % (self.config, sys.exc_value))
+ return False
+ for comment in self.comments:
+ print >>fd, comment
+ # sorting keys
+ for option,value in self.options:
+ # TODO: integrate with remove()
+ if value == None or value == OPTION_DISABLED:
+ self.log.debug("Skipping %s" % option)
+ else:
+ print >>fd, "%s %s" % (option, value)
+ return True
+# }}}
+
# {{{ PermConfig
class PermConfig(MsecConfig):
"""Msec file permission parser"""