tilde-oauth/auth/setKey.php

81 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2022-05-07 08:02:58 +00:00
<?php
// Create an account and apply SSH key
2023-10-02 00:29:59 +00:00
$config = json_decode(file_get_contents("/var/www/usergen/secret/config.json", true));
2022-05-07 08:02:58 +00:00
2023-10-04 05:26:33 +00:00
require_once("/var/www/usergen/secret/helpers.php");
require_once("/var/www/usergen/secret/oauth.php");
require_once("/var/www/usergen/secret/rsa.php");
2022-05-07 08:02:58 +00:00
function checkParameters($parameterArray){
$error = false;
foreach($parameterArray as $parameter){
if(!isset($_POST[$parameter])){
$error = true;
}
}
return $error;
}
function apiResult($result){
header('Content-type: application/json');
echo json_encode($result);
exit();
}
2023-10-04 05:26:33 +00:00
function success($encryptedToken){
$Auth = verifyEncToken($encryptedToken);
returnSuccess(true, buildEncToken($Auth["AuthToken"], $Auth["UserID"], $_SERVER["REMOTE_ADDR"], $_SERVER["HTTP_USER_AGENT"]));
2022-05-07 08:02:58 +00:00
}
function error($error){
2023-10-04 05:26:33 +00:00
returnError($error);
2022-05-07 08:02:58 +00:00
}
function validateUsername($username){
return (preg_match("/^([a-zA-Z0-9_.]+)$/", $username) == 1);
}
2022-05-07 23:45:13 +00:00
function validatePublicKey($key){
return (preg_match("/^(ssh-rsa AAAAB3NzaC1yc2|ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNT|ecdsa-sha2-nistp384 AAAAE2VjZHNhLXNoYTItbmlzdHAzODQAAAAIbmlzdHAzOD|ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1Mj|ssh-ed25519 AAAAC3NzaC1lZDI1NTE5|ssh-dss AAAAB3NzaC1kc3)[0-9A-Za-z+\/]+[=]{0,3}( .*)?$/", $key) == 1);
}
2023-10-04 05:26:33 +00:00
if (checkParameters(array("pubkey", "token"))){
2022-05-07 08:02:58 +00:00
error("Missing parameters");
}
2023-10-04 05:26:33 +00:00
$userToken = $_POST["token"];
2022-05-07 08:02:58 +00:00
$pubkey = $_POST["pubkey"];
2022-05-07 23:45:13 +00:00
if(!validatePublicKey($pubkey)){
error("Invalid public key");
}
2023-10-04 05:26:33 +00:00
$User = verifyEncToken($userToken);
2022-05-07 08:02:58 +00:00
// Check User
2023-10-04 05:26:33 +00:00
if (gettype($User) == "string") {
// Invalid Token
error($User);
}else{
// Valid Token
$User = $User["MastodonData"];
2022-05-07 08:02:58 +00:00
}
2023-10-04 05:26:33 +00:00
2022-05-07 08:02:58 +00:00
if(!validateUsername($User->username)){
2023-10-04 05:26:33 +00:00
error("Invalid POSIX Username");
2022-05-07 08:02:58 +00:00
}
// Create temporary pubkey holding file
2022-06-26 02:40:39 +00:00
$TempFileName = "/etc/ttyserver/tmp/".uniqid("ssh-", true).".pub";
2022-05-07 08:02:58 +00:00
if(!file_put_contents($TempFileName, $pubkey."\n")){
error("Key Addition Failed: Temp");
}
// Run User Generation Tool
2023-10-04 05:26:33 +00:00
// TODO: Replace with custom Rust PHP Extension?
2022-06-26 02:40:39 +00:00
$UserGenCode = shell_exec("/usr/bin/sudo /etc/ttyserver/bin/mkuser \"".$User->username."\" \"".$TempFileName."\" 2>&1; echo $?");
2022-05-07 08:02:58 +00:00
if($UserGenCode != "0"){
error("Key Addition Failed: MK-".$UserGenCode);
}
2023-10-04 05:26:33 +00:00
success($userToken);
2022-05-07 08:02:58 +00:00
2023-10-02 00:29:59 +00:00
?>