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
|
(set! (current-effective-user-id) cid))
;(print 0 "cid " cid " eid:" eid)
(proc)
(if (not (eq? eid cid))
(set! (current-effective-user-id) eid))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Check user types
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;check if a user is an admin
(define (is-admin username)
(let* ((admin #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT users.is_admin FROM users where users.username = '" username "'")))))
(if (not (null? data-row))
(let ((col (car data-row)))
(if (equal? col "yes")
(set! admin #t)))))))
admin))
;;check if user has specifc role for a area
(define (is-user role username area)
(let* ((has-access #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT permissions.access_type, permissions.expiration FROM users , areas, permissions where permissions.user_id = users.id and permissions.area_id = areas.id and users.username = '" username "' and areas.code = '" area "'")))))
(if (not (null? data-row))
(let* ((access-type (car data-row))
(exdate (cadr data-row)))
(if (not (null? exdate))
(begin
(let ((valid (is-access-valid exdate)))
;(print valid)
(if (and (equal? access-type role)
(equal? valid #t))
(set! has-access #t))))
(print "Access expired")))))))
has-access))
;check if area exists
(define (area-exists area)
(let* ((area-defined #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT id FROM areas where areas.code = '" area "'")))))
(if (not (null? data-row))
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
(set! (current-effective-user-id) cid))
;(print 0 "cid " cid " eid:" eid)
(proc)
(if (not (eq? eid cid))
(set! (current-effective-user-id) eid))))
(define (run-cmd cmd arg-list)
; (print (current-effective-user-id))
;(handle-exceptions
; exn
; (print 0 "ERROR: failed to run script " cmd " with params " arg-list " " (exn assert))
(let ((pid (process-run cmd arg-list)))
(process-wait pid))
)
;)
(define (regster-log inl usr-id area-id cmd)
(sauth-common:shell-do-as-adm
(lambda ()
(sauthorize:db-do (lambda (db)
(sauthorize:db-qry db (conc "INSERT INTO actions (cmd,user_id,area_id,action_type ) VALUES ('sretrieve " inl "'," usr-id "," area-id ", 'cat' )")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Check user types
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;check if a user is an admin
(define (is-admin username)
(let* ((admin #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT users.is_admin FROM users where users.username = '" username "'")))))
(if (not (null? data-row))
(let ((col (car data-row)))
(if (equal? col "yes")
(set! admin #t)))))))
admin))
;;check if a user is an read-admin
(define (is-read-admin username)
(let* ((admin #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT users.is_admin FROM users where users.username = '" username "'")))))
(if (not (null? data-row))
(let ((col (car data-row)))
(if (equal? col "read-admin")
(set! admin #t)))))))
admin))
;;check if user has specifc role for a area
(define (is-user role username area)
(let* ((has-access #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT permissions.access_type, permissions.expiration FROM users , areas, permissions where permissions.user_id = users.id and permissions.area_id = areas.id and users.username = '" username "' and areas.code = '" area "'")))))
(if (not (null? data-row))
(begin
(let* ((access-type (car data-row))
(exdate (cadr data-row)))
(if (not (null? exdate))
(begin
(let ((valid (is-access-valid exdate)))
;(print valid)
(if (and (equal? access-type role)
(equal? valid #t))
(set! has-access #t))))
(print "Access expired"))))))))
;(print has-access)
has-access))
(define (is-access-valid exp-str)
(let* ((ret-val #f )
(date-parts (string-split exp-str "/"))
(yr (string->number (car date-parts)))
(month (string->number(car (cdr date-parts))))
(day (string->number(caddr date-parts)))
(exp-date (make-date 0 0 0 0 day month yr )))
;(print exp-date)
;(print (current-date))
(if (> (date-compare exp-date (current-date)) 0)
(set! ret-val #t))
;(print ret-val)
ret-val))
;check if area exists
(define (area-exists area)
(let* ((area-defined #f))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT id FROM areas where areas.code = '" area "'")))))
(if (not (null? data-row))
|
110
111
112
113
114
115
116
117
|
(define (get-perm userid areaid)
(let* ((user-defined '()))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT id FROM permissions where user_id = " userid " and area_id = " areaid)))))
(set! user-defined data-row))))
user-defined))
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
(define (get-perm userid areaid)
(let* ((user-defined '()))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT id FROM permissions where user_id = " userid " and area_id = " areaid)))))
(set! user-defined data-row))))
user-defined))
(define (get-restrictions base-path usr)
(let* ((user-defined '()))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT restriction FROM areas, users, permissions where areas.id = permissions.area_id and users.id = permissions.user_id and users.username = '" usr "' and areas.basepath = '" base-path "'")))))
;(print data-row)
(set! user-defined data-row))))
; (print user-defined)
(if (null? user-defined)
""
(car user-defined))))
(define (get-obj-by-path path)
(let* ((obj '()))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT code,exe_name, id, basepath FROM areas where areas.basepath = '" path "'")))))
(set! obj data-row))))
obj))
(define (get-obj-by-code code )
(let* ((obj '()))
(sauthorize:db-do (lambda (db)
(let* ((data-row (query fetch (sql db (conc "SELECT code, exe_name, id, basepath FROM areas where areas.code = '" code "'")))))
(set! obj data-row))))
;(print obj)
obj))
;; function to validate the users input for target path and resolve the path
;; TODO: Check for restriction in subpath
(define (sauth-common:resolve-path new current allowed-sheets)
(let* ((target-path (append current (string-split new "/")))
(target-path-string (string-join target-path "/"))
(normal-path (normalize-pathname target-path-string))
(normal-list (string-split normal-path "/"))
(ret '()))
(if (string-contains normal-path "..")
(begin
(print "ERROR: Path " new " resolved outside target area ")
#f)
(if(equal? normal-path ".")
ret
(if (not (member (car normal-list) allowed-sheets))
(begin
(print "ERROR: Permision denied to " new )
#f)
normal-list)))))
(define (sauth-common:get-target-path base-path-list ext-path top-areas base-path)
(let* ((resolved-path (sauth-common:resolve-path ext-path base-path-list top-areas ))
(usr (current-user-name) ) )
(if (not (equal? resolved-path #f))
(if (null? resolved-path)
#f
(let* ((sheet (car resolved-path))
(restricted-areas (get-restrictions base-path usr))
(restrictions (conc ".*" (string-join (string-split restricted-areas ",") ".*|.*") ".*"))
(target-path (if (null? (cdr resolved-path))
base-path
(conc base-path "/" (string-join (cdr resolved-path) "/")))))
; (print restricted-areas)
(if (and (not (equal? restricted-areas "" ))
(string-match (regexp restrictions) target-path))
(begin
(print "Access denied to " (string-join resolved-path "/"))
;(exit 1)
#f)
target-path)))
#f)))
(define (sauth-common:shell-ls-cmd base-path-list ext-path top-areas base-path tail-cmd-list)
(if (and (null? base-path-list) (equal? ext-path "") )
(print (string-intersperse top-areas " "))
(let* ((resolved-path (sauth-common:resolve-path ext-path base-path-list top-areas )))
;(print resolved-path)
(if (not (equal? resolved-path #f))
(if (null? resolved-path)
(print (string-intersperse top-areas " "))
(let* ((target-path (sauth-common:get-target-path base-path-list ext-path top-areas base-path)))
(print target-path)
(if (not (equal? target-path #f))
(begin
(cond
((null? tail-cmd-list)
(run (pipe
(ls "-lrt" ,target-path))))
((not (equal? (car tail-cmd-list) "|"))
(print "ls cmd cannot accept " (string-join tail-cmd-list) " as an argument!!"))
(else
(run (pipe
(ls "-lrt" ,target-path)
(begin (system (string-join (cdr tail-cmd-list))))))
)
)))
))))))
|