forked from bacula-web/bacula-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console
121 lines (100 loc) · 3.79 KB
/
console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004 Juan Luis Frances Jimenez |
| Copyright 2010-2018, Davide Franco |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
*/
/*
* Function: printUsage
* Parameters: none
*
*/
function printUsage() {
echo "Bacula-Web version 8.0.0-RC1\n\n";
echo "Usage:\n";
echo " php console [command]\n\n";
echo "Available commands:\n";
echo " help\t\t\tPrint this help summary\n";
echo " setupauth\t\tSetup Apache authentication\n\n";
}
/*
* Function: getPassword
* Parameters: $prompt
*
*/
function getPassword($prompt)
{
// Save current tty settings
$ostty = `stty -g`;
// Set tty in silent mode
system("stty -echo -icanon min 1 time 0 2>/dev/null || " . "stty -echo cbreak");
echo "$prompt :";
// Drop newline at the end of the string
$input = substr(fgets(STDIN), 0, -1);
echo "\n";
// Restore tty settings
system ("stty $ostty");
return $input;
}
// Make sure the script is run from the command line
if(!(php_sapi_name() === 'cli')) {
exit("You are not allowed to run this script from a web browser, but only from the command line");
}
// Make sure at least one parameter has been provided
if( $argc < 2) {
echo "\nError: you should provide at least one command\n\n";
exit(printUsage());
}
// Get command from user input
switch( $argv[1]) {
case'help':
printUsage();
break;
case 'setupauth':
echo "Creating basic user authentication for Apache web server\n\n";
// Make sure current directory is writable by current user
echo "Checking if current directory is writable ...\n";
if(!is_writable(dirname(__FILE__))) {
exit("Error: cannot write into this directory\n");
}else{
echo "Success: This directory is writable, creating htpasswd file\n";
}
// Get user details from command line
$username = readline("Enter your username: ");
$username = str_replace(array("\n", "\t"), '', $username);
$password = getPassword("Enter your password");
// Hash the password
$options = ['cost' => 12];
$hashedPassword = password_hash($password, PASSWORD_DEFAULT, $options);
$htpwdFile = '.htpasswd';
// Check if .htpasswd already exists
if(file_exists($htpwdFile)) {
exit("\nError: file $htpwdFile already exists, please remove it and run this command again\n");
}
$handle = @fopen($htpwdFile, 'w');
if($handle !== FALSE) {
$data = "$username:$hashedPassword";
fwrite($handle, $data);
fclose($handle);
}else{
exit("\nError: unable to create file $htpwdFile, please check permissions\n");
}
break;
default:
exit("\nError: unknown command, use <php console help> for further informations\n\n");
}
echo "\nGreat !!! Apache authentication setup is now completed\n\n";
echo "You can now use the credentials you've provided to access Bacula-Web\n";
?>