Home | History | Annotate | Download | only in sepolgen
      1 # Authors: Karl MacMillan <kmacmillan (at] mentalrootkit.com>
      2 #
      3 # Copyright (C) 2006 Red Hat 
      4 # see file 'COPYING' for use and warranty information
      5 #
      6 # This program is free software; you can redistribute it and/or
      7 # modify it under the terms of the GNU General Public License as
      8 # published by the Free Software Foundation; version 2 only
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public License
     16 # along with this program; if not, write to the Free Software
     17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     18 #
     19 import sys
     20 
     21 tokens = ('DEFINE',
     22           'NAME',
     23           'TICK',
     24           'SQUOTE',
     25           'OBRACE',
     26           'CBRACE',
     27           'SEMI',
     28           'OPAREN',
     29           'CPAREN',
     30           'COMMA')
     31 
     32 reserved = {
     33     'define' : 'DEFINE' }
     34 
     35 t_TICK      = r'\`'
     36 t_SQUOTE    = r'\''
     37 t_OBRACE    = r'\{'
     38 t_CBRACE    = r'\}'
     39 t_SEMI      = r'\;'
     40 t_OPAREN    = r'\('
     41 t_CPAREN    = r'\)'
     42 t_COMMA     = r'\,'
     43 
     44 t_ignore    = " \t\n"
     45 
     46 def t_NAME(t):
     47     r'[a-zA-Z_][a-zA-Z0-9_]*'
     48     t.type = reserved.get(t.value,'NAME')
     49     return t
     50 
     51 def t_error(t):
     52     print "Illegal character '%s'" % t.value[0]
     53     t.skip(1)
     54 
     55 import lex
     56 lex.lex()
     57 
     58 def p_statements(p):
     59     '''statements : define_stmt
     60                   | define_stmt statements
     61     '''
     62     if len(p) == 2:
     63         p[0] = [p[1]]
     64     else:
     65         p[0] = [p[1]] + [p[2]]
     66 
     67 def p_define_stmt(p):
     68     # This sucks - corresponds to 'define(`foo',`{ read write }')
     69     '''define_stmt : DEFINE OPAREN TICK NAME SQUOTE COMMA TICK list SQUOTE CPAREN
     70     '''
     71     
     72     p[0] = [p[4], p[8]]
     73 
     74 def p_list(p):
     75     '''list : NAME
     76             | OBRACE names CBRACE
     77     '''
     78     if p[1] == "{":
     79         p[0] = p[2]
     80     else:
     81         p[0] = [p[1]]
     82 
     83 def p_names(p):
     84     '''names : NAME
     85              | NAME names
     86     '''
     87     if len(p) == 2:
     88         p[0] = [p[1]]
     89     else:
     90         p[0] = [p[1]] + p[2]
     91 
     92 def p_error(p):
     93     print "Syntax error on line %d %s [type=%s]" % (p.lineno, p.value, p.type)
     94     
     95 import yacc
     96 yacc.yacc()
     97 
     98 
     99 f = open("all_perms.spt")
    100 txt = f.read()
    101 f.close()
    102 
    103 #lex.input(txt)
    104 #while 1:
    105 #    tok = lex.token()
    106 #    if not tok:
    107 #        break
    108 #    print tok
    109 
    110 test = "define(`foo',`{ read write append }')"
    111 test2 = """define(`all_filesystem_perms',`{ mount remount unmount getattr relabelfrom relabelto transition associate quotamod quotaget }')
    112 define(`all_security_perms',`{ compute_av compute_create compute_member check_context load_policy compute_relabel compute_user setenforce setbool setsecparam setcheckreqprot }')
    113 """
    114 result = yacc.parse(txt)
    115 print result
    116     
    117