kerberos.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 John Darrington <jmd@gnu.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 kerberos)
  19. #:use-module (gnu services)
  20. #:use-module (gnu services configuration)
  21. #:use-module (gnu system pam)
  22. #:use-module (guix gexp)
  23. #:use-module (guix records)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-34)
  26. #:use-module (srfi srfi-35)
  27. #:use-module (ice-9 match)
  28. #:export (pam-krb5-configuration
  29. pam-krb5-configuration?
  30. pam-krb5-service-type
  31. krb5-realm
  32. krb5-realm?
  33. krb5-configuration
  34. krb5-configuration?
  35. krb5-service-type))
  36. (define unset-field (list 'unset-field))
  37. (define (predicate/unset pred)
  38. (lambda (x) (or (eq? x unset-field) (pred x))))
  39. (define string/unset? (predicate/unset string?))
  40. (define boolean/unset? (predicate/unset boolean?))
  41. (define integer/unset? (predicate/unset integer?))
  42. (define (uglify-field-name field-name)
  43. "Return FIELD-NAME with all instances of '-' replaced by '_' and any
  44. trailing '?' removed."
  45. (let ((str (symbol->string field-name)))
  46. (string-join (string-split (if (string-suffix? "?" str)
  47. (substring str 0 (1- (string-length str)))
  48. str)
  49. #\-)
  50. "_")))
  51. (define (serialize-field* field-name val)
  52. (format #t "~a = ~a\n" (uglify-field-name field-name) val))
  53. (define (serialize-string/unset field-name val)
  54. (unless (eq? val unset-field)
  55. (serialize-field* field-name val)))
  56. (define (serialize-integer/unset field-name val)
  57. (unless (eq? val unset-field)
  58. (serialize-field* field-name val)))
  59. (define (serialize-boolean/unset field-name val)
  60. (unless (eq? val unset-field)
  61. (serialize-field* field-name
  62. (if val "true" "false"))))
  63. ;; An end-point is an address such as "192.168.0.1"
  64. ;; or an address port pair ("foobar.example.com" . 109)
  65. (define (end-point? val)
  66. (match val
  67. ((? string?) #t)
  68. (((? string?) . (? integer?)) #t)
  69. (_ #f)))
  70. (define (serialize-end-point field-name val)
  71. (serialize-field* field-name
  72. (match val
  73. ((host . port)
  74. ;; The [] are needed in the case of IPv6 addresses
  75. (format #f "[~a]:~a" host port))
  76. (host
  77. (format #f "~a" host)))))
  78. (define (serialize-space-separated-string-list/unset field-name val)
  79. (unless (eq? val unset-field)
  80. (serialize-field* field-name (string-join val " "))))
  81. (define (space-separated-string-list? val)
  82. (and (list? val)
  83. (and-map (lambda (x)
  84. (and (string? x) (not (string-index x #\space))))
  85. val)))
  86. (define space-separated-string-list/unset?
  87. (predicate/unset space-separated-string-list?))
  88. (define comma-separated-integer-list/unset?
  89. (predicate/unset (lambda (val)
  90. (and (list? val)
  91. (and-map (lambda (x) (integer? x))
  92. val)))))
  93. (define (serialize-comma-separated-integer-list/unset field-name val)
  94. (unless (eq? val unset-field)
  95. (serialize-field* field-name
  96. (string-drop ; Drop the leading comma
  97. (fold
  98. (lambda (i prev)
  99. (string-append prev "," (number->string i)))
  100. "" val) 1))))
  101. (define file-name? (predicate/unset
  102. (lambda (val)
  103. (string-prefix? "/" val))))
  104. (define (serialize-field field-name val)
  105. (format #t "~a ~a\n" (uglify-field-name field-name) val))
  106. (define (serialize-string field-name val)
  107. (serialize-field field-name val))
  108. (define (serialize-file-name field-name val)
  109. (unless (eq? val unset-field)
  110. (serialize-string field-name val)))
  111. (define (serialize-space-separated-string-list field-name val)
  112. (serialize-field field-name (string-join val " ")))
  113. (define (non-negative-integer? val)
  114. (and (exact-integer? val) (not (negative? val))))
  115. (define (serialize-non-negative-integer/unset field-name val)
  116. (unless (eq? val unset-field)
  117. (serialize-field* field-name val)))
  118. (define (free-form-fields? val)
  119. (match val
  120. (() #t)
  121. ((((? symbol?) . (? string)) . val) (free-form-fields? val))
  122. (_ #f)))
  123. (define (serialize-free-form-fields field-name val)
  124. (for-each (match-lambda ((k . v) (serialize-field* k v))) val))
  125. (define non-negative-integer/unset? (predicate/unset non-negative-integer?))
  126. (define (realm-list? val)
  127. (and (list? val)
  128. (and-map (lambda (x) (krb5-realm? x)) val)))
  129. (define (serialize-realm-list field-name val)
  130. (format #t "\n[~a]\n" field-name)
  131. (for-each (lambda (realm)
  132. (format #t "\n~a = {\n" (krb5-realm-name realm))
  133. (for-each (lambda (field)
  134. (unless (eq? 'name (configuration-field-name field))
  135. ((configuration-field-serializer field)
  136. (configuration-field-name field)
  137. ((configuration-field-getter field)
  138. realm)))) krb5-realm-fields)
  139. (format #t "}\n")) val))
  140. ;; For a more detailed explanation of these fields see man 5 krb5.conf
  141. (define-configuration krb5-realm
  142. (name
  143. (string/unset unset-field)
  144. "The name of the realm.")
  145. (kdc
  146. (end-point unset-field)
  147. "The host and port on which the realm's Key Distribution Server listens.")
  148. (admin-server
  149. (string/unset unset-field)
  150. "The Host running the administration server for the realm.")
  151. (master-kdc
  152. (string/unset unset-field)
  153. "If an attempt to get credentials fails because of an invalid password,
  154. the client software will attempt to contact the master KDC.")
  155. (kpasswd-server
  156. (string/unset unset-field)
  157. "The server where password changes are performed.")
  158. (auth-to-local
  159. (free-form-fields '())
  160. "Rules to map between principals and local users.")
  161. (auth-to-local-names
  162. (free-form-fields '())
  163. "Explicit mappings between principal names and local user names.")
  164. (http-anchors
  165. (free-form-fields '())
  166. "Useful only when http proxy is used to access KDC or KPASSWD.")
  167. ;; The following are useful only for working with V4 services
  168. (default-domain
  169. (string/unset unset-field)
  170. "The domain used to expand host names when translating Kerberos 4 service
  171. principals to Kerberos 5 principals")
  172. (v4-instance-convert
  173. (free-form-fields '())
  174. "Exceptions to the default-domain mapping rule.")
  175. (v4-realm
  176. (string/unset unset-field)
  177. "Used when the V4 realm name and the V5 realm name are not the same, but
  178. still share the same principal names and passwords"))
  179. ;; For a more detailed explanation of these fields see man 5 krb5.conf
  180. (define-configuration krb5-configuration
  181. (allow-weak-crypto?
  182. (boolean/unset unset-field)
  183. "If true, permits access to services which only offer weak encryption.")
  184. (ap-req-checksum-type
  185. (non-negative-integer/unset unset-field)
  186. "The type of the AP-REQ checksum.")
  187. (canonicalize?
  188. (boolean/unset unset-field)
  189. "Should principals in initial ticket requests be canonicalized?")
  190. (ccache-type
  191. (non-negative-integer/unset unset-field)
  192. "The format of the credential cache type.")
  193. (clockskew
  194. (non-negative-integer/unset unset-field)
  195. "Maximum allowable clock skew in seconds (default 300).")
  196. (default-ccache-name
  197. (file-name unset-field)
  198. "The name of the default credential cache.")
  199. (default-client-keytab-name
  200. (file-name unset-field)
  201. "The name of the default keytab for client credentials.")
  202. (default-keytab-name
  203. (file-name unset-field)
  204. "The name of the default keytab file.")
  205. (default-realm
  206. (string/unset unset-field)
  207. "The realm to be accessed if not explicitly specified by clients.")
  208. (default-tgs-enctypes
  209. (free-form-fields '())
  210. "Session key encryption types when making TGS-REQ requests.")
  211. (default-tkt-enctypes
  212. (free-form-fields '())
  213. "Session key encryption types when making AS-REQ requests.")
  214. (dns-canonicalize-hostname?
  215. (boolean/unset unset-field)
  216. "Whether name lookups will be used to canonicalize host names for use in
  217. service principal names.")
  218. (dns-lookup-kdc?
  219. (boolean/unset unset-field)
  220. "Should DNS SRV records should be used to locate the KDCs and other servers
  221. not appearing in the realm specification")
  222. (err-fmt
  223. (string/unset unset-field)
  224. "Custom error message formatting. If not #f error messages will be formatted
  225. by substituting a normal error message for %M and an error code for %C in the
  226. value.")
  227. (forwardable?
  228. (boolean/unset unset-field)
  229. "Should initial tickets be forwardable by default?")
  230. (ignore-acceptor-hostname?
  231. (boolean/unset unset-field)
  232. "When accepting GSSAPI or krb5 security contexts for host-based service
  233. principals, ignore any hostname passed by the calling application, and allow
  234. clients to authenticate to any service principal in the keytab matching the
  235. service name and realm name.")
  236. (k5login-authoritative?
  237. (boolean/unset unset-field)
  238. "If this flag is true, principals must be listed in a local user's k5login
  239. file to be granted login access, if a ~/.k5login file exists.")
  240. (k5login-directory
  241. (string/unset unset-field)
  242. "If not #f, the library will look for a local user's @file{k5login} file
  243. within the named directory (instead of the user's home directory), with a
  244. file name corresponding to the local user name.")
  245. (kcm-mach-service
  246. (string/unset unset-field)
  247. "The name of the bootstrap service used to contact the KCM daemon for the
  248. KCM credential cache type.")
  249. (kcm-socket
  250. (file-name unset-field)
  251. "Path to the Unix domain socket used to access the KCM daemon for the KCM
  252. credential cache type.")
  253. (kdc-default-options
  254. (non-negative-integer/unset unset-field)
  255. "Default KDC options (logored for multiple values) when requesting initial
  256. tickets.")
  257. (kdc-timesync
  258. (non-negative-integer/unset unset-field)
  259. "Attempt to compensate for clock skew between the KDC and client.")
  260. (kdc-req-checksum-type
  261. (non-negative-integer/unset unset-field)
  262. "The type of checksum to use for the KDC requests. Relevant only for DES
  263. keys")
  264. (noaddresses?
  265. (boolean/unset unset-field)
  266. "If true, initial ticket requests will not be made with address restrictions.
  267. This enables their use across NATs.")
  268. (permitted-enctypes
  269. (space-separated-string-list/unset unset-field)
  270. "All encryption types that are permitted for use in session key encryption.")
  271. (plugin-base-dir
  272. (file-name unset-field)
  273. "The directory where krb5 plugins are located.")
  274. (preferred-preauth-types
  275. (comma-separated-integer-list/unset unset-field)
  276. "The preferred pre-authentication types which the client will attempt before
  277. others.")
  278. (proxiable?
  279. (boolean/unset unset-field)
  280. "Should initial tickets be proxiable by default?")
  281. (rdns?
  282. (boolean/unset unset-field)
  283. "Should reverse DNS lookup be used in addition to forward name lookup to
  284. canonicalize host names for use in service principal names.")
  285. (realm-try-domains
  286. (integer/unset unset-field)
  287. "Should a host's domain components should be used to determine the Kerberos
  288. realm of the host.")
  289. (renew-lifetime
  290. (non-negative-integer/unset unset-field)
  291. "The default renewable lifetime for initial ticket requests.")
  292. (safe-checksum-type
  293. (non-negative-integer/unset unset-field)
  294. "The type of checksum to use for the KRB-SAFE requests.")
  295. (ticket-lifetime
  296. (non-negative-integer/unset unset-field)
  297. "The default lifetime for initial ticket requests.")
  298. (udp-preference-limit
  299. (non-negative-integer/unset unset-field)
  300. "When sending messages to the KDC, the library will try using TCP
  301. before UDP if the size of the message greater than this limit.")
  302. (verify-ap-rereq-nofail?
  303. (boolean/unset unset-field)
  304. "If true, then attempts to verify initial credentials will fail if the client
  305. machine does not have a keytab.")
  306. (realms
  307. (realm-list '())
  308. "The list of realms which clients may access."))
  309. (define (krb5-configuration-file config)
  310. "Create a Kerberos 5 configuration file based on CONFIG"
  311. (mixed-text-file "krb5.conf"
  312. "[libdefaults]\n\n"
  313. (with-output-to-string
  314. (lambda ()
  315. (serialize-configuration config
  316. krb5-configuration-fields)))))
  317. (define (krb5-etc-service config)
  318. (list `("krb5.conf" ,(krb5-configuration-file config))))
  319. (define krb5-service-type
  320. (service-type (name 'krb5)
  321. (extensions
  322. (list (service-extension etc-service-type
  323. krb5-etc-service)))))
  324. (define-record-type* <pam-krb5-configuration>
  325. pam-krb5-configuration make-pam-krb5-configuration
  326. pam-krb5-configuration?
  327. (pam-krb5 pam-krb5-configuration-pam-krb5
  328. (default pam-krb5))
  329. (minimum-uid pam-krb5-configuration-minimum-uid
  330. (default 1000)))
  331. (define (pam-krb5-pam-service config)
  332. "Return a PAM service for Kerberos authentication."
  333. (lambda (pam)
  334. (define pam-krb5-module
  335. #~(string-append #$(pam-krb5-configuration-pam-krb5 config)
  336. "/lib/security/pam_krb5.so"))
  337. (let ((pam-krb5-sufficient
  338. (pam-entry
  339. (control "sufficient")
  340. (module pam-krb5-module)
  341. (arguments
  342. (list
  343. (format #f "minimum_uid=~a"
  344. (pam-krb5-configuration-minimum-uid config)))))))
  345. (pam-service
  346. (inherit pam)
  347. (auth (cons* pam-krb5-sufficient
  348. (pam-service-auth pam)))
  349. (session (cons* pam-krb5-sufficient
  350. (pam-service-session pam)))
  351. (account (cons* pam-krb5-sufficient
  352. (pam-service-account pam)))))))
  353. (define (pam-krb5-pam-services config)
  354. (list (pam-krb5-pam-service config)))
  355. (define pam-krb5-service-type
  356. (service-type (name 'pam-krb5)
  357. (extensions
  358. (list
  359. (service-extension pam-root-service-type
  360. pam-krb5-pam-services)))))