From 73673008c7010f4b4cf1964f57f49ee1865d74c2 Mon Sep 17 00:00:00 2001 From: Guido Berhoerster Date: Mon, 9 Oct 2023 13:02:09 +0200 Subject: [PATCH] Add test case for users who have their home directory changed by PAM --- tests/Makefile.am | 4 ++- tests/scripts/change-home-dir-on-session.conf | 34 +++++++++++++++++++ tests/src/libsystem.c | 21 ++++++++++++ tests/src/test-runner.c | 4 ++- tests/test-change-home-dir-on-session | 2 ++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 tests/scripts/change-home-dir-on-session.conf create mode 100755 tests/test-change-home-dir-on-session diff --git a/tests/Makefile.am b/tests/Makefile.am index 98a3acce..ac43aa1c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -256,7 +256,8 @@ TESTS = \ test-wayland-greeter \ test-wayland-session \ test-invalid-seat \ - test-seatdefaults-still-supported + test-seatdefaults-still-supported \ + test-change-home-dir-on-session # test-switch-to-greeter-return-session-repeat # test-session-exit-error @@ -389,6 +390,7 @@ EXTRA_DIST = \ scripts/autologin-timeout-logout.conf \ scripts/autologin-xserver-crash.conf \ scripts/change-authentication.conf \ + scripts/change-home-dir-on-session.conf \ scripts/cancel-authentication.conf \ scripts/console-kit.conf \ scripts/console-kit-no-xdg-runtime.conf \ diff --git a/tests/scripts/change-home-dir-on-session.conf b/tests/scripts/change-home-dir-on-session.conf new file mode 100644 index 00000000..c1e469ba --- /dev/null +++ b/tests/scripts/change-home-dir-on-session.conf @@ -0,0 +1,34 @@ +# +# Check home directory is set correctly in session after having been changed by PAM +# + +[Seat:*] +autologin-user=change-home-dir +user-session=default + +#?*START-DAEMON +#?RUNNER DAEMON-START + +# X server starts +#?XSERVER-0 START VT=7 SEAT=seat0 + +# Daemon connects when X server is ready +#?*XSERVER-0 INDICATE-READY +#?XSERVER-0 INDICATE-READY +#?XSERVER-0 ACCEPT-CONNECT + +# Session starts +#?SESSION-X-0 START XDG_SEAT=seat0 XDG_VTNR=7 XDG_GREETER_DATA_DIR=.*/change-home-dir XDG_SESSION_TYPE=x11 XDG_SESSION_DESKTOP=default USER=change-home-dir +#?LOGIN1 ACTIVATE-SESSION SESSION=c0 +#?XSERVER-0 ACCEPT-CONNECT +#?SESSION-X-0 CONNECT-XSERVER + +# Check environment variables +#?*SESSION-X-0 READ-ENV NAME=HOME +#?SESSION-X-0 READ-ENV NAME=HOME VALUE=.*/users/change-home-dir + +# Cleanup +#?*STOP-DAEMON +#?SESSION-X-0 TERMINATE SIGNAL=15 +#?XSERVER-0 TERMINATE SIGNAL=15 +#?RUNNER DAEMON-EXIT STATUS=0 diff --git a/tests/src/libsystem.c b/tests/src/libsystem.c index a15e7df5..4007ab49 100644 --- a/tests/src/libsystem.c +++ b/tests/src/libsystem.c @@ -1285,6 +1285,27 @@ pam_open_session (pam_handle_t *pamh, int flags) g_mkdir_with_parents (entry->pw_dir, 0755); } + if (strcmp (pamh->user, "change-home-dir") == 0) + { + struct passwd *entry = getpwnam (pamh->user); + + /* Actual home dir is changed by PAM, differing from passwd, strip off + trailing /home/ and replace with /users/ */ + const char *endp = pamh->user; + int slashes = 0; + while (*endp++ != '\0'); + while (slashes < 2 && endp > pamh->user) { + if (*endp-- == '/') + slashes++; + } + g_autofree gchar *changed_home = g_strdup_printf("%.*s/users/%s\n", (int)(endp - entry->pw_dir), entry->pw_dir, pamh->user); + + g_mkdir_with_parents (changed_home, 0755); + + g_autofree gchar *e = g_strdup_printf ("HOME=%s", changed_home); + pam_putenv (pamh, e); + } + /* Open logind session */ g_autoptr(GError) error = NULL; g_autoptr(GVariant) result = g_dbus_connection_call_sync (g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL), diff --git a/tests/src/test-runner.c b/tests/src/test-runner.c index 7de9694c..bfda38ef 100644 --- a/tests/src/test-runner.c +++ b/tests/src/test-runner.c @@ -2772,13 +2772,15 @@ main (int argc, char **argv) {"corrupt-xauth", "password", "Corrupt Xauthority", 1032}, /* User to test properties */ {"prop-user", "", "TEST", 1033}, + /* This account has the home directory changed by PAM during authentication */ + {"change-home-dir", "", "Change Home Dir User", 1034}, {NULL, NULL, NULL, 0} }; g_autoptr(GString) passwd_data = g_string_new (""); g_autoptr(GString) group_data = g_string_new (""); for (int i = 0; users[i].user_name; i++) { - if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0) + if (strcmp (users[i].user_name, "mount-home-dir") != 0 && strcmp (users[i].user_name, "make-home-dir") != 0 && strcmp (users[i].user_name, "change-home-dir") != 0) { g_autofree gchar *path = g_build_filename (home_dir, users[i].user_name, NULL); g_mkdir_with_parents (path, 0755); diff --git a/tests/test-change-home-dir-on-session b/tests/test-change-home-dir-on-session new file mode 100755 index 00000000..38d5a9db --- /dev/null +++ b/tests/test-change-home-dir-on-session @@ -0,0 +1,2 @@ +#!/bin/sh +./src/dbus-env ./src/test-runner change-home-dir-on-session test-gobject-greeter