# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2017-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>

@{TELEGRAM_WORK_DIR} = @{MOUNTS}/Kabi/telegram

@{exec_path} = @{bin}/telegram-desktop
profile telegram-desktop @{exec_path} {
  include <abstractions/base>
  include <abstractions/consoles>
  include <abstractions/dconf-write>
  include <abstractions/opencl-intel>
  include <abstractions/gtk>
  include <abstractions/fonts>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/freedesktop.org>
  include <abstractions/audio-client>
  include <abstractions/user-download-strict>
  include <abstractions/qt5>
  include <abstractions/qt5-compose-cache-write>
  include <abstractions/qt5-settings-write>
  include <abstractions/qt5-shader-cache>
  include <abstractions/mesa>
  include <abstractions/nameservice-strict>
  include <abstractions/enchant>
  include <abstractions/ssl_certs>

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

  @{exec_path} mrix,

  @{sh_path}             rix,

  # Launch external apps
  @{bin}/xdg-open   rCx -> open,

  # What's this for?
  deny @{bin}/fc-list  rx,

  # Telegram files
  /usr/share/TelegramDesktop/{,**} r,

  # Download dir
  owner @{TELEGRAM_WORK_DIR}/ rw,
  owner @{TELEGRAM_WORK_DIR}/** rwkl -> @{TELEGRAM_WORK_DIR}/#@{int},

  # Telegram's profile (via telegram -many -workdir ~/some/dir/)
  #owner @{TELEGRAM_WORK_DIR}/{,**} rw,

  # Autostart
  owner @{user_config_dirs}/autostart/telegramdesktop.desktop rw,

  owner @{tmp}/@{hex}-* rwk,
  owner @{run}/user/@{uid}/@{hex}-* rwk,

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

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

  /etc/fstab r,

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

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

  # Allowed apps to open
  @{lib}/firefox/firefox rPx,
  @{bin}/smplayer        rPx,
  @{bin}/viewnior        rPUx,
  @{bin}/qpdfview        rPx,
  @{bin}/geany           rPx,

  # file_inherit
  owner /dev/tty@{int} rw,


  profile open {
    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 @{TELEGRAM_WORK_DIR}/ r,

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

    # Allowed apps to open
    @{lib}/firefox/firefox rPx,
    @{bin}/smplayer        rPx,
    @{bin}/qpdfview        rPx,
    @{bin}/viewnior        rPUx,
    @{bin}/geany           rPx,

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

  }

  include if exists <local/telegram-desktop>
}
