telephony.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 nee <nee-git@hidamari.blue>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu services telephony)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services shepherd)
  21. #:use-module (gnu system shadow)
  22. #:use-module (gnu packages admin)
  23. #:use-module (gnu packages telephony)
  24. #:use-module (guix records)
  25. #:use-module (guix gexp)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (ice-9 match)
  28. #:export (murmur-configuration
  29. make-murmur-configuration
  30. murmur-configuration?
  31. murmur-configuration-package
  32. murmur-configuration-user
  33. murmur-configuration-group
  34. murmur-configuration-port
  35. murmur-configuration-welcome-text
  36. murmur-configuration-server-password
  37. murmur-configuration-max-users
  38. murmur-configuration-max-user-bandwidth
  39. murmur-configuration-database-file
  40. murmur-configuration-log-file
  41. murmur-configuration-pid-file
  42. murmur-configuration-autoban-attempts
  43. murmur-configuration-autoban-timeframe
  44. murmur-configuration-autoban-time
  45. murmur-configuration-opus-threshold
  46. murmur-configuration-channel-nesting-limit
  47. murmur-configuration-channelname-regex
  48. murmur-configuration-username-regex
  49. murmur-configuration-text-message-length
  50. murmur-configuration-image-message-length
  51. murmur-configuration-cert-required?
  52. murmur-configuration-remember-channel?
  53. murmur-configuration-allow-html?
  54. murmur-configuration-allow-ping?
  55. murmur-configuration-bonjour?
  56. murmur-configuration-send-version?
  57. murmur-configuration-log-days
  58. murmur-configuration-obfuscate-ips?
  59. murmur-configuration-ssl-cert
  60. murmur-configuration-ssl-key
  61. murmur-configuration-ssl-dh-params
  62. murmur-configuration-ssl-ciphers
  63. murmur-configuration-public-registration
  64. murmur-configuration-file
  65. murmur-public-registration-configuration
  66. make-murmur-public-registration-configuration
  67. murmur-public-registration-configuration?
  68. murmur-public-registration-configuration-name
  69. murmur-public-registration-configuration-url
  70. murmur-public-registration-configuration-password
  71. murmur-public-registration-configuration-hostname
  72. murmur-service-type))
  73. ;; https://github.com/mumble-voip/mumble/blob/master/scripts/murmur.ini
  74. (define-record-type* <murmur-configuration> murmur-configuration
  75. make-murmur-configuration
  76. murmur-configuration?
  77. (package murmur-configuration-package ;<package>
  78. (default mumble))
  79. (user murmur-configuration-user
  80. (default "murmur"))
  81. (group murmur-configuration-group
  82. (default "murmur"))
  83. (port murmur-configuration-port
  84. (default 64738))
  85. (welcome-text murmur-configuration-welcome-text
  86. (default ""))
  87. (server-password murmur-configuration-server-password
  88. (default ""))
  89. (max-users murmur-configuration-max-users
  90. (default 100))
  91. (max-user-bandwidth murmur-configuration-max-user-bandwidth
  92. (default #f))
  93. (database-file murmur-configuration-database-file
  94. (default "/var/lib/murmur/db.sqlite"))
  95. (log-file murmur-configuration-log-file
  96. (default "/var/log/murmur/murmur.log"))
  97. (pid-file murmur-configuration-pid-file
  98. (default "/var/run/murmur/murmur.pid"))
  99. (autoban-attempts murmur-configuration-autoban-attempts
  100. (default 10))
  101. (autoban-timeframe murmur-configuration-autoban-timeframe
  102. (default 120))
  103. (autoban-time murmur-configuration-autoban-time
  104. (default 300))
  105. (opus-threshold murmur-configuration-opus-threshold
  106. (default 100)) ; integer percent
  107. (channel-nesting-limit murmur-configuration-channel-nesting-limit
  108. (default 10))
  109. (channelname-regex murmur-configuration-channelname-regex
  110. (default #f))
  111. (username-regex murmur-configuration-username-regex
  112. (default #f))
  113. (text-message-length murmur-configuration-text-message-length
  114. (default 5000))
  115. (image-message-length murmur-configuration-image-message-length
  116. (default (* 128 1024))) ; 128 Kilobytes
  117. (cert-required? murmur-configuration-cert-required?
  118. (default #f))
  119. (remember-channel? murmur-configuration-remember-channel?
  120. (default #f))
  121. (allow-html? murmur-configuration-allow-html?
  122. (default #f))
  123. (allow-ping? murmur-configuration-allow-ping?
  124. (default #f))
  125. (bonjour? murmur-configuration-bonjour?
  126. (default #f))
  127. (send-version? murmur-configuration-send-version?
  128. (default #f))
  129. (log-days murmur-configuration-log-days
  130. (default 31))
  131. (obfuscate-ips? murmur-obfuscate-ips?
  132. (default #t))
  133. (ssl-cert murmur-configuration-ssl-cert
  134. (default #f))
  135. (ssl-key murmur-configuration-ssl-key
  136. (default #f))
  137. (ssl-dh-params murmur-configuration-ssl-dh-params
  138. (default #f))
  139. (ssl-ciphers murmur-configuration-ssl-ciphers
  140. (default #f))
  141. (public-registration murmur-configuration-public-registration
  142. (default #f)) ; <murmur-public-registration-configuration>
  143. (file murmur-configuration-file
  144. (default #f)))
  145. (define-record-type* <murmur-public-registration-configuration>
  146. murmur-public-registration-configuration
  147. make-murmur-public-registration-configuration
  148. murmur-public-registration-configuration?
  149. (name murmur-public-registration-configuration-name)
  150. (password murmur-public-registration-configuration-password)
  151. (url murmur-public-registration-configuration-url)
  152. (hostname murmur-public-registration-configuration-hostname
  153. (default #f)))
  154. (define (flatten . lst)
  155. "Return a list that recursively concatenates all sub-lists of LST."
  156. (define (flatten1 head out)
  157. (if (list? head)
  158. (fold-right flatten1 out head)
  159. (cons head out)))
  160. (fold-right flatten1 '() lst))
  161. (define (default-murmur-config config)
  162. (match-record
  163. config
  164. <murmur-configuration>
  165. (user port welcome-text server-password max-users max-user-bandwidth
  166. database-file log-file pid-file autoban-attempts autoban-timeframe
  167. autoban-time opus-threshold channel-nesting-limit channelname-regex
  168. username-regex text-message-length image-message-length cert-required?
  169. remember-channel? allow-html? allow-ping? bonjour? send-version?
  170. log-days obfuscate-ips? ssl-cert ssl-key ssl-dh-params ssl-ciphers
  171. public-registration)
  172. (apply mixed-text-file "murmur.ini"
  173. (flatten
  174. "welcometext=" welcome-text "\n"
  175. "port=" (number->string port) "\n"
  176. (if server-password (list "serverpassword=" server-password "\n") '())
  177. (if max-user-bandwidth (list "bandwidth=" (number->string max-user-bandwidth)) '())
  178. "users=" (number->string max-users) "\n"
  179. "uname=" user "\n"
  180. "database=" database-file "\n"
  181. "logfile=" log-file "\n"
  182. "pidfile=" pid-file "\n"
  183. (if autoban-attempts (list "autobanAttempts=" (number->string autoban-attempts) "\n") '())
  184. (if autoban-timeframe (list "autobanTimeframe=" (number->string autoban-timeframe) "\n") '())
  185. (if autoban-time (list "autobanTime=" (number->string autoban-time) "\n") '())
  186. (if opus-threshold (list "opusthreshold=" (number->string opus-threshold) "\n") '())
  187. (if channel-nesting-limit (list "channelnestinglimit=" (number->string channel-nesting-limit) "\n") '())
  188. (if channelname-regex (list "channelname=" channelname-regex "\n") '())
  189. (if username-regex (list "username=" username-regex "\n") '())
  190. (if text-message-length (list "textmessagelength=" (number->string text-message-length) "\n") '())
  191. (if image-message-length (list "imagemessagelength=" (number->string image-message-length) "\n") '())
  192. (if log-days (list "logdays=" (number->string log-days) "\n") '())
  193. "obfuscate=" (if obfuscate-ips? "true" "false") "\n"
  194. "certrequired=" (if cert-required? "true" "false") "\n"
  195. "rememberchannel=" (if remember-channel? "true" "false") "\n"
  196. "allowhtml=" (if allow-html? "true" "false") "\n"
  197. "allowping=" (if allow-ping? "true" "false") "\n"
  198. "bonjour=" (if bonjour? "true" "false") "\n"
  199. "sendversion=" (if send-version? "true" "false") "\n"
  200. (cond ((and ssl-cert ssl-key)
  201. (list
  202. "sslCert=" ssl-cert "\n"
  203. "sslKey=" ssl-key "\n"))
  204. ((or ssl-cert ssl-key)
  205. (error "ssl-cert and ssl-key must both be set"
  206. ssl-cert ssl-key))
  207. (else '()))
  208. (if ssl-dh-params (list "sslDHParams=" ssl-dh-params) '())
  209. (if ssl-ciphers (list "sslCiphers=" ssl-ciphers) '())
  210. (match public-registration
  211. (#f '())
  212. (($ <murmur-public-registration-configuration>
  213. name password url hostname)
  214. (if (and (or (not server-password) (string-null? server-password))
  215. allow-ping?)
  216. (list
  217. "registerName=" name "\n"
  218. "registerPassword=" password "\n"
  219. "registerUrl=" url "\n"
  220. (if hostname
  221. (string-append "registerHostname=" hostname "\n")
  222. ""))
  223. (error "To publicly register your murmur server your server must be publicy visible
  224. and users must be able to join without a password. To fix this set:
  225. (allow-ping? #t)
  226. (server-password \"\")
  227. Or set public-registration to #f"))))))))
  228. (define (murmur-activation config)
  229. #~(begin
  230. (use-modules (guix build utils))
  231. (let* ((log-dir (dirname #$(murmur-configuration-log-file config)))
  232. (pid-dir (dirname #$(murmur-configuration-pid-file config)))
  233. (db-dir (dirname #$(murmur-configuration-database-file config)))
  234. (user (getpwnam #$(murmur-configuration-user config)))
  235. (init-dir
  236. (lambda (name dir)
  237. (format #t "creating murmur ~a directory '~a'\n" name dir)
  238. (mkdir-p dir)
  239. (chown dir (passwd:uid user) (passwd:gid user))
  240. (chmod dir #o700)))
  241. (ini #$(or (murmur-configuration-file config)
  242. (default-murmur-config config))))
  243. (init-dir "log" log-dir)
  244. (init-dir "pid" pid-dir)
  245. (init-dir "database" db-dir)
  246. (format #t "murmur: use config file: ~a~%\n" ini)
  247. (format #t "murmur: to set the SuperUser password run:
  248. `~a -ini ~a -readsupw`\n"
  249. #$(file-append (murmur-configuration-package config)
  250. "/bin/murmurd") ini)
  251. #t)))
  252. (define murmur-accounts
  253. (match-lambda
  254. (($ <murmur-configuration> _ user group)
  255. (list
  256. (user-group
  257. (name group)
  258. (system? #t))
  259. (user-account
  260. (name user)
  261. (group group)
  262. (system? #t)
  263. (comment "Murmur Daemon")
  264. (home-directory "/var/empty")
  265. (shell (file-append shadow "/sbin/nologin")))))))
  266. (define (murmur-shepherd-service config)
  267. (list (shepherd-service
  268. (provision '(murmur))
  269. (documentation "Run the Murmur Mumble server.")
  270. (requirement '(networking))
  271. (start #~(make-forkexec-constructor
  272. '(#$(file-append (murmur-configuration-package config)
  273. "/bin/murmurd")
  274. "-ini"
  275. #$(or (murmur-configuration-file config)
  276. (default-murmur-config config)))
  277. #:pid-file #$(murmur-configuration-pid-file config)))
  278. (stop #~(make-kill-destructor)))))
  279. (define murmur-service-type
  280. (service-type (name 'murmur)
  281. (description
  282. "Run the Murmur voice-over-IP (VoIP) server of the Mumble
  283. suite.")
  284. (extensions
  285. (list (service-extension shepherd-root-service-type
  286. murmur-shepherd-service)
  287. (service-extension activation-service-type
  288. murmur-activation)
  289. (service-extension account-service-type
  290. murmur-accounts)))
  291. (default-value (murmur-configuration))))