mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-07 06:22:17 -04:00
AB#2222 replace unlicensed passwd package with own implementation
This commit is contained in:
parent
f57a7e3ed0
commit
48d614c959
8 changed files with 75 additions and 32 deletions
|
@ -1,12 +1,25 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"github.com/willdonnelly/passwd"
|
||||
)
|
||||
|
||||
// Entries contains the information for each user defined in '/etc/passwd'. Re-exported to allow other module to only import this passwd module.
|
||||
type Entries map[string]passwd.Entry
|
||||
// Entry is an entry of a '/etc/passwd' file.
|
||||
type Entry struct {
|
||||
Password string
|
||||
UID string
|
||||
GID string
|
||||
GECOS string
|
||||
Directory string
|
||||
Shell string
|
||||
}
|
||||
|
||||
// Entries contains the information for each user defined in '/etc/passwd'.
|
||||
type Entries map[string]Entry
|
||||
|
||||
// Passwd allows to parse users from '/etc/passwd' on the local system.
|
||||
type Passwd struct{}
|
||||
|
@ -24,6 +37,26 @@ func (p Passwd) parseFile(fs afero.Fs, path string) (Entries, error) {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
entries, err := passwd.ParseReader(file)
|
||||
return Entries(entries), err
|
||||
entries := Entries{}
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
// File format: https://man7.org/linux/man-pages/man5/passwd.5.html
|
||||
|
||||
fields := strings.Split(scanner.Text(), ":")
|
||||
if len(fields) != 7 {
|
||||
return nil, errors.New("invalid number of fields")
|
||||
}
|
||||
|
||||
entries[fields[0]] = Entry{
|
||||
Password: fields[1],
|
||||
UID: fields[2],
|
||||
GID: fields[3],
|
||||
GECOS: fields[4],
|
||||
Directory: fields[5],
|
||||
Shell: fields[6],
|
||||
}
|
||||
}
|
||||
|
||||
return entries, scanner.Err()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue