2014-07-17 16:06:55 +08:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
minId = 0
|
|
|
|
maxId = 1<<31 - 1 //for 32-bit systems compatibility
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrRange = fmt.Errorf("Uids and gids must be in range %d-%d", minId, maxId)
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
Pass string
|
|
|
|
Uid int
|
|
|
|
Gid int
|
|
|
|
Gecos string
|
|
|
|
Home string
|
|
|
|
Shell string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Group struct {
|
|
|
|
Name string
|
|
|
|
Pass string
|
|
|
|
Gid int
|
|
|
|
List []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseLine(line string, v ...interface{}) {
|
|
|
|
if line == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(line, ":")
|
|
|
|
for i, p := range parts {
|
|
|
|
if len(v) <= i {
|
|
|
|
// if we have more "parts" than we have places to put them, bail for great "tolerance" of naughty configuration files
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e := v[i].(type) {
|
|
|
|
case *string:
|
|
|
|
// "root", "adm", "/bin/bash"
|
|
|
|
*e = p
|
|
|
|
case *int:
|
|
|
|
// "0", "4", "1000"
|
|
|
|
// ignore string to int conversion errors, for great "tolerance" of naughty configuration files
|
|
|
|
*e, _ = strconv.Atoi(p)
|
|
|
|
case *[]string:
|
|
|
|
// "", "root", "root,adm,daemon"
|
|
|
|
if p != "" {
|
|
|
|
*e = strings.Split(p, ",")
|
|
|
|
} else {
|
|
|
|
*e = []string{}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// panic, because this is a programming/logic error, not a runtime one
|
|
|
|
panic("parseLine expects only pointers! argument " + strconv.Itoa(i) + " is not a pointer!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := strings.TrimSpace(s.Text())
|
|
|
|
if text == "" {
|
|
|
|
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{}
|
2014-07-17 16:06:55 +08:00
|
|
|
parseLine(
|
|
|
|
text,
|
|
|
|
&p.Name, &p.Pass, &p.Uid, &p.Gid, &p.Gecos, &p.Home, &p.Shell,
|
|
|
|
)
|
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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() {
|
|
|
|
if err := s.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}
|
2014-07-17 16:06:55 +08:00
|
|
|
parseLine(
|
|
|
|
text,
|
|
|
|
&p.Name, &p.Pass, &p.Gid, &p.List,
|
|
|
|
)
|
|
|
|
|
|
|
|
if filter == nil || filter(p) {
|
|
|
|
out = append(out, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
type ExecUser struct {
|
|
|
|
Uid, Gid int
|
|
|
|
Sgids []int
|
|
|
|
Home string
|
|
|
|
}
|
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) {
|
2014-08-15 02:19:17 +08:00
|
|
|
passwd, err := os.Open(passwdPath)
|
|
|
|
if err != nil {
|
|
|
|
passwd = nil
|
|
|
|
} else {
|
|
|
|
defer passwd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := os.Open(groupPath)
|
|
|
|
if err != nil {
|
|
|
|
group = nil
|
|
|
|
} else {
|
|
|
|
defer group.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) (*ExecUser, error) {
|
|
|
|
var (
|
2014-07-17 16:06:55 +08:00
|
|
|
userArg, groupArg string
|
2014-08-15 02:19:17 +08:00
|
|
|
name string
|
2014-07-17 16:06:55 +08:00
|
|
|
)
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
// allow for userArg to have either "user" syntax, or optionally "user:group" syntax
|
|
|
|
parseLine(userSpec, &userArg, &groupArg)
|
|
|
|
|
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 == "" {
|
2014-08-15 02:19:17 +08:00
|
|
|
return u.Uid == user.Uid
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
return u.Name == userArg || strconv.Itoa(u.Uid) == userArg
|
|
|
|
})
|
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
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
return nil, fmt.Errorf("Unable to find user %v: %v", userArg, err)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
haveUser := users != nil && len(users) > 0
|
|
|
|
if haveUser {
|
|
|
|
// if we found any user entries that matched our filter, let's take the first one as "correct"
|
2014-08-15 02:19:17 +08:00
|
|
|
name = users[0].Name
|
|
|
|
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 != "" {
|
|
|
|
// we asked for a user but didn't find them... let's check to see if we wanted a numeric user
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Uid, err = strconv.Atoi(userArg)
|
2014-07-17 16:06:55 +08:00
|
|
|
if err != nil {
|
|
|
|
// not numeric - we have to bail
|
2014-08-15 02:19:17 +08:00
|
|
|
return nil, fmt.Errorf("Unable to find user %v", userArg)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// if userArg couldn't be found in /etc/passwd but is numeric, just roll with it - this is legit
|
|
|
|
}
|
|
|
|
|
2014-08-15 02:19:17 +08:00
|
|
|
if groupArg != "" || name != "" {
|
|
|
|
groups, err := ParseGroupFilter(group, func(g Group) bool {
|
|
|
|
// Explicit group format takes precedence.
|
2014-07-17 16:06:55 +08:00
|
|
|
if groupArg != "" {
|
|
|
|
return g.Name == groupArg || strconv.Itoa(g.Gid) == groupArg
|
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
|
|
|
|
// Check if user is a member.
|
2014-07-17 16:06:55 +08:00
|
|
|
for _, u := range g.List {
|
2014-08-15 02:19:17 +08:00
|
|
|
if u == name {
|
2014-07-17 16:06:55 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
|
2014-07-17 16:06:55 +08:00
|
|
|
return false
|
|
|
|
})
|
2014-08-15 02:19:17 +08:00
|
|
|
if err != nil && group != nil {
|
|
|
|
return nil, fmt.Errorf("Unable to find groups for user %v: %v", users[0].Name, err)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
haveGroup := groups != nil && len(groups) > 0
|
|
|
|
if groupArg != "" {
|
|
|
|
if haveGroup {
|
|
|
|
// if we found any group entries that matched our filter, let's take the first one as "correct"
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Gid = groups[0].Gid
|
2014-07-17 16:06:55 +08:00
|
|
|
} else {
|
|
|
|
// we asked for a group but didn't find id... let's check to see if we wanted a numeric group
|
2014-08-15 02:19:17 +08:00
|
|
|
user.Gid, err = strconv.Atoi(groupArg)
|
2014-07-17 16:06:55 +08:00
|
|
|
if err != nil {
|
|
|
|
// not numeric - we have to bail
|
2014-08-15 02:19:17 +08:00
|
|
|
return nil, fmt.Errorf("Unable to find group %v", groupArg)
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
2014-08-15 02:19:17 +08:00
|
|
|
|
|
|
|
// Ensure gid is inside gid range.
|
|
|
|
if user.Gid < minId || user.Gid > maxId {
|
|
|
|
return nil, ErrRange
|
2014-07-17 16:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if groupArg couldn't be found in /etc/group but is numeric, just roll with it - this is legit
|
|
|
|
}
|
|
|
|
} else if haveGroup {
|
2014-08-15 02:19:17 +08:00
|
|
|
// If implicit group format, fill supplementary gids.
|
|
|
|
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-06-28 09:14:24 +08:00
|
|
|
// GetAdditionalGroups looks up a list of groups by name or group id against
|
|
|
|
// 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, it will be returned
|
|
|
|
// as-is.
|
|
|
|
func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) {
|
|
|
|
groups, err := ParseGroupFilter(group, func(g Group) bool {
|
2015-05-26 03:02:34 +08:00
|
|
|
for _, ag := range additionalGroups {
|
|
|
|
if g.Name == ag || strconv.Itoa(g.Gid) == ag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-05-01 07:02:31 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-05-26 03:02:34 +08:00
|
|
|
return nil, fmt.Errorf("Unable to find additional groups %v: %v", additionalGroups, err)
|
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
|
|
|
|
|
|
|
// Wrapper around GetAdditionalGroups that opens the groupPath given and gives
|
|
|
|
// it as an argument to GetAdditionalGroups.
|
|
|
|
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
|
|
|
|
group, err := os.Open(groupPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to open group file: %v", err)
|
|
|
|
}
|
|
|
|
defer group.Close()
|
|
|
|
return GetAdditionalGroups(additionalGroups, group)
|
|
|
|
}
|