]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/pam-module.c
journal: add output mode that just prints simple messages without any decorations
[thirdparty/systemd.git] / src / login / pam-module.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
8c6db833
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/file.h>
25#include <pwd.h>
a838e6a1 26#include <endian.h>
ac123445 27#include <sys/capability.h>
8c6db833
LP
28
29#include <security/pam_modules.h>
30#include <security/_pam_macros.h>
31#include <security/pam_modutil.h>
32#include <security/pam_ext.h>
33#include <security/pam_misc.h>
34
81527be1
LP
35#include <systemd/sd-daemon.h>
36
8c6db833 37#include "util.h"
8c6db833 38#include "macro.h"
74fe1fe3 39#include "strv.h"
98a28fef
LP
40#include "dbus-common.h"
41#include "def.h"
4d6d6518 42#include "socket-util.h"
8c6db833
LP
43
44static int parse_argv(pam_handle_t *handle,
45 int argc, const char **argv,
b20c6be6 46 char ***controllers,
e9fbc77c 47 char ***reset_controllers,
98a28fef 48 bool *kill_processes,
e9fbc77c 49 char ***kill_only_users,
0e318cad
MS
50 char ***kill_exclude_users,
51 bool *debug) {
8c6db833
LP
52
53 unsigned i;
54
55 assert(argc >= 0);
56 assert(argc == 0 || argv);
57
58 for (i = 0; i < (unsigned) argc; i++) {
59 int k;
60
c36eecdf
LP
61 if (startswith(argv[i], "kill-session-processes=")) {
62 if ((k = parse_boolean(argv[i] + 23)) < 0) {
63 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session-processes= argument.");
8c6db833
LP
64 return k;
65 }
66
98a28fef
LP
67 if (kill_processes)
68 *kill_processes = k;
74fe1fe3 69
8c6db833 70 } else if (startswith(argv[i], "kill-session=")) {
98a28fef
LP
71 /* As compatibility for old versions */
72
8c6db833
LP
73 if ((k = parse_boolean(argv[i] + 13)) < 0) {
74 pam_syslog(handle, LOG_ERR, "Failed to parse kill-session= argument.");
75 return k;
76 }
77
98a28fef
LP
78 if (kill_processes)
79 *kill_processes = k;
74fe1fe3
LP
80
81 } else if (startswith(argv[i], "controllers=")) {
82
83 if (controllers) {
84 char **l;
85
86 if (!(l = strv_split(argv[i] + 12, ","))) {
87 pam_syslog(handle, LOG_ERR, "Out of memory.");
88 return -ENOMEM;
89 }
90
91 strv_free(*controllers);
92 *controllers = l;
93 }
94
b20c6be6
LP
95 } else if (startswith(argv[i], "reset-controllers=")) {
96
97 if (reset_controllers) {
98 char **l;
99
100 if (!(l = strv_split(argv[i] + 18, ","))) {
101 pam_syslog(handle, LOG_ERR, "Out of memory.");
102 return -ENOMEM;
103 }
104
105 strv_free(*reset_controllers);
106 *reset_controllers = l;
107 }
108
e9fbc77c
LP
109 } else if (startswith(argv[i], "kill-only-users=")) {
110
111 if (kill_only_users) {
112 char **l;
113
114 if (!(l = strv_split(argv[i] + 16, ","))) {
115 pam_syslog(handle, LOG_ERR, "Out of memory.");
116 return -ENOMEM;
117 }
118
119 strv_free(*kill_only_users);
120 *kill_only_users = l;
121 }
122
123 } else if (startswith(argv[i], "kill-exclude-users=")) {
124
125 if (kill_exclude_users) {
126 char **l;
127
128 if (!(l = strv_split(argv[i] + 19, ","))) {
129 pam_syslog(handle, LOG_ERR, "Out of memory.");
130 return -ENOMEM;
131 }
132
133 strv_free(*kill_exclude_users);
134 *kill_exclude_users = l;
135 }
136
0e318cad
MS
137 } else if (startswith(argv[i], "debug=")) {
138 if ((k = parse_boolean(argv[i] + 6)) < 0) {
139 pam_syslog(handle, LOG_ERR, "Failed to parse debug= argument.");
140 return k;
141 }
142
143 if (debug)
144 *debug = k;
145
98a28fef
LP
146 } else if (startswith(argv[i], "create-session=") ||
147 startswith(argv[i], "kill-user=")) {
148
149 pam_syslog(handle, LOG_WARNING, "Option %s not supported anymore, ignoring.", argv[i]);
150
8c6db833
LP
151 } else {
152 pam_syslog(handle, LOG_ERR, "Unknown parameter '%s'.", argv[i]);
153 return -EINVAL;
154 }
155 }
156
8c6db833
LP
157 return 0;
158}
159
8c6db833
LP
160static int get_user_data(
161 pam_handle_t *handle,
162 const char **ret_username,
163 struct passwd **ret_pw) {
164
d90b9d27
LP
165 const char *username = NULL;
166 struct passwd *pw = NULL;
87d2c1ff 167 uid_t uid;
8c6db833
LP
168 int r;
169
170 assert(handle);
171 assert(ret_username);
172 assert(ret_pw);
173
87d2c1ff
LP
174 r = audit_loginuid_from_pid(0, &uid);
175 if (r >= 0)
176 pw = pam_modutil_getpwuid(handle, uid);
177 else {
178 r = pam_get_user(handle, &username, NULL);
179 if (r != PAM_SUCCESS) {
d90b9d27
LP
180 pam_syslog(handle, LOG_ERR, "Failed to get user name.");
181 return r;
182 }
183
87d2c1ff 184 if (isempty(username)) {
d90b9d27
LP
185 pam_syslog(handle, LOG_ERR, "User name not valid.");
186 return PAM_AUTH_ERR;
187 }
188
189 pw = pam_modutil_getpwnam(handle, username);
8c6db833
LP
190 }
191
d90b9d27 192 if (!pw) {
8c6db833
LP
193 pam_syslog(handle, LOG_ERR, "Failed to get user data.");
194 return PAM_USER_UNKNOWN;
195 }
196
197 *ret_pw = pw;
d90b9d27 198 *ret_username = username ? username : pw->pw_name;
8c6db833
LP
199
200 return PAM_SUCCESS;
201}
202
e9fbc77c
LP
203static bool check_user_lists(
204 pam_handle_t *handle,
205 uid_t uid,
206 char **kill_only_users,
207 char **kill_exclude_users) {
208
209 const char *name = NULL;
210 char **l;
211
212 assert(handle);
213
214 if (uid == 0)
215 name = "root"; /* Avoid obvious NSS requests, to suppress network traffic */
216 else {
217 struct passwd *pw;
218
98a28fef
LP
219 pw = pam_modutil_getpwuid(handle, uid);
220 if (pw)
e9fbc77c
LP
221 name = pw->pw_name;
222 }
223
224 STRV_FOREACH(l, kill_exclude_users) {
ddd88763 225 uid_t u;
e9fbc77c 226
ddd88763
LP
227 if (parse_uid(*l, &u) >= 0)
228 if (u == uid)
e9fbc77c
LP
229 return false;
230
231 if (name && streq(name, *l))
232 return false;
233 }
234
235 if (strv_isempty(kill_only_users))
236 return true;
237
238 STRV_FOREACH(l, kill_only_users) {
ddd88763 239 uid_t u;
e9fbc77c 240
ddd88763
LP
241 if (parse_uid(*l, &u) >= 0)
242 if (u == uid)
e9fbc77c
LP
243 return true;
244
245 if (name && streq(name, *l))
246 return true;
247 }
248
249 return false;
250}
251
4d6d6518
LP
252static int get_seat_from_display(const char *display, const char **seat, uint32_t *vtnr) {
253 char *p = NULL;
254 int r;
255 int fd;
256 union sockaddr_union sa;
257 struct ucred ucred;
258 socklen_t l;
259 char *tty;
260 int v;
261
262 assert(display);
263 assert(seat);
264 assert(vtnr);
265
266 /* We deduce the X11 socket from the display name, then use
267 * SO_PEERCRED to determine the X11 server process, ask for
268 * the controlling tty of that and if it's a VC then we know
269 * the seat and the virtual terminal. Sounds ugly, is only
270 * semi-ugly. */
271
272 r = socket_from_display(display, &p);
273 if (r < 0)
274 return r;
275
276 fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
277 if (fd < 0) {
278 free(p);
279 return -errno;
280 }
281
282 zero(sa);
283 sa.un.sun_family = AF_UNIX;
284 strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1);
285 free(p);
286
287 if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
288 close_nointr_nofail(fd);
289 return -errno;
290 }
291
292 l = sizeof(ucred);
293 r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l);
294 close_nointr_nofail(fd);
295
296 if (r < 0)
297 return -errno;
298
299 r = get_ctty(ucred.pid, NULL, &tty);
300 if (r < 0)
301 return r;
302
303 v = vtnr_from_tty(tty);
304 free(tty);
305
306 if (v < 0)
307 return v;
308 else if (v == 0)
309 return -ENOENT;
310
311 *seat = "seat0";
312 *vtnr = (uint32_t) v;
313
314 return 0;
315}
316
98a28fef 317_public_ PAM_EXTERN int pam_sm_open_session(
8c6db833
LP
318 pam_handle_t *handle,
319 int flags,
320 int argc, const char **argv) {
321
8c6db833 322 struct passwd *pw;
98a28fef 323 bool kill_processes = false, debug = false;
4d6d6518 324 const char *username, *id, *object_path, *runtime_path, *service = NULL, *tty = NULL, *display = NULL, *remote_user = NULL, *remote_host = NULL, *seat = NULL, *type, *cvtnr = NULL;
98a28fef 325 char **controllers = NULL, **reset_controllers = NULL, **kill_only_users = NULL, **kill_exclude_users = NULL;
98a28fef
LP
326 DBusError error;
327 uint32_t uid, pid;
328 DBusMessageIter iter;
329 dbus_bool_t kp;
98a28fef
LP
330 int session_fd = -1;
331 DBusConnection *bus = NULL;
332 DBusMessage *m = NULL, *reply = NULL;
333 dbus_bool_t remote;
ed18b08b 334 int r;
4d6d6518 335 uint32_t vtnr = 0;
8c6db833
LP
336
337 assert(handle);
338
98a28fef
LP
339 dbus_error_init(&error);
340
ed18b08b 341 /* pam_syslog(handle, LOG_INFO, "pam-systemd initializing"); */
98a28fef 342
8c6db833
LP
343 /* Make this a NOP on non-systemd systems */
344 if (sd_booted() <= 0)
345 return PAM_SUCCESS;
346
e9fbc77c
LP
347 if (parse_argv(handle,
348 argc, argv,
98a28fef
LP
349 &controllers, &reset_controllers,
350 &kill_processes, &kill_only_users, &kill_exclude_users,
ed18b08b
LP
351 &debug) < 0) {
352 r = PAM_SESSION_ERR;
353 goto finish;
354 }
74fe1fe3 355
98a28fef
LP
356 r = get_user_data(handle, &username, &pw);
357 if (r != PAM_SUCCESS)
8c6db833
LP
358 goto finish;
359
30b2c336
LP
360 /* Make sure we don't enter a loop by talking to
361 * systemd-logind when it is actually waiting for the
362 * background to finish start-up. If the service is
363 * "systemd-shared" we simply set XDG_RUNTIME_DIR and
364 * leave. */
365
366 pam_get_item(handle, PAM_SERVICE, (const void**) &service);
367 if (streq_ptr(service, "systemd-shared")) {
368 char *p, *rt = NULL;
369
370 if (asprintf(&p, "/run/systemd/users/%lu", (unsigned long) pw->pw_uid) < 0) {
371 r = PAM_BUF_ERR;
372 goto finish;
373 }
374
375 r = parse_env_file(p, NEWLINE,
376 "RUNTIME", &rt,
377 NULL);
378 free(p);
379
380 if (r < 0 && r != -ENOENT) {
381 r = PAM_SESSION_ERR;
382 free(rt);
383 goto finish;
384 }
385
386 if (rt) {
387 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", rt, 0);
388 free(rt);
389
390 if (r != PAM_SUCCESS) {
391 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
392 goto finish;
393 }
394 }
395
396 r = PAM_SUCCESS;
397 goto finish;
398 }
399
98a28fef
LP
400 if (kill_processes)
401 kill_processes = check_user_lists(handle, pw->pw_uid, kill_only_users, kill_exclude_users);
1f73f0f1 402
ed18b08b
LP
403 dbus_connection_set_change_sigpipe(FALSE);
404
98a28fef
LP
405 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
406 if (!bus) {
407 pam_syslog(handle, LOG_ERR, "Failed to connect to system bus: %s", bus_error_message(&error));
408 r = PAM_SESSION_ERR;
8c6db833
LP
409 goto finish;
410 }
411
98a28fef
LP
412 m = dbus_message_new_method_call(
413 "org.freedesktop.login1",
414 "/org/freedesktop/login1",
415 "org.freedesktop.login1.Manager",
416 "CreateSession");
74fe1fe3 417
98a28fef
LP
418 if (!m) {
419 pam_syslog(handle, LOG_ERR, "Could not allocate create session message.");
8c6db833
LP
420 r = PAM_BUF_ERR;
421 goto finish;
422 }
423
98a28fef
LP
424 uid = pw->pw_uid;
425 pid = getpid();
426
98a28fef
LP
427 pam_get_item(handle, PAM_XDISPLAY, (const void**) &display);
428 pam_get_item(handle, PAM_TTY, (const void**) &tty);
429 pam_get_item(handle, PAM_RUSER, (const void**) &remote_user);
430 pam_get_item(handle, PAM_RHOST, (const void**) &remote_host);
bbc73283
LP
431 seat = pam_getenv(handle, "XDG_SEAT");
432 cvtnr = pam_getenv(handle, "XDG_VTNR");
98a28fef 433
ed18b08b
LP
434 service = strempty(service);
435 tty = strempty(tty);
436 display = strempty(display);
437 remote_user = strempty(remote_user);
438 remote_host = strempty(remote_host);
439 seat = strempty(seat);
98a28fef 440
ee8545b0
LP
441 if (strchr(tty, ':')) {
442 /* A tty with a colon is usually an X11 display, place
443 * there to show up in utmp. We rearrange things and
444 * don't pretend that an X display was a tty */
445
446 if (isempty(display))
447 display = tty;
cd58752a 448 tty = "";
1a4459d6
MS
449 } else if (streq(tty, "cron")) {
450 /* cron has been setting PAM_TTY to "cron" for a very long time
451 * and it cannot stop doing that for compatibility reasons. */
452 tty = "";
ee8545b0
LP
453 }
454
4d6d6518
LP
455 if (!isempty(cvtnr))
456 safe_atou32(cvtnr, &vtnr);
457
458 if (!isempty(display) && isempty(seat) && vtnr <= 0)
459 get_seat_from_display(display, &seat, &vtnr);
460
98a28fef 461 type = !isempty(display) ? "x11" :
1dc99537 462 !isempty(tty) ? "tty" : "unspecified";
98a28fef
LP
463
464 remote = !isempty(remote_host) && !streq(remote_host, "localhost") && !streq(remote_host, "localhost.localdomain");
465
466 if (!dbus_message_append_args(m,
467 DBUS_TYPE_UINT32, &uid,
468 DBUS_TYPE_UINT32, &pid,
469 DBUS_TYPE_STRING, &service,
470 DBUS_TYPE_STRING, &type,
471 DBUS_TYPE_STRING, &seat,
4d6d6518 472 DBUS_TYPE_UINT32, &vtnr,
98a28fef
LP
473 DBUS_TYPE_STRING, &tty,
474 DBUS_TYPE_STRING, &display,
475 DBUS_TYPE_BOOLEAN, &remote,
476 DBUS_TYPE_STRING, &remote_user,
477 DBUS_TYPE_STRING, &remote_host,
478 DBUS_TYPE_INVALID)) {
479 pam_syslog(handle, LOG_ERR, "Could not attach parameters to message.");
480 r = PAM_BUF_ERR;
481 goto finish;
482 }
8c6db833 483
98a28fef 484 dbus_message_iter_init_append(m, &iter);
8c6db833 485
98a28fef
LP
486 r = bus_append_strv_iter(&iter, controllers);
487 if (r < 0) {
488 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
489 r = PAM_BUF_ERR;
490 goto finish;
491 }
672c48cc 492
98a28fef
LP
493 r = bus_append_strv_iter(&iter, reset_controllers);
494 if (r < 0) {
495 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
496 r = PAM_BUF_ERR;
497 goto finish;
498 }
672c48cc 499
98a28fef
LP
500 kp = kill_processes;
501 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &kp)) {
502 pam_syslog(handle, LOG_ERR, "Could not attach parameter to message.");
503 r = PAM_BUF_ERR;
504 goto finish;
505 }
8c6db833 506
ce959314
MS
507 if (debug)
508 pam_syslog(handle, LOG_DEBUG, "Asking logind to create session: "
509 "uid=%u pid=%u service=%s type=%s seat=%s vtnr=%u tty=%s display=%s remote=%s remote_user=%s remote_host=%s",
510 uid, pid, service, type, seat, vtnr, tty, display, yes_no(remote), remote_user, remote_host);
511
98a28fef
LP
512 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
513 if (!reply) {
514 pam_syslog(handle, LOG_ERR, "Failed to create session: %s", bus_error_message(&error));
515 r = PAM_SESSION_ERR;
516 goto finish;
517 }
74fe1fe3 518
98a28fef
LP
519 if (!dbus_message_get_args(reply, &error,
520 DBUS_TYPE_STRING, &id,
521 DBUS_TYPE_OBJECT_PATH, &object_path,
522 DBUS_TYPE_STRING, &runtime_path,
523 DBUS_TYPE_UNIX_FD, &session_fd,
bbc73283
LP
524 DBUS_TYPE_STRING, &seat,
525 DBUS_TYPE_UINT32, &vtnr,
98a28fef
LP
526 DBUS_TYPE_INVALID)) {
527 pam_syslog(handle, LOG_ERR, "Failed to parse message: %s", bus_error_message(&error));
528 r = PAM_SESSION_ERR;
529 goto finish;
530 }
74fe1fe3 531
ce959314
MS
532 if (debug)
533 pam_syslog(handle, LOG_DEBUG, "Reply from logind: "
534 "id=%s object_path=%s runtime_path=%s session_fd=%d seat=%s vtnr=%u",
535 id, object_path, runtime_path, session_fd, seat, vtnr);
536
98a28fef
LP
537 r = pam_misc_setenv(handle, "XDG_SESSION_ID", id, 0);
538 if (r != PAM_SUCCESS) {
539 pam_syslog(handle, LOG_ERR, "Failed to set session id.");
540 goto finish;
8c6db833
LP
541 }
542
98a28fef
LP
543 r = pam_misc_setenv(handle, "XDG_RUNTIME_DIR", runtime_path, 0);
544 if (r != PAM_SUCCESS) {
545 pam_syslog(handle, LOG_ERR, "Failed to set runtime dir.");
546 goto finish;
547 }
8c6db833 548
bbc73283
LP
549 if (!isempty(seat)) {
550 r = pam_misc_setenv(handle, "XDG_SEAT", seat, 0);
551 if (r != PAM_SUCCESS) {
552 pam_syslog(handle, LOG_ERR, "Failed to set seat.");
553 goto finish;
554 }
555 }
556
557 if (vtnr > 0) {
558 char buf[11];
559 snprintf(buf, sizeof(buf), "%u", vtnr);
560 char_array_0(buf);
561
562 r = pam_misc_setenv(handle, "XDG_VTNR", buf, 0);
563 if (r != PAM_SUCCESS) {
564 pam_syslog(handle, LOG_ERR, "Failed to set virtual terminal number.");
565 goto finish;
566 }
567 }
568
21c390cc
LP
569 if (session_fd >= 0) {
570 r = pam_set_data(handle, "systemd.session-fd", INT_TO_PTR(session_fd+1), NULL);
571 if (r != PAM_SUCCESS) {
572 pam_syslog(handle, LOG_ERR, "Failed to install session fd.");
573 return r;
574 }
98a28fef 575 }
8c6db833 576
98a28fef 577 session_fd = -1;
8c6db833 578
98a28fef 579 r = PAM_SUCCESS;
8c6db833 580
98a28fef
LP
581finish:
582 strv_free(controllers);
583 strv_free(reset_controllers);
584 strv_free(kill_only_users);
585 strv_free(kill_exclude_users);
8c6db833 586
98a28fef 587 dbus_error_free(&error);
35d2e7ec 588
98a28fef
LP
589 if (bus) {
590 dbus_connection_close(bus);
591 dbus_connection_unref(bus);
8c6db833
LP
592 }
593
98a28fef
LP
594 if (m)
595 dbus_message_unref(m);
8c6db833 596
ed18b08b
LP
597 if (reply)
598 dbus_message_unref(reply);
599
98a28fef
LP
600 if (session_fd >= 0)
601 close_nointr_nofail(session_fd);
672c48cc 602
98a28fef
LP
603 return r;
604}
8c6db833 605
98a28fef
LP
606_public_ PAM_EXTERN int pam_sm_close_session(
607 pam_handle_t *handle,
608 int flags,
609 int argc, const char **argv) {
8c6db833 610
98a28fef 611 const void *p = NULL;
8c6db833 612
98a28fef 613 pam_get_data(handle, "systemd.session-fd", &p);
74fe1fe3 614
98a28fef
LP
615 if (p)
616 close_nointr(PTR_TO_INT(p) - 1);
1f73f0f1 617
98a28fef 618 return PAM_SUCCESS;
8c6db833 619}