major changes
This commit is contained in:
parent
a3270f399d
commit
2b56832843
83 changed files with 7760 additions and 1244 deletions
|
|
@ -1 +1,2 @@
|
|||
example/example
|
||||
port80
|
||||
port80and443
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Copyright (C) 2016 Marius Schellenberger
|
||||
Copyright (C) 2017 Marius Schellenberger
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# godrop - drop privileges
|
||||
|
||||
Godrop is a simple library to drop privileges on linux maschines.
|
||||
|
||||
See the examples directory on how to use the `Drop` and `MultiDrop` functions.
|
||||
|
|
|
|||
162
vendor/git.giftfish.de/ston1th/godrop/godrop.go
vendored
162
vendor/git.giftfish.de/ston1th/godrop/godrop.go
vendored
|
|
@ -1,133 +1,87 @@
|
|||
// Copyright (C) 2017 Marius Schellenberger
|
||||
|
||||
// Package godrop provides a simple privileges dropping library
|
||||
package godrop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
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")
|
||||
func errf(err error) error {
|
||||
return fmt.Errorf("godrop: %s", err)
|
||||
}
|
||||
|
||||
// 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 compiling without cgo, 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 {
|
||||
uid, err := UID(c.User)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gid, err := GID(c.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch os.Getuid() {
|
||||
case 0:
|
||||
ln, err := f()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l, ok := ln.(*net.TCPListener)
|
||||
if !ok {
|
||||
return errors.New("godrop: interface conversion failed")
|
||||
}
|
||||
f, err := l.File()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||
cmd.ExtraFiles = []*os.File{f}
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Chroot: c.Chroot,
|
||||
Credential: &syscall.Credential{
|
||||
Uid: uint32(uid),
|
||||
Gid: uint32(gid),
|
||||
},
|
||||
Setsid: true,
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Process.Release()
|
||||
os.Exit(0)
|
||||
case uid:
|
||||
return nil
|
||||
}
|
||||
return errors.New("godrop: droping priviledges failed")
|
||||
return MultiDrop(c, func() ([]net.Listener, error) {
|
||||
l, err := f()
|
||||
return []net.Listener{l}, err
|
||||
})
|
||||
}
|
||||
|
||||
// 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 err
|
||||
return errf(err)
|
||||
}
|
||||
gid, err := GID(c.Group)
|
||||
if uid == 0 {
|
||||
return errf(errors.New("unable to drop privileges to uid 0 (root)"))
|
||||
}
|
||||
gid, err := groupID(c.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
return errf(err)
|
||||
}
|
||||
switch os.Getuid() {
|
||||
case 0:
|
||||
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||
ln, err := f()
|
||||
if err != nil {
|
||||
return err
|
||||
return errf(err)
|
||||
}
|
||||
for _, v := range ln {
|
||||
l, ok := v.(*net.TCPListener)
|
||||
if !ok {
|
||||
return errors.New("godrop: interface conversion failed")
|
||||
for i, v := range ln {
|
||||
var f *os.File
|
||||
switch l := v.(type) {
|
||||
case *net.TCPListener:
|
||||
f, err = l.File()
|
||||
l.Close()
|
||||
case *net.UnixListener:
|
||||
f, err = l.File()
|
||||
l.Close()
|
||||
default:
|
||||
return errf(fmt.Errorf("index: %d listener is not type of either *net.TCPListener or *net.UnixListener", i))
|
||||
}
|
||||
f, err := l.File()
|
||||
if err != nil {
|
||||
return err
|
||||
return errf(fmt.Errorf("index: %d %s", i, err))
|
||||
}
|
||||
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{
|
||||
|
|
@ -138,23 +92,35 @@ func MultiDrop(c Config, f func() ([]net.Listener, error)) error {
|
|||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
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:
|
||||
return nil
|
||||
}
|
||||
return errors.New("godrop: droping priviledges failed")
|
||||
return errf(errors.New("dropping priviledges failed"))
|
||||
}
|
||||
|
||||
// GetListener returns the listener socket of file descriptor 3
|
||||
func GetListener() (net.Listener, error) {
|
||||
return net.FileListener(os.NewFile(3, "[socket]"))
|
||||
return GetListenerFd(3)
|
||||
}
|
||||
|
||||
// GetListenerFd returns the listener socket of the given file descriptor
|
||||
func GetListenerFd(fd int) (net.Listener, error) {
|
||||
return net.FileListener(os.NewFile(uintptr(fd), "[socket]"))
|
||||
f := os.NewFile(uintptr(fd), "")
|
||||
defer f.Close()
|
||||
return net.FileListener(f)
|
||||
}
|
||||
|
|
|
|||
29
vendor/git.giftfish.de/ston1th/godrop/lookup.go
vendored
Normal file
29
vendor/git.giftfish.de/ston1th/godrop/lookup.go
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (C) 2017 Marius Schellenberger
|
||||
|
||||
package godrop
|
||||
|
||||
import "strconv"
|
||||
|
||||
func atoi(a string) (int, error) {
|
||||
i, err := strconv.Atoi(a)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func userID(username string) (uid int, err error) {
|
||||
id, err := lookupUID(username)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return atoi(id)
|
||||
}
|
||||
|
||||
func groupID(name string) (int, error) {
|
||||
id, err := lookupGID(name)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return atoi(id)
|
||||
}
|
||||
23
vendor/git.giftfish.de/ston1th/godrop/lookup_cgo.go
vendored
Normal file
23
vendor/git.giftfish.de/ston1th/godrop/lookup_cgo.go
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright (C) 2017 Marius Schellenberger
|
||||
|
||||
// +build cgo
|
||||
|
||||
package godrop
|
||||
|
||||
import "os/user"
|
||||
|
||||
func lookupUID(username string) (string, error) {
|
||||
u, err := user.Lookup(username)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return u.Uid, err
|
||||
}
|
||||
|
||||
func lookupGID(name string) (string, error) {
|
||||
g, err := user.LookupGroup(name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return g.Gid, err
|
||||
}
|
||||
43
vendor/git.giftfish.de/ston1th/godrop/lookup_nocgo.go
vendored
Normal file
43
vendor/git.giftfish.de/ston1th/godrop/lookup_nocgo.go
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// 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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue