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