Commit graph

1520 commits

Author SHA1 Message Date
John Johansen
c4d1cfa487 Merge split off ask_rule_questions() from ask_the_questions()
Also rename aa.py delete_duplicates() and make ruletypes a parameter.

See the commit messages for details.

This reduces usage of global variables.

The final change is that aa-genprof now asks about preamble rules that are \*Rule classes (currently `abi` and `include if exists`).

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/532
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-18 08:16:48 +00:00
Christian Boltz
34d4d9f5af
Make save_profiles() usable for aa-mergeprof
... and replace the aa-mergeprof code that asks about saving the profile
with a call to save_profiles().
2020-05-17 16:50:01 +02:00
Christian Boltz
ed0f2fe505
aa-mergeprof: simplify internal parameter handover
Instead of stuffing two variables into a list and split them again, hand
them over as plain separate variables.
2020-05-14 23:29:31 +02:00
Christian Boltz
7f7fc973f8
valid_include(): move out unrelated check
valid_include() checks if the given include file exists or is
whitelisted in cfg['settings']['custom_includes'].

The check if that include file is already part of the profile is
unrelated to that. Move it to match_includes() where it fits much
better (and drop the now superfluous profile parameter from
valid_include())

In theory is_known_rule() should prevent that case from ever happening,
but let's restrict this commit to moving the code around and keep this
check just to be sure.

While on it, add some documentation to both functions.
2020-05-14 22:48:20 +02:00
Christian Boltz
e0f9b7cb07 Merge branch 'cboltz-fail-verbose' into 'master'
read_profile(): don't fail silently

See merge request apparmor/apparmor!530

Acked-by: Steve Beattie <steve.beattie@canonical.com> for 2.11..master
2020-05-12 19:43:44 +00:00
Christian Boltz
85f2cd4ed4
Let aa-mergeprof ask about more preamble (*Rule) rules
For now, that means aa-mergeprof will ask for `abi` and `include if
exists` rules (currently hardcoded).

This needs storing of `active_profiles` in the Prof object - the
preamble `abi` and `include if exists` rules are stored there.

Since several functions expect an `include` dict, add an empty one to
ProfileList to prevent lots of errors and breakage. It can be removed
again when handling of `include` rules gets moved to IncludeRule.
2020-05-11 23:41:25 +02:00
Christian Boltz
3e0f4e1dba
rename aa.py delete_duplicates() and make ruletypes a parameter
Rename delete_duplicates() to delete_all_duplicates() to make the
function easier grep-able - the *rule classes have delete_duplicates()
which might be confused with the old name.

Also hand over 'ruletypes' as parameter to delete_all_duplicates()
instead of using the global variable.
2020-05-11 23:03:23 +02:00
Christian Boltz
ca693db3b0
split off ask_rule_questions() from ask_the_questions()
ask_the_questions() stays the "main" function, loops over all profiles,
and calls ask_rule_questions() for each profile.

ask_rule_questions() asks the questions for all events in a specific
profile or hat.

