# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2019-2021 Mikhail Morfikov
# Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
# SPDX-License-Identifier: GPL-2.0-only

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{bin}/del{user,group}
profile deluser /{,usr/}{,s}bin/del{user,group} flags=(complain) {
  include <abstractions/base>
  include <abstractions/perl>
  include <abstractions/consoles>
  include <abstractions/nameservice-strict>

  # The deluser command is issued as root and its task is to delete regular user accounts. It
  # optionally can remove user files (via --remove-home or --remove-all-files) or create a backup.
  # Because of that, the deluser command needs the following CAPs to be able to do so.
  capability dac_read_search,
  capability dac_override,

  @{exec_path} r,
  @{bin}/perl r,

  @{sh_path}        rix,

  @{bin}/userdel   rPx,
  @{bin}/groupdel  rPx,
  @{bin}/gpasswd    rPx,

  @{bin}/crontab    rPx,

  @{bin}/mount      rCx -> mount,

  /etc/adduser.conf r,
  /etc/deluser.conf r,

  owner /etc/shadow r,

  # This is for the "--remove-all-files" flag, which it used to remove all files owned by the user
  # that's going to be deleted. Basically it scans all the files in the system in each dir and look
  # for matches. This also includes files required by the "--remove-home" flag as well as the
  # "--backup" and --backup-to flags.
  /   r,
  /** rw,


  profile mount flags=(complain) {
    include <abstractions/base>

    @{bin}/mount mr,

    @{PROC}/@{pid}/mountinfo r,

    @{sys}/devices/virtual/block/**/name r,

  }

  include if exists <local/deluser>
}
