go_clientinfo/args/args_funcs.go
2024-12-12 02:22:54 -05:00

76 lines
1.5 KiB
Go

package args
import (
`errors`
`os/user`
`strconv`
)
// ModesAndOwners returns the evaluated file UID, file GID, dir UID, dir GID, file mode, and dir mode for a UDS socket.
func (a *Args) ModesAndOwners() (perms UdsPerms, err error) {
var idInt int
var uGid int
var u *user.User
var g *user.Group
var nErr *strconv.NumError = new(strconv.NumError)
perms.FMode = a.SockMode
perms.DMode = a.SockDirMode
// UID is always this user
if u, err = user.Current(); err != nil {
return
}
if perms.UID, err = strconv.Atoi(u.Uid); err != nil {
return
}
if g, err = user.LookupGroupId(u.Gid); err != nil {
return
}
if uGid, err = strconv.Atoi(g.Gid); err != nil {
return
}
perms.FGID = uGid
if a.SockGrp != nil {
// First try a direct GID.
if idInt, err = strconv.Atoi(*a.SockGrp); err != nil {
if errors.As(err, &nErr) {
err = nil
// And then try a group name.
if g, err = user.LookupGroup(*a.SockGrp); err != nil {
return
}
if idInt, err = strconv.Atoi(g.Gid); err != nil {
return
}
} else {
return
}
}
perms.FGID = idInt
}
perms.DGID = uGid
if a.SockDirGrp != nil {
// First try a direct GID.
if idInt, err = strconv.Atoi(*a.SockDirGrp); err != nil {
if errors.As(err, &nErr) {
err = nil
// And then try a group name.
if g, err = user.LookupGroup(*a.SockDirGrp); err != nil {
return
}
if idInt, err = strconv.Atoi(g.Gid); err != nil {
return
}
} else {
return
}
}
perms.DGID = idInt
}
return
}