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