2023-09-25 01:06:07 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-09-25 01:06:07 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
|
|
|
|
2024-05-05 15:19:25 +02:00
|
|
|
import "slices"
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
const tokNETWORK = "network"
|
|
|
|
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type AddressExpr struct {
|
|
|
|
Source string
|
|
|
|
Destination string
|
|
|
|
Port string
|
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func newAddressExprFromLog(log map[string]string) AddressExpr {
|
|
|
|
return AddressExpr{
|
|
|
|
Source: log["laddr"],
|
|
|
|
Destination: log["faddr"],
|
|
|
|
Port: log["lport"],
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r AddressExpr) Less(other AddressExpr) bool {
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.Source != other.Source {
|
|
|
|
return r.Source < other.Source
|
|
|
|
}
|
|
|
|
if r.Destination != other.Destination {
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Destination < other.Destination
|
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
return r.Port < other.Port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r AddressExpr) Equals(other AddressExpr) bool {
|
|
|
|
return r.Source == other.Source && r.Destination == other.Destination &&
|
|
|
|
r.Port == other.Port
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2023-09-25 01:06:07 +02:00
|
|
|
|
|
|
|
type Network struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
2024-04-15 00:58:34 +02:00
|
|
|
AddressExpr
|
2023-09-25 01:06:07 +02:00
|
|
|
Domain string
|
|
|
|
Type string
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newNetworkFromLog(log map[string]string) Rule {
|
2023-09-25 01:06:07 +02:00
|
|
|
return &Network{
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase: newRuleFromLog(log),
|
2024-04-15 00:58:34 +02:00
|
|
|
Qualifier: newQualifierFromLog(log),
|
|
|
|
AddressExpr: newAddressExprFromLog(log),
|
|
|
|
Domain: log["family"],
|
|
|
|
Type: log["sock_type"],
|
|
|
|
Protocol: log["protocol"],
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
|
|
|
|
func (r *Network) Less(other any) bool {
|
|
|
|
o, _ := other.(*Network)
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.Domain != o.Domain {
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Domain < o.Domain
|
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.Type != o.Type {
|
|
|
|
return r.Type < o.Type
|
|
|
|
}
|
|
|
|
if r.Protocol != o.Protocol {
|
|
|
|
return r.Protocol < o.Protocol
|
|
|
|
}
|
|
|
|
if r.AddressExpr.Less(o.AddressExpr) {
|
|
|
|
return r.AddressExpr.Less(o.AddressExpr)
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Qualifier.Less(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Network) Equals(other any) bool {
|
|
|
|
o, _ := other.(*Network)
|
|
|
|
return r.Domain == o.Domain && r.Type == o.Type &&
|
|
|
|
r.Protocol == o.Protocol && r.AddressExpr.Equals(o.AddressExpr) &&
|
|
|
|
r.Qualifier.Equals(o.Qualifier)
|
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
func (r *Network) String() string {
|
2024-05-05 00:41:47 +02:00
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Network) Constraint() constraint {
|
|
|
|
return blockKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Network) Kind() string {
|
|
|
|
return tokNETWORK
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|