auth/index.php

27 lines
1002 B
PHP
Raw Normal View History

2018-02-12 22:00:00 +00:00
<?php
2018-02-14 22:59:08 +00:00
if (empty($_REQUEST['user']) || empty($_REQUEST['pw']))
die('0');
2018-02-12 22:00:00 +00:00
2018-02-14 22:59:08 +00:00
function authenticate($user, $pass){
// run shell command to output shadow file, and extract line for $user
// then spit the shadow line by $ or : to get component parts
// store in $shad as array
$shad = preg_split("/[$:]/",`cat /etc/shadow | grep "^$user\:"`);
// use mkpasswd command to generate shadow line passing $pass and $shad[3] (salt)
// split the result into component parts
$mkps = preg_split("/[$:]/",trim(`mkpasswd -m sha-512 $pass $shad[3]`));
// compare the shadow file hashed password with generated hashed password and return
return ($shad[4] == $mkps[3]);
}
2018-02-12 22:00:00 +00:00
2018-02-22 18:05:48 +00:00
if (isset($_REQUEST["json"])) {
2018-02-22 18:09:50 +00:00
$auth = authenticate($_REQUEST['user'], $_REQUEST['pw']);
2018-02-22 18:05:48 +00:00
echo json_encode([
2018-02-22 18:09:50 +00:00
"authenticated" => $auth,
"sudoer" => $auth && in_array($_REQUEST["user"], posix_getgrnam("sudo")["members"])
2018-02-22 18:05:48 +00:00
]);
} else { // v1 api
echo authenticate($_REQUEST['user'], $_REQUEST['pw']) ? '1' : '0';
}