# 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} = /usr/share/netplan/netplan.script
profile netplan.script @{exec_path} flags=(attach_disconnected) {
  include <abstractions/base>
  include <abstractions/nameservice-strict>
  include <abstractions/python>

  network netlink raw,

  @{exec_path} mr,

  @{lib}/netplan/generate  rix,
  @{bin}/udevadm           rCx -> udevadm,
  @{bin}/systemctl         rCx -> systemctl,

  /usr/share/netplan/{,**} r,

  /etc/netplan/{,*} r,

  @{run}/netplan/ r,

  @{run}/NetworkManager/conf.d/@{int}-globally-managed-devices.conf{,.@{rand6}} rw,
  @{run}/NetworkManager/system-connections/ rw,
  @{run}/NetworkManager/system-connections/netplan-*.nmconnection{,.@{rand6}} rw,

  @{run}/systemd/network/ r,
  @{run}/systemd/network/@{int}-netplan{,-*}.{network,link}{,.@{rand6}} rw,
  @{run}/systemd/system/ r,
  @{run}/systemd/system/netplan-* rw,
  @{run}/systemd/system/systemd-networkd-wait-online.service.d/ r,
  @{run}/systemd/system/systemd-networkd-wait-online.service.d/@{int}-netplan.conf{,.@{rand6}} rw,
  @{run}/systemd/system/systemd-networkd.service.wants/ rw,
  @{run}/systemd/system/systemd-networkd.service.wants/netplan-*.service rw,

  @{run}/udev/rules.d/ r,
  @{run}/udev/rules.d/@{int}-netplan{,-*}.rules{,.@{rand6}} rw,

  @{sys}/devices/**/net/*/address r,

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

    capability net_admin,

    @{att}/@{run}/udev/control rw,

    @{run}/udev/rules.d/90-netplan.rules rw,
    @{run}/udev/rules.d/90-netplan.rules.@{rand6} rw,

    include if exists <local/netplan.script_udevadm>
  }

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

    capability net_admin,

    @{att}/@{run}/systemd/private rw,

    include if exists <local/netplan.script_systemctl>
  }

  include if exists <local/netplan.script>
}

# vim:syntax=apparmor
