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
5edfd9e1
Verified
Commit
5edfd9e1
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Remove unnecessary cache object
parent
751f4bbd
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
+20
-34
20 additions, 34 deletions
src/cache.rs
src/nss.rs
+9
-9
9 additions, 9 deletions
src/nss.rs
src/pam.rs
+3
-3
3 additions, 3 deletions
src/pam.rs
with
32 additions
and
46 deletions
src/cache.rs
+
20
−
34
View file @
5edfd9e1
...
@@ -43,45 +43,27 @@ const USER_TOKEN_FILENAME: &str = "user_token.json";
...
@@ -43,45 +43,27 @@ const USER_TOKEN_FILENAME: &str = "user_token.json";
/// Holds (partial or full) information about a user
/// Holds (partial or full) information about a user
/// This will mostly be the context user (see `Cache`), and is filled with
/// This will mostly be the context user (see `Cache`), and is filled with
/// as much detail about the user as is available
/// as much detail about the user as is available
pub
struct
UserInfo
<
'a
>
{
pub
struct
UserInfo
{
/// Numeric user ID
/// Numeric user ID
uid
:
Option
<
uid_t
>
,
uid
:
Option
<
uid_t
>
,
/// Username as used for authentication
/// Username as used for authentication
username
:
Option
<
String
>
,
username
:
Option
<
String
>
,
/// Passwd struct (once full getpwnam/getpwuid resolution was possible)
/// Passwd struct (once full getpwnam/getpwuid resolution was possible)
passwd
:
Option
<
Passwd
<
'a
>
>
,
passwd
:
Option
<
Passwd
>
,
/// OAuth access token if freshly retrieved or known from disk backed storage
/// OAuth access token if freshly retrieved or known from disk backed storage
access_token
:
Option
<
BasicTokenResponse
>
access_token
:
Option
<
BasicTokenResponse
>
}
}
/// In-memory structure to hold global process state
impl
UserInfo
{
/// Needed because the PAM and NSS components might be used chained (if NSS
pub
fn
new
()
->
UserInfo
{
/// resolution is necessary to complete PAM authentication), and we need to
Self
{
/// remember state between the calls.
uid
:
None
,
pub
struct
Cache
<
'a
>
{
username
:
None
,
/// The user currently calling the library
passwd
:
None
,
/// For NSS, this will be the process owner; for PAM, this will be the user
access_token
:
None
/// logging in (after successful authentication)
pub
context_user
:
UserInfo
<
'a
>
}
impl
Cache
{
pub
fn
new
()
->
Cache
{
let
euid
=
unsafe
{
geteuid
()
};
Cache
{
context_user
:
UserInfo
{
uid
:
None
,
username
:
None
,
passwd
:
None
,
access_token
:
None
}
}
}
}
}
}
impl
<
'a
>
UserInfo
<
'a
>
{
/// Set the information of this user object to that of the process owner
/// Set the information of this user object to that of the process owner
// FIXME Move to Cache, with a from_current_user generator method here
// FIXME Move to Cache, with a from_current_user generator method here
pub
fn
set_current_user
(
&
mut
self
)
{
pub
fn
set_current_user
(
&
mut
self
)
{
...
@@ -189,7 +171,7 @@ impl <'a>UserInfo<'a> {
...
@@ -189,7 +171,7 @@ impl <'a>UserInfo<'a> {
pub
fn
set_username
(
&
mut
self
,
username
:
String
)
{
pub
fn
set_username
(
&
mut
self
,
username
:
String
)
{
self
.username
=
Some
(
username
);
self
.username
=
Some
(
username
);
if
self
.passwd
.is_some
()
&&
self
.passwd
.as_ref
()
.unwrap
()
.pw_name
!=
self
.username
.as_ref
()
.unwrap
()
{
if
self
.passwd
.is_some
()
&&
self
.passwd
.as_ref
()
.unwrap
()
.pw_name
!=
self
.username
.as_ref
()
.unwrap
()
.to_string
()
{
// Invalidate passwd because UID does not match anymore
// Invalidate passwd because UID does not match anymore
self
.passwd
=
None
;
self
.passwd
=
None
;
self
.try_resolve
();
self
.try_resolve
();
...
@@ -202,12 +184,12 @@ impl <'a>UserInfo<'a> {
...
@@ -202,12 +184,12 @@ impl <'a>UserInfo<'a> {
/// Return the home directory from the passwd slot,
/// Return the home directory from the passwd slot,
/// attempting NSS resolution before doing so
/// attempting NSS resolution before doing so
pub
fn
get_home_directory
(
&
mut
self
)
->
Result
<
&
str
,
io
::
Error
>
{
pub
fn
get_home_directory
(
&
mut
self
)
->
Result
<
String
,
io
::
Error
>
{
if
self
.passwd
.is_none
()
{
if
self
.passwd
.is_none
()
{
self
.try_resolve
();
self
.try_resolve
();
}
}
match
&
self
.passwd
{
match
&
self
.passwd
{
Some
(
passwd
)
=>
Ok
(
passwd
.pw_dir
),
Some
(
passwd
)
=>
Ok
(
passwd
.pw_dir
.clone
()
),
None
=>
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidInput
,
"foo"
))
None
=>
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidInput
,
"foo"
))
}
}
}
}
...
@@ -277,7 +259,7 @@ impl <'a>UserInfo<'a> {
...
@@ -277,7 +259,7 @@ impl <'a>UserInfo<'a> {
// Determine home directory and override $HOME to make the XDG code return
// Determine home directory and override $HOME to make the XDG code return
// XDG directories for a different user
// XDG directories for a different user
let
user_home
=
self
.get_home_directory
()
?
;
let
user_home
=
self
.get_home_directory
()
?
;
env
::
set_var
(
"HOME"
,
user_home
);
env
::
set_var
(
"HOME"
,
&
user_home
);
debug!
(
"Home directory for UID {} is {}"
,
uid
,
user_home
);
debug!
(
"Home directory for UID {} is {}"
,
uid
,
user_home
);
}
}
...
@@ -401,8 +383,12 @@ fn get_original_euid() -> uid_t {
...
@@ -401,8 +383,12 @@ fn get_original_euid() -> uid_t {
}
}
lazy_static!
{
lazy_static!
{
static
ref
CACHE
:
Mutex
<
Cache
>
=
Mutex
::
new
(
Cache
::
new
());
/// Current context user
/// Needed because the PAM and NSS components might be used chained (if NSS
/// resolution is necessary to complete PAM authentication), and we need to
/// remember state between the calls.
static
ref
CONTEXT_USER
:
Mutex
<
UserInfo
>
=
Mutex
::
new
(
UserInfo
::
new
());
}
}
pub
fn
get_c
ache
()
->
MutexGuard
<
'static
,
Cache
>
{
pub
fn
get_c
ontext_user
()
->
MutexGuard
<
'static
,
UserInfo
>
{
C
ACHE
.lock
()
.unwrap
()
C
ONTEXT_USER
.lock
()
.unwrap
()
}
}
This diff is collapsed.
Click to expand it.
src/nss.rs
+
9
−
9
View file @
5edfd9e1
...
@@ -18,7 +18,7 @@ use crate::config::{
...
@@ -18,7 +18,7 @@ use crate::config::{
get_optional
,
get_optional
,
};
};
use
config
::
Config
;
use
config
::
Config
;
use
crate
::
cache
::
get_c
ache
;
use
crate
::
cache
::
get_c
ontext_user
;
use
crate
::
logging
::
setup_log
;
use
crate
::
logging
::
setup_log
;
...
@@ -53,8 +53,8 @@ fn nss_hook_prepare() -> Config {
...
@@ -53,8 +53,8 @@ fn nss_hook_prepare() -> Config {
// Set the context user to the current user, but only if not already set
// 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
// 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
// lookup, and we want to keep the PAM login user context in that case
if
!
get_
cache
()
.
context_user
.is_initialized
()
{
if
!
get_context_user
()
.is_initialized
()
{
get_
cache
()
.
context_user
.set_current_user
();
get_context_user
()
.set_current_user
();
}
}
return
conf
;
return
conf
;
...
@@ -67,8 +67,8 @@ impl PasswdHooks for OidcPasswd {
...
@@ -67,8 +67,8 @@ impl PasswdHooks for OidcPasswd {
let
conf
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
info!
(
"[NSS] passwd.get_all_entries called"
);
info!
(
"[NSS] passwd.get_all_entries called"
);
let
mut
c
ache
=
get_cache
();
let
mut
c
ontext_user
=
get_context_user
();
let
user_token_res
=
cache
.
context_user
.get_access_token
();
let
user_token_res
=
context_user
.get_access_token
();
// FIXME Implement caching of system token
// FIXME Implement caching of system token
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
system_token_res
.as_ref
();
let
system_token_res
=
system_token_res
.as_ref
();
...
@@ -100,8 +100,8 @@ impl PasswdHooks for OidcPasswd {
...
@@ -100,8 +100,8 @@ impl PasswdHooks for OidcPasswd {
let
conf
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
info!
(
"[NSS] passwd.get_entry_by_uid called for {}"
,
uid
);
info!
(
"[NSS] passwd.get_entry_by_uid called for {}"
,
uid
);
let
mut
c
ache
=
get_cache
();
let
mut
c
ontext_user
=
get_context_user
();
let
user_token_res
=
cache
.
context_user
.get_access_token
();
let
user_token_res
=
context_user
.get_access_token
();
// FIXME Implement caching of system token
// FIXME Implement caching of system token
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
system_token_res
.as_ref
();
let
system_token_res
=
system_token_res
.as_ref
();
...
@@ -133,8 +133,8 @@ impl PasswdHooks for OidcPasswd {
...
@@ -133,8 +133,8 @@ impl PasswdHooks for OidcPasswd {
let
conf
=
nss_hook_prepare
();
let
conf
=
nss_hook_prepare
();
info!
(
"[NSS] passwd.get_entry_by_name called for {}"
,
name
);
info!
(
"[NSS] passwd.get_entry_by_name called for {}"
,
name
);
let
mut
c
ache
=
get_cache
();
let
mut
c
ontext_user
=
get_context_user
();
let
user_token_res
=
cache
.
context_user
.get_access_token
();
let
user_token_res
=
context_user
.get_access_token
();
// FIXME Implement caching of system token
// FIXME Implement caching of system token
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
get_access_token_client
(
&
conf
,
"nss"
,
""
,
""
);
let
system_token_res
=
system_token_res
.as_ref
();
let
system_token_res
=
system_token_res
.as_ref
();
...
...
This diff is collapsed.
Click to expand it.
src/pam.rs
+
3
−
3
View file @
5edfd9e1
...
@@ -24,7 +24,7 @@ use crate::oauth::get_access_token_password;
...
@@ -24,7 +24,7 @@ use crate::oauth::get_access_token_password;
use
crate
::
logging
::
setup_log
;
use
crate
::
logging
::
setup_log
;
use
crate
::
cache
::{
get_c
ache
,
set_is_getpwnam_safe
};
use
crate
::
cache
::{
get_c
ontext_user
,
set_is_getpwnam_safe
};
use
pamsm
::{
PamServiceModule
,
Pam
,
PamFlag
,
PamError
,
PamLibExt
};
use
pamsm
::{
PamServiceModule
,
Pam
,
PamFlag
,
PamError
,
PamLibExt
};
...
@@ -91,8 +91,8 @@ impl PamServiceModule for PamOidc {
...
@@ -91,8 +91,8 @@ impl PamServiceModule for PamOidc {
Ok
(
t
)
=>
{
Ok
(
t
)
=>
{
info!
(
"Authenticated {} using Resource Owner Password Grant"
,
username
);
info!
(
"Authenticated {} using Resource Owner Password Grant"
,
username
);
set_is_getpwnam_safe
(
false
);
set_is_getpwnam_safe
(
false
);
get_
cache
()
.
context_user
.set_username
(
username
.to_string
());
get_context_user
()
.set_username
(
username
.to_string
());
get_
cache
()
.
context_user
.set_access_token
(
t
);
get_context_user
()
.set_access_token
(
t
);
set_is_getpwnam_safe
(
true
);
set_is_getpwnam_safe
(
true
);
return
PamError
::
SUCCESS
;
return
PamError
::
SUCCESS
;
},
},
...
...
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