2014-07-17 16:06:55 +08:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-03-01 03:14:24 +08:00
|
|
|
"os/user"
|
2014-07-17 16:06:55 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minId = 0
|
|
|
|
maxId = 1<<31 - 1 //for 32-bit systems compatibility
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-03-30 19:50:14 +08:00
|
|
|
ErrRange = fmt.Errorf("uids and gids must be in range %d-%d", minId, maxId)
|
2014-07-17 16:06:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
Pass string
|
|
|
|
Uid int
|
|
|
|
Gid int
|
|
|
|
Gecos string
|
|
|
|
Home string
|
|
|
|
Shell string
|
|
|
|
}
|
|
|
|
|
2018-03-01 03:14:24 +08:00
|
|
|
// userFromOS converts an os/user.(*User) to local User
|
|
|
|
//
|
|
|
|
// (This does not include Pass, Shell or Gecos)
|
|
|
|
func userFromOS(u *user.User) (User, error) {
|
|
|
|
newUser := User{
|
|
|
|
Name: u.Username,
|
|
|
|
Home: u.HomeDir,
|
|
|
|
}
|
|
|
|
id, err := strconv.Atoi(u.Uid)
|
|
|
|
if err != nil {
|
|
|
|
return newUser, err
|
|
|
|
}
|
|
|
|
newUser.Uid = id
|
|
|
|
|
|
|
|
id, err = strconv.Atoi(u.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return newUser, err
|
|
|
|
}
|
|
|
|
newUser.Gid = id
|
|
|
|
return newUser, nil
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
type Group struct {
|
|
|
|
Name string
|
|
|
|
Pass string
|
|
|
|
Gid int
|
|
|
|
List []string
|
|
|
|
}
|
|
|
|
|
2018-03-01 03:14:24 +08:00
|
|
|
// groupFromOS converts an os/user.(*Group) to local Group
|
|
|
|
//
|
2020-07-05 12:10:47 +08:00
|
|
|
// (This does not include Pass or List)
|
2018-03-01 03:14:24 +08:00
|
|
|
func groupFromOS(g *user.Group) (Group, error) {
|
|
|
|
newGroup := Group{
|
|
|
|
Name: g.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(g.Gid)
|
|
|
|
if err != nil {
|
|
|
|
return newGroup, err
|
|
|
|
}
|
|
|
|
newGroup.Gid = id
|
|
|
|
|
|
|
|
return newGroup, nil
|
|
|
|
}
|
|
|
|
|
2018-01-16 14:11:56 +08:00
|
|
|
// SubID represents an entry in /etc/sub{u,g}id
|
|
|
|
type SubID struct {
|
|
|
|
Name string
|
2018-06-15 01:59:28 +08:00
|
|
|
SubID int64
|
|
|
|
Count int64
|
2018-01-16 14:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IDMap represents an entry in /proc/PID/{u,g}id_map
|
|
|
|
type IDMap struct {
|
2018-06-15 01:59:28 +08:00
|
|
|
ID int64
|
|
|
|
ParentID int64
|
|
|
|
Count int64
|
2018-01-16 14:11:56 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
func parseLine(line string, v ...interface{}) {
|
2018-01-16 14:11:56 +08:00
|
|
|
parseParts(strings.Split(line, ":"), v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseParts(parts []string, v ...interface{}) {
|
|
|
|
if len(parts) == 0 {
|
2014-07-17 16:06:55 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range parts {
|
2016-03-30 19:50:14 +08:00
|
|
|
// Ignore cases where we don't have enough fields to populate the arguments.
|
|
|
|
// Some configuration files like to misbehave.
|
2014-07-17 16:06:55 +08:00
|
|
|
if len(v) <= i {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-03-30 19:50:14 +08:00
|
|
|
// Use the type of the argument to figure out how to parse it, scanf() style.
|
|
|
|
// This is legit.
|
2014-07-17 16:06:55 +08:00
|
|
|
switch e := v[i].(type) {
|
|
|
|
case *string:
|
|
|
|
*e = p
|
|
|
|
case *int:
|
2016-03-30 19:50:14 +08:00
|
|
|
// "numbers", with conversion errors ignored because of some misbehaving configuration files.
|
2014-07-17 16:06:55 +08:00
|
|
|
*e, _ = strconv.Atoi(p)
|
2018-06-15 01:59:28 +08:00
|
|
|
case *int64:
|
|
|
|
*e, _ = strconv.ParseInt(p, 10, 64)
|
2014-07-17 16:06:55 +08:00
|
|
|
case *[]string:
|
2016-03-30 19:50:14 +08:00
|
|
|
// Comma-separated lists.
|
2014-07-17 16:06:55 +08:00
|
|
|
if p != "" {
|
|
|
|
*e = strings.Split(p, ",")
|
|
|
|
} else {
|
|
|
|
*e = []string{}
|
|
|
|
}
|
|
|
|
default:
|
2016-03-30 19:50:14 +08:00
|
|
|
// Someone goof'd when writing code using this function. Scream so they can hear us.
|
2018-06-15 01:59:28 +08:00
|
|
|
panic(fmt.Sprintf("parseLine only accepts {*string, *int, *int64, *[]string} as arguments! %#v is not a pointer!", e))
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParsePasswdFile(path string) ([]User, error) {
|
|
|
|
passwd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer passwd.Close()
|
|
|
|
return ParsePasswd(passwd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParsePasswd(passwd io.Reader) ([]User, error) {
|
|
|
|
return ParsePasswdFilter(passwd, nil)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error) {
|
|
|
|
passwd, err := os.Open(path)
|
2014-07-17 16:06:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
defer passwd.Close()
|
|
|
|
return ParsePasswdFilter(passwd, filter)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("nil source for passwd-formatted data")
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(r)
|
2014-08-15 02:19:17 +08:00
|
|
|
out = []User{}
|
2014-07-17 16:06:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
for s.Scan() {
|
2016-03-30 19:50:14 +08:00
|
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if line == "" {
|
2014-07-17 16:06:55 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// see: man 5 passwd
|
|
|
|
// name:password:UID:GID:GECOS:directory:shell
|
|
|
|
// Name:Pass:Uid:Gid:Gecos:Home:Shell
|
|
|
|
// root:x:0:0:root:/root:/bin/bash
|
|
|
|
// adm:x:3:4:adm:/var/adm:/bin/false
|
2014-08-15 02:19:17 +08:00
|
|
|
p := User{}
|
2016-03-30 19:50:14 +08:00
|
|
|
parseLine(line, &p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell)
|
2014-07-17 16:06:55 +08:00
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:10:53 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-17 16:06:55 +08:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParseGroupFile(path string) ([]Group, error) {
|
|
|
|
group, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-30 19:50:14 +08:00
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
defer group.Close()
|
|
|
|
return ParseGroup(group)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseGroup(group io.Reader) ([]Group, error) {
|
|
|
|
return ParseGroupFilter(group, nil)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParseGroupFileFilter(path string, filter func(Group) bool) ([]Group, error) {
|
|
|
|
group, err := os.Open(path)
|
2014-07-17 16:06:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
defer group.Close()
|
|
|
|
return ParseGroupFilter(group, filter)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("nil source for group-formatted data")
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(r)
|
2014-08-15 02:19:17 +08:00
|
|
|
out = []Group{}
|
2014-07-17 16:06:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
text := s.Text()
|
|
|
|
if text == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// see: man 5 group
|
|
|
|
// group_name:password:GID:user_list
|
|
|
|
// Name:Pass:Gid:List
|
|
|
|
// root:x:0:root
|
|
|
|
// adm:x:4:root,adm,daemon
|
2014-08-15 02:19:17 +08:00
|
|
|
p := Group{}
|
2016-03-30 19:50:14 +08:00
|
|
|
parseLine(text, &p.Name, &p.Pass, &p.Gid, &p.List)
|
2014-07-17 16:06:55 +08:00
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:10:53 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-17 16:06:55 +08:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
type ExecUser struct {
|
2016-03-30 19:50:14 +08:00
|
|
|
Uid int
|
|
|
|
Gid int
|
|
|
|
Sgids []int
|
|
|
|
Home string
|
2014-08-15 02:19:17 +08:00
|
|
|
}
|
2014-07-17 16:06:55 +08:00
|
|
|
|
2014-12-16 22:39:09 +08:00
|
|
|
// GetExecUserPath is a wrapper for GetExecUser. It reads data from each of the
|
2014-08-15 02:19:17 +08:00
|
|
|
// given file paths and uses that data as the arguments to GetExecUser. If the
|
|
|
|
// files cannot be opened for any reason, the error is ignored and a nil
|
|
|
|
// io.Reader is passed instead.
|
2014-12-16 22:39:09 +08:00
|
|
|
func GetExecUserPath(userSpec string, defaults *ExecUser, passwdPath, groupPath string) (*ExecUser, error) {
|
2017-01-18 17:18:22 +08:00
|
|
|
var passwd, group io.Reader
|
|
|
|
|
|
|
|
if passwdFile, err := os.Open(passwdPath); err == nil {
|
|
|
|
passwd = passwdFile
|
|
|
|
defer passwdFile.Close()
|
2014-08-15 02:19:17 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 17:18:22 +08:00
|
|
|
if groupFile, err := os.Open(groupPath); err == nil {
|
|
|
|
group = groupFile
|
|
|
|
defer groupFile.Close()
|
2014-08-15 02:19:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return GetExecUser(userSpec, defaults, passwd, group)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetExecUser parses a user specification string (using the passwd and group
|
|
|
|
// readers as sources for /etc/passwd and /etc/group data, respectively). In
|
|
|
|
// the case of blank fields or missing data from the sources, the values in
|
|
|
|
// defaults is used.
|
|
|
|
//
|
|
|
|
// GetExecUser will return an error if a user or group literal could not be
|
|
|
|
// found in any entry in passwd and group respectively.
|
|
|
|
//
|
|
|
|
// Examples of valid user specifications are:
|
|
|
|
// * ""
|
|
|
|
// * "user"
|
|
|
|
// * "uid"
|
|
|
|
// * "user:group"
|
|
|
|
// * "uid:gid
|
|
|
|
// * "user:gid"
|
|
|
|
// * "uid:group"
|
2016-03-30 19:48:07 +08:00
|
|
|
//
|
|
|
|
// It should be noted that if you specify a numeric user or group id, they will
|
|
|
|
// not be evaluated as usernames (only the metadata will be filled). So attempting
|
|
|
|
// to parse a user with user.Name = "1337" will produce the user with a UID of
|
|
|
|
// 1337.
|
2014-08-15 02:19:17 +08:00
|
|
|
func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) (*ExecUser, error) {
|
|
|
|
if defaults == nil {
|
|
|
|
defaults = new(ExecUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy over defaults.
|
|
|
|
user := &ExecUser{
|
|
|
|
Uid: defaults.Uid,
|
|
|
|
Gid: defaults.Gid,
|
|
|
|
Sgids: defaults.Sgids,
|
|
|
|
Home: defaults.Home,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sgids slice *cannot* be nil.
|
|
|
|
if user.Sgids == nil {
|
|
|
|
user.Sgids = []int{}
|
|
|
|
}
|
|
|
|
|
2016-03-30 19:50:14 +08:00
|
|
|
// Allow for userArg to have either "user" syntax, or optionally "user:group" syntax
|
|
|
|
var userArg, groupArg string
|
2014-07-17 16:06:55 +08:00
|
|
|
parseLine(userSpec, &userArg, &groupArg)
|
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
// Convert userArg and groupArg to be numeric, so we don't have to execute
|
|
|
|
// Atoi *twice* for each iteration over lines.
|
|
|
|
uidArg, uidErr := strconv.Atoi(userArg)
|
|
|
|
gidArg, gidErr := strconv.Atoi(groupArg)
|
|
|
|
|
2016-03-30 19:50:14 +08:00
|
|
|
// Find the matching user.
|
2014-08-15 02:19:17 +08:00
|
|
|
users, err := ParsePasswdFilter(passwd, func(u User) bool {
|
2014-07-17 16:06:55 +08:00
|
|
|
if userArg == "" {
|
2016-03-30 19:50:14 +08:00
|
|
|
// Default to current state of the user.
|
2014-08-15 02:19:17 +08:00
|
|
|
return u.Uid == user.Uid
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2016-03-30 19:48:07 +08:00
|
|
|
|
|
|
|
if uidErr == nil {
|
|
|
|
// If the userArg is numeric, always treat it as a UID.
|
|
|
|
return uidArg == u.Uid
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Name == userArg
|
2014-07-17 16:06:55 +08:00
|
|
|
})
|
2016-03-30 19:50:14 +08:00
|
|
|
|
|
|
|
// If we can't find the user, we have to bail.
|
2014-08-15 02:19:17 +08:00
|
|
|
if err != nil && passwd != nil {
|
2014-07-17 16:06:55 +08:00
|
|
|
if userArg == "" {
|
2014-08-15 02:19:17 +08:00
|
|
|
userArg = strconv.Itoa(user.Uid)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2016-03-30 19:50:14 +08:00
|
|
|
return nil, fmt.Errorf("unable to find user %s: %v", userArg, err)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
var matchedUserName string
|
|
|
|
if len(users) > 0 {
|
|
|
|
// First match wins, even if there's more than one matching entry.
|
|
|
|
matchedUserName = users[0].Name
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Uid = users[0].Uid
|
|
|
|
user.Gid = users[0].Gid
|
|
|
|
user.Home = users[0].Home
|
2014-07-17 16:06:55 +08:00
|
|
|
} else if userArg != "" {
|
2016-03-30 19:48:07 +08:00
|
|
|
// If we can't find a user with the given username, the only other valid
|
|
|
|
// option is if it's a numeric username with no associated entry in passwd.
|
|
|
|
|
|
|
|
if uidErr != nil {
|
|
|
|
// Not numeric.
|
|
|
|
return nil, fmt.Errorf("unable to find user %s: %v", userArg, ErrNoPasswdEntries)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2016-03-30 19:48:07 +08:00
|
|
|
user.Uid = uidArg
|
2014-08-15 02:19:17 +08:00
|
|
|
|
|
|
|
// Must be inside valid uid range.
|
|
|
|
if user.Uid < minId || user.Uid > maxId {
|
|
|
|
return nil, ErrRange
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
// Okay, so it's numeric. We can just roll with this.
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
// On to the groups. If we matched a username, we need to do this because of
|
|
|
|
// the supplementary group IDs.
|
|
|
|
if groupArg != "" || matchedUserName != "" {
|
2014-08-15 02:19:17 +08:00
|
|
|
groups, err := ParseGroupFilter(group, func(g Group) bool {
|
2016-03-30 19:48:07 +08:00
|
|
|
// If the group argument isn't explicit, we'll just search for it.
|
|
|
|
if groupArg == "" {
|
|
|
|
// Check if user is a member of this group.
|
|
|
|
for _, u := range g.List {
|
|
|
|
if u == matchedUserName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
if gidErr == nil {
|
|
|
|
// If the groupArg is numeric, always treat it as a GID.
|
|
|
|
return gidArg == g.Gid
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
return g.Name == groupArg
|
2014-07-17 16:06:55 +08:00
|
|
|
})
|
2014-08-15 02:19:17 +08:00
|
|
|
if err != nil && group != nil {
|
2016-03-30 19:48:07 +08:00
|
|
|
return nil, fmt.Errorf("unable to find groups for spec %v: %v", matchedUserName, err)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:50:14 +08:00
|
|
|
// Only start modifying user.Gid if it is in explicit form.
|
2014-07-17 16:06:55 +08:00
|
|
|
if groupArg != "" {
|
2016-03-30 19:50:14 +08:00
|
|
|
if len(groups) > 0 {
|
|
|
|
// First match wins, even if there's more than one matching entry.
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Gid = groups[0].Gid
|
2017-01-09 14:56:14 +08:00
|
|
|
} else {
|
2016-03-30 19:48:07 +08:00
|
|
|
// If we can't find a group with the given name, the only other valid
|
|
|
|
// option is if it's a numeric group name with no associated entry in group.
|
|
|
|
|
|
|
|
if gidErr != nil {
|
|
|
|
// Not numeric.
|
|
|
|
return nil, fmt.Errorf("unable to find group %s: %v", groupArg, ErrNoGroupEntries)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2016-03-30 19:48:07 +08:00
|
|
|
user.Gid = gidArg
|
2014-08-15 02:19:17 +08:00
|
|
|
|
2016-03-30 19:50:14 +08:00
|
|
|
// Must be inside valid gid range.
|
2014-08-15 02:19:17 +08:00
|
|
|
if user.Gid < minId || user.Gid > maxId {
|
|
|
|
return nil, ErrRange
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 19:48:07 +08:00
|
|
|
// Okay, so it's numeric. We can just roll with this.
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2017-08-05 05:28:21 +08:00
|
|
|
} else if len(groups) > 0 {
|
|
|
|
// Supplementary group ids only make sense if in the implicit form.
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Sgids = make([]int, len(groups))
|
2014-07-17 16:06:55 +08:00
|
|
|
for i, group := range groups {
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Sgids[i] = group.Gid
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
return user, nil
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
|
2015-10-05 07:02:30 +08:00
|
|
|
// GetAdditionalGroups looks up a list of groups by name or group id
|
|
|
|
// against the given /etc/group formatted data. If a group name cannot
|
|
|
|
// be found, an error will be returned. If a group id cannot be found,
|
|
|
|
// or the given group data is nil, the id will be returned as-is
|
|
|
|
// provided it is in the legal range.
|
2015-06-28 09:14:24 +08:00
|
|
|
func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) {
|
2015-10-05 07:02:30 +08:00
|
|
|
var groups = []Group{}
|
|
|
|
if group != nil {
|
|
|
|
var err error
|
|
|
|
groups, err = ParseGroupFilter(group, func(g Group) bool {
|
|
|
|
for _, ag := range additionalGroups {
|
|
|
|
if g.Name == ag || strconv.Itoa(g.Gid) == ag {
|
|
|
|
return true
|
|
|
|
}
|
2015-05-26 03:02:34 +08:00
|
|
|
}
|
2015-10-05 07:02:30 +08:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to find additional groups %v: %v", additionalGroups, err)
|
2015-05-26 03:02:34 +08:00
|
|
|
}
|
2015-05-01 07:02:31 +08:00
|
|
|
}
|
|
|
|
|
2015-05-26 03:02:34 +08:00
|
|
|
gidMap := make(map[int]struct{})
|
|
|
|
for _, ag := range additionalGroups {
|
|
|
|
var found bool
|
|
|
|
for _, g := range groups {
|
|
|
|
// if we found a matched group either by name or gid, take the
|
|
|
|
// first matched as correct
|
|
|
|
if g.Name == ag || strconv.Itoa(g.Gid) == ag {
|
|
|
|
if _, ok := gidMap[g.Gid]; !ok {
|
|
|
|
gidMap[g.Gid] = struct{}{}
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we asked for a group but didn't find it. let's check to see
|
|
|
|
// if we wanted a numeric group
|
|
|
|
if !found {
|
|
|
|
gid, err := strconv.Atoi(ag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to find group %s", ag)
|
|
|
|
}
|
|
|
|
// Ensure gid is inside gid range.
|
|
|
|
if gid < minId || gid > maxId {
|
|
|
|
return nil, ErrRange
|
|
|
|
}
|
|
|
|
gidMap[gid] = struct{}{}
|
2015-05-01 07:02:31 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 03:02:34 +08:00
|
|
|
gids := []int{}
|
|
|
|
for gid := range gidMap {
|
|
|
|
gids = append(gids, gid)
|
|
|
|
}
|
|
|
|
return gids, nil
|
2015-05-01 07:02:31 +08:00
|
|
|
}
|
2015-06-28 09:14:24 +08:00
|
|
|
|
2015-10-05 07:02:30 +08:00
|
|
|
// GetAdditionalGroupsPath is a wrapper around GetAdditionalGroups
|
|
|
|
// that opens the groupPath given and gives it as an argument to
|
|
|
|
// GetAdditionalGroups.
|
2015-06-28 09:14:24 +08:00
|
|
|
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
|
2017-01-18 17:18:22 +08:00
|
|
|
var group io.Reader
|
|
|
|
|
|
|
|
if groupFile, err := os.Open(groupPath); err == nil {
|
|
|
|
group = groupFile
|
|
|
|
defer groupFile.Close()
|
2015-06-28 09:14:24 +08:00
|
|
|
}
|
|
|
|
return GetAdditionalGroups(additionalGroups, group)
|
|
|
|
}
|
2018-01-16 14:11:56 +08:00
|
|
|
|
|
|
|
func ParseSubIDFile(path string) ([]SubID, error) {
|
|
|
|
subid, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer subid.Close()
|
|
|
|
return ParseSubID(subid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSubID(subid io.Reader) ([]SubID, error) {
|
|
|
|
return ParseSubIDFilter(subid, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error) {
|
|
|
|
subid, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer subid.Close()
|
|
|
|
return ParseSubIDFilter(subid, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("nil source for subid-formatted data")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(r)
|
|
|
|
out = []SubID{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// see: man 5 subuid
|
|
|
|
p := SubID{}
|
|
|
|
parseLine(line, &p.Name, &p.SubID, &p.Count)
|
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:10:53 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 14:11:56 +08:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIDMapFile(path string) ([]IDMap, error) {
|
|
|
|
r, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
return ParseIDMap(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIDMap(r io.Reader) ([]IDMap, error) {
|
|
|
|
return ParseIDMapFilter(r, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error) {
|
|
|
|
r, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
return ParseIDMapFilter(r, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, fmt.Errorf("nil source for idmap-formatted data")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
s = bufio.NewScanner(r)
|
|
|
|
out = []IDMap{}
|
|
|
|
)
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
line := strings.TrimSpace(s.Text())
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// see: man 7 user_namespaces
|
|
|
|
p := IDMap{}
|
|
|
|
parseParts(strings.Fields(line), &p.ID, &p.ParentID, &p.Count)
|
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 15:10:53 +08:00
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-16 14:11:56 +08:00
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|