initial commit
This commit is contained in:
commit
3f79fc8e6c
527 changed files with 373170 additions and 0 deletions
1
vendor/git.giftfish.de/ston1th/godrop/.gitignore
vendored
Normal file
1
vendor/git.giftfish.de/ston1th/godrop/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
example/example
|
||||
24
vendor/git.giftfish.de/ston1th/godrop/LICENSE
vendored
Normal file
24
vendor/git.giftfish.de/ston1th/godrop/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
Copyright (C) 2016 Marius Schellenberger
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* The names of the authors and/or contributors may not be used to
|
||||
endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL ston1th BE LIABLE FOR ANY
|
||||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
3
vendor/git.giftfish.de/ston1th/godrop/README.md
vendored
Normal file
3
vendor/git.giftfish.de/ston1th/godrop/README.md
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# godrop - drop privileges
|
||||
|
||||
Godrop is a simple library to drop privileges on linux maschines.
|
||||
160
vendor/git.giftfish.de/ston1th/godrop/godrop.go
vendored
Normal file
160
vendor/git.giftfish.de/ston1th/godrop/godrop.go
vendored
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
package godrop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"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")
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
User string
|
||||
Group string
|
||||
Chroot string
|
||||
}
|
||||
|
||||
// Drop will spawn a new process, hand over the listening socket file descriptor and terminate itself after
|
||||
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")
|
||||
}
|
||||
|
||||
// MultiDrop will spawn a new process, hand over the all listening sockets and terminate itself after
|
||||
func MultiDrop(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:
|
||||
cmd := exec.Command(os.Args[0], os.Args[1:]...)
|
||||
ln, err := f()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range ln {
|
||||
l, ok := v.(*net.TCPListener)
|
||||
if !ok {
|
||||
return errors.New("godrop: interface conversion failed")
|
||||
}
|
||||
f, err := l.File()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd.ExtraFiles = append(cmd.ExtraFiles, 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")
|
||||
}
|
||||
|
||||
// GetListener returns the listener socket of file descriptor 3
|
||||
func GetListener() (net.Listener, error) {
|
||||
return net.FileListener(os.NewFile(3, "[socket]"))
|
||||
}
|
||||
|
||||
// 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]"))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue