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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{bin}/qnapi
profile qnapi /{,usr/}{,s}bin/qnapi flags=(complain) {
  include <abstractions/base>
  include <abstractions/dri-enumerate>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/fonts>
  include <abstractions/freedesktop.org>
  include <abstractions/gtk>
  include <abstractions/mesa>
  include <abstractions/nameservice-strict>
  include <abstractions/qt5-compose-cache-write>
  include <abstractions/qt5-settings-write>
  include <abstractions/user-download-strict>
  include <abstractions/X>

  network inet dgram,
  network inet6 dgram,
  network inet stream,
  network inet6 stream,
  network netlink raw,
  network netlink dgram,

  # Some apps can use qnapi to automate downloading of subtitles. When a user wants to abort the
  # action (stop qnapi), the apps send the term/kill signal to qnapi.
  signal (receive) set=(kill, term),

  @{exec_path} mr,

  @{bin}/7z        rix,
  @{lib}/p7zip/7z  rix,

  @{bin}/ffprobe  rPx,
  @{bin}/xdg-open  rCx -> open,
  @{lib}/firefox/firefox rPx,

  /usr/share/qt5ct/** r,
  /usr/share/hwdata/pnp.ids r,

  /etc/fstab r,
  /etc/machine-id r,
  /var/lib/dbus/machine-id r,

  owner @{HOME}/ r,
  owner @{user_music_dirs}/{,**} rw,
  owner @{user_pictures_dirs}/{,**} rw,
  owner @{user_torrents_dirs}/{,**} rw,
  owner @{user_videos_dirs}/{,**} rw,

  owner @{user_config_dirs}/qnapi.ini rw,
  owner @{user_config_dirs}/qnapi.ini.lock rwk,
  owner @{user_config_dirs}/qnapi.ini.* rwl -> @{user_config_dirs}/#@{int},
  owner @{user_config_dirs}/qnapi.ini.mlXXXY rwl -> @{user_config_dirs}/#@{int},

  owner @{user_config_dirs}/qt5ct/{,**} r,
  owner @{user_cache_dirs}/ rw,

        /tmp/ r,
  owner /tmp/@{hex}.* rw,
  owner /tmp/** rw,
  owner /tmp/#@{int} rw,
  owner /tmp/QNapi-*-rc wl -> /tmp/#@{int},
  owner /tmp/QNapi-*-rc.lock rwk,
  owner /tmp/QNapi.@{int}.tmp rw,
  owner /tmp/QNapi.@{int}.tmp.* rw,
  owner /tmp/QNapi.@{int}.tmp.* rwl -> /tmp/#@{int},
  owner /tmp/QNapi.@{int} rw,

  owner /dev/shm/#@{int} rw,

  deny owner @{PROC}/@{pid}/cmdline r,
       owner @{PROC}/@{pid}/mountinfo r,
       owner @{PROC}/@{pid}/mounts r,
  deny       @{PROC}/sys/kernel/random/boot_id r,

  owner /dev/tty@{int} rw,

  profile open flags=(complain) {
    include <abstractions/base>
    include <abstractions/xdg-open>

    @{bin}/xdg-open mr,

    @{sh_path}             rix,
    @{bin}/{m,g,}awk       rix,
    @{bin}/readlink        rix,
    @{bin}/basename        rix,

    owner @{HOME}/ r,

    owner @{run}/user/@{uid}/ r,

    # Allowed apps to open
    @{lib}/firefox/firefox rPUx,

    # file_inherit
    owner @{HOME}/.xsession-errors w,

  }

  include if exists <local/qnapi>
}
