-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.php
308 lines (272 loc) · 12.9 KB
/
config.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
session_start();
include 'sidebar.php';
include 'functions.php';
error_reporting(E_ALL);
ini_set('display_errors', 'on');
set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
include('Net/SSH2.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
</head>
<!-- set body id for bootstrap.css to determine which menu item to highlight -->
<body id="bconfigs">
<div class="span8">
<h2> Config File Settings</h2>
<?php
//read the settings we created in install.php
read_config_file();
$config_file_with_path = $config_dir.$config_file;
//TODO.. throw in a test to make sure our $config_file_with_path ends in conf....
//So we don't copy the folder itself to /etc/openvpn and cause heart attacks.
echo "<h3>Reading config from: $config_file_with_path</h3><br />";
$vpn_config = read_openvpn_config($config_file_with_path);
//THIS TIME WILL DUMP TO GLOBAL... maybe change in future? //TODO
extract($vpn_config);
//Now.. if we updated the config file.....
if ((isset($_GET['action'])) and ($_GET['action'] == "update")){
$config_file_temp = $config_file;
file_put_contents($config_file_temp, "port " . $_POST['port_value'].PHP_EOL);
file_put_contents($config_file_temp, "proto " . $_POST['proto_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "dev " . $_POST['dev_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "ca " . $_POST['ca_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "cert " . $_POST['crt_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "key " . $_POST['key_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "dh " . $_POST['dh_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "server " . $_POST['server_value']. " " . $_POST['server_value2'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "ifconfig-pool-persist " . $_POST['ifconfig_pool_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "keepalive " . $_POST['keepalive_value']. " " . $_POST['keepalive_value2'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "status " . $_POST['status_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "user " . $_POST['user_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "group " . $_POST['group_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
file_put_contents($config_file_temp, "verb " . $_POST['verb_value'].PHP_EOL, FILE_APPEND | LOCK_EX);
//file_put_contents($config_file_temp, "management " . $_POST['management_value']. " " . $_POST['management_value2'].PHP_EOL, FILE_APPEND | LOCK_EX);
//creating array with just the "extra settings" that will delete any post values that are empty.
$a_extra_config_settings_write = array();
foreach ($_POST as $key => $value) {
if ((in_array($value, $a_extra_config_settings)) and ($value != "")){//cleaning post of stuff not in the extra settings array
if ($key == $value){
array_push($a_extra_config_settings_write, $value);//if this is not done will print key and value ie comp-lzo comp-lzo, since I throw away settings that start with integers
}else{$a_extra_config_settings_write[$key] = $value;
Continue;}
}
}
if ((isset($_POST['new_setting_name'])) and (!empty($_POST['new_setting_name'])) and ($_POST['new_setting_name'] != "New Setting Name (if needed)")) {
$a_extra_config_settings_write[$_POST['new_setting_name']] = $_POST['new_setting_value'];
}else{
if ((isset($_POST['new_setting_value'])) and ($_POST['new_setting_value'] != "New Setting Data")){
array_push($a_extra_config_settings_write, $_POST['new_setting_value']);
}
}
//Checking for empty values
//match POST values to $a_extra_config_settings array, create new array with updated post values.
foreach ($a_extra_config_settings_write as $key => $value) {
if (!is_numeric($key)){ //if the value that was added appears to have an option
file_put_contents($config_file_temp, $key. " " . $value.PHP_EOL, FILE_APPEND | LOCK_EX);
} else {
if ($value == ""){Continue;}else{file_put_contents($config_file_temp, $value.PHP_EOL, FILE_APPEND | LOCK_EX);}
}
}
$cwdir = getcwd();
echo "<h3> Config file written to $cwdir/$config_file_temp.</h3><br />";
echo "Shall I attempt to copy config file to $config_file_with_path?<br />";
echo "This will require openvpn to be restarted..... continue?<br />";
echo "<a class='btn btn-danger' href='config.php?action=restart'>Restart Openvpn</a>";
echo " ";
echo "<a class='btn btn-primary' href='index.php'>I will restart manually</a>";
exit;
}
//End update file... now checking for a restart command....
if ((isset($_GET['action'])) and ($_GET['action'] == "restart")){
if (! (isset($_SESSION['password']))){
//will start the session if needed, and return to config.php?action=restart
start_session('config.php?action=restart');
}
$password = stripslashes(trim($_SESSION['password']));
$username = stripslashes(trim($_SESSION['username']));
if ($username == ""){
$username = "root";
}
$ssh = new Net_SSH2('localhost');
if (!$ssh->login($username, $password)) {
exit('Login Failed');
}
read_config_file();
$config_file_temp = $config_file;
$cwdir = getcwd();
echo "<pre>";
echo $ssh->exec("sudo killall -v openvpn");
echo "</pre>";
echo str_repeat(' ',1024*64);
echo "<pre>Creating backup of current (working) config! sudo cp $config_dir$config_file $config_dir$config_file.bak";
echo $ssh->exec("sudo cp $config_dir$config_file $config_dir$config_file.bak");
echo "</pre>";
echo str_repeat(' ',1024*64);
echo "<pre>executing sudo mv $cwdir/$config_file_temp $config_dir$config_file<br />";
echo $ssh->exec("sudo mv $cwdir/$config_file_temp $config_dir$config_file");
echo "</pre>";
echo str_repeat(' ',1024*64);
$start_openvpn = $ssh->exec("/etc/init.d/openvpn start");
if (stristr($start_openvpn, 'failed')){
echo "<pre>$start_openvpn</pre>";
echo "<pre><font color='B22222'>Whoops</font>... openvpn failed to start! Most likely an invalid configuration file!</pre>";
echo "<a class='btn btn-primary' href='config.php?action=revert'>Revert to old config!</a>";
echo " ";
echo "<a class='btn' href='index.php'>I will fix it manually</a>";
exit;
}
echo "<pre>$start_openvpn</pre>";
echo "<br />";
echo "Apparent Success...<br />";
echo "<a class='btn' href='index.php'>Back to Home</a>";
exit;
}
//And... checking if we need to revert back to old/default config
if ((isset($_GET['action'])) and ($_GET['action'] == "revert")){
//TODO Session variables
$password = 'RHB12+ADMIN';
if (empty($_POST['username'])){
$username="root";
}else{$username = stripslashes(trim($_POST['username']));}
$ssh = new Net_SSH2('localhost');
if (!$ssh->login($username, $password)) {
exit('Login Failed');
}
read_config_file();
$config_file_temp = $config_file;
$cwdir = getcwd();
echo "<pre>";
echo $ssh->exec("sudo killall -v openvpn");
echo "</pre>";
echo str_repeat(' ',1024*64);
echo "<pre>Restoring backup(working) config! sudo mv $config_dir$config_file.bak $config_dir$config_file";
echo $ssh->exec("sudo mv $config_dir$config_file.bak $config_dir$config_file");
echo "</pre>";
echo str_repeat(' ',1024*64);
$start_openvpn = $ssh->exec("/etc/init.d/openvpn start");
if (stristr($start_openvpn, 'failed')){
echo "<pre>$start_openvpn</pre>";
echo "<pre><font color='B22222'>Whoops</font>... openvpn failed to start again! View log file for more information...</pre>";
//TODO Show them log file....
echo "<a class='btn' href='index.php'>Home Page</a>";
exit;
}
echo "<pre>$start_openvpn</pre>";
echo "Apparent Success...<br />";
echo "<a class='btn' href='index.php'>Back to Home</a>";
exit;
}
//exit revert loop...
echo "<form class='well span6' action='config.php?action=update' method='post'>";
//Let user change openvpn settings
//cant do for loop.. some are dropdown selections, some have multiple inputs
//Since not all values are required... check to see if they are empty first
if (!(empty($port_values[0]))){
echo "$port_values[0] : ";
echo "<input type='text' name='port_value' value='$port_values[1]'><br />";
}
if (!(empty($proto_values[0]))){
echo "$proto_values[0] : ";
//creating default select options
if ($proto_values[1] == "udp"){
$device_choice = "tcp";
} else { $device_choice = "udp";}
echo "<select name='proto_value'>";
echo "<option value='$proto_values[1]' selected='selected'>$proto_values[1]</option>";
echo "<option value='$device_choice'>$device_choice</option>";
echo "</select><br />";
}
if (!(empty($dev_values[0]))){
echo "$dev_values[0] : ";
//creating default select options
if ($dev_values[1] == "tun"){
$dev_choice = "tap";
} else { $dev_choice = "tun";}
echo "<select name='dev_value'>";
echo "<option value='$dev_values[1]' selected='selected'>$dev_values[1]</option>";
echo "<option value='$dev_choice'>$dev_choice</option>";
echo "</select><br />";
}
if (!(empty($ca_values[0]))){
echo "$ca_values[0] : ";
echo "<input type='text' name='ca_value' value='$ca_values[1]'><br />";
}
if (!(empty($crt_values[0]))){
echo "$crt_values[0] : ";
echo "<input type='text' name='crt_value' value='$crt_values[1]'><br />";
}
if (!(empty($key_values[0]))){
echo "$key_values[0] : ";
echo "<input type='text' name='key_value' value='$key_values[1]'><br />";
}
if (!(empty($dh_values[0]))){
echo "$dh_values[0] : ";
echo "<input type='text' name='dh_value' value='$dh_values[1]'><br />";
}
if (!(empty($server_values[0]))){
echo "$server_values[0] : ";
echo "<input type='text' name='server_value' value='$server_values[1]'>";
echo "<input type='text' name='server_value2' value='$server_values[2]'><br />";
}
if (!(empty($ifconfig_pool_values[0]))){
echo "$ifconfig_pool_values[0] : ";
echo "<input type='text' name='ifconfig_pool_value' value='$ifconfig_pool_values[1]'><br />";
}
if (!(empty($keepalive_values[0]))){
echo "$keepalive_values[0] : ";
echo "<input type='text' name='keepalive_value' value='$keepalive_values[1]'>";
echo "<input type='text' name='keepalive_value2' value='$keepalive_values[2]'><br />";
}
if (!(empty($status_values[0]))){
echo "$status_values[0] : ";
echo "<input type='text' name='status_value' value='$status_values[1]'><br />";
}
if (!(empty($user_values[0]))){
echo "$user_values[0] : ";
echo "<input type='text' name='user_value' value='$user_values[1]'><br />";
}
if (!(empty($group_values[0]))){
echo "$group_values[0] : ";
echo "<input type='text' name='group_value' value='$group_values[1]'><br />";
}
if (!(empty($verb_values[0]))){
echo "$verb_values[0] : ";
echo "<input type='text' name='verb_value' value='$verb_values[1]'><br />";
}
//if (!(empty($management_values[0]))){
//echo "$management_values[0] : ";
//echo "<input type='text' name='management_value' value='$management_values[1]'>";
//echo "<input type='text' name='management_value2' value='$management_values[2]'><br />";
//}
//print_r($a_extra_config_settings);
foreach ($a_extra_config_settings as $key => $value) {
echo "<input type='text' name='$value' value='$value'> <br />";
}
if ((isset($_GET['action'])) and ($_GET['action'] == "add-line")){
echo "<input type='text' name='new_setting_name' value='New Setting Name (if needed)'>";
echo "<input type='text' name='new_setting_value' value='New Setting Data'> <br />";
//array_push($a_extra_config_settings, "New Setting");
}
echo "<br /><br />";
if (!(isset($_GET['action'])) or ($_GET['action'] != "add-line")){
echo "<a class='btn btn-primary' href='config.php?action=add-line'>Add 1 Line</a>";
}
if ((isset($_GET['action'])) and ($_GET['action'] == "add-line")){
echo "<a class='btn btn-primary' href='config.php?action=del-line'>Remove 1 Line</a>";
}
echo "<br /><br />";
echo "<button type='submit' class='btn btn-danger'>Update Config</button>";
echo "</form>";
//echo "Test!";
//echo "<pre>";
//print_r($a_extra_config_settings);
//echo "</pre>";
?>
<!-- Body content -->
</div>
<!-- Required to have 2 divs at the end to close sidebar.php -->
</body>
</html>