| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | (define-module (gnu services opensnitch)
|
| 20 | #:use-module (gnu packages networking)
|
| 21 | #:use-module (gnu services)
|
| 22 | #:use-module (gnu services base)
|
| 23 | #:use-module (gnu services configuration)
|
| 24 | #:use-module (gnu services shepherd)
|
| 25 | #:use-module (guix gexp)
|
| 26 | #:use-module (guix packages)
|
| 27 | #:use-module (guix records)
|
| 28 | #:use-module (json)
|
| 29 | #:export (opensnitch-configuration
|
| 30 | opensnitch-configuration?
|
| 31 | opensnitch-service-type))
|
| 32 |
|
| 33 | (define-configuration/no-serialization opensnitch-configuration
|
| 34 | (opensnitch
|
| 35 | (package opensnitch-daemon)
|
| 36 | "The @code{opensnitch-daemon} package to use.")
|
| 37 |
|
| 38 | |
| 39 | (server-address
|
| 40 | (string "unix:///tmp/osui.sock")
|
| 41 | "Address for the UI to connect to the daemon.")
|
| 42 | (server-log-file
|
| 43 | (string "/var/log/opensnitchd.log")
|
| 44 | "Path to the daemon log file.")
|
| 45 |
|
| 46 | |
| 47 | (authentication-type
|
| 48 | (string "simple")
|
| 49 | "Authentication type for UI-daemon communication.")
|
| 50 | (tls-ca-cert
|
| 51 | (string "")
|
| 52 | "Path to TLS CA certificate.")
|
| 53 | (tls-server-cert
|
| 54 | (string "")
|
| 55 | "Path to TLS server certificate.")
|
| 56 | (tls-client-cert
|
| 57 | (string "")
|
| 58 | "Path to TLS client certificate.")
|
| 59 | (tls-client-key
|
| 60 | (string "")
|
| 61 | "Path to TLS client key.")
|
| 62 | (tls-skip-verify?
|
| 63 | (boolean #f)
|
| 64 | "Whether to skip TLS verification.")
|
| 65 | (tls-client-auth-type
|
| 66 | (string "no-client-cert")
|
| 67 | "TLS client authentication type.")
|
| 68 |
|
| 69 | |
| 70 | (default-action
|
| 71 | (string "allow")
|
| 72 | "Default action for connections: @code{\"allow\"} or @code{\"deny\"}.")
|
| 73 | (default-duration
|
| 74 | (string "once")
|
| 75 | "Default duration for rules: @code{\"once\"}, @code{\"until-restart\"},
|
| 76 | @code{\"always\"}, etc.")
|
| 77 | (intercept-unknown?
|
| 78 | (boolean #f)
|
| 79 | "Whether to intercept connections from unknown processes.")
|
| 80 |
|
| 81 | |
| 82 | (proc-monitor-method
|
| 83 | (string "ebpf")
|
| 84 | "Method for monitoring processes: @code{\"ebpf\"}, @code{\"proc\"}, or
|
| 85 | @code{\"audit\"}.")
|
| 86 |
|
| 87 | |
| 88 | (log-level
|
| 89 | (integer 2)
|
| 90 | "Log level: 0=silent, 1=error, 2=warning, 3=important, 4=debug.")
|
| 91 | (log-utc?
|
| 92 | (boolean #t)
|
| 93 | "Whether to log timestamps in UTC.")
|
| 94 | (log-micro?
|
| 95 | (boolean #f)
|
| 96 | "Whether to include microseconds in log timestamps.")
|
| 97 |
|
| 98 | |
| 99 | (firewall
|
| 100 | (string "nftables")
|
| 101 | "Firewall backend: @code{\"nftables\"} or @code{\"iptables\"}.")
|
| 102 | (fw-config-path
|
| 103 | (string "/etc/opensnitchd/system-fw.json")
|
| 104 | "Path to the system firewall configuration file.")
|
| 105 | (fw-monitor-interval
|
| 106 | (string "15s")
|
| 107 | "Interval for monitoring firewall rules.")
|
| 108 | (fw-queue-bypass?
|
| 109 | (boolean #t)
|
| 110 | "Whether to bypass the queue when the daemon is not running.")
|
| 111 |
|
| 112 | |
| 113 | (rules-path
|
| 114 | (string "/etc/opensnitchd/rules/")
|
| 115 | "Directory where firewall rules are stored.")
|
| 116 | (rules-enable-checksums?
|
| 117 | (boolean #f)
|
| 118 | "Whether to enable checksums for rules.")
|
| 119 |
|
| 120 | |
| 121 | (ebpf-events-workers
|
| 122 | (integer 8)
|
| 123 | "Number of eBPF event worker threads.")
|
| 124 | (ebpf-queue-events-size
|
| 125 | (integer 0)
|
| 126 | "Size of the eBPF events queue (0 = default).")
|
| 127 |
|
| 128 | |
| 129 | (stats-max-events
|
| 130 | (integer 250)
|
| 131 | "Maximum number of events to keep in statistics.")
|
| 132 | (stats-max-stats
|
| 133 | (integer 25)
|
| 134 | "Maximum number of statistics entries.")
|
| 135 | (stats-workers
|
| 136 | (integer 6)
|
| 137 | "Number of statistics worker threads.")
|
| 138 |
|
| 139 | |
| 140 | (internal-gc-percent
|
| 141 | (integer 100)
|
| 142 | "Go garbage collector percentage.")
|
| 143 | (internal-flush-conns-on-start?
|
| 144 | (boolean #t)
|
| 145 | "Whether to flush existing connections on daemon start."))
|
| 146 |
|
| 147 | (define (opensnitch-configuration->json config)
|
| 148 | "Convert CONFIG to a JSON string for the OpenSnitch daemon."
|
| 149 | (match-record config <opensnitch-configuration>
|
| 150 | (server-address server-log-file
|
| 151 | authentication-type tls-ca-cert tls-server-cert tls-client-cert
|
| 152 | tls-client-key tls-skip-verify? tls-client-auth-type
|
| 153 | default-action default-duration intercept-unknown?
|
| 154 | proc-monitor-method log-level log-utc? log-micro?
|
| 155 | firewall fw-config-path fw-monitor-interval fw-queue-bypass?
|
| 156 | rules-path rules-enable-checksums?
|
| 157 | ebpf-events-workers ebpf-queue-events-size
|
| 158 | stats-max-events stats-max-stats stats-workers
|
| 159 | internal-gc-percent internal-flush-conns-on-start?)
|
| 160 | (scm->json-string
|
| 161 | `((Server . ((Address . ,server-address)
|
| 162 | (Authentication . ((Type . ,authentication-type)
|
| 163 | (TLSOptions . ((CACert . ,tls-ca-cert)
|
| 164 | (ServerCert . ,tls-server-cert)
|
| 165 | (ClientCert . ,tls-client-cert)
|
| 166 | (ClientKey . ,tls-client-key)
|
| 167 | (SkipVerify . ,tls-skip-verify?)
|
| 168 | (ClientAuthType . ,tls-client-auth-type)))))
|
| 169 | (LogFile . ,server-log-file)))
|
| 170 | (DefaultAction . ,default-action)
|
| 171 | (DefaultDuration . ,default-duration)
|
| 172 | (InterceptUnknown . ,intercept-unknown?)
|
| 173 | (ProcMonitorMethod . ,proc-monitor-method)
|
| 174 | (LogLevel . ,log-level)
|
| 175 | (LogUTC . ,log-utc?)
|
| 176 | (LogMicro . ,log-micro?)
|
| 177 | (Firewall . ,firewall)
|
| 178 | (FwOptions . ((ConfigPath . ,fw-config-path)
|
| 179 | (MonitorInterval . ,fw-monitor-interval)
|
| 180 | (QueueBypass . ,fw-queue-bypass?)))
|
| 181 | (Rules . ((Path . ,rules-path)
|
| 182 | (EnableChecksums . ,rules-enable-checksums?)))
|
| 183 | (Ebpf . ((EventsWorkers . ,ebpf-events-workers)
|
| 184 | (QueueEventsSize . ,ebpf-queue-events-size)))
|
| 185 | (Stats . ((MaxEvents . ,stats-max-events)
|
| 186 | (MaxStats . ,stats-max-stats)
|
| 187 | (Workers . ,stats-workers)))
|
| 188 | (Internal . ((GCPercent . ,internal-gc-percent)
|
| 189 | (FlushConnsOnStart . ,internal-flush-conns-on-start?))))
|
| 190 | #:pretty #t)))
|
| 191 |
|
| 192 | (define (opensnitch-config-file config)
|
| 193 | "Return a file-like object for the OpenSnitch configuration."
|
| 194 | (plain-file "opensnitch-config.json"
|
| 195 | (opensnitch-configuration->json config)))
|
| 196 |
|
| 197 | (define (opensnitch-activation config)
|
| 198 | "Return the activation gexp for CONFIG."
|
| 199 | (match-record config <opensnitch-configuration>
|
| 200 | (rules-path)
|
| 201 | (with-imported-modules '((guix build utils))
|
| 202 | #~(begin
|
| 203 | (use-modules (guix build utils))
|
| 204 | (mkdir-p #$rules-path)))))
|
| 205 |
|
| 206 | (define (opensnitch-shepherd-service config)
|
| 207 | (match-record config <opensnitch-configuration>
|
| 208 | (opensnitch server-log-file)
|
| 209 | (list (shepherd-service
|
| 210 | (documentation "Run the OpenSnitch application firewall daemon.")
|
| 211 | (provision '(opensnitch))
|
| 212 | (requirement '(user-processes networking))
|
| 213 | (start #~(make-forkexec-constructor
|
| 214 | (list #$(file-append opensnitch "/sbin/opensnitchd")
|
| 215 | "-config-file" #$(opensnitch-config-file config))
|
| 216 | #:log-file #$server-log-file))
|
| 217 | (stop #~(make-kill-destructor))))))
|
| 218 |
|
| 219 | (define opensnitch-service-type
|
| 220 | (service-type
|
| 221 | (name 'opensnitch)
|
| 222 | (description "Run the OpenSnitch application firewall daemon.")
|
| 223 | (extensions
|
| 224 | (list (service-extension shepherd-root-service-type
|
| 225 | opensnitch-shepherd-service)
|
| 226 | (service-extension activation-service-type
|
| 227 | opensnitch-activation)
|
| 228 | (service-extension profile-service-type
|
| 229 | (compose list opensnitch-configuration-opensnitch))))
|
| 230 | (default-value (opensnitch-configuration))))
|