]>
Commit | Line | Data |
---|---|---|
26cf9fb7 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <security/pam_ext.h> | |
4 | #include <security/pam_modules.h> | |
5 | ||
6 | #include "sd-bus.h" | |
7 | ||
8 | #include "bus-common-errors.h" | |
9b71e4ab | 9 | #include "bus-locator.h" |
26cf9fb7 LP |
10 | #include "errno-util.h" |
11 | #include "fd-util.h" | |
12 | #include "home-util.h" | |
13 | #include "memory-util.h" | |
14 | #include "pam-util.h" | |
15 | #include "parse-util.h" | |
16 | #include "strv.h" | |
17 | #include "user-record-util.h" | |
18 | #include "user-record.h" | |
19 | #include "user-util.h" | |
20 | ||
26cf9fb7 LP |
21 | static int parse_argv( |
22 | pam_handle_t *handle, | |
23 | int argc, const char **argv, | |
24 | bool *please_suspend, | |
25 | bool *debug) { | |
26 | ||
27 | int i; | |
28 | ||
29 | assert(argc >= 0); | |
30 | assert(argc == 0 || argv); | |
31 | ||
32 | for (i = 0; i < argc; i++) { | |
33 | const char *v; | |
34 | ||
f12d19b3 | 35 | if ((v = startswith(argv[i], "suspend="))) { |
26cf9fb7 LP |
36 | int k; |
37 | ||
38 | k = parse_boolean(v); | |
39 | if (k < 0) | |
80ace4f2 | 40 | pam_syslog(handle, LOG_WARNING, "Failed to parse suspend= argument, ignoring: %s", v); |
26cf9fb7 LP |
41 | else if (please_suspend) |
42 | *please_suspend = k; | |
43 | ||
332f38d0 JS |
44 | } else if (streq(argv[i], "debug")) { |
45 | if (debug) | |
46 | *debug = true; | |
47 | ||
26cf9fb7 LP |
48 | } else if ((v = startswith(argv[i], "debug="))) { |
49 | int k; | |
26cf9fb7 LP |
50 | k = parse_boolean(v); |
51 | if (k < 0) | |
52 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", v); | |
53 | else if (debug) | |
54 | *debug = k; | |
55 | ||
56 | } else | |
57 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]); | |
58 | } | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
764ae4dd LP |
63 | static int parse_env( |
64 | pam_handle_t *handle, | |
65 | bool *please_suspend) { | |
66 | ||
67 | const char *v; | |
68 | int r; | |
69 | ||
70 | /* Let's read the suspend setting from an env var in addition to the PAM command line. That makes it | |
71 | * easy to declare the features of a display manager in code rather than configuration, and this is | |
72 | * really a feature of code */ | |
73 | ||
74 | v = pam_getenv(handle, "SYSTEMD_HOME_SUSPEND"); | |
75 | if (!v) { | |
76 | /* Also check the process env block, so that people can control this via an env var from the | |
77 | * outside of our process. */ | |
78 | v = secure_getenv("SYSTEMD_HOME_SUSPEND"); | |
79 | if (!v) | |
80 | return 0; | |
81 | } | |
82 | ||
83 | r = parse_boolean(v); | |
84 | if (r < 0) | |
85 | pam_syslog(handle, LOG_WARNING, "Failed to parse $SYSTEMD_HOME_SUSPEND argument, ignoring: %s", v); | |
86 | else if (please_suspend) | |
87 | *please_suspend = r; | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
26cf9fb7 LP |
92 | static int acquire_user_record( |
93 | pam_handle_t *handle, | |
6c8428bb | 94 | const char *username, |
26cf9fb7 LP |
95 | UserRecord **ret_record) { |
96 | ||
97 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
98 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
99 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; | |
100 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
dbe7fff4 | 101 | _cleanup_free_ char *homed_field = NULL; |
6c8428bb | 102 | const char *json = NULL; |
26cf9fb7 LP |
103 | int r; |
104 | ||
105 | assert(handle); | |
106 | ||
6c8428bb LP |
107 | if (!username) { |
108 | r = pam_get_user(handle, &username, NULL); | |
109 | if (r != PAM_SUCCESS) { | |
110 | pam_syslog(handle, LOG_ERR, "Failed to get user name: %s", pam_strerror(handle, r)); | |
111 | return r; | |
112 | } | |
26cf9fb7 | 113 | |
6c8428bb LP |
114 | if (isempty(username)) { |
115 | pam_syslog(handle, LOG_ERR, "User name not set."); | |
116 | return PAM_SERVICE_ERR; | |
117 | } | |
26cf9fb7 LP |
118 | } |
119 | ||
120 | /* Let's bypass all IPC complexity for the two user names we know for sure we don't manage, and for | |
121 | * user names we don't consider valid. */ | |
7a8867ab | 122 | if (STR_IN_SET(username, "root", NOBODY_USER_NAME) || !valid_user_group_name(username, 0)) |
26cf9fb7 LP |
123 | return PAM_USER_UNKNOWN; |
124 | ||
dbe7fff4 LP |
125 | /* We cache the user record in the PAM context. We use a field name that includes the username, since |
126 | * clients might change the user name associated with a PAM context underneath us. Notably, 'sudo' | |
127 | * creates a single PAM context and first authenticates it with the user set to the originating user, | |
128 | * then updates the user for the destination user and issues the session stack with the same PAM | |
129 | * context. We thus must be prepared that the user record changes between calls and we keep any | |
130 | * caching separate. */ | |
131 | homed_field = strjoin("systemd-home-user-record-", username); | |
132 | if (!homed_field) | |
133 | return pam_log_oom(handle); | |
134 | ||
135 | /* Let's use the cache, so that we can share it between the session and the authentication hooks */ | |
136 | r = pam_get_data(handle, homed_field, (const void**) &json); | |
26cf9fb7 | 137 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) { |
dbe7fff4 | 138 | pam_syslog(handle, LOG_ERR, "Failed to get PAM user record data: %s", pam_strerror(handle, r)); |
26cf9fb7 | 139 | return r; |
26cf9fb7 | 140 | } |
dbe7fff4 LP |
141 | if (r == PAM_SUCCESS && json) { |
142 | /* We determined earlier that this is not a homed user? Then exit early. (We use -1 as | |
143 | * negative cache indicator) */ | |
144 | if (json == (void*) -1) | |
145 | return PAM_USER_UNKNOWN; | |
146 | } else { | |
26cf9fb7 | 147 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
dbe7fff4 | 148 | _cleanup_free_ char *generic_field = NULL, *json_copy = NULL; |
26cf9fb7 LP |
149 | |
150 | r = pam_acquire_bus_connection(handle, &bus); | |
151 | if (r != PAM_SUCCESS) | |
152 | return r; | |
153 | ||
8a1596aa | 154 | r = bus_call_method(bus, bus_home_mgr, "GetUserRecordByName", &error, &reply, "s", username); |
26cf9fb7 LP |
155 | if (r < 0) { |
156 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_SERVICE_UNKNOWN) || | |
157 | sd_bus_error_has_name(&error, SD_BUS_ERROR_NAME_HAS_NO_OWNER)) { | |
158 | pam_syslog(handle, LOG_DEBUG, "systemd-homed is not available: %s", bus_error_message(&error, r)); | |
159 | goto user_unknown; | |
160 | } | |
161 | ||
162 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_HOME)) { | |
163 | pam_syslog(handle, LOG_DEBUG, "Not a user managed by systemd-homed: %s", bus_error_message(&error, r)); | |
164 | goto user_unknown; | |
165 | } | |
166 | ||
167 | pam_syslog(handle, LOG_ERR, "Failed to query user record: %s", bus_error_message(&error, r)); | |
168 | return PAM_SERVICE_ERR; | |
169 | } | |
170 | ||
171 | r = sd_bus_message_read(reply, "sbo", &json, NULL, NULL); | |
172 | if (r < 0) | |
173 | return pam_bus_log_parse_error(handle, r); | |
174 | ||
dbe7fff4 LP |
175 | /* First copy: for the homed-specific data field, i.e. where we know the user record is from |
176 | * homed */ | |
26cf9fb7 LP |
177 | json_copy = strdup(json); |
178 | if (!json_copy) | |
179 | return pam_log_oom(handle); | |
180 | ||
dbe7fff4 | 181 | r = pam_set_data(handle, homed_field, json_copy, pam_cleanup_free); |
26cf9fb7 | 182 | if (r != PAM_SUCCESS) { |
dbe7fff4 LP |
183 | pam_syslog(handle, LOG_ERR, "Failed to set PAM user record data '%s': %s", |
184 | homed_field, pam_strerror(handle, r)); | |
26cf9fb7 LP |
185 | return r; |
186 | } | |
187 | ||
dbe7fff4 LP |
188 | /* Take a second copy: for the generic data field, the one which we share with |
189 | * pam_systemd. While we insist on only reusing homed records, pam_systemd is fine with homed | |
190 | * and non-homed user records. */ | |
191 | json_copy = strdup(json); | |
192 | if (!json_copy) | |
193 | return pam_log_oom(handle); | |
194 | ||
195 | generic_field = strjoin("systemd-user-record-", username); | |
196 | if (!generic_field) | |
197 | return pam_log_oom(handle); | |
26cf9fb7 | 198 | |
dbe7fff4 | 199 | r = pam_set_data(handle, generic_field, json_copy, pam_cleanup_free); |
26cf9fb7 | 200 | if (r != PAM_SUCCESS) { |
dbe7fff4 LP |
201 | pam_syslog(handle, LOG_ERR, "Failed to set PAM user record data '%s': %s", |
202 | homed_field, pam_strerror(handle, r)); | |
26cf9fb7 LP |
203 | return r; |
204 | } | |
dbe7fff4 LP |
205 | |
206 | TAKE_PTR(json_copy); | |
26cf9fb7 LP |
207 | } |
208 | ||
209 | r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); | |
210 | if (r < 0) { | |
211 | pam_syslog(handle, LOG_ERR, "Failed to parse JSON user record: %s", strerror_safe(r)); | |
212 | return PAM_SERVICE_ERR; | |
213 | } | |
214 | ||
215 | ur = user_record_new(); | |
216 | if (!ur) | |
217 | return pam_log_oom(handle); | |
218 | ||
219 | r = user_record_load(ur, v, USER_RECORD_LOAD_REFUSE_SECRET); | |
220 | if (r < 0) { | |
221 | pam_syslog(handle, LOG_ERR, "Failed to load user record: %s", strerror_safe(r)); | |
222 | return PAM_SERVICE_ERR; | |
223 | } | |
224 | ||
dbe7fff4 | 225 | /* Safety check if cached record actually matches what we are looking for */ |
26cf9fb7 LP |
226 | if (!streq_ptr(username, ur->user_name)) { |
227 | pam_syslog(handle, LOG_ERR, "Acquired user record does not match user name."); | |
228 | return PAM_SERVICE_ERR; | |
229 | } | |
230 | ||
231 | if (ret_record) | |
232 | *ret_record = TAKE_PTR(ur); | |
233 | ||
234 | return PAM_SUCCESS; | |
235 | ||
236 | user_unknown: | |
237 | /* Cache this, so that we don't check again */ | |
dbe7fff4 | 238 | r = pam_set_data(handle, homed_field, (void*) -1, NULL); |
26cf9fb7 | 239 | if (r != PAM_SUCCESS) |
dbe7fff4 LP |
240 | pam_syslog(handle, LOG_ERR, "Failed to set PAM user record data '%s' to invalid, ignoring: %s", |
241 | homed_field, pam_strerror(handle, r)); | |
26cf9fb7 LP |
242 | |
243 | return PAM_USER_UNKNOWN; | |
244 | } | |
245 | ||
dbe7fff4 LP |
246 | static int release_user_record(pam_handle_t *handle, const char *username) { |
247 | _cleanup_free_ char *homed_field = NULL, *generic_field = NULL; | |
26cf9fb7 LP |
248 | int r, k; |
249 | ||
dbe7fff4 LP |
250 | assert(handle); |
251 | assert(username); | |
252 | ||
253 | homed_field = strjoin("systemd-home-user-record-", username); | |
254 | if (!homed_field) | |
255 | return pam_log_oom(handle); | |
256 | ||
257 | r = pam_set_data(handle, homed_field, NULL, NULL); | |
26cf9fb7 | 258 | if (r != PAM_SUCCESS) |
dbe7fff4 LP |
259 | pam_syslog(handle, LOG_ERR, "Failed to release PAM user record data '%s': %s", homed_field, pam_strerror(handle, r)); |
260 | ||
261 | generic_field = strjoin("systemd-user-record-", username); | |
262 | if (!generic_field) | |
263 | return pam_log_oom(handle); | |
26cf9fb7 | 264 | |
dbe7fff4 | 265 | k = pam_set_data(handle, generic_field, NULL, NULL); |
26cf9fb7 | 266 | if (k != PAM_SUCCESS) |
dbe7fff4 | 267 | pam_syslog(handle, LOG_ERR, "Failed to release PAM user record data '%s': %s", generic_field, pam_strerror(handle, k)); |
26cf9fb7 LP |
268 | |
269 | return IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA) ? k : r; | |
270 | } | |
271 | ||
272 | static void cleanup_home_fd(pam_handle_t *handle, void *data, int error_status) { | |
273 | safe_close(PTR_TO_FD(data)); | |
274 | } | |
275 | ||
276 | static int handle_generic_user_record_error( | |
277 | pam_handle_t *handle, | |
278 | const char *user_name, | |
279 | UserRecord *secret, | |
280 | int ret, | |
281 | const sd_bus_error *error) { | |
282 | ||
283 | assert(user_name); | |
284 | assert(secret); | |
285 | assert(error); | |
286 | ||
287 | int r; | |
288 | ||
289 | /* Logs about all errors, except for PAM_CONV_ERR, i.e. when requesting more info failed. */ | |
290 | ||
291 | if (sd_bus_error_has_name(error, BUS_ERROR_HOME_ABSENT)) { | |
292 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Home of user %s is currently absent, please plug in the necessary storage device or backing file system.", user_name); | |
293 | pam_syslog(handle, LOG_ERR, "Failed to acquire home for user %s: %s", user_name, bus_error_message(error, ret)); | |
294 | return PAM_PERM_DENIED; | |
295 | ||
296 | } else if (sd_bus_error_has_name(error, BUS_ERROR_AUTHENTICATION_LIMIT_HIT)) { | |
297 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Too frequent unsuccessful login attempts for user %s, try again later.", user_name); | |
298 | pam_syslog(handle, LOG_ERR, "Failed to acquire home for user %s: %s", user_name, bus_error_message(error, ret)); | |
299 | return PAM_MAXTRIES; | |
300 | ||
301 | } else if (sd_bus_error_has_name(error, BUS_ERROR_BAD_PASSWORD)) { | |
302 | _cleanup_(erase_and_freep) char *newp = NULL; | |
303 | ||
304 | /* This didn't work? Ask for an (additional?) password */ | |
305 | ||
306 | if (strv_isempty(secret->password)) | |
307 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Password: "); | |
d4232943 JS |
308 | else { |
309 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password incorrect or not sufficient for authentication of user %s.", user_name); | |
310 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Sorry, try again: "); | |
311 | } | |
26cf9fb7 LP |
312 | if (r != PAM_SUCCESS) |
313 | return PAM_CONV_ERR; /* no logging here */ | |
314 | ||
315 | if (isempty(newp)) { | |
316 | pam_syslog(handle, LOG_DEBUG, "Password request aborted."); | |
317 | return PAM_AUTHTOK_ERR; | |
318 | } | |
319 | ||
320 | r = user_record_set_password(secret, STRV_MAKE(newp), true); | |
321 | if (r < 0) { | |
322 | pam_syslog(handle, LOG_ERR, "Failed to store password: %s", strerror_safe(r)); | |
323 | return PAM_SERVICE_ERR; | |
324 | } | |
325 | ||
326 | } else if (sd_bus_error_has_name(error, BUS_ERROR_BAD_PASSWORD_AND_NO_TOKEN)) { | |
327 | _cleanup_(erase_and_freep) char *newp = NULL; | |
328 | ||
d4232943 JS |
329 | if (strv_isempty(secret->password)) { |
330 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Security token of user %s not inserted.", user_name); | |
331 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Try again with password: "); | |
332 | } else { | |
333 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password incorrect or not sufficient, and configured security token of user %s not inserted.", user_name); | |
334 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Try again with password: "); | |
335 | } | |
26cf9fb7 LP |
336 | if (r != PAM_SUCCESS) |
337 | return PAM_CONV_ERR; /* no logging here */ | |
338 | ||
339 | if (isempty(newp)) { | |
340 | pam_syslog(handle, LOG_DEBUG, "Password request aborted."); | |
341 | return PAM_AUTHTOK_ERR; | |
342 | } | |
343 | ||
344 | r = user_record_set_password(secret, STRV_MAKE(newp), true); | |
345 | if (r < 0) { | |
346 | pam_syslog(handle, LOG_ERR, "Failed to store password: %s", strerror_safe(r)); | |
347 | return PAM_SERVICE_ERR; | |
348 | } | |
349 | ||
350 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_PIN_NEEDED)) { | |
351 | _cleanup_(erase_and_freep) char *newp = NULL; | |
352 | ||
d4232943 | 353 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Security token PIN: "); |
26cf9fb7 LP |
354 | if (r != PAM_SUCCESS) |
355 | return PAM_CONV_ERR; /* no logging here */ | |
356 | ||
357 | if (isempty(newp)) { | |
358 | pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); | |
359 | return PAM_AUTHTOK_ERR; | |
360 | } | |
361 | ||
c0bde0d2 | 362 | r = user_record_set_token_pin(secret, STRV_MAKE(newp), false); |
26cf9fb7 LP |
363 | if (r < 0) { |
364 | pam_syslog(handle, LOG_ERR, "Failed to store PIN: %s", strerror_safe(r)); | |
365 | return PAM_SERVICE_ERR; | |
366 | } | |
367 | ||
368 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_PROTECTED_AUTHENTICATION_PATH_NEEDED)) { | |
369 | ||
370 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Please authenticate physically on security token of user %s.", user_name); | |
371 | ||
372 | r = user_record_set_pkcs11_protected_authentication_path_permitted(secret, true); | |
373 | if (r < 0) { | |
374 | pam_syslog(handle, LOG_ERR, "Failed to set PKCS#11 protected authentication path permitted flag: %s", strerror_safe(r)); | |
375 | return PAM_SERVICE_ERR; | |
376 | } | |
377 | ||
7b78db28 LP |
378 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_USER_PRESENCE_NEEDED)) { |
379 | ||
380 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Please verify presence on security token of user %s.", user_name); | |
381 | ||
382 | r = user_record_set_fido2_user_presence_permitted(secret, true); | |
383 | if (r < 0) { | |
384 | pam_syslog(handle, LOG_ERR, "Failed to set FIDO2 user presence permitted flag: %s", strerror_safe(r)); | |
385 | return PAM_SERVICE_ERR; | |
386 | } | |
387 | ||
26cf9fb7 LP |
388 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN)) { |
389 | _cleanup_(erase_and_freep) char *newp = NULL; | |
390 | ||
d4232943 JS |
391 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Security token PIN incorrect for user %s.", user_name); |
392 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Sorry, retry security token PIN: "); | |
26cf9fb7 LP |
393 | if (r != PAM_SUCCESS) |
394 | return PAM_CONV_ERR; /* no logging here */ | |
395 | ||
396 | if (isempty(newp)) { | |
397 | pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); | |
398 | return PAM_AUTHTOK_ERR; | |
399 | } | |
400 | ||
c0bde0d2 | 401 | r = user_record_set_token_pin(secret, STRV_MAKE(newp), false); |
26cf9fb7 LP |
402 | if (r < 0) { |
403 | pam_syslog(handle, LOG_ERR, "Failed to store PIN: %s", strerror_safe(r)); | |
404 | return PAM_SERVICE_ERR; | |
405 | } | |
406 | ||
407 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN_FEW_TRIES_LEFT)) { | |
408 | _cleanup_(erase_and_freep) char *newp = NULL; | |
409 | ||
d4232943 JS |
410 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Security token PIN of user %s incorrect (only a few tries left!)", user_name); |
411 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Sorry, retry security token PIN: "); | |
26cf9fb7 LP |
412 | if (r != PAM_SUCCESS) |
413 | return PAM_CONV_ERR; /* no logging here */ | |
414 | ||
415 | if (isempty(newp)) { | |
416 | pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); | |
417 | return PAM_AUTHTOK_ERR; | |
418 | } | |
419 | ||
c0bde0d2 | 420 | r = user_record_set_token_pin(secret, STRV_MAKE(newp), false); |
26cf9fb7 LP |
421 | if (r < 0) { |
422 | pam_syslog(handle, LOG_ERR, "Failed to store PIN: %s", strerror_safe(r)); | |
423 | return PAM_SERVICE_ERR; | |
424 | } | |
425 | ||
426 | } else if (sd_bus_error_has_name(error, BUS_ERROR_TOKEN_BAD_PIN_ONE_TRY_LEFT)) { | |
427 | _cleanup_(erase_and_freep) char *newp = NULL; | |
428 | ||
d4232943 JS |
429 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Security token PIN of user %s incorrect (only one try left!)", user_name); |
430 | r = pam_prompt(handle, PAM_PROMPT_ECHO_OFF, &newp, "Sorry, retry security token PIN: "); | |
26cf9fb7 LP |
431 | if (r != PAM_SUCCESS) |
432 | return PAM_CONV_ERR; /* no logging here */ | |
433 | ||
434 | if (isempty(newp)) { | |
435 | pam_syslog(handle, LOG_DEBUG, "PIN request aborted."); | |
436 | return PAM_AUTHTOK_ERR; | |
437 | } | |
438 | ||
c0bde0d2 | 439 | r = user_record_set_token_pin(secret, STRV_MAKE(newp), false); |
26cf9fb7 LP |
440 | if (r < 0) { |
441 | pam_syslog(handle, LOG_ERR, "Failed to store PIN: %s", strerror_safe(r)); | |
442 | return PAM_SERVICE_ERR; | |
443 | } | |
444 | ||
445 | } else { | |
446 | pam_syslog(handle, LOG_ERR, "Failed to acquire home for user %s: %s", user_name, bus_error_message(error, ret)); | |
447 | return PAM_SERVICE_ERR; | |
448 | } | |
449 | ||
450 | return PAM_SUCCESS; | |
451 | } | |
452 | ||
453 | static int acquire_home( | |
454 | pam_handle_t *handle, | |
455 | bool please_authenticate, | |
456 | bool please_suspend, | |
457 | bool debug) { | |
458 | ||
459 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *secret = NULL; | |
460 | bool do_auth = please_authenticate, home_not_active = false, home_locked = false; | |
461 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
462 | _cleanup_close_ int acquired_fd = -1; | |
6c8428bb | 463 | _cleanup_free_ char *fd_field = NULL; |
26cf9fb7 | 464 | const void *home_fd_ptr = NULL; |
6c8428bb | 465 | const char *username = NULL; |
26cf9fb7 LP |
466 | unsigned n_attempts = 0; |
467 | int r; | |
468 | ||
469 | assert(handle); | |
470 | ||
471 | /* This acquires a reference to a home directory in one of two ways: if please_authenticate is true, | |
472 | * then we'll call AcquireHome() after asking the user for a password. Otherwise it tries to call | |
473 | * RefHome() and if that fails queries the user for a password and uses AcquireHome(). | |
474 | * | |
475 | * The idea is that the PAM authentication hook sets please_authenticate and thus always | |
476 | * authenticates, while the other PAM hooks unset it so that they can a ref of their own without | |
477 | * authentication if possible, but with authentication if necessary. */ | |
478 | ||
6c8428bb LP |
479 | r = pam_get_user(handle, &username, NULL); |
480 | if (r != PAM_SUCCESS) { | |
481 | pam_syslog(handle, LOG_ERR, "Failed to get user name: %s", pam_strerror(handle, r)); | |
482 | return r; | |
483 | } | |
484 | ||
485 | if (isempty(username)) { | |
486 | pam_syslog(handle, LOG_ERR, "User name not set."); | |
487 | return PAM_SERVICE_ERR; | |
488 | } | |
489 | ||
26cf9fb7 | 490 | /* If we already have acquired the fd, let's shortcut this */ |
6c8428bb LP |
491 | fd_field = strjoin("systemd-home-fd-", username); |
492 | if (!fd_field) | |
493 | return pam_log_oom(handle); | |
494 | ||
495 | r = pam_get_data(handle, fd_field, &home_fd_ptr); | |
496 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) { | |
497 | pam_syslog(handle, LOG_ERR, "Failed to retrieve PAM home reference fd: %s", pam_strerror(handle, r)); | |
498 | return r; | |
499 | } | |
da4340fd | 500 | if (r == PAM_SUCCESS && PTR_TO_FD(home_fd_ptr) >= 0) |
26cf9fb7 LP |
501 | return PAM_SUCCESS; |
502 | ||
503 | r = pam_acquire_bus_connection(handle, &bus); | |
504 | if (r != PAM_SUCCESS) | |
505 | return r; | |
506 | ||
6c8428bb | 507 | r = acquire_user_record(handle, username, &ur); |
26cf9fb7 LP |
508 | if (r != PAM_SUCCESS) |
509 | return r; | |
510 | ||
511 | /* Implement our own retry loop here instead of relying on the PAM client's one. That's because it | |
512 | * might happen that the the record we stored on the host does not match the encryption password of | |
513 | * the LUKS image in case the image was used in a different system where the password was | |
514 | * changed. In that case it will happen that the LUKS password and the host password are | |
515 | * different, and we handle that by collecting and passing multiple passwords in that case. Hence we | |
516 | * treat bad passwords as a request to collect one more password and pass the new all all previously | |
517 | * used passwords again. */ | |
518 | ||
519 | for (;;) { | |
520 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
521 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
522 | ||
523 | if (do_auth && !secret) { | |
524 | const char *cached_password = NULL; | |
525 | ||
526 | secret = user_record_new(); | |
527 | if (!secret) | |
528 | return pam_log_oom(handle); | |
529 | ||
530 | /* If there's already a cached password, use it. But if not let's authenticate | |
531 | * without anything, maybe some other authentication mechanism systemd-homed | |
532 | * implements (such as PKCS#11) allows us to authenticate without anything else. */ | |
533 | r = pam_get_item(handle, PAM_AUTHTOK, (const void**) &cached_password); | |
534 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) { | |
535 | pam_syslog(handle, LOG_ERR, "Failed to get cached password: %s", pam_strerror(handle, r)); | |
536 | return r; | |
537 | } | |
538 | ||
539 | if (!isempty(cached_password)) { | |
540 | r = user_record_set_password(secret, STRV_MAKE(cached_password), true); | |
541 | if (r < 0) { | |
542 | pam_syslog(handle, LOG_ERR, "Failed to store password: %s", strerror_safe(r)); | |
543 | return PAM_SERVICE_ERR; | |
544 | } | |
545 | } | |
546 | } | |
547 | ||
8a1596aa | 548 | r = bus_message_new_method_call(bus, &m, bus_home_mgr, do_auth ? "AcquireHome" : "RefHome"); |
26cf9fb7 LP |
549 | if (r < 0) |
550 | return pam_bus_log_create_error(handle, r); | |
551 | ||
552 | r = sd_bus_message_append(m, "s", ur->user_name); | |
553 | if (r < 0) | |
554 | return pam_bus_log_create_error(handle, r); | |
555 | ||
556 | if (do_auth) { | |
557 | r = bus_message_append_secret(m, secret); | |
558 | if (r < 0) | |
559 | return pam_bus_log_create_error(handle, r); | |
560 | } | |
561 | ||
562 | r = sd_bus_message_append(m, "b", please_suspend); | |
563 | if (r < 0) | |
564 | return pam_bus_log_create_error(handle, r); | |
565 | ||
566 | r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); | |
567 | if (r < 0) { | |
568 | ||
569 | if (sd_bus_error_has_name(&error, BUS_ERROR_HOME_NOT_ACTIVE)) | |
570 | /* Only on RefHome(): We can't access the home directory currently, unless | |
571 | * it's unlocked with a password. Hence, let's try this again, this time with | |
572 | * authentication. */ | |
573 | home_not_active = true; | |
574 | else if (sd_bus_error_has_name(&error, BUS_ERROR_HOME_LOCKED)) | |
575 | home_locked = true; /* Similar */ | |
576 | else { | |
577 | r = handle_generic_user_record_error(handle, ur->user_name, secret, r, &error); | |
578 | if (r == PAM_CONV_ERR) { | |
579 | /* Password/PIN prompts will fail in certain environments, for example when | |
580 | * we are called from OpenSSH's account or session hooks, or in systemd's | |
581 | * per-service PAM logic. In that case, print a friendly message and accept | |
582 | * failure. */ | |
583 | ||
584 | if (home_not_active) | |
585 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Home of user %s is currently not active, please log in locally first.", ur->user_name); | |
586 | if (home_locked) | |
587 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Home of user %s is currently locked, please unlock locally first.", ur->user_name); | |
588 | ||
589 | pam_syslog(handle, please_authenticate ? LOG_ERR : LOG_DEBUG, "Failed to prompt for password/prompt."); | |
590 | ||
591 | return home_not_active || home_locked ? PAM_PERM_DENIED : PAM_CONV_ERR; | |
592 | } | |
593 | if (r != PAM_SUCCESS) | |
594 | return r; | |
595 | } | |
596 | ||
597 | } else { | |
598 | int fd; | |
599 | ||
600 | r = sd_bus_message_read(reply, "h", &fd); | |
601 | if (r < 0) | |
602 | return pam_bus_log_parse_error(handle, r); | |
603 | ||
604 | acquired_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); | |
605 | if (acquired_fd < 0) { | |
606 | pam_syslog(handle, LOG_ERR, "Failed to duplicate acquired fd: %s", bus_error_message(&error, r)); | |
607 | return PAM_SERVICE_ERR; | |
608 | } | |
609 | ||
610 | break; | |
611 | } | |
612 | ||
613 | if (++n_attempts >= 5) { | |
614 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Too many unsuccessful login attempts for user %s, refusing.", ur->user_name); | |
615 | pam_syslog(handle, LOG_ERR, "Failed to acquire home for user %s: %s", ur->user_name, bus_error_message(&error, r)); | |
616 | return PAM_MAXTRIES; | |
617 | } | |
618 | ||
619 | /* Try again, this time with authentication if we didn't do that before. */ | |
620 | do_auth = true; | |
621 | } | |
622 | ||
6c8428bb | 623 | r = pam_set_data(handle, fd_field, FD_TO_PTR(acquired_fd), cleanup_home_fd); |
26cf9fb7 LP |
624 | if (r < 0) { |
625 | pam_syslog(handle, LOG_ERR, "Failed to set PAM bus data: %s", pam_strerror(handle, r)); | |
626 | return r; | |
627 | } | |
628 | TAKE_FD(acquired_fd); | |
629 | ||
630 | if (do_auth) { | |
631 | /* We likely just activated the home directory, let's flush out the user record, since a | |
632 | * newer embedded user record might have been acquired from the activation. */ | |
633 | ||
dbe7fff4 | 634 | r = release_user_record(handle, ur->user_name); |
26cf9fb7 LP |
635 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) |
636 | return r; | |
637 | } | |
638 | ||
639 | pam_syslog(handle, LOG_NOTICE, "Home for user %s successfully acquired.", ur->user_name); | |
640 | ||
641 | return PAM_SUCCESS; | |
642 | } | |
643 | ||
6c8428bb LP |
644 | static int release_home_fd(pam_handle_t *handle, const char *username) { |
645 | _cleanup_free_ char *fd_field = NULL; | |
26cf9fb7 LP |
646 | const void *home_fd_ptr = NULL; |
647 | int r; | |
648 | ||
6c8428bb LP |
649 | assert(handle); |
650 | assert(username); | |
651 | ||
652 | fd_field = strjoin("systemd-home-fd-", username); | |
653 | if (!fd_field) | |
654 | return pam_log_oom(handle); | |
655 | ||
656 | r = pam_get_data(handle, fd_field, &home_fd_ptr); | |
657 | if (r == PAM_NO_MODULE_DATA || (r == PAM_SUCCESS && PTR_TO_FD(home_fd_ptr) < 0)) | |
26cf9fb7 | 658 | return PAM_NO_MODULE_DATA; |
6c8428bb LP |
659 | if (r != PAM_SUCCESS) { |
660 | pam_syslog(handle, LOG_ERR, "Failed to retrieve PAM home reference fd: %s", pam_strerror(handle, r)); | |
661 | return r; | |
662 | } | |
26cf9fb7 | 663 | |
6c8428bb | 664 | r = pam_set_data(handle, fd_field, NULL, NULL); |
26cf9fb7 LP |
665 | if (r != PAM_SUCCESS) |
666 | pam_syslog(handle, LOG_ERR, "Failed to release PAM home reference fd: %s", pam_strerror(handle, r)); | |
667 | ||
668 | return r; | |
669 | } | |
670 | ||
671 | _public_ PAM_EXTERN int pam_sm_authenticate( | |
672 | pam_handle_t *handle, | |
673 | int flags, | |
674 | int argc, const char **argv) { | |
675 | ||
676 | bool debug = false, suspend_please = false; | |
677 | ||
764ae4dd LP |
678 | if (parse_env(handle, &suspend_please) < 0) |
679 | return PAM_AUTH_ERR; | |
680 | ||
26cf9fb7 LP |
681 | if (parse_argv(handle, |
682 | argc, argv, | |
683 | &suspend_please, | |
684 | &debug) < 0) | |
685 | return PAM_AUTH_ERR; | |
686 | ||
687 | if (debug) | |
688 | pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed authenticating"); | |
689 | ||
690 | return acquire_home(handle, /* please_authenticate= */ true, suspend_please, debug); | |
691 | } | |
692 | ||
693 | _public_ PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv) { | |
694 | return PAM_SUCCESS; | |
695 | } | |
696 | ||
697 | _public_ PAM_EXTERN int pam_sm_open_session( | |
698 | pam_handle_t *handle, | |
699 | int flags, | |
700 | int argc, const char **argv) { | |
701 | ||
702 | bool debug = false, suspend_please = false; | |
703 | int r; | |
704 | ||
764ae4dd LP |
705 | if (parse_env(handle, &suspend_please) < 0) |
706 | return PAM_SESSION_ERR; | |
707 | ||
26cf9fb7 LP |
708 | if (parse_argv(handle, |
709 | argc, argv, | |
710 | &suspend_please, | |
711 | &debug) < 0) | |
712 | return PAM_SESSION_ERR; | |
713 | ||
714 | if (debug) | |
715 | pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed session start"); | |
716 | ||
717 | r = acquire_home(handle, /* please_authenticate = */ false, suspend_please, debug); | |
718 | if (r == PAM_USER_UNKNOWN) /* Not managed by us? Don't complain. */ | |
719 | return PAM_SUCCESS; | |
720 | if (r != PAM_SUCCESS) | |
721 | return r; | |
722 | ||
723 | r = pam_putenv(handle, "SYSTEMD_HOME=1"); | |
724 | if (r != PAM_SUCCESS) { | |
725 | pam_syslog(handle, LOG_ERR, "Failed to set PAM environment variable $SYSTEMD_HOME: %s", pam_strerror(handle, r)); | |
726 | return r; | |
727 | } | |
728 | ||
764ae4dd LP |
729 | r = pam_putenv(handle, suspend_please ? "SYSTEMD_HOME_SUSPEND=1" : "SYSTEMD_HOME_SUSPEND=0"); |
730 | if (r != PAM_SUCCESS) { | |
731 | pam_syslog(handle, LOG_ERR, "Failed to set PAM environment variable $SYSTEMD_HOME_SUSPEND: %s", pam_strerror(handle, r)); | |
732 | return r; | |
733 | } | |
734 | ||
26cf9fb7 LP |
735 | /* Let's release the D-Bus connection, after all the session might live quite a long time, and we are |
736 | * not going to process the bus connection in that time, so let's better close before the daemon | |
737 | * kicks us off because we are not processing anything. */ | |
738 | (void) pam_release_bus_connection(handle); | |
739 | return PAM_SUCCESS; | |
740 | } | |
741 | ||
742 | _public_ PAM_EXTERN int pam_sm_close_session( | |
743 | pam_handle_t *handle, | |
744 | int flags, | |
745 | int argc, const char **argv) { | |
746 | ||
747 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
748 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
749 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
750 | const char *username = NULL; | |
751 | bool debug = false; | |
752 | int r; | |
753 | ||
754 | if (parse_argv(handle, | |
755 | argc, argv, | |
756 | NULL, | |
757 | &debug) < 0) | |
758 | return PAM_SESSION_ERR; | |
759 | ||
760 | if (debug) | |
761 | pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed session end"); | |
762 | ||
6c8428bb LP |
763 | r = pam_get_user(handle, &username, NULL); |
764 | if (r != PAM_SUCCESS) { | |
765 | pam_syslog(handle, LOG_ERR, "Failed to get user name: %s", pam_strerror(handle, r)); | |
766 | return r; | |
767 | } | |
768 | ||
769 | if (isempty(username)) { | |
770 | pam_syslog(handle, LOG_ERR, "User name not set."); | |
771 | return PAM_SERVICE_ERR; | |
772 | } | |
773 | ||
26cf9fb7 LP |
774 | /* Let's explicitly drop the reference to the homed session, so that the subsequent ReleaseHome() |
775 | * call will be able to do its thing. */ | |
6c8428bb | 776 | r = release_home_fd(handle, username); |
26cf9fb7 LP |
777 | if (r == PAM_NO_MODULE_DATA) /* Nothing to do, we never acquired an fd */ |
778 | return PAM_SUCCESS; | |
779 | if (r != PAM_SUCCESS) | |
780 | return r; | |
781 | ||
26cf9fb7 LP |
782 | r = pam_acquire_bus_connection(handle, &bus); |
783 | if (r != PAM_SUCCESS) | |
784 | return r; | |
785 | ||
8a1596aa | 786 | r = bus_message_new_method_call(bus, &m, bus_home_mgr, "ReleaseHome"); |
26cf9fb7 LP |
787 | if (r < 0) |
788 | return pam_bus_log_create_error(handle, r); | |
789 | ||
790 | r = sd_bus_message_append(m, "s", username); | |
791 | if (r < 0) | |
792 | return pam_bus_log_create_error(handle, r); | |
793 | ||
794 | r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL); | |
795 | if (r < 0) { | |
796 | if (sd_bus_error_has_name(&error, BUS_ERROR_HOME_BUSY)) | |
797 | pam_syslog(handle, LOG_NOTICE, "Not deactivating home directory of %s, as it is still used.", username); | |
798 | else { | |
799 | pam_syslog(handle, LOG_ERR, "Failed to release user home: %s", bus_error_message(&error, r)); | |
800 | return PAM_SESSION_ERR; | |
801 | } | |
802 | } | |
803 | ||
804 | return PAM_SUCCESS; | |
805 | } | |
806 | ||
807 | _public_ PAM_EXTERN int pam_sm_acct_mgmt( | |
808 | pam_handle_t *handle, | |
809 | int flags, | |
810 | int argc, | |
811 | const char **argv) { | |
812 | ||
813 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; | |
814 | bool debug = false, please_suspend = false; | |
815 | usec_t t; | |
816 | int r; | |
817 | ||
764ae4dd LP |
818 | if (parse_env(handle, &please_suspend) < 0) |
819 | return PAM_AUTH_ERR; | |
820 | ||
26cf9fb7 LP |
821 | if (parse_argv(handle, |
822 | argc, argv, | |
823 | &please_suspend, | |
824 | &debug) < 0) | |
825 | return PAM_AUTH_ERR; | |
826 | ||
827 | if (debug) | |
828 | pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed account management"); | |
829 | ||
830 | r = acquire_home(handle, /* please_authenticate = */ false, please_suspend, debug); | |
831 | if (r == PAM_USER_UNKNOWN) | |
832 | return PAM_SUCCESS; /* we don't have anything to say about users we don't manage */ | |
833 | if (r != PAM_SUCCESS) | |
834 | return r; | |
835 | ||
6c8428bb | 836 | r = acquire_user_record(handle, NULL, &ur); |
26cf9fb7 LP |
837 | if (r != PAM_SUCCESS) |
838 | return r; | |
839 | ||
840 | r = user_record_test_blocked(ur); | |
841 | switch (r) { | |
842 | ||
843 | case -ESTALE: | |
844 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record is newer than current system time, prohibiting access."); | |
845 | return PAM_ACCT_EXPIRED; | |
846 | ||
847 | case -ENOLCK: | |
848 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record is blocked, prohibiting access."); | |
849 | return PAM_ACCT_EXPIRED; | |
850 | ||
851 | case -EL2HLT: | |
852 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record is not valid yet, prohibiting access."); | |
853 | return PAM_ACCT_EXPIRED; | |
854 | ||
855 | case -EL3HLT: | |
856 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record is not valid anymore, prohibiting access."); | |
857 | return PAM_ACCT_EXPIRED; | |
858 | ||
859 | default: | |
860 | if (r < 0) { | |
861 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record not valid, prohibiting access."); | |
862 | return PAM_ACCT_EXPIRED; | |
863 | } | |
864 | ||
865 | break; | |
866 | } | |
867 | ||
868 | t = user_record_ratelimit_next_try(ur); | |
869 | if (t != USEC_INFINITY) { | |
870 | usec_t n = now(CLOCK_REALTIME); | |
871 | ||
872 | if (t > n) { | |
873 | char buf[FORMAT_TIMESPAN_MAX]; | |
874 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Too many logins, try again in %s.", | |
875 | format_timespan(buf, sizeof(buf), t - n, USEC_PER_SEC)); | |
876 | ||
877 | return PAM_MAXTRIES; | |
878 | } | |
879 | } | |
880 | ||
881 | r = user_record_test_password_change_required(ur); | |
882 | switch (r) { | |
883 | ||
884 | case -EKEYREVOKED: | |
885 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password change required."); | |
886 | return PAM_NEW_AUTHTOK_REQD; | |
887 | ||
888 | case -EOWNERDEAD: | |
889 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password expired, change requird."); | |
890 | return PAM_NEW_AUTHTOK_REQD; | |
891 | ||
892 | case -EKEYREJECTED: | |
893 | /* Strictly speaking this is only about password expiration, and we might want to allow | |
894 | * authentication via PKCS#11 or so, but let's ignore this fine distinction for now. */ | |
895 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password is expired, but can't change, refusing login."); | |
896 | return PAM_AUTHTOK_EXPIRED; | |
897 | ||
898 | case -EKEYEXPIRED: | |
899 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "Password will expire soon, please change."); | |
900 | break; | |
901 | ||
902 | case -EROFS: | |
903 | /* All good, just means the password if we wanted to change we couldn't, but we don't need to */ | |
904 | break; | |
905 | ||
906 | default: | |
907 | if (r < 0) { | |
908 | (void) pam_prompt(handle, PAM_ERROR_MSG, NULL, "User record not valid, prohibiting access."); | |
909 | return PAM_AUTHTOK_EXPIRED; | |
910 | } | |
911 | ||
912 | break; | |
913 | } | |
914 | ||
915 | return PAM_SUCCESS; | |
916 | } | |
917 | ||
918 | _public_ PAM_EXTERN int pam_sm_chauthtok( | |
919 | pam_handle_t *handle, | |
920 | int flags, | |
921 | int argc, | |
922 | const char **argv) { | |
923 | ||
924 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL, *old_secret = NULL, *new_secret = NULL; | |
925 | const char *old_password = NULL, *new_password = NULL; | |
926 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; | |
927 | unsigned n_attempts = 0; | |
928 | bool debug = false; | |
929 | int r; | |
930 | ||
931 | if (parse_argv(handle, | |
932 | argc, argv, | |
933 | NULL, | |
934 | &debug) < 0) | |
935 | return PAM_AUTH_ERR; | |
936 | ||
937 | if (debug) | |
938 | pam_syslog(handle, LOG_DEBUG, "pam-systemd-homed account management"); | |
939 | ||
940 | r = pam_acquire_bus_connection(handle, &bus); | |
941 | if (r != PAM_SUCCESS) | |
942 | return r; | |
943 | ||
6c8428bb | 944 | r = acquire_user_record(handle, NULL, &ur); |
26cf9fb7 LP |
945 | if (r != PAM_SUCCESS) |
946 | return r; | |
947 | ||
948 | /* Start with cached credentials */ | |
949 | r = pam_get_item(handle, PAM_OLDAUTHTOK, (const void**) &old_password); | |
950 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) { | |
951 | pam_syslog(handle, LOG_ERR, "Failed to get old password: %s", pam_strerror(handle, r)); | |
952 | return r; | |
953 | } | |
954 | r = pam_get_item(handle, PAM_AUTHTOK, (const void**) &new_password); | |
955 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) { | |
956 | pam_syslog(handle, LOG_ERR, "Failed to get cached password: %s", pam_strerror(handle, r)); | |
957 | return r; | |
958 | } | |
959 | ||
960 | if (isempty(new_password)) { | |
961 | /* No, it's not cached, then let's ask for the password and its verification, and cache | |
962 | * it. */ | |
963 | ||
964 | r = pam_get_authtok_noverify(handle, &new_password, "New password: "); | |
965 | if (r != PAM_SUCCESS) { | |
966 | pam_syslog(handle, LOG_ERR, "Failed to get new password: %s", pam_strerror(handle, r)); | |
967 | return r; | |
968 | } | |
969 | if (isempty(new_password)) { | |
970 | pam_syslog(handle, LOG_DEBUG, "Password request aborted."); | |
971 | return PAM_AUTHTOK_ERR; | |
972 | } | |
973 | ||
974 | r = pam_get_authtok_verify(handle, &new_password, "new password: "); /* Lower case, since PAM prefixes 'Repeat' */ | |
975 | if (r != PAM_SUCCESS) { | |
976 | pam_syslog(handle, LOG_ERR, "Failed to get password again: %s", pam_strerror(handle, r)); | |
977 | return r; | |
978 | } | |
979 | ||
980 | // FIXME: pam_pwquality will ask for the password a third time. It really shouldn't do | |
981 | // that, and instead assume the password was already verified once when it is found to be | |
982 | // cached already. needs to be fixed in pam_pwquality | |
983 | } | |
984 | ||
985 | /* Now everything is cached and checked, let's exit from the preliminary check */ | |
986 | if (FLAGS_SET(flags, PAM_PRELIM_CHECK)) | |
987 | return PAM_SUCCESS; | |
988 | ||
26cf9fb7 LP |
989 | old_secret = user_record_new(); |
990 | if (!old_secret) | |
991 | return pam_log_oom(handle); | |
992 | ||
993 | if (!isempty(old_password)) { | |
994 | r = user_record_set_password(old_secret, STRV_MAKE(old_password), true); | |
995 | if (r < 0) { | |
996 | pam_syslog(handle, LOG_ERR, "Failed to store old password: %s", strerror_safe(r)); | |
997 | return PAM_SERVICE_ERR; | |
998 | } | |
999 | } | |
1000 | ||
1001 | new_secret = user_record_new(); | |
1002 | if (!new_secret) | |
1003 | return pam_log_oom(handle); | |
1004 | ||
1005 | r = user_record_set_password(new_secret, STRV_MAKE(new_password), true); | |
1006 | if (r < 0) { | |
1007 | pam_syslog(handle, LOG_ERR, "Failed to store new password: %s", strerror_safe(r)); | |
1008 | return PAM_SERVICE_ERR; | |
1009 | } | |
1010 | ||
1011 | for (;;) { | |
1012 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1013 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; | |
1014 | ||
8a1596aa | 1015 | r = bus_message_new_method_call(bus, &m, bus_home_mgr, "ChangePasswordHome"); |
26cf9fb7 LP |
1016 | if (r < 0) |
1017 | return pam_bus_log_create_error(handle, r); | |
1018 | ||
1019 | r = sd_bus_message_append(m, "s", ur->user_name); | |
1020 | if (r < 0) | |
1021 | return pam_bus_log_create_error(handle, r); | |
1022 | ||
1023 | r = bus_message_append_secret(m, new_secret); | |
1024 | if (r < 0) | |
1025 | return pam_bus_log_create_error(handle, r); | |
1026 | ||
1027 | r = bus_message_append_secret(m, old_secret); | |
1028 | if (r < 0) | |
1029 | return pam_bus_log_create_error(handle, r); | |
1030 | ||
1031 | r = sd_bus_call(bus, m, HOME_SLOW_BUS_CALL_TIMEOUT_USEC, &error, NULL); | |
1032 | if (r < 0) { | |
1033 | r = handle_generic_user_record_error(handle, ur->user_name, old_secret, r, &error); | |
1034 | if (r == PAM_CONV_ERR) { | |
1035 | pam_syslog(handle, LOG_ERR, "Failed to prompt for password/prompt."); | |
1036 | return PAM_CONV_ERR; | |
1037 | } | |
1038 | if (r != PAM_SUCCESS) | |
1039 | return r; | |
1040 | } else { | |
1041 | pam_syslog(handle, LOG_NOTICE, "Successfully changed password for user %s.", ur->user_name); | |
1042 | return PAM_SUCCESS; | |
1043 | } | |
1044 | ||
1045 | if (++n_attempts >= 5) | |
1046 | break; | |
1047 | ||
1048 | /* Try again */ | |
1049 | }; | |
1050 | ||
1051 | pam_syslog(handle, LOG_NOTICE, "Failed to change password for user %s: %m", ur->user_name); | |
1052 | return PAM_MAXTRIES; | |
1053 | } |