This reduces the usage of global variables in ask_rule_questions().
2020-05-11 23:01:06 +02:00
Christian Boltz
e841c866d3
ProfileList: add __repr__() with list of filenames
Even if incomplete (it doesn't print the whole ProfileList content),
this should make debugging easier.
2020-05-11 22:46:29 +02:00
Christian Boltz
af8b9dc5bb
read_profile(): don't fail silently
If a profile file can't be read, print a warning so that the user is
aware of the problem.
2020-05-11 22:23:20 +02:00
Steve Beattie
da2ce3ac67 utils: Use ProfileList to handle aliases
Merge branch 'cboltz-alias' into 'master'

Acked-by: Steve Beattie <steve.beattie@canonical.com>
MR: https://gitlab.com/apparmor/apparmor/-/merge_requests/526
2020-05-11 08:51:42 +00:00
Christian Boltz
cb44e6d47d Merge branch 'cboltz-drop-is-active-profile' into 'master'
Drop is_active_profile()

See merge request apparmor/apparmor!529

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-11 08:37:16 +00:00
Christian Boltz
a5434e562d Merge branch 'cboltz-drop-write-rules' into 'master'
Drop / inline write_rules()

See merge request apparmor/apparmor!528

Acked-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-10 23:03:47 +00:00
Christian Boltz
710d5ae466
Drop is_active_profile()
... and inline it at the only place that uses it
2020-05-10 23:49:16 +02:00
Christian Boltz
5c48eb7aa7
Drop / inline write_rules()
The TODO in write_rules() was long solved - remove the safety net type
check, it's no longer needed.

Without this, the function becomes a simple .get_rules_clean() call
which can easily be inlined into write_piece() at the two places where
it's called.
2020-05-10 18:09:14 +02:00
Christian Boltz
3c9c162951
write_alias(): drop depth parameter
... which is always '0' anyways, and therefore superfluous
2020-05-10 17:42:55 +02:00
Christian Boltz
8855c60f23
move write_alias to ProfileList
It's only needed in the preamble, which makes ProfileList the perfect
place.
2020-05-10 17:40:53 +02:00
Christian Boltz
fc6bd72781
Drop alias handling from ProfileStorage
ProfileStorage handles the profile content, but not the preamble. This
also means it will never see any aliases.
2020-05-10 17:39:02 +02:00
Christian Boltz
c636580f18
apparmor.vim: allow leading whitespace for alias rules 2020-05-10 17:24:09 +02:00
Christian Boltz
c0da20cf2c
Use ProfileList to handle aliases 2020-05-10 13:37:05 +02:00
Christian Boltz
77a22979ed
ProfileList: Add support for alias rules
... and also some tests
2020-05-10 13:37:05 +02:00
John Johansen
13bea6f4a7 Merge Introduce and use AbiRule to handle abi rules
See the individual commits for details.

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/525
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-10 02:24:01 +00:00
Christian Boltz
66d3d92de3
Use AbiRule in ProfileStorage (for abi rules inside profiles)
This also drops the last usage of write_abi(), drop it.
2020-05-09 21:02:06 +02:00
Christian Boltz
d003cf814c
Use AbiRule for profile preamble 2020-05-09 21:02:06 +02:00
Christian Boltz
5efccd146e
ProfileList: Use AbiRuleset and add add_abi()
... and also tests for it
2020-05-09 21:02:06 +02:00
Christian Boltz
50d896705f
Add AbiRule and testsuite
This is probably the shortest rule class we'll ever have because it can
inherit most things from IncludeRule.
2020-05-09 21:02:06 +02:00
Christian Boltz
3d13ddb30a
Un-hardcode 'include' and 'IncludeRule' in IncludeRule
... as a preparation to recycle the class
2020-05-09 21:02:06 +02:00
Christian Boltz
e9557284a3
re_match_include_parse() raise bug exceptions only for include
... but not for abi rules, which (according to the simple_tests
profiles) do not share these bugs)

For unquoted paths, make sure that the path doesn't include whitespace.
2020-05-09 21:02:06 +02:00
Christian Boltz
8ca486715f
extend re_match_include_parse() to also match abi rules
... and add some tests
2020-05-09 20:08:48 +02:00
Christian Boltz
b96fd81184
split out common parts of RE_INCLUDE and RE_ABI
... into RE_MAGIC_OR_QUOTED_PATH
2020-05-09 18:57:25 +02:00
Christian Boltz
874d3385a3
Stop writing to filelist[file]['profiles']
... because after the previous three commits, nothing reads/needs this
anymore

Note: file_name in ask_exec() was only used in the (dropped) filelist
usage.
2020-05-08 23:00:35 +02:00
Christian Boltz
4e09f315c3
Use active_profiles.profiles_in_file()
... instead of filelist[file_name]['profiles']
2020-05-08 22:40:38 +02:00
Christian Boltz
01e46ab453
Add profiles_in_file() to ProfileList
... and some tests for it.
2020-05-08 22:37:45 +02:00
Christian Boltz
34a0457090
parse_profile_data(): better way to check for duplicate hats
Instead of checking filelist[file]['profiles'] for duplicate hats, check
profile_data[profile][hat].

With this, the duplicate hat check is done in the same way as the check
for duplicate profiles and child profiles.

Also add tests for duplicate child profiles and duplicate hats.
2020-05-08 22:15:10 +02:00
Christian Boltz
48bae9e3b0
less shell ;-)
Using a list as parameter for subprocess.call is more secure, and avoids
funny problems if the filename to display contains spaces or other funny
characters.
2020-05-07 19:45:39 +02:00
Christian Boltz
d459cbf0f4
drop unused get_pager()
The only usage was dropped/replaced when switching to UI_ShowFile() in
https://gitlab.com/apparmor/apparmor/-/merge_requests/514
2020-05-07 12:13:20 +02:00
Christian Boltz
986b4f2b72 Merge branch 'cboltz-fix-genprof-json' into 'master'
Fix showing the local inactive profile in json mode

