2017-01-26 18:55:53 +00:00
|
|
|
#!/usr/bin/php
|
|
|
|
|
2017-01-27 10:09:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../include/const';
|
2017-01-26 18:55:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [1] Function that generates a random secret
|
|
|
|
=========================================================*/
|
|
|
|
function generate_secret(){
|
|
|
|
|
|
|
|
/* (1) Generate random set */
|
|
|
|
$charlist = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
|
|
|
|
|
|
|
|
/* (2) Set useful variables */
|
|
|
|
$clen = strlen($charlist);
|
|
|
|
$secret = '';
|
|
|
|
|
|
|
|
/* (3) Generate random characters one by one */
|
|
|
|
for( $i = 0 ; $i < SECRET_SIZE ; $i++ )
|
|
|
|
$secret .= $charlist[rand(0, $clen - 1)];
|
|
|
|
|
|
|
|
/* (4) Return the secret */
|
|
|
|
return $secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Fetch necessary data
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Fetch secret file */
|
2017-01-27 10:09:34 +00:00
|
|
|
$secret = @file_get_contents(SECRET_CONF);
|
2017-01-26 18:55:53 +00:00
|
|
|
|
|
|
|
/* (2) Check secret file format */
|
|
|
|
if( !is_string($secret) || !preg_match("/^(.{250}):(\d+)$/", $secret, $match) ){
|
|
|
|
|
|
|
|
// Generate new secret
|
|
|
|
$secret = generate_secret().':1000';
|
|
|
|
|
|
|
|
// Try to override the secret file
|
2017-01-27 10:09:34 +00:00
|
|
|
@file_put_contents(SECRET_CONF, $secret) && die(0) || die(127);
|
2017-01-26 18:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Extract data */
|
|
|
|
$key = (string) $match[1];
|
|
|
|
$depth = (int) $match[2];
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] If can decrement, decrement
|
|
|
|
=========================================================*/
|
|
|
|
if( $depth > 1 ){
|
|
|
|
|
|
|
|
/* (1) Decrement the depth */
|
|
|
|
$depth--;
|
|
|
|
|
|
|
|
/* (2) Try to override the secret file */
|
2017-01-27 10:09:34 +00:00
|
|
|
@file_put_contents(SECRET_CONF, "$key:$depth") && die(0) || die(127);
|
2017-01-26 18:55:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] If cannot decrement, generate new password
|
|
|
|
=========================================================*/
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// Generate new secret
|
|
|
|
$secret = generate_secret().':1000';
|
|
|
|
|
|
|
|
// Try to override the secret file
|
2017-01-27 10:09:34 +00:00
|
|
|
file_put_contents(SECRET_CONF, $secret) && die(0) || die(127);
|
2017-01-26 18:55:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|