# 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}/adequate
profile adequate /{,usr/}{,s}bin/adequate flags=(complain) {
  include <abstractions/base>
  include <abstractions/consoles>
  include <abstractions/perl>
  include <abstractions/nameservice-strict>

  #capability sys_tty_config,

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

  @{bin}/ldconfig        rix,

  # It wants to ldd all binaries/libs in packages.
  @{bin}/ldd                 rCx -> ldd,

  # Think what to do about this (#FIXME#)
  /usr/share/debconf/frontend     rPx,
  #/usr/share/debconf/frontend    rCx -> frontend,

  @{bin}/pkg-config          rCx -> pkg-config,
  @{bin}/dpkg                rPx -> child-dpkg,
  # Do not strip env to avoid errors like the following:
  #  ERROR: ld.so: object 'libfakeroot-sysv.so' from LD_PRELOAD cannot be preloaded (cannot open
  #  shared object file): ignored.
  @{bin}/dpkg-query          rpx,
  #
  @{bin}/update-alternatives rPx,

  /var/lib/adequate/pending rwk,

  /etc/shadow r,

  /usr/share/python{,3}/debian_defaults r,
  /usr/share/doc/*/copyright r,
  /usr/share/**/__pycache__/ r,
  /usr/**/*.py r,


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

    @{bin}/ldd mr,

    @{bin}/*               mr,
    /usr/games/*           mr,
    @{lib}{,x}/**  mr,
    @{lib}/@{multiarch}/** mr,
    /usr/share/**                r,

    /opt/google/chrome{,-beta,-unstable}/google-chrome{,-beta,-unstable} mr,

    @{lib}/@{multiarch}/ld-*.so rix,
    @{lib}{,x}32/ld-*.so        rix,

  }

  profile frontend flags=(complain) {
    include <abstractions/base>
    include <abstractions/consoles>
    include <abstractions/perl>
    include <abstractions/nameservice-strict>

    /usr/share/debconf/frontend r,
    @{bin}/perl r,

    @{bin}/adequate   rPx,

    @{sh_path}        rix,
    @{bin}/stty       rix,
    @{bin}/locale     rix,

    /etc/debconf.conf r,
    owner /var/cache/debconf/{config,passwords,templates}.dat{,-new,-old} rwk,
    /usr/share/debconf/templates/adequate.templates r,

    # The following is needed when debconf uses GUI frontends.
    include <abstractions/gtk>
    include <abstractions/fonts>
    include <abstractions/fontconfig-cache-read>
    include <abstractions/freedesktop.org>
    capability dac_read_search,
    @{bin}/lsb_release rPx -> lsb_release,
    @{bin}/hostname    rix,
    owner @{PROC}/@{pid}/mounts r,
    @{HOME}/.Xauthority r,

    /etc/shadow r,

  }

  profile pkg-config flags=(complain) {
    include <abstractions/base>

    @{bin}/pkg-config mr,

  }

  include if exists <local/adequate>
}
