-
Notifications
You must be signed in to change notification settings - Fork 533
Feat: Implement PAM authentication support #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vitalivu992
wants to merge
2
commits into
siteboon:main
Choose a base branch
from
vitalivu992:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+340
−23
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { spawn } from 'child_process'; | ||
import { promisify } from 'util'; | ||
|
||
class PAMAuthService { | ||
constructor() { | ||
this.serviceName = 'login'; // Default PAM service | ||
} | ||
|
||
/** | ||
* Authenticate a user using PAM via system commands | ||
* @param {string} username - The username to authenticate | ||
* @param {string} password - The password to verify | ||
* @returns {Promise<boolean>} True if authentication succeeds | ||
*/ | ||
async authenticate(username, password) { | ||
try { | ||
// Only use su command for PAM authentication | ||
const result = await this.authenticateWithSu(username, password); | ||
return result; | ||
} catch (error) { | ||
console.error('PAM authentication error:', error); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Authenticate using su command | ||
*/ | ||
async authenticateWithSu(username, password) { | ||
return new Promise((resolve) => { | ||
const child = spawn('su', [username, '-c', 'exit'], { | ||
stdio: ['pipe', 'pipe', 'pipe'] | ||
}); | ||
|
||
let output = ''; | ||
let error = ''; | ||
|
||
child.stdout.on('data', (data) => { | ||
output += data.toString(); | ||
}); | ||
|
||
child.stderr.on('data', (data) => { | ||
error += data.toString(); | ||
}); | ||
|
||
child.on('close', (code) => { | ||
resolve(code === 0); | ||
}); | ||
|
||
child.on('error', () => { | ||
resolve(false); | ||
}); | ||
|
||
// Send password to stdin | ||
child.stdin.write(password + '\n'); | ||
child.stdin.end(); | ||
|
||
// Timeout after 5 seconds | ||
setTimeout(() => { | ||
if (!child.killed) { | ||
child.kill(); | ||
resolve(false); | ||
} | ||
}, 5000); | ||
}); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Check if PAM authentication is available on this system | ||
*/ | ||
async isAvailable() { | ||
try { | ||
// Check if 'su' command is available (only command we need) | ||
const child = spawn('which', ['su']); | ||
await new Promise((resolve) => { | ||
child.on('close', resolve); | ||
}); | ||
|
||
return child.exitCode === 0; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Get user information from system | ||
*/ | ||
async getUserInfo(username) { | ||
return new Promise((resolve) => { | ||
const child = spawn('getent', ['passwd', username], { | ||
stdio: ['pipe', 'pipe', 'pipe'] | ||
}); | ||
|
||
let output = ''; | ||
|
||
child.stdout.on('data', (data) => { | ||
output += data.toString(); | ||
}); | ||
|
||
child.on('close', (code) => { | ||
if (code === 0 && output) { | ||
const parts = output.trim().split(':'); | ||
resolve({ | ||
username: parts[0], | ||
uid: parts[2], | ||
gid: parts[3], | ||
name: parts[4], | ||
home: parts[5], | ||
shell: parts[6] | ||
}); | ||
} else { | ||
resolve(null); | ||
} | ||
}); | ||
|
||
child.on('error', () => { | ||
resolve(null); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Set the PAM service name | ||
*/ | ||
setServiceName(serviceName) { | ||
this.serviceName = serviceName; | ||
} | ||
} | ||
|
||
export default new PAMAuthService(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
su
path can authenticate everyone when the service runs as rootIf this Node service ends up running as UID 0 (very common in containers or when started with sudo),
su ... -c exit
will always succeed regardless of the supplied password because PAM’spam_rootok
module lets root skip password checks entirely. That means a wrong password is accepted as soon as this branch is hit. We need to avoid usingsu
(or wrap it in a helper that enforces PAM verification) when the caller is privileged, and instead call a real PAM binding that validates the target user’s credentials.(man7.org)