-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (137 loc) · 2.95 KB
/
main.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"flag"
"fmt"
"sync"
"github.com/johnmikee/ctcc/returner"
"github.com/johnmikee/ctcc/tcc"
"github.com/johnmikee/ctcc/version"
)
type Cfg struct {
FileName string `json:"file_name"`
ToCSV bool `json:"to_csv"`
ToJSON bool `json:"to_json"`
ToSal bool `json:"to_sal"`
Version bool `json:"version"`
}
func systemDefaultPermissions() []*tcc.TCCEntry {
resp, err := tcc.SystemQuery()
if err != nil {
fmt.Printf("error querying system: %s", err)
return nil
}
entries := []*tcc.TCCEntry{}
for _, r := range resp {
row := tcc.ProcessRow("System", r)
entries = append(entries, row)
}
return entries
}
func userPermissionOverrides() []*tcc.TCCEntry {
localUsers := tcc.ListUsers()
entries := []*tcc.TCCEntry{}
entryChan := make(chan *tcc.TCCEntry)
var wg sync.WaitGroup
for _, user := range localUsers {
wg.Add(1)
go func(u tcc.Users) {
defer wg.Done()
resp, err := tcc.UserQuery(u.DB)
if err != nil {
fmt.Printf("error querying user %s: %s", u.Name, err)
return
}
for _, r := range resp {
row := tcc.ProcessRow(u.Name, r)
entryChan <- row
}
}(user)
}
go func() {
wg.Wait()
close(entryChan)
}()
for entry := range entryChan {
entries = append(entries, entry)
}
return entries
}
func mdmProfileOverrides() []*tcc.MDMEntry {
overrides, err := tcc.CheckMDMOverrides()
if err != nil {
return nil
}
entries := []*tcc.MDMEntry{}
for item, vals := range overrides {
entry := tcc.ProcessMDMOverrides(item, vals)
entries = append(entries, entry)
}
return entries
}
func main() {
o := Cfg{
ToCSV: true,
ToJSON: false,
ToSal: false,
Version: false,
}
flag.StringVar(&o.FileName, "file", "ctcc.csv", "File name to output to")
flag.BoolVar(&o.ToCSV, "csv", false, "Output to CSV")
flag.BoolVar(&o.ToJSON, "json", false, "Output to JSON")
flag.BoolVar(&o.ToSal, "sal", false, "Output for sal to pick it up")
flag.BoolVar(&o.Version, "version", false, "Print version and exit")
flag.Parse()
if o.Version {
version.Print()
return
}
var m []*tcc.MDMEntry
var s []*tcc.TCCEntry
var u []*tcc.TCCEntry
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
m = mdmProfileOverrides()
}()
wg.Add(1)
go func() {
defer wg.Done()
s = systemDefaultPermissions()
}()
wg.Add(1)
go func() {
defer wg.Done()
u = userPermissionOverrides()
}()
wg.Wait()
// create the report after all functions have finished
report := &returner.TCCReport{
MDM: m,
System: s,
User: u,
}
if o.ToCSV {
err := returner.CSV(o.FileName, report)
if err != nil {
fmt.Printf("error generating csv: %s", err)
return
}
}
if o.ToJSON {
report, err := returner.Json(report)
if err != nil {
fmt.Printf("error marshalling report: %s", err)
return
}
fmt.Println(report)
}
if o.ToSal {
report, err := returner.Sal(report)
if err != nil {
fmt.Printf("error generating report: %s", err)
return
}
fmt.Println(report)
}
}