From 908ebf8964903d601e06c4a39c2a0eaf3e5d3d8a Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Sun, 2 Jan 2022 15:55:36 +0530 Subject: [PATCH 1/2] Use XDG_RUNTIME_DIR for storing Xauthority --- src/login.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/login.c b/src/login.c index 732ef76..5feb266 100644 --- a/src/login.c +++ b/src/login.c @@ -304,10 +304,10 @@ void remove_utmp_entry(struct utmp *entry) { endutent(); } -void xauth(const char* display_name, const char* shell, const char* dir) +void xauth(const char* display_name, const char* shell) { char xauthority[256]; - snprintf(xauthority, 256, "%s/%s", dir, ".lyxauth"); + snprintf(xauthority, 256, "%s/%s", getenv("XDG_RUNTIME_DIR"), "lyxauth"); setenv("XAUTHORITY", xauthority, 1); setenv("DISPLAY", display_name, 1); @@ -343,18 +343,10 @@ void xorg( const char* vt, const char* desktop_cmd) { - // generate xauthority file - const char* xauth_dir = getenv("XDG_CONFIG_HOME"); - - if ((xauth_dir == NULL) || (*xauth_dir == '\0')) - { - xauth_dir = pwd->pw_dir; - } - char display_name[4]; snprintf(display_name, 3, ":%d", get_free_display()); - xauth(display_name, pwd->pw_shell, xauth_dir); + xauth(display_name, pwd->pw_shell); // start xorg pid_t pid = fork(); @@ -531,7 +523,7 @@ void auth( if (pwd->pw_shell[0] == '\0') { setusershell(); - + char* shell = getusershell(); if (shell != NULL) @@ -552,7 +544,7 @@ void auth( if (pid == 0) { - // set user info + // set user info ok = initgroups(pwd->pw_name, pwd->pw_gid); if (ok != 0) @@ -658,21 +650,21 @@ void auth( // close pam session ok = pam_do(pam_close_session, handle, 0, buf); - + if (ok != PAM_SUCCESS) { return; } ok = pam_do(pam_setcred, handle, PAM_DELETE_CRED, buf); - + if (ok != PAM_SUCCESS) { return; } ok = pam_end(handle, 0); - + if (ok != PAM_SUCCESS) { pam_diagnose(ok, buf); From 38aacc6b1cdeaffa9bd7ffb22746b8e5fafb7b98 Mon Sep 17 00:00:00 2001 From: Utkarsh Verma Date: Tue, 24 May 2022 07:41:35 +0530 Subject: [PATCH 2/2] Implement fallback logic for XAUTHORITY --- src/login.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/login.c b/src/login.c index 5feb266..bbdcadc 100644 --- a/src/login.c +++ b/src/login.c @@ -304,10 +304,31 @@ void remove_utmp_entry(struct utmp *entry) { endutent(); } -void xauth(const char* display_name, const char* shell) +void xauth(const char* display_name, const char* shell, char* pwd) { + const char* xauth_file = ".lyxauth"; + char* xauth_dir = getenv("XDG_RUNTIME_DIR"); + if ((xauth_dir == NULL) || (*xauth_dir == '\0')) + { + xauth_dir = getenv("XDG_CONFIG_DIR"); + if ((xauth_dir == NULL) || (*xauth_dir == '\0')) + { + xauth_dir = pwd; + xauth_file = "lyxauth"; + } + } + else + { + xauth_file = "lyxauth"; + } + + // trim trailing slashes + int i = strlen(xauth_dir) - 1; + while (xauth_dir[i] == '/') i--; + xauth_dir[i + 1] = '\0'; + char xauthority[256]; - snprintf(xauthority, 256, "%s/%s", getenv("XDG_RUNTIME_DIR"), "lyxauth"); + snprintf(xauthority, 256, "%s/%s", xauth_dir, xauth_file); setenv("XAUTHORITY", xauthority, 1); setenv("DISPLAY", display_name, 1); @@ -346,7 +367,7 @@ void xorg( char display_name[4]; snprintf(display_name, 3, ":%d", get_free_display()); - xauth(display_name, pwd->pw_shell); + xauth(display_name, pwd->pw_shell, pwd->pw_dir); // start xorg pid_t pid = fork();