This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
uac.go
361 lines (296 loc) · 9.68 KB
/
uac.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package accesscontrol
import (
"database/sql"
"github.com/pkg/errors"
"github.com/seknox/trasa/server/api/devices"
"github.com/sirupsen/logrus"
"net"
"strconv"
"strings"
"time"
"github.com/jinzhu/now"
"github.com/phuslu/iploc"
"github.com/seknox/trasa/server/api/system"
"github.com/seknox/trasa/server/consts"
"github.com/seknox/trasa/server/models"
"github.com/seknox/trasa/server/utils"
)
/*
cases:
- allow login only on monday
- allow login only between 5pm-6pm
- allow login between 10am-6pm on weekdays but allow login only between 5pm-6pm on saturday
*/
// TrasaUAC or User access control is handled when assigning user to application.
// User access control handles users timing for access, users workstation for access
// and user IP for access. Access can be assigned for limited time per day, limited day per week
// or limited access with expiry date.
// type TrasaUAC struct {
// Days []string
// time []int
// IP string
// Workstation string
// ExpiryTimer string
// }
func CheckTrasaUAC(timezone, clientip string, policy *models.Policy) (bool, consts.FailedReason) {
allow := false
reason := consts.REASON_TIME_POLICY_FAILED
for _, dayTimePolicy := range policy.DayAndTime {
allow, reason = dayTimeExpiryChecker(timezone, dayTimePolicy)
logrus.Tracef("day time check:%v , failed reason:%v", allow, reason)
}
// we check expiry
current := time.Now()
delta, err := time.Parse("2006-01-02", policy.Expiry)
if err != nil {
logrus.Error(err)
return false, consts.REASON_UNKNOWN
}
if current.Before(delta) != true {
logrus.Tracef("policy expired")
return false, consts.REASON_POLICY_EXPIRED
}
//check ip policy
allowedIps := strings.Split(policy.IPSource, ",")
//logrus.Debug(allowedIps, clientip)
chk, err := utils.NewChecker(allowedIps)
if err != nil {
logrus.Error(err)
}
err = chk.IsAuthorized(clientip)
if err != nil {
return false, consts.REASON_IP_POLICY_FAILED
}
//logrus.Debug(policy.AllowedCountries)
//Country check
allowedCountries := strings.Split(policy.AllowedCountries, ",")
//logrus.Debug(allowedCountries)
//logrus.Debug(geoip.Country(net.ParseIP(clientip))," country")
//private ip and invalid ip has "ZZ" country
//We alllow invalid or private ips
if !utils.ArrayContainsString(allowedCountries, "ZZ") {
allowedCountries = append(allowedCountries, "ZZ")
}
//len(allowedCountries)==1 means blank allowed countries
if !utils.ArrayContainsString(allowedCountries, string(iploc.Country(net.ParseIP(clientip)))) && policy.AllowedCountries != "" {
return false, consts.REASON_COUNTRY_POLICY_FAILED
}
return allow, reason
}
func dayTimeExpiryChecker(timezone string, perm models.DayAndTimePolicy) (bool, consts.FailedReason) {
nep, err := time.LoadLocation(timezone)
if err != nil {
logrus.Errorf("load location: %v", err)
nep, err = time.LoadLocation("UTC")
if err != nil {
logrus.Errorf("load location: %v", err)
return false, consts.REASON_UNKNOWN
}
}
local := time.Now().In(nep)
//current := time.Now()
hour := local.Hour()
minute := local.Minute()
// we check day
daycheck := dayChecker(perm.Days)
if daycheck != true {
logrus.Trace("daycheck failed") // should return here
return false, consts.REASON_TIME_POLICY_FAILED
}
// we check time
fromstr := strings.Split(perm.FromTime, ":")
tostr := strings.Split(perm.ToTime, ":")
from := make([]int, len(fromstr))
for i := range from {
from[i], _ = strconv.Atoi(fromstr[i])
}
to := make([]int, len(tostr))
for i := range to {
to[i], _ = strconv.Atoi(tostr[i])
}
check := timeChecker(hour, minute, from, to)
if check != true {
logrus.Trace("failed at FromTO")
return false, consts.REASON_TIME_POLICY_FAILED
}
return true, ""
}
func dayChecker(days []string) bool {
date := now.BeginningOfHour()
day := date.Weekday()
for _, v := range days {
if strings.Compare(v, day.String()) == 0 {
return true
}
}
return false
}
func timeChecker(hour, minute int, from, to []int) bool {
if hour > from[0] && hour < to[0] {
return true
}
if (hour >= from[0] && minute >= from[1]) && (hour <= to[0] && minute <= to[1]) {
return true
}
return false
}
// TrasaUAC validates policy for user access
func TrasaUAC(params *models.ConnectionParams, policy *models.Policy, adHocSwitch bool) (bool, consts.FailedReason) {
ok := false
reason := consts.REASON_UNKNOWN
//we check users policy
ok, reason = CheckTrasaUAC(params.Timezone, params.UserIP, policy)
if ok == true {
return true, "user authorised by uac check"
}
return ok, reason
}
//CheckDevicePolicy checks if device hygiene of user is according to device policy
func CheckDevicePolicy(policy models.DevicePolicy, accessDeviceID, tfaDeviceID, orgID string) (consts.FailedReason, bool, error) {
//Skipping device policy for now
//return "", true, nil
sett, err := system.Store.GetGlobalSetting(orgID, consts.GLOBAL_DEVICE_HYGIENE_CHECK)
if err != nil {
logrus.Error(err)
return consts.REASON_UNKNOWN, false, errors.Errorf("could not get global settings: %v", err)
}
if !sett.Status {
return "", true, nil
}
accessDevice, err := devices.Store.GetFromID(accessDeviceID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return consts.REASON_DEVICE_NOT_ENROLLED, false, errors.Errorf("could not get access device detail of %s: %v", accessDeviceID, err)
} else {
return "Something is wrong", false, errors.Errorf("could not get access device detail: %v", err)
}
}
accessDeviceHygiene := accessDevice.DeviceHygiene
tfaDevice, err := devices.Store.GetFromID(tfaDeviceID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return (consts.REASON_DEVICE_NOT_ENROLLED), false, err
} else {
return consts.REASON_UNKNOWN, false, errors.Errorf("could not get access device detail: %v", err)
}
}
tfaDeviceHygiene := tfaDevice.DeviceHygiene
//
//pol,_:=json.Marshal(policy)
//logrus.Trace("policy ",string(pol))
//
//pol,_=json.Marshal(accessDeviceHygiene)
//logrus.Trace("acc ",string(pol))
//
//pol,_=json.Marshal(tfaDeviceHygiene)
//logrus.Trace("tfa ",string(pol))
//
if policy.BlockUntrustedDevices {
if !accessDevice.Trusted {
logrus.Trace("!Trusted")
return "Device policy failed: device not trusted by admin", false, nil
}
if !tfaDevice.Trusted {
logrus.Trace("!Trusted")
return "Device policy failed: device not trusted by admin", false, nil
}
}
if policy.BlockRemoteLoginEnabled {
if accessDeviceHygiene.LoginSecurity.RemoteLoginEnabled {
logrus.Trace("RemoteLoginEnabled")
return "Device policy failed: remote login enabled", false, nil
}
}
if policy.BlockEncryptionNotSet {
if !accessDeviceHygiene.EndpointSecurity.DeviceEncryptionEnabled { //|| !tfaDeviceHygiene.EndpointSecurity.DeviceEncryptionEnabled {
logrus.Trace("!DeviceEncryptionEnabled")
return "Device policy failed: device encryption not enabled", false, nil
}
}
if policy.BlockAntivirusDisabled {
if !accessDeviceHygiene.EndpointSecurity.EpsConfigured {
logrus.Trace("!EpsConfigured")
return "Device policy failed: endpoint security/antivirus not enabled", false, nil
}
}
if policy.BlockFirewallDisabled {
logrus.Trace("BlockFirewallDisabled")
if !accessDeviceHygiene.EndpointSecurity.FirewallEnabled {
logrus.Trace("!FirewallEnabled")
return "Device policy failed: firewall not enabled", false, nil
}
}
if policy.BlockCriticalAutoUpdateDisabled {
logrus.Trace("BlockCriticalAutoUpdateDisabled")
if !accessDeviceHygiene.DeviceOS.AutoUpdate {
logrus.Trace("!AutoUpdate")
return "Device policy failed: autoUpdate disabled", false, nil
}
}
if policy.BlockIdleScreenLockDisabled {
if !accessDeviceHygiene.LoginSecurity.IdleDeviceScreenLock {
return "Device policy failed: screen lock disabled", false, nil
}
}
if policy.BlockAutologinEnabled {
if accessDeviceHygiene.LoginSecurity.AutologinEnabled || tfaDeviceHygiene.LoginSecurity.AutologinEnabled {
logrus.Trace("!AutologinEnabled")
return "Device policy failed: autologin enabled", false, nil
}
}
if policy.BlockEmulated {
if tfaDeviceHygiene.DeviceOS.IsEmulator {
logrus.Trace("IsEmulator")
return "Device policy failed: tfa device is a emulator", false, nil
}
}
if policy.BlockJailBroken {
if tfaDeviceHygiene.DeviceOS.JailBroken {
logrus.Trace("JailBroken")
return "Device policy failed: tfa device is jail broken", false, nil
}
}
return "", true, nil
if policy.BlockDebuggingEnabled {
if accessDeviceHygiene.DeviceOS.DebugModeEnabled || tfaDeviceHygiene.DeviceOS.DebugModeEnabled {
logrus.Trace("DebugModeEnabled")
return "Device policy failed: debug mode enabled", false, nil
}
}
if policy.BlockOpenWifiConn {
if accessDeviceHygiene.NetworkInfo.OpenWifiConn || tfaDeviceHygiene.NetworkInfo.OpenWifiConn {
logrus.Trace("OpenWifiConn")
return "Device policy failed: connected to open WiFi", false, nil
}
}
if policy.BlockTfaNotConfigured {
if !accessDeviceHygiene.LoginSecurity.TfaConfigured {
logrus.Trace("!TfaConfigured")
return "Device policy failed: second factor authentication not configured in device", false, nil
}
}
return "", true, nil
}
func checkVersion(policy string, ver string) (bool, error) {
splittedPolicy := strings.Split(policy, ".")
splittedVersion := strings.Split(ver, ".")
for i, v := range splittedPolicy {
polInt, err := strconv.Atoi(v)
if err != nil {
return false, errors.New("invalid policy: not a number")
}
if len(splittedVersion) < i+1 {
return false, errors.Errorf("length mismatch: %d %d", len(splittedVersion), i+1)
}
verInt, err := strconv.Atoi(splittedVersion[i])
if err != nil {
return false, errors.New("invalid version: not a number")
}
if polInt > verInt {
return false, nil
} else if polInt < verInt {
return true, nil
}
}
return true, nil
}