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

abi <abi/4.0>,

include <tunables/global>

@{exec_path} = @{bin}/cp{pw,gr}
@{att} = ""
profile cppw-cpgr /{,usr/}bin/cp{pw,gr} flags=(complain) {
  include <abstractions/base-strict>

  # To set the right permission to the files in the /etc/ dir.
  capability chown,
  capability fsetid,

  @{exec_path} mr,

  /etc/{passwd,shadow,gshadow,group} rw,
  /etc/{passwd,shadow,gshadow,group}.@{pid} rw,
  /etc/{passwd,shadow,gshadow,group}.new rw,
  /etc/passwd.lock  wl -> /etc/passwd.@{pid},
  /etc/shadow.lock  wl -> /etc/shadow.@{pid},
  /etc/gshadow.lock wl -> /etc/gshadow.@{pid},
  /etc/group.lock   wl -> /etc/group.@{pid},

  # A process first uses lckpwdf() to lock the lock file, thereby gaining exclusive rights to
  # modify the /etc/passwd or /etc/shadow password database.
  /etc/.pwd.lock rwk,

  # Source of the files to be replaced
  owner /root/* r,

  include if exists <local/cppw-cpgr>
}

# vim:syntax=apparmor
