2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-06-10 08:08:02 +00:00
|
|
|
|
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"
|
2023-11-02 01:58:03 +00:00
|
|
|
"slices"
|
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
|
|
|
|
}
|
2023-11-02 01:58:03 +00:00
|
|
|
g, err := user.LookupGroup(group)
|
2022-03-27 04:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-11-02 01:58:03 +00:00
|
|
|
ugids, err := u.GroupIds()
|
2022-03-27 04:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-11-02 01:58:03 +00:00
|
|
|
return slices.Contains(ugids, g.Gid), nil
|
2021-06-10 08:08:02 +00:00
|
|
|
}
|