From c576ee5204be71b3a6c2211fa6429b9ce88bf646 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Thu, 20 May 2021 00:55:07 +0200
Subject: [PATCH] Unify Passwd structs

---
 src/cache.rs | 18 +++++++++---------
 src/nss.rs   | 15 +--------------
 src/unix.rs  | 35 ++++++++++++++++++++---------------
 3 files changed, 30 insertions(+), 38 deletions(-)

diff --git a/src/cache.rs b/src/cache.rs
index e9159cf..bbd7c3d 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -128,7 +128,7 @@ impl UserInfo {
             self.try_resolve().ok();
         }
         match &self.passwd {
-            Some(passwd) => Ok(passwd.pw_uid),
+            Some(passwd) => Ok(passwd.uid),
             None => match self.uid {
                 Some(uid) => Ok(uid),
                 None => Err(io::Error::new(io::ErrorKind::InvalidInput, "foo"))
@@ -141,13 +141,13 @@ impl UserInfo {
     pub fn set_uid(&mut self, uid: uid_t) {
         self.uid = Some(uid);
 
-        if self.passwd.is_some() && self.passwd.as_ref().unwrap().pw_uid != uid {
+        if self.passwd.is_some() && self.passwd.as_ref().unwrap().uid != uid {
             // Invalidate passwd because UID does not match anymore
             self.passwd = None;
             self.try_resolve().ok();
         }
         self.username = match &self.passwd {
-            Some(p) => Some(p.pw_name.to_string()),
+            Some(p) => Some(p.name.to_string()),
             None => None
         };
     }
@@ -159,7 +159,7 @@ impl UserInfo {
             self.try_resolve().ok();
         }
         match &self.passwd {
-            Some(passwd) => Ok(passwd.pw_name.to_string()),
+            Some(passwd) => Ok(passwd.name.to_string()),
             None => match &self.username {
                 Some(username) => Ok(username.to_string()),
                 None => Err(io::Error::new(io::ErrorKind::InvalidInput, "foo"))
@@ -172,13 +172,13 @@ impl UserInfo {
     pub fn set_username(&mut self, username: String) {
         self.username = Some(username);
 
-        if self.passwd.is_some() && self.passwd.as_ref().unwrap().pw_name != self.username.as_ref().unwrap().to_string() {
+        if self.passwd.is_some() && self.passwd.as_ref().unwrap().name != self.username.as_ref().unwrap().to_string() {
             // Invalidate passwd because UID does not match anymore
             self.passwd = None;
             self.try_resolve().ok();
         }
         self.uid = match &self.passwd {
-            Some(p) => Some(p.pw_uid),
+            Some(p) => Some(p.uid),
             None => None
         };
     }
@@ -186,8 +186,8 @@ impl UserInfo {
     /// Set the full passwd struct from outside
     pub fn set_passwd(&mut self, passwd: Passwd) {
         self.passwd = Some(passwd.clone());
-        self.username = Some(passwd.pw_name);
-        self.uid = Some(passwd.pw_uid);
+        self.username = Some(passwd.name);
+        self.uid = Some(passwd.uid);
     }
 
     /// Return the home directory from the passwd slot,
@@ -197,7 +197,7 @@ impl UserInfo {
             self.try_resolve().ok();
         }
         match &self.passwd {
-            Some(passwd) => Ok(passwd.pw_dir.clone()),
+            Some(passwd) => Ok(passwd.dir.clone()),
             None => Err(io::Error::new(io::ErrorKind::InvalidInput, "foo"))
         }
     }
diff --git a/src/nss.rs b/src/nss.rs
index e35a37e..86502f7 100644
--- a/src/nss.rs
+++ b/src/nss.rs
@@ -25,23 +25,10 @@ use crate::cache::get_context_user;
 use crate::logging::setup_log;
 
 use crate::oauth::{get_data_jq, get_usable_token};
-use serde::{Serialize, Deserialize};
 
 use libnss::interop::Response;
 use libnss::passwd::{PasswdHooks, Passwd};
-
-#[derive(Serialize, Deserialize)]
-#[serde(remote = "Passwd")]
-struct PasswdDef {
-    name: String,
-    passwd: String,
-    uid: libc::uid_t,
-    gid: libc::gid_t,
-    gecos: String,
-    dir: String,
-    shell: String
-}
-#[derive(Deserialize)] struct PasswdHelper(#[serde(with = "PasswdDef")] Passwd);
+use crate::unix::PasswdHelper;
 
 fn nss_hook_prepare() -> Config {
     let conf = get_config(None);
diff --git a/src/unix.rs b/src/unix.rs
index bcb8173..ab1aad9 100644
--- a/src/unix.rs
+++ b/src/unix.rs
@@ -22,29 +22,34 @@ use std::io;
 use std::mem::uninitialized;
 use std::ptr::null_mut;
 
-#[derive(Clone)]
+use libnss;
+use serde::{Serialize, Deserialize};
+
+#[derive(Clone, Deserialize, Serialize)]
+#[serde(remote = "libnss::passwd::Passwd")]
 pub struct Passwd {
-    pub pw_name: String,
-    pub pw_passwd: String,
-    pub pw_uid: uid_t,
-    pub pw_gid: gid_t,
-    pub pw_gecos: String,
-    pub pw_dir: String,
-    pub pw_shell: String
+    pub name: String,
+    pub passwd: String,
+    pub uid: uid_t,
+    pub gid: gid_t,
+    pub gecos: String,
+    pub dir: String,
+    pub shell: String
 }
+#[derive(Deserialize)] pub struct PasswdHelper(#[serde(with = "Passwd")] pub libnss::passwd::Passwd);
 
 const MAX_BUFLEN: size_t = 1024 * 1024;
 
 fn getpwxx_fillpw(c_passwd: passwd) -> Passwd {
     unsafe {
         Passwd {
-            pw_name: CStr::from_ptr(c_passwd.pw_name).to_string_lossy().into_owned(),
-            pw_passwd: CStr::from_ptr(c_passwd.pw_passwd).to_string_lossy().into_owned(),
-            pw_uid: c_passwd.pw_uid,
-            pw_gid: c_passwd.pw_gid,
-            pw_gecos: CStr::from_ptr(c_passwd.pw_gecos).to_string_lossy().into_owned(),
-            pw_dir: CStr::from_ptr(c_passwd.pw_dir).to_string_lossy().into_owned(),
-            pw_shell: CStr::from_ptr(c_passwd.pw_shell).to_string_lossy().into_owned(),
+            name: CStr::from_ptr(c_passwd.pw_name).to_string_lossy().into_owned(),
+            passwd: CStr::from_ptr(c_passwd.pw_passwd).to_string_lossy().into_owned(),
+            uid: c_passwd.pw_uid,
+            gid: c_passwd.pw_gid,
+            gecos: CStr::from_ptr(c_passwd.pw_gecos).to_string_lossy().into_owned(),
+            dir: CStr::from_ptr(c_passwd.pw_dir).to_string_lossy().into_owned(),
+            shell: CStr::from_ptr(c_passwd.pw_shell).to_string_lossy().into_owned(),
         }
     }
 }
-- 
GitLab