function create_client_config_and_send($cert_name, $config_dir, $config_file, $remote_value, $send_type, $key_dir_name) { //will need phpseclib later.... so... if (!isset($_SESSION['password'])) { start_session('certs.php?action=send_cert&type=$send_type&cert_name=$cert_name'); } $password = stripslashes(trim($_SESSION['password'])); $username = stripslashes(trim($_SESSION['username'])); if ($username == "") { $username = "******"; } $ssh = new Net_SSH2('localhost'); if (!$ssh->login($username, $password)) { exit('Login Failed'); } //Creating a default client config file //TODO have a check to make sure /etc/openvpn.conf exists.... //If none exists... then you should prolly do some checks when the webui is first launched.... duh if (!file_exists("openvpn-client-default.conf")) { create_default_client_config(); } $vpn_config = read_openvpn_config($config_dir . $config_file); extract($vpn_config); echo "Default client file exists... <br />"; echo "Now copying to new config {$cert_name}.conf<br />"; copy("openvpn-client-default.conf", "{$cert_name}.conf"); //New Conf file should exist.... Now edit specifics... if (file_exists("{$cert_name}.conf")) { $client_config = "{$cert_name}.conf"; file_put_contents($client_config, "cert " . $cert_name . ".crt" . PHP_EOL, FILE_APPEND | LOCK_EX); file_put_contents($client_config, "key " . $cert_name . ".key" . PHP_EOL, FILE_APPEND | LOCK_EX); } else { echo "Error... client config file not found?<br />"; exit; } //Finding what dir $curr_work_dir = getcwd(); echo "Copying needed files...<br />"; //Now copying all the necc. sh!t to the root folder... then zip it? tar it? idk.. prolly zip $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); $ssh->write("cd {$key_dir_name};cp {$cert_name}.key {$ca_values['1']} {$cert_name}.crt {$curr_work_dir}\n"); $result = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); echo "<pre>{$result}</pre>"; //HAVE to change the permissions on *.key... or php can't touch it echo "Have to change permissions on the *.key file.. or php can't touch..<br />"; $ssh->write("cd {$curr_work_dir};chmod 555 {$cert_name}.key\n"); $result = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); echo "<pre>{$result}</pre>"; echo str_repeat(' ', 1024 * 64); //create array of files to zip $files_to_zip = array("{$cert_name}.crt", "{$cert_name}.key", "{$ca_values['1']}", "{$cert_name}.conf"); echo "Creating zip file <b>{$cert_name}.zip</b><br />"; echo str_repeat(' ', 1024 * 64); $result = create_zip($files_to_zip, "{$cert_name}.zip", $cert_name); // unlink every other file... unlink("{$cert_name}.crt"); unlink("{$cert_name}.key"); unlink("{$ca_values['1']}"); unlink("{$cert_name}.conf"); if ($send_type == "download") { echo "<br /> Download transfer type selected!"; echo "<br />"; echo "Result:{$result}"; echo str_repeat(' ', 1024 * 64); sleep(5); // return name of zip file, will use that to generate download link return $result; exit; } if ($send_type == "scp") { if (!isset($_SESSION['password'])) { start_session('certs.php?action=send_cert&type=$send_type&cert_name=$cert_name'); } $password = stripslashes(trim($_SESSION['password'])); $username = stripslashes(trim($_SESSION['username'])); if ($username == "") { $username = "******"; } $ssh = new Net_SSH2('localhost'); if (!$ssh->login($username, $password)) { exit('Login Failed'); } echo "SCP transfer type selected!"; echo "TYPE TOO"; echo str_repeat(' ', 1024 * 64); $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); $ssh->write("cd {$curr_work_dir};scp {$cert_name}.zip {$username}@{$remote_host}:{$remote_dir}\n"); $ssh->setTimeout(10); sleep(5); $result = $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX); echo "<pre>{$result}</pre>"; echo str_repeat(' ', 1024 * 64); exit; } }
<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="bclientconfig"> <div class="span8"> <h2> Client Config File Settings</h2> <?php //read the settings we created in install.php read_config_file(); if (!file_exists("openvpn-client-default.conf")) { create_default_client_config(); } $client_config_file_default = "openvpn-client-default.conf"; echo "<h3>Reading config from: {$client_config_file_default}</h3><br />"; $vpn_config = read_openvpn_config($client_config_file_default); //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 = $client_config_file_default; 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);
echo "Checking for ca keys......<br /><br />"; echo "Checking for server keys.....<br /><br /><br /><br />"; echo "Checking for pkitool.....<br /><br />"; echo "Checking for openssl.cnf.....<br /><br /><br />"; echo "</div>"; echo "<div class='span5'>"; //Just need to find the config file to read from it... foreach ($config_dir_files as $current_file) { if (fnmatch("*.conf", $current_file)) { //searching for files ending in .conf $config_file = $current_file; $config_file_found = "yes"; file_put_contents("settings.conf", "config_file:" . $config_file . PHP_EOL, FILE_APPEND | LOCK_EX); //Reading config file to get values $config_file_full_path = $config_dir . $config_file; $vpn_config = read_openvpn_config($config_file_full_path); //in functions.php, creates arrays of config file //TODO.. once again, doing a var dump... need to change to call directly.. extract($vpn_config); } } //Getting some values from config file $ca_crt_name = trim($ca_values[1]); $ca_key_name_no_ext = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $ca_crt_name); //purging ext from file $ca_key_name = $ca_key_name_no_ext . '.key'; $server_key_name = trim($key_values[1]); $server_crt_name = trim($crt_values[1]); //setting default values as no for some variables $key_dir_found = "no"; //Next loop will loop through files again, looking for values found in config file (validity check)
<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);