2
0
mirror of https://gitlab.com/apparmor/apparmor synced 2025-09-10 11:15:20 +00:00
Files
apparmor/lib/config.py

61 lines
1.9 KiB
Python
Raw Normal View History

import configparser
2013-06-01 15:37:31 +05:30
import os
import shutil
2013-06-01 15:37:31 +05:30
import stat
import tempfile
2013-06-01 15:37:31 +05:30
confdir = '/etc/apparmor'
cfg = None
repo_cfg = None
def read_config(filename):
"""Reads the file and returns a configparser config[section][attribute]=property"""
config = configparser.ConfigParser()
2013-06-01 15:37:31 +05:30
filepath = confdir + '/' + filename
config.read(filepath)
2013-06-01 15:37:31 +05:30
# LP: Bug #692406
# Explicitly disabled repository
if filename == "repository.conf":
2013-06-01 16:11:55 +05:30
config['repository'] = {'enabled': 'no'}
2013-06-01 15:37:31 +05:30
return config
def write_config(filename, config):
"""Writes the given configparser to the specified file"""
2013-06-01 15:37:31 +05:30
filepath = confdir + '/' + filename
permission_600 = stat.S_IRUSR | stat.S_IWUSR # Owner read and write
2013-06-01 15:37:31 +05:30
try:
# Open a temporary file to write the config file
config_file = tempfile.NamedTemporaryFile('w', prefix='aa_temp', delete=False)
# Set file permissions as 0600
os.chmod(config_file.name, permission_600)
config.write(config_file)
config_file.close()
2013-06-01 15:56:56 +05:30
except IOError:
2013-06-01 15:37:31 +05:30
raise IOError("Unable to write to %s"%filename)
else:
# Move the temporary file to the target config file
shutil.move(config_file.name, filepath)
2013-06-01 15:37:31 +05:30
def find_first_file(file_list):
"""Returns name of first matching file None otherwise"""
# I don't understand why it searches the CWD, maybe I'll find out about it in some module
filename = None
if len(file_list):
for file in file_list.split():
if os.path.isfile(file):
filename = file
break
return filename
def find_first_dir(dir_list):
"""Returns name of first matching directory None otherwise"""
dirname = None
if (len(dir_list)):
for direc in dir_list.split():
if os.path.isdir(direc):
dirname = direc
break
return dirname