forked from square/keywhiz-fs
/
ownership.go
70 lines (60 loc) · 1.89 KB
/
ownership.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2015 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keywhizfs
import (
"log"
"os"
"os/user"
"strconv"
)
// Ownership indicates the default ownership of filesystem entries.
type Ownership struct {
Uid uint32
Gid uint32
}
// NewOwnership initializes default file ownership struct.
func NewOwnership(username, groupname string) Ownership {
return Ownership{
Uid: lookupUid(username),
Gid: lookupGid(groupname),
}
}
// lookupUid resolves a username to a numeric id. Current euid is returned on failure.
func lookupUid(username string) uint32 {
u, err := user.Lookup(username)
if err != nil {
log.Printf("Error resolving uid for %v: %v\n", username, err)
return uint32(os.Geteuid())
}
uid, err := strconv.ParseUint(u.Uid, 10 /* base */, 32 /* bits */)
if err != nil {
log.Printf("Error resolving uid for %v: %v\n", username, err)
return uint32(os.Geteuid())
}
return uint32(uid)
}
// lookupGid resolves a groupname to a numeric id. Current egid is returned on failure.
func lookupGid(groupname string) uint32 {
g, err := user.Lookup(groupname)
if err != nil {
log.Printf("Error resolving gid for %v: %v\n", groupname, err)
return uint32(os.Getegid())
}
gid, err := strconv.ParseUint(g.Gid, 10 /* base */, 32 /* bits */)
if err != nil {
log.Printf("Error resolving gid for %v: %v\n", groupname, err)
return uint32(os.Getegid())
}
return uint32(gid)
}