From ca7aed4c049ab0cf6d01ed8034eee345b0161562 Mon Sep 17 00:00:00 2001 From: Adrian Cybulski Date: Sun, 2 Oct 2022 00:09:49 +0100 Subject: [PATCH] use master.password file and format on freebsd platform --- cmd/localize/main.go | 2 +- pkg/maps/base/base.go | 2 +- pkg/maps/base/netauth.go | 27 ----------------------- pkg/maps/base/netauth_freebsd.go | 38 ++++++++++++++++++++++++++++++++ pkg/maps/base/netauth_linux.go | 37 +++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 pkg/maps/base/netauth_freebsd.go create mode 100644 pkg/maps/base/netauth_linux.go diff --git a/cmd/localize/main.go b/cmd/localize/main.go index 8df3a5e..adbe43e 100644 --- a/cmd/localize/main.go +++ b/cmd/localize/main.go @@ -76,7 +76,7 @@ func main() { baseIdentity.MergePasswd() baseIdentity.MergeGroup() - if err := renameio.WriteFile(filepath.Join(*baseDir, "passwd"), baseIdentity.PasswdBytes(), 0644); err != nil { + if err := renameio.WriteFile(filepath.Join(*baseDir, base.PasswdDB), baseIdentity.PasswdBytes(), 0644); err != nil { log.Error("Error writing passwd", "error", err) os.Exit(2) } diff --git a/pkg/maps/base/base.go b/pkg/maps/base/base.go index 6888b79..2a45027 100644 --- a/pkg/maps/base/base.go +++ b/pkg/maps/base/base.go @@ -90,7 +90,7 @@ func (b *Base) loadShells() error { // manipulation. No filtering is done on load, all filtering is // handled during patching. func (b *Base) loadPasswd() error { - pf, err := os.Open(filepath.Join(b.baseDir, "passwd")) + pf, err := os.Open(filepath.Join(b.baseDir, PasswdDB)) if err != nil { return err } diff --git a/pkg/maps/base/netauth.go b/pkg/maps/base/netauth.go index 25b2f98..6f02208 100644 --- a/pkg/maps/base/netauth.go +++ b/pkg/maps/base/netauth.go @@ -2,7 +2,6 @@ package base import ( "context" - "path/filepath" "github.com/netauth/netauth/pkg/netauth" "github.com/the-maldridge/shadow" @@ -135,32 +134,6 @@ func (b *Base) findMembers(ctx context.Context) error { return nil } -// MergePasswd removes all entries above the minimum UID and then adds -// NetAuth entities that were filtered above. -func (b *Base) MergePasswd() { - b.passwd.Del(b.passwd.FilterUID(func(i int) bool { - return i >= int(b.minUID) - })) - - tmp := make([]*shadow.PasswdEntry, len(b.entities)) - idx := 0 - for i := range b.entities { - e := shadow.PasswdEntry{ - Login: b.entities[i].GetID(), - Password: "*", - UID: int(b.entities[i].GetNumber()), - GID: int(b.pgroups[b.entities[i].GetMeta().GetPrimaryGroup()]), - Comment: b.entities[i].GetMeta().GetGECOS(), - Home: filepath.Join(b.baseHome, b.entities[i].GetID()), - Shell: b.shell(b.entities[i].GetMeta().GetShell()), - } - tmp[idx] = &e - idx++ - } - - b.passwd.Add(tmp) -} - // MergeGroup removes all group entries above the minimum GID and then // adds the NetAuth groups that were filtered above. func (b *Base) MergeGroup() { diff --git a/pkg/maps/base/netauth_freebsd.go b/pkg/maps/base/netauth_freebsd.go new file mode 100644 index 0000000..63ec5db --- /dev/null +++ b/pkg/maps/base/netauth_freebsd.go @@ -0,0 +1,38 @@ +package base + +import ( + "path/filepath" + + "github.com/the-maldridge/shadow" +) + +const ( + PasswdDB = "master.passwd" +) + +func (b *Base) MergePasswd() { + b.passwd.Del(b.passwd.FilterUID(func(i int) bool { + return i >= int(b.minUID) + })) + + tmp := make([]*shadow.PasswdEntry, len(b.entities)) + idx := 0 + for i := range b.entities { + e := shadow.PasswdEntry{ + Login: b.entities[i].GetID(), + Password: "*", + UID: int(b.entities[i].GetNumber()), + GID: int(b.pgroups[b.entities[i].GetMeta().GetPrimaryGroup()]), + Class: "", + Change: "0", + Expire: "0", + Comment: b.entities[i].GetMeta().GetGECOS(), + Home: filepath.Join(b.baseHome, b.entities[i].GetID()), + Shell: b.shell(b.entities[i].GetMeta().GetShell()), + } + tmp[idx] = &e + idx++ + } + + b.passwd.Add(tmp) +} diff --git a/pkg/maps/base/netauth_linux.go b/pkg/maps/base/netauth_linux.go new file mode 100644 index 0000000..a53475b --- /dev/null +++ b/pkg/maps/base/netauth_linux.go @@ -0,0 +1,37 @@ +package base + +import ( + "path/filepath" + + "github.com/the-maldridge/shadow" +) + +const ( + PasswdDB = "passwd" +) + +// MergePasswd removes all entries above the minimum UID and then adds +// NetAuth entities that were filtered above. +func (b *Base) MergePasswd() { + b.passwd.Del(b.passwd.FilterUID(func(i int) bool { + return i >= int(b.minUID) + })) + + tmp := make([]*shadow.PasswdEntry, len(b.entities)) + idx := 0 + for i := range b.entities { + e := shadow.PasswdEntry{ + Login: b.entities[i].GetID(), + Password: "*", + UID: int(b.entities[i].GetNumber()), + GID: int(b.pgroups[b.entities[i].GetMeta().GetPrimaryGroup()]), + Comment: b.entities[i].GetMeta().GetGECOS(), + Home: filepath.Join(b.baseHome, b.entities[i].GetID()), + Shell: b.shell(b.entities[i].GetMeta().GetShell()), + } + tmp[idx] = &e + idx++ + } + + b.passwd.Add(tmp) +}