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

@{JD_INSTALLDIR} = /home/*/jd2

@{exec_path} = @{JD_INSTALLDIR}/*JDownloader*
profile jdownloader @{exec_path} {
  include <abstractions/base>
  include <abstractions/freedesktop.org>
  include <abstractions/fonts>
  include <abstractions/fontconfig-cache-read>
  include <abstractions/freedesktop.org>
  include <abstractions/user-download-strict>
  include <abstractions/nameservice-strict>

  @{exec_path} rix,

  @{bin}/basename rix,
  @{bin}/dirname  rix,
  @{bin}/expr     rix,
  @{bin}/cut      rix,
  @{bin}/ls       rix,
  @{bin}/{,e}grep rix,
  @{bin}/find     rix,
  @{bin}/sed      rix,
  @{bin}/chmod    rix,

  @{bin}/ffmpeg   rPx,

  # These are needed when the above tools are in some nonstandard locations
  #@{bin}/which{,.debianutils}   rix,
  #/usr/ r,
  #/usr/local/ r,
  #@{bin}/ r,
  #@{lib}/ r,

  deny /opt/ r,

  owner @{HOME}/ r,
  owner @{JD_INSTALLDIR}/ rw,
  owner @{JD_INSTALLDIR}/** rwk,
  owner @{JD_INSTALLDIR}/jre/bin/java rix,
  owner @{JD_INSTALLDIR}/jre/lib/*/jli/libjli.so mrw,
  owner @{JD_INSTALLDIR}/jre/lib/*/server/libjvm.so mrw,
  owner @{JD_INSTALLDIR}/jre/lib/*/*.so mrw,
  owner @{JD_INSTALLDIR}/tmp/jna/jna@{int}.tmp mrw,
  owner @{JD_INSTALLDIR}/tmp/7zip/SevenZipJBinding-*/lib7-Zip-JBinding.so mrw,

  owner @{HOME}/.oracle_jre_usage/@{hex}.timestamp rw,
  owner @{HOME}/.java/.userPrefs/.user.lock.* rwk,
  owner @{HOME}/.java/.userPrefs/com/install4j/installations/prefs.xml rw,
  owner @{HOME}/.java/fonts/@{int}/ rw,
  owner @{HOME}/.java/fonts/@{int}/fcinfo*.tmp rw,
  owner @{HOME}/.java/fonts/@{int}/fcinfo-*.properties rw,

  owner @{HOME}/.install4j rw,

  owner @{tmp}/hsperfdata_@{user}/ rw,
  owner @{tmp}/hsperfdata_@{user}/@{pid} rw,
  # If the @{JD_INSTALLDIR}/tmp/ dir can't be accessed, the /tmp/ dir will be used instead
  owner @{tmp}/SevenZipJBinding-*/ rw,
  owner @{tmp}/SevenZipJBinding-*/lib7-Zip-JBinding.so mrw,
  # For auto updates
  owner @{tmp}/lastChanceSrc@{int}lch rw,
  owner @{tmp}/lastChanceDst@{int}.jar rw,
  owner @{tmp}/i4j_log_jd2_@{int}.log rw,
  owner @{tmp}/install4jError@{int}.log rw,

  owner @{HOME}/.Xauthority r,

  # What's this for?
  deny owner @{HOME}/.mozilla/firefox/ r,
  deny owner @{HOME}/.mozilla/firefox/*.*/prefs.js r,

       owner @{PROC}/@{pid}/fd/ r,
  deny       @{PROC}/@{pid}/net/ipv6_route r,
  deny       @{PROC}/@{pid}/net/if_inet6 r,
       owner @{PROC}/@{pid}/mountinfo r,
       owner @{PROC}/@{pid}/mounts r,
  deny owner @{PROC}/@{pid}/cmdline r,
  deny       @{PROC}/asound/version r,

  # For Reconnect -> Share Settings/Get Route
  #@{bin}/netstat rix,
  #@{bin}/route rix,
  #@{bin}/ping rix,
  #@{bin}/ip rix,
  #@{PROC}/@{pid}/net/route r,

  # To open a web browser for CAPTCHA
  @{bin}/xdg-open                                    rCx -> open,
  @{lib}/@{multiarch}/glib-[0-9]*/gio-launch-desktop rCx -> open,


  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/jdownloader>
}

# vim:syntax=apparmor
