# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2020-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}/calibre{,-*} @{bin}/calibredb @{bin}/ebook{,-*}
@{exec_path} += @{bin}/fetch-ebook-metadata
@{exec_path} += @{bin}/lrs2lrf @{bin}/lrf2lrs @{bin}/lrfviewer @{bin}/web2disk
profile calibre @{exec_path} {
  include <abstractions/base>
  include <abstractions/bus-accessibility>
  include <abstractions/bus-session>
  include <abstractions/bus/org.a11y>
  include <abstractions/bus/org.gtk.vfs.MountTracker>
  include <abstractions/bus/org.freedesktop.UDisks2>
  include <abstractions/bus/org.kde.StatusNotifierWatcher>
  include <abstractions/desktop>
  include <abstractions/devices-usb>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/graphics>
  include <abstractions/gstreamer>
  include <abstractions/nameservice-strict>
  include <abstractions/python>
  include <abstractions/qt5-compose-cache-write>
  include <abstractions/qt5-settings-write>
  include <abstractions/qt5-shader-cache>
  include <abstractions/ssl_certs>
  include <abstractions/thumbnails-cache-read>
  include <abstractions/trash-strict>
  include <abstractions/user-download-strict>

  capability sys_ptrace,

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

  unix (send, receive) type=stream peer=(addr=none, label=xorg),
  unix (bind, listen)  type=stream addr="@*-calibre-gui.socket",
  unix (bind)          type=stream addr="@calibre-*",

  @{exec_path} mrix,

  @{sh_path}              rix,
  @{python_path}          rix,
  @{bin}/file             rix,
  @{bin}/ldconfig{,.real} rix,
  @{bin}/uname            rix,
  @{lib}/{,@{multiarch}/}qt{5,6}{,/libexec/}QtWebEngineProcess rix,

  @{bin}/pdftoppm        rPUx, # (#FIXME#)
  @{bin}/pdfinfo         rPUx,
  @{bin}/pdftohtml       rPUx,

  @{open_path}            rPx -> child-open,

  /usr/share/calibre/{,**} r,

  /etc/fstab r,
  /etc/inputrc r,
  /etc/magic r,
  /etc/mime.types r,

  owner @{HOME}/ r,
  owner "@{HOME}/Calibre Library/{,**}" rw,
  owner "@{HOME}/Calibre Library/metadata.db" rwk,
  owner @{user_documents_dirs}/{,**} rwl,
  owner @{user_books_dirs}/{,**} rwl,
  owner @{user_torrents_dirs}/{,**} rwl,
  owner @{user_work_dirs}/{,**} rwl,

  owner @{user_config_dirs}/calibre/ rw,
  owner @{user_config_dirs}/calibre/** rwk,

  owner @{user_share_dirs}/calibre-ebook.com/ rw,
  owner @{user_share_dirs}/calibre-ebook.com/** rwk,

  owner @{user_cache_dirs}/calibre/ rw,
  owner @{user_cache_dirs}/calibre/** rwkl -> @{user_cache_dirs}/calibre/**,

  owner @{tmp}/calibre_*_tmp_*/{,**} rw,
  owner @{tmp}/calibre-*/{,**} rw,
  owner @{tmp}/@{int}-*/ rw,
  owner @{tmp}/@{int}-*/** rwl,

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

  @{sys}/devices/@{pci}/irq r,

        @{PROC}/ r,
        @{PROC}/@{pids}/net/route r,
        @{PROC}/sys/fs/inotify/max_user_watches r,
        @{PROC}/sys/kernel/random/boot_id r,
        @{PROC}/sys/kernel/yama/ptrace_scope r,
        @{PROC}/vmstat r,
  owner @{PROC}/@{pid}/cmdline r,
  owner @{PROC}/@{pid}/comm r,
  owner @{PROC}/@{pid}/fd/ r,
  owner @{PROC}/@{pid}/mountinfo r,
  owner @{PROC}/@{pid}/mounts r,
  owner @{PROC}/@{pid}/oom_{,score_}adj rw,
  owner @{PROC}/@{pid}/stat{,m} r,
  owner @{PROC}/@{pid}/task/ r,
  owner @{PROC}/@{pid}/task/@{tid}/comm rw,
  owner @{PROC}/@{pid}/task/@{tid}/status r,

  owner /dev/tty@{int} rw,

  include if exists <local/calibre>
}

# vim:syntax=apparmor
