networking.scm 43 KB

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