43 lines
752 B
Go
43 lines
752 B
Go
// Copyright (C) 2017 Marius Schellenberger
|
|
|
|
// +build darwin dragonfly freebsd android linux netbsd openbsd solaris
|
|
// +build !cgo
|
|
|
|
package godrop
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"regexp"
|
|
)
|
|
|
|
func readFile(name, file string) (id string, err error) {
|
|
id = "-1"
|
|
if name == "" {
|
|
return
|
|
}
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
return
|
|
}
|
|
b, err := ioutil.ReadAll(f)
|
|
if err != nil {
|
|
return
|
|
}
|
|
r := regexp.MustCompile(name + `:.*?:(\d+):`)
|
|
m := r.FindAllStringSubmatch(string(b), 1)
|
|
if len(m) == 1 {
|
|
if len(m[0]) == 2 {
|
|
return m[0][1], nil
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func lookupUID(username string) (string, error) {
|
|
return readFile(username, "/etc/passwd")
|
|
}
|
|
|
|
func lookupGID(name string) (string, error) {
|
|
return readFile(name, "/etc/group")
|
|
}
|