1
1
package gosddl
2
2
3
3
import (
4
+ "bufio"
5
+ "flag"
4
6
"fmt"
5
7
"log"
8
+ "os"
6
9
"strings"
10
+
11
+ "encoding/json"
7
12
)
8
13
9
- type entryACLInternal struct {
10
- AccountSid string `json:"accountSID"`
11
- AceType string `json:"aceType"`
12
- AceFlags []string `json:"aceFlags"`
13
- Rights []string `json:"rights"`
14
- ObjectGUID string `json:"objectGUID"`
15
- InheritObjectGUID string `json:"inheritObjectGUID"`
14
+ // ACLProcessor main struct with methods
15
+ type ACLProcessor struct {
16
+ Rights Permissons
17
+ File string
18
+ }
19
+
20
+ type entryACL struct {
21
+ AccountSid string `json:"accountSID,omitempty"`
22
+ AceType string `json:"aceType,omitempty"`
23
+ AceFlags []string `json:"aceFlags,omitempty"`
24
+ Rights []string `json:"rights,omitempty"`
25
+ ObjectGUID string `json:"objectGUID,omitempty"`
26
+ InheritObjectGUID string `json:"inheritObjectGUID,omitempty"`
16
27
}
17
28
18
29
type Permissons struct {
19
- Owner string `json:"owner"`
20
- Primary string `json:"primary"`
21
- Dacl []entryACLInternal `json:"dacl"`
22
- DaclInher []string `json:"daclInheritFlags"`
23
- Sacl []entryACLInternal `json:"sacl"`
24
- SaclInger []string `json:"saclInheritFlags"`
30
+ Owner string `json:"owner,omitempty "`
31
+ Primary string `json:"primary,omitempty "`
32
+ Dacl []entryACL `json:"dacl,omitempty "`
33
+ DaclInher []string `json:"daclInheritFlags,omitempty "`
34
+ Sacl []entryACL `json:"sacl,omitempty "`
35
+ SaclInger []string `json:"saclInheritFlags,omitempty "`
25
36
}
26
37
27
- // replace identification account: sid/wellkhownsid/usersid
28
- func sidReplace (str string ) string {
38
+ // checkSIDsFile check file of SIDs where data saved in SID,User
39
+ func checkSIDsFile (filePath string , sid string ) string {
40
+ file , err := os .Open (filePath )
41
+ if err != nil {
42
+ log .Fatal (err )
43
+ }
44
+ defer file .Close ()
45
+
46
+ scanner := bufio .NewScanner (file )
47
+ for scanner .Scan () {
48
+ if strings .Split (scanner .Text (), "," )[0 ] == sid {
49
+ return strings .Split (scanner .Text (), "," )[1 ]
50
+ }
51
+ }
52
+ if err := scanner .Err (); err != nil {
53
+ log .Fatal (err )
54
+ }
55
+ return sid
56
+ }
57
+
58
+ // sidReplace replace identification account: sid/wellkhownsid/usersid
59
+ func (app * ACLProcessor ) sidReplace (str string ) string {
29
60
if len (str ) > 2 {
61
+
30
62
if x , ok := sddlWellKnownSidsRep [str ]; ok {
31
63
return x
32
- } else {
33
- return str
64
+ } else if app . File != "" {
65
+ return checkSIDsFile ( app . File , str )
34
66
}
35
- return replacer ( sddlWellKnownSidsRep , str )[ 0 ]
67
+ return str
36
68
}
37
- return replacer (sddlSidsRep , str )[0 ]
69
+ return app . replacer (sddlSidsRep , str )[0 ]
38
70
}
39
71
40
- // chunk string with 2 letters, add to array and then resolve
41
- func replacer (maps map [string ]string , str string ) []string {
72
+ // replacer chunk string with 2 letters, add to array and then resolve
73
+ func ( app * ACLProcessor ) replacer (maps map [string ]string , str string ) []string {
42
74
var temp , result []string
43
75
if len (str ) > 2 {
44
76
for j := 0 ; j < len (str )- 1 ; j = j + 2 {
@@ -57,81 +89,83 @@ func replacer(maps map[string]string, str string) []string {
57
89
return result
58
90
}
59
91
60
- // Base format ACL: (ace_type;ace_flags;rights;object_guid;inherit_object_guid;account_sid)
61
- // Convert values from string to struct with replace strings
62
- func splitBodyACL (str string ) entryACLInternal {
63
- temp := strings .Split (str , ";" )
64
- return entryACLInternal {
65
- AceType : replacer (sddlAceType , temp [0 ])[0 ],
66
- AceFlags : replacer (sddlAceFlags , temp [1 ]),
67
- Rights : replacer (sddlRights , temp [2 ]),
68
- ObjectGUID : temp [3 ],
69
- InheritObjectGUID : temp [4 ],
70
- AccountSid : sidReplace (temp [5 ]),
92
+ /* splitBodyACL Convert values from string to struct with replace strings
93
+ Base format Rights: (ace_type;ace_flags;rights;object_guid;inherit_object_guid;account_sid)
94
+ */
95
+ func (app * ACLProcessor ) splitBodyACL (str string ) entryACL {
96
+ splitACL := strings .Split (str , ";" )
97
+ return entryACL {
98
+ AceType : app .replacer (sddlAceType , splitACL [0 ])[0 ],
99
+ AceFlags : app .replacer (sddlAceFlags , splitACL [1 ]),
100
+ Rights : app .replacer (sddlRights , splitACL [2 ]),
101
+ ObjectGUID : splitACL [3 ],
102
+ InheritObjectGUID : splitACL [4 ],
103
+ AccountSid : app .sidReplace (splitACL [5 ]),
71
104
}
72
105
}
73
106
74
- func splitBody (body string ) []entryACLInternal {
75
- var entryACLInternalArr []entryACLInternal
107
+ func ( app * ACLProcessor ) splitBody (body string ) []entryACL {
108
+ var entryACLInternalArr []entryACL
76
109
for _ , y := range strings .Split (body , "(" ) {
77
110
if y != "" {
78
111
ace := strings .TrimSuffix (y , ")" )
79
- entryACLInternalArr = append (entryACLInternalArr , splitBodyACL (ace ))
112
+ entryACLInternalArr = append (entryACLInternalArr , app . splitBodyACL (ace ))
80
113
}
81
114
}
82
115
return entryACLInternalArr
83
116
}
84
117
85
- func (p * Permissons ) parseBody (body string ) ([]string , []entryACLInternal ) {
118
+ func (app * ACLProcessor ) parseBody (body string ) ([]string , []entryACL ) {
86
119
var inheritFlagArr []string
87
- var entryACLInternalArr []entryACLInternal
120
+ var entryACLInternalArr []entryACL
88
121
if strings .Index (body , "(" ) != 0 {
89
122
inheritFlag := body [0 :strings .Index (body , "(" )]
90
123
ace := body [strings .Index (body , "(" ):]
91
124
if len (inheritFlag ) > 2 {
92
125
for j := 0 ; j < len (inheritFlag )- 1 ; j = j + 2 {
93
- inheritFlagArr = append (inheritFlagArr , replacer (sddlInheritanceFlags , fmt .Sprintf ("%s%s" , string (inheritFlag [j ]), string (inheritFlag [j + 1 ])))[0 ])
126
+ inheritFlagArr = append (inheritFlagArr , app . replacer (sddlInheritanceFlags , fmt .Sprintf ("%s%s" , string (inheritFlag [j ]), string (inheritFlag [j + 1 ])))[0 ])
94
127
}
95
128
}
96
- entryACLInternalArr = splitBody (ace )
129
+ entryACLInternalArr = app . splitBody (ace )
97
130
} else {
98
- entryACLInternalArr = splitBody (body )
131
+ entryACLInternalArr = app . splitBody (body )
99
132
}
100
133
return inheritFlagArr , entryACLInternalArr
101
134
}
102
135
103
- func (p * Permissons ) parseSDDL (sddrArr []string ) {
136
+ func (app * ACLProcessor ) parseSDDL (sddrArr []string ) {
104
137
for _ , y := range sddrArr {
105
138
sddlSplit := strings .Split (y , ":" )
106
139
letter := sddlSplit [0 ]
107
140
body := sddlSplit [1 ]
108
141
switch letter {
109
142
case "O" :
110
- p . Owner = sidReplace (body )
143
+ app . Rights . Owner = app . sidReplace (body )
111
144
case "G" :
112
- p . Primary = sidReplace (body )
145
+ app . Rights . Primary = app . sidReplace (body )
113
146
case "D" :
114
- p . DaclInher , p . Dacl = p .parseBody (body )
147
+ app . Rights . DaclInher , app . Rights . Dacl = app .parseBody (body )
115
148
case "S" :
116
- p . SaclInger , p . Sacl = p .parseBody (body )
149
+ app . Rights . SaclInger , app . Rights . Sacl = app .parseBody (body )
117
150
default :
118
151
log .Fatal ("Unresolved group" )
119
152
}
120
153
}
121
154
122
155
}
123
156
124
- // create slice objects from str to array of strings
125
- func (p * Permissons ) sliceSDDL (indecs []int , str string ) {
157
+ // slice SDDL create slice objects from str to array of strings
158
+ func (app * ACLProcessor ) sliceSDDL (indecs []int , str string ) {
126
159
var sddlArr []string
127
160
for i := 0 ; i < len (indecs )- 1 ; i ++ {
128
161
sl := str [indecs [i ]:indecs [i + 1 ]]
129
162
sddlArr = append (sddlArr , sl )
130
163
}
131
- p .parseSDDL (sddlArr )
164
+ app .parseSDDL (sddlArr )
132
165
}
133
166
134
- func (p * Permissons ) FindGroupIndex (str string ) {
167
+ // FindGroupIndex used for find index of group Owner, Primary, DACL, SACL
168
+ func (app * ACLProcessor ) findGroupIndex (str string ) {
135
169
groups := []string {"O:" , "G:" , "D:" , "S:" }
136
170
var result []int
137
171
for _ , i := range groups {
@@ -140,5 +174,24 @@ func (p *Permissons) FindGroupIndex(str string) {
140
174
}
141
175
}
142
176
result = append (result , len (str ))
143
- p .sliceSDDL (result , str )
177
+ app .sliceSDDL (result , str )
178
+ }
179
+
180
+ // Processor main function in gosddl package
181
+ func Processor (api bool , port string , file string ) {
182
+ var app ACLProcessor
183
+ app .File = file
184
+ if api {
185
+ fmt .Println ("API Interface started on port" , port )
186
+ app .httpHandler (port )
187
+ } else if flag .Args () != nil {
188
+ app .findGroupIndex (flag .Args ()[0 ])
189
+ body , err := json .Marshal (app .Rights )
190
+ if err != nil {
191
+ log .Fatal (err )
192
+ }
193
+ fmt .Println (string (body ))
194
+ } else {
195
+ log .Fatal ("You should give me SDDL string or use API mode" )
196
+ }
144
197
}
0 commit comments