2016-10-01 20:57:09 +02:00
|
|
|
#! /usr/bin/python3
|
2013-09-28 20:43:06 +05:30
|
|
|
# ----------------------------------------------------------------------
|
|
|
|
# Copyright (C) 2013 Kshitij Gupta <kgupta8592@gmail.com>
|
2018-10-22 23:56:07 +02:00
|
|
|
# Copyright (C) 2014-2018 Christian Boltz <apparmor@cboltz.de>
|
2013-09-28 20:43:06 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of version 2 of the GNU General Public
|
|
|
|
# License as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# ----------------------------------------------------------------------
|
2013-09-12 14:42:15 +05:30
|
|
|
import argparse
|
|
|
|
|
2013-09-23 19:32:25 +05:30
|
|
|
import apparmor.aa
|
2013-09-17 22:30:48 +05:30
|
|
|
import apparmor.cleanprofile as cleanprofile
|
2014-07-28 00:24:26 +02:00
|
|
|
import apparmor.ui as aaui
|
2015-07-06 22:02:34 +02:00
|
|
|
from apparmor.fail import enable_aa_exception_handler
|
2014-02-11 16:23:21 -08:00
|
|
|
from apparmor.translations import init_translation
|
2022-08-07 20:32:07 -04:00
|
|
|
|
|
|
|
enable_aa_exception_handler() # setup exception handling
|
|
|
|
_ = init_translation() # setup module translations
|
2014-02-10 22:15:05 -08:00
|
|
|
|
2014-10-16 23:25:33 +02:00
|
|
|
parser = argparse.ArgumentParser(description=_('Merge the given profiles into /etc/apparmor.d/ (or the directory specified with -d)'))
|
2014-10-16 23:35:06 +02:00
|
|
|
parser.add_argument('files', nargs='+', type=str, help=_('Profile(s) to merge'))
|
2013-09-22 15:25:20 +05:30
|
|
|
parser.add_argument('-d', '--dir', type=str, help=_('path to profiles'))
|
2022-08-07 12:26:24 -04:00
|
|
|
# parser.add_argument('-a', '--auto', action='store_true', help=_('Automatically merge profiles, exits incase of *x conflicts'))
|
2020-10-25 15:48:41 +01:00
|
|
|
parser.add_argument('--configdir', type=str, help=argparse.SUPPRESS)
|
2013-09-12 14:42:15 +05:30
|
|
|
args = parser.parse_args()
|
|
|
|
|
2014-10-16 20:06:45 +02:00
|
|
|
args.other = None
|
2013-09-12 14:42:15 +05:30
|
|
|
|
2020-10-25 15:48:41 +01:00
|
|
|
apparmor.aa.init_aa(confdir=args.configdir, profiledir=args.dir)
|
2017-03-02 21:21:53 +00:00
|
|
|
|
2017-01-19 16:45:29 +01:00
|
|
|
profiles = args.files
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2014-10-16 20:06:45 +02:00
|
|
|
def find_profiles_from_files(files):
|
|
|
|
profile_to_filename = dict()
|
|
|
|
for file_name in files:
|
|
|
|
apparmor.aa.read_profile(file_name, True)
|
2020-05-08 22:40:38 +02:00
|
|
|
for profile_name in apparmor.aa.active_profiles.profiles_in_file(file_name):
|
2014-10-16 20:06:45 +02:00
|
|
|
profile_to_filename[profile_name] = file_name
|
2020-05-23 15:07:33 +02:00
|
|
|
apparmor.aa.reset_aa()
|
2014-10-16 20:06:45 +02:00
|
|
|
|
|
|
|
return profile_to_filename
|
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2014-10-16 20:06:45 +02:00
|
|
|
def find_files_from_profiles(profiles):
|
|
|
|
profile_to_filename = dict()
|
|
|
|
apparmor.aa.read_profiles()
|
|
|
|
|
|
|
|
for profile_name in profiles:
|
2018-10-22 22:51:34 +02:00
|
|
|
profile_to_filename[profile_name] = apparmor.aa.get_profile_filename_from_profile_name(profile_name, True)
|
2014-10-16 20:06:45 +02:00
|
|
|
|
2020-05-23 15:07:33 +02:00
|
|
|
apparmor.aa.reset_aa()
|
2014-10-16 20:06:45 +02:00
|
|
|
|
|
|
|
return profile_to_filename
|
2014-08-04 20:19:08 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2013-09-12 14:42:15 +05:30
|
|
|
def main():
|
2017-01-19 16:45:29 +01:00
|
|
|
base_profile_to_file = find_profiles_from_files(profiles)
|
2014-10-16 20:06:45 +02:00
|
|
|
|
2017-01-19 16:45:29 +01:00
|
|
|
profiles_to_merge = set(base_profile_to_file.keys())
|
2014-10-16 20:06:45 +02:00
|
|
|
|
|
|
|
user_profile_to_file = find_files_from_profiles(profiles_to_merge)
|
|
|
|
|
|
|
|
for profile_name in profiles_to_merge:
|
2014-10-16 23:25:33 +02:00
|
|
|
aaui.UI_Info("\n\n" + _("Merging profile for %s" % profile_name))
|
2014-10-16 20:06:45 +02:00
|
|
|
user_file = user_profile_to_file[profile_name]
|
|
|
|
base_file = base_profile_to_file.get(profile_name, None)
|
|
|
|
|
2020-05-14 23:27:50 +02:00
|
|
|
act(user_file, base_file, profile_name)
|
2014-10-16 20:06:45 +02:00
|
|
|
|
2020-05-23 15:07:33 +02:00
|
|
|
apparmor.aa.reset_aa()
|
2014-10-16 20:06:45 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2020-05-14 23:27:50 +02:00
|
|
|
def act(user_file, base_file, merging_profile):
|
|
|
|
mergeprofiles = Merge(user_file, base_file)
|
2022-08-07 12:26:24 -04:00
|
|
|
# Get rid of common/superfluous stuff
|
2016-10-01 20:09:36 +02:00
|
|
|
mergeprofiles.clear_common()
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2020-05-17 16:45:11 +02:00
|
|
|
mergeprofiles.ask_merge_questions()
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2020-05-17 16:45:11 +02:00
|
|
|
apparmor.aa.changed[merging_profile] = True # force asking to save the profile
|
|
|
|
apparmor.aa.save_profiles(True)
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2013-09-12 14:42:15 +05:30
|
|
|
class Merge(object):
|
2020-05-14 23:27:50 +02:00
|
|
|
def __init__(self, user, base):
|
2022-08-07 12:26:24 -04:00
|
|
|
# Read and parse base profile and save profile data, include data from it and reset them
|
2013-09-23 19:32:25 +05:30
|
|
|
apparmor.aa.read_profile(base, True)
|
2013-09-23 03:47:15 +05:30
|
|
|
self.base = cleanprofile.Prof(base)
|
2013-09-23 02:14:11 +05:30
|
|
|
|
2020-05-23 15:07:33 +02:00
|
|
|
apparmor.aa.reset_aa()
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
# Read and parse user profile
|
2014-07-29 12:39:12 +02:00
|
|
|
apparmor.aa.read_profile(user, True)
|
2013-09-23 03:47:15 +05:30
|
|
|
self.user = cleanprofile.Prof(user)
|
2013-09-22 22:51:30 +05:30
|
|
|
|
2013-09-12 14:42:15 +05:30
|
|
|
def clear_common(self):
|
2013-09-23 02:14:11 +05:30
|
|
|
deleted = 0
|
2014-07-29 12:39:12 +02:00
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
# Remove off the parts in base profile which are common/superfluous from user profile
|
2013-09-23 03:47:15 +05:30
|
|
|
user_base = cleanprofile.CleanProf(False, self.user, self.base)
|
2013-09-23 02:14:11 +05:30
|
|
|
deleted += user_base.compare_profiles()
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2017-01-19 16:54:47 +01:00
|
|
|
def ask_merge_questions(self):
|
2017-01-19 16:45:29 +01:00
|
|
|
other = self.base
|
2021-04-04 15:46:37 +02:00
|
|
|
log_dict = {'merge': apparmor.aa.split_to_merged(other.aa)}
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2016-10-01 20:20:27 +02:00
|
|
|
apparmor.aa.loadincludes()
|
2014-02-13 08:31:59 -08:00
|
|
|
|
2024-10-20 22:32:35 +02:00
|
|
|
apparmor.aa.load_sev_db()
|
2017-01-19 16:54:47 +01:00
|
|
|
|
2020-05-11 23:33:30 +02:00
|
|
|
# ask about preamble rules
|
|
|
|
apparmor.aa.ask_rule_questions(
|
2024-05-17 13:53:42 +02:00
|
|
|
other.active_profiles.files[other.filename], # prof_events aka log_dict
|
|
|
|
'[preamble]', # displayed profile name
|
|
|
|
self.user.active_profiles.files[self.user.filename], # profile to update
|
|
|
|
['abi', 'inc_ie'] # rule types - TODO: don't hardcode
|
2020-05-11 23:33:30 +02:00
|
|
|
)
|
|
|
|
|
2017-01-19 16:54:47 +01:00
|
|
|
apparmor.aa.ask_the_questions(log_dict)
|
2013-09-12 14:42:15 +05:30
|
|
|
|
2022-08-07 12:26:24 -04:00
|
|
|
|
2013-09-23 02:14:11 +05:30
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|