kerberos.scm 15 KB

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