2024-10-16 01:31:23 +09:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
|
|
|
|
2024-10-20 19:50:13 +09:00
|
|
|
"git.ophivana.moe/security/fortify/acl"
|
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-10-16 01:31:23 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// UpdatePerm appends an ephemeral acl update Op.
|
2024-10-23 12:34:16 +09:00
|
|
|
func (sys *I) UpdatePerm(path string, perms ...acl.Perm) *I {
|
2024-10-16 01:31:23 +09:00
|
|
|
sys.UpdatePermType(Process, path, perms...)
|
2024-10-23 12:34:16 +09:00
|
|
|
|
|
|
|
return sys
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePermType appends an acl update Op.
|
2024-10-23 12:34:16 +09:00
|
|
|
func (sys *I) UpdatePermType(et Enablement, path string, perms ...acl.Perm) *I {
|
2024-10-16 01:31:23 +09:00
|
|
|
sys.lock.Lock()
|
|
|
|
defer sys.lock.Unlock()
|
|
|
|
|
|
|
|
sys.ops = append(sys.ops, &ACL{et, path, perms})
|
2024-10-23 12:34:16 +09:00
|
|
|
|
|
|
|
return sys
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
type ACL struct {
|
2024-10-16 14:38:57 +09:00
|
|
|
et Enablement
|
2024-10-16 01:31:23 +09:00
|
|
|
path string
|
2024-10-25 16:23:22 +09:00
|
|
|
perms acl.Perms
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|
|
|
|
|
2024-10-16 14:38:57 +09:00
|
|
|
func (a *ACL) Type() Enablement {
|
2024-10-16 01:31:23 +09:00
|
|
|
return a.et
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) apply(sys *I) error {
|
2024-10-25 16:23:22 +09:00
|
|
|
fmsg.VPrintln("applying ACL", a)
|
2024-10-16 01:31:23 +09:00
|
|
|
return fmsg.WrapErrorSuffix(acl.UpdatePerm(a.path, sys.uid, a.perms...),
|
|
|
|
fmt.Sprintf("cannot apply ACL entry to %q:", a.path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) revert(sys *I, ec *Criteria) error {
|
|
|
|
if ec.hasType(a) {
|
2024-10-25 16:23:22 +09:00
|
|
|
fmsg.VPrintln("stripping ACL", a)
|
2024-10-16 01:31:23 +09:00
|
|
|
return fmsg.WrapErrorSuffix(acl.UpdatePerm(a.path, sys.uid),
|
|
|
|
fmt.Sprintf("cannot strip ACL entry from %q:", a.path))
|
|
|
|
} else {
|
2024-10-25 16:23:22 +09:00
|
|
|
fmsg.VPrintln("skipping ACL", a)
|
2024-10-16 01:31:23 +09:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) Is(o Op) bool {
|
|
|
|
a0, ok := o.(*ACL)
|
|
|
|
return ok && a0 != nil &&
|
|
|
|
a.et == a0.et &&
|
|
|
|
a.path == a0.path &&
|
|
|
|
slices.Equal(a.perms, a0.perms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) Path() string {
|
|
|
|
return a.path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ACL) String() string {
|
2024-10-25 16:23:22 +09:00
|
|
|
return fmt.Sprintf("%s type: %s path: %q",
|
|
|
|
a.perms, TypeString(a.et), a.path)
|
2024-10-16 01:31:23 +09:00
|
|
|
}
|