-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapabilities.go
54 lines (46 loc) · 1.38 KB
/
capabilities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2023 Joshua Rich <joshua.rich@gmail.com>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package gokbd
import (
"os/user"
"strconv"
"github.com/rs/zerolog/log"
"kernel.org/pub/linux/libs/security/libcap/cap"
)
// taken from https://git.kernel.org/pub/scm/libs/libcap/libcap.git/tree/goapps/setid/setid.go#n32
func setIDsWithCaps(setgid, setuid int, gids []int) {
if err := cap.SetGroups(setgid, gids...); err != nil {
log.Fatal().Err(err).Msgf("Unable to set gid to %d.", setgid)
}
if err := cap.SetUID(setuid); err != nil {
log.Fatal().Err(err).Msgf("Unable to set uid to %d.", setuid)
}
}
func getInputGroupGid() int {
inputGroup, err := user.LookupGroup("input")
if err != nil {
log.Fatal().Err(err).Msg("No input group defined.")
}
inputGroupGid, err := strconv.Atoi(inputGroup.Gid)
if err != nil {
log.Fatal().Err(err).Msg("Could not convert gid string to int.")
}
return inputGroupGid
}
func getUserIds() (int, int) {
userDetails, err := user.Current()
if err != nil {
log.Fatal().Err(err).Msg("Could not retrieve user details.")
}
uid, err := strconv.Atoi(userDetails.Uid)
if err != nil {
log.Fatal().Err(err).Msg("Could not convert uid string to int.")
}
gid, err := strconv.Atoi(userDetails.Gid)
if err != nil {
log.Fatal().Err(err).Msg("Could not convert gid string to int.")
}
return uid, gid
}