added generic user lookup and foreground processes
This commit is contained in:
parent
4b88a07ef5
commit
1e230617ce
7 changed files with 134 additions and 50 deletions
71
godrop.go
71
godrop.go
|
|
@ -6,12 +6,10 @@ package godrop
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
|
|
@ -19,47 +17,20 @@ func errf(err error) error {
|
|||
return fmt.Errorf("godrop: %s", err)
|
||||
}
|
||||
|
||||
func readFile(name, file string) (id int, 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 strconv.Atoi(m[0][1])
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UID of user
|
||||
func UID(user string) (int, error) {
|
||||
return readFile(user, "/etc/passwd")
|
||||
}
|
||||
|
||||
// GID of group
|
||||
func GID(group string) (int, error) {
|
||||
return readFile(group, "/etc/group")
|
||||
}
|
||||
|
||||
// Config represents the drop config
|
||||
type Config struct {
|
||||
User string
|
||||
Group string
|
||||
// User is the user to drop privileges to.
|
||||
User string
|
||||
// Group is the group to drop privileges to.
|
||||
Group string
|
||||
// Chroot is the directory to chroot into. Leave this emptry for no chroot.
|
||||
// When running on linux, make sure the chroot directory contains the /etc/passwd and /etc/group files.
|
||||
Chroot string
|
||||
// Set to true, to run the process in the foreground.
|
||||
Foreground bool
|
||||
}
|
||||
|
||||
// Drop will spawn a new process, hand over the listening socket file descriptor and terminate itself after
|
||||
// Drop will spawn a new process and hand over the listening socket file descriptor
|
||||
func Drop(c Config, f func() (net.Listener, error)) error {
|
||||
return MultiDrop(c, func() ([]net.Listener, error) {
|
||||
l, err := f()
|
||||
|
|
@ -67,16 +38,16 @@ func Drop(c Config, f func() (net.Listener, error)) error {
|
|||
})
|
||||
}
|
||||
|
||||
// MultiDrop will spawn a new process, hand over the all listening sockets and terminate itself after
|
||||
// MultiDrop will spawn a new process and hand over the all listening sockets
|
||||
func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
||||
uid, err := UID(c.User)
|
||||
uid, err := userID(c.User)
|
||||
if err != nil {
|
||||
return errf(err)
|
||||
}
|
||||
if uid == 0 {
|
||||
return errf(errors.New("unable to drop privileges to uid 0 (root)"))
|
||||
}
|
||||
gid, err := GID(c.Group)
|
||||
gid, err := groupID(c.Group)
|
||||
if err != nil {
|
||||
return errf(err)
|
||||
}
|
||||
|
|
@ -105,6 +76,12 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
||||
}
|
||||
|
||||
if c.Foreground {
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
}
|
||||
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Chroot: c.Chroot,
|
||||
Credential: &syscall.Credential{
|
||||
|
|
@ -118,6 +95,16 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
return errf(err)
|
||||
}
|
||||
|
||||
if c.Foreground {
|
||||
go func() {
|
||||
sigs := make(chan os.Signal)
|
||||
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||
sig := <-sigs
|
||||
cmd.Process.Signal(sig)
|
||||
}()
|
||||
_ = cmd.Wait()
|
||||
os.Exit(int(cmd.ProcessState.Sys().(syscall.WaitStatus)))
|
||||
}
|
||||
cmd.Process.Release()
|
||||
os.Exit(0)
|
||||
case uid:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue