#!/usr/bin/python -E

################################################################################
#                                                                              #
# The RBACPP Self Test Helper                                                  #
#                                                                              #
# Writes, reads, and deletes test files in /var/run for rbac-self-test.        #
#                                                                              #
# Copyright (C) 2007 IBM Corporation                                           #
# Licensed under GNU General Public License                                    #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation; either version 2 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# This program is distributed in the hope that it will be useful,              #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with this program; see the file COPYING.  If not, write to             #
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        #
#                                                                              #
# Author: George C. Wilson <gcwilson@us.ibm.com>                               #
#                                                                              #
################################################################################

import sys
import os
import selinux

def main():

    f = None
    rc = 0
    datafile_basename = '/var/run/rbac-self-test-'

    if len(sys.argv) == 3:
        context = sys.argv[1]
        op = sys.argv[2]
        datafile_name = datafile_basename + context
        if (op != 'r') and (op != 'w') and (op != 'd'):
            print 'op must be either \'r\', \'w\', or \'d\''
            rc = 1
    else:
        print 'usage: helper <context> <op>'
        rc = 1

    if rc == 0:

        if (op == 'd'):

            try:
                os.unlink(datafile_name)
            except:
                rc = 1

        else:

            try:
                f = open(datafile_name , op)
            except:
                rc = 1

            if rc == 0:
                if op == 'w':
                    try:
                        f.write(context)
                    except:
                        rc = 1
                elif op == 'r':
                    try:
                        junk = f.read()
                    except:
                        rc = 1
                else:
                    print 'Bad file operator ' + op
                    rc = 1

            if f != None:
                try:
                    f.close()
                except:
                    rc = 1

    return(rc)

if __name__ == "__main__":
    sys.exit(main())
