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