ruleset.py
changeset 32 4059dbe9f26a
parent 23 9fa62cfd2821
equal deleted inserted replaced
31:d54720d47ca2 32:4059dbe9f26a
     3 # Paul Crowley <paul@lshift.net>
     3 # Paul Crowley <paul@lshift.net>
     4 #
     4 #
     5 # This software may be used and distributed according to the terms
     5 # This software may be used and distributed according to the terms
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
       
     8 import sys
     8 import re
     9 import re
     9 
    10 
    10 allowedchars = "A-Za-z0-9_-"
    11 allowedchars = "A-Za-z0-9_-"
    11 
    12 
    12 goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
    13 goodpathre = re.compile("([%s]+/)*[%s]+$" % (allowedchars, allowedchars))
    47     '''Class representing the rules in a rule file'''
    48     '''Class representing the rules in a rule file'''
    48     
    49     
    49     levels = ["init", "write", "read", "deny"]
    50     levels = ["init", "write", "read", "deny"]
    50 
    51 
    51     def __init__(self):
    52     def __init__(self):
    52         self.rules = []
    53         # The user called "root" automatically has the highest
       
    54         # privilege
       
    55         self.rules = [(self.levels[0], rule([('user', 'root')]))]
    53         self.preset = {}
    56         self.preset = {}
    54 
    57 
    55     def add(self, action, conditions):
    58     def add(self, action, conditions):
    56         self.rules.append((action, conditions))
    59         self.rules.append((action, conditions))
    57 
    60 
    72         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    75         return a in self.levels and self.levels.index(a) <= self.levels.index(level)
    73     
    76     
    74     @classmethod
    77     @classmethod
    75     def readfile(cls, fn):
    78     def readfile(cls, fn):
    76         res = cls()
    79         res = cls()
    77         f = open(fn)
       
    78         try:
    80         try:
    79             for l in f:
    81             f = open(fn)
    80                 l = l.strip()
    82             try:
    81                 if len(l) == 0 or l.startswith("#"):
    83                 for l in f:
    82                     continue
    84                     l = l.strip()
    83                 l = l.split()
    85                     if len(l) == 0 or l.startswith("#"):
    84                 res.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
    86                         continue
    85         finally:
    87                     l = l.split()
    86             f.close()
    88                     res.add(l[0], rule([c.split("=", 1) for c in l[1:]]))
       
    89             finally:
       
    90                 f.close()
       
    91         except Exception, e:
       
    92             print >> sys.stderr, "Failure reading rules file:", e
       
    93             return cls()
    87         return res
    94         return res
       
    95