-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
140 lines (123 loc) · 3.81 KB
/
group.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
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
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package group
// #include <stdlib.h>
// #include <grp.h>
import "C"
import (
"fmt"
"unsafe"
)
// Group wraps up `group` struct used in kernel
type Group struct {
Name string
Passwd string // Password is encrypted if password-shadowing is on
Gid uint32
Members *[]string // member list
}
var char *C.char
// stringArrayC2Go converts string arrray in C to string slice in Golang
func stringArrayC2Go(strArrC **C.char) *[]string {
strArr := make([]string, 5)
for offset := uintptr(0); uintptr(*(*C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(strArrC)) + offset))) != uintptr(0); offset += unsafe.Sizeof(uintptr(0)) {
str := C.GoString(*(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(strArrC)) + offset)))
strArr = append(strArr, str)
}
return &strArr
}
// stringArrayC2Go_2 converts string array in C to string slice in Golang
// It is a revised version of @stringArrayC2Go
func stringArrayC2Go_2(strArrC **C.char) *[]string {
strArr := make([]string, 0)
arrIndex := (*[1 << 10]*C.char)(unsafe.Pointer(strArrC))
for _, p := range arrIndex {
if p == nil {
break
}
strArr = append(strArr, C.GoString(p))
}
return &strArr
}
// groupC2Go converts `group` struct from C to golang native struct
func groupC2Go(groupC *C.struct_group) *Group {
members := stringArrayC2Go_2(groupC.gr_mem)
return &Group{
Name: C.GoString(groupC.gr_name),
Passwd: C.GoString(groupC.gr_passwd),
Gid: uint32(groupC.gr_gid),
Members: members,
}
}
type GroupNotFoundError struct {
Name string
Gid uint32
}
func (err *GroupNotFoundError) Error() string {
if len(err.Name) > 0 {
return fmt.Sprintf("Group with name `%s` not found!", err.Name)
} else {
return fmt.Sprintf("Group with gid `%d` not found!", err.Gid)
}
}
// GetGroupByName wraps up `getgrnam` system call.
// It retrieves group records from group file based on group name.
func GetGroupByName(name string) (*Group, error) {
nameC := C.CString(name)
defer C.free(unsafe.Pointer(nameC))
groupC, err := C.getgrnam(nameC)
if groupC == nil {
if err == nil {
return nil, &GroupNotFoundError{Name: name}
} else {
return nil, err
}
} else {
return groupC2Go(groupC), nil
}
}
// GetGroupByGid wraps up `getgrgid` system call.
// It retrieves group records from group file based on gid.
func GetGroupByGid(gid uint32) (*Group, error) {
gidC := C.__gid_t(gid)
groupC, err := C.getgrgid(gidC)
if groupC == nil {
if err == nil {
return nil, &GroupNotFoundError{Gid: gid}
} else {
return nil, err
}
} else {
return groupC2Go(groupC), nil
}
}
// GetGroupEntry wraps up `getgrent` system call.
// It performs sequential scans of the records in the group file.
func GetGroupEntry() []*Group {
groups := make([]*Group, 0)
// Restart scanning from the begging of the group file.
C.setgrent()
for groupC, err := C.getgrent(); groupC != nil && err == nil; groupC, err = C.getgrent() {
group := groupC2Go(groupC)
groups = append(groups, group)
}
// Call endgrent() is necessary so that any subsequent getgrent() call will
// reopen the group file and start from the beginning.
C.endgrent()
return groups
}