# 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/3.0>,

include <tunables/global>

@{exec_path} = @{bin}/gtk{,2,3}-youtube-viewer
profile gtk-youtube-viewer @{exec_path} {
  include <abstractions/base>
  include <abstractions/fonts>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/freedesktop.org>
  include <abstractions/gtk>
  include <abstractions/nameservice-strict>
  include <abstractions/perl>
  include <abstractions/ssl_certs>

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

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

  @{sh_path}              rix,

  @{bin}/xterm            rCx -> xterm,
  @{bin}/rxvt             rCx -> xterm,
  @{bin}/urxvt            rCx -> xterm,

  # Players
  @{bin}/mpv              rPx,
  @{bin}/vlc              rPx,
  @{bin}/smplayer         rPx,

  @{lib}/firefox/firefox  rPx,

  @{bin}/xdg-open                                    rCx -> open,
  @{lib}/@{multiarch}/glib-[0-9]*/gio-launch-desktop rCx -> open,

  owner @{user_config_dirs}/youtube-viewer/{,*} rw,

  owner @{user_cache_dirs}/ rw,
  owner @{user_cache_dirs}/youtube-viewer/ rw,

  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/mounts r,


  profile xterm {
    include <abstractions/base>
    include <abstractions/consoles>
    include <abstractions/fonts>
    include <abstractions/fontconfig-cache-read>
    include <abstractions/freedesktop.org>
    include <abstractions/nameservice-strict>
    include <abstractions/perl>
    include <abstractions/wutmp>

    signal (send) set=(hup, winch) peer=youtube-viewer,
    signal (send) set=(hup, winch) peer=youtube-viewer//wget,

    @{bin}/xterm mr,
    @{bin}/rxvt  mr,
    @{bin}/urxvt mr,

    @{bin}/zsh            rix,
    @{sh_path}            rix,

    @{bin}/youtube-viewer rPx,

    owner @{PROC}/@{pid}/loginuid r,

    /etc/shells r,
    /etc/zsh/* r,

    /etc/X11/app-defaults/* r,

    /usr/include/X11/bitmaps/vlines2 r,

    owner @{HOME}/.urxvt/** r,

    owner @{HOME}/.Xauthority r,
    owner @{HOME}/.ICEauthority r,

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

  }

  profile open {
    include <abstractions/base>
    include <abstractions/xdg-open>

    @{bin}/xdg-open mr,
    @{lib}/@{multiarch}/glib-[0-9]*/gio-launch-desktop 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/gtk-youtube-viewer>
}
