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

# TODO: rethink how the scripts should be managed

abi <abi/3.0>,

include <tunables/global>

@{exec_path} = @{lib}/{,NetworkManager/}nm-dispatcher
profile nm-dispatcher @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/bus-system>
  include <abstractions/nameservice-strict>

  capability net_admin,
  capability sys_nice,
  capability sys_ptrace,

  ptrace (read) peer=@{p_systemd},

  #aa:dbus own bus=system name=org.freedesktop.nm_dispatcher

  @{exec_path} mr,

  @{sh_path}                rix,
  @{bin}/basename           rix,
  @{bin}/cat                rix,
  @{bin}/chronyc           rPUx,
  @{bin}/date               rix,
  @{bin}/gawk               rix,
  @{bin}/grep               rix,
  @{bin}/id                 rix,
  @{bin}/invoke-rc.d        rCx -> invoke-rc,
  @{bin}/mkdir              rix,
  @{bin}/mktemp             rix,
  @{bin}/netconfig         rPUx,
  @{bin}/nmcli              rix,
  @{bin}/readlink           rix,
  @{bin}/rm                 rix,
  @{bin}/run-parts          rCx -> run-parts,
  @{bin}/sed                rix,
  @{bin}/systemctl          rCx -> systemctl,
  @{bin}/systemd-cat        rPx,
  @{bin}/tr                 rix,
  /usr/share/tlp/tlp-readconfs  rPUx,

  @{lib}/NetworkManager/dispatcher.d/ r,
  @{lib}/NetworkManager/dispatcher.d/** rix,
  /etc/NetworkManager/dispatcher.d/ r,
  /etc/NetworkManager/dispatcher.d/** rix,
  /etc/dhcp/dhclient-exit-hooks.d/ntp r,

  /usr/share/tlp/{,**} rw,

  /etc/sysconfig/network/config r,
  /etc/fstab r,

  @{run}/systemd/notify rw,
  @{run}/tlp/{,*} rw,
  @{run}/chrony-dhcp/ rw,
  @{run}/ntp.conf.dhcp rw,

  @{sys}/class/net/ r,

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

  /dev/tty rw,

  profile systemctl {
    include <abstractions/base>
    include <abstractions/app/systemctl>

    capability net_admin,

    @{etc_ro}/ r,
    @{etc_ro}/systemd/ r,
    @{etc_ro}/systemd/system/ r,
    @{etc_ro}/systemd/system/ntp.service r,

    include if exists <local/nm-dispatcher_systemctl>
  }

  profile invoke-rc {
    include <abstractions/base>

    @{sh_path}       rix,
    @{bin}/ls        rix,
    @{bin}/systemctl rCx -> systemctl,

    / r,

    /etc/ r,
    @{etc_ro}/rc{[0-9],S}.d/{,*} r,
    @{etc_ro}/init.d/ntp r,

    include if exists <local/nm-dispatcher_invoke-rc>
  }

  profile run-parts {
    include <abstractions/base>

    /{usr/,}bin/run-parts mr,

    /etc/network/if-*.d/ r,
    /etc/network/if-*.d/* rPUx,
    /etc/wpa_supplicant/ifupdown.sh rPUx,

    include if exists <local/nm-dispatcher_run-parts>
  }

  include if exists <local/nm-dispatcher>
}
