# apparmor.d - Full set of apparmor profiles
# 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}/loginctl
@{att} = /att/loginctl/
profile loginctl /{,usr/}bin/loginctl flags=(attach_disconnected,attach_disconnected.path=@{att},complain) {
  include <abstractions/attached/base>
  include <abstractions/bus-system>
  include <abstractions/bus/system/org.freedesktop.systemd1>
  include <abstractions/common/systemd>
  include <abstractions/attached/consoles>
  include <abstractions/attached/nameservice-strict>

  capability net_admin,
  capability sys_resource,

  signal send set=cont peer=child-pager,

  #aa/dbus talk bus=system name=org.freedesktop.login1 label="@{p_systemd_logind}"
  # Unix: allow connection to the profile
  unix type=stream peer=(label="@{p_systemd_logind}"),
  # org.freedesktop.login1: send and receive anything to the interface on the specific peer label
  dbus (send receive) bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.login1{,.*}
       peer=(name="{@{busname},org.freedesktop.login1{,.*},org.freedesktop.DBus}", label="@{p_systemd_logind}"),
  dbus send bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.login1{,.*}
       peer=(name="org.freedesktop.login1{,.*}"),
  # DBus.Properties: read and send properties
  dbus (send receive) bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.DBus.Properties
       member={Get,GetAll,Set,PropertiesChanged}
       peer=(name="{@{busname},org.freedesktop.login1{,.*},org.freedesktop.DBus}", label="@{p_systemd_logind}"),
  # DBus.Introspectable: allow service introspection
  dbus send bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.DBus.Introspectable
       member=Introspect
       peer=(name="{@{busname},org.freedesktop.login1{,.*},org.freedesktop.DBus}", label="@{p_systemd_logind}"),
  # DBus.ObjectManager: allow clients to enumerate sources
  dbus send bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member=GetManagedObjects
       peer=(name="{@{busname},org.freedesktop.login1{,.*},org.freedesktop.DBus}", label="@{p_systemd_logind}"),
  dbus receive bus=system path=/org/freedesktop/login1{,/**}
       interface=org.freedesktop.DBus.ObjectManager
       member={InterfacesAdded,InterfacesRemoved}
       peer=(name="{@{busname},org.freedesktop.login1{,.*},org.freedesktop.DBus}", label="@{p_systemd_logind}"),


  @{exec_path} mr,

  @{pager_path} rpx -> child-pager,
  @{bin}/ssh    rpx,

  /etc/machine-id r,

  @{run}/log/journal/ r,

  owner @{tmp}/xauth_@{rand6} r,

  /var/lib/systemd/catalog/database r,

  /{run,var}/log/journal/ r,
  /{run,var}/log/journal/@{hex32}/ r,
  /{run,var}/log/journal/@{hex32}/system.journal* r,
  /{run,var}/log/journal/@{hex32}/system@@{hex}-@{hex}.journal* r,
  /{run,var}/log/journal/@{hex32}/system@@{hex32}-@{hex16}-@{hex16}.journal* r,
  /{run,var}/log/journal/@{hex32}/user-@{hex}.journal* r,
  /{run,var}/log/journal/@{hex32}/user-@{uid}@@{hex}-@{hex}.journal* r,
  /{run,var}/log/journal/@{hex32}/user-@{uid}@@{hex32}-@{hex16}-@{hex16}.journal* r,

        @{PROC}/sys/fs/nr_open r,
  owner @{PROC}/@{pid}/cgroup r,

  /dev/rfkill r,

  include if exists <local/loginctl>
}

# vim:syntax=apparmor
