fix part of golint
This commit is contained in:
1
go.mod
1
go.mod
@@ -19,6 +19,7 @@ require (
|
|||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||||
github.com/magiconair/properties v1.8.6 // indirect
|
github.com/magiconair/properties v1.8.6 // indirect
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -142,6 +142,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
|
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
|
||||||
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ var (
|
|||||||
EXCHANGE_MEDIUM SCSICommandType = 0xa6
|
EXCHANGE_MEDIUM SCSICommandType = 0xa6
|
||||||
READ_12 SCSICommandType = 0xa8
|
READ_12 SCSICommandType = 0xa8
|
||||||
WRITE_12 SCSICommandType = 0xaa
|
WRITE_12 SCSICommandType = 0xaa
|
||||||
GET_PERFORMACE SCSICommandType = 0xac
|
GET_PERFORMANCE SCSICommandType = 0xac
|
||||||
READ_DVD_STRUCTURE SCSICommandType = 0xad
|
READ_DVD_STRUCTURE SCSICommandType = 0xad
|
||||||
WRITE_VERIFY_12 SCSICommandType = 0xae
|
WRITE_VERIFY_12 SCSICommandType = 0xae
|
||||||
VERIFY_12 SCSICommandType = 0xaf
|
VERIFY_12 SCSICommandType = 0xaf
|
||||||
@@ -193,7 +193,7 @@ type SCSICommand struct {
|
|||||||
type ITNexus struct {
|
type ITNexus struct {
|
||||||
// UUID v1
|
// UUID v1
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
// For protocal spec identifer
|
// For protocol spec identifer
|
||||||
Tag string `json:"Tag"`
|
Tag string `json:"Tag"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gostor/gotgt/pkg/homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -33,7 +33,7 @@ Format of configuration file
|
|||||||
"storages": [
|
"storages": [
|
||||||
{
|
{
|
||||||
"deviceID": integer, uniqu device id,
|
"deviceID": integer, uniqu device id,
|
||||||
"path": string, <protocal>:<absolute/file/path>",
|
"path": string, <protocol>:<absolute/file/path>",
|
||||||
"online": bool, online/offline
|
"online": bool, online/offline
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -126,7 +126,8 @@ type Config struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if configDir == "" {
|
if configDir == "" {
|
||||||
configDir = filepath.Join(homedir.Get(), ".gotgt")
|
homeDir, _ := homedir.Dir()
|
||||||
|
configDir = filepath.Join(homeDir, ".gotgt")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
Tianon Gravi <admwiggin@gmail.com> (@tianon)
|
|
||||||
Aleksa Sarai <cyphar@cyphar.com> (@cyphar)
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package homedir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Key returns the env var name for the user's home dir based on
|
|
||||||
// the platform being run on
|
|
||||||
func Key() string {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
return "USERPROFILE"
|
|
||||||
}
|
|
||||||
return "HOME"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the home directory of the current user with the help of
|
|
||||||
// environment variables depending on the target operating system.
|
|
||||||
// Returned path should be used with "path/filepath" to form new paths.
|
|
||||||
func Get() string {
|
|
||||||
home := os.Getenv(Key())
|
|
||||||
if home == "" && runtime.GOOS != "windows" {
|
|
||||||
if u, err := CurrentUser(); err == nil {
|
|
||||||
return u.Home
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return home
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetShortcutString returns the string that is shortcut to user's home directory
|
|
||||||
// in the native shell of the platform running on.
|
|
||||||
func GetShortcutString() string {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
return "%USERPROFILE%" // be careful while using in format functions
|
|
||||||
}
|
|
||||||
return "~"
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package homedir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
home := Get()
|
|
||||||
if home == "" {
|
|
||||||
t.Fatal("returned home directory is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !filepath.IsAbs(home) {
|
|
||||||
t.Fatalf("returned path is not absolute: %s", home)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetShortcutString(t *testing.T) {
|
|
||||||
shortcut := GetShortcutString()
|
|
||||||
if shortcut == "" {
|
|
||||||
t.Fatal("returned shortcut string is empty")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
package homedir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// The current operating system does not provide the required data for user lookups.
|
|
||||||
ErrUnsupported = errors.New("user lookup: operating system does not provide passwd-formatted data")
|
|
||||||
// No matching entries found in file.
|
|
||||||
ErrNoPasswdEntries = errors.New("no matching entries in passwd file")
|
|
||||||
ErrNoGroupEntries = errors.New("no matching entries in group file")
|
|
||||||
)
|
|
||||||
|
|
||||||
func lookupUser(filter func(u User) bool) (User, error) {
|
|
||||||
// Get operating system-specific passwd reader-closer.
|
|
||||||
passwd, err := GetPasswd()
|
|
||||||
if err != nil {
|
|
||||||
return User{}, err
|
|
||||||
}
|
|
||||||
defer passwd.Close()
|
|
||||||
|
|
||||||
// Get the users.
|
|
||||||
users, err := ParsePasswdFilter(passwd, filter)
|
|
||||||
if err != nil {
|
|
||||||
return User{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// No user entries found.
|
|
||||||
if len(users) == 0 {
|
|
||||||
return User{}, ErrNoPasswdEntries
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume the first entry is the "correct" one.
|
|
||||||
return users[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CurrentUser looks up the current user by their user id in /etc/passwd. If the
|
|
||||||
// user cannot be found (or there is no /etc/passwd file on the filesystem),
|
|
||||||
// then CurrentUser returns an error.
|
|
||||||
func CurrentUser() (User, error) {
|
|
||||||
return LookupUid(syscall.Getuid())
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupUser looks up a user by their username in /etc/passwd. If the user
|
|
||||||
// cannot be found (or there is no /etc/passwd file on the filesystem), then
|
|
||||||
// LookupUser returns an error.
|
|
||||||
func LookupUser(username string) (User, error) {
|
|
||||||
return lookupUser(func(u User) bool {
|
|
||||||
return u.Name == username
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
|
|
||||||
// be found (or there is no /etc/passwd file on the filesystem), then LookupId
|
|
||||||
// returns an error.
|
|
||||||
func LookupUid(uid int) (User, error) {
|
|
||||||
return lookupUser(func(u User) bool {
|
|
||||||
return u.Uid == uid
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupGroup(filter func(g Group) bool) (Group, error) {
|
|
||||||
// Get operating system-specific group reader-closer.
|
|
||||||
group, err := GetGroup()
|
|
||||||
if err != nil {
|
|
||||||
return Group{}, err
|
|
||||||
}
|
|
||||||
defer group.Close()
|
|
||||||
|
|
||||||
// Get the users.
|
|
||||||
groups, err := ParseGroupFilter(group, filter)
|
|
||||||
if err != nil {
|
|
||||||
return Group{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// No user entries found.
|
|
||||||
if len(groups) == 0 {
|
|
||||||
return Group{}, ErrNoGroupEntries
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume the first entry is the "correct" one.
|
|
||||||
return groups[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CurrentGroup looks up the current user's group by their primary group id's
|
|
||||||
// entry in /etc/passwd. If the group cannot be found (or there is no
|
|
||||||
// /etc/group file on the filesystem), then CurrentGroup returns an error.
|
|
||||||
func CurrentGroup() (Group, error) {
|
|
||||||
return LookupGid(syscall.Getgid())
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupGroup looks up a group by its name in /etc/group. If the group cannot
|
|
||||||
// be found (or there is no /etc/group file on the filesystem), then LookupGroup
|
|
||||||
// returns an error.
|
|
||||||
func LookupGroup(groupname string) (Group, error) {
|
|
||||||
return lookupGroup(func(g Group) bool {
|
|
||||||
return g.Name == groupname
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupGid looks up a group by its group id in /etc/group. If the group cannot
|
|
||||||
// be found (or there is no /etc/group file on the filesystem), then LookupGid
|
|
||||||
// returns an error.
|
|
||||||
func LookupGid(gid int) (Group, error) {
|
|
||||||
return lookupGroup(func(g Group) bool {
|
|
||||||
return g.Gid == gid
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
|
||||||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
||||||
|
|
||||||
package homedir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Unix-specific path to the passwd and group formatted files.
|
|
||||||
const (
|
|
||||||
unixPasswdPath = "/etc/passwd"
|
|
||||||
unixGroupPath = "/etc/group"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetPasswdPath() (string, error) {
|
|
||||||
return unixPasswdPath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetPasswd() (io.ReadCloser, error) {
|
|
||||||
return os.Open(unixPasswdPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupPath() (string, error) {
|
|
||||||
return unixGroupPath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroup() (io.ReadCloser, error) {
|
|
||||||
return os.Open(unixGroupPath)
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
//go:build !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris
|
|
||||||
// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris
|
|
||||||
|
|
||||||
package homedir
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
func GetPasswdPath() (string, error) {
|
|
||||||
return "", ErrUnsupported
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetPasswd() (io.ReadCloser, error) {
|
|
||||||
return nil, ErrUnsupported
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupPath() (string, error) {
|
|
||||||
return "", ErrUnsupported
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroup() (io.ReadCloser, error) {
|
|
||||||
return nil, ErrUnsupported
|
|
||||||
}
|
|
||||||
@@ -1,441 +0,0 @@
|
|||||||
package homedir
|
|
||||||
|
|
||||||
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 {
|
|
||||||
// Ignore cases where we don't have enough fields to populate the arguments.
|
|
||||||
// Some configuration files like to misbehave.
|
|
||||||
if len(v) <= i {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the type of the argument to figure out how to parse it, scanf() style.
|
|
||||||
// This is legit.
|
|
||||||
switch e := v[i].(type) {
|
|
||||||
case *string:
|
|
||||||
*e = p
|
|
||||||
case *int:
|
|
||||||
// "numbers", with conversion errors ignored because of some misbehaving configuration files.
|
|
||||||
*e, _ = strconv.Atoi(p)
|
|
||||||
case *[]string:
|
|
||||||
// Comma-separated lists.
|
|
||||||
if p != "" {
|
|
||||||
*e = strings.Split(p, ",")
|
|
||||||
} else {
|
|
||||||
*e = []string{}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
// Someone goof'd when writing code using this function. Scream so they can hear us.
|
|
||||||
panic(fmt.Sprintf("parseLine only accepts {*string, *int, *[]string} as arguments! %#v is not a pointer!", e))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error) {
|
|
||||||
passwd, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer passwd.Close()
|
|
||||||
return ParsePasswdFilter(passwd, filter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) {
|
|
||||||
if r == nil {
|
|
||||||
return nil, fmt.Errorf("nil source for passwd-formatted data")
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
s = bufio.NewScanner(r)
|
|
||||||
out = []User{}
|
|
||||||
)
|
|
||||||
|
|
||||||
for s.Scan() {
|
|
||||||
if err := s.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
line := strings.TrimSpace(s.Text())
|
|
||||||
if line == "" {
|
|
||||||
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
|
|
||||||
p := User{}
|
|
||||||
parseLine(line, &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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseGroupFileFilter(path string, filter func(Group) bool) ([]Group, error) {
|
|
||||||
group, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer group.Close()
|
|
||||||
return ParseGroupFilter(group, filter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) {
|
|
||||||
if r == nil {
|
|
||||||
return nil, fmt.Errorf("nil source for group-formatted data")
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
s = bufio.NewScanner(r)
|
|
||||||
out = []Group{}
|
|
||||||
)
|
|
||||||
|
|
||||||
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
|
|
||||||
p := Group{}
|
|
||||||
parseLine(text, &p.Name, &p.Pass, &p.Gid, &p.List)
|
|
||||||
|
|
||||||
if filter == nil || filter(p) {
|
|
||||||
out = append(out, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExecUser struct {
|
|
||||||
Uid int
|
|
||||||
Gid int
|
|
||||||
Sgids []int
|
|
||||||
Home string
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetExecUserPath is a wrapper for GetExecUser. It reads data from each of the
|
|
||||||
// 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.
|
|
||||||
func GetExecUserPath(userSpec string, defaults *ExecUser, passwdPath, groupPath string) (*ExecUser, error) {
|
|
||||||
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"
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
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{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allow for userArg to have either "user" syntax, or optionally "user:group" syntax
|
|
||||||
var userArg, groupArg string
|
|
||||||
parseLine(userSpec, &userArg, &groupArg)
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
|
|
||||||
// Find the matching user.
|
|
||||||
users, err := ParsePasswdFilter(passwd, func(u User) bool {
|
|
||||||
if userArg == "" {
|
|
||||||
// Default to current state of the user.
|
|
||||||
return u.Uid == user.Uid
|
|
||||||
}
|
|
||||||
|
|
||||||
if uidErr == nil {
|
|
||||||
// If the userArg is numeric, always treat it as a UID.
|
|
||||||
return uidArg == u.Uid
|
|
||||||
}
|
|
||||||
|
|
||||||
return u.Name == userArg
|
|
||||||
})
|
|
||||||
|
|
||||||
// If we can't find the user, we have to bail.
|
|
||||||
if err != nil && passwd != nil {
|
|
||||||
if userArg == "" {
|
|
||||||
userArg = strconv.Itoa(user.Uid)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("unable to find user %s: %v", userArg, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var matchedUserName string
|
|
||||||
if len(users) > 0 {
|
|
||||||
// First match wins, even if there's more than one matching entry.
|
|
||||||
matchedUserName = users[0].Name
|
|
||||||
user.Uid = users[0].Uid
|
|
||||||
user.Gid = users[0].Gid
|
|
||||||
user.Home = users[0].Home
|
|
||||||
} else if userArg != "" {
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
user.Uid = uidArg
|
|
||||||
|
|
||||||
// Must be inside valid uid range.
|
|
||||||
if user.Uid < minId || user.Uid > maxId {
|
|
||||||
return nil, ErrRange
|
|
||||||
}
|
|
||||||
|
|
||||||
// Okay, so it's numeric. We can just roll with this.
|
|
||||||
}
|
|
||||||
|
|
||||||
// On to the groups. If we matched a username, we need to do this because of
|
|
||||||
// the supplementary group IDs.
|
|
||||||
if groupArg != "" || matchedUserName != "" {
|
|
||||||
groups, err := ParseGroupFilter(group, func(g Group) bool {
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
if gidErr == nil {
|
|
||||||
// If the groupArg is numeric, always treat it as a GID.
|
|
||||||
return gidArg == g.Gid
|
|
||||||
}
|
|
||||||
|
|
||||||
return g.Name == groupArg
|
|
||||||
})
|
|
||||||
if err != nil && group != nil {
|
|
||||||
return nil, fmt.Errorf("unable to find groups for spec %v: %v", matchedUserName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only start modifying user.Gid if it is in explicit form.
|
|
||||||
if groupArg != "" {
|
|
||||||
if len(groups) > 0 {
|
|
||||||
// First match wins, even if there's more than one matching entry.
|
|
||||||
user.Gid = groups[0].Gid
|
|
||||||
} else if groupArg != "" {
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
user.Gid = gidArg
|
|
||||||
|
|
||||||
// Must be inside valid gid range.
|
|
||||||
if user.Gid < minId || user.Gid > maxId {
|
|
||||||
return nil, ErrRange
|
|
||||||
}
|
|
||||||
|
|
||||||
// Okay, so it's numeric. We can just roll with this.
|
|
||||||
}
|
|
||||||
} else if len(groups) > 0 {
|
|
||||||
// Supplementary group ids only make sense if in the implicit form.
|
|
||||||
user.Sgids = make([]int, len(groups))
|
|
||||||
for i, group := range groups {
|
|
||||||
user.Sgids[i] = group.Gid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error) {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unable to find additional groups %v: %v", additionalGroups, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gids := []int{}
|
|
||||||
for gid := range gidMap {
|
|
||||||
gids = append(gids, gid)
|
|
||||||
}
|
|
||||||
return gids, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAdditionalGroupsPath is a 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 {
|
|
||||||
defer group.Close()
|
|
||||||
}
|
|
||||||
return GetAdditionalGroups(additionalGroups, group)
|
|
||||||
}
|
|
||||||
@@ -1,500 +0,0 @@
|
|||||||
package homedir
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestUserParseLine(t *testing.T) {
|
|
||||||
var (
|
|
||||||
a, b string
|
|
||||||
c []string
|
|
||||||
d int
|
|
||||||
)
|
|
||||||
|
|
||||||
parseLine("", &a, &b)
|
|
||||||
if a != "" || b != "" {
|
|
||||||
t.Fatalf("a and b should be empty ('%v', '%v')", a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("a", &a, &b)
|
|
||||||
if a != "a" || b != "" {
|
|
||||||
t.Fatalf("a should be 'a' and b should be empty ('%v', '%v')", a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("bad boys:corny cows", &a, &b)
|
|
||||||
if a != "bad boys" || b != "corny cows" {
|
|
||||||
t.Fatalf("a should be 'bad boys' and b should be 'corny cows' ('%v', '%v')", a, b)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("", &c)
|
|
||||||
if len(c) != 0 {
|
|
||||||
t.Fatalf("c should be empty (%#v)", c)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("d,e,f:g:h:i,j,k", &c, &a, &b, &c)
|
|
||||||
if a != "g" || b != "h" || len(c) != 3 || c[0] != "i" || c[1] != "j" || c[2] != "k" {
|
|
||||||
t.Fatalf("a should be 'g', b should be 'h', and c should be ['i','j','k'] ('%v', '%v', '%#v')", a, b, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("::::::::::", &a, &b, &c)
|
|
||||||
if a != "" || b != "" || len(c) != 0 {
|
|
||||||
t.Fatalf("a, b, and c should all be empty ('%v', '%v', '%#v')", a, b, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("not a number", &d)
|
|
||||||
if d != 0 {
|
|
||||||
t.Fatalf("d should be 0 (%v)", d)
|
|
||||||
}
|
|
||||||
|
|
||||||
parseLine("b:12:c", &a, &d, &b)
|
|
||||||
if a != "b" || b != "c" || d != 12 {
|
|
||||||
t.Fatalf("a should be 'b' and b should be 'c', and d should be 12 ('%v', '%v', %v)", a, b, d)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUserParsePasswd(t *testing.T) {
|
|
||||||
users, err := ParsePasswdFilter(strings.NewReader(`
|
|
||||||
root:x:0:0:root:/root:/bin/bash
|
|
||||||
adm:x:3:4:adm:/var/adm:/bin/false
|
|
||||||
this is just some garbage data
|
|
||||||
`), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if len(users) != 3 {
|
|
||||||
t.Fatalf("Expected 3 users, got %v", len(users))
|
|
||||||
}
|
|
||||||
if users[0].Uid != 0 || users[0].Name != "root" {
|
|
||||||
t.Fatalf("Expected users[0] to be 0 - root, got %v - %v", users[0].Uid, users[0].Name)
|
|
||||||
}
|
|
||||||
if users[1].Uid != 3 || users[1].Name != "adm" {
|
|
||||||
t.Fatalf("Expected users[1] to be 3 - adm, got %v - %v", users[1].Uid, users[1].Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUserParseGroup(t *testing.T) {
|
|
||||||
groups, err := ParseGroupFilter(strings.NewReader(`
|
|
||||||
root:x:0:root
|
|
||||||
adm:x:4:root,adm,daemon
|
|
||||||
this is just some garbage data
|
|
||||||
`), nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
if len(groups) != 3 {
|
|
||||||
t.Fatalf("Expected 3 groups, got %v", len(groups))
|
|
||||||
}
|
|
||||||
if groups[0].Gid != 0 || groups[0].Name != "root" || len(groups[0].List) != 1 {
|
|
||||||
t.Fatalf("Expected groups[0] to be 0 - root - 1 member, got %v - %v - %v", groups[0].Gid, groups[0].Name, len(groups[0].List))
|
|
||||||
}
|
|
||||||
if groups[1].Gid != 4 || groups[1].Name != "adm" || len(groups[1].List) != 3 {
|
|
||||||
t.Fatalf("Expected groups[1] to be 4 - adm - 3 members, got %v - %v - %v", groups[1].Gid, groups[1].Name, len(groups[1].List))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidGetExecUser(t *testing.T) {
|
|
||||||
const passwdContent = `
|
|
||||||
root:x:0:0:root user:/root:/bin/bash
|
|
||||||
adm:x:42:43:adm:/var/adm:/bin/false
|
|
||||||
111:x:222:333::/var/garbage
|
|
||||||
odd:x:111:112::/home/odd:::::
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
const groupContent = `
|
|
||||||
root:x:0:root
|
|
||||||
adm:x:43:
|
|
||||||
grp:x:1234:root,adm
|
|
||||||
444:x:555:111
|
|
||||||
odd:x:444:
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
defaultExecUser := ExecUser{
|
|
||||||
Uid: 8888,
|
|
||||||
Gid: 8888,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/8888",
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
ref string
|
|
||||||
expected ExecUser
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
ref: "root",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 0,
|
|
||||||
Gid: 0,
|
|
||||||
Sgids: []int{0, 1234},
|
|
||||||
Home: "/root",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "adm",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 42,
|
|
||||||
Gid: 43,
|
|
||||||
Sgids: []int{1234},
|
|
||||||
Home: "/var/adm",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "root:adm",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 0,
|
|
||||||
Gid: 43,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: "/root",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "adm:1234",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 42,
|
|
||||||
Gid: 1234,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: "/var/adm",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "42:1234",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 42,
|
|
||||||
Gid: 1234,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: "/var/adm",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "1337:1234",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 1337,
|
|
||||||
Gid: 1234,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: defaultExecUser.Home,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "1337",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 1337,
|
|
||||||
Gid: defaultExecUser.Gid,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: defaultExecUser.Home,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: defaultExecUser.Uid,
|
|
||||||
Gid: defaultExecUser.Gid,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: defaultExecUser.Home,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Regression tests for #695.
|
|
||||||
{
|
|
||||||
ref: "111",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 111,
|
|
||||||
Gid: 112,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: "/home/odd",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "111:444",
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 111,
|
|
||||||
Gid: 444,
|
|
||||||
Sgids: defaultExecUser.Sgids,
|
|
||||||
Home: "/home/odd",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
passwd := strings.NewReader(passwdContent)
|
|
||||||
group := strings.NewReader(groupContent)
|
|
||||||
|
|
||||||
execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error())
|
|
||||||
t.Fail()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(test.expected, *execUser) {
|
|
||||||
t.Logf("ref: %v", test.ref)
|
|
||||||
t.Logf("got: %#v", execUser)
|
|
||||||
t.Logf("expected: %#v", test.expected)
|
|
||||||
t.Fail()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInvalidGetExecUser(t *testing.T) {
|
|
||||||
const passwdContent = `
|
|
||||||
root:x:0:0:root user:/root:/bin/bash
|
|
||||||
adm:x:42:43:adm:/var/adm:/bin/false
|
|
||||||
-42:x:12:13:broken:/very/broken
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
const groupContent = `
|
|
||||||
root:x:0:root
|
|
||||||
adm:x:43:
|
|
||||||
grp:x:1234:root,adm
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
|
|
||||||
tests := []string{
|
|
||||||
// No such user/group.
|
|
||||||
"notuser",
|
|
||||||
"notuser:notgroup",
|
|
||||||
"root:notgroup",
|
|
||||||
"notuser:adm",
|
|
||||||
"8888:notgroup",
|
|
||||||
"notuser:8888",
|
|
||||||
|
|
||||||
// Invalid user/group values.
|
|
||||||
"-1:0",
|
|
||||||
"0:-3",
|
|
||||||
"-5:-2",
|
|
||||||
"-42",
|
|
||||||
"-43",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
passwd := strings.NewReader(passwdContent)
|
|
||||||
group := strings.NewReader(groupContent)
|
|
||||||
|
|
||||||
execUser, err := GetExecUser(test, nil, passwd, group)
|
|
||||||
if err == nil {
|
|
||||||
t.Logf("got unexpected success when parsing '%s': %#v", test, execUser)
|
|
||||||
t.Fail()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetExecUserNilSources(t *testing.T) {
|
|
||||||
const passwdContent = `
|
|
||||||
root:x:0:0:root user:/root:/bin/bash
|
|
||||||
adm:x:42:43:adm:/var/adm:/bin/false
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
const groupContent = `
|
|
||||||
root:x:0:root
|
|
||||||
adm:x:43:
|
|
||||||
grp:x:1234:root,adm
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
|
|
||||||
defaultExecUser := ExecUser{
|
|
||||||
Uid: 8888,
|
|
||||||
Gid: 8888,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/8888",
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
ref string
|
|
||||||
passwd, group bool
|
|
||||||
expected ExecUser
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
ref: "",
|
|
||||||
passwd: false,
|
|
||||||
group: false,
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 8888,
|
|
||||||
Gid: 8888,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/8888",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "root",
|
|
||||||
passwd: true,
|
|
||||||
group: false,
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 0,
|
|
||||||
Gid: 0,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/root",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "0",
|
|
||||||
passwd: false,
|
|
||||||
group: false,
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 0,
|
|
||||||
Gid: 8888,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/8888",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ref: "0:0",
|
|
||||||
passwd: false,
|
|
||||||
group: false,
|
|
||||||
expected: ExecUser{
|
|
||||||
Uid: 0,
|
|
||||||
Gid: 0,
|
|
||||||
Sgids: []int{8888},
|
|
||||||
Home: "/8888",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
var passwd, group io.Reader
|
|
||||||
|
|
||||||
if test.passwd {
|
|
||||||
passwd = strings.NewReader(passwdContent)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.group {
|
|
||||||
group = strings.NewReader(groupContent)
|
|
||||||
}
|
|
||||||
|
|
||||||
execUser, err := GetExecUser(test.ref, &defaultExecUser, passwd, group)
|
|
||||||
if err != nil {
|
|
||||||
t.Logf("got unexpected error when parsing '%s': %s", test.ref, err.Error())
|
|
||||||
t.Fail()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(test.expected, *execUser) {
|
|
||||||
t.Logf("got: %#v", execUser)
|
|
||||||
t.Logf("expected: %#v", test.expected)
|
|
||||||
t.Fail()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetAdditionalGroups(t *testing.T) {
|
|
||||||
const groupContent = `
|
|
||||||
root:x:0:root
|
|
||||||
adm:x:43:
|
|
||||||
grp:x:1234:root,adm
|
|
||||||
adm:x:4343:root,adm-duplicate
|
|
||||||
this is just some garbage data
|
|
||||||
`
|
|
||||||
tests := []struct {
|
|
||||||
groups []string
|
|
||||||
expected []int
|
|
||||||
hasError bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
// empty group
|
|
||||||
groups: []string{},
|
|
||||||
expected: []int{},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// single group
|
|
||||||
groups: []string{"adm"},
|
|
||||||
expected: []int{43},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// multiple groups
|
|
||||||
groups: []string{"adm", "grp"},
|
|
||||||
expected: []int{43, 1234},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// invalid group
|
|
||||||
groups: []string{"adm", "grp", "not-exist"},
|
|
||||||
expected: nil,
|
|
||||||
hasError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// group with numeric id
|
|
||||||
groups: []string{"43"},
|
|
||||||
expected: []int{43},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// group with unknown numeric id
|
|
||||||
groups: []string{"adm", "10001"},
|
|
||||||
expected: []int{43, 10001},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// groups specified twice with numeric and name
|
|
||||||
groups: []string{"adm", "43"},
|
|
||||||
expected: []int{43},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// groups with too small id
|
|
||||||
groups: []string{"-1"},
|
|
||||||
expected: nil,
|
|
||||||
hasError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// groups with too large id
|
|
||||||
groups: []string{strconv.Itoa(1 << 31)},
|
|
||||||
expected: nil,
|
|
||||||
hasError: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
group := strings.NewReader(groupContent)
|
|
||||||
|
|
||||||
gids, err := GetAdditionalGroups(test.groups, group)
|
|
||||||
if test.hasError && err == nil {
|
|
||||||
t.Errorf("Parse(%#v) expects error but has none", test)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !test.hasError && err != nil {
|
|
||||||
t.Errorf("Parse(%#v) has error %v", test, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sort.Sort(sort.IntSlice(gids))
|
|
||||||
if !reflect.DeepEqual(gids, test.expected) {
|
|
||||||
t.Errorf("Gids(%v), expect %v from groups %v", gids, test.expected, test.groups)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetAdditionalGroupsNumeric(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
groups []string
|
|
||||||
expected []int
|
|
||||||
hasError bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
// numeric groups only
|
|
||||||
groups: []string{"1234", "5678"},
|
|
||||||
expected: []int{1234, 5678},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// numeric and alphabetic
|
|
||||||
groups: []string{"1234", "fake"},
|
|
||||||
expected: nil,
|
|
||||||
hasError: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
gids, err := GetAdditionalGroups(test.groups, nil)
|
|
||||||
if test.hasError && err == nil {
|
|
||||||
t.Errorf("Parse(%#v) expects error but has none", test)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !test.hasError && err != nil {
|
|
||||||
t.Errorf("Parse(%#v) has error %v", test, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
sort.Sort(sort.IntSlice(gids))
|
|
||||||
if !reflect.DeepEqual(gids, test.expected) {
|
|
||||||
t.Errorf("Gids(%v), expect %v from groups %v", gids, test.expected, test.groups)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -117,7 +117,7 @@ var (
|
|||||||
ASC_MEDIUM_SRC_EMPTY SCSISubError = 0x3b0e
|
ASC_MEDIUM_SRC_EMPTY SCSISubError = 0x3b0e
|
||||||
ASC_POSITION_PAST_BOM SCSISubError = 0x3b0c
|
ASC_POSITION_PAST_BOM SCSISubError = 0x3b0c
|
||||||
ASC_MEDIUM_REMOVAL_PREVENTED SCSISubError = 0x5302
|
ASC_MEDIUM_REMOVAL_PREVENTED SCSISubError = 0x5302
|
||||||
ASC_INSUFFICENT_REGISTRATION_RESOURCES SCSISubError = 0x5504
|
ASC_INSUFFICIENT_REGISTRATION_RESOURCES SCSISubError = 0x5504
|
||||||
ASC_BAD_MICROCODE_DETECTED SCSISubError = 0x8283
|
ASC_BAD_MICROCODE_DETECTED SCSISubError = 0x8283
|
||||||
|
|
||||||
// Key 6: Unit Attention
|
// Key 6: Unit Attention
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SCSITargetService SCSI target service
|
||||||
type SCSITargetService struct {
|
type SCSITargetService struct {
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
Targets []*api.SCSITarget
|
Targets []*api.SCSITarget
|
||||||
@@ -36,6 +37,7 @@ type SCSITargetService struct {
|
|||||||
var _instance *SCSITargetService
|
var _instance *SCSITargetService
|
||||||
var service sync.Once
|
var service sync.Once
|
||||||
|
|
||||||
|
// NewSCSITargetService create a new SCSITargetService
|
||||||
func NewSCSITargetService() *SCSITargetService {
|
func NewSCSITargetService() *SCSITargetService {
|
||||||
service.Do(func() {
|
service.Do(func() {
|
||||||
_instance = &SCSITargetService{Targets: []*api.SCSITarget{}}
|
_instance = &SCSITargetService{Targets: []*api.SCSITarget{}}
|
||||||
@@ -43,6 +45,7 @@ func NewSCSITargetService() *SCSITargetService {
|
|||||||
return _instance
|
return _instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTargetList get SCSI target list
|
||||||
func (s *SCSITargetService) GetTargetList() ([]api.SCSITarget, error) {
|
func (s *SCSITargetService) GetTargetList() ([]api.SCSITarget, error) {
|
||||||
result := []api.SCSITarget{}
|
result := []api.SCSITarget{}
|
||||||
s.mutex.RLock()
|
s.mutex.RLock()
|
||||||
@@ -53,6 +56,7 @@ func (s *SCSITargetService) GetTargetList() ([]api.SCSITarget, error) {
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resize resize
|
||||||
func (s *SCSITargetService) Resize(size uint64) error {
|
func (s *SCSITargetService) Resize(size uint64) error {
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
//TODO for multiple LUNs
|
//TODO for multiple LUNs
|
||||||
@@ -67,6 +71,7 @@ func (s *SCSITargetService) Resize(size uint64) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddCommandQueue add command queue
|
||||||
func (s *SCSITargetService) AddCommandQueue(tid int, scmd *api.SCSICommand) error {
|
func (s *SCSITargetService) AddCommandQueue(tid int, scmd *api.SCSICommand) error {
|
||||||
var (
|
var (
|
||||||
target *api.SCSITarget
|
target *api.SCSITarget
|
||||||
@@ -115,22 +120,26 @@ func (s *SCSITargetService) AddCommandQueue(tid int, scmd *api.SCSICommand) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SCSIServiceAction SCSI service action
|
||||||
type SCSIServiceAction struct {
|
type SCSIServiceAction struct {
|
||||||
ServiceAction uint8
|
ServiceAction uint8
|
||||||
CommandPerformFunc api.CommandFunc
|
CommandPerformFunc api.CommandFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SCSIDeviceOperation SCSI device operation
|
||||||
type SCSIDeviceOperation struct {
|
type SCSIDeviceOperation struct {
|
||||||
CommandPerformFunc api.CommandFunc
|
CommandPerformFunc api.CommandFunc
|
||||||
ServiceAction []*SCSIServiceAction
|
ServiceAction []*SCSIServiceAction
|
||||||
PRConflictBits uint8
|
PRConflictBits uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BaseSCSIDeviceProtocol base SCSI device protocol
|
||||||
type BaseSCSIDeviceProtocol struct {
|
type BaseSCSIDeviceProtocol struct {
|
||||||
DeviceType api.SCSIDeviceType
|
DeviceType api.SCSIDeviceType
|
||||||
SCSIDeviceOps []SCSIDeviceOperation
|
SCSIDeviceOps []SCSIDeviceOperation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSCSIDeviceOperation create a new SCSI device operation
|
||||||
func NewSCSIDeviceOperation(fn api.CommandFunc, sa []*SCSIServiceAction, pr uint8) SCSIDeviceOperation {
|
func NewSCSIDeviceOperation(fn api.CommandFunc, sa []*SCSIServiceAction, pr uint8) SCSIDeviceOperation {
|
||||||
return SCSIDeviceOperation{
|
return SCSIDeviceOperation{
|
||||||
CommandPerformFunc: fn,
|
CommandPerformFunc: fn,
|
||||||
@@ -139,6 +148,7 @@ func NewSCSIDeviceOperation(fn api.CommandFunc, sa []*SCSIServiceAction, pr uint
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildSenseData build sense data
|
||||||
func BuildSenseData(cmd *api.SCSICommand, key byte, asc SCSISubError) {
|
func BuildSenseData(cmd *api.SCSICommand, key byte, asc SCSISubError) {
|
||||||
senseBuffer := &bytes.Buffer{}
|
senseBuffer := &bytes.Buffer{}
|
||||||
inBufLen, ok := SCSICDBBufXLength(cmd.SCB)
|
inBufLen, ok := SCSICDBBufXLength(cmd.SCB)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
// Version shows the version of gotgt.
|
// Version shows the version of gotgt.
|
||||||
Version = "Not provided."
|
Version = "Not provided."
|
||||||
// SCSI version string MUST be shorter than 4 characters
|
// SCSIVersion string MUST be shorter than 4 characters
|
||||||
SCSIVersion = "0.1"
|
SCSIVersion = "0.1"
|
||||||
// GitSHA shoows the git commit id of volcano.
|
// GitSHA shoows the git commit id of volcano.
|
||||||
GitSHA = "Not provided."
|
GitSHA = "Not provided."
|
||||||
|
|||||||
Reference in New Issue
Block a user