# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2017-2022 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}/gpg-agent
profile gpg-agent @{exec_path} {
  include <abstractions/base>
  include <abstractions/nameservice-strict>

  signal (receive) peer=pinentry-*,
  signal (receive) peer=scdaemon,

  @{exec_path} mr,

  @{bin}/pinentry{,-*}  rPx,
  @{bin}/scdaemon       rPx,
  @{lib}/{,gnupg/}scdaemon rPx,

  /usr/share/gnupg/* r,

  owner @{HOME}/@{XDG_GPG_DIR}/ rw,
  owner @{HOME}/@{XDG_GPG_DIR}/gpg-agent.conf r,
  owner @{HOME}/@{XDG_GPG_DIR}/private-keys-v1.d/ rw,
  owner @{HOME}/@{XDG_GPG_DIR}/private-keys-v1.d/@{hex}.key rw,
  owner @{HOME}/@{XDG_GPG_DIR}/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner @{HOME}/@{XDG_GPG_DIR}/sshcontrol r,

  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/ rw,
  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/gpg-agent.conf r,
  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/private-keys-v1.d/ rw,
  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/private-keys-v1.d/@{hex}.key rw,
  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner @{MOUNTS}/{,/*}/@{XDG_GPG_DIR}/sshcontrol r,

  owner @{user_projects_dirs}/**/{.,}gnupg/ rw,
  owner @{user_projects_dirs}/**/{.,}gnupg/gpg-agent.conf r,
  owner @{user_projects_dirs}/**/{.,}gnupg/private-keys-v1.d/ rw,
  owner @{user_projects_dirs}/**/{.,}gnupg/private-keys-v1.d/@{hex}.key rw,
  owner @{user_projects_dirs}/**/{.,}gnupg/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner @{user_projects_dirs}/**/{.,}gnupg/sshcontrol r,

  owner @{run}/user/@{uid}/gnupg/ rw,
  owner @{run}/user/@{uid}/gnupg/gpg-agent.conf r,
  owner @{run}/user/@{uid}/gnupg/private-keys-v1.d/ rw,
  owner @{run}/user/@{uid}/gnupg/private-keys-v1.d/@{hex}.key rw,
  owner @{run}/user/@{uid}/gnupg/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner @{run}/user/@{uid}/gnupg/sshcontrol r,

  owner @{user_tmp_dirs}/**/{.,}gnupg/ rw,
  owner @{user_tmp_dirs}/**/{.,}gnupg/gpg-agent.conf r,
  owner @{user_tmp_dirs}/**/{.,}gnupg/private-keys-v1.d/ rw,
  owner @{user_tmp_dirs}/**/{.,}gnupg/private-keys-v1.d/@{hex}.key rw,
  owner @{user_tmp_dirs}/**/{.,}gnupg/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner @{user_tmp_dirs}/**/{.,}gnupg/sshcontrol r,

  owner /var/lib/*/.gnupg/ rw,
  owner /var/lib/*/.gnupg/private-keys-v1.d/ rw,
  owner /var/lib/*/.gnupg/private-keys-v1.d/@{hex}.key rw,
  owner /var/lib/*/.gnupg/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner /var/lib/*/.gnupg/sshcontrol r,

  owner /var/lib/*/gnupg/ rw,
  owner /var/lib/*/gnupg/private-keys-v1.d/ rw,
  owner /var/lib/*/gnupg/private-keys-v1.d/@{hex}.key rw,
  owner /var/lib/*/gnupg/{,d.*/}S.gpg-agent{,.ssh,.browser,.extra} rw,
  owner /var/lib/*/gnupg/sshcontrol r,

  owner /var/tmp/zypp.*/ rw,
  owner /var/tmp/zypp.*/{,*/}private-keys-v1.d/ rw,
  owner /var/tmp/zypp.*/{,*/}private-keys-v1.d/@{hex}.key rw,
  owner /var/tmp/zypp.*/{,*/}S.gpg-agent{,.ssh,.browser,.extra} rw,

  owner @{tmp}/tmp.*/gnupg/ rw,
  owner @{tmp}/tmp.*/gnupg/private-keys-v1.d/ rw,
  owner @{tmp}/tmp.*/gnupg/private-keys-v1.d/@{hex}.key rw,
  owner @{tmp}/tmp.*/gnupg/{,d.*/}S.gpg-agent rw,
  owner @{tmp}/tmp.*/gnupg/sshcontrol r,

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

  # Silencer
  deny @{bin}/.gnupg/ w,

  # file inherit
  owner /dev/tty@{int} rw,

  include if exists <local/gpg-agent>
}
