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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{bin}/yay
@{att} = ""
profile yay /{,usr/}bin/yay flags=(complain) {
  include <abstractions/base-strict>
  include <abstractions/consoles>
  include <abstractions/nameservice-strict>
  include <abstractions/ssl_certs>

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

  @{exec_path} mr,

  @{editor_path}      cx -> editor,
  @{bin}/git          cx -> git,
  @{bin}/gpg{,2}      cx -> gpg,
  @{bin}/makepkg      px,
  @{bin}/pacman       px,
  @{bin}/pacman-conf  px,
  @{bin}/sudo         cx -> sudo,

  /var/lib/pacman/** r,

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

  owner @{user_config_dirs}/yay/{,**} rw,

  profile git flags=(complain) {
    include <abstractions/base-strict>
    include <abstractions/consoles>
    include <abstractions/nameservice-strict>
    include <abstractions/ssl_certs>

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

    @{bin}/git*              mrix,
    @{lib}/git{,-core}/git*  mrix,

    @{pager_path}   rpx -> child-pager,
    @{bin}/gpg{2,}  rpx -> yay//gpg,

    /usr/share/git{,-core}/{,**} r,

    owner @{user_build_dirs}/**/.git/** r,
    owner @{user_pkg_dirs}/**/.git/** r,
    owner @{user_projects_dirs}/**/.git/** r,

    owner @{HOME}/.gitconfig r,
    owner @{user_cache_dirs}/yay/ rw,
    owner @{user_cache_dirs}/yay/** rwlk -> @{user_cache_dirs}/yay/**,
    owner @{user_config_dirs}/git/{,*} r,

    include if exists <local/yay_git>
  }

  profile editor flags=(complain) {
    include <abstractions/base-strict>
    include <abstractions/app/editor>

    owner @{HOME}/**/ r, # For pwd

    owner @{user_cache_dirs}/yay/*/** rw,

    include if exists <local/yay_editor>
  }

  profile gpg flags=(complain) {
    include <abstractions/base-strict>
    include <abstractions/consoles>

    @{bin}/gpg{,2}     mr,
    @{bin}/gpg-agent  rpx,
    @{bin}/dirmngr    rpx,

    owner @{HOME}/@{XDG_GPG_DIR}/ rw,
    owner @{HOME}/@{XDG_GPG_DIR}/** rwkl -> @{HOME}/@{XDG_GPG_DIR}/**,

    include if exists <local/yay_gpg>
  }

  profile sudo flags=(complain) {
    include <abstractions/base-strict>
    include <abstractions/app/sudo>

    capability sys_ptrace,

    ptrace read peer=unconfined,

    @{bin}/pacman px,

    include if exists <local/yay_sudo>
  }

  include if exists <local/yay>
}

# vim:syntax=apparmor
