samba.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Simon Streit <simon@netpanic.org>
  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 samba)
  19. #:use-module (gnu packages)
  20. #:use-module (gnu packages base)
  21. #:use-module (gnu packages admin)
  22. #:use-module (gnu packages samba)
  23. #:use-module (gnu services)
  24. #:use-module (gnu services shepherd)
  25. #:use-module (gnu services base)
  26. #:use-module (gnu system shadow)
  27. #:use-module (guix gexp)
  28. #:use-module (guix packages)
  29. #:use-module (guix modules)
  30. #:use-module (guix records)
  31. #:use-module (ice-9 format)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 textual-ports)
  34. #:use-module (srfi srfi-1)
  35. #:export (samba-service-type
  36. samba-configuration
  37. wsdd-service-type
  38. wsdd-configuration))
  39. (define-record-type* <samba-configuration>
  40. samba-configuration
  41. make-samba-configuration
  42. samba-configuration?
  43. (package samba-configuration-package
  44. (default samba))
  45. (config-file samba-configuration-config-file
  46. (default #f))
  47. (enable-samba? samba-configuration-enable-samba?
  48. (default #f))
  49. (enable-smbd? samba-configuration-enable-smbd?
  50. (default #t))
  51. (enable-nmbd? samba-configuration-enable-nmbd?
  52. (default #t))
  53. (enable-winbindd? samba-configuration-enable-winbindd?
  54. (default #t)))
  55. (define (samba-activation config)
  56. (let ((package (samba-configuration-package config))
  57. (config-file (samba-configuration-config-file config)))
  58. (with-imported-modules '((guix build utils))
  59. (let ((lib-dir "/var/lib/samba")
  60. (log-dir "/var/log/samba")
  61. (run-dir "/var/run/samba")
  62. (lock-dir "/var/lock/samba")
  63. (cache-dir "/var/cache/samba")
  64. (etc-dir "/etc/samba")
  65. (smb.conf "/etc/samba/smb.conf"))
  66. #~(begin
  67. (use-modules (guix build utils))
  68. (mkdir-p #$etc-dir)
  69. (mkdir-p #$lib-dir)
  70. (mkdir-p/perms (string-append #$lib-dir "/private")
  71. (getpwnam "root") #o700)
  72. (mkdir-p #$log-dir)
  73. (mkdir-p #$run-dir)
  74. (mkdir-p #$lock-dir)
  75. (mkdir-p #$cache-dir)
  76. (copy-file #$config-file #$smb.conf)
  77. (invoke #$(file-append package "/bin/testparm")
  78. "--suppress-prompt" #$smb.conf))))))
  79. (define (samba-samba-shepherd-service config)
  80. (let ((package (samba-configuration-package config))
  81. (config-file (samba-configuration-config-file config)))
  82. (list (shepherd-service
  83. (documentation "Run Samba")
  84. (provision '(samba-samba))
  85. (requirement '(networking))
  86. (start #~(make-forkexec-constructor
  87. (list #$(file-append package "/sbin/samba")
  88. (string-append "--configfile=" #$config-file)
  89. "--foreground"
  90. "--no-process-group")))
  91. (stop #~(make-kill-destructor))))))
  92. (define (samba-nmbd-shepherd-service config)
  93. (let ((package (samba-configuration-package config))
  94. (config-file (samba-configuration-config-file config)))
  95. (list (shepherd-service
  96. (documentation "Run NMBD")
  97. (provision '(samba-nmbd))
  98. (requirement '(networking))
  99. (start #~(make-forkexec-constructor
  100. (list #$(file-append package "/sbin/nmbd")
  101. (string-append "--configfile=" #$config-file)
  102. "--foreground"
  103. "--no-process-group")))
  104. (stop #~(make-kill-destructor))))))
  105. (define (samba-smbd-shepherd-service config)
  106. (let ((package (samba-configuration-package config))
  107. (config-file (samba-configuration-config-file config)))
  108. (list (shepherd-service
  109. (documentation "Run SMBD")
  110. (provision '(samba-smbd))
  111. (requirement '(networking))
  112. (start #~(make-forkexec-constructor
  113. (list #$(file-append package "/sbin/smbd")
  114. (string-append "--configfile=" #$config-file)
  115. "--foreground"
  116. "--no-process-group")))
  117. (stop #~(make-kill-destructor))))))
  118. (define (samba-winbindd-shepherd-service config)
  119. (let ((package (samba-configuration-package config))
  120. (config-file (samba-configuration-config-file config)))
  121. (list (shepherd-service
  122. (documentation "Run Winnbindd for Name Service Switch")
  123. (provision '(samba-winbindd))
  124. (requirement '(networking))
  125. (start #~(make-forkexec-constructor
  126. (list #$(file-append package "/sbin/winbindd")
  127. (string-append "--configfile=" #$config-file)
  128. "--foreground"
  129. "--no-process-group")))
  130. (stop #~(make-kill-destructor))))))
  131. (define (samba-shepherd-services config)
  132. (append (if (samba-configuration-enable-samba? config)
  133. (samba-samba-shepherd-service config)
  134. '())
  135. (if (samba-configuration-enable-nmbd? config)
  136. (samba-nmbd-shepherd-service config)
  137. '())
  138. (if (samba-configuration-enable-smbd? config)
  139. (samba-smbd-shepherd-service config)
  140. '())
  141. (if (samba-configuration-enable-winbindd? config)
  142. (samba-winbindd-shepherd-service config)
  143. '())))
  144. (define samba-service-type
  145. (service-type
  146. (name 'samba)
  147. (description "Run @uref{https://www.samba.org/, Samba}, a network file and
  148. print service for all clients using the SMB/CIFS protocol. Samba is an
  149. important component to seamlessly integrate Linux/Unix Servers and Desktops
  150. into Active Directory environments. It can function both as a domain
  151. controller or as a regular domain member.")
  152. (extensions
  153. (list (service-extension shepherd-root-service-type
  154. samba-shepherd-services)
  155. (service-extension activation-service-type
  156. samba-activation)
  157. (service-extension profile-service-type
  158. (compose list samba-configuration-package))))
  159. (default-value (samba-configuration))))
  160. ;;;
  161. ;;; WSDD
  162. ;;;
  163. (define-record-type* <wsdd-configuration>
  164. wsdd-configuration
  165. make-wsdd-configuration
  166. wsdd-configuration?
  167. (package wsdd-configuration-package
  168. (default wsdd))
  169. (ipv4only? wsdd-configuration-ipv4only?
  170. (default #f))
  171. (ipv6only? wsdd-configuration-ipv6only?
  172. (default #f))
  173. (chroot wsdd-configuration-chroot
  174. (default #f))
  175. (hop-limit wsdd-configuration-hop-limit
  176. (default 1))
  177. (interfaces wsdd-configuration-interfaces
  178. (default '()))
  179. (uuid-device wsdd-configuration-uuid-device
  180. (default #f))
  181. (domain wsdd-configuration-domain
  182. (default #f))
  183. (host-name wsdd-configuration-host-name
  184. (default #f))
  185. (preserve-case? wsdd-configuration-preserve-case?
  186. (default #f))
  187. (workgroup wsdd-configuration-workgroup
  188. (default "WORKGROUP")))
  189. (define wsdd-accounts
  190. (list
  191. (user-group (name "wsdd"))
  192. (user-account (name "wsdd")
  193. (group "wsdd")
  194. (comment "Web Service Discovery user")
  195. (home-directory "/var/empty")
  196. (shell (file-append shadow "/sbin/nologin")))))
  197. (define (wsdd-shepherd-service config)
  198. (match-record config <wsdd-configuration>
  199. (package ipv4only? ipv6only? chroot hop-limit interfaces uuid-device
  200. domain host-name preserve-case? workgroup)
  201. (list (shepherd-service
  202. (documentation "The Web Service Discovery daemon enables (Samba) hosts,
  203. like your local NAS device, to be found by Web Service Discovery Clients
  204. like Windows.")
  205. (provision '(wsdd))
  206. (requirement '(networking))
  207. (start #~(make-forkexec-constructor
  208. (list #$(file-append package "/bin/wsdd")
  209. #$@(if ipv4only?
  210. #~("--ipv4only")
  211. '())
  212. #$@(if ipv6only?
  213. #~("--ipv6only")
  214. '())
  215. #$@(if chroot
  216. #~("--chroot" #$chroot)
  217. '())
  218. #$@(if hop-limit
  219. #~("--hoplimit" #$(number->string hop-limit))
  220. '())
  221. #$@(map (lambda (interfaces)
  222. (string-append "--interface=" interfaces))
  223. interfaces)
  224. #$@(if uuid-device
  225. #~("--uuid" #$uuid-device)
  226. '())
  227. #$@(if domain
  228. #~("--domain" #$domain)
  229. '())
  230. #$@(if host-name
  231. #~("--hostname" #$host-name)
  232. '())
  233. #$@(if preserve-case?
  234. #~("--preserve-case")
  235. '())
  236. #$@(if workgroup
  237. #~("--workgroup" #$workgroup)
  238. '()))
  239. #:user "wsdd"
  240. #:group "wsdd"
  241. #:log-file "/var/log/wsdd.log"))
  242. (stop #~(make-kill-destructor))))))
  243. (define wsdd-service-type
  244. (service-type
  245. (name 'wsdd)
  246. (description "Web Service Discovery Daemon")
  247. (extensions
  248. (list (service-extension shepherd-root-service-type
  249. wsdd-shepherd-service)
  250. (service-extension account-service-type
  251. (const wsdd-accounts))
  252. (service-extension profile-service-type
  253. (compose list wsdd-configuration-package))))
  254. (default-value (wsdd-configuration))))