Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nss-pam-webapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
magicfelix
nss-pam-webapi
Commits
4aebf539
Verified
Commit
4aebf539
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Revert "[Cache] Get rid of global state"
This reverts commit
b5bfa128
.
parent
0d6048ae
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/cache.rs
+18
-11
18 additions, 11 deletions
src/cache.rs
src/nss.rs
+9
-8
9 additions, 8 deletions
src/nss.rs
src/pam.rs
+5
-7
5 additions, 7 deletions
src/pam.rs
with
32 additions
and
26 deletions
src/cache.rs
+
18
−
11
View file @
4aebf539
...
...
@@ -15,10 +15,11 @@
*/
use
crate
::
BASE_NAME
;
use
crate
::
unix
::
getpwnam_safe
;
use
lazy_static
::
lazy_static
;
use
std
::
collections
::
HashMap
;
use
std
::
sync
::{
RwLock
,
RwLockReadGuard
};
use
libc
::{
geteuid
,
seteuid
,
uid_t
};
...
...
@@ -39,19 +40,17 @@ const USER_TOKEN_FILENAME: &str = "user_token.json";
pub
struct
Cache
{
user_tokens
:
HashMap
<
String
,
BasicTokenResponse
>
,
original_euid
:
uid_t
,
prefix
:
String
}
impl
Cache
{
pub
fn
new
(
prefix
:
&
str
)
->
Cache
{
pub
fn
new
()
->
Cache
{
let
euid
;
unsafe
{
euid
=
geteuid
();
};
Cache
{
user_tokens
:
HashMap
::
new
(),
original_euid
:
euid
,
prefix
:
prefix
.to_string
()
original_euid
:
euid
}
}
...
...
@@ -145,7 +144,7 @@ impl Cache {
}
}
pub
fn
load_user_token
(
&
mut
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
pub
fn
load_user_token
(
&
self
,
owner
:
&
String
)
->
Option
<&
BasicTokenResponse
>
{
if
!
self
.user_tokens
.contains_key
(
owner
)
{
debug!
(
"No token for {} in memory, trying to load from file"
,
owner
);
...
...
@@ -161,7 +160,7 @@ impl Cache {
match
new_token
{
Some
(
t
)
=>
{
self
.user_tokens
.insert
(
owner
.to_string
(),
t
);
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
t
);
self
.user_tokens
.get
(
owner
)
},
None
=>
None
...
...
@@ -172,8 +171,8 @@ impl Cache {
}
}
pub
fn
save_user_token
(
&
mut
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
self
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
pub
fn
save_user_token
(
&
self
,
owner
:
&
String
,
token
:
BasicTokenResponse
)
->
Result
<
(),
io
::
Error
>
{
CACHE
.write
()
.unwrap
()
.user_tokens
.insert
(
owner
.to_string
(),
token
.clone
());
debug!
(
"Saved token for {} in memory"
,
owner
);
// Try to write user's token cache file
...
...
@@ -191,8 +190,8 @@ impl Cache {
return
res
;
}
pub
fn
delete_user_token
(
&
mut
self
,
owner
:
&
String
)
{
self
.user_tokens
.remove
(
owner
);
pub
fn
delete_user_token
(
&
self
,
owner
:
&
String
)
{
CACHE
.write
()
.unwrap
()
.user_tokens
.remove
(
owner
);
debug!
(
"Token for {} removed from memory"
,
owner
);
// Try to remove user's token cache file
...
...
@@ -226,3 +225,11 @@ fn save_json<O: Serialize>(path: PathBuf, obj: O) -> Result<(), io::Error> {
fs
::
write
(
path
,
json
)
}
lazy_static!
{
static
ref
CACHE
:
RwLock
<
Cache
>
=
RwLock
::
new
(
Cache
::
new
());
}
pub
fn
get_cache
()
->
RwLockReadGuard
<
'static
,
Cache
>
{
CACHE
.read
()
.unwrap
()
}
This diff is collapsed.
Click to expand it.
src/nss.rs
+
9
−
8
View file @
4aebf539
...
...
@@ -19,7 +19,7 @@ use crate::config::{
get_or_error
};
use
config
::
Config
;
use
crate
::
cache
::
C
ache
;
use
crate
::
cache
::
get_c
ache
;
use
crate
::
logging
::
setup_log
;
...
...
@@ -46,7 +46,7 @@ struct PasswdDef {
}
#[derive(Deserialize)]
struct
PasswdHelper
(
#[serde(with
=
"PasswdDef"
)]
Passwd
);
fn
nss_hook_prepare
()
->
(
Cache
,
Config
)
{
fn
nss_hook_prepare
()
->
Config
{
let
conf
=
get_config
(
None
);
let
mut
log_level
=
log
::
LevelFilter
::
Error
;
...
...
@@ -55,9 +55,7 @@ fn nss_hook_prepare() -> (Cache, Config) {
}
setup_log
(
log_level
);
let
cache
=
Cache
::
new
(
"nss"
);
return
(
cache
,
conf
);
return
conf
;
}
fn
get_current_user
()
->
String
{
...
...
@@ -79,7 +77,8 @@ struct OidcPasswd;
impl
PasswdHooks
for
OidcPasswd
{
fn
get_all_entries
()
->
Response
<
Vec
<
Passwd
>>
{
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
user
=
get_current_user
();
let
ctc
;
...
...
@@ -112,7 +111,8 @@ impl PasswdHooks for OidcPasswd {
}
fn
get_entry_by_uid
(
uid
:
libc
::
uid_t
)
->
Response
<
Passwd
>
{
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
user
=
get_current_user
();
let
ctc
;
...
...
@@ -145,7 +145,8 @@ impl PasswdHooks for OidcPasswd {
}
fn
get_entry_by_name
(
name
:
String
)
->
Response
<
Passwd
>
{
let
(
mut
cache
,
conf
)
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
let
mut
cache
=
get_cache
();
let
user
=
get_current_user
();
let
ctc
;
...
...
This diff is collapsed.
Click to expand it.
src/pam.rs
+
5
−
7
View file @
4aebf539
...
...
@@ -24,11 +24,11 @@ use crate::oauth::get_access_token_password;
use
crate
::
logging
::
setup_log
;
use
crate
::
cache
::
C
ache
;
use
crate
::
cache
::
get_c
ache
;
use
pamsm
::{
PamServiceModule
,
Pam
,
PamFlag
,
PamError
,
PamLibExt
};
fn
pam_sm_prepare
(
argv
:
&
Vec
<
String
>
)
->
(
Cache
,
Config
)
{
fn
pam_sm_prepare
(
argv
:
&
Vec
<
String
>
)
->
Config
{
let
conf_args
=
argv_to_config
(
argv
);
let
conf
=
get_config
(
Some
(
conf_args
));
...
...
@@ -38,16 +38,14 @@ fn pam_sm_prepare(argv: &Vec<String>) -> (Cache, Config) {
}
setup_log
(
log_level
);
let
cache
=
Cache
::
new
(
"pam"
);
return
(
cache
,
conf
);
return
conf
;
}
struct
PamOidc
;
impl
PamServiceModule
for
PamOidc
{
fn
authenticate
(
pamh
:
Pam
,
_
:
PamFlag
,
argv
:
Vec
<
String
>
)
->
PamError
{
let
(
mut
cache
,
conf
)
=
pam_sm_prepare
(
&
argv
);
let
conf
=
pam_sm_prepare
(
&
argv
);
if
conf
.get_str
(
"pam.flow"
)
.unwrap
()
==
"password"
{
debug!
(
"Starting Resource Owner Password Credentials OAuth flow"
);
...
...
@@ -92,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
)
{
Ok
(
t
)
=>
{
info!
(
"Authenticated {} using Resource Owner Password Grant"
,
username
);
cache
.save_user_token
(
&
username
.to_string
(),
t
.into
());
get_
cache
()
.save_user_token
(
&
username
.to_string
(),
t
.into
());
return
PamError
::
SUCCESS
;
},
Err
(
e
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment