Exemple #1
0
function buildConf($url, $data)
{
    global $proto, $debug, $purge;
    if ($data['dryrun'] && $debug) {
        echo fM("Dryrun mode (nothing will be written to disk)\n\n");
    }
    $raw_data = getPostData($url, $data);
    $raw_data = $data['compress'] ? @unserialize(gzuncompress($raw_data)) : @unserialize($raw_data);
    if (!is_array($raw_data)) {
        if ($debug) {
            echo fM($raw_data);
        }
        addLogEntry($raw_data);
        exit(1);
    }
    extract($raw_data, EXTR_SKIP);
    if (dirname($server_chroot_dir)) {
        $server_root_dir = $server_chroot_dir . $server_root_dir;
        $server_zones_dir = $server_chroot_dir . $server_zones_dir;
        $server_config_file = $server_chroot_dir . $server_config_file;
        foreach ($files as $filename => $contents) {
            $new_files[$server_chroot_dir . $filename] = $contents;
        }
        $files = $new_files;
        unset($new_files);
    }
    if ($debug) {
        foreach ($files as $filename => $contents) {
            echo str_repeat('=', 50) . "\n";
            echo $filename . ":\n";
            echo str_repeat('=', 50) . "\n";
            echo $contents . "\n\n";
        }
    }
    $runas = $server_run_as_predefined == 'as defined:' ? $server_run_as : $server_run_as_predefined;
    $chown_files = array($server_root_dir, $server_zones_dir);
    /** Remove previous files so there are no stale files */
    if ($purge || $purge_config_files == 'yes' && $server_update_config == 'conf') {
        /** Server config files */
        $path_parts = pathinfo($server_config_file);
        if (version_compare(PHP_VERSION, '5.2.0', '<')) {
            $path_parts['filename'] = str_replace('.' . $path_parts['extension'], '', $path_parts['basename']);
        }
        $config_file_pattern = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $path_parts['filename'] . '.*';
        exec('ls ' . $config_file_pattern, $config_file_match);
        foreach ($config_file_match as $config_file) {
            deleteFile($config_file, $debug, $data['dryrun']);
        }
        /** Zone files */
        deleteFile($server_zones_dir, $debug, $data['dryrun']);
    }
    /** Install the new files */
    installFiles($runas, $chown_files, $files, $data['dryrun']);
    /** Reload the server */
    $message = "Reloading the server\n";
    if ($debug) {
        echo fM($message);
    }
    if (!$data['dryrun']) {
        addLogEntry($message);
        if (shell_exec('ps -A | grep named | grep -vc grep') > 0) {
            $rndc_actions = array('freeze', 'reload', 'thaw');
            /** Handle dynamic zones to support reloading */
            foreach ($rndc_actions as $action) {
                $last_line = system(findProgram('rndc') . " {$action} 2>&1", $retval);
                if ($retval) {
                    return processReloadFailure($last_line);
                }
            }
        } else {
            $message = "The server is not running - attempting to start it\n";
            if ($debug) {
                echo fM($message);
            }
            addLogEntry($message);
            $named_rc_script = getStartupScript();
            if ($named_rc_script === false) {
                $last_line = "Cannot locate the start script\n";
                $retval = true;
            } else {
                $last_line = system($named_rc_script . ' 2>&1', $retval);
            }
        }
        if ($retval) {
            return processReloadFailure($last_line);
        } else {
            /** Only update reloaded zones */
            $data['reload_domain_ids'] = $reload_domain_ids;
            if (!isset($server_build_all)) {
                $data['zone'] = 'update';
            }
            /** Update the server with a successful reload */
            $data['action'] = 'update';
            $raw_update = getPostData($url, $data);
            $raw_update = $data['compress'] ? @unserialize(gzuncompress($raw_update)) : @unserialize($raw_update);
            if ($debug) {
                echo $raw_update;
            }
        }
    }
    return true;
}
Exemple #2
0
function buildConf($url, $data)
{
    global $proto, $debug;
    if ($data['dryrun'] && $debug) {
        echo fM("Dryrun mode (nothing will be written to disk)\n\n");
    }
    $raw_data = getPostData($url, $data);
    $raw_data = $data['compress'] ? @unserialize(gzuncompress($raw_data)) : @unserialize($raw_data);
    if (!is_array($raw_data)) {
        if ($debug) {
            echo fM($raw_data);
        }
        addLogEntry($raw_data);
        exit(1);
    }
    if ($debug) {
        foreach ($raw_data['files'] as $filename => $contents) {
            echo str_repeat('=', 50) . "\n";
            echo $filename . ":\n";
            echo str_repeat('=', 50) . "\n";
            echo $contents . "\n\n";
        }
    }
    extract($raw_data, EXTR_SKIP);
    $runas = 'root';
    $chown_files = array($server_root_dir);
    /** Install the new files */
    installFiles($runas, $chown_files, $files, $data['dryrun']);
    $message = "Reloading the server\n";
    if ($debug) {
        echo fM($message);
    }
    $rc_script = str_replace('__FILE__', $server_config_file, getStartupScript($server_type));
    $message = "{$rc_script}\n";
    if ($debug) {
        echo fM($message);
    }
    if (!$data['dryrun']) {
        addLogEntry($message);
        $rc_script = str_replace('__FILE__', $server_config_file, getStartupScript($server_type));
        if ($rc_script === false) {
            $last_line = "Cannot locate the start script\n";
            if ($debug) {
                echo fM($last_line);
            }
            addLogEntry($last_line);
            $retval = true;
        } else {
            $last_line = system($rc_script . ' 2>&1', $retval);
            addLogEntry($last_line);
        }
        if ($retval) {
            $message = "There was an error reloading the firewall - please check the logs for details\n";
            if ($debug) {
                echo fM($message);
            }
            addLogEntry($message);
            return false;
        } else {
            /** Update the server with a successful reload */
            $data['action'] = 'update';
            $raw_update = getPostData($url, $data);
            $raw_update = $data['compress'] ? @unserialize(gzuncompress($raw_update)) : @unserialize($raw_update);
        }
    }
    return true;
}