networking.scm 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. ;;;
  13. ;;; This file is part of GNU Guix.
  14. ;;;
  15. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  16. ;;; under the terms of the GNU General Public License as published by
  17. ;;; the Free Software Foundation; either version 3 of the License, or (at
  18. ;;; your option) any later version.
  19. ;;;
  20. ;;; GNU Guix is distributed in the hope that it will be useful, but
  21. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;;; GNU General Public License for more details.
  24. ;;;
  25. ;;; You should have received a copy of the GNU General Public License
  26. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  27. (define-module (gnu services networking)
  28. #:use-module (gnu services)
  29. #:use-module (gnu services base)
  30. #:use-module (gnu services shepherd)
  31. #:use-module (gnu services dbus)
  32. #:use-module (gnu system shadow)
  33. #:use-module (gnu system pam)
  34. #:use-module (gnu packages admin)
  35. #:use-module (gnu packages connman)
  36. #:use-module (gnu packages freedesktop)
  37. #:use-module (gnu packages linux)
  38. #:use-module (gnu packages tor)
  39. #:use-module (gnu packages messaging)
  40. #:use-module (gnu packages networking)
  41. #:use-module (gnu packages ntp)
  42. #:use-module (gnu packages wicd)
  43. #:use-module (gnu packages gnome)
  44. #:use-module (guix gexp)
  45. #:use-module (guix records)
  46. #:use-module (guix modules)
  47. #:use-module (guix deprecation)
  48. #:use-module (srfi srfi-1)
  49. #:use-module (srfi srfi-9)
  50. #:use-module (srfi srfi-26)
  51. #:use-module (ice-9 match)
  52. #:re-export (static-networking-service
  53. static-networking-service-type)
  54. #:export (%facebook-host-aliases
  55. dhcp-client-service
  56. dhcp-client-service-type
  57. dhcpd-service-type
  58. dhcpd-configuration
  59. dhcpd-configuration?
  60. dhcpd-configuration-package
  61. dhcpd-configuration-config-file
  62. dhcpd-configuration-version
  63. dhcpd-configuration-run-directory
  64. dhcpd-configuration-lease-file
  65. dhcpd-configuration-pid-file
  66. dhcpd-configuration-interfaces
  67. %ntp-servers
  68. ntp-configuration
  69. ntp-configuration?
  70. ntp-service
  71. ntp-service-type
  72. openntpd-configuration
  73. openntpd-configuration?
  74. openntpd-service-type
  75. inetd-configuration
  76. inetd-entry
  77. inetd-service-type
  78. tor-configuration
  79. tor-configuration?
  80. tor-hidden-service
  81. tor-service
  82. tor-service-type
  83. wicd-service-type
  84. wicd-service
  85. network-manager-configuration
  86. network-manager-configuration?
  87. network-manager-configuration-dns
  88. network-manager-service-type
  89. connman-configuration
  90. connman-configuration?
  91. connman-service-type
  92. modem-manager-configuration
  93. modem-manager-configuration?
  94. modem-manager-service-type
  95. <wpa-supplicant-configuration>
  96. wpa-supplicant-configuration
  97. wpa-supplicant-configuration?
  98. wpa-supplicant-configuration-wpa-supplicant
  99. wpa-supplicant-configuration-pid-file
  100. wpa-supplicant-configuration-dbus?
  101. wpa-supplicant-configuration-interface
  102. wpa-supplicant-configuration-config-file
  103. wpa-supplicant-configuration-extra-options
  104. wpa-supplicant-service-type
  105. openvswitch-service-type
  106. openvswitch-configuration
  107. iptables-configuration
  108. iptables-configuration?
  109. iptables-configuration-iptables
  110. iptables-configuration-ipv4-rules
  111. iptables-configuration-ipv6-rules
  112. iptables-service-type))
  113. ;;; Commentary:
  114. ;;;
  115. ;;; Networking services.
  116. ;;;
  117. ;;; Code:
  118. (define %facebook-host-aliases
  119. ;; This is the list of known Facebook hosts to be added to /etc/hosts if you
  120. ;; are to block it.
  121. "\
  122. # Block Facebook IPv4.
  123. 127.0.0.1 www.facebook.com
  124. 127.0.0.1 facebook.com
  125. 127.0.0.1 login.facebook.com
  126. 127.0.0.1 www.login.facebook.com
  127. 127.0.0.1 fbcdn.net
  128. 127.0.0.1 www.fbcdn.net
  129. 127.0.0.1 fbcdn.com
  130. 127.0.0.1 www.fbcdn.com
  131. 127.0.0.1 static.ak.fbcdn.net
  132. 127.0.0.1 static.ak.connect.facebook.com
  133. 127.0.0.1 connect.facebook.net
  134. 127.0.0.1 www.connect.facebook.net
  135. 127.0.0.1 apps.facebook.com
  136. # Block Facebook IPv6.
  137. fe80::1%lo0 facebook.com
  138. fe80::1%lo0 login.facebook.com
  139. fe80::1%lo0 www.login.facebook.com
  140. fe80::1%lo0 fbcdn.net
  141. fe80::1%lo0 www.fbcdn.net
  142. fe80::1%lo0 fbcdn.com
  143. fe80::1%lo0 www.fbcdn.com
  144. fe80::1%lo0 static.ak.fbcdn.net
  145. fe80::1%lo0 static.ak.connect.facebook.com
  146. fe80::1%lo0 connect.facebook.net
  147. fe80::1%lo0 www.connect.facebook.net
  148. fe80::1%lo0 apps.facebook.com\n")
  149. (define dhcp-client-service-type
  150. (shepherd-service-type
  151. 'dhcp-client
  152. (lambda (dhcp)
  153. (define dhclient
  154. (file-append dhcp "/sbin/dhclient"))
  155. (define pid-file
  156. "/var/run/dhclient.pid")
  157. (shepherd-service
  158. (documentation "Set up networking via DHCP.")
  159. (requirement '(user-processes udev))
  160. ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when
  161. ;; networking is unavailable, but also means that the interface is not up
  162. ;; yet when 'start' completes. To wait for the interface to be ready, one
  163. ;; should instead monitor udev events.
  164. (provision '(networking))
  165. (start #~(lambda _
  166. ;; When invoked without any arguments, 'dhclient' discovers all
  167. ;; non-loopback interfaces *that are up*. However, the relevant
  168. ;; interfaces are typically down at this point. Thus we perform
  169. ;; our own interface discovery here.
  170. (define valid?
  171. (negate loopback-network-interface?))
  172. (define ifaces
  173. (filter valid? (all-network-interface-names)))
  174. ;; XXX: Make sure the interfaces are up so that 'dhclient' can
  175. ;; actually send/receive over them.
  176. (for-each set-network-interface-up ifaces)
  177. (false-if-exception (delete-file #$pid-file))
  178. (let ((pid (fork+exec-command
  179. (cons* #$dhclient "-nw"
  180. "-pf" #$pid-file ifaces))))
  181. (and (zero? (cdr (waitpid pid)))
  182. (read-pid-file #$pid-file)))))
  183. (stop #~(make-kill-destructor))))
  184. isc-dhcp))
  185. (define-deprecated (dhcp-client-service #:key (dhcp isc-dhcp))
  186. dhcp-client-service-type
  187. "Return a service that runs @var{dhcp}, a Dynamic Host Configuration
  188. Protocol (DHCP) client, on all the non-loopback network interfaces."
  189. (service dhcp-client-service-type dhcp))
  190. (define-record-type* <dhcpd-configuration>
  191. dhcpd-configuration make-dhcpd-configuration
  192. dhcpd-configuration?
  193. (package dhcpd-configuration-package ;<package>
  194. (default isc-dhcp))
  195. (config-file dhcpd-configuration-config-file ;file-like
  196. (default #f))
  197. (version dhcpd-configuration-version ;"4", "6", or "4o6"
  198. (default "4"))
  199. (run-directory dhcpd-configuration-run-directory
  200. (default "/run/dhcpd"))
  201. (lease-file dhcpd-configuration-lease-file
  202. (default "/var/db/dhcpd.leases"))
  203. (pid-file dhcpd-configuration-pid-file
  204. (default "/run/dhcpd/dhcpd.pid"))
  205. ;; list of strings, e.g. (list "enp0s25")
  206. (interfaces dhcpd-configuration-interfaces
  207. (default '())))
  208. (define dhcpd-shepherd-service
  209. (match-lambda
  210. (($ <dhcpd-configuration> package config-file version run-directory
  211. lease-file pid-file interfaces)
  212. (unless config-file
  213. (error "Must supply a config-file"))
  214. (list (shepherd-service
  215. ;; Allow users to easily run multiple versions simultaneously.
  216. (provision (list (string->symbol
  217. (string-append "dhcpv" version "-daemon"))))
  218. (documentation (string-append "Run the DHCPv" version " daemon"))
  219. (requirement '(networking))
  220. (start #~(make-forkexec-constructor
  221. '(#$(file-append package "/sbin/dhcpd")
  222. #$(string-append "-" version)
  223. "-lf" #$lease-file
  224. "-pf" #$pid-file
  225. "-cf" #$config-file
  226. #$@interfaces)
  227. #:pid-file #$pid-file))
  228. (stop #~(make-kill-destructor)))))))
  229. (define dhcpd-activation
  230. (match-lambda
  231. (($ <dhcpd-configuration> package config-file version run-directory
  232. lease-file pid-file interfaces)
  233. (with-imported-modules '((guix build utils))
  234. #~(begin
  235. (unless (file-exists? #$run-directory)
  236. (mkdir #$run-directory))
  237. ;; According to the DHCP manual (man dhcpd.leases), the lease
  238. ;; database must be present for dhcpd to start successfully.
  239. (unless (file-exists? #$lease-file)
  240. (with-output-to-file #$lease-file
  241. (lambda _ (display ""))))
  242. ;; Validate the config.
  243. (invoke
  244. #$(file-append package "/sbin/dhcpd") "-t" "-cf"
  245. #$config-file))))))
  246. (define dhcpd-service-type
  247. (service-type
  248. (name 'dhcpd)
  249. (extensions
  250. (list (service-extension shepherd-root-service-type dhcpd-shepherd-service)
  251. (service-extension activation-service-type dhcpd-activation)))))
  252. (define %ntp-servers
  253. ;; Default set of NTP servers. These URLs are managed by the NTP Pool project.
  254. ;; Within Guix, Leo Famulari <leo@famulari.name> is the administrative contact
  255. ;; for this NTP pool "zone".
  256. '("0.guix.pool.ntp.org"
  257. "1.guix.pool.ntp.org"
  258. "2.guix.pool.ntp.org"
  259. "3.guix.pool.ntp.org"))
  260. ;;;
  261. ;;; NTP.
  262. ;;;
  263. ;; TODO: Export.
  264. (define-record-type* <ntp-configuration>
  265. ntp-configuration make-ntp-configuration
  266. ntp-configuration?
  267. (ntp ntp-configuration-ntp
  268. (default ntp))
  269. (servers ntp-configuration-servers
  270. (default %ntp-servers))
  271. (allow-large-adjustment? ntp-allow-large-adjustment?
  272. (default #f)))
  273. (define ntp-shepherd-service
  274. (match-lambda
  275. (($ <ntp-configuration> ntp servers allow-large-adjustment?)
  276. (let ()
  277. ;; TODO: Add authentication support.
  278. (define config
  279. (string-append "driftfile /var/run/ntpd/ntp.drift\n"
  280. (string-join (map (cut string-append "server " <>)
  281. servers)
  282. "\n")
  283. "
  284. # Disable status queries as a workaround for CVE-2013-5211:
  285. # <http://support.ntp.org/bin/view/Main/SecurityNotice#DRDoS_Amplification_Attack_using>.
  286. restrict default kod nomodify notrap nopeer noquery
  287. restrict -6 default kod nomodify notrap nopeer noquery
  288. # Yet, allow use of the local 'ntpq'.
  289. restrict 127.0.0.1
  290. restrict -6 ::1\n"))
  291. (define ntpd.conf
  292. (plain-file "ntpd.conf" config))
  293. (list (shepherd-service
  294. (provision '(ntpd))
  295. (documentation "Run the Network Time Protocol (NTP) daemon.")
  296. (requirement '(user-processes networking))
  297. (start #~(make-forkexec-constructor
  298. (list (string-append #$ntp "/bin/ntpd") "-n"
  299. "-c" #$ntpd.conf "-u" "ntpd"
  300. #$@(if allow-large-adjustment?
  301. '("-g")
  302. '()))))
  303. (stop #~(make-kill-destructor))))))))
  304. (define %ntp-accounts
  305. (list (user-account
  306. (name "ntpd")
  307. (group "nogroup")
  308. (system? #t)
  309. (comment "NTP daemon user")
  310. (home-directory "/var/empty")
  311. (shell (file-append shadow "/sbin/nologin")))))
  312. (define (ntp-service-activation config)
  313. "Return the activation gexp for CONFIG."
  314. (with-imported-modules '((guix build utils))
  315. #~(begin
  316. (use-modules (guix build utils))
  317. (define %user
  318. (getpw "ntpd"))
  319. (let ((directory "/var/run/ntpd"))
  320. (mkdir-p directory)
  321. (chown directory (passwd:uid %user) (passwd:gid %user))))))
  322. (define ntp-service-type
  323. (service-type (name 'ntp)
  324. (extensions
  325. (list (service-extension shepherd-root-service-type
  326. ntp-shepherd-service)
  327. (service-extension account-service-type
  328. (const %ntp-accounts))
  329. (service-extension activation-service-type
  330. ntp-service-activation)))
  331. (description
  332. "Run the @command{ntpd}, the Network Time Protocol (NTP)
  333. daemon of the @uref{http://www.ntp.org, Network Time Foundation}. The daemon
  334. will keep the system clock synchronized with that of the given servers.")
  335. (default-value (ntp-configuration))))
  336. (define-deprecated (ntp-service #:key (ntp ntp)
  337. (servers %ntp-servers)
  338. allow-large-adjustment?)
  339. ntp-service-type
  340. "Return a service that runs the daemon from @var{ntp}, the
  341. @uref{http://www.ntp.org, Network Time Protocol package}. The daemon will
  342. keep the system clock synchronized with that of @var{servers}.
  343. @var{allow-large-adjustment?} determines whether @command{ntpd} is allowed to
  344. make an initial adjustment of more than 1,000 seconds."
  345. (service ntp-service-type
  346. (ntp-configuration (ntp ntp)
  347. (servers servers)
  348. (allow-large-adjustment?
  349. allow-large-adjustment?))))
  350. ;;;
  351. ;;; OpenNTPD.
  352. ;;;
  353. (define-record-type* <openntpd-configuration>
  354. openntpd-configuration make-openntpd-configuration
  355. openntpd-configuration?
  356. (openntpd openntpd-configuration-openntpd
  357. (default openntpd))
  358. (listen-on openntpd-listen-on
  359. (default '("127.0.0.1"
  360. "::1")))
  361. (query-from openntpd-query-from
  362. (default '()))
  363. (sensor openntpd-sensor
  364. (default '()))
  365. (server openntpd-server
  366. (default %ntp-servers))
  367. (servers openntpd-servers
  368. (default '()))
  369. (constraint-from openntpd-constraint-from
  370. (default '()))
  371. (constraints-from openntpd-constraints-from
  372. (default '()))
  373. (allow-large-adjustment? openntpd-allow-large-adjustment?
  374. (default #f))) ; upstream default
  375. (define (openntpd-shepherd-service config)
  376. (match-record config <openntpd-configuration>
  377. (openntpd listen-on query-from sensor server servers constraint-from
  378. constraints-from allow-large-adjustment?)
  379. (let ()
  380. (define config
  381. (string-join
  382. (filter-map
  383. (lambda (field value)
  384. (string-join
  385. (map (cut string-append field <> "\n")
  386. value)))
  387. '("listen on " "query from " "sensor " "server " "servers "
  388. "constraint from ")
  389. (list listen-on query-from sensor server servers constraint-from))
  390. ;; The 'constraints from' field needs to be enclosed in double quotes.
  391. (string-join
  392. (map (cut string-append "constraints from \"" <> "\"\n")
  393. constraints-from))))
  394. (define ntpd.conf
  395. (plain-file "ntpd.conf" config))
  396. (list (shepherd-service
  397. (provision '(ntpd))
  398. (documentation "Run the Network Time Protocol (NTP) daemon.")
  399. (requirement '(user-processes networking))
  400. (start #~(make-forkexec-constructor
  401. (list (string-append #$openntpd "/sbin/ntpd")
  402. "-f" #$ntpd.conf
  403. "-d" ;; don't daemonize
  404. #$@(if allow-large-adjustment?
  405. '("-s")
  406. '()))
  407. ;; When ntpd is daemonized it repeatedly tries to respawn
  408. ;; while running, leading shepherd to disable it. To
  409. ;; prevent spamming stderr, redirect output to logfile.
  410. #:log-file "/var/log/ntpd"))
  411. (stop #~(make-kill-destructor)))))))
  412. (define (openntpd-service-activation config)
  413. "Return the activation gexp for CONFIG."
  414. (with-imported-modules '((guix build utils))
  415. #~(begin
  416. (use-modules (guix build utils))
  417. (mkdir-p "/var/db")
  418. (mkdir-p "/var/run")
  419. (unless (file-exists? "/var/db/ntpd.drift")
  420. (with-output-to-file "/var/db/ntpd.drift"
  421. (lambda _
  422. (format #t "0.0")))))))
  423. (define openntpd-service-type
  424. (service-type (name 'openntpd)
  425. (extensions
  426. (list (service-extension shepherd-root-service-type
  427. openntpd-shepherd-service)
  428. (service-extension account-service-type
  429. (const %ntp-accounts))
  430. (service-extension profile-service-type
  431. (compose list openntpd-configuration-openntpd))
  432. (service-extension activation-service-type
  433. openntpd-service-activation)))
  434. (default-value (openntpd-configuration))
  435. (description
  436. "Run the @command{ntpd}, the Network Time Protocol (NTP)
  437. daemon, as implemented by @uref{http://www.openntpd.org, OpenNTPD}. The
  438. daemon will keep the system clock synchronized with that of the given servers.")))
  439. ;;;
  440. ;;; Inetd.
  441. ;;;
  442. (define-record-type* <inetd-configuration> inetd-configuration
  443. make-inetd-configuration
  444. inetd-configuration?
  445. (program inetd-configuration-program ;file-like
  446. (default (file-append inetutils "/libexec/inetd")))
  447. (entries inetd-configuration-entries ;list of <inetd-entry>
  448. (default '())))
  449. (define-record-type* <inetd-entry> inetd-entry make-inetd-entry
  450. inetd-entry?
  451. (node inetd-entry-node ;string or #f
  452. (default #f))
  453. (name inetd-entry-name) ;string, from /etc/services
  454. (socket-type inetd-entry-socket-type) ;stream | dgram | raw |
  455. ;rdm | seqpacket
  456. (protocol inetd-entry-protocol) ;string, from /etc/protocols
  457. (wait? inetd-entry-wait? ;Boolean
  458. (default #t))
  459. (user inetd-entry-user) ;string
  460. (program inetd-entry-program ;string or file-like object
  461. (default "internal"))
  462. (arguments inetd-entry-arguments ;list of strings or file-like objects
  463. (default '())))
  464. (define (inetd-config-file entries)
  465. (apply mixed-text-file "inetd.conf"
  466. (map
  467. (lambda (entry)
  468. (let* ((node (inetd-entry-node entry))
  469. (name (inetd-entry-name entry))
  470. (socket
  471. (if node (string-append node ":" name) name))
  472. (type
  473. (match (inetd-entry-socket-type entry)
  474. ((or 'stream 'dgram 'raw 'rdm 'seqpacket)
  475. (symbol->string (inetd-entry-socket-type entry)))))
  476. (protocol (inetd-entry-protocol entry))
  477. (wait (if (inetd-entry-wait? entry) "wait" "nowait"))
  478. (user (inetd-entry-user entry))
  479. (program (inetd-entry-program entry))
  480. (args (inetd-entry-arguments entry)))
  481. #~(string-append
  482. (string-join
  483. (list #$@(list socket type protocol wait user program) #$@args)
  484. " ") "\n")))
  485. entries)))
  486. (define inetd-shepherd-service
  487. (match-lambda
  488. (($ <inetd-configuration> program ()) '()) ; empty list of entries -> do nothing
  489. (($ <inetd-configuration> program entries)
  490. (list
  491. (shepherd-service
  492. (documentation "Run inetd.")
  493. (provision '(inetd))
  494. (requirement '(user-processes networking syslogd))
  495. (start #~(make-forkexec-constructor
  496. (list #$program #$(inetd-config-file entries))
  497. #:pid-file "/var/run/inetd.pid"))
  498. (stop #~(make-kill-destructor)))))))
  499. (define-public inetd-service-type
  500. (service-type
  501. (name 'inetd)
  502. (extensions
  503. (list (service-extension shepherd-root-service-type
  504. inetd-shepherd-service)))
  505. ;; The service can be extended with additional lists of entries.
  506. (compose concatenate)
  507. (extend (lambda (config entries)
  508. (inetd-configuration
  509. (inherit config)
  510. (entries (append (inetd-configuration-entries config)
  511. entries)))))
  512. (description
  513. "Start @command{inetd}, the @dfn{Internet superserver}. It is responsible
  514. for listening on Internet sockets and spawning the corresponding services on
  515. demand.")))
  516. ;;;
  517. ;;; Tor.
  518. ;;;
  519. (define-record-type* <tor-configuration>
  520. tor-configuration make-tor-configuration
  521. tor-configuration?
  522. (tor tor-configuration-tor
  523. (default tor))
  524. (config-file tor-configuration-config-file
  525. (default (plain-file "empty" "")))
  526. (hidden-services tor-configuration-hidden-services
  527. (default '()))
  528. (socks-socket-type tor-configuration-socks-socket-type ; 'tcp or 'unix
  529. (default 'tcp)))
  530. (define %tor-accounts
  531. ;; User account and groups for Tor.
  532. (list (user-group (name "tor") (system? #t))
  533. (user-account
  534. (name "tor")
  535. (group "tor")
  536. (system? #t)
  537. (comment "Tor daemon user")
  538. (home-directory "/var/empty")
  539. (shell (file-append shadow "/sbin/nologin")))))
  540. (define-record-type <hidden-service>
  541. (hidden-service name mapping)
  542. hidden-service?
  543. (name hidden-service-name) ;string
  544. (mapping hidden-service-mapping)) ;list of port/address tuples
  545. (define (tor-configuration->torrc config)
  546. "Return a 'torrc' file for CONFIG."
  547. (match config
  548. (($ <tor-configuration> tor config-file services socks-socket-type)
  549. (computed-file
  550. "torrc"
  551. (with-imported-modules '((guix build utils))
  552. #~(begin
  553. (use-modules (guix build utils)
  554. (ice-9 match))
  555. (call-with-output-file #$output
  556. (lambda (port)
  557. (display "\
  558. ### These lines were generated from your system configuration:
  559. User tor
  560. DataDirectory /var/lib/tor
  561. PidFile /var/run/tor/tor.pid
  562. Log notice syslog\n" port)
  563. (when (eq? 'unix '#$socks-socket-type)
  564. (display "\
  565. SocksPort unix:/var/run/tor/socks-sock
  566. UnixSocksGroupWritable 1\n" port))
  567. (for-each (match-lambda
  568. ((service (ports hosts) ...)
  569. (format port "\
  570. HiddenServiceDir /var/lib/tor/hidden-services/~a~%"
  571. service)
  572. (for-each (lambda (tcp-port host)
  573. (format port "\
  574. HiddenServicePort ~a ~a~%"
  575. tcp-port host))
  576. ports hosts)))
  577. '#$(map (match-lambda
  578. (($ <hidden-service> name mapping)
  579. (cons name mapping)))
  580. services))
  581. (display "\
  582. ### End of automatically generated lines.\n\n" port)
  583. ;; Append the user's config file.
  584. (call-with-input-file #$config-file
  585. (lambda (input)
  586. (dump-port input port)))
  587. #t))))))))
  588. (define (tor-shepherd-service config)
  589. "Return a <shepherd-service> running Tor."
  590. (match config
  591. (($ <tor-configuration> tor)
  592. (let ((torrc (tor-configuration->torrc config)))
  593. (with-imported-modules (source-module-closure
  594. '((gnu build shepherd)
  595. (gnu system file-systems)))
  596. (list (shepherd-service
  597. (provision '(tor))
  598. ;; Tor needs at least one network interface to be up, hence the
  599. ;; dependency on 'loopback'.
  600. (requirement '(user-processes loopback syslogd))
  601. (modules '((gnu build shepherd)
  602. (gnu system file-systems)))
  603. (start #~(make-forkexec-constructor/container
  604. (list #$(file-append tor "/bin/tor") "-f" #$torrc)
  605. #:mappings (list (file-system-mapping
  606. (source "/var/lib/tor")
  607. (target source)
  608. (writable? #t))
  609. (file-system-mapping
  610. (source "/dev/log") ;for syslog
  611. (target source))
  612. (file-system-mapping
  613. (source "/var/run/tor")
  614. (target source)
  615. (writable? #t)))
  616. #:pid-file "/var/run/tor/tor.pid"))
  617. (stop #~(make-kill-destructor))
  618. (documentation "Run the Tor anonymous network overlay."))))))))
  619. (define (tor-activation config)
  620. "Set up directories for Tor and its hidden services, if any."
  621. #~(begin
  622. (use-modules (guix build utils))
  623. (define %user
  624. (getpw "tor"))
  625. (define (initialize service)
  626. (let ((directory (string-append "/var/lib/tor/hidden-services/"
  627. service)))
  628. (mkdir-p directory)
  629. (chown directory (passwd:uid %user) (passwd:gid %user))
  630. ;; The daemon bails out if we give wider permissions.
  631. (chmod directory #o700)))
  632. ;; Allow Tor to write its PID file.
  633. (mkdir-p "/var/run/tor")
  634. (chown "/var/run/tor" (passwd:uid %user) (passwd:gid %user))
  635. ;; Set the group permissions to rw so that if the system administrator
  636. ;; has specified UnixSocksGroupWritable=1 in their torrc file, members
  637. ;; of the "tor" group will be able to use the SOCKS socket.
  638. (chmod "/var/run/tor" #o750)
  639. ;; Allow Tor to access the hidden services' directories.
  640. (mkdir-p "/var/lib/tor")
  641. (chown "/var/lib/tor" (passwd:uid %user) (passwd:gid %user))
  642. (chmod "/var/lib/tor" #o700)
  643. ;; Make sure /var/lib is accessible to the 'tor' user.
  644. (chmod "/var/lib" #o755)
  645. (for-each initialize
  646. '#$(map hidden-service-name
  647. (tor-configuration-hidden-services config)))))
  648. (define tor-service-type
  649. (service-type (name 'tor)
  650. (extensions
  651. (list (service-extension shepherd-root-service-type
  652. tor-shepherd-service)
  653. (service-extension account-service-type
  654. (const %tor-accounts))
  655. (service-extension activation-service-type
  656. tor-activation)))
  657. ;; This can be extended with hidden services.
  658. (compose concatenate)
  659. (extend (lambda (config services)
  660. (tor-configuration
  661. (inherit config)
  662. (hidden-services
  663. (append (tor-configuration-hidden-services config)
  664. services)))))
  665. (default-value (tor-configuration))
  666. (description
  667. "Run the @uref{https://torproject.org, Tor} anonymous
  668. networking daemon.")))
  669. (define-deprecated (tor-service #:optional
  670. (config-file (plain-file "empty" ""))
  671. #:key (tor tor))
  672. tor-service-type
  673. "Return a service to run the @uref{https://torproject.org, Tor} anonymous
  674. networking daemon.
  675. The daemon runs as the @code{tor} unprivileged user. It is passed
  676. @var{config-file}, a file-like object, with an additional @code{User tor} line
  677. and lines for hidden services added via @code{tor-hidden-service}. Run
  678. @command{man tor} for information about the configuration file."
  679. (service tor-service-type
  680. (tor-configuration (tor tor)
  681. (config-file config-file))))
  682. (define tor-hidden-service-type
  683. ;; A type that extends Tor with hidden services.
  684. (service-type (name 'tor-hidden-service)
  685. (extensions
  686. (list (service-extension tor-service-type list)))
  687. (description
  688. "Define a new Tor @dfn{hidden service}.")))
  689. (define (tor-hidden-service name mapping)
  690. "Define a new Tor @dfn{hidden service} called @var{name} and implementing
  691. @var{mapping}. @var{mapping} is a list of port/host tuples, such as:
  692. @example
  693. '((22 \"127.0.0.1:22\")
  694. (80 \"127.0.0.1:8080\"))
  695. @end example
  696. In this example, port 22 of the hidden service is mapped to local port 22, and
  697. port 80 is mapped to local port 8080.
  698. This creates a @file{/var/lib/tor/hidden-services/@var{name}} directory, where
  699. the @file{hostname} file contains the @code{.onion} host name for the hidden
  700. service.
  701. See @uref{https://www.torproject.org/docs/tor-hidden-service.html.en, the Tor
  702. project's documentation} for more information."
  703. (service tor-hidden-service-type
  704. (hidden-service name mapping)))
  705. ;;;
  706. ;;; Wicd.
  707. ;;;
  708. (define %wicd-activation
  709. ;; Activation gexp for Wicd.
  710. #~(begin
  711. (use-modules (guix build utils))
  712. (mkdir-p "/etc/wicd")
  713. (let ((file-name "/etc/wicd/dhclient.conf.template.default"))
  714. (unless (file-exists? file-name)
  715. (copy-file (string-append #$wicd file-name)
  716. file-name)))
  717. ;; Wicd invokes 'wpa_supplicant', which needs this directory for its
  718. ;; named socket files.
  719. (mkdir-p "/var/run/wpa_supplicant")
  720. (chmod "/var/run/wpa_supplicant" #o750)))
  721. (define (wicd-shepherd-service wicd)
  722. "Return a shepherd service for WICD."
  723. (list (shepherd-service
  724. (documentation "Run the Wicd network manager.")
  725. (provision '(networking))
  726. (requirement '(user-processes dbus-system loopback))
  727. (start #~(make-forkexec-constructor
  728. (list (string-append #$wicd "/sbin/wicd")
  729. "--no-daemon")))
  730. (stop #~(make-kill-destructor)))))
  731. (define wicd-service-type
  732. (service-type (name 'wicd)
  733. (extensions
  734. (list (service-extension shepherd-root-service-type
  735. wicd-shepherd-service)
  736. (service-extension dbus-root-service-type
  737. list)
  738. (service-extension activation-service-type
  739. (const %wicd-activation))
  740. ;; Add Wicd to the global profile.
  741. (service-extension profile-service-type list)))
  742. (description
  743. "Run @url{https://launchpad.net/wicd,Wicd}, a network
  744. management daemon that aims to simplify wired and wireless networking.")))
  745. (define* (wicd-service #:key (wicd wicd))
  746. "Return a service that runs @url{https://launchpad.net/wicd,Wicd}, a network
  747. management daemon that aims to simplify wired and wireless networking.
  748. This service adds the @var{wicd} package to the global profile, providing
  749. several commands to interact with the daemon and configure networking:
  750. @command{wicd-client}, a graphical user interface, and the @command{wicd-cli}
  751. and @command{wicd-curses} user interfaces."
  752. (service wicd-service-type wicd))
  753. ;;;
  754. ;;; ModemManager
  755. ;;;
  756. (define-record-type* <modem-manager-configuration>
  757. modem-manager-configuration make-modem-manager-configuration
  758. modem-manager-configuration?
  759. (modem-manager modem-manager-configuration-modem-manager
  760. (default modem-manager)))
  761. ;;;
  762. ;;; NetworkManager
  763. ;;;
  764. (define-record-type* <network-manager-configuration>
  765. network-manager-configuration make-network-manager-configuration
  766. network-manager-configuration?
  767. (network-manager network-manager-configuration-network-manager
  768. (default network-manager))
  769. (dns network-manager-configuration-dns
  770. (default "default"))
  771. (vpn-plugins network-manager-vpn-plugins ;list of <package>
  772. (default '())))
  773. (define %network-manager-activation
  774. ;; Activation gexp for NetworkManager.
  775. #~(begin
  776. (use-modules (guix build utils))
  777. (mkdir-p "/etc/NetworkManager/system-connections")))
  778. (define (vpn-plugin-directory plugins)
  779. "Return a directory containing PLUGINS, the NM VPN plugins."
  780. (directory-union "network-manager-vpn-plugins" plugins))
  781. (define network-manager-environment
  782. (match-lambda
  783. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  784. ;; Define this variable in the global environment such that
  785. ;; "nmcli connection import type openvpn file foo.ovpn" works.
  786. `(("NM_VPN_PLUGIN_DIR"
  787. . ,(file-append (vpn-plugin-directory vpn-plugins)
  788. "/lib/NetworkManager/VPN"))))))
  789. (define network-manager-shepherd-service
  790. (match-lambda
  791. (($ <network-manager-configuration> network-manager dns vpn-plugins)
  792. (let ((conf (plain-file "NetworkManager.conf"
  793. (string-append "[main]\ndns=" dns "\n")))
  794. (vpn (vpn-plugin-directory vpn-plugins)))
  795. (list (shepherd-service
  796. (documentation "Run the NetworkManager.")
  797. (provision '(networking))
  798. (requirement '(user-processes dbus-system wpa-supplicant loopback))
  799. (start #~(make-forkexec-constructor
  800. (list (string-append #$network-manager
  801. "/sbin/NetworkManager")
  802. (string-append "--config=" #$conf)
  803. "--no-daemon")
  804. #:environment-variables
  805. (list (string-append "NM_VPN_PLUGIN_DIR=" #$vpn
  806. "/lib/NetworkManager/VPN"))))
  807. (stop #~(make-kill-destructor))))))))
  808. (define network-manager-service-type
  809. (let
  810. ((config->package
  811. (match-lambda
  812. (($ <network-manager-configuration> network-manager)
  813. (list network-manager)))))
  814. (service-type
  815. (name 'network-manager)
  816. (extensions
  817. (list (service-extension shepherd-root-service-type
  818. network-manager-shepherd-service)
  819. (service-extension dbus-root-service-type config->package)
  820. (service-extension polkit-service-type config->package)
  821. (service-extension activation-service-type
  822. (const %network-manager-activation))
  823. (service-extension session-environment-service-type
  824. network-manager-environment)
  825. ;; Add network-manager to the system profile.
  826. (service-extension profile-service-type config->package)))
  827. (default-value (network-manager-configuration))
  828. (description
  829. "Run @uref{https://wiki.gnome.org/Projects/NetworkManager,
  830. NetworkManager}, a network management daemon that aims to simplify wired and
  831. wireless networking."))))
  832. ;;;
  833. ;;; Connman
  834. ;;;
  835. (define-record-type* <connman-configuration>
  836. connman-configuration make-connman-configuration
  837. connman-configuration?
  838. (connman connman-configuration-connman
  839. (default connman))
  840. (disable-vpn? connman-configuration-disable-vpn?
  841. (default #f)))
  842. (define (connman-activation config)
  843. (let ((disable-vpn? (connman-configuration-disable-vpn? config)))
  844. (with-imported-modules '((guix build utils))
  845. #~(begin
  846. (use-modules (guix build utils))
  847. (mkdir-p "/var/lib/connman/")
  848. (unless #$disable-vpn?
  849. (mkdir-p "/var/lib/connman-vpn/"))))))
  850. (define (connman-shepherd-service config)
  851. "Return a shepherd service for Connman"
  852. (and
  853. (connman-configuration? config)
  854. (let ((connman (connman-configuration-connman config))
  855. (disable-vpn? (connman-configuration-disable-vpn? config)))
  856. (list (shepherd-service
  857. (documentation "Run Connman")
  858. (provision '(networking))
  859. (requirement
  860. '(user-processes dbus-system loopback wpa-supplicant))
  861. (start #~(make-forkexec-constructor
  862. (list (string-append #$connman
  863. "/sbin/connmand")
  864. "-n" "-r"
  865. #$@(if disable-vpn? '("--noplugin=vpn") '()))))
  866. (stop #~(make-kill-destructor)))))))
  867. (define connman-service-type
  868. (let ((connman-package (compose list connman-configuration-connman)))
  869. (service-type (name 'connman)
  870. (extensions
  871. (list (service-extension shepherd-root-service-type
  872. connman-shepherd-service)
  873. (service-extension polkit-service-type
  874. connman-package)
  875. (service-extension dbus-root-service-type
  876. connman-package)
  877. (service-extension activation-service-type
  878. connman-activation)
  879. ;; Add connman to the system profile.
  880. (service-extension profile-service-type
  881. connman-package)))
  882. (default-value (connman-configuration))
  883. (description
  884. "Run @url{https://01.org/connman,Connman},
  885. a network connection manager."))))
  886. ;;;
  887. ;;; Modem manager
  888. ;;;
  889. (define modem-manager-service-type
  890. (let ((config->package
  891. (match-lambda
  892. (($ <modem-manager-configuration> modem-manager)
  893. (list modem-manager)))))
  894. (service-type (name 'modem-manager)
  895. (extensions
  896. (list (service-extension dbus-root-service-type
  897. config->package)
  898. (service-extension udev-service-type
  899. config->package)
  900. (service-extension polkit-service-type
  901. config->package)))
  902. (default-value (modem-manager-configuration))
  903. (description
  904. "Run @uref{https://wiki.gnome.org/Projects/ModemManager,
  905. ModemManager}, a modem management daemon that aims to simplify dialup
  906. networking."))))
  907. ;;;
  908. ;;; WPA supplicant
  909. ;;;
  910. (define-record-type* <wpa-supplicant-configuration>
  911. wpa-supplicant-configuration make-wpa-supplicant-configuration
  912. wpa-supplicant-configuration?
  913. (wpa-supplicant wpa-supplicant-configuration-wpa-supplicant ;<package>
  914. (default wpa-supplicant))
  915. (pid-file wpa-supplicant-configuration-pid-file ;string
  916. (default "/var/run/wpa_supplicant.pid"))
  917. (dbus? wpa-supplicant-configuration-dbus? ;Boolean
  918. (default #t))
  919. (interface wpa-supplicant-configuration-interface ;#f | string
  920. (default #f))
  921. (config-file wpa-supplicant-configuration-config-file ;#f | <file-like>
  922. (default #f))
  923. (extra-options wpa-supplicant-configuration-extra-options ;list of strings
  924. (default '())))
  925. (define wpa-supplicant-shepherd-service
  926. (match-lambda
  927. (($ <wpa-supplicant-configuration> wpa-supplicant pid-file dbus? interface
  928. config-file extra-options)
  929. (list (shepherd-service
  930. (documentation "Run the WPA supplicant daemon")
  931. (provision '(wpa-supplicant))
  932. (requirement '(user-processes dbus-system loopback))
  933. (start #~(make-forkexec-constructor
  934. (list (string-append #$wpa-supplicant
  935. "/sbin/wpa_supplicant")
  936. (string-append "-P" #$pid-file)
  937. "-B" ;run in background
  938. #$@(if dbus?
  939. #~("-u")
  940. #~())
  941. #$@(if interface
  942. #~((string-append "-i" #$interface))
  943. #~())
  944. #$@(if config-file
  945. #~((string-append "-c" #$config-file))
  946. #~())
  947. #$@extra-options)
  948. #:pid-file #$pid-file))
  949. (stop #~(make-kill-destructor)))))))
  950. (define wpa-supplicant-service-type
  951. (let ((config->package
  952. (match-lambda
  953. (($ <wpa-supplicant-configuration> wpa-supplicant)
  954. (list wpa-supplicant)))))
  955. (service-type (name 'wpa-supplicant)
  956. (extensions
  957. (list (service-extension shepherd-root-service-type
  958. wpa-supplicant-shepherd-service)
  959. (service-extension dbus-root-service-type config->package)
  960. (service-extension profile-service-type config->package)))
  961. (description "Run the WPA Supplicant daemon, a service that
  962. implements authentication, key negotiation and more for wireless networks.")
  963. (default-value (wpa-supplicant-configuration)))))
  964. ;;;
  965. ;;; Open vSwitch
  966. ;;;
  967. (define-record-type* <openvswitch-configuration>
  968. openvswitch-configuration make-openvswitch-configuration
  969. openvswitch-configuration?
  970. (package openvswitch-configuration-package
  971. (default openvswitch)))
  972. (define openvswitch-activation
  973. (match-lambda
  974. (($ <openvswitch-configuration> package)
  975. (let ((ovsdb-tool (file-append package "/bin/ovsdb-tool")))
  976. (with-imported-modules '((guix build utils))
  977. #~(begin
  978. (use-modules (guix build utils))
  979. (mkdir-p "/var/run/openvswitch")
  980. (mkdir-p "/var/lib/openvswitch")
  981. (let ((conf.db "/var/lib/openvswitch/conf.db"))
  982. (unless (file-exists? conf.db)
  983. (system* #$ovsdb-tool "create" conf.db)))))))))
  984. (define openvswitch-shepherd-service
  985. (match-lambda
  986. (($ <openvswitch-configuration> package)
  987. (let ((ovsdb-server (file-append package "/sbin/ovsdb-server"))
  988. (ovs-vswitchd (file-append package "/sbin/ovs-vswitchd")))
  989. (list
  990. (shepherd-service
  991. (provision '(ovsdb))
  992. (documentation "Run the Open vSwitch database server.")
  993. (start #~(make-forkexec-constructor
  994. (list #$ovsdb-server "--pidfile"
  995. "--remote=punix:/var/run/openvswitch/db.sock")
  996. #:pid-file "/var/run/openvswitch/ovsdb-server.pid"))
  997. (stop #~(make-kill-destructor)))
  998. (shepherd-service
  999. (provision '(vswitchd))
  1000. (requirement '(ovsdb))
  1001. (documentation "Run the Open vSwitch daemon.")
  1002. (start #~(make-forkexec-constructor
  1003. (list #$ovs-vswitchd "--pidfile")
  1004. #:pid-file "/var/run/openvswitch/ovs-vswitchd.pid"))
  1005. (stop #~(make-kill-destructor))))))))
  1006. (define openvswitch-service-type
  1007. (service-type
  1008. (name 'openvswitch)
  1009. (extensions
  1010. (list (service-extension activation-service-type
  1011. openvswitch-activation)
  1012. (service-extension profile-service-type
  1013. (compose list openvswitch-configuration-package))
  1014. (service-extension shepherd-root-service-type
  1015. openvswitch-shepherd-service)))
  1016. (description
  1017. "Run @uref{http://www.openvswitch.org, Open vSwitch}, a multilayer virtual
  1018. switch designed to enable massive network automation through programmatic
  1019. extension.")))
  1020. ;;;
  1021. ;;; iptables
  1022. ;;;
  1023. (define %iptables-accept-all-rules
  1024. (plain-file "iptables-accept-all.rules"
  1025. "*filter
  1026. :INPUT ACCEPT
  1027. :FORWARD ACCEPT
  1028. :OUTPUT ACCEPT
  1029. COMMIT
  1030. "))
  1031. (define-record-type* <iptables-configuration>
  1032. iptables-configuration make-iptables-configuration iptables-configuration?
  1033. (iptables iptables-configuration-iptables
  1034. (default iptables))
  1035. (ipv4-rules iptables-configuration-ipv4-rules
  1036. (default %iptables-accept-all-rules))
  1037. (ipv6-rules iptables-configuration-ipv6-rules
  1038. (default %iptables-accept-all-rules)))
  1039. (define iptables-shepherd-service
  1040. (match-lambda
  1041. (($ <iptables-configuration> iptables ipv4-rules ipv6-rules)
  1042. (let ((iptables-restore (file-append iptables "/sbin/iptables-restore"))
  1043. (ip6tables-restore (file-append iptables "/sbin/ip6tables-restore")))
  1044. (shepherd-service
  1045. (documentation "Packet filtering framework")
  1046. (provision '(iptables))
  1047. (start #~(lambda _
  1048. (invoke #$iptables-restore #$ipv4-rules)
  1049. (invoke #$ip6tables-restore #$ipv6-rules)))
  1050. (stop #~(lambda _
  1051. (invoke #$iptables-restore #$%iptables-accept-all-rules)
  1052. (invoke #$ip6tables-restore #$%iptables-accept-all-rules))))))))
  1053. (define iptables-service-type
  1054. (service-type
  1055. (name 'iptables)
  1056. (description
  1057. "Run @command{iptables-restore}, setting up the specified rules.")
  1058. (extensions
  1059. (list (service-extension shepherd-root-service-type
  1060. (compose list iptables-shepherd-service))))))
  1061. ;;; networking.scm ends here