| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 |
|
| 19 | (define-module (gnu system linux)
|
| 20 | #:use-module (guix store)
|
| 21 | #:use-module (guix records)
|
| 22 | #:use-module (guix derivations)
|
| 23 | #:use-module (guix monads)
|
| 24 | #:use-module (guix gexp)
|
| 25 | #:use-module (ice-9 match)
|
| 26 | #:use-module (srfi srfi-1)
|
| 27 | #:use-module (srfi srfi-26)
|
| 28 | #:use-module ((guix utils) #:select (%current-system))
|
| 29 | #:export (pam-service
|
| 30 | pam-entry
|
| 31 | pam-services->directory
|
| 32 | unix-pam-service
|
| 33 | base-pam-services))
|
| 34 |
|
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 |
|
| 42 | |
| 43 | |
| 44 | (define-record-type* <pam-service> pam-service
|
| 45 | make-pam-service
|
| 46 | pam-service?
|
| 47 | (name pam-service-name) |
| 48 |
|
| 49 | |
| 50 | (account pam-service-account |
| 51 | (default '()))
|
| 52 | (auth pam-service-auth
|
| 53 | (default '()))
|
| 54 | (password pam-service-password
|
| 55 | (default '()))
|
| 56 | (session pam-service-session
|
| 57 | (default '())))
|
| 58 |
|
| 59 | (define-record-type* <pam-entry> pam-entry
|
| 60 | make-pam-entry
|
| 61 | pam-entry?
|
| 62 | (control pam-entry-control) |
| 63 | (module pam-entry-module) |
| 64 | (arguments pam-entry-arguments |
| 65 | (default '())))
|
| 66 |
|
| 67 | (define (pam-service->configuration service)
|
| 68 | "Return the derivation building the configuration file for SERVICE, to be
|
| 69 | dumped in /etc/pam.d/NAME, where NAME is the name of SERVICE."
|
| 70 | (define (entry->gexp type entry)
|
| 71 | (match entry
|
| 72 | (($ <pam-entry> control module (arguments ...))
|
| 73 | #~(format #t "~a ~a ~a ~a~%"
|
| 74 | #$type #$control #$module
|
| 75 | (string-join (list #$@arguments))))))
|
| 76 |
|
| 77 | (match service
|
| 78 | (($ <pam-service> name account auth password session)
|
| 79 | (define builder
|
| 80 | #~(begin
|
| 81 | (with-output-to-file #$output
|
| 82 | (lambda ()
|
| 83 | #$@(append (map (cut entry->gexp "account" <>) account)
|
| 84 | (map (cut entry->gexp "auth" <>) auth)
|
| 85 | (map (cut entry->gexp "password" <>) password)
|
| 86 | (map (cut entry->gexp "session" <>) session))
|
| 87 | #t))))
|
| 88 |
|
| 89 | (gexp->derivation name builder))))
|
| 90 |
|
| 91 | (define (pam-services->directory services)
|
| 92 | "Return the derivation to build the configuration directory to be used as
|
| 93 | /etc/pam.d for SERVICES."
|
| 94 | (mlet %store-monad
|
| 95 | ((names -> (map pam-service-name services))
|
| 96 | (files (sequence %store-monad
|
| 97 | (map pam-service->configuration
|
| 98 | |
| 99 | |
| 100 | services))))
|
| 101 | (define builder
|
| 102 | #~(begin
|
| 103 | (use-modules (ice-9 match)
|
| 104 | (srfi srfi-1))
|
| 105 |
|
| 106 | (mkdir #$output)
|
| 107 | (for-each (match-lambda
|
| 108 | ((name file)
|
| 109 | (symlink file (string-append #$output "/" name))))
|
| 110 |
|
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | (delete-duplicates '#$(zip names files)))))
|
| 116 |
|
| 117 | (gexp->derivation "pam.d" builder)))
|
| 118 |
|
| 119 | (define %pam-other-services
|
| 120 | |
| 121 | |
| 122 | (let ((deny (pam-entry
|
| 123 | (control "required")
|
| 124 | (module "pam_deny.so"))))
|
| 125 | (pam-service
|
| 126 | (name "other")
|
| 127 | (account (list deny))
|
| 128 | (auth (list deny))
|
| 129 | (password (list deny))
|
| 130 | (session (list deny)))))
|
| 131 |
|
| 132 | (define unix-pam-service
|
| 133 | (let ((unix (pam-entry
|
| 134 | (control "required")
|
| 135 | (module "pam_unix.so"))))
|
| 136 | (lambda* (name #:key allow-empty-passwords? motd)
|
| 137 | "Return a standard Unix-style PAM service for NAME. When
|
| 138 | ALLOW-EMPTY-PASSWORDS? is true, allow empty passwords. When MOTD is true, it
|
| 139 | should be the name of a file used as the message-of-the-day."
|
| 140 | |
| 141 | (let ((name* name))
|
| 142 | (pam-service
|
| 143 | (name name*)
|
| 144 | (account (list unix))
|
| 145 | (auth (list (if allow-empty-passwords?
|
| 146 | (pam-entry
|
| 147 | (control "required")
|
| 148 | (module "pam_unix.so")
|
| 149 | (arguments '("nullok")))
|
| 150 | unix)))
|
| 151 | (password (list unix))
|
| 152 | (session (if motd
|
| 153 | (list unix
|
| 154 | (pam-entry
|
| 155 | (control "optional")
|
| 156 | (module "pam_motd.so")
|
| 157 | (arguments
|
| 158 | (list #~(string-append "motd=" #$motd)))))
|
| 159 | (list unix))))))))
|
| 160 |
|
| 161 | (define (rootok-pam-service command)
|
| 162 | "Return a PAM service for COMMAND such that 'root' does not need to
|
| 163 | authenticate to run COMMAND."
|
| 164 | (let ((unix (pam-entry
|
| 165 | (control "required")
|
| 166 | (module "pam_unix.so"))))
|
| 167 | (pam-service
|
| 168 | (name command)
|
| 169 | (account (list unix))
|
| 170 | (auth (list (pam-entry
|
| 171 | (control "sufficient")
|
| 172 | (module "pam_rootok.so"))))
|
| 173 | (password (list unix))
|
| 174 | (session (list unix)))))
|
| 175 |
|
| 176 | (define* (base-pam-services #:key allow-empty-passwords?)
|
| 177 | "Return the list of basic PAM services everyone would want."
|
| 178 | |
| 179 | (append (list %pam-other-services)
|
| 180 |
|
| 181 | |
| 182 | (map (cut unix-pam-service <>
|
| 183 | #:allow-empty-passwords? allow-empty-passwords?)
|
| 184 | '("su" "passwd" "sudo"
|
| 185 | "xlock" "xscreensaver"))
|
| 186 |
|
| 187 | |
| 188 | |
| 189 | |
| 190 | (map rootok-pam-service
|
| 191 | '("useradd" "userdel" "usermod"
|
| 192 | "groupadd" "groupdel" "groupmod"))))
|
| 193 |
|
| 194 | |