Newer
Older
unset($webserver_check_output, $webserver_check_retval);
exec('which httpd', $webserver_check_output, $webserver_check_retval);
if($webserver_check_retval == 0){
$webserver_binary = 'httpd';
} else {
unset($webserver_check_output, $webserver_check_retval);
exec('which apache', $webserver_check_output, $webserver_check_retval);
if($webserver_check_retval == 0){
$webserver_binary = 'apache';
}
}
}
}
}
if($webserver_binary != ''){
exec($webserver_binary.' -t 2>&1', $tmp_output, $tmp_retval);
if($tmp_retval > 0 && is_array($tmp_output) && !empty($tmp_output)){
$app->log('Reason for Apache restart failure: '.implode("\n", $tmp_output), LOGLEVEL_WARN);
$app->dbmaster->datalogError(implode("\n", $tmp_output));
}
unset($tmp_output, $tmp_retval);
}
}
$app->system->copy($vhost_file, $vhost_file.'.err');
if(is_file($vhost_file.'~')) {
//* Copy back the last backup file
$app->system->copy($vhost_file.'~', $vhost_file);
} else {
//* There is no backup file, so we create a empty vhost file with a warning message inside
$app->system->file_put_contents($vhost_file, "# Apache did not start after modifying this vhost file.\n# Please check file $vhost_file.err for syntax errors.");
if($this->ssl_certificate_changed === true) {
//* Backup the files that might have caused the error

tbrehm
committed
if(is_file($key_file)){
$app->system->copy($key_file, $key_file.'.err');
$app->system->chmod($key_file.'.err', 0400);

tbrehm
committed
}
if(is_file($key_file2)){
$app->system->copy($key_file2, $key_file2.'.err');
$app->system->chmod($key_file2.'.err', 0400);

tbrehm
committed
}
if(is_file($csr_file)) $app->system->copy($csr_file, $csr_file.'.err');
if(is_file($crt_file)) $app->system->copy($crt_file, $crt_file.'.err');
if(is_file($bundle_file)) $app->system->copy($bundle_file, $bundle_file.'.err');
//* Restore the ~ backup files
if(is_file($key_file.'~')) $app->system->copy($key_file.'~', $key_file);
if(is_file($key_file2.'~')) $app->system->copy($key_file2.'~', $key_file2);
if(is_file($crt_file.'~')) $app->system->copy($crt_file.'~', $crt_file);
if(is_file($csr_file.'~')) $app->system->copy($csr_file.'~', $csr_file);
if(is_file($bundle_file.'~')) $app->system->copy($bundle_file.'~', $bundle_file);
$app->log('Apache did not restart after the configuration change for website '.$data['new']['domain'].' Reverting the SSL configuration. Saved non-working SSL files with .err extension.', LOGLEVEL_WARN);
$app->services->restartService('httpd', 'restart');
} else {
//* We do not check the apache config after changes (is faster)
if($apache_chrooted) {
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
}
//* The vhost is written and apache has been restarted, so we
// can reset the ssl changed var to false and cleanup some files
$this->ssl_certificate_changed = false;
if(@is_file($key_file.'~')) $app->system->unlink($key_file.'~');
if(@is_file($key_file2.'~')) $app->system->unlink($key_file2.'~');
if(@is_file($crt_file.'~')) $app->system->unlink($crt_file.'~');
if(@is_file($csr_file.'~')) $app->system->unlink($csr_file.'~');
if(@is_file($bundle_file.'~')) $app->system->unlink($bundle_file.'~');
// Remove the backup copy of the config file.
if(@is_file($vhost_file.'~')) $app->system->unlink($vhost_file.'~');
//* Unset action to clean it for next processed vhost.
$this->action = '';
function delete($event_name, $data) {
global $app, $conf;
// load the server configuration options
$app->uses('getconf');
$app->uses('system');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$fastcgi_config = $app->getconf->get_server_config($conf['server_id'], 'fastcgi');
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') $app->system->web_folder_protection($data['old']['document_root'], false);

tbrehm
committed
//* Check if this is a chrooted setup
if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {

tbrehm
committed
$apache_chrooted = true;
} else {
$apache_chrooted = false;
}
//* Remove the mounts
$log_folder = 'log';
if($data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
$tmp = $app->db->queryOneRecord('SELECT `domain`,`document_root` FROM web_domain WHERE domain_id = ?', $data['old']['parent_domain_id']);
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
if($tmp['domain'] != ''){
$subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['old']['domain']);
} else {
// get log folder from /etc/fstab
/*
$bind_mounts = $app->system->file_get_contents('/etc/fstab');
$bind_mount_lines = explode("\n", $bind_mounts);
if(is_array($bind_mount_lines) && !empty($bind_mount_lines)){
foreach($bind_mount_lines as $bind_mount_line){
$bind_mount_line = preg_replace('/\s+/', ' ', $bind_mount_line);
$bind_mount_parts = explode(' ', $bind_mount_line);
if(is_array($bind_mount_parts) && !empty($bind_mount_parts)){
if($bind_mount_parts[0] == '/var/log/ispconfig/httpd/'.$data['old']['domain'] && $bind_mount_parts[2] == 'none' && strpos($bind_mount_parts[3], 'bind') !== false){
$subdomain_host = str_replace($data['old']['document_root'].'/log/', '', $bind_mount_parts[1]);
}
}
}
}
*/
// we are deleting the parent domain, so we can delete everything in the log directory
$subdomain_hosts = array();
$files = array_diff(scandir($data['old']['document_root'].'/'.$log_folder), array('.', '..'));
if(is_array($files) && !empty($files)){
foreach($files as $file){
if(is_dir($data['old']['document_root'].'/'.$log_folder.'/'.$file)){
$subdomain_hosts[] = $file;
}
}
}
}
if(is_array($subdomain_hosts) && !empty($subdomain_hosts)){
$log_folders = array();
foreach($subdomain_hosts as $subdomain_host){
$log_folders[] = $log_folder.'/'.$subdomain_host;
}
} else {
if($subdomain_host == '') $subdomain_host = 'web'.$data['old']['domain_id'];
$log_folder .= '/' . $subdomain_host;
}
$web_folder = $data['old']['web_folder'];
}
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias'){
if(is_array($log_folders) && !empty($log_folders)){
foreach($log_folders as $log_folder){
$app->system->exec_safe('umount -l ? 2>/dev/null', $data['old']['document_root'].'/'.$log_folder);
$app->system->exec_safe('umount -l ? 2>/dev/null', $data['old']['document_root'].'/'.$log_folder);
// remove letsencrypt if it exists (renew will always fail otherwise)
$old_domain = $data['old']['domain'];
if(substr($old_domain, 0, 2) === '*.') {
// wildcard domain not yet supported by letsencrypt!
$old_domain = substr($old_domain, 2);
}
$le_conf_file = '/etc/letsencrypt/renewal/' . $old_domain . '.conf';
@rename('/etc/letsencrypt/renewal/' . $old_domain . '.conf', '/etc/letsencrypt/renewal/' . $old_domain . '.conf~backup');
//* remove mountpoint from fstab
if(is_array($log_folders) && !empty($log_folders)){
foreach($log_folders as $log_folder){
$fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.' none bind';
$app->system->removeLine('/etc/fstab', $fstab_line);
}
} else {
$fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.' none bind';
$app->system->removeLine('/etc/fstab', $fstab_line);
if($data['old']['type'] != 'vhost' && $data['old']['type'] != 'vhostsubdomain' && $data['old']['type'] != 'vhostalias' && $data['old']['parent_domain_id'] > 0) {
//* This is a alias domain or subdomain, so we have to update the website instead
$parent_domain_id = intval($data['old']['parent_domain_id']);
$tmp = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ? AND active = 'y'", $parent_domain_id);
$data['new'] = $tmp;
$data['old'] = $tmp;
$this->action = 'update';
$this->update_letsencrypt = true;
// just run the update function
$this->update($event_name, $data);
} else {
//* This is a website
// Deleting the vhost file, symlink and the data directory

Marius Burkard
committed
$vhost_file = $web_config['vhost_conf_dir'].'/'.$data['old']['domain'].'.vhost';

Marius Burkard
committed
$vhost_symlink = $web_config['vhost_conf_enabled_dir'].'/'.$data['old']['domain'].'.vhost';
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);

Marius Burkard
committed
$vhost_symlink = $web_config['vhost_conf_enabled_dir'].'/900-'.$data['old']['domain'].'.vhost';
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);

Marius Burkard
committed
$vhost_symlink = $web_config['vhost_conf_enabled_dir'].'/100-'.$data['old']['domain'].'.vhost';
if(is_link($vhost_symlink)){
$app->system->unlink($vhost_symlink);
$app->log('Removing symlink: '.$vhost_symlink.'->'.$vhost_file, LOGLEVEL_DEBUG);
$app->system->unlink($vhost_file);
$app->log('Removing vhost file: '.$vhost_file, LOGLEVEL_DEBUG);
if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {

Marius Burkard
committed
$docroot = $data['old']['document_root'];
if($docroot != '' && !stristr($docroot, '..')) {
if($data['old']['type'] == 'vhost') {
// this is a vhost - we delete everything in here.

Marius Burkard
committed
$app->system->exec_safe('rm -rf ?', $docroot);
} elseif(!stristr($data['old']['web_folder'], '..')) {
// this is a vhost subdomain
// IMPORTANT: do some folder checks before we delete this!
$do_delete = true;
$delete_folder = preg_replace('/[\/]{2,}/', '/', $web_folder); // replace / occuring multiple times
if(substr($delete_folder, 0, 1) === '/') $delete_folder = substr($delete_folder, 1);
if(substr($delete_folder, -1) === '/') $delete_folder = substr($delete_folder, 0, -1);
$path_elements = explode('/', $delete_folder);
if($path_elements[0] == 'web' || $path_elements[0] === '') {
// paths beginning with /web should NEVER EVER be deleted, empty paths should NEVER occur - but for safety reasons we check it here!
// we use strict check as otherwise directories named '0' may not be deleted
$do_delete = false;
} else {
// read all vhost subdomains and alias with same parent domain
$tmp = $app->db->queryAllRecords("SELECT `web_folder` FROM web_domain WHERE (type = 'vhostsubdomain' OR type = 'vhostalias') AND parent_domain_id = ? AND domain_id != ?", $data['old']['parent_domain_id'], $data['old']['domain_id']);
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
foreach($tmp as $tmprec) {
// we normalize the folder entries because we need to compare them
$tmp_folder = preg_replace('/[\/]{2,}/', '/', $tmprec['web_folder']); // replace / occuring multiple times
if(substr($tmp_folder, 0, 1) === '/') $tmp_folder = substr($tmp_folder, 1);
if(substr($tmp_folder, -1) === '/') $tmp_folder = substr($tmp_folder, 0, -1);
// add this path and it's parent paths to used_paths array
while(strpos($tmp_folder, '/') !== false) {
if(in_array($tmp_folder, $used_paths) == false) $used_paths[] = $tmp_folder;
$tmp_folder = substr($tmp_folder, 0, strrpos($tmp_folder, '/'));
}
if(in_array($tmp_folder, $used_paths) == false) $used_paths[] = $tmp_folder;
}
unset($tmp);
// loop and check if the path is still used and stop at first used one
// set do_delete to false so nothing gets deleted if the web_folder itself is still used
$do_delete = false;
while(count($path_elements) > 0) {
$tmp_folder = implode('/', $path_elements);
if(in_array($tmp_folder, $used_paths) == true) break;
// this path is not used - set it as path to delete, strip the last element from the array and set do_delete to true
$delete_folder = $tmp_folder;
$do_delete = true;
array_pop($path_elements);
}
unset($tmp_folder);
unset($used_paths);
}

Marius Burkard
committed
if($do_delete === true && $delete_folder !== '') $app->system->exec_safe('rm -rf ?', $docroot.'/'.$delete_folder);
unset($delete_folder);
unset($path_elements);
}
}
//remove the php fastgi starter script if available
if ($data['old']['php'] == 'fast-cgi') {
$fastcgi_starter_path = str_replace('[system_user]', $data['old']['system_user'], $fastcgi_config['fastcgi_starter_path']);
if($data['old']['type'] == 'vhost') {
if (is_dir($fastcgi_starter_path)) {
$app->system->set_immutable($fastcgi_starter_path, false, true);

Marius Burkard
committed
$app->system->exec_safe('rm -rf ?', $fastcgi_starter_path);
}
} else {
$fcgi_starter_script = $fastcgi_starter_path.$fastcgi_config['fastcgi_starter_script'].'_web'.$data['old']['domain_id'];
if (file_exists($fcgi_starter_script)) {
$app->system->set_immutable($fcgi_starter_script, false);

Marius Burkard
committed
$app->system->exec_safe('rm -f ?', $fcgi_starter_script);
}
}
}
// remove PHP-FPM pool
if ($data['old']['php'] == 'php-fpm') {
$this->php_fpm_pool_delete($data, $web_config);
} elseif($data['old']['php'] == 'hhvm') {
$this->hhvm_update($data, $web_config);
}
//remove the php cgi starter script if available
if ($data['old']['php'] == 'cgi') {
// TODO: fetch the date from the server-settings
$web_config['cgi_starter_path'] = $web_config['website_basedir'].'/php-cgi-scripts/[system_user]/';
$cgi_starter_path = str_replace('[system_user]', $data['old']['system_user'], $web_config['cgi_starter_path']);
if($data['old']['type'] == 'vhost') {
if (is_dir($cgi_starter_path)) {
$app->system->set_immutable($cgi_starter_path, false, true);

Marius Burkard
committed
$app->system->exec_safe('rm -rf ?', $cgi_starter_path);
}
} else {
$cgi_starter_script = $cgi_starter_path.'php-cgi-starter_web'.$data['old']['domain_id'];
if (file_exists($cgi_starter_script)) {
$app->system->set_immutable($cgi_starter_script, false);

Marius Burkard
committed
$app->system->exec_safe('rm -f ?', $cgi_starter_script);
}
}
}
$app->log('Removing website: '.$docroot, LOGLEVEL_DEBUG);
// Delete the symlinks for the sites
$client = $app->db->queryOneRecord('SELECT client_id FROM sys_group WHERE sys_group.groupid = ?', $data['old']['sys_groupid']);
$client_id = intval($client['client_id']);
unset($client);
$tmp_symlinks_array = explode(':', $web_config['website_symlinks']);
if(is_array($tmp_symlinks_array)) {
foreach($tmp_symlinks_array as $tmp_symlink) {
$tmp_symlink = str_replace('[client_id]', $client_id, $tmp_symlink);
$tmp_symlink = str_replace('[website_domain]', $data['old']['domain'], $tmp_symlink);
// Remove trailing slash
if(substr($tmp_symlink, -1, 1) == '/') $tmp_symlink = substr($tmp_symlink, 0, -1);
// delete the symlink
if(is_link($tmp_symlink)) {
$app->system->unlink($tmp_symlink);
$app->log('Removing symlink: '.$tmp_symlink, LOGLEVEL_DEBUG);
}
}
}
// end removing symlinks
}
// Delete the log file directory

Marius Burkard
committed
$vhost_logfile_dir = '/var/log/ispconfig/httpd/'.$data['old']['domain'];
if($data['old']['domain'] != '' && !stristr($vhost_logfile_dir, '..')) $app->system->exec_safe('rm -rf ?', $vhost_logfile_dir);
$app->log('Removing website logfile directory: '.$vhost_logfile_dir, LOGLEVEL_DEBUG);
if($data['old']['type'] == 'vhost') {
//delete the web user

Marius Burkard
committed
$command = 'killall -u ? ; userdel ?';
$app->system->exec_safe($command, $data['old']['system_user'], $data['old']['system_user']);
if($apache_chrooted) $app->system->exec_safe('chroot ? ?', $web_config['website_basedir'], $command);
//* Remove the AWstats configuration file
if($data['old']['stats_type'] == 'awstats') {
$this->awstats_delete($data, $web_config);
}
if($data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
$app->system->web_folder_protection($parent_web_document_root, true);
}
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
//* Delete the web-backups
if($data['old']['type'] == 'vhost') {
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
$backup_dir = $server_config['backup_dir'];
$mount_backup = true;

Florian Schaal
committed
if($server_config['backup_dir'] != '' && $server_config['backup_delete'] == 'y') {
//* mount backup directory, if necessary
if( $server_config['backup_dir_is_mount'] == 'y' && !$app->system->mount_backup_dir($backup_dir) ) $mount_backup = false;
if($mount_backup){
$web_backup_dir = $backup_dir.'/web'.$data_old['domain_id'];
//** do not use rm -rf $web_backup_dir because database(s) may exits

Marius Burkard
committed
$app->system->exec_safe('rm -f ?*', $web_backup_dir.'/web'.$data_old['domain_id'].'_');
$sql = "DELETE FROM web_backup WHERE server_id = ? AND parent_domain_id = ? AND filename LIKE ?";
$app->db->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
if($app->db->dbHost != $app->dbmaster->dbHost) $app->dbmaster->query($sql, $conf['server_id'], $data_old['domain_id'], "web".$data_old['domain_id']."_%");
$app->log('Deleted the web backup files', LOGLEVEL_DEBUG);
if($data['old']['type'] != 'vhost') $app->system->web_folder_protection($data['old']['document_root'], true);
//* This function is called when a IP on the server is inserted, updated or deleted or when anon_ip setting is altered
function server_ip($event_name, $data) {
global $app, $conf;
// load the server configuration options
$app->uses('getconf');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$app->load('tpl');
$tpl = new tpl();
$tpl->newTemplate('apache_ispconfig.conf.master');
$tpl->setVar('apache_version', $app->system->getapacheversion());
$tpl->setVar('logging', $web_config['logging']);
$records = $app->db->queryAllRecords("SELECT * FROM server_ip WHERE server_id = ? AND virtualhost = 'y'", $conf['server_id']);
$records_out= array();
if(is_array($records)) {
foreach($records as $rec) {
if($rec['ip_type'] == 'IPv6') {
$ip_address = '['.$rec['ip_address'].']';
} else {
$ip_address = $rec['ip_address'];
}
$ports = explode(',', $rec['virtualhost_port']);
if(is_array($ports)) {
foreach($ports as $port) {
$port = intval($port);
if($port > 0 && $port < 65536 && $ip_address != '') {
$records_out[] = array('ip_address' => $ip_address, 'port' => $port);
}
}
}
}
}
if(count($records_out) > 0) {
$tpl->setLoop('ip_adresses', $records_out);

Marius Burkard
committed
$vhost_file = $web_config['vhost_conf_dir'].'/ispconfig.conf';
$app->system->file_put_contents($vhost_file, $tpl->grab());
$app->log('Writing the conf file: '.$vhost_file, LOGLEVEL_DEBUG);
unset($tpl);
//* Create or update the .htaccess folder protection
function web_folder_user($event_name, $data) {
if($event_name == 'web_folder_user_delete') {
$folder_id = $data['old']['web_folder_id'];
} else {
$folder_id = $data['new']['web_folder_id'];
}
$folder = $app->db->queryOneRecord("SELECT * FROM web_folder WHERE web_folder_id = ?", $folder_id);
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $folder['parent_domain_id']);
if(!is_array($folder) or !is_array($website)) {
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
if(substr($folder['path'], -1) == '/') $folder['path'] = substr($folder['path'], 0, -1);

Marius Burkard
committed
$folder_path = $website['document_root'].'/' . $web_folder . '/'.$folder['path'];
if(substr($folder_path, -1) != '/') $folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(stristr($folder_path, '..') || stristr($folder_path, './') || stristr($folder_path, '\\')) {
$app->log('Folder path "'.$folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
//* Create the folder path, if it does not exist

Dominik
committed
$app->system->mkdirpath($folder_path, 0755, $website['system_user'], $website['system_group']);
//* Create empty .htpasswd file, if it does not exist
if(!is_file($folder_path.'.htpasswd')) {
$app->system->touch($folder_path.'.htpasswd');
$app->system->chmod($folder_path.'.htpasswd', 0751);
$app->system->chown($folder_path.'.htpasswd', $website['system_user']);
$app->system->chgrp($folder_path.'.htpasswd', $website['system_group']);
$app->log('Created file '.$folder_path.'.htpasswd', LOGLEVEL_DEBUG);
if(($data['new']['username'] != $data['old']['username'] || $data['new']['active'] == 'n') && $data['old']['username'] != '') {
$app->system->removeLine($folder_path.'.htpasswd', $data['old']['username'].':');
$app->log('Removed user: '.$data['old']['username'], LOGLEVEL_DEBUG);
//* Add or remove the user from .htpasswd file
if($event_name == 'web_folder_user_delete') {
$app->system->removeLine($folder_path.'.htpasswd', $data['old']['username'].':');
$app->log('Removed user: '.$data['old']['username'], LOGLEVEL_DEBUG);
$app->system->replaceLine($folder_path.'.htpasswd', $data['new']['username'].':', $data['new']['username'].':'.$data['new']['password'], 0, 1);
$app->log('Added or updated user: '.$data['new']['username'], LOGLEVEL_DEBUG);
//if(!is_file($folder_path.'.htaccess')) {
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
$ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
if(file_exists($folder_path.'.htaccess')) {
$old_content = $app->system->file_get_contents($folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
$ht_file = str_replace($matches[0], $ht_file, $old_content);
} else {
$ht_file .= $old_content;
}
}
unset($old_content);
$app->system->file_put_contents($folder_path.'.htaccess', $ht_file);
$app->system->chmod($folder_path.'.htaccess', 0751);
$app->system->chown($folder_path.'.htaccess', $website['system_user']);
$app->system->chgrp($folder_path.'.htaccess', $website['system_group']);
$app->log('Created/modified file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
//* Remove .htaccess and .htpasswd file, when folder protection is removed
function web_folder_delete($event_name, $data) {
$folder_id = $data['old']['web_folder_id'];
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $folder['parent_domain_id']);
if(!is_array($folder) or !is_array($website)) {
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
if(substr($folder['path'], -1) == '/') $folder['path'] = substr($folder['path'], 0, -1);
$folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$folder['path']);
if(substr($folder_path, -1) != '/') $folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(substr($folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('Folder path is outside of docroot.', LOGLEVEL_DEBUG);
//* Remove .htpasswd file
if(is_file($folder_path.'.htpasswd')) {
$app->system->unlink($folder_path.'.htpasswd');
$app->log('Removed file '.$folder_path.'.htpasswd', LOGLEVEL_DEBUG);
//* Remove .htaccess file
if(is_file($folder_path.'.htaccess')) {
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
$ht_file = $app->system->file_get_contents($folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($folder_path.'.htaccess');
$app->log('Removed file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
//* Update folder protection, when path has been changed
function web_folder_update($event_name, $data) {
$website = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $data['new']['parent_domain_id']);
$app->log('Not able to retrieve folder or website record.', LOGLEVEL_DEBUG);
if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
if(substr($data['old']['path'], 0, 1) == '/') $data['old']['path'] = substr($data['old']['path'], 1);
if(substr($data['old']['path'], -1) == '/') $data['old']['path'] = substr($data['old']['path'], 0, -1);
$old_folder_path = realpath($website['document_root'].'/' . $web_folder . '/'.$data['old']['path']);
if(substr($old_folder_path, -1) != '/') $old_folder_path .= '/';
if(substr($data['new']['path'], 0, 1) == '/') $data['new']['path'] = substr($data['new']['path'], 1);
if(substr($data['new']['path'], -1) == '/') $data['new']['path'] = substr($data['new']['path'], 0, -1);

Marius Burkard
committed
$new_folder_path = $website['document_root'].'/' . $web_folder . '/'.$data['new']['path'];
if(substr($new_folder_path, -1) != '/') $new_folder_path .= '/';
//* Check if the resulting path is inside the docroot
if(stristr($new_folder_path, '..') || stristr($new_folder_path, './') || stristr($new_folder_path, '\\')) {
$app->log('Folder path "'.$new_folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
if(stristr($old_folder_path, '..') || stristr($old_folder_path, './') || stristr($old_folder_path, '\\')) {
$app->log('Folder path "'.$old_folder_path.'" contains .. or ./.', LOGLEVEL_DEBUG);
//* Check if the resulting path is inside the docroot
if(substr($old_folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('Old folder path '.$old_folder_path.' is outside of docroot.', LOGLEVEL_DEBUG);
if(substr($new_folder_path, 0, strlen($website['document_root'])) != $website['document_root']) {
$app->log('New folder path '.$new_folder_path.' is outside of docroot.', LOGLEVEL_DEBUG);
if(!is_dir($new_folder_path)) $app->system->mkdirpath($new_folder_path);
$begin_marker = '### ISPConfig folder protection begin ###';
$end_marker = "### ISPConfig folder protection end ###\n\n";
if($data['old']['path'] != $data['new']['path']) {
//* move .htpasswd file
if(is_file($old_folder_path.'.htpasswd')) {
$app->system->rename($old_folder_path.'.htpasswd', $new_folder_path.'.htpasswd');
$app->log('Moved file '.$old_folder_path.'.htpasswd to '.$new_folder_path.'.htpasswd', LOGLEVEL_DEBUG);
//* delete old .htaccess file
$ht_file = $app->system->file_get_contents($old_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$old_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($old_folder_path.'.htaccess');
$app->log('Removed file '.$old_folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($old_folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$old_folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
}
}
if($data['new']['active'] == 'y') {
$ht_file = $begin_marker."\nAuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user\n".$end_marker;
if(file_exists($new_folder_path.'.htaccess')) {
$old_content = $app->system->file_get_contents($new_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $old_content, $matches)) {
$ht_file = str_replace($matches[0], $ht_file, $old_content);
} else {
$ht_file .= $old_content;
}
}
$app->system->file_put_contents($new_folder_path.'.htaccess', $ht_file);
$app->system->chmod($new_folder_path.'.htaccess', 0751);
$app->system->chown($new_folder_path.'.htaccess', $website['system_user']);
$app->system->chgrp($new_folder_path.'.htaccess', $website['system_group']);
$app->log('Created/modified file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
//* Create empty .htpasswd file, if it does not exist
if(!is_file($folder_path.'.htpasswd')) {
$app->system->touch($new_folder_path.'.htpasswd');
$app->system->chmod($new_folder_path.'.htpasswd', 0751);
$app->system->chown($new_folder_path.'.htpasswd', $website['system_user']);
$app->system->chgrp($new_folder_path.'.htpasswd', $website['system_group']);
$app->log('Created file '.$new_folder_path.'.htpasswd', LOGLEVEL_DEBUG);
}
//* Remove .htaccess file
if($data['new']['active'] == 'n' && is_file($new_folder_path.'.htaccess')) {
$ht_file = $app->system->file_get_contents($new_folder_path.'.htaccess');
if(preg_match('/' . preg_quote($begin_marker, '/') . '(.*?)' . preg_quote($end_marker, '/') . '/s', $ht_file, $matches)) {
$ht_file = str_replace($matches[0], '', $ht_file);
} else {
$ht_file = str_replace("AuthType Basic\nAuthName \"Members Only\"\nAuthUserFile ".$new_folder_path.".htpasswd\nrequire valid-user", '', $ht_file);
}
if(trim($ht_file) == '') {
$app->system->unlink($new_folder_path.'.htaccess');
$app->log('Removed file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
} else {
$app->system->file_put_contents($new_folder_path.'.htaccess', $ht_file);
$app->log('Removed protection content from file '.$new_folder_path.'.htaccess', LOGLEVEL_DEBUG);
}
}
public function ftp_user_delete($event_name, $data) {
$ftpquota_file = $data['old']['dir'].'/.ftpquota';
if(file_exists($ftpquota_file)) $app->system->unlink($ftpquota_file);
/**
* This function is called when a Webdav-User is inserted, updated or deleted.
*
* @author Oliver Vogel
* @param string $event_name
* @param array $data
*/
public function webdav($event_name, $data) {
/*
* load the server configuration options
*/
$app->uses('getconf');
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
if (($event_name == 'webdav_user_insert') || ($event_name == 'webdav_user_update')) {
/*
* Get additional informations
*/
$sitedata = $app->db->queryOneRecord('SELECT document_root, domain, system_user, system_group FROM web_domain WHERE domain_id = ?', $data['new']['parent_domain_id']);
$documentRoot = $sitedata['document_root'];
$domain = $sitedata['domain'];
$user = $sitedata['system_user'];
$group = $sitedata['system_group'];
$webdav_user_dir = $documentRoot . '/webdav/' . $data['new']['dir'];
/* Check if this is a chrooted setup */
if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
$apache_chrooted = true;
$app->log('Info: Apache is chrooted.', LOGLEVEL_DEBUG);
} else {
$apache_chrooted = false;
}
//* We dont want to have relative paths here
if(stristr($webdav_user_dir, '..') || stristr($webdav_user_dir, './')) {
$app->log('Folder path '.$webdav_user_dir.' contains ./ or .. '.$documentRoot, LOGLEVEL_WARN);
//* Check if the resulting path exists if yes, if it is inside the docroot
if(is_dir($webdav_user_dir) && substr(realpath($webdav_user_dir), 0, strlen($documentRoot)) != $documentRoot) {
$app->log('Folder path '.$webdav_user_dir.' is outside of docroot '.$documentRoot, LOGLEVEL_WARN);
/*
* First the webdav-root - folder has to exist
*/
if(!is_dir($webdav_user_dir)) {
$app->log('Webdav User directory '.$webdav_user_dir.' does not exist. Creating it now.', LOGLEVEL_DEBUG);
$app->system->mkdirpath($webdav_user_dir);
* The webdav - Root needs the group/user as owner and the apache as read and write
$app->system->chown($documentRoot . '/webdav', $user);
$app->system->chgrp($documentRoot . '/webdav', $group);
$app->system->chmod($documentRoot . '/webdav', 0770);
* The webdav folder (not the webdav-root!) needs the same (not in ONE step, because the
* pwd-files are owned by root)
$app->system->chown($webdav_user_dir, $user);
$app->system->chgrp($webdav_user_dir, $group);
$app->system->chmod($webdav_user_dir, 0770);
/*
* if the user is active, we have to write/update the password - file
* if the user is inactive, we have to inactivate the user by removing the user from the file
*/
if ($data['new']['active'] == 'y') {
$this->_writeHtDigestFile( $webdav_user_dir . '.htdigest', $data['new']['username'], $data['new']['dir'], $data['new']['password']);
}
else {
/* empty pwd removes the user! */
$this->_writeHtDigestFile( $webdav_user_dir . '.htdigest', $data['new']['username'], $data['new']['dir'], '');
/*
* Next step, patch the vhost - file
*/

Marius Burkard
committed
$vhost_file = $web_config['vhost_conf_dir'] . '/' . $domain . '.vhost';
$this->_patchVhostWebdav($vhost_file, $documentRoot . '/webdav');
/*
* Last, restart apache
*/
if($apache_chrooted) {
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
}
}
if ($event_name == 'webdav_user_delete') {
/*
* Get additional informations
*/
$sitedata = $app->db->queryOneRecord('SELECT document_root, domain FROM web_domain WHERE domain_id = ?', $data['old']['parent_domain_id']);
$documentRoot = $sitedata['document_root'];
/*
* We dont't want to destroy any (transfer)-Data. So we do NOT delete any dir.
* So the only thing, we have to do, is to delete the user from the password-file
$this->_writeHtDigestFile( $documentRoot . '/webdav/' . $data['old']['dir'] . '.htdigest', $data['old']['username'], $data['old']['dir'], '');
/*
* Next step, patch the vhost - file
*/

Marius Burkard
committed
$vhost_file = $web_config['vhost_conf_dir'] . '/' . $domain . '.vhost';
$this->_patchVhostWebdav($vhost_file, $documentRoot . '/webdav');
/*
* Last, restart apache
*/
if($apache_chrooted) {
$app->services->restartServiceDelayed('httpd', 'restart');
} else {
// request a httpd reload when all records have been processed
$app->services->restartServiceDelayed('httpd', 'reload');
}
}
/**
* This function writes the htdigest - files used by webdav and digest
* more info: see http://riceball.com/d/node/424
* @author Oliver Vogel
* @param string $filename The name of the digest-file
* @param string $username The name of the webdav-user
* @param string $authname The name of the realm
* @param string $pwd The password-hash of the user
private function _writeHtDigestFile($filename, $username, $authname, $pwdhash ) {
if(is_file($filename) && !is_link($filename)) {
$in = fopen($filename, 'r');
$output = '';
/*
* read line by line and search for the username and authname
*/
while (preg_match("/:/", $line = fgets($in))) {
$line = rtrim($line);
$tmp = explode(':', $line);
if ($tmp[0] == $username && $tmp[1] == $authname) {
/*
* found the user. delete or change it?
*/
if ($pwdhash != '') {
$output .= $tmp[0] . ':' . $tmp[1] . ':' . $pwdhash . "\n";
$changed = true;
}
else {
$output .= $line . "\n";
}
fclose($in);
}
/*
* if we didn't change anything, we have to add the new user at the end of the file
*/
if (!$changed) {
$output .= $username . ':' . $authname . ':' . $pwdhash . "\n";
/*
* Now lets write the new file
*/
$app->system->unlink($filename);
$app->system->file_put_contents($filename, $output);
}
/**
* This function patches the vhost-file and adds all webdav - user.
* This function is written, because the creation of the vhost - file is sophisticated and
* i don't want to make it more "heavy" by also adding this code too...
* @author Oliver Vogel
* @param string $fileName The Name of the .vhost-File (path included)
* @param string $webdavRoot The root of the webdav-folder
*/
private function _patchVhostWebdav($fileName, $webdavRoot) {
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
$in = fopen($fileName, 'r');
$output = '';
$inWebdavSection = false;
/*
* read line by line and search for the username and authname
*/
while ($line = fgets($in)) {
/*
* is the "replace-comment" found...
*/
if (trim($line) == '# WEBDAV BEGIN') {
/*
* The begin of the webdav - section is found, so ignore all lines til the end is found
*/
$inWebdavSection = true;
$output .= " # WEBDAV BEGIN\n";
/*
* add all the webdav-dirs to the webdav-section
*/

tbrehm
committed
$files = @scandir($webdavRoot);
if(is_array($files)) {
foreach($files as $file) {
if (substr($file, strlen($file) - strlen('.htdigest')) == '.htdigest' && preg_match("/^[a-zA-Z0-9\-_\.]*$/", $file)) {
/*
* found a htdigest - file, so add it to webdav
*/
$fn = substr($file, 0, strlen($file) - strlen('.htdigest'));
$output .= "\n";
// $output .= " Alias /" . $fn . ' ' . $webdavRoot . '/' . $fn . "\n";
// $output .= " <Location /" . $fn . ">\n";
$output .= " Alias /webdav/" . $fn . ' ' . $webdavRoot . '/' . $fn . "\n";
$output .= " <Location /webdav/" . $fn . ">\n";
$output .= " DAV On\n";
$output .= ' BrowserMatch "MSIE" AuthDigestEnableQueryStringHack=On'."\n";
$output .= " AuthType Digest\n";
if($fn != '' && $fn != '/') {
$output .= " AuthName \"" . $fn . "\"\n";
} else {
$output .= " AuthName \"Restricted Area\"\n";
}
$output .= " AuthUserFile " . $webdavRoot . '/' . $file . "\n";
$output .= " Require valid-user \n";
$output .= " Options +Indexes \n";
$output .= " Order allow,deny \n";
$output .= " Allow from all \n";
$output .= " </Location> \n";
}