1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2015 Mark H Weaver <mhw@netris.org>
4 ;;; Copyright © 2016, 2018 Efraim Flashner <efraim@flashner.co.il>
5 ;;; Copyright © 2016 John Darrington <jmd@gnu.org>
6 ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
7 ;;; Copyright © 2017 Thomas Danckaert <post@thomasdanckaert.be>
8 ;;; Copyright © 2017, 2018 Marius Bakke <mbakke@fastmail.com>
9 ;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
10 ;;; Copyright © 2018 Chris Marusich <cmmarusich@gmail.com>
11 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
12 ;;; Copyright © 2019 Florian Pelz <pelzflorian@pelzflorian.de>
13 ;;; Copyright © 2019 Maxim Cournoyer <maxim.cournoyer@gmail.com>
14 ;;; Copyright © 2019 Sou Bunnbu <iyzsong@member.fsf.org>
16 ;;; This file is part of GNU Guix.
18 ;;; GNU Guix is free software; you can redistribute it and/or modify it
19 ;;; under the terms of the GNU General Public License as published by
20 ;;; the Free Software Foundation; either version 3 of the License, or (at
21 ;;; your option) any later version.
23 ;;; GNU Guix is distributed in the hope that it will be useful, but
24 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 ;;; GNU General Public License for more details.
28 ;;; You should have received a copy of the GNU General Public License
29 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
31 (define-module (gnu services networking)
32 #:use-module (gnu services)
33 #:use-module (gnu services base)
34 #:use-module (gnu services shepherd)
35 #:use-module (gnu services dbus)
36 #:use-module (gnu system shadow)
37 #:use-module (gnu system pam)
38 #:use-module (gnu packages admin)
39 #:use-module (gnu packages base)
40 #:use-module (gnu packages bash)
41 #:use-module (gnu packages connman)
42 #:use-module (gnu packages freedesktop)
43 #:use-module (gnu packages linux)
44 #:use-module (gnu packages tor)
45 #:use-module (gnu packages usb-modeswitch)
46 #:use-module (gnu packages messaging)
47 #:use-module (gnu packages networking)
48 #:use-module (gnu packages ntp)
49 #:use-module (gnu packages wicd)
50 #:use-module (gnu packages gnome)
51 #:use-module (guix gexp)
52 #:use-module (guix records)
53 #:use-module (guix modules)
54 #:use-module (guix packages)
55 #:use-module (guix deprecation)
56 #:use-module (rnrs enums)
57 #:use-module (srfi srfi-1)
58 #:use-module (srfi srfi-9)
59 #:use-module (srfi srfi-26)
60 #:use-module (ice-9 match)
61 #:re-export (static-networking-service
62 static-networking-service-type)
63 #:export (%facebook-host-aliases
65 dhcp-client-service-type
70 dhcpd-configuration-package
71 dhcpd-configuration-config-file
72 dhcpd-configuration-version
73 dhcpd-configuration-run-directory
74 dhcpd-configuration-lease-file
75 dhcpd-configuration-pid-file
76 dhcpd-configuration-interfaces
81 ntp-configuration-servers
82 ntp-allow-large-adjustment?
94 openntpd-configuration
95 openntpd-configuration?
111 network-manager-configuration
112 network-manager-configuration?
113 network-manager-configuration-dns
114 network-manager-configuration-vpn-plugins
115 network-manager-service-type
117 connman-configuration
118 connman-configuration?
121 modem-manager-configuration
122 modem-manager-configuration?
123 modem-manager-service-type
125 usb-modeswitch-configuration
126 usb-modeswitch-configuration?
127 usb-modeswitch-configuration-usb-modeswitch
128 usb-modeswitch-configuration-usb-modeswitch-data
129 usb-modeswitch-service-type
131 <wpa-supplicant-configuration>
132 wpa-supplicant-configuration
133 wpa-supplicant-configuration?
134 wpa-supplicant-configuration-wpa-supplicant
135 wpa-supplicant-configuration-pid-file
136 wpa-supplicant-configuration-dbus?
137 wpa-supplicant-configuration-interface
138 wpa-supplicant-configuration-config-file
139 wpa-supplicant-configuration-extra-options
140 wpa-supplicant-service-type
142 openvswitch-service-type
143 openvswitch-configuration
145 iptables-configuration
146 iptables-configuration?
147 iptables-configuration-iptables
148 iptables-configuration-ipv4-rules
149 iptables-configuration-ipv6-rules
150 iptables-service-type
152 nftables-service-type
153 nftables-configuration
154 nftables-configuration?
155 nftables-configuration-package
156 nftables-configuration-ruleset
157 %default-nftables-ruleset))
161 ;;; Networking services.
165 (define %facebook-host-aliases
166 ;; This is the list of known Facebook hosts to be added to /etc/hosts if you
169 # Block Facebook IPv4.
170 127.0.0.1 www.facebook.com
171 127.0.0.1 facebook.com
172 127.0.0.1 login.facebook.com
173 127.0.0.1 www.login.facebook.com
175 127.0.0.1 www.fbcdn.net
177 127.0.0.1 www.fbcdn.com
178 127.0.0.1 static.ak.fbcdn.net
179 127.0.0.1 static.ak.connect.facebook.com
180 127.0.0.1 connect.facebook.net
181 127.0.0.1 www.connect.facebook.net
182 127.0.0.1 apps.facebook.com
184 # Block Facebook IPv6.
185 fe80::1%lo0 facebook.com
186 fe80::1%lo0 login.facebook.com
187 fe80::1%lo0 www.login.facebook.com
188 fe80::1%lo0 fbcdn.net
189 fe80::1%lo0 www.fbcdn.net
190 fe80::1%lo0 fbcdn.com
191 fe80::1%lo0 www.fbcdn.com
192 fe80::1%lo0 static.ak.fbcdn.net
193 fe80::1%lo0 static.ak.connect.facebook.com
194 fe80::1%lo0 connect.facebook.net
195 fe80::1%lo0 www.connect.facebook.net
196 fe80::1%lo0 apps.facebook.com\n")
198 (define dhcp-client-service-type
199 (shepherd-service-type
203 (file-append dhcp "/sbin/dhclient"))
206 "/var/run/dhclient.pid")
209 (documentation "Set up networking via DHCP.")
210 (requirement '(user-processes udev))
212 ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when
213 ;; networking is unavailable, but also means that the interface is not up
214 ;; yet when 'start' completes. To wait for the interface to be ready, one
215 ;; should instead monitor udev events.
216 (provision '(networking))
219 ;; When invoked without any arguments, 'dhclient' discovers all
220 ;; non-loopback interfaces *that are up*. However, the relevant
221 ;; interfaces are typically down at this point. Thus we perform
222 ;; our own interface discovery here.
225 (and (arp-network-interface? interface)
226 (not (loopback-network-interface? interface)))))
228 (filter valid? (all-network-interface-names)))
230 ;; XXX: Make sure the interfaces are up so that 'dhclient' can
231 ;; actually send/receive over them.
232 (for-each set-network-interface-up ifaces)
234 (false-if-exception (delete-file #$pid-file))
235 (let ((pid (fork+exec-command
236 (cons* #$dhclient "-nw"
237 "-pf" #$pid-file ifaces))))
238 (and (zero? (cdr (waitpid pid)))
239 (read-pid-file #$pid-file)))))
240 (stop #~(make-kill-destructor))))
243 (define-deprecated (dhcp-client-service #:key (dhcp isc-dhcp))
244 dhcp-client-service-type
245 "Return a service that runs @var{dhcp}, a Dynamic Host Configuration
246 Protocol (DHCP) client, on all the non-loopback network interfaces."
247 (service dhcp-client-service-type dhcp))
249 (define-record-type* <dhcpd-configuration>
250 dhcpd-configuration make-dhcpd-configuration
252 (package dhcpd-configuration-package ;<package>
254 (config-file dhcpd-configuration-config-file ;file-like
256 (version dhcpd-configuration-version ;"4", "6", or "4o6"
258 (run-directory dhcpd-configuration-run-directory
259 (default "/run/dhcpd"))
260 (lease-file dhcpd-configuration-lease-file
261 (default "/var/db/dhcpd.leases"))
262 (pid-file dhcpd-configuration-pid-file
263 (default "/run/dhcpd/dhcpd.pid"))
264 ;; list of strings, e.g. (list "enp0s25")
265 (interfaces dhcpd-configuration-interfaces
268 (define dhcpd-shepherd-service
270 (($ <dhcpd-configuration> package config-file version run-directory
271 lease-file pid-file interfaces)
273 (error "Must supply a config-file"))
274 (list (shepherd-service
275 ;; Allow users to easily run multiple versions simultaneously.
276 (provision (list (string->symbol
277 (string-append "dhcpv" version "-daemon"))))
278 (documentation (string-append "Run the DHCPv" version " daemon"))
279 (requirement '(networking))
280 (start #~(make-forkexec-constructor
281 '(#$(file-append package "/sbin/dhcpd")
282 #$(string-append "-" version)
287 #:pid-file #$pid-file))
288 (stop #~(make-kill-destructor)))))))
290 (define dhcpd-activation
292 (($ <dhcpd-configuration> package config-file version run-directory
293 lease-file pid-file interfaces)
294 (with-imported-modules '((guix build utils))
296 (unless (file-exists? #$run-directory)
297 (mkdir #$run-directory))
298 ;; According to the DHCP manual (man dhcpd.leases), the lease
299 ;; database must be present for dhcpd to start successfully.
300 (unless (file-exists? #$lease-file)
301 (with-output-to-file #$lease-file
302 (lambda _ (display ""))))
303 ;; Validate the config.
305 #$(file-append package "/sbin/dhcpd") "-t" "-cf"
308 (define dhcpd-service-type
312 (list (service-extension shepherd-root-service-type dhcpd-shepherd-service)
313 (service-extension activation-service-type dhcpd-activation)))))
320 (define ntp-server-types (make-enumeration
327 (define-record-type* <ntp-server>
328 ntp-server make-ntp-server
330 ;; The type can be one of the symbols of the NTP-SERVER-TYPE? enumeration.
331 (type ntp-server-type
333 (address ntp-server-address) ; a string
334 ;; The list of options can contain single option names or tuples in the form
336 (options ntp-server-options
339 (define (ntp-server->string ntp-server)
340 ;; Serialize the NTP server object as a string, ready to use in the NTP
341 ;; configuration file.
342 (define (flatten lst)
348 (cons (format #f "~s" x) res)))))
351 (($ <ntp-server> type address options)
352 ;; XXX: It'd be neater if fields were validated at the syntax level (for
353 ;; static ones at least). Perhaps the Guix record type could support a
354 ;; predicate property on a field?
355 (unless (enum-set-member? type ntp-server-types)
356 (error "Invalid NTP server type" type))
357 (string-join (cons* (symbol->string type)
359 (flatten options))))))
362 ;; Default set of NTP servers. These URLs are managed by the NTP Pool project.
363 ;; Within Guix, Leo Famulari <leo@famulari.name> is the administrative contact
364 ;; for this NTP pool "zone".
368 (address "0.guix.pool.ntp.org")
369 (options '("iburst"))))) ;as recommended in the ntpd manual
371 (define-record-type* <ntp-configuration>
372 ntp-configuration make-ntp-configuration
374 (ntp ntp-configuration-ntp
376 (servers %ntp-configuration-servers ;list of <ntp-server> objects
377 (default %ntp-servers))
378 (allow-large-adjustment? ntp-allow-large-adjustment?
379 (default #t))) ;as recommended in the ntpd manual
381 (define (ntp-configuration-servers ntp-configuration)
382 ;; A wrapper to support the deprecated form of this field.
383 (let ((ntp-servers (%ntp-configuration-servers ntp-configuration)))
385 (((? string?) (? string?) ...)
386 (format (current-error-port) "warning: Defining NTP servers as strings is \
387 deprecated. Please use <ntp-server> records instead.\n")
392 (options '()))) ntp-servers))
393 ((($ <ntp-server>) ($ <ntp-server>) ...)
396 (define ntp-shepherd-service
398 (($ <ntp-configuration> ntp servers allow-large-adjustment?)
400 ;; TODO: Add authentication support.
402 (string-append "driftfile /var/run/ntpd/ntp.drift\n"
403 (string-join (map ntp-server->string servers)
406 # Disable status queries as a workaround for CVE-2013-5211:
407 # <http://support.ntp.org/bin/view/Main/SecurityNotice#DRDoS_Amplification_Attack_using>.
408 restrict default kod nomodify notrap nopeer noquery limited
409 restrict -6 default kod nomodify notrap nopeer noquery limited
411 # Yet, allow use of the local 'ntpq'.
415 # This is required to use servers from a pool directive when using the 'nopeer'
416 # option by default, as documented in the 'ntp.conf' manual.
417 restrict source notrap nomodify noquery\n"))
420 (plain-file "ntpd.conf" config))
422 (list (shepherd-service
424 (documentation "Run the Network Time Protocol (NTP) daemon.")
425 (requirement '(user-processes networking))
426 (start #~(make-forkexec-constructor
427 (list (string-append #$ntp "/bin/ntpd") "-n"
428 "-c" #$ntpd.conf "-u" "ntpd"
429 #$@(if allow-large-adjustment?
432 (stop #~(make-kill-destructor))))))))
434 (define %ntp-accounts
439 (comment "NTP daemon user")
440 (home-directory "/var/empty")
441 (shell (file-append shadow "/sbin/nologin")))))
444 (define (ntp-service-activation config)
445 "Return the activation gexp for CONFIG."
446 (with-imported-modules '((guix build utils))
448 (use-modules (guix build utils))
452 (let ((directory "/var/run/ntpd"))
454 (chown directory (passwd:uid %user) (passwd:gid %user))))))
456 (define ntp-service-type
457 (service-type (name 'ntp)
459 (list (service-extension shepherd-root-service-type
460 ntp-shepherd-service)
461 (service-extension account-service-type
462 (const %ntp-accounts))
463 (service-extension activation-service-type
464 ntp-service-activation)))
466 "Run the @command{ntpd}, the Network Time Protocol (NTP)
467 daemon of the @uref{http://www.ntp.org, Network Time Foundation}. The daemon
468 will keep the system clock synchronized with that of the given servers.")
469 (default-value (ntp-configuration))))
471 (define-deprecated (ntp-service #:key (ntp ntp)
472 (servers %ntp-servers)
473 allow-large-adjustment?)
475 "Return a service that runs the daemon from @var{ntp}, the
476 @uref{http://www.ntp.org, Network Time Protocol package}. The daemon will
477 keep the system clock synchronized with that of @var{servers}.
478 @var{allow-large-adjustment?} determines whether @command{ntpd} is allowed to
479 make an initial adjustment of more than 1,000 seconds."
480 (service ntp-service-type
481 (ntp-configuration (ntp ntp)
483 (allow-large-adjustment?
484 allow-large-adjustment?))))
491 (define %openntpd-servers
492 (map ntp-server-address %ntp-servers))
494 (define-record-type* <openntpd-configuration>
495 openntpd-configuration make-openntpd-configuration
496 openntpd-configuration?
497 (openntpd openntpd-configuration-openntpd
499 (listen-on openntpd-listen-on
500 (default '("127.0.0.1"
502 (query-from openntpd-query-from
504 (sensor openntpd-sensor
506 (server openntpd-server
508 (servers openntpd-servers
509 (default %openntpd-servers))
510 (constraint-from openntpd-constraint-from
512 (constraints-from openntpd-constraints-from
514 (allow-large-adjustment? openntpd-allow-large-adjustment?
515 (default #f))) ; upstream default
517 (define (openntpd-configuration->string config)
519 (define (quote-field? name)
520 (member name '("constraints from")))
522 (match-record config <openntpd-configuration>
523 (listen-on query-from sensor server servers constraint-from
528 (filter-map (lambda (field values)
530 (() #f) ;discard entry with filter-map
531 ((val ...) ;validate value type
533 (if (quote-field? field)
534 (format #f "~a \"~a\"" field value)
535 (format #f "~a ~a" field value)))
538 '("listen on" "query from" "sensor" "server" "servers"
539 "constraint from" "constraints from")
540 ;; The corresponding entry values.
541 (list listen-on query-from sensor server servers
542 constraint-from constraints-from)))
544 "\n"))) ;add a trailing newline
546 (define (openntpd-shepherd-service config)
547 (let ((openntpd (openntpd-configuration-openntpd config))
548 (allow-large-adjustment? (openntpd-allow-large-adjustment? config)))
551 (plain-file "ntpd.conf" (openntpd-configuration->string config)))
553 (list (shepherd-service
555 (documentation "Run the Network Time Protocol (NTP) daemon.")
556 (requirement '(user-processes networking))
557 (start #~(make-forkexec-constructor
558 (list (string-append #$openntpd "/sbin/ntpd")
560 "-d" ;; don't daemonize
561 #$@(if allow-large-adjustment?
564 ;; When ntpd is daemonized it repeatedly tries to respawn
565 ;; while running, leading shepherd to disable it. To
566 ;; prevent spamming stderr, redirect output to logfile.
567 #:log-file "/var/log/ntpd"))
568 (stop #~(make-kill-destructor))))))
570 (define (openntpd-service-activation config)
571 "Return the activation gexp for CONFIG."
572 (with-imported-modules '((guix build utils))
574 (use-modules (guix build utils))
578 (unless (file-exists? "/var/db/ntpd.drift")
579 (with-output-to-file "/var/db/ntpd.drift"
581 (format #t "0.0")))))))
583 (define openntpd-service-type
584 (service-type (name 'openntpd)
586 (list (service-extension shepherd-root-service-type
587 openntpd-shepherd-service)
588 (service-extension account-service-type
589 (const %ntp-accounts))
590 (service-extension profile-service-type
591 (compose list openntpd-configuration-openntpd))
592 (service-extension activation-service-type
593 openntpd-service-activation)))
594 (default-value (openntpd-configuration))
596 "Run the @command{ntpd}, the Network Time Protocol (NTP)
597 daemon, as implemented by @uref{http://www.openntpd.org, OpenNTPD}. The
598 daemon will keep the system clock synchronized with that of the given servers.")))
605 (define-record-type* <inetd-configuration> inetd-configuration
606 make-inetd-configuration
608 (program inetd-configuration-program ;file-like
609 (default (file-append inetutils "/libexec/inetd")))
610 (entries inetd-configuration-entries ;list of <inetd-entry>
613 (define-record-type* <inetd-entry> inetd-entry make-inetd-entry
615 (node inetd-entry-node ;string or #f
617 (name inetd-entry-name) ;string, from /etc/services
619 (socket-type inetd-entry-socket-type) ;stream | dgram | raw |
621 (protocol inetd-entry-protocol) ;string, from /etc/protocols
623 (wait? inetd-entry-wait? ;Boolean
625 (user inetd-entry-user) ;string
627 (program inetd-entry-program ;string or file-like object
628 (default "internal"))
629 (arguments inetd-entry-arguments ;list of strings or file-like objects
632 (define (inetd-config-file entries)
633 (apply mixed-text-file "inetd.conf"
636 (let* ((node (inetd-entry-node entry))
637 (name (inetd-entry-name entry))
639 (if node (string-append node ":" name) name))
641 (match (inetd-entry-socket-type entry)
642 ((or 'stream 'dgram 'raw 'rdm 'seqpacket)
643 (symbol->string (inetd-entry-socket-type entry)))))
644 (protocol (inetd-entry-protocol entry))
645 (wait (if (inetd-entry-wait? entry) "wait" "nowait"))
646 (user (inetd-entry-user entry))
647 (program (inetd-entry-program entry))
648 (args (inetd-entry-arguments entry)))
651 (list #$@(list socket type protocol wait user program) #$@args)
655 (define inetd-shepherd-service
657 (($ <inetd-configuration> program ()) '()) ; empty list of entries -> do nothing
658 (($ <inetd-configuration> program entries)
661 (documentation "Run inetd.")
663 (requirement '(user-processes networking syslogd))
664 (start #~(make-forkexec-constructor
665 (list #$program #$(inetd-config-file entries))
666 #:pid-file "/var/run/inetd.pid"))
667 (stop #~(make-kill-destructor)))))))
669 (define-public inetd-service-type
673 (list (service-extension shepherd-root-service-type
674 inetd-shepherd-service)))
676 ;; The service can be extended with additional lists of entries.
677 (compose concatenate)
678 (extend (lambda (config entries)
681 (entries (append (inetd-configuration-entries config)
684 "Start @command{inetd}, the @dfn{Internet superserver}. It is responsible
685 for listening on Internet sockets and spawning the corresponding services on
693 (define-record-type* <tor-configuration>
694 tor-configuration make-tor-configuration
696 (tor tor-configuration-tor
698 (config-file tor-configuration-config-file
699 (default (plain-file "empty" "")))
700 (hidden-services tor-configuration-hidden-services
702 (socks-socket-type tor-configuration-socks-socket-type ; 'tcp or 'unix
705 (define %tor-accounts
706 ;; User account and groups for Tor.
707 (list (user-group (name "tor") (system? #t))
712 (comment "Tor daemon user")
713 (home-directory "/var/empty")
714 (shell (file-append shadow "/sbin/nologin")))))
716 (define-record-type <hidden-service>
717 (hidden-service name mapping)
719 (name hidden-service-name) ;string
720 (mapping hidden-service-mapping)) ;list of port/address tuples
722 (define (tor-configuration->torrc config)
723 "Return a 'torrc' file for CONFIG."
725 (($ <tor-configuration> tor config-file services socks-socket-type)
728 (with-imported-modules '((guix build utils))
730 (use-modules (guix build utils)
733 (call-with-output-file #$output
736 ### These lines were generated from your system configuration:
738 DataDirectory /var/lib/tor
739 PidFile /var/run/tor/tor.pid
740 Log notice syslog\n" port)
741 (when (eq? 'unix '#$socks-socket-type)
743 SocksPort unix:/var/run/tor/socks-sock
744 UnixSocksGroupWritable 1\n" port))
746 (for-each (match-lambda
747 ((service (ports hosts) ...)
749 HiddenServiceDir /var/lib/tor/hidden-services/~a~%"
751 (for-each (lambda (tcp-port host)
753 HiddenServicePort ~a ~a~%"
756 '#$(map (match-lambda
757 (($ <hidden-service> name mapping)
758 (cons name mapping)))
762 ### End of automatically generated lines.\n\n" port)
764 ;; Append the user's config file.
765 (call-with-input-file #$config-file
767 (dump-port input port)))
770 (define (tor-shepherd-service config)
771 "Return a <shepherd-service> running Tor."
773 (($ <tor-configuration> tor)
774 (let ((torrc (tor-configuration->torrc config)))
775 (with-imported-modules (source-module-closure
776 '((gnu build shepherd)
777 (gnu system file-systems)))
778 (list (shepherd-service
781 ;; Tor needs at least one network interface to be up, hence the
782 ;; dependency on 'loopback'.
783 (requirement '(user-processes loopback syslogd))
785 (modules '((gnu build shepherd)
786 (gnu system file-systems)))
788 (start #~(make-forkexec-constructor/container
789 (list #$(file-append tor "/bin/tor") "-f" #$torrc)
791 #:mappings (list (file-system-mapping
792 (source "/var/lib/tor")
796 (source "/dev/log") ;for syslog
799 (source "/var/run/tor")
802 #:pid-file "/var/run/tor/tor.pid"))
803 (stop #~(make-kill-destructor))
804 (documentation "Run the Tor anonymous network overlay."))))))))
806 (define (tor-activation config)
807 "Set up directories for Tor and its hidden services, if any."
809 (use-modules (guix build utils))
814 (define (initialize service)
815 (let ((directory (string-append "/var/lib/tor/hidden-services/"
818 (chown directory (passwd:uid %user) (passwd:gid %user))
820 ;; The daemon bails out if we give wider permissions.
821 (chmod directory #o700)))
823 ;; Allow Tor to write its PID file.
824 (mkdir-p "/var/run/tor")
825 (chown "/var/run/tor" (passwd:uid %user) (passwd:gid %user))
826 ;; Set the group permissions to rw so that if the system administrator
827 ;; has specified UnixSocksGroupWritable=1 in their torrc file, members
828 ;; of the "tor" group will be able to use the SOCKS socket.
829 (chmod "/var/run/tor" #o750)
831 ;; Allow Tor to access the hidden services' directories.
832 (mkdir-p "/var/lib/tor")
833 (chown "/var/lib/tor" (passwd:uid %user) (passwd:gid %user))
834 (chmod "/var/lib/tor" #o700)
836 ;; Make sure /var/lib is accessible to the 'tor' user.
837 (chmod "/var/lib" #o755)
840 '#$(map hidden-service-name
841 (tor-configuration-hidden-services config)))))
843 (define tor-service-type
844 (service-type (name 'tor)
846 (list (service-extension shepherd-root-service-type
847 tor-shepherd-service)
848 (service-extension account-service-type
849 (const %tor-accounts))
850 (service-extension activation-service-type
853 ;; This can be extended with hidden services.
854 (compose concatenate)
855 (extend (lambda (config services)
859 (append (tor-configuration-hidden-services config)
861 (default-value (tor-configuration))
863 "Run the @uref{https://torproject.org, Tor} anonymous
864 networking daemon.")))
866 (define-deprecated (tor-service #:optional
867 (config-file (plain-file "empty" ""))
870 "Return a service to run the @uref{https://torproject.org, Tor} anonymous
873 The daemon runs as the @code{tor} unprivileged user. It is passed
874 @var{config-file}, a file-like object, with an additional @code{User tor} line
875 and lines for hidden services added via @code{tor-hidden-service}. Run
876 @command{man tor} for information about the configuration file."
877 (service tor-service-type
878 (tor-configuration (tor tor)
879 (config-file config-file))))
881 (define tor-hidden-service-type
882 ;; A type that extends Tor with hidden services.
883 (service-type (name 'tor-hidden-service)
885 (list (service-extension tor-service-type list)))
887 "Define a new Tor @dfn{hidden service}.")))
889 (define (tor-hidden-service name mapping)
890 "Define a new Tor @dfn{hidden service} called @var{name} and implementing
891 @var{mapping}. @var{mapping} is a list of port/host tuples, such as:
894 '((22 \"127.0.0.1:22\")
895 (80 \"127.0.0.1:8080\"))
898 In this example, port 22 of the hidden service is mapped to local port 22, and
899 port 80 is mapped to local port 8080.
901 This creates a @file{/var/lib/tor/hidden-services/@var{name}} directory, where
902 the @file{hostname} file contains the @code{.onion} host name for the hidden
905 See @uref{https://www.torproject.org/docs/tor-hidden-service.html.en, the Tor
906 project's documentation} for more information."
907 (service tor-hidden-service-type
908 (hidden-service name mapping)))
915 (define %wicd-activation
916 ;; Activation gexp for Wicd.
918 (use-modules (guix build utils))
920 (mkdir-p "/etc/wicd")
921 (let ((file-name "/etc/wicd/dhclient.conf.template.default"))
922 (unless (file-exists? file-name)
923 (copy-file (string-append #$wicd file-name)
926 ;; Wicd invokes 'wpa_supplicant', which needs this directory for its
927 ;; named socket files.
928 (mkdir-p "/var/run/wpa_supplicant")
929 (chmod "/var/run/wpa_supplicant" #o750)))
931 (define (wicd-shepherd-service wicd)
932 "Return a shepherd service for WICD."
933 (list (shepherd-service
934 (documentation "Run the Wicd network manager.")
935 (provision '(networking))
936 (requirement '(user-processes dbus-system loopback))
937 (start #~(make-forkexec-constructor
938 (list (string-append #$wicd "/sbin/wicd")
940 (stop #~(make-kill-destructor)))))
942 (define wicd-service-type
943 (service-type (name 'wicd)
945 (list (service-extension shepherd-root-service-type
946 wicd-shepherd-service)
947 (service-extension dbus-root-service-type
949 (service-extension activation-service-type
950 (const %wicd-activation))
952 ;; Add Wicd to the global profile.
953 (service-extension profile-service-type list)))
955 "Run @url{https://launchpad.net/wicd,Wicd}, a network
956 management daemon that aims to simplify wired and wireless networking.")))
958 (define* (wicd-service #:key (wicd wicd))
959 "Return a service that runs @url{https://launchpad.net/wicd,Wicd}, a network
960 management daemon that aims to simplify wired and wireless networking.
962 This service adds the @var{wicd} package to the global profile, providing
963 several commands to interact with the daemon and configure networking:
964 @command{wicd-client}, a graphical user interface, and the @command{wicd-cli}
965 and @command{wicd-curses} user interfaces."
966 (service wicd-service-type wicd))
973 (define-record-type* <modem-manager-configuration>
974 modem-manager-configuration make-modem-manager-configuration
975 modem-manager-configuration?
976 (modem-manager modem-manager-configuration-modem-manager
977 (default modem-manager)))
984 (define-record-type* <network-manager-configuration>
985 network-manager-configuration make-network-manager-configuration
986 network-manager-configuration?
987 (network-manager network-manager-configuration-network-manager
988 (default network-manager))
989 (dns network-manager-configuration-dns
991 (vpn-plugins network-manager-configuration-vpn-plugins ;list of <package>
994 (define network-manager-activation
995 ;; Activation gexp for NetworkManager
997 (($ <network-manager-configuration> network-manager dns vpn-plugins)
999 (use-modules (guix build utils))
1000 (mkdir-p "/etc/NetworkManager/system-connections")
1001 #$@(if (equal? dns "dnsmasq")
1002 ;; create directory to store dnsmasq lease file
1003 '((mkdir-p "/var/lib/misc"))
1006 (define (vpn-plugin-directory plugins)
1007 "Return a directory containing PLUGINS, the NM VPN plugins."
1008 (directory-union "network-manager-vpn-plugins" plugins))
1010 (define (network-manager-accounts config)
1011 "Return the list of <user-account> and <user-group> for CONFIG."
1013 (file-append shadow "/sbin/nologin"))
1016 (append-map (lambda (package)
1018 (user-account (system? #t)
1020 (group "network-manager")
1021 (comment "NetworkManager helper")
1022 (home-directory "/var/empty")
1023 (create-home-directory? #f)
1025 (or (assoc-ref (package-properties package)
1028 (network-manager-configuration-vpn-plugins config)))
1034 (cons (user-group (name "network-manager") (system? #t))
1037 (define network-manager-environment
1039 (($ <network-manager-configuration> network-manager dns vpn-plugins)
1040 ;; Define this variable in the global environment such that
1041 ;; "nmcli connection import type openvpn file foo.ovpn" works.
1042 `(("NM_VPN_PLUGIN_DIR"
1043 . ,(file-append (vpn-plugin-directory vpn-plugins)
1044 "/lib/NetworkManager/VPN"))))))
1046 (define network-manager-shepherd-service
1048 (($ <network-manager-configuration> network-manager dns vpn-plugins)
1049 (let ((conf (plain-file "NetworkManager.conf"
1050 (string-append "[main]\ndns=" dns "\n")))
1051 (vpn (vpn-plugin-directory vpn-plugins)))
1052 (list (shepherd-service
1053 (documentation "Run the NetworkManager.")
1054 (provision '(networking))
1055 (requirement '(user-processes dbus-system wpa-supplicant loopback))
1056 (start #~(make-forkexec-constructor
1057 (list (string-append #$network-manager
1058 "/sbin/NetworkManager")
1059 (string-append "--config=" #$conf)
1061 #:environment-variables
1062 (list (string-append "NM_VPN_PLUGIN_DIR=" #$vpn
1063 "/lib/NetworkManager/VPN")
1064 ;; Override non-existent default users
1066 "NM_OPENVPN_GROUP=")))
1067 (stop #~(make-kill-destructor))))))))
1069 (define network-manager-service-type
1073 (($ <network-manager-configuration> network-manager _ vpn-plugins)
1074 `(,network-manager ,@vpn-plugins)))))
1077 (name 'network-manager)
1079 (list (service-extension shepherd-root-service-type
1080 network-manager-shepherd-service)
1081 (service-extension dbus-root-service-type config->packages)
1082 (service-extension polkit-service-type
1085 network-manager-configuration-network-manager))
1086 (service-extension account-service-type
1087 network-manager-accounts)
1088 (service-extension activation-service-type
1089 network-manager-activation)
1090 (service-extension session-environment-service-type
1091 network-manager-environment)
1092 ;; Add network-manager to the system profile.
1093 (service-extension profile-service-type config->packages)))
1094 (default-value (network-manager-configuration))
1096 "Run @uref{https://wiki.gnome.org/Projects/NetworkManager,
1097 NetworkManager}, a network management daemon that aims to simplify wired and
1098 wireless networking."))))
1105 (define-record-type* <connman-configuration>
1106 connman-configuration make-connman-configuration
1107 connman-configuration?
1108 (connman connman-configuration-connman
1110 (disable-vpn? connman-configuration-disable-vpn?
1113 (define (connman-activation config)
1114 (let ((disable-vpn? (connman-configuration-disable-vpn? config)))
1115 (with-imported-modules '((guix build utils))
1117 (use-modules (guix build utils))
1118 (mkdir-p "/var/lib/connman/")
1119 (unless #$disable-vpn?
1120 (mkdir-p "/var/lib/connman-vpn/"))))))
1122 (define (connman-shepherd-service config)
1123 "Return a shepherd service for Connman"
1125 (connman-configuration? config)
1126 (let ((connman (connman-configuration-connman config))
1127 (disable-vpn? (connman-configuration-disable-vpn? config)))
1128 (list (shepherd-service
1129 (documentation "Run Connman")
1130 (provision '(networking))
1132 '(user-processes dbus-system loopback wpa-supplicant))
1133 (start #~(make-forkexec-constructor
1134 (list (string-append #$connman
1137 #$@(if disable-vpn? '("--noplugin=vpn") '()))
1139 ;; As connman(8) notes, when passing '-n', connman
1140 ;; "directs log output to the controlling terminal in
1141 ;; addition to syslog." Redirect stdout and stderr
1142 ;; to avoid spamming the console (XXX: for some reason
1143 ;; redirecting to /dev/null doesn't work.)
1144 #:log-file "/var/log/connman.log"))
1145 (stop #~(make-kill-destructor)))))))
1147 (define connman-service-type
1148 (let ((connman-package (compose list connman-configuration-connman)))
1149 (service-type (name 'connman)
1151 (list (service-extension shepherd-root-service-type
1152 connman-shepherd-service)
1153 (service-extension polkit-service-type
1155 (service-extension dbus-root-service-type
1157 (service-extension activation-service-type
1159 ;; Add connman to the system profile.
1160 (service-extension profile-service-type
1162 (default-value (connman-configuration))
1164 "Run @url{https://01.org/connman,Connman},
1165 a network connection manager."))))
1172 (define modem-manager-service-type
1173 (let ((config->package
1175 (($ <modem-manager-configuration> modem-manager)
1176 (list modem-manager)))))
1177 (service-type (name 'modem-manager)
1179 (list (service-extension dbus-root-service-type
1181 (service-extension udev-service-type
1183 (service-extension polkit-service-type
1185 (default-value (modem-manager-configuration))
1187 "Run @uref{https://wiki.gnome.org/Projects/ModemManager,
1188 ModemManager}, a modem management daemon that aims to simplify dialup
1196 (define-record-type* <usb-modeswitch-configuration>
1197 usb-modeswitch-configuration make-usb-modeswitch-configuration
1198 usb-modeswitch-configuration?
1199 (usb-modeswitch usb-modeswitch-configuration-usb-modeswitch
1200 (default usb-modeswitch))
1201 (usb-modeswitch-data usb-modeswitch-configuration-usb-modeswitch-data
1202 (default usb-modeswitch-data))
1203 (config-file usb-modeswitch-configuration-config-file
1204 (default #~(string-append #$usb-modeswitch:dispatcher
1205 "/etc/usb_modeswitch.conf"))))
1207 (define (usb-modeswitch-sh usb-modeswitch config-file)
1208 "Build a copy of usb_modeswitch.sh located in package USB-MODESWITCH,
1209 modified to pass the CONFIG-FILE in its calls to usb_modeswitch_dispatcher,
1210 and wrap it to actually find the dispatcher in USB-MODESWITCH. The script
1211 will be run by USB_ModeSwitch’s udev rules file when a modeswitchable USB
1212 device is detected."
1215 (with-imported-modules '((guix build utils))
1217 (use-modules (guix build utils))
1220 #~(string-append " --config-file=" #$config-file)
1223 (install-file (string-append #$usb-modeswitch:dispatcher
1224 "/lib/udev/usb_modeswitch")
1227 ;; insert CFG-PARAM into usb_modeswitch_dispatcher command-lines
1228 (substitute* (string-append #$output "/usb_modeswitch")
1229 (("(exec usb_modeswitch_dispatcher .*)( 2>>)" _ left right)
1230 (string-append left cfg-param right))
1231 (("(exec usb_modeswitch_dispatcher .*)( &)" _ left right)
1232 (string-append left cfg-param right)))
1234 ;; wrap-program needs bash in PATH:
1235 (putenv (string-append "PATH=" #$bash "/bin"))
1236 (wrap-program (string-append #$output "/usb_modeswitch")
1237 `("PATH" ":" = (,(string-append #$coreutils "/bin")
1239 #$usb-modeswitch:dispatcher
1242 (define (usb-modeswitch-configuration->udev-rules config)
1243 "Build a rules file for extending udev-service-type from the rules in the
1244 usb-modeswitch package specified in CONFIG. The rules file will invoke
1245 usb_modeswitch.sh from the usb-modeswitch package, modified to pass the right
1248 (($ <usb-modeswitch-configuration> usb-modeswitch data config-file)
1250 "usb_modeswitch.rules"
1251 (with-imported-modules '((guix build utils))
1253 (use-modules (guix build utils))
1254 (let ((in (string-append #$data "/udev/40-usb_modeswitch.rules"))
1255 (out (string-append #$output "/lib/udev/rules.d"))
1256 (script #$(usb-modeswitch-sh usb-modeswitch config-file)))
1259 (install-file in out)
1260 (substitute* "40-usb_modeswitch.rules"
1261 (("PROGRAM=\"usb_modeswitch")
1262 (string-append "PROGRAM=\"" script "/usb_modeswitch"))
1263 (("RUN\\+=\"usb_modeswitch")
1264 (string-append "RUN+=\"" script "/usb_modeswitch"))))))))))
1266 (define usb-modeswitch-service-type
1268 (name 'usb-modeswitch)
1274 (let ((rules (usb-modeswitch-configuration->udev-rules config)))
1276 (default-value (usb-modeswitch-configuration))
1277 (description "Run @uref{http://www.draisberghof.de/usb_modeswitch/,
1278 USB_ModeSwitch}, a mode switching tool for controlling USB devices with
1279 multiple @dfn{modes}. When plugged in for the first time many USB
1280 devices (primarily high-speed WAN modems) act like a flash storage containing
1281 installers for Windows drivers. USB_ModeSwitch replays the sequence the
1282 Windows drivers would send to switch their mode from storage to modem (or
1283 whatever the thing is supposed to do).")))
1290 (define-record-type* <wpa-supplicant-configuration>
1291 wpa-supplicant-configuration make-wpa-supplicant-configuration
1292 wpa-supplicant-configuration?
1293 (wpa-supplicant wpa-supplicant-configuration-wpa-supplicant ;<package>
1294 (default wpa-supplicant))
1295 (pid-file wpa-supplicant-configuration-pid-file ;string
1296 (default "/var/run/wpa_supplicant.pid"))
1297 (dbus? wpa-supplicant-configuration-dbus? ;Boolean
1299 (interface wpa-supplicant-configuration-interface ;#f | string
1301 (config-file wpa-supplicant-configuration-config-file ;#f | <file-like>
1303 (extra-options wpa-supplicant-configuration-extra-options ;list of strings
1306 (define wpa-supplicant-shepherd-service
1308 (($ <wpa-supplicant-configuration> wpa-supplicant pid-file dbus? interface
1309 config-file extra-options)
1310 (list (shepherd-service
1311 (documentation "Run the WPA supplicant daemon")
1312 (provision '(wpa-supplicant))
1313 (requirement '(user-processes dbus-system loopback syslogd))
1314 (start #~(make-forkexec-constructor
1315 (list (string-append #$wpa-supplicant
1316 "/sbin/wpa_supplicant")
1317 (string-append "-P" #$pid-file)
1318 "-B" ;run in background
1319 "-s" ;log to syslogd
1324 #~((string-append "-i" #$interface))
1327 #~((string-append "-c" #$config-file))
1330 #:pid-file #$pid-file))
1331 (stop #~(make-kill-destructor)))))))
1333 (define wpa-supplicant-service-type
1334 (let ((config->package
1336 (($ <wpa-supplicant-configuration> wpa-supplicant)
1337 (list wpa-supplicant)))))
1338 (service-type (name 'wpa-supplicant)
1340 (list (service-extension shepherd-root-service-type
1341 wpa-supplicant-shepherd-service)
1342 (service-extension dbus-root-service-type config->package)
1343 (service-extension profile-service-type config->package)))
1344 (description "Run the WPA Supplicant daemon, a service that
1345 implements authentication, key negotiation and more for wireless networks.")
1346 (default-value (wpa-supplicant-configuration)))))
1353 (define-record-type* <openvswitch-configuration>
1354 openvswitch-configuration make-openvswitch-configuration
1355 openvswitch-configuration?
1356 (package openvswitch-configuration-package
1357 (default openvswitch)))
1359 (define openvswitch-activation
1361 (($ <openvswitch-configuration> package)
1362 (let ((ovsdb-tool (file-append package "/bin/ovsdb-tool")))
1363 (with-imported-modules '((guix build utils))
1365 (use-modules (guix build utils))
1366 (mkdir-p "/var/run/openvswitch")
1367 (mkdir-p "/var/lib/openvswitch")
1368 (let ((conf.db "/var/lib/openvswitch/conf.db"))
1369 (unless (file-exists? conf.db)
1370 (system* #$ovsdb-tool "create" conf.db)))))))))
1372 (define openvswitch-shepherd-service
1374 (($ <openvswitch-configuration> package)
1375 (let ((ovsdb-server (file-append package "/sbin/ovsdb-server"))
1376 (ovs-vswitchd (file-append package "/sbin/ovs-vswitchd")))
1379 (provision '(ovsdb))
1380 (documentation "Run the Open vSwitch database server.")
1381 (start #~(make-forkexec-constructor
1382 (list #$ovsdb-server "--pidfile"
1383 "--remote=punix:/var/run/openvswitch/db.sock")
1384 #:pid-file "/var/run/openvswitch/ovsdb-server.pid"))
1385 (stop #~(make-kill-destructor)))
1387 (provision '(vswitchd))
1388 (requirement '(ovsdb))
1389 (documentation "Run the Open vSwitch daemon.")
1390 (start #~(make-forkexec-constructor
1391 (list #$ovs-vswitchd "--pidfile")
1392 #:pid-file "/var/run/openvswitch/ovs-vswitchd.pid"))
1393 (stop #~(make-kill-destructor))))))))
1395 (define openvswitch-service-type
1399 (list (service-extension activation-service-type
1400 openvswitch-activation)
1401 (service-extension profile-service-type
1402 (compose list openvswitch-configuration-package))
1403 (service-extension shepherd-root-service-type
1404 openvswitch-shepherd-service)))
1406 "Run @uref{http://www.openvswitch.org, Open vSwitch}, a multilayer virtual
1407 switch designed to enable massive network automation through programmatic
1409 (default-value (openvswitch-configuration))))
1415 (define %iptables-accept-all-rules
1416 (plain-file "iptables-accept-all.rules"
1424 (define-record-type* <iptables-configuration>
1425 iptables-configuration make-iptables-configuration iptables-configuration?
1426 (iptables iptables-configuration-iptables
1428 (ipv4-rules iptables-configuration-ipv4-rules
1429 (default %iptables-accept-all-rules))
1430 (ipv6-rules iptables-configuration-ipv6-rules
1431 (default %iptables-accept-all-rules)))
1433 (define iptables-shepherd-service
1435 (($ <iptables-configuration> iptables ipv4-rules ipv6-rules)
1436 (let ((iptables-restore (file-append iptables "/sbin/iptables-restore"))
1437 (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")))
1439 (documentation "Packet filtering framework")
1440 (provision '(iptables))
1442 (invoke #$iptables-restore #$ipv4-rules)
1443 (invoke #$ip6tables-restore #$ipv6-rules)))
1445 (invoke #$iptables-restore #$%iptables-accept-all-rules)
1446 (invoke #$ip6tables-restore #$%iptables-accept-all-rules))))))))
1448 (define iptables-service-type
1452 "Run @command{iptables-restore}, setting up the specified rules.")
1454 (list (service-extension shepherd-root-service-type
1455 (compose list iptables-shepherd-service))))))
1461 (define %default-nftables-ruleset
1462 (plain-file "nftables.conf"
1463 "# A simple and safe firewall
1466 type filter hook input priority 0; policy drop;
1468 # early drop of invalid connections
1469 ct state invalid drop
1471 # allow established/related connections
1472 ct state { established, related } accept
1474 # allow from loopback
1478 ip protocol icmp accept
1479 ip6 nexthdr icmpv6 accept
1482 tcp dport ssh accept
1484 # reject everything else
1485 reject with icmpx type port-unreachable
1488 type filter hook forward priority 0; policy drop;
1491 type filter hook output priority 0; policy accept;
1496 (define-record-type* <nftables-configuration>
1497 nftables-configuration
1498 make-nftables-configuration
1499 nftables-configuration?
1500 (package nftables-configuration-package
1502 (ruleset nftables-configuration-ruleset ; file-like object
1503 (default %default-nftables-ruleset)))
1505 (define nftables-shepherd-service
1507 (($ <nftables-configuration> package ruleset)
1508 (let ((nft (file-append package "/sbin/nft")))
1510 (documentation "Packet filtering and classification")
1511 (provision '(nftables))
1513 (invoke #$nft "--file" #$ruleset)))
1515 (invoke #$nft "flush" "ruleset"))))))))
1517 (define nftables-service-type
1521 "Run @command{nft}, setting up the specified ruleset.")
1523 (list (service-extension shepherd-root-service-type
1524 (compose list nftables-shepherd-service))
1525 (service-extension profile-service-type
1526 (compose list nftables-configuration-package))))
1527 (default-value (nftables-configuration))))
1529 ;;; networking.scm ends here