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