rsync.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Oleg Pykhalov <go.wigust@gmail.com>
  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 rsync)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services base)
  21. #:use-module (gnu services shepherd)
  22. #:use-module (gnu system shadow)
  23. #:use-module (gnu packages rsync)
  24. #:use-module (gnu packages admin)
  25. #:use-module (guix records)
  26. #:use-module (guix gexp)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-26)
  29. #:use-module (ice-9 match)
  30. #:export (rsync-configuration
  31. rsync-configuration?
  32. rsync-service-type))
  33. ;;;; Commentary:
  34. ;;;
  35. ;;; This module implements a service that to run instance of Rsync,
  36. ;;; files synchronization tool.
  37. ;;;
  38. ;;;; Code:
  39. (define-record-type* <rsync-configuration>
  40. rsync-configuration
  41. make-rsync-configuration
  42. rsync-configuration?
  43. (package rsync-configuration-package ; package
  44. (default rsync))
  45. (port-number rsync-configuration-port-number ; integer
  46. (default 873))
  47. (pid-file rsync-configuration-pid-file ; string
  48. (default "/var/run/rsyncd/rsyncd.pid"))
  49. (lock-file rsync-configuration-lock-file ; string
  50. (default "/var/run/rsyncd/rsyncd.lock"))
  51. (log-file rsync-configuration-log-file ; string
  52. (default "/var/log/rsyncd.log"))
  53. (use-chroot? rsync-configuration-use-chroot? ; boolean
  54. (default #t))
  55. (share-path rsync-configuration-share-path ; string
  56. (default "/srv/rsyncd"))
  57. (share-comment rsync-configuration-share-comment ; string
  58. (default "Rsync share"))
  59. (read-only? rsync-configuration-read-only? ; boolean
  60. (default #f))
  61. (timeout rsync-configuration-timeout ; integer
  62. (default 300))
  63. (user rsync-configuration-user ; string
  64. (default "root"))
  65. (group rsync-configuration-group ; string
  66. (default "root"))
  67. (uid rsync-configuration-uid ; string
  68. (default "rsyncd"))
  69. (gid rsync-configuration-gid ; string
  70. (default "rsyncd")))
  71. (define (rsync-account config)
  72. "Return the user accounts and user groups for CONFIG."
  73. (let ((rsync-user (if (rsync-configuration-uid config)
  74. (rsync-configuration-uid config)
  75. (rsync-configuration-user config)))
  76. (rsync-group (if (rsync-configuration-gid config)
  77. (rsync-configuration-gid config)
  78. (rsync-configuration-group config))))
  79. (list (user-group (name rsync-group) (system? #t))
  80. (user-account
  81. (name rsync-user)
  82. (system? #t)
  83. (group rsync-group)
  84. (comment "rsyncd privilege separation user")
  85. (home-directory (string-append "/var/run/"
  86. rsync-user))
  87. (shell #~(string-append #$shadow "/sbin/nologin"))))))
  88. (define (rsync-activation config)
  89. "Return the activation GEXP for CONFIG."
  90. (with-imported-modules '((guix build utils))
  91. #~(begin
  92. (let ((share-directory #$(rsync-configuration-share-path config))
  93. (user (getpw (if #$(rsync-configuration-uid config)
  94. #$(rsync-configuration-uid config)
  95. #$(rsync-configuration-user config))))
  96. (group (getpw (if #$(rsync-configuration-gid config)
  97. #$(rsync-configuration-gid config)
  98. #$(rsync-configuration-group config)))))
  99. (mkdir-p (dirname #$(rsync-configuration-pid-file config)))
  100. (and=> share-directory mkdir-p)
  101. (chown share-directory
  102. (passwd:uid user)
  103. (group:gid group))))))
  104. (define rsync-config-file
  105. ;; Return the rsync configuration file corresponding to CONFIG.
  106. (match-lambda
  107. (($ <rsync-configuration> package port-number pid-file lock-file log-file
  108. use-chroot? share-path share-comment read-only?
  109. timeout user group uid gid)
  110. (if (not (string=? user "root"))
  111. (cond
  112. ((<= port-number 1024)
  113. (error (string-append "rsync-service: to run on port "
  114. (number->string port-number)
  115. ", user must be root.")))
  116. (use-chroot?
  117. (error (string-append "rsync-service: to run in a chroot"
  118. ", user must be root.")))
  119. (uid
  120. (error "rsync-service: to use uid, user must be root."))
  121. (gid
  122. (error "rsync-service: to use gid, user must be root."))))
  123. (mixed-text-file
  124. "rsync.conf"
  125. "# Generated by 'rsync-service'.\n\n"
  126. "pid file = " pid-file "\n"
  127. "lock file = " lock-file "\n"
  128. "log file = " log-file "\n"
  129. "port = " (number->string port-number) "\n"
  130. "use chroot = " (if use-chroot? "true" "false") "\n"
  131. (if uid (string-append "uid = " uid "\n") "")
  132. "gid = " (if gid gid "nogroup") "\n" ; no group nobody
  133. "\n"
  134. "[files]\n"
  135. "path = " share-path "\n"
  136. "comment = " share-comment "\n"
  137. "read only = " (if read-only? "true" "false") "\n"
  138. "timeout = " (number->string timeout) "\n"))))
  139. (define (rsync-shepherd-service config)
  140. "Return a <shepherd-service> for rsync with CONFIG."
  141. (let* ((rsync (rsync-configuration-package config))
  142. (pid-file (rsync-configuration-pid-file config))
  143. (port-number (rsync-configuration-port-number config))
  144. (user (rsync-configuration-user config))
  145. (group (rsync-configuration-group config)))
  146. (list (shepherd-service
  147. (provision '(rsync))
  148. (documentation "Run rsync daemon.")
  149. (start #~(make-forkexec-constructor
  150. (list (string-append #$rsync "/bin/rsync")
  151. "--config" #$(rsync-config-file config)
  152. "--daemon")
  153. #:pid-file #$pid-file
  154. #:user #$user
  155. #:group #$group))
  156. (stop #~(make-kill-destructor))))))
  157. (define rsync-service-type
  158. (service-type
  159. (name 'rsync)
  160. (extensions
  161. (list (service-extension shepherd-root-service-type rsync-shepherd-service)
  162. (service-extension account-service-type rsync-account)
  163. (service-extension activation-service-type rsync-activation)))
  164. (default-value (rsync-configuration))))