See merge request apparmor/apparmor!514

Acked-by: John Johansen <john.johansen@canonical.com> for 2.11..master
2020-05-06 22:43:35 +00:00
Christian Boltz
68a258b006
Fix showing the local inactive profile in json mode
When aa-genprof proposes a local inactive profile, it had a hardcoded
call to 'less' to display that profile.

Unsurprisingly, this doesn't work in JSON mode and breaks YaST (luckily
it's only a case of "the button doesn't work").

References: https://bugzilla.opensuse.org/show_bug.cgi?id=1171315
2020-05-06 23:20:07 +02:00
Christian Boltz
bb3803b931
Split off UI_ShowFile() from UI_Changes
UI_ShowFile() is more generic and can be used to display various (text)
files, not only diffs.
2020-05-06 23:16:47 +02:00
Christian Boltz
4906bbf965
get_profile(): get rid of profile_type
It's always set to 'INACTIVE_LOCAL' - no need to set and later check it.

Also add a TODO note ;-)
2020-05-06 22:53:57 +02:00
Christian Boltz
f96304fe33
get_profile(): get rid of profile_data variable
... which also makes the code easier to understand
2020-05-06 22:53:22 +02:00
Christian Boltz
476848da46
get_profile: convert profile_hash from hasher() to dict 2020-05-06 22:53:22 +02:00
Christian Boltz
47620e10cb
Rewrite get_profile() to exit early if no extra profile exists 2020-05-06 22:53:22 +02:00
Christian Boltz
2d2fc41cc1
inline get_inactive_profile() into get_profile()
... which is the only user of get_inactive_profile()
2020-05-06 22:53:22 +02:00
Steve Beattie
e6cf0d44d1 utils + parser: minor test cleanups
Fix a typo, some shellcheck warnings, and move one test script from using bash to dash.

See merge request apparmor/apparmor!512

Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: Christian Boltz <opensuse@cboltz.de>
https://gitlab.com/apparmor/apparmor/-/merge_requests/512
2020-05-06 20:35:28 +00:00
Christian Boltz
c7949ba6c4
Read all profiles on aa-genprof startup instead of later
... which can mean "too late" in some special cases (if a profile
already exists in /etc/apparmor.d/$non_default_filename).

However, the main reason is that without this change
- the new profile will be added to (otherwise empty) active_profiles
- the first do_logprof_pass() will read all profiles, including the new
  one, and add them to active_profiles - which unsurprisingly results in
  an error like `ERROR: Profile /usr/sbin/vsftpd exists in
  /etc/apparmor.d/usr.sbin.vsftpd and /etc/apparmor.d/usr.sbin.vsftpd`

To fix this,
- change do_logprof_pass to never call read_profiles() (and get rid of
  the 'passno' parameter)
- adjust its callers (aa-logprof and aa-genprof) to call read_profiles()
  themself
- move printing the 'Updating AppArmor profiles in $directory.' message
  to read_profiles(), but only display it if requested (to keep the
  current UI behaviour)
2020-05-05 23:56:55 +02:00
Christian Boltz
db9f8d38b0
Fix aa-genprof/aa-autodep crash
Since 4b7108f1e5, aa-genprof and
aa-autodep crashed with

    apparmor.common.AppArmorBug: /etc/apparmor.d/usr.sbin.vsftpd not listed in ProfileList files

because the profile was not added to active_profiles.

Add the profile to active_profiles to fix this regression, and to have
all the needed data in active_profiles.
2020-05-05 22:24:08 +02:00
Steve Beattie
253073b798
utils test: fix typo in simple_tests output
Signed-off-by: Steve Beattie <steve.beattie@canonical.com>
2020-05-05 11:54:49 -07:00
Christian Boltz
256f55925c
Drop profile repo code from utils
The profile repo is dead since years and most likely won't come back, so
there's no point in keeping and maintaining the code for uploading and
downloading profiles.
2020-05-05 13:09:16 +02:00
John Johansen
5d52ab0783 Merge Handle preamble "include if exists" rules in ProfileList
Add support for handling `include if exists` rules in profile preambles in the `ProfileList` class.

Besides that, do some groundwork for future changes - see the individual commits for details and spoilers ;-)

PR: https://gitlab.com/apparmor/apparmor/-/merge_requests/507
Acked-by: Steve Beattie <steve.beattie@canonical.com>
Acked-by: John Johansen <john.johansen@canonical.com>
2020-05-05 07:18:09 +00:00