endlessh-service.scm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021, 2023 Joshua Branson <jbranso@dismail.de>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation, either version 3 of the License, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful,
  12. ;;; but 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. ;;;
  19. ;;; TODO use this link to try to get https://issues.guix.gnu.org/60788
  20. ;;; this service looking more like that service.
  21. ;;; I can play with that service via
  22. ;;; https://paste.centos.org/view/252474c7
  23. ;;;
  24. ;;;
  25. ;;; /gnu/store/a61fxpb2g7fgcr3zmcq891qlwqjinyvn-run-vm.sh
  26. ;;;
  27. ;;; newer vm:
  28. ;;; /gnu/store/07g2mklr9fr3xhwcw8n917cbzpm0dzr9-run-vm.sh
  29. ;;;
  30. ;;; even never vm:
  31. ;;; /gnu/store/4iw889ffc43xay3g4qg7ccv5l8yr5zk9-run-vm.sh
  32. ;;;
  33. ;;; even even newer vm:
  34. ;;; /gnu/store/yx4dbwqxn3ykf2clwl85mzgkzpr1c7xp-run-vm.sh
  35. ;;;
  36. (define-module (endlessh-service)
  37. #:use-module (guix gexp)
  38. #:use-module (guix records)
  39. #:use-module (guix packages)
  40. #:use-module (guix build-system trivial)
  41. #:use-module (gnu packages admin)
  42. #:use-module (gnu packages ssh)
  43. #:use-module (gnu services)
  44. #:use-module (gnu services base)
  45. #:use-module (gnu services configuration)
  46. #:use-module (gnu services shepherd)
  47. #:use-module (gnu services dbus)
  48. #:use-module (gnu services admin)
  49. #:use-module (gnu system shadow)
  50. #:use-module (ice-9 match)
  51. #:use-module (srfi srfi-1)
  52. #:use-module (srfi srfi-26)
  53. #:export (
  54. endlessh-configuration
  55. endlessh-configuration?
  56. endlessh-configuration-bind-family
  57. endlessh-configuration-delay
  58. endlessh-configuration-length
  59. endlessh-configuration-log-level
  60. endlessh-configuration-max-clients
  61. endlessh-configuration-package
  62. endlessh-configuration-port
  63. endlessh-service-type
  64. %default-endlessh
  65. ))
  66. ;;;; Commentary:
  67. ;;; This file provides a basic server to run endlessh.
  68. ;;; https://github.com/skeeto/endlessh
  69. ;;;; Code:
  70. (define (camelfy-field-name field-name)
  71. (match (string-split (symbol->string field-name) #\-)
  72. ((head tail ...)
  73. (string-join (cons (string-upcase head 0 1)
  74. (map (cut string-upcase <> 0 1) tail)) ""))))
  75. (define (serialize-integer field-name val)
  76. (string-append (camelfy-field-name field-name) " "
  77. (number->string val) "\n"))
  78. (define (list-of-bind-family? val)
  79. (or (equal? val '(ipv4 ipv6))
  80. (equal? val '(ipv6 ipv4))
  81. (equal? val '(ipv4))
  82. (equal? val '(ipv6))))
  83. (define (serialize-list-of-bind-family field-name val)
  84. (string-append
  85. (camelfy-field-name field-name) " "
  86. (cond ((or (equal? val '(ipv4 ipv6))
  87. (equal? val '(ipv6 ipv4)))
  88. "0")
  89. ((equal? val '(ipv4))
  90. "4")
  91. (else "6")))
  92. "\n")
  93. (define-configuration endlessh-configuration
  94. (package
  95. (package endlessh)
  96. "The endlessh package to use.")
  97. (bind-family
  98. (list-of-bind-family '(ipv4 ipv6))
  99. "A list of symbols of ip families to run endlessh: ipv4 and/or ipv6.")
  100. (delay
  101. (integer 10000)
  102. "Message milliseconds delay. Default: 10000")
  103. (max-line-length
  104. (integer 32)
  105. "Maximum banner line length (3-255). Default: 32")
  106. (max-clients
  107. (integer 4096)
  108. "Maximum number of clients. Default: 4096")
  109. (port
  110. (integer 2222)
  111. "Set the listening port. Default: 2222")
  112. (log-level
  113. (integer 0)
  114. "Print diagnostics. Allowed values are 0, 1, 2. The higher the value
  115. the more detailed the log. A value of 0 turns off logging."))
  116. (define (serialize-endlessh-configuration config)
  117. (mixed-text-file
  118. "endlessh.conf"
  119. (serialize-configuration config endlessh-configuration-fields)))
  120. (define (generate-endlessh-documentation)
  121. (generate-documentation
  122. `((endlessh-configuration
  123. ,endlessh-configuration-fields))
  124. 'endlessh-configuration))
  125. ;; how can I add shepherd-configuration-file? to return the service's
  126. ;; current config file?
  127. (define (endlessh-shepherd-service config)
  128. (list (shepherd-service
  129. (provision '(endlessh))
  130. (documentation "Run the endlessh daemon.")
  131. ;; change this to user-processes & networking? like nginx?
  132. (requirement '(networking))
  133. (start #~(make-forkexec-constructor
  134. (list #$(file-append (endlessh-configuration-package config)
  135. "/bin/endlessh")
  136. "-f" #$(serialize-endlessh-configuration config))
  137. #:user "endlessh"
  138. #:group "endlessh"))
  139. (stop #~(make-kill-destructor)))))
  140. (define %endlessh-accounts
  141. (list (user-group (name "endlessh")
  142. (system? #t))
  143. (user-account
  144. (name "endlessh")
  145. (group "endlessh")
  146. (system? #t)
  147. (comment "endlessh user")
  148. (home-directory "/var/empty")
  149. (shell (file-append shadow "/sbin/nologin")))))
  150. ;; (define endlessh-activation
  151. ;; (match-lambda
  152. ;; (($ <endlessh-configuration> package config-file)
  153. ;; (with-imported-modules '((guix build utils))
  154. ;; #~(begin
  155. ;; (use-modules (guix build utils))
  156. ;; (let ([x #t])
  157. ;; x))))))
  158. (define endlessh-service-type
  159. (service-type
  160. (name 'endlessh)
  161. (description "Run endlessh, a small turning ssh tarpit.")
  162. (extensions
  163. (list (service-extension shepherd-root-service-type endlessh-shepherd-service)
  164. (service-extension account-service-type
  165. (const %endlessh-accounts))
  166. ;;(service-extension activation-service-type endlessh-activation)
  167. ))
  168. (default-value (endlessh-configuration))))