PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / SocketAPI / Client / WellKnown.php
backup / src / JetBackup / 3rdparty / SocketAPI / Client Last commit date
.htaccess 1 year ago Client.php 9 months ago WellKnown.php 9 months ago index.html 1 year ago web.config 1 year ago
WellKnown.php
81 lines
1 <?php
2 /*
3 *
4 * JetBackup @ package
5 * Created By Idan Ben-Ezra
6 *
7 * Copyrights @ JetApps
8 * https://www.jetapps.com
9 *
10 **/
11 namespace JetBackup\SocketAPI\Client;
12
13 use JetBackup\Entities\Util;
14 use JetBackup\SocketAPI\Exception\WellKnownException;
15
16 class WellKnown {
17
18 const WELL_KNOWN_FILENAME = '.jetbackup-well-known';
19 const HASH_CHARS = '0123456789abcdef';
20 const HASH_REGEX = '^[0-9a-f]{32}$';
21 const HASH_LENGTH = 32;
22
23 private $_details;
24 private $_file;
25
26 private function __construct() {
27 $this->_details = Util::getpwuid(Util::geteuid());
28 if (!$this->_details) throw new WellKnownException("[wellknown auth] Cannot get posix uid/gid details");
29 $this->_file = preg_replace("#/+#", "/" ,$this->_details['dir'] . '/' . self::WELL_KNOWN_FILENAME);
30 }
31
32 private static function _generateHash() {
33 $randomString = '';
34 for ($i = 0; $i < self::HASH_LENGTH; $i++) $randomString .= self::HASH_CHARS[rand(0, strlen(self::HASH_CHARS)-1)];
35 return $randomString;
36 }
37
38 /**
39 * @return string
40 * @throws WellKnownException
41 */
42 private function _generate() {
43 if(file_exists($this->_file) && !@unlink($this->_file)) throw new WellKnownException("Failed deleting old well known file");
44 $password = self::_generateHash();
45 $umask = umask(077);
46 if(!file_put_contents($this->_file, $password)) throw new WellKnownException("Failed writing password to well known file");
47 umask($umask);
48 return $password;
49 }
50
51 /**
52 * @return string
53 * @throws WellKnownException
54 */
55 private function _fetch() {
56 if(file_exists($this->_file)) {
57 $stat = stat($this->_file);
58 if(
59 $stat['mtime'] > (time()-3600) &&
60 substr(sprintf('%o', $stat['mode']), -4) == '0600' &&
61 $this->_details['uid'] == $stat['uid'] &&
62 ($password = file_get_contents($this->_file)) &&
63 self::_validateHash($password)
64 ) return $password;
65 }
66 return $this->_generate();
67 }
68
69 private static function _validateHash($hash) {
70 return preg_match("/" . self::HASH_REGEX . "/", $hash);
71 }
72
73 /**
74 * @return string
75 * @throws WellKnownException
76 */
77 public static function getPassword() {
78 return (new WellKnown())->_fetch();
79 }
80 }
81