]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/pam_systemd.c
pam_systemd: also make $XDG_SESSION_DESKTOP configurable via PAM module command line
[thirdparty/systemd.git] / src / login / pam_systemd.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
8c6db833 2
00229fe4 3#include <endian.h>
8c6db833
LP
4#include <errno.h>
5#include <fcntl.h>
8c6db833 6#include <pwd.h>
8c6db833 7#include <security/_pam_macros.h>
8c6db833
LP
8#include <security/pam_ext.h>
9#include <security/pam_misc.h>
00229fe4
LP
10#include <security/pam_modules.h>
11#include <security/pam_modutil.h>
12#include <sys/file.h>
8c6db833 13
b5efdb8a 14#include "alloc-util.h"
430f0182 15#include "audit-util.h"
00229fe4
LP
16#include "bus-common-errors.h"
17#include "bus-error.h"
ffcfcb6b 18#include "bus-util.h"
98a28fef 19#include "def.h"
3ffd4af2 20#include "fd-util.h"
a5c32cff 21#include "fileio.h"
f97b34a6 22#include "format-util.h"
958b66ea 23#include "hostname-util.h"
00229fe4
LP
24#include "login-util.h"
25#include "macro.h"
6bedfcbb 26#include "parse-util.h"
df0ff127 27#include "process-util.h"
00229fe4
LP
28#include "socket-util.h"
29#include "strv.h"
30#include "terminal-util.h"
31#include "util.h"
c9ed61e7 32#include "path-util.h"
22f93314 33#include "cgroup-util.h"
8c6db833 34
baae0358
LP
35static int parse_argv(
36 pam_handle_t *handle,
37 int argc, const char **argv,
38 const char **class,
49ebd11f 39 const char **type,
f5cb2820 40 const char **desktop,
baae0358 41 bool *debug) {
8c6db833
LP
42
43 unsigned i;
44
45 assert(argc >= 0);
46 assert(argc == 0 || argv);
47
49ebd11f 48 for (i = 0; i < (unsigned) argc; i++) {
fb6becb4 49 if (startswith(argv[i], "class=")) {
485507b8
MM
50 if (class)
51 *class = argv[i] + 6;
52
49ebd11f
LP
53 } else if (startswith(argv[i], "type=")) {
54 if (type)
55 *type = argv[i] + 5;
56
f5cb2820
LP
57 } else if (startswith(argv[i], "desktop=")) {
58 if (desktop)
59 *desktop = argv[i] + 8;
60
05a049cc
ZJS
61 } else if (streq(argv[i], "debug")) {
62 if (debug)
63 *debug = true;
fb6becb4 64
05a049cc
ZJS
65 } else if (startswith(argv[i], "debug=")) {
66 int k;
0e318cad 67
05a049cc
ZJS
68 k = parse_boolean(argv[i] + 6);
69 if (k < 0)
70 pam_syslog(handle, LOG_WARNING, "Failed to parse debug= argument, ignoring.");
71 else if (debug)
0e318cad
MS
72 *debug = k;
73
05a049cc 74 } else
fb6becb4 75 pam_syslog(handle, LOG_WARNING, "Unknown parameter '%s', ignoring", argv[i]);
49ebd11f 76 }
8c6db833 77
8c6db833
LP
78 return 0;
79}
80
8c6db833
LP
81static int get_user_data(
82 pam_handle_t *handle,
83 const char **ret_username,
84 struct passwd **ret_pw) {
85
d90b9d27
LP
86 const char *username = NULL;
87 struct passwd *pw = NULL;
8c6db833
LP
88 int r;
89
90 assert(handle);
91 assert(ret_username);
92 assert(ret_pw);
93
baae0358
LP
94 r = pam_get_user(handle, &username, NULL);
95 if (r != PAM_SUCCESS) {
96 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
97 return r;
98 }
d90b9d27 99
baae0358
LP
100 if (isempty(username)) {
101 pam_syslog(handle, LOG_ERR, "User name not valid.");
102 return PAM_AUTH_ERR;
8c6db833
LP
103 }
104
baae0358 105 pw = pam_modutil_getpwnam(handle, username);
d90b9d27 106 if (!pw) {
8c6db833
LP
107 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
108 return PAM_USER_UNKNOWN;
109 }
110
111 *ret_pw = pw;
8e24a4f8 112 *ret_username = username;
8c6db833
LP
113
114 return PAM_SUCCESS;
115}
116
4d6d6518 117static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
b92bea5d
ZJS
118 union sockaddr_union sa = {
119 .un.sun_family = AF_UNIX,
120 };
baae0358
LP
121 _cleanup_free_ char *p = NULL, *tty = NULL;
122 _cleanup_close_ int fd = -1;
4d6d6518 123 struct ucred ucred;
baae0358 124 int v, r;
4d6d6518
LP
125
126 assert(display);
4d6d6518
LP
127 assert(vtnr);
128
129 /* We deduce the X11 socket from the display name, then use
130 * SO_PEERCRED to determine the X11 server process, ask for
131 * the controlling tty of that and if it's a VC then we know
132 * the seat and the virtual terminal. Sounds ugly, is only
133 * semi-ugly. */
134
135 r = socket_from_display(display, &p);
136 if (r < 0)
137 return r;
b92bea5d 138 strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
4d6d6518
LP
139
140 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
b92bea5d 141 if (fd < 0)
4d6d6518 142 return -errno;
4d6d6518 143
fc2fffe7 144 if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0)
4d6d6518 145 return -errno;
4d6d6518 146
eff05270 147 r = getpeercred(fd, &ucred);
4d6d6518 148 if (r < 0)
eff05270 149 return r;
4d6d6518
LP
150
151 r = get_ctty(ucred.pid, NULL, &tty);
152 if (r < 0)
153 return r;
154
155 v = vtnr_from_tty(tty);
4d6d6518
LP
156 if (v < 0)
157 return v;
158 else if (v == 0)
159 return -ENOENT;
160
fc7985ed
LP
161 if (seat)
162 *seat = "seat0";
4d6d6518
LP
163 *vtnr = (uint32_t) v;
164
165 return 0;
166}
167
22f93314
JS
168static int append_session_memory_max(pam_handle_t *handle, sd_bus_message *m, const char *limit) {
169 uint64_t val;
170 int r;
171
172 if (isempty(limit))
173 return 0;
174
175 if (streq(limit, "infinity")) {
176 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", (uint64_t)-1);
177 if (r < 0) {
178 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
179 return r;
180 }
181 } else {
182 r = parse_percent(limit);
183 if (r >= 0) {
184 r = sd_bus_message_append(m, "(sv)", "MemoryMaxScale", "u", (uint32_t) (((uint64_t) UINT32_MAX * r) / 100U));
185 if (r < 0) {
186 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
187 return r;
188 }
189 } else {
190 r = parse_size(limit, 1024, &val);
191 if (r >= 0) {
192 r = sd_bus_message_append(m, "(sv)", "MemoryMax", "t", val);
193 if (r < 0) {
194 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
195 return r;
196 }
197 } else
198 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit);
199 }
200 }
201
202 return 0;
203}
204
205static int append_session_tasks_max(pam_handle_t *handle, sd_bus_message *m, const char *limit)
206{
207 uint64_t val;
208 int r;
209
210 /* No need to parse "infinity" here, it will be set unconditionally later in manager_start_scope() */
211 if (isempty(limit) || streq(limit, "infinity"))
212 return 0;
213
214 r = safe_atou64(limit, &val);
215 if (r >= 0) {
216 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", val);
217 if (r < 0) {
218 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
219 return r;
220 }
221 } else
222 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.limit: %s, ignoring.", limit);
223
224 return 0;
225}
226
227static int append_session_cg_weight(pam_handle_t *handle, sd_bus_message *m, const char *limit, const char *field) {
228 uint64_t val;
229 int r;
230
231 if (!isempty(limit)) {
232 r = cg_weight_parse(limit, &val);
233 if (r >= 0) {
234 r = sd_bus_message_append(m, "(sv)", field, "t", val);
235 if (r < 0) {
236 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
237 return r;
238 }
239 } else if (streq(field, "CPUWeight"))
240 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.cpu_weight: %s, ignoring.", limit);
241 else
242 pam_syslog(handle, LOG_WARNING, "Failed to parse systemd.io_weight: %s, ignoring.", limit);
243 }
244
245 return 0;
246}
247
0ecc1c9d
LP
248static const char* getenv_harder(pam_handle_t *handle, const char *key, const char *fallback) {
249 const char *v;
250
251 assert(handle);
252 assert(key);
253
254 /* Looks for an environment variable, preferrably in the environment block associated with the specified PAM
255 * handle, falling back to the process' block instead. */
256
257 v = pam_getenv(handle, key);
258 if (!isempty(v))
259 return v;
260
261 v = getenv(key);
262 if (!isempty(v))
263 return v;
264
265 return fallback;
266}
267
98a28fef 268_public_ PAM_EXTERN int pam_sm_open_session(
8c6db833
LP
269 pam_handle_t *handle,
270 int flags,
271 int argc, const char **argv) {
272
4afd3348 273 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
22f93314 274 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
d1529c9e
LP
275 const char
276 *username, *id, *object_path, *runtime_path,
277 *service = NULL,
278 *tty = NULL, *display = NULL,
279 *remote_user = NULL, *remote_host = NULL,
280 *seat = NULL,
281 *type = NULL, *class = NULL,
f5cb2820 282 *class_pam = NULL, *type_pam = NULL, *cvtnr = NULL, *desktop = NULL, *desktop_pam = NULL,
22f93314 283 *memory_max = NULL, *tasks_max = NULL, *cpu_weight = NULL, *io_weight = NULL;
4afd3348 284 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
d1529c9e 285 int session_fd = -1, existing, r;
d1529c9e
LP
286 bool debug = false, remote;
287 struct passwd *pw;
baae0358
LP
288 uint32_t vtnr = 0;
289 uid_t original_uid;
8c6db833 290
ffcfcb6b 291 assert(handle);
98a28fef 292
79d860fe
MP
293 /* Make this a NOP on non-logind systems */
294 if (!logind_running())
8c6db833
LP
295 return PAM_SUCCESS;
296
e9fbc77c
LP
297 if (parse_argv(handle,
298 argc, argv,
fb6becb4 299 &class_pam,
49ebd11f 300 &type_pam,
f5cb2820 301 &desktop_pam,
5a330cda
ZJS
302 &debug) < 0)
303 return PAM_SESSION_ERR;
74fe1fe3 304
baae0358 305 if (debug)
3831838a 306 pam_syslog(handle, LOG_DEBUG, "pam-systemd initializing");
baae0358 307
98a28fef 308 r = get_user_data(handle, &username, &pw);
5a330cda
ZJS
309 if (r != PAM_SUCCESS) {
310 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
311 return r;
312 }
8c6db833 313
30b2c336
LP
314 /* Make sure we don't enter a loop by talking to
315 * systemd-logind when it is actually waiting for the
316 * background to finish start-up. If the service is
5c390a4a 317 * "systemd-user" we simply set XDG_RUNTIME_DIR and
30b2c336
LP
318 * leave. */
319
320 pam_get_item(handle, PAM_SERVICE, (const void**) &service);
5c390a4a 321 if (streq_ptr(service, "systemd-user")) {
f9e4283d 322 _cleanup_free_ char *rt = NULL;
30b2c336 323
f9e4283d 324 if (asprintf(&rt, "/run/user/"UID_FMT, pw->pw_uid) < 0)
ffcfcb6b 325 return PAM_BUF_ERR;
30b2c336 326
f9e4283d
DH
327 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
328 if (r != PAM_SUCCESS) {
329 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
330 return r;
30b2c336
LP
331 }
332
ffcfcb6b 333 return PAM_SUCCESS;
8c6db833
LP
334 }
335
ffcfcb6b 336 /* Otherwise, we ask logind to create a session for us */
8c6db833 337
98a28fef
LP
338 pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
339 pam_get_item(handle, PAM_TTY, (const void**) &tty);
340 pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
341 pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
a8573ccc 342
0ecc1c9d
LP
343 seat = getenv_harder(handle, "XDG_SEAT", NULL);
344 cvtnr = getenv_harder(handle, "XDG_VTNR", NULL);
345 type = getenv_harder(handle, "XDG_SESSION_TYPE", type_pam);
346 class = getenv_harder(handle, "XDG_SESSION_CLASS", class_pam);
f5cb2820 347 desktop = getenv_harder(handle, "XDG_SESSION_DESKTOP", desktop_pam);
a4cd87e9 348
ed18b08b 349 tty = strempty(tty);
98a28fef 350
ee8545b0 351 if (strchr(tty, ':')) {
3a736949
LP
352 /* A tty with a colon is usually an X11 display, placed there to show up in utmp. We rearrange things
353 * and don't pretend that an X display was a tty. */
ee8545b0
LP
354 if (isempty(display))
355 display = tty;
a4cd87e9 356 tty = NULL;
3a736949 357
1a4459d6 358 } else if (streq(tty, "cron")) {
3a736949
LP
359 /* cron is setting PAM_TTY to "cron" for some reason (the commit carries no information why, but
360 * probably because it wants to set it to something as pam_time/pam_access/… require PAM_TTY to be set
361 * (as they otherwise even try to update it!) — but cron doesn't actually allocate a TTY for its forked
362 * off processes.) */
0ad1271f 363 type = "unspecified";
49ebd11f 364 class = "background";
a4cd87e9 365 tty = NULL;
3a736949 366
0ad1271f 367 } else if (streq(tty, "ssh")) {
3a736949
LP
368 /* ssh has been setting PAM_TTY to "ssh" (for the same reason as cron does this, see above. For further
369 * details look for "PAM_TTY_KLUDGE" in the openssh sources). */
0ad1271f 370 type ="tty";
49ebd11f 371 class = "user";
3a736949
LP
372 tty = NULL; /* This one is particularly sad, as this means that ssh sessions — even though usually
373 * associated with a pty — won't be tracked by their tty in logind. This is because ssh
374 * does the PAM session registration early for new connections, and registers a pty only
375 * much later (this is because it doesn't know yet if it needs one at all, as whether to
376 * register a pty or not is negotiated much later in the protocol). */
377
c9ed61e7
LP
378 } else
379 /* Chop off leading /dev prefix that some clients specify, but others do not. */
380 tty = skip_dev_prefix(tty);
ee8545b0 381
8e7705e5 382 /* If this fails vtnr will be 0, that's intended */
4d6d6518 383 if (!isempty(cvtnr))
2ae4842b 384 (void) safe_atou32(cvtnr, &vtnr);
4d6d6518 385
92bd5ff3 386 if (!isempty(display) && !vtnr) {
fc7985ed 387 if (isempty(seat))
6ef25fb6 388 get_seat_from_display(display, &seat, &vtnr);
fc7985ed 389 else if (streq(seat, "seat0"))
6ef25fb6 390 get_seat_from_display(display, NULL, &vtnr);
fc7985ed 391 }
4d6d6518 392
49ebd11f 393 if (seat && !streq(seat, "seat0") && vtnr != 0) {
1fa2f38f 394 pam_syslog(handle, LOG_DEBUG, "Ignoring vtnr %"PRIu32" for %s which is not seat0", vtnr, seat);
d7353ef6
MM
395 vtnr = 0;
396 }
397
49ebd11f 398 if (isempty(type))
0ad1271f 399 type = !isempty(display) ? "x11" :
49ebd11f 400 !isempty(tty) ? "tty" : "unspecified";
98a28fef 401
55efac6c 402 if (isempty(class))
e2acb67b 403 class = streq(type, "unspecified") ? "background" : "user";
55efac6c 404
fecc80c1 405 remote = !isempty(remote_host) && !is_localhost(remote_host);
98a28fef 406
22f93314
JS
407 (void) pam_get_data(handle, "systemd.memory_max", (const void **)&memory_max);
408 (void) pam_get_data(handle, "systemd.tasks_max", (const void **)&tasks_max);
409 (void) pam_get_data(handle, "systemd.cpu_weight", (const void **)&cpu_weight);
410 (void) pam_get_data(handle, "systemd.io_weight", (const void **)&io_weight);
411
4d49b48c 412 /* Talk to logind over the message bus */
5a330cda 413
ffcfcb6b
ZJS
414 r = sd_bus_open_system(&bus);
415 if (r < 0) {
416 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r));
417 return PAM_SESSION_ERR;
cc377381
LP
418 }
419
22f93314 420 if (debug) {
ce959314 421 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
1fa2f38f 422 "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",
df0ff127 423 pw->pw_uid, getpid_cached(),
baae0358 424 strempty(service),
cda7ecb0 425 type, class, strempty(desktop),
a4cd87e9 426 strempty(seat), vtnr, strempty(tty), strempty(display),
baae0358 427 yes_no(remote), strempty(remote_user), strempty(remote_host));
22f93314
JS
428 pam_syslog(handle, LOG_DEBUG, "Session limits: "
429 "memory_max=%s tasks_max=%s cpu_weight=%s io_weight=%s",
430 strna(memory_max), strna(tasks_max), strna(cpu_weight), strna(io_weight));
431 }
432
433 r = sd_bus_message_new_method_call(
434 bus,
435 &m,
436 "org.freedesktop.login1",
437 "/org/freedesktop/login1",
438 "org.freedesktop.login1.Manager",
439 "CreateSession");
440 if (r < 0) {
441 pam_syslog(handle, LOG_ERR, "Failed to create CreateSession method call: %s", strerror(-r));
442 return PAM_SESSION_ERR;
443 }
444
445 r = sd_bus_message_append(m, "uusssssussbss",
446 (uint32_t) pw->pw_uid,
447 (uint32_t) getpid_cached(),
448 service,
449 type,
450 class,
451 desktop,
452 seat,
453 vtnr,
454 tty,
455 display,
456 remote,
457 remote_user,
458 remote_host);
459 if (r < 0) {
460 pam_syslog(handle, LOG_ERR, "Failed to append to bus message: %s", strerror(-r));
461 return PAM_SESSION_ERR;
462 }
463
464 r = sd_bus_message_open_container(m, 'a', "(sv)");
465 if (r < 0) {
466 pam_syslog(handle, LOG_ERR, "Failed to open message container: %s", strerror(-r));
467 return PAM_SYSTEM_ERR;
468 }
469
470 r = append_session_memory_max(handle, m, memory_max);
471 if (r < 0)
472 return PAM_SESSION_ERR;
473
474 r = append_session_tasks_max(handle, m, tasks_max);
475 if (r < 0)
476 return PAM_SESSION_ERR;
477
478 r = append_session_cg_weight(handle, m, cpu_weight, "CPUWeight");
479 if (r < 0)
480 return PAM_SESSION_ERR;
481
482 r = append_session_cg_weight(handle, m, io_weight, "IOWeight");
483 if (r < 0)
484 return PAM_SESSION_ERR;
485
486 r = sd_bus_message_close_container(m);
487 if (r < 0) {
488 pam_syslog(handle, LOG_ERR, "Failed to close message container: %s", strerror(-r));
489 return PAM_SYSTEM_ERR;
490 }
ce959314 491
22f93314 492 r = sd_bus_call(bus, m, 0, &error, &reply);
ffcfcb6b 493 if (r < 0) {
b80120c4
DH
494 if (sd_bus_error_has_name(&error, BUS_ERROR_SESSION_BUSY)) {
495 pam_syslog(handle, LOG_DEBUG, "Cannot create session: %s", bus_error_message(&error, r));
496 return PAM_SUCCESS;
497 } else {
498 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error, r));
499 return PAM_SYSTEM_ERR;
500 }
98a28fef 501 }
74fe1fe3 502
ffcfcb6b 503 r = sd_bus_message_read(reply,
baae0358 504 "soshusub",
ffcfcb6b
ZJS
505 &id,
506 &object_path,
507 &runtime_path,
508 &session_fd,
baae0358 509 &original_uid,
ffcfcb6b
ZJS
510 &seat,
511 &vtnr,
512 &existing);
513 if (r < 0) {
514 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", strerror(-r));
5a330cda 515 return PAM_SESSION_ERR;
98a28fef 516 }
74fe1fe3 517
ce959314
MS
518 if (debug)
519 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
baae0358
LP
520 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u original_uid=%u",
521 id, object_path, runtime_path, session_fd, seat, vtnr, original_uid);
ce959314 522
98a28fef
LP
523 r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
524 if (r != PAM_SUCCESS) {
525 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
5a330cda 526 return r;
8c6db833
LP
527 }
528
baae0358
LP
529 if (original_uid == pw->pw_uid) {
530 /* Don't set $XDG_RUNTIME_DIR if the user we now
531 * authenticated for does not match the original user
532 * of the session. We do this in order not to result
533 * in privileged apps clobbering the runtime directory
534 * unnecessarily. */
535
536 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
537 if (r != PAM_SUCCESS) {
538 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
539 return r;
540 }
98a28fef 541 }
8c6db833 542
bbc73283
LP
543 if (!isempty(seat)) {
544 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
545 if (r != PAM_SUCCESS) {
546 pam_syslog(handle, LOG_ERR, "Failed to set seat.");
5a330cda 547 return r;
bbc73283
LP
548 }
549 }
550
551 if (vtnr > 0) {
29d230f6 552 char buf[DECIMAL_STR_MAX(vtnr)];
baae0358 553 sprintf(buf, "%u", vtnr);
bbc73283
LP
554
555 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
556 if (r != PAM_SUCCESS) {
557 pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
5a330cda 558 return r;
bbc73283
LP
559 }
560 }
561
77085881
LP
562 r = pam_set_data(handle, "systemd.existing", INT_TO_PTR(!!existing), NULL);
563 if (r != PAM_SUCCESS) {
564 pam_syslog(handle, LOG_ERR, "Failed to install existing flag.");
5a330cda 565 return r;
77085881
LP
566 }
567
21c390cc 568 if (session_fd >= 0) {
85c08dc0 569 session_fd = fcntl(session_fd, F_DUPFD_CLOEXEC, 3);
5a330cda
ZJS
570 if (session_fd < 0) {
571 pam_syslog(handle, LOG_ERR, "Failed to dup session fd: %m");
572 return PAM_SESSION_ERR;
573 }
574
23e096cc 575 r = pam_set_data(handle, "systemd.session-fd", FD_TO_PTR(session_fd), NULL);
21c390cc
LP
576 if (r != PAM_SUCCESS) {
577 pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
03e334a1 578 safe_close(session_fd);
5a330cda 579 return r;
21c390cc 580 }
98a28fef 581 }
8c6db833 582
ffcfcb6b 583 return PAM_SUCCESS;
98a28fef 584}
8c6db833 585
98a28fef
LP
586_public_ PAM_EXTERN int pam_sm_close_session(
587 pam_handle_t *handle,
588 int flags,
589 int argc, const char **argv) {
8c6db833 590
4afd3348
LP
591 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
592 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5f41d1f1 593 const void *existing = NULL;
75c8e3cf 594 const char *id;
75c8e3cf 595 int r;
8c6db833 596
ffcfcb6b 597 assert(handle);
75c8e3cf 598
77085881
LP
599 /* Only release session if it wasn't pre-existing when we
600 * tried to create it */
601 pam_get_data(handle, "systemd.existing", &existing);
602
75c8e3cf 603 id = pam_getenv(handle, "XDG_SESSION_ID");
77085881 604 if (id && !existing) {
75c8e3cf
LP
605
606 /* Before we go and close the FIFO we need to tell
607 * logind that this is a clean session shutdown, so
608 * that it doesn't just go and slaughter us
609 * immediately after closing the fd */
610
ffcfcb6b
ZJS
611 r = sd_bus_open_system(&bus);
612 if (r < 0) {
5f41d1f1
LP
613 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", strerror(-r));
614 return PAM_SESSION_ERR;
75c8e3cf
LP
615 }
616
ffcfcb6b
ZJS
617 r = sd_bus_call_method(bus,
618 "org.freedesktop.login1",
619 "/org/freedesktop/login1",
620 "org.freedesktop.login1.Manager",
621 "ReleaseSession",
622 &error,
623 NULL,
624 "s",
625 id);
626 if (r < 0) {
5f41d1f1
LP
627 pam_syslog(handle, LOG_ERR, "Failed to release session: %s", bus_error_message(&error, r));
628 return PAM_SESSION_ERR;
75c8e3cf
LP
629 }
630 }
631
5f41d1f1
LP
632 /* Note that we are knowingly leaking the FIFO fd here. This
633 * way, logind can watch us die. If we closed it here it would
634 * not have any clue when that is completed. Given that one
635 * cannot really have multiple PAM sessions open from the same
636 * process this means we will leak one FD at max. */
75c8e3cf 637
5f41d1f1 638 return PAM_SUCCESS;
8c6db833 639}