1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org>
3 ;;; Copyright © 2018, 2019 Ricardo Wurmus <rekado@elephly.net>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services authentication)
21 #:use-module (gnu services)
22 #:use-module (gnu services base)
23 #:use-module (gnu services configuration)
24 #:use-module (gnu services dbus)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu system pam)
27 #:use-module (gnu system shadow)
28 #:use-module (gnu packages admin)
29 #:use-module (gnu packages freedesktop)
30 #:use-module (gnu packages openldap)
31 #:use-module (guix gexp)
32 #:use-module (guix records)
33 #:use-module (guix packages)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-26)
37 #:export (fprintd-configuration
38 fprintd-configuration?
45 (define-record-type* <fprintd-configuration>
46 fprintd-configuration make-fprintd-configuration
47 fprintd-configuration?
48 (ntp fprintd-configuration-fprintd
51 (define fprintd-service-type
52 (service-type (name 'fprintd)
54 (list (service-extension dbus-root-service-type
57 "Run fprintd, a fingerprint management daemon.")))
61 ;;; NSS Pam LDAP service (nslcd)
64 (define (uglify-field-name name)
68 (_ (string-map (match-lambda
71 (symbol->string name)))))
73 (define (value->string val)
80 (string-map (match-lambda
83 (symbol->string val)))
86 (define (serialize-field field-name val)
87 (if (eq? field-name 'pam-services)
90 (uglify-field-name field-name)
91 (value->string val))))
93 (define serialize-string serialize-field)
94 (define serialize-boolean serialize-field)
95 (define serialize-number serialize-field)
96 (define (serialize-list field-name val)
97 (map (cut serialize-field field-name <>) val))
99 (define-maybe boolean)
100 (define-maybe number)
102 (define (ssl-option? val)
104 (eq? val 'start-tls)))
105 (define serialize-ssl-option serialize-field)
106 (define-maybe ssl-option)
108 (define (tls-reqcert-option? val)
109 (member val '(never allow try demand hard)))
110 (define serialize-tls-reqcert-option serialize-field)
111 (define-maybe tls-reqcert-option)
113 (define (deref-option? val)
114 (member val '(never searching finding always)))
115 (define serialize-deref-option serialize-field)
116 (define-maybe deref-option)
118 (define (comma-separated-list-of-strings? val)
120 (every string? val)))
121 (define (ignore-users-option? val)
122 (or (comma-separated-list-of-strings? val)
123 (eq? 'all-local val)))
124 (define (serialize-ignore-users-option field-name val)
125 (serialize-field field-name (if (eq? 'all-local val)
127 (string-join val ","))))
128 (define-maybe ignore-users-option)
130 (define (log-option? val)
131 (let ((valid-scheme? (lambda (scheme)
133 (member scheme '(none syslog))))))
136 (and (valid-scheme? scheme)
137 (member level '(crit error warning notice info debug))))
139 (valid-scheme? scheme)))))
140 (define (serialize-log-option field-name val)
141 (serialize-field field-name
142 (string-join (map (cut format #f "~a" <>) val))))
144 (define (valid-map? val)
145 "Is VAL a supported map name?"
147 '(alias aliases ether ethers group host hosts netgroup network networks
148 passwd protocol protocols rpc service services shadow)))
150 (define (scope-option? val)
151 (let ((valid-scopes '(subtree onelevel base children)))
154 (and (valid-map? map-name)
155 (member scope valid-scopes)))
157 (member scope valid-scopes)))))
158 (define (serialize-scope-option field-name val)
159 (serialize-field field-name
160 (string-join (map (cut format #f "~a" <>) val))))
162 (define (map-entry? val)
164 (((? valid-map? map-name)
165 (? string? attribute)
166 (? string? new-attribute)) #t)
169 (define (list-of-map-entries? val)
171 (every map-entry? val)))
173 (define (filter-entry? val)
175 (((? valid-map? map-name)
176 (? string? filter-expression)) #t)
179 (define (list-of-filter-entries? val)
181 (every filter-entry? val)))
183 (define (serialize-filter-entry field-name val)
184 (serialize-field 'filter
186 (((? valid-map? map-name)
187 (? string? filter-expression))
188 (string-append (symbol->string map-name)
189 " " filter-expression)))))
191 (define (serialize-list-of-filter-entries field-name val)
192 (for-each (cut serialize-filter-entry field-name <>) val))
194 (define (serialize-map-entry field-name val)
195 (serialize-field 'map
197 (((? valid-map? map-name)
198 (? string? attribute)
199 (? string? new-attribute))
200 (string-append (symbol->string map-name)
202 " " new-attribute)))))
204 (define (serialize-list-of-map-entries field-name val)
205 (for-each (cut serialize-map-entry field-name <>) val))
208 (define-configuration nslcd-configuration
210 (package nss-pam-ldapd)
211 "The NSS-PAM-LDAPD package to use.")
215 (maybe-number 'disabled)
216 "The number of threads to start that can handle requests and perform LDAP
217 queries. Each thread opens a separate connection to the LDAP server. The
218 default is to start 5 threads.")
221 "This specifies the user id with which the daemon should be run.")
224 "This specifies the group id with which the daemon should be run.")
226 (log-option '("/var/log/nslcd" info))
227 "This option controls the way logging is done via a list containing SCHEME
228 and LEVEL. The SCHEME argument may either be the symbols \"none\" or
229 \"syslog\", or an absolute file name. The LEVEL argument is optional and
230 specifies the log level. The log level may be one of the following symbols:
231 \"crit\", \"error\", \"warning\", \"notice\", \"info\" or \"debug\". All
232 messages with the specified log level or higher are logged.")
234 ;; LDAP connection settings
236 (list '("ldap://localhost:389/"))
237 "The list of LDAP server URIs. Normally, only the first server will be
238 used with the following servers as fall-back.")
240 (maybe-string 'disabled)
241 "The version of the LDAP protocol to use. The default is to use the
242 maximum version supported by the LDAP library.")
244 (maybe-string 'disabled)
245 "Specifies the distinguished name with which to bind to the directory
246 server for lookups. The default is to bind anonymously.")
248 (maybe-string 'disabled)
249 "Specifies the credentials with which to bind. This option is only
250 applicable when used with binddn.")
252 (maybe-string 'disabled)
253 "Specifies the distinguished name to use when the root user tries to modify
254 a user's password using the PAM module.")
256 (maybe-string 'disabled)
257 "Specifies the credentials with which to bind if the root user tries to
258 change a user's password. This option is only applicable when used with
261 ;; SASL authentication options
263 (maybe-string 'disabled)
264 "Specifies the SASL mechanism to be used when performing SASL
267 (maybe-string 'disabled)
268 "Specifies the SASL realm to be used when performing SASL authentication.")
270 (maybe-string 'disabled)
271 "Specifies the authentication identity to be used when performing SASL
274 (maybe-string 'disabled)
275 "Specifies the authorization identity to be used when performing SASL
278 (maybe-boolean 'disabled)
279 "Determines whether the LDAP server host name should be canonicalised. If
280 this is enabled the LDAP library will do a reverse host name lookup. By
281 default, it is left up to the LDAP library whether this check is performed or
284 ;; Kerberos authentication options
286 (maybe-string 'disabled)
287 "Set the name for the GSS-API Kerberos credentials cache.")
289 ;; Search / mapping options
291 (string "dc=example,dc=com")
292 "The directory search base.")
294 (scope-option '(subtree))
295 "Specifies the search scope (subtree, onelevel, base or children). The
296 default scope is subtree; base scope is almost never useful for name service
297 lookups; children scope is not supported on all servers.")
299 (maybe-deref-option 'disabled)
300 "Specifies the policy for dereferencing aliases. The default policy is to
301 never dereference aliases.")
303 (maybe-boolean 'disabled)
304 "Specifies whether automatic referral chasing should be enabled. The
305 default behaviour is to chase referrals.")
307 (list-of-map-entries '())
308 "This option allows for custom attributes to be looked up instead of the
309 default RFC 2307 attributes. It is a list of maps, each consisting of the
310 name of a map, the RFC 2307 attribute to match and the query expression for
311 the attribute as it is available in the directory.")
313 (list-of-filter-entries '())
314 "A list of filters consisting of the name of a map to which the filter
315 applies and an LDAP search filter expression.")
317 ;; Timing / reconnect options
319 (maybe-number 'disabled)
320 "Specifies the time limit in seconds to use when connecting to the
321 directory server. The default value is 10 seconds.")
323 (maybe-number 'disabled)
324 "Specifies the time limit (in seconds) to wait for a response from the LDAP
325 server. A value of zero, which is the default, is to wait indefinitely for
326 searches to be completed.")
328 (maybe-number 'disabled)
329 "Specifies the period if inactivity (in seconds) after which the con‐
330 nection to the LDAP server will be closed. The default is not to time out
333 (maybe-number 'disabled)
334 "Specifies the number of seconds to sleep when connecting to all LDAP
335 servers fails. By default one second is waited between the first failure and
338 (maybe-number 'disabled)
339 "Specifies the time after which the LDAP server is considered to be
340 permanently unavailable. Once this time is reached retries will be done only
341 once per this time period. The default value is 10 seconds.")
345 (maybe-ssl-option 'disabled)
346 "Specifies whether to use SSL/TLS or not (the default is not to). If
347 'start-tls is specified then StartTLS is used rather than raw LDAP over SSL.")
349 (maybe-tls-reqcert-option 'disabled)
350 "Specifies what checks to perform on a server-supplied certificate.
351 The meaning of the values is described in the ldap.conf(5) manual page.")
353 (maybe-string 'disabled)
354 "Specifies the directory containing X.509 certificates for peer authen‐
355 tication. This parameter is ignored when using GnuTLS.")
357 (maybe-string 'disabled)
358 "Specifies the path to the X.509 certificate for peer authentication.")
360 (maybe-string 'disabled)
361 "Specifies the path to an entropy source. This parameter is ignored when
364 (maybe-string 'disabled)
365 "Specifies the ciphers to use for TLS as a string.")
367 (maybe-string 'disabled)
368 "Specifies the path to the file containing the local certificate for client
369 TLS authentication.")
371 (maybe-string 'disabled)
372 "Specifies the path to the file containing the private key for client TLS
377 (maybe-number 'disabled)
378 "Set this to a number greater than 0 to request paged results from the LDAP
379 server in accordance with RFC2696. The default (0) is to not request paged
381 (nss-initgroups-ignoreusers
382 (maybe-ignore-users-option 'disabled)
383 "This option prevents group membership lookups through LDAP for the
384 specified users. Alternatively, the value 'all-local may be used. With that
385 value nslcd builds a full list of non-LDAP users on startup.")
387 (maybe-number 'disabled)
388 "This option ensures that LDAP users with a numeric user id lower than the
389 specified value are ignored.")
391 (maybe-number 'disabled)
392 "This option specifies an offset that is added to all LDAP numeric user
393 ids. This can be used to avoid user id collisions with local users.")
395 (maybe-number 'disabled)
396 "This option specifies an offset that is added to all LDAP numeric group
397 ids. This can be used to avoid user id collisions with local groups.")
399 (maybe-boolean 'disabled)
400 "If this option is set, the member attribute of a group may point to
401 another group. Members of nested groups are also returned in the higher level
402 group and parent groups are returned when finding groups for a specific user.
403 The default is not to perform extra searches for nested groups.")
404 (nss-getgrent-skipmembers
405 (maybe-boolean 'disabled)
406 "If this option is set, the group member list is not retrieved when looking
407 up groups. Lookups for finding which groups a user belongs to will remain
408 functional so the user will likely still get the correct groups assigned on
410 (nss-disable-enumeration
411 (maybe-boolean 'disabled)
412 "If this option is set, functions which cause all user/group entries to be
413 loaded from the directory will not succeed in doing so. This can dramatically
414 reduce LDAP server load in situations where there are a great number of users
415 and/or groups. This option is not recommended for most configurations.")
417 (maybe-string 'disabled)
418 "This option can be used to specify how user and group names are verified
419 within the system. This pattern is used to check all user and group names
420 that are requested and returned from LDAP.")
422 (maybe-boolean 'disabled)
423 "This specifies whether or not to perform searches using case-insensitive
424 matching. Enabling this could open up the system to authorization bypass
425 vulnerabilities and introduce nscd cache poisoning vulnerabilities which allow
428 (maybe-boolean 'disabled)
429 "This option specifies whether password policy controls are requested and
430 handled from the LDAP server when performing user authentication.")
432 (maybe-string 'disabled)
433 "By default nslcd performs an LDAP search with the user's credentials after
434 BIND (authentication) to ensure that the BIND operation was successful. The
435 default search is a simple check to see if the user's DN exists. A search
436 filter can be specified that will be used instead. It should return at least
439 (maybe-string 'disabled)
440 "This option allows flexible fine tuning of the authorisation check that
441 should be performed. The search filter specified is executed and if any
442 entries match, access is granted, otherwise access is denied.")
443 (pam-password-prohibit-message
444 (maybe-string 'disabled)
445 "If this option is set password modification using pam_ldap will be denied
446 and the specified message will be presented to the user instead. The message
447 can be used to direct the user to an alternative means of changing their
450 ;; Options for extension of pam-root-service-type.
453 "List of pam service names for which LDAP authentication should suffice."))
455 (define %nslcd-accounts
462 (comment "NSLCD service account")
463 (home-directory "/var/empty")
464 (shell (file-append shadow "/sbin/nologin"))
467 (define (nslcd-config-file config)
468 "Return an NSLCD configuration file."
469 (plain-file "nslcd.conf"
470 (with-output-to-string
472 (serialize-configuration config nslcd-configuration-fields)
473 ;; The file must end with a newline character.
476 ;; XXX: The file should only be readable by root if it contains a "bindpw"
477 ;; declaration. Unfortunately, this etc-service-type extension does not
478 ;; support setting file modes, so we do this in the activation service.
479 (define (nslcd-etc-service config)
480 `(("nslcd.conf" ,(nslcd-config-file config))))
482 (define (nslcd-shepherd-service config)
483 (list (shepherd-service
484 (documentation "Run the nslcd service for resolving names from LDAP.")
486 (requirement '(networking user-processes))
487 (start #~(make-forkexec-constructor
488 (list (string-append #$(nslcd-configuration-nss-pam-ldapd config)
491 #:pid-file "/var/run/nslcd/nslcd.pid"
492 #:environment-variables
493 (list (string-append "LD_LIBRARY_PATH="
494 #$(nslcd-configuration-nss-pam-ldapd config)
496 (stop #~(make-kill-destructor)))))
498 (define (pam-ldap-pam-service config)
499 "Return a PAM service for LDAP authentication."
500 (define pam-ldap-module
501 #~(string-append #$(nslcd-configuration-nss-pam-ldapd config)
502 "/lib/security/pam_ldap.so"))
504 (if (member (pam-service-name pam)
505 (nslcd-configuration-pam-services config))
508 (control "sufficient")
509 (module pam-ldap-module))))
512 (auth (cons sufficient (pam-service-auth pam)))
513 (session (cons sufficient (pam-service-session pam)))
514 (account (cons sufficient (pam-service-account pam)))))
517 (define (pam-ldap-pam-services config)
518 (list (pam-ldap-pam-service config)))
520 (define nslcd-service-type
523 (description "Run the NSLCD service for looking up names from LDAP.")
525 (list (service-extension account-service-type
526 (const %nslcd-accounts))
527 (service-extension etc-service-type
529 (service-extension activation-service-type
531 (use-modules (guix build utils))
532 (let ((rundir "/var/run/nslcd")
533 (user (getpwnam "nslcd")))
535 (chown rundir (passwd:uid user) (passwd:gid user))
537 (when (file-exists? "/etc/nslcd.conf")
538 (chmod "/etc/nslcd.conf" #o400))))))
539 (service-extension pam-root-service-type
540 pam-ldap-pam-services)
541 (service-extension nscd-service-type
542 (const (list nss-pam-ldapd)))
543 (service-extension shepherd-root-service-type
544 nslcd-shepherd-service)))
545 (default-value (nslcd-configuration))))
547 (define (generate-nslcd-documentation)
548 (generate-documentation
549 `((nslcd-configuration ,nslcd-configuration-fields))
550 'nslcd-configuration))