Skip to content

Commit

Permalink
Generate a user id in the range of human users
Browse files Browse the repository at this point in the history
otherwise we may end up with id 65535 (the "nobody" user)

Part of: mudler/yip#159

Signed-off-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
jimmykarily committed Jun 25, 2024
1 parent f8f11ba commit 9a6f6ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 29 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/mudler/entities
go 1.21

require (
github.com/mauromorales/xpasswd v0.3.1
github.com/gofrs/flock v0.8.1
github.com/mauromorales/xpasswd v0.4.0
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.33.0
Expand All @@ -15,7 +16,6 @@ require (
)

require (
github.com/gofrs/flock v0.8.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mauromorales/xpasswd v0.3.0 h1:yVfSoAgKp3uEzkbqzp0mWAdbVFdBzSsBknTqn1Wy/Jo=
github.com/mauromorales/xpasswd v0.3.0/go.mod h1:Z3+aY19mhNfcGi3st0+RAVSz2vC+pyoju2S/FPN8kEg=
github.com/mauromorales/xpasswd v0.3.1 h1:mVPGISfzN/WaCUjYRFiDgIREb2NMfwgPSj3LS6QMm0Q=
github.com/mauromorales/xpasswd v0.3.1/go.mod h1:Z3+aY19mhNfcGi3st0+RAVSz2vC+pyoju2S/FPN8kEg=
github.com/mauromorales/xpasswd v0.4.0 h1:Jf6mfA8lwQsYzwgfQADPDGV7l/liAvRrnG+nQTPy0j8=
github.com/mauromorales/xpasswd v0.4.0/go.mod h1:Z3+aY19mhNfcGi3st0+RAVSz2vC+pyoju2S/FPN8kEg=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
34 changes: 11 additions & 23 deletions pkg/entities/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strconv"
"strings"

"github.com/mauromorales/xpasswd/pkg/users"
xusers "github.com/mauromorales/xpasswd/pkg/users"
permbits "github.com/phayes/permbits"
"github.com/pkg/errors"

Expand All @@ -41,31 +41,19 @@ func UserDefault(s string) string {
}

func userGetFreeUid(path string) (int, error) {
uidStart, uidEnd := DynamicRange()
mUids := make(map[int]*UserPasswd)
ans := -1

current, err := ParseUser(path)
list := xusers.NewUserList()
list.SetPath(path)
err := list.Load()
if err != nil {
return ans, err
return 0, errors.Wrap(err, "Failed parsing passwd")
}

for _, e := range current {
mUids[e.Uid] = &e
}

for i := uidStart; i >= uidEnd; i-- {
if _, ok := mUids[i]; !ok {
ans = i
break
}
}

if ans < 0 {
return ans, errors.New("No free UID found")
id, err := list.GenerateUIDInRange(1000, 65534)
if err != nil {
return 0, errors.Wrap(err, "Failed generating a unique uid")
}

return ans, nil
return id, nil
}

type UserPasswd struct {
Expand All @@ -82,7 +70,7 @@ type UserPasswd struct {
func ParseUser(path string) (map[string]UserPasswd, error) {
ans := make(map[string]UserPasswd, 0)

list := users.NewUserList()
list := xusers.NewUserList()
list.SetPath(path)
users, err := list.GetAll()
if err != nil && len(users) == 0 {
Expand Down Expand Up @@ -248,7 +236,7 @@ func (u UserPasswd) Create(s string) error {
return errors.Wrap(err, "Failed entity preparation")
}

list := users.NewUserList()
list := xusers.NewUserList()
list.SetPath(s)
err = list.Load()
if err != nil {
Expand Down

0 comments on commit 9a6f6ac

Please sign in to comment.