Skip to content

Commit

Permalink
Implement drop privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Oct 20, 2013
1 parent aadd5da commit a78cc9f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions config_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
port: 1234
# user: hooktftp
hooks:

- name: Shell hook
Expand Down
43 changes: 43 additions & 0 deletions drop_privileges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"os/user"
"strconv"
"syscall"
)

func DropPrivileges(username string) error {
userInfo, err := user.Lookup(username)
if err != nil {
return err
}

uid, err := strconv.Atoi(userInfo.Uid)
if err != nil {
return err
}

gid, err := strconv.Atoi(userInfo.Gid)
if err != nil {
return err
}

// TODO: should set secondary groups too
err = syscall.Setgroups([]int{gid})
if err != nil {
return err
}

err = syscall.Setgid(gid)
if err != nil {
return err
}

err = syscall.Setuid(uid)
if err != nil {
return err
}

return nil
}

18 changes: 18 additions & 0 deletions hooktftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"io/ioutil"
"net"
"os"
"os/user"
"syscall"
"time"
)

Expand Down Expand Up @@ -154,6 +156,22 @@ func main() {

fmt.Println("Listening on", conf.Port)

if conf.User != "" {
err := DropPrivileges(conf.User)
if err != nil {
fmt.Printf("Failed to drop privileges to '%s' error: %v", conf.User, err)
return
}
currentUser, _ := user.Current()
fmt.Println("Dropped privileges to", currentUser)
}

if conf.User == "" && syscall.Getuid() == 0 {
fmt.Println("!!!!!!!!!")
fmt.Println("WARNING: Running as root and 'user' is not set in", CONFIG_PATH)
fmt.Println("!!!!!!!!!")
}

for {
res, err := server.Accept()
if err != nil {
Expand Down

0 comments on commit a78cc9f

Please sign in to comment.