Debian Codegraph @

mstypes/sid.go

raw symbols SCIP
1 package mstypes
2
3 import (
4 "encoding/binary"
5 "encoding/hex"
6 "fmt"
7
8 "gopkg.in/jcmturner/rpc.v0/ndr"
9 )
10
11 // RPCSID implements https://msdn.microsoft.com/en-us/library/cc230364.aspx
12 type RPCSID struct {
13 Revision uint8
14 SubAuthorityCount uint8
15 IdentifierAuthority RPCSIDIdentifierAuthority
16 SubAuthority []uint32
17 }
18
19 // RPCSIDIdentifierAuthority implements https://msdn.microsoft.com/en-us/library/cc230372.aspx
20 type RPCSIDIdentifierAuthority struct {
21 Value []byte // 6 bytes
22 }
23
24 // ReadRPCSID reads a RPC_SID from the bytes slice.
25 func ReadRPCSID(b *[]byte, p *int, e *binary.ByteOrder) (RPCSID, error) {
26 size := int(ndr.ReadUint32(b, p, e)) // This is part of the NDR encoding rather than the data type.
27 r := ndr.ReadUint8(b, p)
28 if r != uint8(1) {
29 return RPCSID{}, ndr.Malformed{EText: fmt.Sprintf("SID revision value read as %d when it must be 1", r)}
30 }
31 c := ndr.ReadUint8(b, p)
32 a := ReadRPCSIDIdentifierAuthority(b, p, e)
33 s := make([]uint32, c, c)
34 if size != len(s) {
35 return RPCSID{}, ndr.Malformed{EText: fmt.Sprintf("Number of elements (%d) within SID in the byte stream does not equal the SubAuthorityCount (%d)", size, c)}
36 }
37 for i := 0; i < len(s); i++ {
38 s[i] = ndr.ReadUint32(b, p, e)
39 }
40 return RPCSID{
41 Revision: r,
42 SubAuthorityCount: c,
43 IdentifierAuthority: a,
44 SubAuthority: s,
45 }, nil
46 }
47
48 // ReadRPCSIDIdentifierAuthority reads a RPC_SIDIdentifierAuthority from the bytes slice.
49 func ReadRPCSIDIdentifierAuthority(b *[]byte, p *int, e *binary.ByteOrder) RPCSIDIdentifierAuthority {
50 return RPCSIDIdentifierAuthority{
51 Value: ndr.ReadBytes(b, p, 6, e),
52 }
53 }
54
55 // ToString returns the string representation of the RPC_SID.
56 func (s *RPCSID) ToString() string {
57 var str string
58 b := append(make([]byte, 2, 2), s.IdentifierAuthority.Value...)
59 // For a strange reason this is read big endian: https://msdn.microsoft.com/en-us/library/dd302645.aspx
60 i := binary.BigEndian.Uint64(b)
61 if i >= 4294967296 {
62 str = fmt.Sprintf("S-1-0x%s", hex.EncodeToString(s.IdentifierAuthority.Value))
63 } else {
64 str = fmt.Sprintf("S-1-%d", i)
65 }
66 for _, sub := range s.SubAuthority {
67 str = fmt.Sprintf("%s-%d", str, sub)
68 }
69 return str
70 }