2023-05-06 14:01:07 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-05-06 14:01:07 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package prebuild
|
|
|
|
|
|
|
|
import (
|
2023-12-15 20:14:32 +01:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
"github.com/arduino/go-paths-helper"
|
2023-12-15 20:14:32 +01:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
2024-03-21 19:58:32 +01:00
|
|
|
oss "github.com/roddhjav/apparmor.d/pkg/os"
|
2024-03-21 23:13:00 +01:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/prebuild/directive"
|
2023-05-06 14:01:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-03-10 15:47:13 +01:00
|
|
|
overwrite bool = false
|
2023-05-06 14:01:07 +02:00
|
|
|
DistDir *paths.Path
|
|
|
|
Root *paths.Path
|
|
|
|
RootApparmord *paths.Path
|
2023-07-23 21:36:48 +02:00
|
|
|
FlagDir *paths.Path
|
2023-05-06 14:01:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
DistDir = paths.New("dists")
|
|
|
|
Root = paths.New(".build")
|
2023-07-23 21:36:48 +02:00
|
|
|
FlagDir = DistDir.Join("flags")
|
2023-05-06 14:01:07 +02:00
|
|
|
RootApparmord = Root.Join("apparmor.d")
|
2024-03-21 19:58:32 +01:00
|
|
|
if oss.Distribution == "ubuntu" {
|
|
|
|
if oss.Release["VERSION_CODENAME"] == "noble" {
|
2024-02-28 18:35:14 +01:00
|
|
|
Builds = append(Builds, BuildABI3)
|
2024-03-10 15:47:13 +01:00
|
|
|
overwrite = true
|
2024-02-28 18:35:14 +01:00
|
|
|
}
|
|
|
|
}
|
2023-05-06 14:01:07 +02:00
|
|
|
}
|
|
|
|
|
2023-12-15 20:14:32 +01:00
|
|
|
func getFctName(i any) string {
|
|
|
|
tmp := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
|
|
|
res := strings.Split(tmp, ".")
|
|
|
|
return res[len(res)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func printPrepareMessage(name string, msg []string) {
|
|
|
|
logging.Success("%v", PrepareMsg[name])
|
|
|
|
logging.Indent = " "
|
|
|
|
for _, line := range msg {
|
|
|
|
logging.Bullet("%s", line)
|
|
|
|
}
|
|
|
|
logging.Indent = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func printBuildMessage() {
|
|
|
|
for _, fct := range Builds {
|
|
|
|
name := getFctName(fct)
|
|
|
|
logging.Success("%v", BuildMsg[name])
|
|
|
|
}
|
2023-12-15 23:11:25 +01:00
|
|
|
for _, fct := range Directives {
|
|
|
|
name := getFctName(fct)
|
|
|
|
logging.Success("%v", DirectiveMsg[name])
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
}
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
func Prepare() error {
|
|
|
|
for _, fct := range Prepares {
|
2023-12-15 20:14:32 +01:00
|
|
|
msg, err := fct()
|
|
|
|
if err != nil {
|
2023-05-06 14:01:07 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
printPrepareMessage(getFctName(fct), msg)
|
2023-05-06 14:01:07 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Build() error {
|
|
|
|
files, _ := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories())
|
|
|
|
for _, file := range files {
|
|
|
|
if !file.Exist() {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-10 15:24:59 +01:00
|
|
|
content, err := file.ReadFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-06 14:01:07 +02:00
|
|
|
profile := string(content)
|
|
|
|
for _, fct := range Builds {
|
|
|
|
profile = fct(profile)
|
|
|
|
}
|
2023-12-15 23:11:25 +01:00
|
|
|
for _, fct := range Directives {
|
|
|
|
profile = fct(file, profile)
|
|
|
|
}
|
2024-03-21 21:36:41 +01:00
|
|
|
profile = directive.Run(file, profile)
|
2023-05-06 14:01:07 +02:00
|
|
|
if err := file.WriteFile([]byte(profile)); err != nil {
|
2024-03-10 15:24:59 +01:00
|
|
|
return err
|
2023-05-06 14:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
printBuildMessage()
|
2023-05-06 14:01:07 +02:00
|
|
|
return nil
|
|
|
|
}
|