forked from jmckaskill/goldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sid.go
95 lines (74 loc) · 1.67 KB
/
sid.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
package ldap
import (
"encoding/binary"
"strconv"
"strings"
)
type SID []byte
const sidRevision = 1
func ParseSID(s string) (SID, error) {
if !strings.HasPrefix(s, "S-1-") {
return nil, ErrInvalidSID
}
parts := strings.Split(s[len("S-1-"):], "-")
sid := make([]byte, 4+4*len(parts))
if len(parts) < 1 {
return nil, ErrInvalidSID
}
v, err := strconv.ParseUint(parts[0], 10, 48)
if err != nil {
return nil, ErrInvalidSID
}
binary.BigEndian.PutUint64(sid, v)
sid[0] = sidRevision
sid[1] = byte(len(parts) - 1)
for i, p := range parts[1:] {
v, err := strconv.ParseUint(p, 10, 32)
if err != nil {
return nil, ErrInvalidSID
}
binary.LittleEndian.PutUint32(sid[8+i*4:], uint32(v))
}
return sid, nil
}
func (s SID) String() string {
if len(s) < 8 || s[0] != sidRevision || len(s) != (int(s[1])*4)+8 {
return ""
}
ret := []byte("S-1-")
ret = strconv.AppendUint(ret, binary.BigEndian.Uint64(s[:8])&0xFFFFFFFFFFFF, 10)
for i := 0; i < int(s[1]); i++ {
ret = append(ret, "-"...)
ret = strconv.AppendUint(ret, uint64(binary.LittleEndian.Uint32(s[8+i*4:])), 10)
}
return string(ret)
}
func (s SID) Domain() (SID, error) {
if len(s) < 24 ||
s[0] != sidRevision ||
binary.BigEndian.Uint64(s)&0xFFFFFFFFFFFF != 5 ||
binary.LittleEndian.Uint32(s[8:]) != 21 {
return nil, ErrInvalidSID
}
if len(s) == 24 && s[1] == 4 {
return s, nil
}
if len(s) != 28 || s[1] != 5 {
return nil, ErrInvalidSID
}
ret := make([]byte, 24)
copy(ret, s)
ret[1] = 4 // change the length
return ret, nil
}
func (s SID) Equal(r SID) bool {
if len(s) != len(r) {
return false
}
for i, a := range s {
if a != r[i] {
return false
}
}
return true
}