From b599803f628f2b01457e13399b942d005c2ca5a1 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Thu, 13 May 2021 14:00:14 +0200
Subject: [PATCH] [NSS] Move context_user initialisation to nss_hook_prepare

---
 src/nss.rs | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/src/nss.rs b/src/nss.rs
index 21fa3b4..e972146 100644
--- a/src/nss.rs
+++ b/src/nss.rs
@@ -50,6 +50,13 @@ fn nss_hook_prepare() -> Config {
     }
     setup_log(log_level);
 
+    // Set the context user to the current user, but only if not already set
+    // When doing PAM, we might get called back into by libc to do some NSS
+    // lookup, and we want to keep the PAM login user context in that case
+    if !get_cache().context_user.is_initialized() {
+        get_cache().context_user.set_current_user();
+    }
+
     return conf;
 }
 
@@ -59,13 +66,6 @@ impl PasswdHooks for OidcPasswd {
     fn get_all_entries() -> Response<Vec<Passwd>> {
         let conf = nss_hook_prepare();
 
-        // Set the context user to the current user, but only if not already set
-        // When doing PAM, we might get called back into by libc to do some NSS
-        // lookup, and we want to keep the PAM login user context in that case
-        if !get_cache().context_user.is_initialized() {
-            get_cache().context_user.set_current_user();
-        }
-
         let mut cache = get_cache();
         let user_token_res = cache.context_user.get_access_token();
         // FIXME Implement caching of system token
@@ -98,10 +98,6 @@ impl PasswdHooks for OidcPasswd {
     fn get_entry_by_uid(uid: libc::uid_t) -> Response<Passwd> {
         let conf = nss_hook_prepare();
 
-        if !get_cache().context_user.is_initialized() {
-            get_cache().context_user.set_current_user();
-        }
-
         let mut cache = get_cache();
         let user_token_res = cache.context_user.get_access_token();
         // FIXME Implement caching of system token
@@ -133,9 +129,6 @@ impl PasswdHooks for OidcPasswd {
 
     fn get_entry_by_name(name: String) -> Response<Passwd> {
         let conf = nss_hook_prepare();
-        if !get_cache().context_user.is_initialized() {
-            get_cache().context_user.set_current_user();
-        }
 
         let mut cache = get_cache();
         let user_token_res = cache.context_user.get_access_token();
-- 
GitLab