2021-06-10 08:08:02 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-10-05 22:21:18 +00:00
|
|
|
// Package groupmember verifies group membership of the provided user on the
|
2021-06-10 08:08:02 +00:00
|
|
|
// local system.
|
|
|
|
package groupmember
|
|
|
|
|
|
|
|
import (
|
2022-03-27 04:45:06 +00:00
|
|
|
"os/user"
|
2021-06-10 08:08:02 +00:00
|
|
|
)
|
|
|
|
|
2021-10-05 22:21:18 +00:00
|
|
|
// IsMemberOfGroup reports whether the provided user is a member of
|
|
|
|
// the provided system group.
|
2021-06-10 08:08:02 +00:00
|
|
|
func IsMemberOfGroup(group, userName string) (bool, error) {
|
2022-03-27 04:45:06 +00:00
|
|
|
u, err := user.Lookup(userName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
ugids, err := u.GroupIds()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
g, err := user.LookupGroup(group)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, ugid := range ugids {
|
|
|
|
if g.Gid == ugid {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
2021-06-10 08:08:02 +00:00
|
|
|
}
|