Skip to content

Commit

Permalink
Add test case for users who have their home directory changed by PAM
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Berhoerster authored and robert-ancell committed Mar 19, 2024
1 parent 2e1d7eb commit 7367300
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
34 changes: 34 additions & 0 deletions tests/scripts/change-home-dir-on-session.conf
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/src/libsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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/<user> and replace with /users/<user> */
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),
Expand Down
4 changes: 3 additions & 1 deletion tests/src/test-runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/test-change-home-dir-on-session
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
./src/dbus-env ./src/test-runner change-home-dir-on-session test-gobject-greeter

0 comments on commit 7367300

Please sign in to comment.