2024-03-26 00:34:14 +01:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
|
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package prepare
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-02 17:22:46 +02:00
|
|
|
"os"
|
2024-03-26 00:34:14 +01:00
|
|
|
|
2024-10-02 17:22:46 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
2024-03-26 00:34:14 +01:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Configure struct {
|
2024-10-02 17:22:46 +02:00
|
|
|
prebuild.Base
|
|
|
|
OneFile bool
|
2024-03-26 00:34:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterTask(&Configure{
|
2024-10-02 17:22:46 +02:00
|
|
|
Base: prebuild.Base{
|
2024-03-26 00:34:14 +01:00
|
|
|
Keyword: "configure",
|
|
|
|
Msg: "Set distribution specificities",
|
|
|
|
},
|
2024-10-02 17:22:46 +02:00
|
|
|
OneFile: false,
|
2024-03-26 00:34:14 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Configure) Apply() ([]string, error) {
|
|
|
|
res := []string{}
|
2024-10-02 17:22:46 +02:00
|
|
|
|
|
|
|
if prebuild.ABI == 4 {
|
|
|
|
if err := OverwriteUpstreamProfile(p.OneFile); err != nil {
|
|
|
|
return res, err
|
2024-06-04 20:52:06 +02:00
|
|
|
}
|
2024-10-02 17:22:46 +02:00
|
|
|
}
|
|
|
|
switch prebuild.Distribution {
|
|
|
|
case "arch", "opensuse":
|
2024-03-26 00:34:14 +01:00
|
|
|
|
|
|
|
case "ubuntu":
|
2024-10-02 17:22:46 +02:00
|
|
|
if err := prebuild.DebianHide.Init(); err != nil {
|
2024-06-04 20:55:53 +02:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2024-10-02 17:22:46 +02:00
|
|
|
if prebuild.ABI == 3 {
|
|
|
|
if err := util.CopyTo(prebuild.DistDir.Join("ubuntu"), prebuild.RootApparmord); err != nil {
|
2024-03-26 00:34:14 +01:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case "debian", "whonix":
|
2024-10-02 17:22:46 +02:00
|
|
|
if err := prebuild.DebianHide.Init(); err != nil {
|
2024-06-04 20:55:53 +02:00
|
|
|
return res, err
|
|
|
|
}
|
2024-03-26 00:34:14 +01:00
|
|
|
|
|
|
|
// Copy Debian specific abstractions
|
2024-10-02 17:22:46 +02:00
|
|
|
if err := util.CopyTo(prebuild.DistDir.Join("ubuntu"), prebuild.RootApparmord); err != nil {
|
2024-03-26 00:34:14 +01:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2024-10-02 17:22:46 +02:00
|
|
|
return []string{}, fmt.Errorf("%s is not a supported distribution", prebuild.Distribution)
|
2024-03-26 00:34:14 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2024-10-02 17:22:46 +02:00
|
|
|
|
|
|
|
// Overwrite upstream profile: disable upstream & rename ours
|
|
|
|
func OverwriteUpstreamProfile(oneFile bool) error {
|
|
|
|
const ext = ".apparmor.d"
|
|
|
|
disableDir := prebuild.RootApparmord.Join("disable")
|
|
|
|
if err := disableDir.Mkdir(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := prebuild.DistDir.Join("overwrite")
|
|
|
|
if !path.Exist() {
|
|
|
|
return fmt.Errorf("%s not found", path)
|
|
|
|
}
|
|
|
|
for _, name := range util.MustReadFileAsLines(path) {
|
|
|
|
origin := prebuild.RootApparmord.Join(name)
|
|
|
|
dest := prebuild.RootApparmord.Join(name + ext)
|
|
|
|
if !dest.Exist() && oneFile {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := origin.Rename(dest); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
originRel, err := origin.RelFrom(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(originRel.String(), disableDir.Join(name).String()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|