]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <endian.h> | |
4 | #include <errno.h> | |
5 | #include <fcntl.h> | |
6 | #include <pwd.h> | |
7 | #include <security/_pam_macros.h> | |
8 | #include <security/pam_ext.h> | |
9 | #include <security/pam_misc.h> | |
10 | #include <security/pam_modules.h> | |
11 | #include <security/pam_modutil.h> | |
12 | #include <sys/file.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/sysmacros.h> | |
15 | #include <sys/types.h> | |
16 | #include <unistd.h> | |
17 | ||
18 | #include "alloc-util.h" | |
19 | #include "audit-util.h" | |
20 | #include "bus-common-errors.h" | |
21 | #include "bus-error.h" | |
22 | #include "bus-internal.h" | |
23 | #include "bus-locator.h" | |
24 | #include "cap-list.h" | |
25 | #include "capability-util.h" | |
26 | #include "cgroup-setup.h" | |
27 | #include "devnum-util.h" | |
28 | #include "errno-util.h" | |
29 | #include "fd-util.h" | |
30 | #include "fileio.h" | |
31 | #include "format-util.h" | |
32 | #include "fs-util.h" | |
33 | #include "hostname-util.h" | |
34 | #include "locale-util.h" | |
35 | #include "login-util.h" | |
36 | #include "macro.h" | |
37 | #include "pam-util.h" | |
38 | #include "parse-util.h" | |
39 | #include "path-util.h" | |
40 | #include "percent-util.h" | |
41 | #include "process-util.h" | |
42 | #include "rlimit-util.h" | |
43 | #include "socket-util.h" | |
44 | #include "stdio-util.h" | |
45 | #include "strv.h" | |
46 | #include "terminal-util.h" | |
47 | #include "user-util.h" | |
48 | #include "userdb.h" | |
49 | ||
50 | #define LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) | |
51 | ||
52 | static int parse_caps( | |
53 | pam_handle_t *handle, | |
54 | const char *value, | |
55 | uint64_t *caps) { | |
56 | ||
57 | bool subtract; | |
58 | int r; | |
59 | ||
60 | assert(handle); | |
61 | assert(value); | |
62 | ||
63 | if (value[0] == '~') { | |
64 | subtract = true; | |
65 | value++; | |
66 | } else | |
67 | subtract = false; | |
68 | ||
69 | for (;;) { | |
70 | _cleanup_free_ char *s = NULL; | |
71 | uint64_t b, m; | |
72 | int c; | |
73 | ||
74 | /* We can't use spaces as separators here, as PAM's simplistic argument parser doesn't allow | |
75 | * spaces inside of arguments. We use commas instead (which is similar to cap_from_text(), | |
76 | * which also uses commas). */ | |
77 | r = extract_first_word(&value, &s, ",", EXTRACT_DONT_COALESCE_SEPARATORS); | |
78 | if (r < 0) | |
79 | return r; | |
80 | if (r == 0) | |
81 | break; | |
82 | ||
83 | c = capability_from_name(s); | |
84 | if (c < 0) { | |
85 | pam_syslog(handle, LOG_WARNING, "Unknown capability, ignoring: %s", s); | |
86 | continue; | |
87 | } | |
88 | ||
89 | m = UINT64_C(1) << c; | |
90 | ||
91 | if (!caps) | |
92 | continue; | |
93 | ||
94 | if (*caps == UINT64_MAX) | |
95 | b = subtract ? all_capabilities() : 0; | |
96 | else | |
97 | b = *caps; | |
98 | ||
99 | if (subtract) | |
100 | *caps = b & ~m; | |
101 | else | |
102 | *caps = b | m; | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static int parse_argv( | |
109 | pam_handle_t *handle, | |
110 | int argc, const char **argv, | |
111 | const char **class, | |
112 | const char **type, | |
113 | const char **desktop, | |
114 | bool *debug, | |
115 | uint64_t *default_capability_bounding_set, | |
116 | uint64_t *default_capability_ambient_set) { | |
117 | ||
118 | int r; | |
119 | ||
120 | assert(handle); | |
121 | assert(argc >= 0); | |
122 | assert(argc == 0 || argv); | |
123 | ||
124 | for (int i = 0; i < argc; i++) { | |
125 | const char *p; | |
126 | ||
127 | if ((p = startswith(argv[i], "class="))) { | |
128 | if (class) | |
129 | *class = p; | |
130 | ||
131 | } else if ((p = startswith(argv[i], "type="))) { | |
132 | if (type) | |
133 | *type = p; | |
134 | ||
135 | } else if ((p = startswith(argv[i], "desktop="))) { | |
136 | if (desktop) | |
137 | *desktop = p; | |
138 | ||
139 | } else if (streq(argv[i], "debug")) { | |
140 | if (debug) | |
141 | *debug = true; | |
142 | ||
143 | } else if ((p = startswith(argv[i], "debug="))) { | |
144 | r = parse_boolean(p); | |
145 | if (r < 0) | |
146 | pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring: %s", p); | |
147 | else if (debug) | |
148 | *debug = r; | |
149 | ||
150 | } else if ((p = startswith(argv[i], "default-capability-bounding-set="))) { | |
151 | r = parse_caps(handle, p, default_capability_bounding_set); | |
152 | if (r < 0) | |
153 | pam_syslog(handle, LOG_WARNING, "Failed to parse default-capability-bounding-set= argument, ignoring: %s", p); | |
154 | ||
155 | } else if ((p = startswith(argv[i], "default-capability-ambient-set="))) { | |
156 | r = parse_caps(handle, p, default_capability_ambient_set); | |
157 | if (r < 0) | |
158 | pam_syslog(handle, LOG_WARNING, "Failed to parse default-capability-ambient-set= argument, ignoring: %s", p); | |
159 | ||
160 | } else | |
161 | pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring.", argv[i]); | |
162 | } | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static int acquire_user_record( | |
168 | pam_handle_t *handle, | |
169 | UserRecord **ret_record) { | |
170 | ||
171 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; | |
172 | const char *username = NULL, *json = NULL; | |
173 | _cleanup_free_ char *field = NULL; | |
174 | int r; | |
175 | ||
176 | assert(handle); | |
177 | ||
178 | r = pam_get_user(handle, &username, NULL); | |
179 | if (r != PAM_SUCCESS) | |
180 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get user name: @PAMERR@"); | |
181 | ||
182 | if (isempty(username)) | |
183 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, "User name not valid."); | |
184 | ||
185 | /* If pam_systemd_homed (or some other module) already acquired the user record we can reuse it | |
186 | * here. */ | |
187 | field = strjoin("systemd-user-record-", username); | |
188 | if (!field) | |
189 | return pam_log_oom(handle); | |
190 | ||
191 | r = pam_get_data(handle, field, (const void**) &json); | |
192 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
193 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM user record data: @PAMERR@"); | |
194 | if (r == PAM_SUCCESS && json) { | |
195 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
196 | ||
197 | /* Parse cached record */ | |
198 | r = json_parse(json, JSON_PARSE_SENSITIVE, &v, NULL, NULL); | |
199 | if (r < 0) | |
200 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to parse JSON user record: %m"); | |
201 | ||
202 | ur = user_record_new(); | |
203 | if (!ur) | |
204 | return pam_log_oom(handle); | |
205 | ||
206 | r = user_record_load(ur, v, USER_RECORD_LOAD_REFUSE_SECRET|USER_RECORD_PERMISSIVE); | |
207 | if (r < 0) | |
208 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to load user record: %m"); | |
209 | ||
210 | /* Safety check if cached record actually matches what we are looking for */ | |
211 | if (!streq_ptr(username, ur->user_name)) | |
212 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, | |
213 | "Acquired user record does not match user name."); | |
214 | } else { | |
215 | _cleanup_free_ char *formatted = NULL; | |
216 | ||
217 | /* Request the record ourselves */ | |
218 | r = userdb_by_name(username, 0, &ur); | |
219 | if (r < 0) { | |
220 | pam_syslog_errno(handle, LOG_ERR, r, "Failed to get user record: %m"); | |
221 | return PAM_USER_UNKNOWN; | |
222 | } | |
223 | ||
224 | r = json_variant_format(ur->json, 0, &formatted); | |
225 | if (r < 0) | |
226 | return pam_syslog_errno(handle, LOG_ERR, r, "Failed to format user JSON: %m"); | |
227 | ||
228 | /* And cache it for everyone else */ | |
229 | r = pam_set_data(handle, field, formatted, pam_cleanup_free); | |
230 | if (r != PAM_SUCCESS) | |
231 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
232 | "Failed to set PAM user record data '%s': @PAMERR@", field); | |
233 | TAKE_PTR(formatted); | |
234 | } | |
235 | ||
236 | if (!uid_is_valid(ur->uid)) | |
237 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SERVICE_ERR, | |
238 | "Acquired user record does not have a UID."); | |
239 | ||
240 | if (ret_record) | |
241 | *ret_record = TAKE_PTR(ur); | |
242 | ||
243 | return PAM_SUCCESS; | |
244 | } | |
245 | ||
246 | static bool display_is_local(const char *display) { | |
247 | assert(display); | |
248 | ||
249 | return | |
250 | display[0] == ':' && | |
251 | ascii_isdigit(display[1]); | |
252 | } | |
253 | ||
254 | static int socket_from_display(const char *display) { | |
255 | _cleanup_free_ char *f = NULL; | |
256 | size_t k; | |
257 | char *c; | |
258 | union sockaddr_union sa; | |
259 | socklen_t sa_len; | |
260 | _cleanup_close_ int fd = -EBADF; | |
261 | int r; | |
262 | ||
263 | assert(display); | |
264 | ||
265 | if (!display_is_local(display)) | |
266 | return -EINVAL; | |
267 | ||
268 | k = strspn(display+1, "0123456789"); | |
269 | ||
270 | /* Try abstract socket first. */ | |
271 | f = new(char, STRLEN("@/tmp/.X11-unix/X") + k + 1); | |
272 | if (!f) | |
273 | return -ENOMEM; | |
274 | ||
275 | c = stpcpy(f, "@/tmp/.X11-unix/X"); | |
276 | memcpy(c, display+1, k); | |
277 | c[k] = 0; | |
278 | ||
279 | r = sockaddr_un_set_path(&sa.un, f); | |
280 | if (r < 0) | |
281 | return r; | |
282 | sa_len = r; | |
283 | ||
284 | fd = RET_NERRNO(socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0)); | |
285 | if (fd < 0) | |
286 | return fd; | |
287 | ||
288 | r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); | |
289 | if (r >= 0) | |
290 | return TAKE_FD(fd); | |
291 | if (r != -ECONNREFUSED) | |
292 | return r; | |
293 | ||
294 | /* Try also non-abstract socket. */ | |
295 | r = sockaddr_un_set_path(&sa.un, f + 1); | |
296 | if (r < 0) | |
297 | return r; | |
298 | sa_len = r; | |
299 | ||
300 | r = RET_NERRNO(connect(fd, &sa.sa, sa_len)); | |
301 | if (r >= 0) | |
302 | return TAKE_FD(fd); | |
303 | return r; | |
304 | } | |
305 | ||
306 | static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) { | |
307 | _cleanup_free_ char *sys_path = NULL, *tty = NULL; | |
308 | _cleanup_close_ int fd = -EBADF; | |
309 | struct ucred ucred; | |
310 | int v, r; | |
311 | dev_t display_ctty; | |
312 | ||
313 | assert(display); | |
314 | assert(vtnr); | |
315 | ||
316 | /* We deduce the X11 socket from the display name, then use | |
317 | * SO_PEERCRED to determine the X11 server process, ask for | |
318 | * the controlling tty of that and if it's a VC then we know | |
319 | * the seat and the virtual terminal. Sounds ugly, is only | |
320 | * semi-ugly. */ | |
321 | ||
322 | fd = socket_from_display(display); | |
323 | if (fd < 0) | |
324 | return fd; | |
325 | ||
326 | r = getpeercred(fd, &ucred); | |
327 | if (r < 0) | |
328 | return r; | |
329 | ||
330 | r = get_ctty_devnr(ucred.pid, &display_ctty); | |
331 | if (r < 0) | |
332 | return r; | |
333 | ||
334 | if (asprintf(&sys_path, "/sys/dev/char/" DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(display_ctty)) < 0) | |
335 | return -ENOMEM; | |
336 | r = readlink_value(sys_path, &tty); | |
337 | if (r < 0) | |
338 | return r; | |
339 | ||
340 | v = vtnr_from_tty(tty); | |
341 | if (v < 0) | |
342 | return v; | |
343 | else if (v == 0) | |
344 | return -ENOENT; | |
345 | ||
346 | if (seat) | |
347 | *seat = "seat0"; | |
348 | *vtnr = (uint32_t) v; | |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
353 | static int export_legacy_dbus_address( | |
354 | pam_handle_t *handle, | |
355 | const char *runtime) { | |
356 | ||
357 | const char *s; | |
358 | _cleanup_free_ char *t = NULL; | |
359 | int r = PAM_BUF_ERR; | |
360 | ||
361 | /* We need to export $DBUS_SESSION_BUS_ADDRESS because various applications will not connect | |
362 | * correctly to the bus without it. This setting matches what dbus.socket does for the user | |
363 | * session using 'systemctl --user set-environment'. We want to have the same configuration | |
364 | * in processes started from the PAM session. | |
365 | * | |
366 | * The setting of the address is guarded by the access() check because it is also possible to compile | |
367 | * dbus without --enable-user-session, in which case this socket is not used, and | |
368 | * $DBUS_SESSION_BUS_ADDRESS should not be set. An alternative approach would to not do the access() | |
369 | * check here, and let applications try on their own, by using "unix:path=%s/bus;autolaunch:". But we | |
370 | * expect the socket to be present by the time we do this check, so we can just as well check once | |
371 | * here. */ | |
372 | ||
373 | s = strjoina(runtime, "/bus"); | |
374 | if (access(s, F_OK) < 0) | |
375 | return PAM_SUCCESS; | |
376 | ||
377 | if (asprintf(&t, DEFAULT_USER_BUS_ADDRESS_FMT, runtime) < 0) | |
378 | return pam_log_oom(handle); | |
379 | ||
380 | r = pam_misc_setenv(handle, "DBUS_SESSION_BUS_ADDRESS", t, 0); | |
381 | if (r != PAM_SUCCESS) | |
382 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to set bus variable: @PAMERR@"); | |
383 | ||
384 | return PAM_SUCCESS; | |
385 | } | |
386 | ||
387 | static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { | |
388 | uint64_t val; | |
389 | int r; | |
390 | ||
391 | if (isempty(limit)) | |
392 | return PAM_SUCCESS; | |
393 | ||
394 | if (streq(limit, "infinity")) { | |
395 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", UINT64_MAX); | |
396 | if (r < 0) | |
397 | return pam_bus_log_create_error(handle, r); | |
398 | ||
399 | return PAM_SUCCESS; | |
400 | } | |
401 | ||
402 | r = parse_permyriad(limit); | |
403 | if (r >= 0) { | |
404 | r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", UINT32_SCALE_FROM_PERMYRIAD(r)); | |
405 | if (r < 0) | |
406 | return pam_bus_log_create_error(handle, r); | |
407 | ||
408 | return PAM_SUCCESS; | |
409 | } | |
410 | ||
411 | r = parse_size(limit, 1024, &val); | |
412 | if (r >= 0) { | |
413 | r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val); | |
414 | if (r < 0) | |
415 | return pam_bus_log_create_error(handle, r); | |
416 | ||
417 | return PAM_SUCCESS; | |
418 | } | |
419 | ||
420 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.memory_max, ignoring: %s", limit); | |
421 | return PAM_SUCCESS; | |
422 | } | |
423 | ||
424 | static int append_session_runtime_max_sec(pam_handle_t *handle, sd_bus_message *m, const char *limit) { | |
425 | usec_t val; | |
426 | int r; | |
427 | ||
428 | /* No need to parse "infinity" here, it will be set by default later in scope_init() */ | |
429 | if (isempty(limit) || streq(limit, "infinity")) | |
430 | return PAM_SUCCESS; | |
431 | ||
432 | r = parse_sec(limit, &val); | |
433 | if (r >= 0) { | |
434 | r = sd_bus_message_append(m, "(sv)", "RuntimeMaxUSec", "t", (uint64_t) val); | |
435 | if (r < 0) | |
436 | return pam_bus_log_create_error(handle, r); | |
437 | } else | |
438 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.runtime_max_sec: %s, ignoring.", limit); | |
439 | ||
440 | return PAM_SUCCESS; | |
441 | } | |
442 | ||
443 | static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) { | |
444 | uint64_t val; | |
445 | int r; | |
446 | ||
447 | /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */ | |
448 | if (isempty(limit) || streq(limit, "infinity")) | |
449 | return PAM_SUCCESS; | |
450 | ||
451 | r = safe_atou64(limit, &val); | |
452 | if (r >= 0) { | |
453 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val); | |
454 | if (r < 0) | |
455 | return pam_bus_log_create_error(handle, r); | |
456 | } else | |
457 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.tasks_max, ignoring: %s", limit); | |
458 | ||
459 | return PAM_SUCCESS; | |
460 | } | |
461 | ||
462 | static int append_session_cpu_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit) { | |
463 | uint64_t val; | |
464 | int r; | |
465 | ||
466 | if (isempty(limit)) | |
467 | return PAM_SUCCESS; | |
468 | ||
469 | r = cg_cpu_weight_parse(limit, &val); | |
470 | if (r < 0) | |
471 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight, ignoring: %s", limit); | |
472 | else { | |
473 | r = sd_bus_message_append(m, "(sv)", "CPUWeight", "t", val); | |
474 | if (r < 0) | |
475 | return pam_bus_log_create_error(handle, r); | |
476 | } | |
477 | ||
478 | return PAM_SUCCESS; | |
479 | } | |
480 | ||
481 | static int append_session_io_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit) { | |
482 | uint64_t val; | |
483 | int r; | |
484 | ||
485 | if (isempty(limit)) | |
486 | return PAM_SUCCESS; | |
487 | ||
488 | r = cg_weight_parse(limit, &val); | |
489 | if (r < 0) | |
490 | pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight, ignoring: %s", limit); | |
491 | else { | |
492 | r = sd_bus_message_append(m, "(sv)", "IOWeight", "t", val); | |
493 | if (r < 0) | |
494 | return pam_bus_log_create_error(handle, r); | |
495 | } | |
496 | ||
497 | return PAM_SUCCESS; | |
498 | } | |
499 | ||
500 | static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) { | |
501 | const char *v; | |
502 | ||
503 | assert(handle); | |
504 | assert(key); | |
505 | ||
506 | /* Looks for an environment variable, preferably in the environment block associated with the | |
507 | * specified PAM handle, falling back to the process' block instead. Why check both? Because we want | |
508 | * to permit configuration of session properties from unit files that invoke PAM services, so that | |
509 | * PAM services don't have to be reworked to set systemd-specific properties, but these properties | |
510 | * can still be set from the unit file Environment= block. */ | |
511 | ||
512 | v = pam_getenv(handle, key); | |
513 | if (!isempty(v)) | |
514 | return v; | |
515 | ||
516 | /* We use secure_getenv() here, since we might get loaded into su/sudo, which are SUID. Ideally | |
517 | * they'd clean up the environment before invoking foreign code (such as PAM modules), but alas they | |
518 | * currently don't (to be precise, they clean up the environment they pass to their children, but | |
519 | * not their own environ[]). */ | |
520 | v = secure_getenv(key); | |
521 | if (!isempty(v)) | |
522 | return v; | |
523 | ||
524 | return fallback; | |
525 | } | |
526 | ||
527 | static int update_environment(pam_handle_t *handle, const char *key, const char *value) { | |
528 | int r; | |
529 | ||
530 | assert(handle); | |
531 | assert(key); | |
532 | ||
533 | /* Updates the environment, but only if there's actually a value set. Also, log about errors */ | |
534 | ||
535 | if (isempty(value)) | |
536 | return PAM_SUCCESS; | |
537 | ||
538 | r = pam_misc_setenv(handle, key, value, 0); | |
539 | if (r != PAM_SUCCESS) | |
540 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
541 | "Failed to set environment variable %s: @PAMERR@", key); | |
542 | ||
543 | return PAM_SUCCESS; | |
544 | } | |
545 | ||
546 | static bool validate_runtime_directory(pam_handle_t *handle, const char *path, uid_t uid) { | |
547 | struct stat st; | |
548 | ||
549 | assert(handle); | |
550 | assert(path); | |
551 | ||
552 | /* Some extra paranoia: let's not set $XDG_RUNTIME_DIR if the directory we'd set it to isn't actually | |
553 | * set up properly for us. This is supposed to provide a careful safety net for supporting su/sudo | |
554 | * type transitions: in that case the UID changes, but the session and thus the user owning it | |
555 | * doesn't change. Since the $XDG_RUNTIME_DIR lifecycle is bound to the session's user being logged | |
556 | * in at least once we should be particularly careful when setting the environment variable, since | |
557 | * otherwise we might end up setting $XDG_RUNTIME_DIR to some directory owned by the wrong user. */ | |
558 | ||
559 | if (!path_is_absolute(path)) { | |
560 | pam_syslog(handle, LOG_ERR, "Provided runtime directory '%s' is not absolute.", path); | |
561 | goto fail; | |
562 | } | |
563 | ||
564 | if (lstat(path, &st) < 0) { | |
565 | pam_syslog_errno(handle, LOG_ERR, errno, "Failed to stat() runtime directory '%s': %m", path); | |
566 | goto fail; | |
567 | } | |
568 | ||
569 | if (!S_ISDIR(st.st_mode)) { | |
570 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not actually a directory.", path); | |
571 | goto fail; | |
572 | } | |
573 | ||
574 | if (st.st_uid != uid) { | |
575 | pam_syslog(handle, LOG_ERR, "Runtime directory '%s' is not owned by UID " UID_FMT ", as it should.", path, uid); | |
576 | goto fail; | |
577 | } | |
578 | ||
579 | return true; | |
580 | ||
581 | fail: | |
582 | pam_syslog(handle, LOG_WARNING, "Not setting $XDG_RUNTIME_DIR, as the directory is not in order."); | |
583 | return false; | |
584 | } | |
585 | ||
586 | static int pam_putenv_and_log(pam_handle_t *handle, const char *e, bool debug) { | |
587 | int r; | |
588 | ||
589 | assert(handle); | |
590 | assert(e); | |
591 | ||
592 | r = pam_putenv(handle, e); | |
593 | if (r != PAM_SUCCESS) | |
594 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
595 | "Failed to set PAM environment variable %s: @PAMERR@", e); | |
596 | ||
597 | if (debug) | |
598 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable %s set based on user record.", e); | |
599 | ||
600 | return PAM_SUCCESS; | |
601 | } | |
602 | ||
603 | static int apply_user_record_settings( | |
604 | pam_handle_t *handle, | |
605 | UserRecord *ur, | |
606 | bool debug, | |
607 | uint64_t default_capability_bounding_set, | |
608 | uint64_t default_capability_ambient_set) { | |
609 | int r; | |
610 | ||
611 | assert(handle); | |
612 | assert(ur); | |
613 | ||
614 | if (ur->umask != MODE_INVALID) { | |
615 | umask(ur->umask); | |
616 | ||
617 | if (debug) | |
618 | pam_syslog(handle, LOG_DEBUG, "Set user umask to %04o based on user record.", ur->umask); | |
619 | } | |
620 | ||
621 | STRV_FOREACH(i, ur->environment) { | |
622 | _cleanup_free_ char *n = NULL; | |
623 | const char *e; | |
624 | ||
625 | assert_se(e = strchr(*i, '=')); /* environment was already validated while parsing JSON record, this thus must hold */ | |
626 | ||
627 | n = strndup(*i, e - *i); | |
628 | if (!n) | |
629 | return pam_log_oom(handle); | |
630 | ||
631 | if (pam_getenv(handle, n)) { | |
632 | if (debug) | |
633 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $%s already set, not changing based on record.", *i); | |
634 | continue; | |
635 | } | |
636 | ||
637 | r = pam_putenv_and_log(handle, *i, debug); | |
638 | if (r != PAM_SUCCESS) | |
639 | return r; | |
640 | } | |
641 | ||
642 | if (ur->email_address) { | |
643 | if (pam_getenv(handle, "EMAIL")) { | |
644 | if (debug) | |
645 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $EMAIL already set, not changing based on user record."); | |
646 | } else { | |
647 | _cleanup_free_ char *joined = NULL; | |
648 | ||
649 | joined = strjoin("EMAIL=", ur->email_address); | |
650 | if (!joined) | |
651 | return pam_log_oom(handle); | |
652 | ||
653 | r = pam_putenv_and_log(handle, joined, debug); | |
654 | if (r != PAM_SUCCESS) | |
655 | return r; | |
656 | } | |
657 | } | |
658 | ||
659 | if (ur->time_zone) { | |
660 | if (pam_getenv(handle, "TZ")) { | |
661 | if (debug) | |
662 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $TZ already set, not changing based on user record."); | |
663 | } else if (!timezone_is_valid(ur->time_zone, LOG_DEBUG)) { | |
664 | if (debug) | |
665 | pam_syslog(handle, LOG_DEBUG, "Time zone specified in user record is not valid locally, not setting $TZ."); | |
666 | } else { | |
667 | _cleanup_free_ char *joined = NULL; | |
668 | ||
669 | joined = strjoin("TZ=:", ur->time_zone); | |
670 | if (!joined) | |
671 | return pam_log_oom(handle); | |
672 | ||
673 | r = pam_putenv_and_log(handle, joined, debug); | |
674 | if (r != PAM_SUCCESS) | |
675 | return r; | |
676 | } | |
677 | } | |
678 | ||
679 | if (ur->preferred_language) { | |
680 | if (pam_getenv(handle, "LANG")) { | |
681 | if (debug) | |
682 | pam_syslog(handle, LOG_DEBUG, "PAM environment variable $LANG already set, not changing based on user record."); | |
683 | } else if (locale_is_installed(ur->preferred_language) <= 0) { | |
684 | if (debug) | |
685 | pam_syslog(handle, LOG_DEBUG, "Preferred language specified in user record is not valid or not installed, not setting $LANG."); | |
686 | } else { | |
687 | _cleanup_free_ char *joined = NULL; | |
688 | ||
689 | joined = strjoin("LANG=", ur->preferred_language); | |
690 | if (!joined) | |
691 | return pam_log_oom(handle); | |
692 | ||
693 | r = pam_putenv_and_log(handle, joined, debug); | |
694 | if (r != PAM_SUCCESS) | |
695 | return r; | |
696 | } | |
697 | } | |
698 | ||
699 | if (nice_is_valid(ur->nice_level)) { | |
700 | if (nice(ur->nice_level) < 0) | |
701 | pam_syslog_errno(handle, LOG_ERR, errno, | |
702 | "Failed to set nice level to %i, ignoring: %m", ur->nice_level); | |
703 | else if (debug) | |
704 | pam_syslog(handle, LOG_DEBUG, | |
705 | "Nice level set to %i, based on user record.", ur->nice_level); | |
706 | } | |
707 | ||
708 | for (int rl = 0; rl < _RLIMIT_MAX; rl++) { | |
709 | ||
710 | if (!ur->rlimits[rl]) | |
711 | continue; | |
712 | ||
713 | r = setrlimit_closest(rl, ur->rlimits[rl]); | |
714 | if (r < 0) | |
715 | pam_syslog_errno(handle, LOG_ERR, r, | |
716 | "Failed to set resource limit %s, ignoring: %m", rlimit_to_string(rl)); | |
717 | else if (debug) | |
718 | pam_syslog(handle, LOG_DEBUG, | |
719 | "Resource limit %s set, based on user record.", rlimit_to_string(rl)); | |
720 | } | |
721 | ||
722 | uint64_t a, b; | |
723 | a = user_record_capability_ambient_set(ur); | |
724 | if (a == UINT64_MAX) | |
725 | a = default_capability_ambient_set; | |
726 | ||
727 | b = user_record_capability_bounding_set(ur); | |
728 | if (b == UINT64_MAX) | |
729 | b = default_capability_bounding_set; | |
730 | ||
731 | if (a != UINT64_MAX && a != 0) { | |
732 | a &= b; | |
733 | ||
734 | r = capability_ambient_set_apply(a, /* also_inherit= */ true); | |
735 | if (r < 0) | |
736 | pam_syslog_errno(handle, LOG_ERR, r, | |
737 | "Failed to set ambient capabilities, ignoring: %m"); | |
738 | } | |
739 | ||
740 | if (b != UINT64_MAX && !cap_test_all(b)) { | |
741 | r = capability_bounding_set_drop(b, /* right_now= */ false); | |
742 | if (r < 0) | |
743 | pam_syslog_errno(handle, LOG_ERR, r, | |
744 | "Failed to set bounding capabilities, ignoring: %m"); | |
745 | } | |
746 | ||
747 | return PAM_SUCCESS; | |
748 | } | |
749 | ||
750 | static int configure_runtime_directory( | |
751 | pam_handle_t *handle, | |
752 | UserRecord *ur, | |
753 | const char *rt) { | |
754 | ||
755 | int r; | |
756 | ||
757 | assert(handle); | |
758 | assert(ur); | |
759 | assert(rt); | |
760 | ||
761 | if (!validate_runtime_directory(handle, rt, ur->uid)) | |
762 | return PAM_SUCCESS; | |
763 | ||
764 | r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0); | |
765 | if (r != PAM_SUCCESS) | |
766 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to set runtime dir: @PAMERR@"); | |
767 | ||
768 | return export_legacy_dbus_address(handle, rt); | |
769 | } | |
770 | ||
771 | static uint64_t pick_default_capability_ambient_set( | |
772 | UserRecord *ur, | |
773 | const char *service, | |
774 | const char *seat) { | |
775 | ||
776 | /* If not configured otherwise, let's enable CAP_WAKE_ALARM for regular users when logging in on a | |
777 | * seat (i.e. when they are present physically on the device), or when invoked for the systemd --user | |
778 | * instances. This allows desktops to install CAP_WAKE_ALARM to implement alarm clock apps without | |
779 | * much fuss. */ | |
780 | ||
781 | return ur && | |
782 | user_record_disposition(ur) == USER_REGULAR && | |
783 | (streq_ptr(service, "systemd-user") || !isempty(seat)) ? (UINT64_C(1) << CAP_WAKE_ALARM) : UINT64_MAX; | |
784 | } | |
785 | ||
786 | _public_ PAM_EXTERN int pam_sm_open_session( | |
787 | pam_handle_t *handle, | |
788 | int flags, | |
789 | int argc, const char **argv) { | |
790 | ||
791 | /* Let's release the D-Bus connection once this function exits, after all the session might live | |
792 | * quite a long time, and we are not going to process the bus connection in that time, so let's | |
793 | * better close before the daemon kicks us off because we are not processing anything. */ | |
794 | _cleanup_(pam_bus_data_disconnectp) PamBusData *d = NULL; | |
795 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
796 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
797 | const char | |
798 | *id, *object_path, *runtime_path, | |
799 | *service = NULL, | |
800 | *tty = NULL, *display = NULL, | |
801 | *remote_user = NULL, *remote_host = NULL, | |
802 | *seat = NULL, | |
803 | *type = NULL, *class = NULL, | |
804 | *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL, | |
805 | *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL, *runtime_max_sec = NULL; | |
806 | uint64_t default_capability_bounding_set = UINT64_MAX, default_capability_ambient_set = UINT64_MAX; | |
807 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
808 | _cleanup_(user_record_unrefp) UserRecord *ur = NULL; | |
809 | int session_fd = -EBADF, existing, r; | |
810 | bool debug = false, remote; | |
811 | uint32_t vtnr = 0; | |
812 | uid_t original_uid; | |
813 | ||
814 | assert(handle); | |
815 | ||
816 | if (parse_argv(handle, | |
817 | argc, argv, | |
818 | &class_pam, | |
819 | &type_pam, | |
820 | &desktop_pam, | |
821 | &debug, | |
822 | &default_capability_bounding_set, | |
823 | &default_capability_ambient_set) < 0) | |
824 | return PAM_SESSION_ERR; | |
825 | ||
826 | if (debug) | |
827 | pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing"); | |
828 | ||
829 | r = acquire_user_record(handle, &ur); | |
830 | if (r != PAM_SUCCESS) | |
831 | return r; | |
832 | ||
833 | /* Make most of this a NOP on non-logind systems */ | |
834 | if (!logind_running()) | |
835 | goto success; | |
836 | ||
837 | /* Make sure we don't enter a loop by talking to | |
838 | * systemd-logind when it is actually waiting for the | |
839 | * background to finish start-up. If the service is | |
840 | * "systemd-user" we simply set XDG_RUNTIME_DIR and | |
841 | * leave. */ | |
842 | ||
843 | r = pam_get_item(handle, PAM_SERVICE, (const void**) &service); | |
844 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) | |
845 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM service: @PAMERR@"); | |
846 | if (streq_ptr(service, "systemd-user")) { | |
847 | char rt[STRLEN("/run/user/") + DECIMAL_STR_MAX(uid_t)]; | |
848 | ||
849 | xsprintf(rt, "/run/user/"UID_FMT, ur->uid); | |
850 | r = configure_runtime_directory(handle, ur, rt); | |
851 | if (r != PAM_SUCCESS) | |
852 | return r; | |
853 | ||
854 | goto success; | |
855 | } | |
856 | ||
857 | /* Otherwise, we ask logind to create a session for us */ | |
858 | ||
859 | r = pam_get_item(handle, PAM_XDISPLAY, (const void**) &display); | |
860 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) | |
861 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_XDISPLAY: @PAMERR@"); | |
862 | r = pam_get_item(handle, PAM_TTY, (const void**) &tty); | |
863 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) | |
864 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_TTY: @PAMERR@"); | |
865 | r = pam_get_item(handle, PAM_RUSER, (const void**) &remote_user); | |
866 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) | |
867 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_RUSER: @PAMERR@"); | |
868 | r = pam_get_item(handle, PAM_RHOST, (const void**) &remote_host); | |
869 | if (!IN_SET(r, PAM_BAD_ITEM, PAM_SUCCESS)) | |
870 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM_RHOST: @PAMERR@"); | |
871 | ||
872 | seat = getenv_harder(handle, "XDG_SEAT", NULL); | |
873 | cvtnr = getenv_harder(handle, "XDG_VTNR", NULL); | |
874 | type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam); | |
875 | class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam); | |
876 | desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam); | |
877 | ||
878 | tty = strempty(tty); | |
879 | ||
880 | if (strchr(tty, ':')) { | |
881 | /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things | |
882 | * and don't pretend that an X display was a tty. */ | |
883 | if (isempty(display)) | |
884 | display = tty; | |
885 | tty = NULL; | |
886 | ||
887 | } else if (streq(tty, "cron")) { | |
888 | /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but | |
889 | * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set | |
890 | * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked | |
891 | * off processes.) */ | |
892 | type = "unspecified"; | |
893 | class = "background"; | |
894 | tty = NULL; | |
895 | ||
896 | } else if (streq(tty, "ssh")) { | |
897 | /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further | |
898 | * details look for "PAM_TTY_KLUDGE" in the openssh sources). */ | |
899 | type ="tty"; | |
900 | class = "user"; | |
901 | tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually | |
902 | * associated with a pty — won't be tracked by their tty in logind. This is because ssh | |
903 | * does the PAM session registration early for new connections, and registers a pty only | |
904 | * much later (this is because it doesn't know yet if it needs one at all, as whether to | |
905 | * register a pty or not is negotiated much later in the protocol). */ | |
906 | ||
907 | } else | |
908 | /* Chop off leading /dev prefix that some clients specify, but others do not. */ | |
909 | tty = skip_dev_prefix(tty); | |
910 | ||
911 | /* If this fails vtnr will be 0, that's intended */ | |
912 | if (!isempty(cvtnr)) | |
913 | (void) safe_atou32(cvtnr, &vtnr); | |
914 | ||
915 | if (!isempty(display) && !vtnr) { | |
916 | if (isempty(seat)) | |
917 | (void) get_seat_from_display(display, &seat, &vtnr); | |
918 | else if (streq(seat, "seat0")) | |
919 | (void) get_seat_from_display(display, NULL, &vtnr); | |
920 | } | |
921 | ||
922 | if (seat && !streq(seat, "seat0") && vtnr != 0) { | |
923 | if (debug) | |
924 | pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat); | |
925 | vtnr = 0; | |
926 | } | |
927 | ||
928 | if (isempty(type)) | |
929 | type = !isempty(display) ? "x11" : | |
930 | !isempty(tty) ? "tty" : "unspecified"; | |
931 | ||
932 | if (isempty(class)) | |
933 | class = streq(type, "unspecified") ? "background" : "user"; | |
934 | ||
935 | remote = !isempty(remote_host) && !is_localhost(remote_host); | |
936 | ||
937 | r = pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max); | |
938 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
939 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.memory_max data: @PAMERR@"); | |
940 | r = pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max); | |
941 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
942 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.tasks_max data: @PAMERR@"); | |
943 | r = pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight); | |
944 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
945 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.cpu_weight data: @PAMERR@"); | |
946 | r = pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight); | |
947 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
948 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.io_weight data: @PAMERR@"); | |
949 | r = pam_get_data(handle, "systemd.runtime_max_sec", (const void **)&runtime_max_sec); | |
950 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
951 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to get PAM systemd.runtime_max_sec data: @PAMERR@"); | |
952 | ||
953 | /* Talk to logind over the message bus */ | |
954 | r = pam_acquire_bus_connection(handle, "pam-systemd", &bus, &d); | |
955 | if (r != PAM_SUCCESS) | |
956 | return r; | |
957 | ||
958 | if (debug) { | |
959 | pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: " | |
960 | "uid="UID_FMT" pid="PID_FMT" service=%s type=%s class=%s desktop=%s seat=%s vtnr=%"PRIu32" tty=%s display=%s remote=%s remote_user=%s remote_host=%s", | |
961 | ur->uid, getpid_cached(), | |
962 | strempty(service), | |
963 | type, class, strempty(desktop), | |
964 | strempty(seat), vtnr, strempty(tty), strempty(display), | |
965 | yes_no(remote), strempty(remote_user), strempty(remote_host)); | |
966 | pam_syslog(handle, LOG_DEBUG, "Session limits: " | |
967 | "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s runtime_max_sec=%s", | |
968 | strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight), strna(runtime_max_sec)); | |
969 | } | |
970 | ||
971 | r = bus_message_new_method_call(bus, &m, bus_login_mgr, "CreateSession"); | |
972 | if (r < 0) | |
973 | return pam_bus_log_create_error(handle, r); | |
974 | ||
975 | r = sd_bus_message_append(m, "uusssssussbss", | |
976 | (uint32_t) ur->uid, | |
977 | 0, | |
978 | service, | |
979 | type, | |
980 | class, | |
981 | desktop, | |
982 | seat, | |
983 | vtnr, | |
984 | tty, | |
985 | display, | |
986 | remote, | |
987 | remote_user, | |
988 | remote_host); | |
989 | if (r < 0) | |
990 | return pam_bus_log_create_error(handle, r); | |
991 | ||
992 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
993 | if (r < 0) | |
994 | return pam_bus_log_create_error(handle, r); | |
995 | ||
996 | r = append_session_memory_max(handle, m, memory_max); | |
997 | if (r != PAM_SUCCESS) | |
998 | return r; | |
999 | ||
1000 | r = append_session_runtime_max_sec(handle, m, runtime_max_sec); | |
1001 | if (r != PAM_SUCCESS) | |
1002 | return r; | |
1003 | ||
1004 | r = append_session_tasks_max(handle, m, tasks_max); | |
1005 | if (r != PAM_SUCCESS) | |
1006 | return r; | |
1007 | ||
1008 | r = append_session_cpu_weight(handle, m, cpu_weight); | |
1009 | if (r != PAM_SUCCESS) | |
1010 | return r; | |
1011 | ||
1012 | r = append_session_io_weight(handle, m, io_weight); | |
1013 | if (r != PAM_SUCCESS) | |
1014 | return r; | |
1015 | ||
1016 | r = sd_bus_message_close_container(m); | |
1017 | if (r < 0) | |
1018 | return pam_bus_log_create_error(handle, r); | |
1019 | ||
1020 | r = sd_bus_call(bus, m, LOGIN_SLOW_BUS_CALL_TIMEOUT_USEC, &error, &reply); | |
1021 | if (r < 0) { | |
1022 | if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) { | |
1023 | if (debug) | |
1024 | pam_syslog(handle, LOG_DEBUG, "Not creating session: %s", bus_error_message(&error, r)); | |
1025 | ||
1026 | /* We are already in a session, don't do anything */ | |
1027 | goto success; | |
1028 | } else { | |
1029 | pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r)); | |
1030 | return PAM_SESSION_ERR; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | r = sd_bus_message_read(reply, | |
1035 | "soshusub", | |
1036 | &id, | |
1037 | &object_path, | |
1038 | &runtime_path, | |
1039 | &session_fd, | |
1040 | &original_uid, | |
1041 | &seat, | |
1042 | &vtnr, | |
1043 | &existing); | |
1044 | if (r < 0) | |
1045 | return pam_bus_log_parse_error(handle, r); | |
1046 | ||
1047 | if (debug) | |
1048 | pam_syslog(handle, LOG_DEBUG, "Reply from logind: " | |
1049 | "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u", | |
1050 | id, object_path, runtime_path, session_fd, seat, vtnr, original_uid); | |
1051 | ||
1052 | r = update_environment(handle, "XDG_SESSION_ID", id); | |
1053 | if (r != PAM_SUCCESS) | |
1054 | return r; | |
1055 | ||
1056 | if (original_uid == ur->uid) { | |
1057 | /* Don't set $XDG_RUNTIME_DIR if the user we now authenticated for does not match the | |
1058 | * original user of the session. We do this in order not to result in privileged apps | |
1059 | * clobbering the runtime directory unnecessarily. */ | |
1060 | ||
1061 | r = configure_runtime_directory(handle, ur, runtime_path); | |
1062 | if (r != PAM_SUCCESS) | |
1063 | return r; | |
1064 | } | |
1065 | ||
1066 | /* Most likely we got the session/type/class from environment variables, but might have gotten the data | |
1067 | * somewhere else (for example PAM module parameters). Let's now update the environment variables, so that this | |
1068 | * data is inherited into the session processes, and programs can rely on them to be initialized. */ | |
1069 | ||
1070 | r = update_environment(handle, "XDG_SESSION_TYPE", type); | |
1071 | if (r != PAM_SUCCESS) | |
1072 | return r; | |
1073 | ||
1074 | r = update_environment(handle, "XDG_SESSION_CLASS", class); | |
1075 | if (r != PAM_SUCCESS) | |
1076 | return r; | |
1077 | ||
1078 | r = update_environment(handle, "XDG_SESSION_DESKTOP", desktop); | |
1079 | if (r != PAM_SUCCESS) | |
1080 | return r; | |
1081 | ||
1082 | r = update_environment(handle, "XDG_SEAT", seat); | |
1083 | if (r != PAM_SUCCESS) | |
1084 | return r; | |
1085 | ||
1086 | if (vtnr > 0) { | |
1087 | char buf[DECIMAL_STR_MAX(vtnr)]; | |
1088 | sprintf(buf, "%u", vtnr); | |
1089 | ||
1090 | r = update_environment(handle, "XDG_VTNR", buf); | |
1091 | if (r != PAM_SUCCESS) | |
1092 | return r; | |
1093 | } | |
1094 | ||
1095 | r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL); | |
1096 | if (r != PAM_SUCCESS) | |
1097 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to install existing flag: @PAMERR@"); | |
1098 | ||
1099 | if (session_fd >= 0) { | |
1100 | _cleanup_close_ int fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3); | |
1101 | if (fd < 0) | |
1102 | return pam_syslog_errno(handle, LOG_ERR, errno, "Failed to dup session fd: %m"); | |
1103 | ||
1104 | r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(fd), NULL); | |
1105 | if (r != PAM_SUCCESS) | |
1106 | return pam_syslog_pam_error(handle, LOG_ERR, r, "Failed to install session fd: @PAMERR@"); | |
1107 | TAKE_FD(fd); | |
1108 | } | |
1109 | ||
1110 | success: | |
1111 | if (default_capability_ambient_set == UINT64_MAX) | |
1112 | default_capability_ambient_set = pick_default_capability_ambient_set(ur, service, seat); | |
1113 | ||
1114 | return apply_user_record_settings(handle, ur, debug, default_capability_bounding_set, default_capability_ambient_set); | |
1115 | } | |
1116 | ||
1117 | _public_ PAM_EXTERN int pam_sm_close_session( | |
1118 | pam_handle_t *handle, | |
1119 | int flags, | |
1120 | int argc, const char **argv) { | |
1121 | ||
1122 | const void *existing = NULL; | |
1123 | bool debug = false; | |
1124 | const char *id; | |
1125 | int r; | |
1126 | ||
1127 | assert(handle); | |
1128 | ||
1129 | if (parse_argv(handle, | |
1130 | argc, argv, | |
1131 | NULL, | |
1132 | NULL, | |
1133 | NULL, | |
1134 | &debug, | |
1135 | NULL, | |
1136 | NULL) < 0) | |
1137 | return PAM_SESSION_ERR; | |
1138 | ||
1139 | if (debug) | |
1140 | pam_syslog(handle, LOG_DEBUG, "pam-systemd shutting down"); | |
1141 | ||
1142 | /* Only release session if it wasn't pre-existing when we | |
1143 | * tried to create it */ | |
1144 | r = pam_get_data(handle, "systemd.existing", &existing); | |
1145 | if (!IN_SET(r, PAM_SUCCESS, PAM_NO_MODULE_DATA)) | |
1146 | return pam_syslog_pam_error(handle, LOG_ERR, r, | |
1147 | "Failed to get PAM systemd.existing data: @PAMERR@"); | |
1148 | ||
1149 | id = pam_getenv(handle, "XDG_SESSION_ID"); | |
1150 | if (id && !existing) { | |
1151 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1152 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
1153 | ||
1154 | /* Before we go and close the FIFO we need to tell logind that this is a clean session | |
1155 | * shutdown, so that it doesn't just go and slaughter us immediately after closing the fd */ | |
1156 | ||
1157 | r = pam_acquire_bus_connection(handle, "pam-systemd", &bus, NULL); | |
1158 | if (r != PAM_SUCCESS) | |
1159 | return r; | |
1160 | ||
1161 | r = bus_call_method(bus, bus_login_mgr, "ReleaseSession", &error, NULL, "s", id); | |
1162 | if (r < 0) | |
1163 | return pam_syslog_pam_error(handle, LOG_ERR, PAM_SESSION_ERR, | |
1164 | "Failed to release session: %s", bus_error_message(&error, r)); | |
1165 | } | |
1166 | ||
1167 | /* Note that we are knowingly leaking the FIFO fd here. This way, logind can watch us die. If we | |
1168 | * closed it here it would not have any clue when that is completed. Given that one cannot really | |
1169 | * have multiple PAM sessions open from the same process this means we will leak one FD at max. */ | |
1170 | ||
1171 | return PAM_SUCCESS; | |
1172 | } |