From 2e23cef7bb952e4964cd4994867e2d51e978e95c Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Sun, 17 Nov 2024 23:31:06 +0900 Subject: [PATCH] cmd/fuserdb: generate group entries Signed-off-by: Ophestra Umiker --- cmd/fuserdb/main.go | 34 +++------------------- cmd/fuserdb/payload.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 cmd/fuserdb/payload.go diff --git a/cmd/fuserdb/main.go b/cmd/fuserdb/main.go index 9283d82..6f10ee7 100644 --- a/cmd/fuserdb/main.go +++ b/cmd/fuserdb/main.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "encoding/json" "errors" "flag" "fmt" @@ -46,18 +45,9 @@ func main() { fmsg.Fatalf("cannot create output: %v", err) } - type payload struct { - UserName string `json:"userName"` - Uid int `json:"uid"` - Gid int `json:"gid"` - RealName string `json:"realName"` - HomeDirectory string `json:"homeDirectory"` - Shell string `json:"shell"` - } - for _, u := range users { fidString := strconv.Itoa(u.fid) - for aid := 0; aid < 9999; aid++ { + for aid := 0; aid < 10000; aid++ { userName := fmt.Sprintf("u%d_a%d", u.fid, aid) uid := 1000000 + u.fid*10000 + aid us := strconv.Itoa(uid) @@ -69,27 +59,11 @@ func main() { homeDirectory = varEmpty } - fileName := userName + ".user" - if f, err := os.OpenFile(path.Join(*out, fileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil { - fmsg.Fatalf("cannot create %s: %v", userName, err) - } else if err = json.NewEncoder(f).Encode(&payload{ - UserName: userName, - Uid: uid, - Gid: uid, - RealName: realName, - HomeDirectory: homeDirectory, - Shell: *shell, - }); err != nil { - fmsg.Fatalf("cannot serialise %s: %v", userName, err) - } else if err = f.Close(); err != nil { - fmsg.Printf("cannot close %s: %v", userName, err) - } - if err := os.Symlink(fileName, path.Join(*out, us+".user")); err != nil { - fmsg.Fatalf("cannot link %s: %v", userName, err) - } + writeUser(userName, uid, us, realName, homeDirectory, *shell, *out) + writeGroup(userName, uid, us, nil, *out) } } - fmsg.Printf("created %d entries", len(users)*10000) + fmsg.Printf("created %d entries", len(users)*2*10000) fmsg.Exit(0) } diff --git a/cmd/fuserdb/payload.go b/cmd/fuserdb/payload.go new file mode 100644 index 0000000..fb24121 --- /dev/null +++ b/cmd/fuserdb/payload.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "os" + "path" + + "git.ophivana.moe/security/fortify/internal/fmsg" +) + +type payloadU struct { + UserName string `json:"userName"` + Uid int `json:"uid"` + Gid int `json:"gid"` + MemberOf []string `json:"memberOf,omitempty"` + RealName string `json:"realName"` + HomeDirectory string `json:"homeDirectory"` + Shell string `json:"shell"` +} + +func writeUser(userName string, uid int, us string, realName, homeDirectory, shell string, out string) { + userFileName := userName + ".user" + if f, err := os.OpenFile(path.Join(out, userFileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil { + fmsg.Fatalf("cannot create %s: %v", userName, err) + } else if err = json.NewEncoder(f).Encode(&payloadU{ + UserName: userName, + Uid: uid, + Gid: uid, + RealName: realName, + HomeDirectory: homeDirectory, + Shell: shell, + }); err != nil { + fmsg.Fatalf("cannot serialise %s: %v", userName, err) + } else if err = f.Close(); err != nil { + fmsg.Printf("cannot close %s: %v", userName, err) + } + if err := os.Symlink(userFileName, path.Join(out, us+".user")); err != nil { + fmsg.Fatalf("cannot link %s: %v", userName, err) + } +} + +type payloadG struct { + GroupName string `json:"groupName"` + Gid int `json:"gid"` + Members []string `json:"members,omitempty"` +} + +func writeGroup(groupName string, gid int, gs string, members []string, out string) { + groupFileName := groupName + ".group" + if f, err := os.OpenFile(path.Join(out, groupFileName), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644); err != nil { + fmsg.Fatalf("cannot create %s: %v", groupName, err) + } else if err = json.NewEncoder(f).Encode(&payloadG{ + GroupName: groupName, + Gid: gid, + Members: members, + }); err != nil { + fmsg.Fatalf("cannot serialise %s: %v", groupName, err) + } else if err = f.Close(); err != nil { + fmsg.Printf("cannot close %s: %v", groupName, err) + } + if err := os.Symlink(groupFileName, path.Join(out, gs+".group")); err != nil { + fmsg.Fatalf("cannot link %s: %v", groupName, err) + } +}