telephony.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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="
  178. (number->string max-user-bandwidth) "\n")
  179. '())
  180. "users=" (number->string max-users) "\n"
  181. "uname=" user "\n"
  182. "database=" database-file "\n"
  183. "logfile=" log-file "\n"
  184. "pidfile=" pid-file "\n"
  185. (if autoban-attempts (list "autobanAttempts=" (number->string autoban-attempts) "\n") '())
  186. (if autoban-timeframe (list "autobanTimeframe=" (number->string autoban-timeframe) "\n") '())
  187. (if autoban-time (list "autobanTime=" (number->string autoban-time) "\n") '())
  188. (if opus-threshold (list "opusthreshold=" (number->string opus-threshold) "\n") '())
  189. (if channel-nesting-limit (list "channelnestinglimit=" (number->string channel-nesting-limit) "\n") '())
  190. (if channelname-regex (list "channelname=" channelname-regex "\n") '())
  191. (if username-regex (list "username=" username-regex "\n") '())
  192. (if text-message-length (list "textmessagelength=" (number->string text-message-length) "\n") '())
  193. (if image-message-length (list "imagemessagelength=" (number->string image-message-length) "\n") '())
  194. (if log-days (list "logdays=" (number->string log-days) "\n") '())
  195. "obfuscate=" (if obfuscate-ips? "true" "false") "\n"
  196. "certrequired=" (if cert-required? "true" "false") "\n"
  197. "rememberchannel=" (if remember-channel? "true" "false") "\n"
  198. "allowhtml=" (if allow-html? "true" "false") "\n"
  199. "allowping=" (if allow-ping? "true" "false") "\n"
  200. "bonjour=" (if bonjour? "true" "false") "\n"
  201. "sendversion=" (if send-version? "true" "false") "\n"
  202. (cond ((and ssl-cert ssl-key)
  203. (list
  204. "sslCert=" ssl-cert "\n"
  205. "sslKey=" ssl-key "\n"))
  206. ((or ssl-cert ssl-key)
  207. (error "ssl-cert and ssl-key must both be set"
  208. ssl-cert ssl-key))
  209. (else '()))
  210. (if ssl-dh-params (list "sslDHParams=" ssl-dh-params) '())
  211. (if ssl-ciphers (list "sslCiphers=" ssl-ciphers) '())
  212. (match public-registration
  213. (#f '())
  214. (($ <murmur-public-registration-configuration>
  215. name password url hostname)
  216. (if (and (or (not server-password) (string-null? server-password))
  217. allow-ping?)
  218. (list
  219. "registerName=" name "\n"
  220. "registerPassword=" password "\n"
  221. "registerUrl=" url "\n"
  222. (if hostname
  223. (string-append "registerHostname=" hostname "\n")
  224. ""))
  225. (error "To publicly register your murmur server your server must be publicy visible
  226. and users must be able to join without a password. To fix this set:
  227. (allow-ping? #t)
  228. (server-password \"\")
  229. Or set public-registration to #f"))))))))
  230. (define (murmur-activation config)
  231. #~(begin
  232. (use-modules (guix build utils))
  233. (let* ((log-dir (dirname #$(murmur-configuration-log-file config)))
  234. (pid-dir (dirname #$(murmur-configuration-pid-file config)))
  235. (db-dir (dirname #$(murmur-configuration-database-file config)))
  236. (user (getpwnam #$(murmur-configuration-user config)))
  237. (init-dir
  238. (lambda (name dir)
  239. (format #t "creating murmur ~a directory '~a'\n" name dir)
  240. (mkdir-p dir)
  241. (chown dir (passwd:uid user) (passwd:gid user))
  242. (chmod dir #o700)))
  243. (ini #$(or (murmur-configuration-file config)
  244. (default-murmur-config config))))
  245. (init-dir "log" log-dir)
  246. (init-dir "pid" pid-dir)
  247. (init-dir "database" db-dir)
  248. (format #t "murmur: use config file: ~a~%\n" ini)
  249. (format #t "murmur: to set the SuperUser password run:
  250. `~a -ini ~a -readsupw`\n"
  251. #$(file-append (murmur-configuration-package config)
  252. "/bin/murmurd") ini)
  253. #t)))
  254. (define murmur-accounts
  255. (match-lambda
  256. (($ <murmur-configuration> _ user group)
  257. (list
  258. (user-group
  259. (name group)
  260. (system? #t))
  261. (user-account
  262. (name user)
  263. (group group)
  264. (system? #t)
  265. (comment "Murmur Daemon")
  266. (home-directory "/var/empty")
  267. (shell (file-append shadow "/sbin/nologin")))))))
  268. (define (murmur-shepherd-service config)
  269. (list (shepherd-service
  270. (provision '(murmur))
  271. (documentation "Run the Murmur Mumble server.")
  272. (requirement '(networking))
  273. (start #~(make-forkexec-constructor
  274. '(#$(file-append (murmur-configuration-package config)
  275. "/bin/murmurd")
  276. "-ini"
  277. #$(or (murmur-configuration-file config)
  278. (default-murmur-config config)))
  279. #:pid-file #$(murmur-configuration-pid-file config)))
  280. (stop #~(make-kill-destructor)))))
  281. (define murmur-service-type
  282. (service-type (name 'murmur)
  283. (description
  284. "Run the Murmur voice-over-IP (VoIP) server of the Mumble
  285. suite.")
  286. (extensions
  287. (list (service-extension shepherd-root-service-type
  288. murmur-shepherd-service)
  289. (service-extension activation-service-type
  290. murmur-activation)
  291. (service-extension account-service-type
  292. murmur-accounts)))
  293. (default-value (murmur-configuration))))