-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe-io.scm
117 lines (100 loc) · 4.12 KB
/
safe-io.scm
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
;;;; safe-io.scm -- restricted user I/O for use with safe-eval
;;
;; Copyright (c) 2009 Alex Shinn. All rights reserved.
;; BSD-style license: http://synthcode.com/license.txt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require-library posix)
(module safe-io
(current-safe-user-id current-safe-group-id
file-user-readable? file-user-writeable? touch-file
file-readable? file-writeable?
open-input-file open-output-file
call-with-input-file call-with-output-file
with-input-from-file with-output-to-file
;; load delete-file rename-file
)
(import (except (rename scheme
(open-input-file %open-input-file)
(open-output-file %open-output-file))
with-input-from-file with-output-to-file
call-with-input-file call-with-output-file)
chicken extras posix files ports)
(define current-safe-user-id (make-parameter (current-user-id)))
(define current-safe-group-id (make-parameter (current-group-id)))
(define (file-user-readable? file user-id group-id)
(condition-case
(let* ((stats (file-stat file))
(mode (vector-ref stats 1)))
(or (and (eq? user-id (vector-ref stats 3))
(not (zero? (bitwise-and mode perm/irusr))))
(and (eq? group-id (vector-ref stats 4))
(not (zero? (bitwise-and mode perm/irgrp))))
(not (zero? (bitwise-and mode perm/iroth)))))
(exn () #f)))
(define (file-user-writeable? file user-id group-id)
(condition-case
(let* ((stats (file-stat file))
(mode (vector-ref stats 1)))
(or (and (eq? user-id (vector-ref stats 3))
(not (zero? (bitwise-and mode perm/iwusr))))
(and (eq? group-id (vector-ref stats 4))
(not (zero? (bitwise-and mode perm/iwgrp))))
(not (zero? (bitwise-and mode perm/iwoth)))))
(exn ()
(and (not (file-exists? file))
(not (equal? (pathname-directory file) file))
(condition-case
(let* ((stats (file-stat (or (pathname-directory file) ".")))
(mode (vector-ref stats 1)))
(or (and (eq? user-id (vector-ref stats 3))
(not (zero? (bitwise-and mode perm/iwusr))))
(and (eq? group-id (vector-ref stats 4))
(not (zero? (bitwise-and mode perm/iwgrp))))
(not (zero? (bitwise-and mode perm/iwoth)))))
(exn () #f))
'new))))
(define (touch-file file uid gid . o)
(let ((out (%open-output-file file)))
(close-output-port out))
(if (memv (current-user-id) (list 0 uid))
(change-file-owner file uid gid))
(if (pair? o)
(change-file-mode file (car o))))
(define (file-readable? file)
(file-user-readable? file (current-safe-user-id) (current-safe-group-id)))
(define (file-writeable? file)
(file-user-writeable? file (current-safe-user-id) (current-safe-group-id)))
(define open-input-file
(lambda (file)
(if (file-readable? file)
(%open-input-file file)
(error "file not readable: " file))))
(define open-output-file
(lambda (file)
(let ((ok? (file-writeable? file)))
(if (eq? ok? 'new) ; create an empty file owned by the user
(touch-file file (current-safe-user-id) (current-safe-group-id)))
(if ok?
(%open-output-file file)
(error "file not writeable: " file)))))
(define (call-with-input-file file proc)
(let* ((p (open-input-file file))
(res (proc p)))
(close-input-port p)
res))
(define (call-with-output-file file proc)
(let* ((p (open-output-file file))
(res (proc p)))
(close-output-port p)
res))
(define (with-input-from-file file thunk)
(let* ((port (open-input-file file))
(res (with-input-from-port port thunk)))
(close-input-port port)
res))
(define (with-output-to-file file thunk)
(let* ((port (open-output-file file))
(res (with-output-to-port port thunk)))
(close-output-port port)
res))
)