networking.scm 44 KB

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