aboutsummaryrefslogtreecommitdiff
path: root/battering/services/opensnitch.scm
blob: 300f1fe5a48da49b9282c4dece405225e3520d7e (plain) (blame)
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
(define-module (battering services opensnitch)
  #:use-module (guix gexp)
  #:use-module (guix records)
  #:use-module (guix modules)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu services configuration)
  #:use-module (gnu packages guile)
  #:use-module (battering packages opensnitch)
  #:use-module (srfi srfi-171)

  #:export (opensnitch-configuration
            opensnitch-rule
            opensnitch-service-type))

;; Turns lisp-case into dot.case
(define (dot-field-name field-name)
  (string-join
   (string-tokenize (format #f "~a" field-name)
                    char-set:letter)
   "."))

(define methods-list '(ftrace audit ebpf proc))
(define actions-list '(allow deny reject))
(define durations-list '(once until-restart forever))
(define (method? val)
  (memq val methods-list))
(define (action? val)
  (memq val actions-list))
(define (duration? val)
  (memq val durations-list))

(define (port? val)
  (or (integer? val)
      ((list-of integer?) val)))

(define-maybe string)
(define-maybe gexp)
(define-maybe file-like)
(define-maybe port)
(define-maybe path)

(define-configuration/no-serialization opensnitch-rule
  (name
   (string)
   "Name of the rule")
  (description
   (string "")
   "Description of the rule")
  (action
   (action 'allow)
   "Action to execute on the rule.")
  (nolog?
   (boolean #f)
   "Should connections with this rule be ignored from logging?")
  (priority-rule?
   (boolean #f)
   "Should this rule have priority over others?")
  (regexp-fields
   (list '("dest.port"))
   "Opensnich fields with regular expressions.")
  (case-sensitive?
   (boolean #f)
   "Are fields case-sensitive")
  (file-path
   maybe-file-like
   "Command-line of the process")
  (command-line
   maybe-gexp
   "Path of the process")
  (dest-host
   maybe-string
   "Destination host")
  (dest-ip
   maybe-string
   "Destination IP")
  (dest-network
   maybe-string
   "Destination network")
  (dest-port
   maybe-port
   "Destination port")
  (dest-iface
   maybe-string
   "Outside interface")
  (protocol
   maybe-string
   "Transport Layer protocol")
  (user
   maybe-string
   "User")
  (hosts-file
   maybe-string
   "List of domains")
  (hosts-regex?
   maybe-string
   "List of domains is a list of regexes?"))

(define (serialize-opensnitch-rule rule)
  (match-record rule
      <opensnitch-rule>
      (name
       description
       action
       nolog? priority-rule? case-sensitive?
       regexp-fields
       file-path command-line
       dest-host dest-ip dest-network dest-port dest-iface
       protocol user hosts-file hosts-regex?)

    (define (parse-port port)
      (if (maybe-value-set? port)
          (if (integer? port)
              (format #f "~a" port)
              (format #f "^(~a)$"
                      (string-join
                       (map number->string port)
                       "|")))
          port))

    (with-extensions (list guile-json-4)
      (with-imported-modules (source-module-closure
                              '((json builder)))
        #~(begin
            (use-modules (json builder)
                         (srfi srfi-1))

            ;; Uuuuurgh ugly hack.
            (define %unset-marker% '%unset-marker%)

            (define (serialize-single-operand operand)
              (let* ((name (car operand))
                     (data (cdr operand))
                     (type (if (find
                                (lambda (s) (string= name s))
                                '#$regexp-fields)
                               "regexp"
                               "simple")))
                (if (not (eq? data %unset-marker%))
                    `((operand . ,name)
                      (data . ,data)
                      (type . ,type)
                      (sensitive . #$case-sensitive?))
                    #f)))

            (define serialized-fields
              (filter-map
               serialize-single-operand
               `(("process.path" . ,#$file-path)
                 ("process.command" . ,#$command-line)
                 ("dest.host" . ,#$dest-host)
                 ("dest.ip" . ,#$dest-ip)
                 ("dest.network" . ,#$dest-network)
                 ("dest.port" . ,#$(parse-port dest-port))
                 ("iface.out" . ,#$dest-iface)
                 ("protocol" . ,#$protocol)
                 ("user.id" .
                  #$(if (eq? user '%unset-marker%)
                        user
                        (number->string (passwd:uid (getpw user)))))
                 (#$(if hosts-regex?
                        "lists.domain_regexp"
                        "lists.domain") . ,#$hosts-file))))

            (begin
              (scm->json
               `((name . #$name)
                 (description . #$description)
                 (action . #$action)
                 ;; These rules will be read-only in the store and always present at
                 ;; boot, so "on restart" would be functionally the same as "always", and
                 ;; "once" would be nonsensical.  Plus, Opensnich wouldn't be able to
                 ;; delete them.
                 (duration . "always")
                 (precedence . #$priority-rule?)
                 (enabled . #t)
                 (nolog . #$nolog?)
                 (operator
                  ,@(if (= (length serialized-fields) 1)
                        (car serialized-fields)
                        `((sensitive . #$case-sensitive?)
                          (list . #(,@serialized-fields))
                          (operand . "list")
                          (data . "")
                          (type . "list"))))))))))))

;; Turns lisp-case into PascalCase
(define (pascal-field-name field-name)
  (apply
   string-append
   (map string-capitalize (string-tokenize
                           (format #f "~a" field-name)
                           char-set:letter))))

(define (serialize-field field-name val)
  `(,(pascal-field-name field-name) . ,val))

(define serialize-boolean serialize-field)

(define (serialize-integer field-name val)
  (let ((serialized (serialize-field field-name val)))
    (if (eq? field-name 'workers)
        `("Stats" ,serialized)
        serialized)))

(define (serialize-string field-name val)
  (let ((serialized (serialize-field field-name val)))
    (if (eq? field-name 'address)
        `("Server" ,serialized
          ;; LogFile should always be stdout because logging will be managed
          ;; by Shepherd
          ("LogFile" . "/dev/stdout"))
        serialized)))

(define serialize-method serialize-field)
(define serialize-action serialize-field)
(define (serialize-duration field-name val)
  (serialize-field
   field-name
   (string-join (string-split (format #f "~a" val) #\-))))

(define (serialize-opensnitch-configuration config fields)
  "Return a G-expression that contains a json representation the values
corresponding to the FIELDS of CONFIG."
  (with-extensions (list guile-json-4)
    (with-imported-modules (source-module-closure '((json builder)))
      #~(begin
          (use-modules (json builder))
          (scm->json
           '#$(list-transduce (base-transducer config) rcons fields)
           #:pretty #t)))))

(define list-of-opensnitch-rules?
  (list-of opensnitch-rule?))

(define-configuration opensnitch-configuration
  (opensnitchd
   (file-like opensnitchd)
   "Opensnitchd package to use.")
  (rules
   (list-of-opensnitch-rules '())
   "List of rules to add in /etc/opensnitchd/rules."
   (serializer empty-serializer))
  (address
   (string "unix:///tmp/osui.sock")
   "Opensnitch UI socket path.")
  (workers
   (integer 6)
   "Number of workers started by the daemon.")
  (log-utc?
   (boolean #t)
   "Use UTC timestamps in event logging?")
  (log-micro?
   (boolean #f)
   "Include microseconds in event timestamps?")
  (log-level
   (integer 2)
   "Daemon log level.")
  (process-monitor-method
   (method 'proc)
   "Process monitor method to use.")
  (default-action
    (action 'allow)
    "Default action to take when the UI isn't connected.")
  (default-duration
    (duration 'once)
    "Duration of the rules created automatically.")
  (intercept-unknown?
   (boolean #f)
   "Intercept unknown network connections?"))

(define (opensnitch-activation config)
  "Create the opensnitch rules and configuration according to CONFIG."
  (match-record config <opensnitch-configuration> (opensnitchd rules)
    (with-extensions (list guile-json-4)
      (with-imported-modules '((guix build utils))
        #~(begin
            (use-modules (guix build utils)
                         (ice-9 ftw))
            (when (not (file-exists? "/etc/opensnitchd"))
              (mkdir-p "/etc/opensnitchd/rules/")
              (copy-file #$(file-append opensnitchd "/etc/system-fw.json")
                         "/etc/opensnitchd/system-fw.json"))

            ;; Not the most elegant of solutions. I'd be happy to find
            ;; something more Guix-y, or even Guix-ish.
            (nftw "/etc/opensnitchd/rules"
                  (lambda (filename s flag b l)
                    (when (eq? flag 'symlink)
                      (delete-file filename))
                    #t)
                  'physical)

            #$@(map
                (lambda (rule)
                  (let ((rule-name
                         (string-append (opensnitch-rule-name rule) ".json")))
                    #~(symlink
                       #$(computed-file
                          rule-name
                          #~(with-output-to-file #$output
                              (lambda _
                                #$(serialize-opensnitch-rule rule))))
                       #$(string-append "/etc/opensnitchd/rules/" rule-name))))
                rules))))))

(define (opensnitch-shepherd-service config)
  "Return a <shepherd-service> for opensnitch with CONFIG."
  (let ((default-config
          (computed-file
           "opensnitchd-config.json"
           #~(with-output-to-file #$output
               (lambda _
                 #$(serialize-opensnitch-configuration
                    config
                    opensnitch-configuration-fields))))))
    (list (shepherd-service
           (documentation "Opensnitchd daemon.")
           (requirement '(syslogd loopback))
           (provision '(opensnitch))
           (start
            #~(make-forkexec-constructor
               (list #$(file-append opensnitchd "/bin/opensnitchd")
                     ;; Live reload breaks with custom rules.
                     "-no-live-reload" "-config-file" #$default-config)))
           (stop #~(make-kill-destructor))))))

(define opensnitch-service-type
  (service-type
   (name 'opensnitch)
   (description "Run the Opensnitch application firewall daemon.")
   (extensions
    (list
     (service-extension shepherd-root-service-type
                        opensnitch-shepherd-service)
     (service-extension activation-service-type
                        opensnitch-activation)
     (service-extension profile-service-type
                        (lambda (config)
                          `(,(opensnitch-configuration-opensnitchd config))))))
   (compose identity)
   (default-value (opensnitch-configuration))))