forked from jiacfan/keyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_linux.go
More file actions
185 lines (164 loc) · 3.6 KB
/
sys_linux.go
File metadata and controls
185 lines (164 loc) · 3.6 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package keyctl
import (
"log"
"syscall"
"unsafe"
)
type keyctlCommand int
type keyId int32
const (
keySpecThreadKeyring keyId = -1
keySpecProcessKeyring keyId = -2
keySpecSessionKeyring keyId = -3
keySpecUserKeyring keyId = -4
keySpecUserSessionKeyring keyId = -5
keySpecGroupKeyring keyId = -6
keySpecReqKeyAuthKey keyId = -7
)
const (
keyctlGetKeyringId keyctlCommand = iota
keyctlJoinSessionKeyring
keyctlUpdate
keyctlRevoke
keyctlChown
keyctlSetPerm
keyctlDescribe
keyctlClear
keyctlLink
keyctlUnlink
keyctlSearch
keyctlRead
keyctlInstantiate
keyctlNegate
keyctlSetReqKeyKeyring
keyctlSetTimeout
keyctlAssumeAuthority
)
var debugSyscalls bool
func (id keyId) Id() int32 {
return int32(id)
}
func (cmd keyctlCommand) String() string {
switch cmd {
case keyctlGetKeyringId:
return "keyctlGetKeyringId"
case keyctlJoinSessionKeyring:
return "keyctlJoinSessionKeyring"
case keyctlUpdate:
return "keyctlUpdate"
case keyctlRevoke:
return "keyctlRevoke"
case keyctlChown:
return "keyctlChown"
case keyctlSetPerm:
return "keyctlSetPerm"
case keyctlDescribe:
return "keyctlDescribe"
case keyctlClear:
return "keyctlClear"
case keyctlLink:
return "keyctlLink"
case keyctlUnlink:
return "keyctlUnlink"
case keyctlSearch:
return "keyctlSearch"
case keyctlRead:
return "keyctlRead"
case keyctlInstantiate:
return "keyctlInstantiate"
case keyctlNegate:
return "keyctlNegate"
case keyctlSetReqKeyKeyring:
return "keyctlSetReqKeyKeyring"
case keyctlSetTimeout:
return "keyctlSetTimeout"
case keyctlAssumeAuthority:
return "keyctlAssumeAuthority"
}
panic("bad arg")
}
func keyctl(cmd keyctlCommand, args ...uintptr) (r1 int32, r2 int32, err error) {
a := make([]uintptr, 6)
l := len(args)
if l > 5 {
l = 5
}
a[0] = uintptr(cmd)
for idx, v := range args[:l] {
a[idx+1] = v
}
if debugSyscalls {
log.Printf("%v: %v %v\n", syscall_keyctl, cmd, a[1:])
}
v1, v2, errno := syscall.Syscall6(syscall_keyctl, a[0], a[1], a[2], a[3], a[4], a[5])
if errno != 0 {
err = errno
return
}
r1 = int32(v1)
r2 = int32(v2)
return
}
func add_key(keyType, keyDesc string, payload []byte, id int32) (int32, error) {
var (
err error
errno syscall.Errno
b1, b2 *byte
r1 uintptr
pptr unsafe.Pointer
)
if b1, err = syscall.BytePtrFromString(keyType); err != nil {
return 0, err
}
if b2, err = syscall.BytePtrFromString(keyDesc); err != nil {
return 0, err
}
if len(payload) > 0 {
pptr = unsafe.Pointer(&payload[0])
}
r1, _, errno = syscall.Syscall6(syscall_add_key,
uintptr(unsafe.Pointer(b1)),
uintptr(unsafe.Pointer(b2)),
uintptr(pptr),
uintptr(len(payload)),
uintptr(id),
0)
if errno != 0 {
err = errno
return 0, err
}
return int32(r1), nil
}
func newKeyring(id keyId) (*keyring, error) {
r1, _, err := keyctl(keyctlGetKeyringId, uintptr(id), uintptr(1))
if err != nil {
return nil, err
}
if id < 0 {
r1 = int32(id)
}
return &keyring{id: keyId(r1)}, nil
}
func searchKeyring(id keyId, name, keyType string) (keyId, error) {
var (
r1 int32
b1, b2 *byte
err error
)
if b1, err = syscall.BytePtrFromString(keyType); err != nil {
return 0, err
}
if b2, err = syscall.BytePtrFromString(name); err != nil {
return 0, err
}
r1, _, err = keyctl(keyctlSearch, uintptr(id), uintptr(unsafe.Pointer(b1)), uintptr(unsafe.Pointer(b2)))
return keyId(r1), err
}
func updateKey(id keyId, payload []byte) error {
size := len(payload)
if size == 0 {
payload = make([]byte, 1)
}
_, _, err := keyctl(keyctlUpdate, uintptr(id), uintptr(unsafe.Pointer(&payload[0])), uintptr(size))
return err
}