Skip to content
Snippets Groups Projects
Verified Commit 169f0d9d authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

[Unix] Use &str instead of String for Passwd values

parent 7583deeb
No related branches found
No related tags found
No related merge requests found
...@@ -36,19 +36,19 @@ use serde_json; ...@@ -36,19 +36,19 @@ use serde_json;
const USER_TOKEN_FILENAME: &str = "user_token.json"; const USER_TOKEN_FILENAME: &str = "user_token.json";
struct UserInfo { struct UserInfo<'a> {
uid: Option<uid_t>, uid: Option<uid_t>,
username: Option<String>, username: Option<&'a str>,
passwd: Option<Passwd>, passwd: Option<Passwd<'a>>,
access_token: Option<BasicTokenResponse> access_token: Option<BasicTokenResponse>
} }
pub struct Cache { pub struct Cache<'a> {
pub context_user: UserInfo pub context_user: UserInfo<'a>
} }
impl Cache { impl Cache<'_> {
pub fn new() -> Cache { pub fn new<'a>() -> Cache<'a> {
let euid = unsafe { let euid = unsafe {
geteuid() geteuid()
}; };
...@@ -63,7 +63,7 @@ impl Cache { ...@@ -63,7 +63,7 @@ impl Cache {
} }
} }
impl UserInfo { impl <'a>UserInfo<'a> {
pub fn set_current_user(&mut self) { pub fn set_current_user(&mut self) {
self.set_uid(original_euid); self.set_uid(original_euid);
} }
...@@ -125,9 +125,9 @@ impl UserInfo { ...@@ -125,9 +125,9 @@ impl UserInfo {
self.try_resolve(); self.try_resolve();
} }
pub fn get_username(&mut self) -> Result<String, io::Error> { pub fn get_username(&mut self) -> Result<&str, io::Error> {
match self.try_resolve() { match self.try_resolve() {
Ok(passwd) => Ok(&passwd.pw_name), Ok(passwd) => Ok(passwd.pw_name),
Err(e) => match self.username { Err(e) => match self.username {
Some(username) => Ok(username), Some(username) => Ok(username),
None => Err(e) None => Err(e)
...@@ -135,14 +135,14 @@ impl UserInfo { ...@@ -135,14 +135,14 @@ impl UserInfo {
} }
} }
pub fn set_username(&mut self, username: String) { pub fn set_username(&mut self, username: &'a str) {
self.username = Some(username); self.username = Some(username);
self.uid = None; self.uid = None;
self.passwd = None; self.passwd = None;
self.try_resolve(); self.try_resolve();
} }
pub fn get_home_directory(&mut self) -> Result<String, io::Error> { pub fn get_home_directory(&mut self) -> Result<&str, io::Error> {
match self.try_resolve() { match self.try_resolve() {
Ok(passwd) => Ok(passwd.pw_dir), Ok(passwd) => Ok(passwd.pw_dir),
Err(e) => Err(e) Err(e) => Err(e)
...@@ -301,8 +301,8 @@ static original_euid: uid_t = unsafe { ...@@ -301,8 +301,8 @@ static original_euid: uid_t = unsafe {
}; };
lazy_static! { lazy_static! {
static ref CACHE: Mutex<Cache> = Mutex::new(Cache::new()); static ref CACHE: Mutex<Cache<'static>> = Mutex::new(Cache::new());
} }
pub fn get_cache() -> MutexGuard<'static, Cache> { pub fn get_cache() -> MutexGuard<'static, Cache<'static>> {
CACHE.lock().unwrap() CACHE.lock().unwrap()
} }
...@@ -90,7 +90,7 @@ impl PamServiceModule for PamOidc { ...@@ -90,7 +90,7 @@ impl PamServiceModule for PamOidc {
match get_access_token_password(&conf, "pam", username.to_string(), password.to_string(), PamError::SERVICE_ERR, PamError::AUTH_ERR) { match get_access_token_password(&conf, "pam", username.to_string(), password.to_string(), PamError::SERVICE_ERR, PamError::AUTH_ERR) {
Ok(t) => { Ok(t) => {
info!("Authenticated {} using Resource Owner Password Grant", username); info!("Authenticated {} using Resource Owner Password Grant", username);
get_cache().context_user.set_username(username.to_string()); get_cache().context_user.set_username(username);
get_cache().context_user.set_access_token(t); get_cache().context_user.set_access_token(t);
return PamError::SUCCESS; return PamError::SUCCESS;
}, },
......
/* Copyright 2021 Dominik George <dominik.george@teckids.org> /* Copyright 2021 Dominik George <dominik.george@teckids.org>
* Copyright 2021 mirabilos <thorsten.glaser@teckids.org> 65;6203;1c * Copyright 2021 mirabilos <thorsten.glaser@teckids.org>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,33 +20,33 @@ use std::io; ...@@ -20,33 +20,33 @@ use std::io;
use std::mem::uninitialized; use std::mem::uninitialized;
use std::ptr::null_mut; use std::ptr::null_mut;
pub struct Passwd { pub struct Passwd<'a> {
pub pw_name: String, pub pw_name: &'a str,
pub pw_passwd: String, pub pw_passwd: &'a str,
pub pw_uid: uid_t, pub pw_uid: uid_t,
pub pw_gid: gid_t, pub pw_gid: gid_t,
pub pw_gecos: String, pub pw_gecos: &'a str,
pub pw_dir: String, pub pw_dir: &'a str,
pub pw_shell: String pub pw_shell: &'a str
} }
const MAX_BUFLEN: size_t = 1024 * 1024; const MAX_BUFLEN: size_t = 1024 * 1024;
fn getpwxx_fillpw(c_passwd: passwd) -> Passwd { fn getpwxx_fillpw<'a>(c_passwd: passwd) -> Passwd<'a> {
unsafe { unsafe {
Passwd { Passwd {
pw_name: CStr::from_ptr(c_passwd.pw_name).to_string_lossy().into_owned(), pw_name: CStr::from_ptr(c_passwd.pw_name).to_str().ok().unwrap(),
pw_passwd: CStr::from_ptr(c_passwd.pw_passwd).to_string_lossy().into_owned(), pw_passwd: CStr::from_ptr(c_passwd.pw_passwd).to_str().ok().unwrap(),
pw_uid: c_passwd.pw_uid, pw_uid: c_passwd.pw_uid,
pw_gid: c_passwd.pw_gid, pw_gid: c_passwd.pw_gid,
pw_gecos: CStr::from_ptr(c_passwd.pw_gecos).to_string_lossy().into_owned(), pw_gecos: CStr::from_ptr(c_passwd.pw_gecos).to_str().ok().unwrap(),
pw_dir: CStr::from_ptr(c_passwd.pw_dir).to_string_lossy().into_owned(), pw_dir: CStr::from_ptr(c_passwd.pw_dir).to_str().ok().unwrap(),
pw_shell: CStr::from_ptr(c_passwd.pw_shell).to_string_lossy().into_owned(), pw_shell: CStr::from_ptr(c_passwd.pw_shell).to_str().ok().unwrap(),
} }
} }
} }
pub fn getpwnam_safe(name: String) -> Result<Passwd, io::Error> { pub fn getpwnam_safe<'a>(name: String) -> Result<Passwd<'a>, io::Error> {
let res: Passwd; let res: Passwd;
unsafe { unsafe {
...@@ -82,7 +82,7 @@ pub fn getpwnam_safe(name: String) -> Result<Passwd, io::Error> { ...@@ -82,7 +82,7 @@ pub fn getpwnam_safe(name: String) -> Result<Passwd, io::Error> {
return Ok(res); return Ok(res);
} }
pub fn getpwuid_safe(uid: uid_t) -> Result<Passwd, io::Error> { pub fn getpwuid_safe<'a>(uid: uid_t) -> Result<Passwd<'a>, io::Error> {
let res: Passwd; let res: Passwd;
unsafe { unsafe {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment