All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ $nocsrf = true; require("globals.inc"); require("guiconfig.inc"); define('PC_SYSINSTALL', '/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh'); if($g['platform'] == "pfSense" or $g['platform'] == "nanobsd") { header("Location: /"); exit; } // Main switch dispatcher switch ($_REQUEST['state']) { case "update_installer_status": update_installer_status(); exit; case "custominstall": installer_custom(); exit; case "begin_install": installing_gui(); begin_install(); exit; case "verify_before_install": verify_before_install(); exit; case "easy_install_ufs": easy_install("UFS+S"); exit; case "easy_install_ufs": easy_install("ZFS"); exit; default: installer_main(); } function easy_install($fstype = "UFS+S") { // Calculate swap and disk sizes $disks = installer_find_all_disks(); $memory = get_memory(); $swap_size = $memory[0] * 2; $first_disk = trim(installer_find_first_disk()); $disk_info = pcsysinstall_get_disk_info($first_disk); $size = $disk_info['size']; $first_disk_size = $size - $swap_size; $disk_setup = array(); $tmp_array = array(); // Build the disk layout for / $tmp_array['disk'] = $first_disk; $tmp_array['size'] = $first_disk_size; $tmp_array['mountpoint'] = "/"; $tmp_array['fstype'] = $fstype; $disk_setup[] = $tmp_array; unset($tmp_array); $tmp_array = array(); // Build the disk layout for SWAP $tmp_array['disk'] = $first_disk; $tmp_array['size'] = $swap_size; $tmp_array['mountpoint'] = "none"; $tmp_array['fstype'] = "SWAP"; $disk_setup[] = $tmp_array; unset($tmp_array); $bootmanager = "bsd"; file_put_contents("/tmp/webInstaller_disk_layout.txt", serialize($disk_setup)); file_put_contents("/tmp/webInstaller_disk_bootmanager.txt", serialize($bootmanager)); header("Location: installer.php?state=verify_before_install"); exit; } function write_out_pc_sysinstaller_config($disks, $bootmanager = "bsd") { $diskareas = ""; $fd = fopen("/usr/sbin/pc-sysinstall/examples/pfSense-install.cfg", "w"); if(!$fd) return true; if($bootmanager == "") $bootmanager = "none"; // Yes, -1. We ++ early in loop. $numdisks = -1; $lastdisk = ""; $diskdefs = ""; // Run through the disks and create the conf areas for pc-sysinstaller foreach($disks as $disksa) { $fstype = $disksa['fstype']; $size = $disksa['size']; $mountpoint = $disksa['mountpoint']; $disk = $disksa['disk']; if($disk <> $lastdisk) { $lastdisk = $disk; $numdisks++; $diskdefs .= "# disk {$disk}\n"; $diskdefs .= "disk{$numdisks}={$disk}\n"; $diskdefs .= "partition=all\n"; $diskdefs .= "bootManager={$bootmanager}\n"; $diskdefs .= "commitDiskPart\n\n"; } $diskareas .= "disk{$numdisks}-part={$fstype} {$size} {$mountpoint} \n"; if($encpass) $diskareas .= "encpass={$encpass}\n"; } $config = <</dev/null\n"); fwrite($fd, "/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh -c /usr/sbin/pc-sysinstall/examples/pfSense-install.cfg \n"); fwrite($fd, "/bin/chmod a+rx /usr/local/bin/after_installation_routines.sh\n"); fwrite($fd, "cd / && /usr/local/bin/after_installation_routines.sh\n"); fwrite($fd, "/bin/mkdir /mnt/tmp\n"); fwrite($fd, "/usr/bin/touch /tmp/install_complete\n"); fclose($fd); exec("/bin/chmod a+rx /tmp/installer.sh"); mwexec_bg("/bin/sh /tmp/installer.sh"); } function installer_find_first_disk() { global $g, $fstype, $savemsg; $disk = `/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh disk-list | head -n1 | cut -d':' -f1`; return trim($disk); } function pcsysinstall_get_disk_info($diskname) { global $g, $fstype, $savemsg; $disk = explode("\n", `/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh disk-list`); $disks_array = array(); foreach($disk as $d) { $disks_info = explode(":", $d); $tmp_array = array(); if($disks_info[0] == $diskname) { $disk_info = explode("\n", `/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh disk-info {$disks_info[0]}`); $disk_info_split = explode("=", $disk_info); foreach($disk_info as $di) { $di_s = explode("=", $di); if($di_s[0]) $tmp_array[$di_s[0]] = $di_s[1]; } $tmp_array['size']--; $tmp_array['disk'] = trim($disks_info[0]); $tmp_array['desc'] = trim(htmlentities($disks_info[1])); return $tmp_array; } } } // Return an array with all disks information. function installer_find_all_disks() { global $g, $fstype, $savemsg; $disk = explode("\n", `/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh disk-list`); $disks_array = array(); foreach($disk as $d) { if(!$d) continue; $disks_info = explode(":", $d); $tmp_array = array(); $disk_info = explode("\n", `/usr/sbin/pc-sysinstall/pc-sysinstall/pc-sysinstall.sh disk-info {$disks_info[0]}`); foreach($disk_info as $di) { $di_s = explode("=", $di); if($di_s[0]) $tmp_array[$di_s[0]] = $di_s[1]; } $tmp_array['size']--; $tmp_array['disk'] = trim($disks_info[0]); $tmp_array['desc'] = trim(htmlentities($disks_info[1])); $disks_array[] = $tmp_array; } return $disks_array; } function update_installer_status() { global $g, $fstype, $savemsg; // Ensure status files exist if(!file_exists("/tmp/installer_installer_running")) touch("/tmp/installer_installer_running"); $status = `cat /tmp/.pc-sysinstall/pc-sysinstall.log`; $status = str_replace("\n", "\\n", $status); $status = str_replace("\n", "\\r", $status); $status = str_replace("'", "\\'", $status); echo "document.forms[0].installeroutput.value='$status';\n"; echo "document.forms[0].installeroutput.scrollTop = document.forms[0].installeroutput.scrollHeight;\n"; // Find out installer progress $progress = "5"; if(strstr($status, "Running: dd")) $progress = "6"; if(strstr($status, "Running: gpart create -s GPT")) $progress = "7"; if(strstr($status, "Running: gpart bootcode")) $progress = "7"; if(strstr($status, "Running: newfs -U")) $progress = "8"; if(strstr($status, "Running: sync")) $progress = "9"; if(strstr($status, "/boot /mnt/boot")) $progress = "10"; if(strstr($status, "/COPYRIGHT /mnt/COPYRIGHT")) $progress = "11"; if(strstr($status, "/bin /mnt/bin")) $progress = "12"; if(strstr($status, "/conf /mnt/conf")) $progress = "15"; if(strstr($status, "/conf.default /mnt/conf.default")) $progress = "20"; if(strstr($status, "/dev /mnt/dev")) $progress = "25"; if(strstr($status, "/etc /mnt/etc")) $progress = "30"; if(strstr($status, "/home /mnt/home")) $progress = "35"; if(strstr($status, "/kernels /mnt/kernels")) $progress = "40"; if(strstr($status, "/libexec /mnt/libexec")) $progress = "50"; if(strstr($status, "/lib /mnt/lib")) $progress = "60"; if(strstr($status, "/root /mnt/root")) $progress = "70"; if(strstr($status, "/sbin /mnt/sbin")) $progress = "75"; if(strstr($status, "/sys /mnt/sys")) $progress = "80"; if(strstr($status, "/usr /mnt/usr")) $progress = "95"; if(strstr($status, "/usr /mnt/usr")) $progress = "90"; if(strstr($status, "/var /mnt/var")) $progress = "95"; if(strstr($status, "cap_mkdb /etc/login.conf")) $progress = "96"; if(strstr($status, "Setting hostname")) $progress = "97"; if(strstr($status, "umount -f /mnt")) $progress = "98"; if(strstr($status, "umount -f /mnt")) $progress = "99"; if(strstr($status, "Installation finished")) $progress = "100"; // Check for error and bail if we see one. if(stristr($status, "error")) { $error = true; echo "\$('#installerrunning').html(' An error occurred. Aborting installation. Back to webInstaller'); "; echo "\$('#progressbar').css('width','100%');\n"; unlink_if_exists("/tmp/install_complete"); return; } $running_old = trim(file_get_contents("/tmp/installer_installer_running")); if($installer_running <> "running") { $ps_running = exec("/bin/ps awwwux | /usr/bin/grep -v grep | /usr/bin/grep 'sh /tmp/installer.sh'"); if($ps_running) { $running = "\$('#installerrunning').html('
 Installer running ({$progress}% completed)...
'); "; if($running_old <> $running) { echo $running; file_put_contents("/tmp/installer_installer_running", "$running"); } } } if($progress) echo "\$('#progressbar').css('width','{$progress}%');\n"; if(file_exists("/tmp/install_complete")) { echo "\$('#installerrunning').html(' Installation completed. Please reboot to continue');\n"; echo "\$('#pbdiv').fadeOut();\n"; unlink_if_exists("/tmp/installer.sh"); file_put_contents("/tmp/installer_installer_running", "finished"); } } function update_installer_status_win($status) { global $g, $fstype, $savemsg; echo "\n"; } function begin_install() { global $g, $savemsg; if(file_exists("/tmp/install_complete")) return; unlink_if_exists("/tmp/install_complete"); update_installer_status_win(sprintf(gettext("Beginning installation on disk %s."),$disk)); start_installation(); } function head_html() { global $g, $fstype, $savemsg; echo << EOF; } function body_html() { global $g, $fstype, $savemsg; $pgtitle = array("{$g['product_name']}", gettext("Installer")); include("head.inc"); echo << EOF; if($one_two) echo "

{$pgtitle}

"; if ($savemsg) print_info_box($savemsg); } function end_html() { global $g, $fstype, $savemsg; echo ""; echo ""; echo ""; } function template() { global $g, $fstype, $savemsg; head_html(); body_html(); echo <<
EOF; end_html(); } function verify_before_install() { global $g, $fstype, $savemsg; $encrypted_root = false; $non_encrypted_boot = false; $non_encrypted_notice = false; head_html(); body_html(); page_table_start($g['product_name'] . " installer - Verify final installation settings"); // If we are visiting this step from anything but the row editor / custom install // then load the on disk layout contents if they are available. if(!$_REQUEST['fstype0'] && file_exists("/tmp/webInstaller_disk_layout.txt")) { $disks = unserialize(file_get_contents("/tmp/webInstaller_disk_layout.txt")); $bootmanager = unserialize(file_get_contents("/tmp/webInstaller_disk_bootmanager.txt")); $restored_layout_from_file = true; $restored_layout_txt = "The previous disk layout was restored from disk"; } else { $disks = array(); } if(!$bootmanager) $bootmanager = $_REQUEST['bootmanager']; // echo "\n\n"; $disk = pcsysinstall_get_disk_info(htmlspecialchars($_REQUEST['disk'])); $disksize = format_bytes($disk['size'] * 1048576); // Loop through posted items and create an array for($x=0; $x<99; $x++) { // XXX: Make this more optimal if(!$_REQUEST['fstype' . $x]) continue; $tmparray = array(); if($_REQUEST['fstype' . $x] <> "SWAP") { $tmparray['mountpoint'] = $_REQUEST['mountpoint' . $x]; // Check for encrypted slice / if(stristr($_REQUEST['fstype' . $x], ".eli")) { if($tmparray['mountpoint'] == "/") $encrypted_root = true; } // Check if we have a non-encrypted /boot if($tmparray['mountpoint'] == "/boot") { if(!stristr($_REQUEST['fstype' . $x], ".eli")) $non_encrypted_boot = true; } if($tmparray['mountpoint'] == "/conf") { $tmparray['mountpoint'] = "/conf{$x}"; $error_txt[] = "/conf is not an allowed mount point and has been renamed to /conf{$x}."; } } else { $tmparray['mountpoint'] = "none"; } // If we have an encrypted /root and lack a non encrypted /boot, throw an error/warning if($encrypted_root && !$non_encrypted_boot && !$non_encrypted_notice) { $error_txt[] = "A non-encrypted /boot slice is required when encrypting the / slice"; $non_encrypted_notice = true; } $tmparray['disk'] = $_REQUEST['disk' . $x]; $tmparray['fstype'] = $_REQUEST['fstype' . $x]; $tmparray['size'] = $_REQUEST['size' . $x]; $tmparray['encpass'] = $_REQUEST['encpass' . $x]; $disks[] = $tmparray; } // echo "\n \n"; $bootmanagerupper = strtoupper($bootmanager); echo <<
EOFAMBAC; // If errors are found, throw the big red box. if ($error_txt) { echo ""; echo ""; } else echo ""; echo << EOFAMBACBAF; foreach($disks as $disk) { $desc = pcsysinstall_get_disk_info($disk['disk']); echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo <<
 
"; print_input_errors($error_txt); echo "
 
Boot manager: {$bootmanagerupper}
 
Mount point Filesysytem type Disk Size Encryption password

   " . htmlspecialchars($disk['mountpoint']) . "" . htmlspecialchars($disk['fstype']) . "" . htmlspecialchars($disk['disk']) . " " . htmlspecialchars($desc['desc']) . "" . htmlspecialchars($disk['size']) . "" . htmlspecialchars($disk['encpass']) . "

   EOFAMB; if(!$error_txt) echo "
 "; echo <<

EOFAMBASDF; page_table_end(); end_html(); write_out_pc_sysinstaller_config($disks, $bootmanager); // Serialize layout to disk so it can be read in later. file_put_contents("/tmp/webInstaller_disk_layout.txt", serialize($disks)); file_put_contents("/tmp/webInstaller_disk_bootmanager.txt", serialize($bootmanager)); } function installing_gui() { global $g, $fstype, $savemsg; head_html(); body_html(); echo ""; page_table_start(); echo <<
 Starting Installer... Please wait...


EOF; page_table_end(); end_html(); } function page_table_start($pgtitle = "") { global $g, $fstype, $savemsg; if($pgtitle == "") $pgtitle = "{$g['product_name']} installer"; echo <<
{$pgtitle}
EOF; } function page_table_end() { global $g, $fstype, $savemsg; echo <<
EOF; } function installer_custom() { global $g, $fstype, $savemsg; global $select_txt, $custom_disks; if(file_exists("/tmp/.pc-sysinstall/pc-sysinstall.log")) unlink("/tmp/.pc-sysinstall/pc-sysinstall.log"); $disks = installer_find_all_disks(); // Pass size of disks down to javascript. $disk_sizes_js_txt = "var disk_sizes = new Array();\n"; foreach($disks as $disk) $disk_sizes_js_txt .= "disk_sizes['{$disk['disk']}'] = '{$disk['size']}';\n"; head_html(); body_html(); page_table_start($g['product_name'] . " installer - Customize disk(s) layout"); echo << Array.prototype.in_array = function(p_val) { for(var i = 0, l = this.length; i < l; i++) { if(this[i] == p_val) { return true; } } return false; } function row_helper_dynamic_custom() { var totalsize = 0; {$disk_sizes_js_txt} // Run through all rows and process data for(var x = 0; x<99; x++) { //optimize me better if(\$('#fstype' + x).length) { if(\$('#size' + x).val() == '') \$('#size' + x).val(disk_sizes[\$('disk' + x).value]); var fstype = \$('#fstype' + x).val(); if(fstype.substring(fstype.length - 4) == ".eli") { \$('#encpass' + x).prop('disabled',false); if(!encryption_warning_shown) { alert('NOTE: If you define a disk encryption password you will need to enter it on *EVERY* bootup!'); encryption_warning_shown = true; } } else { \$('#encpass' + x).prop('disabled',true); } } // Calculate size allocations if(\$('#size' + x).length) { if(parseInt($('#size' + x).val()) > 0) totalsize += parseInt($('#size' + x).val()); } } // If the totalsize element exists, set it and disable if(\$('#totalsize').length) { if(\$('#totalsize').val() != totalsize) { // When size allocation changes, draw attention. jQuery('#totalsize').effect('highlight'); \$('#totalsize').val(totalsize); } \$('#totalsize').prop('disabled',true); } if(\$('#disktotals').length) { var disks_seen = new Array(); var tmp_sizedisks = 0; var disksseen = 0; for(var xx = 0; xx<99; xx++) { if(\$('#disk' + xx).length) { if(!disks_seen.in_array(\$('#disk' + xx).val())) { tmp_sizedisks += parseInt(disk_sizes[\$('#disk' + xx).val()]); disks_seen[disksseen] = \$('#disk' + xx).val(); disksseen++; } } \$('#disktotals').val(tmp_sizedisks); \$('#disktotals').prop('disabled',true); \$('#disktotals').css('color','#000000'); var remaining = parseInt(\$('#disktotals').val()) - parseInt(\$('#totalsize').val()); if(remaining == 0) { if(\$('#totalsize').length) \$('#totalsize').css({ 'background':'#00FF00', 'color':'#000000' }); } else { if(\$('#totalsize').length) \$('#totalsize').css({ 'background':'#FFFFFF', 'color':'#000000' }); } if(parseInt(\$('#totalsize').val()) > parseInt(\$('#disktotals').val())) { if(\$('#totalsize')) \$('#totalsize').css({ 'background':'#FF0000', 'color':'#000000' }); } if(\$('#availalloc').length) { \$('#availalloc').prop('disabled',true); \$('#availalloc').val(remaining); \$('#availalloc').css({ 'background':'#FFFFFF', 'color':'#000000' }); } } } }
 Probing disks, please wait...
EOF; ob_flush(); // Read bootmanager setting from disk if found if(file_exists("/tmp/webInstaller_disk_bootmanager.txt")) $bootmanager = unserialize(file_get_contents("/tmp/webInstaller_disk_bootmanager.txt")); if($bootmanager == "none") $noneselected = " SELECTED"; if($bootmanager == "bsd") $bsdeselected = " SELECTED"; if(!$disks) { $custom_txt = gettext("ERROR: Could not find any suitable disks for installation."); } else { // Prepare disk selection dropdown $custom_txt = <<
Boot manager:

EOF; // Calculate swap disk sizes $memory = get_memory(); $swap_size = $memory[0] * 2; $first_disk = trim(installer_find_first_disk()); $disk_info = pcsysinstall_get_disk_info($first_disk); $size = $disk_info['size']; $first_disk_size = $size - $swap_size; // Debugging // echo "\n\n\n\n"; // Check to see if a on disk layout exists if(file_exists("/tmp/webInstaller_disk_layout.txt")) { $disks_restored = unserialize(file_get_contents("/tmp/webInstaller_disk_layout.txt")); $restored_layout_from_file = true; $restored_layout_txt = "
* The previous disk layout was restored from a previous session"; } // If we restored disk layout(s) from a file then build the rows if($restored_layout_from_file == true) { $diskcounter = 0; foreach($disks_restored as $dr) { $custom_txt .= return_rowhelper_row("$diskcounter", $dr['mountpoint'], $dr['fstype'], $dr['disk'], $dr['size'], $dr['encpass']); $diskcounter++; } } else { // Construct the default rows that outline the disks configuration. $custom_txt .= return_rowhelper_row("0", "/", "UFS+S", $first_disk, "{$first_disk_size}", ""); $custom_txt .= return_rowhelper_row("1", "none", "SWAP", $first_disk, "$swap_size", ""); } // tfoot and tbody are used by rowhelper $custom_txt .= ""; $custom_txt .= ""; // Total allocation box $custom_txt .= ""; // Add row button $custom_txt .= ""; // Disk capacity box $custom_txt .= ""; // Remaining allocation box $custom_txt .= ""; $custom_txt .= "
Mount Filesysytem Disk Size Encryption password  
Total allocated: "; $custom_txt .= "
"; $custom_txt .= ""; $custom_txt .= "\"\""; $custom_txt .= "
"; $custom_txt .= "
Disk(s) capacity total:
Available space for allocation:
"; $custom_txt .= ""; } echo <<
NOTES:
* Sizes are in megabytes.
* Mount points named /conf are not allowed. Use /cf if you want to make a configuration slice/mount. {$restored_layout_txt}
EOF; page_table_end(); end_html(); } function installer_main() { global $g, $fstype, $savemsg; if(file_exists("/tmp/.pc-sysinstall/pc-sysinstall.log")) unlink("/tmp/.pc-sysinstall/pc-sysinstall.log"); head_html(); body_html(); $disk = installer_find_first_disk(); // Only enable ZFS if this exists. The install will fail otherwise. if(file_exists("/boot/gptzfsboot")) $zfs_enabled = "Easy installation of {$g['product_name']} using the ZFS filesystem on disk {$disk}"; page_table_start(); echo <<
Welcome to the {$g['product_name']} webInstaller!

This utility will install {$g['product_name']} to a hard disk, flash drive, etc.

Please select an installer option to begin:

EOF; if(!$disk) { echo gettext("ERROR: Could not find any suitable disks for installation."); echo "

"; end_html(); exit; } echo << Easy installation of {$g['product_name']} using the UFS filesystem on disk {$disk} {$zfs_enabled} Custom installation of {$g['product_name']} Cancel and return to Dashboard EOF; page_table_end(); end_html(); } function return_rowhelper_row($rownum, $mountpoint, $fstype, $disk, $size, $encpass) { global $g, $select_txt, $custom_disks, $savemsg; $release = php_uname("r"); $release = trim($release[0]); // Mount point $disks = installer_find_all_disks(); $custom_txt .= ""; $custom_txt .= ""; // Filesystem type array $types = array( 'UFS' => 'UFS', 'UFS+S' => 'UFS + Softupdates', 'UFS.eli' => 'Encrypted UFS', 'UFS+S.eli' => 'Encrypted UFS + Softupdates', 'SWAP' => 'SWAP' ); // UFS + Journaling was introduced in 9.0 if($release == "9") { $types['UFS+J'] = "UFS + Journaling"; $types['UFS+J.eli'] = "Encrypted UFS + Journaling"; } // Add ZFS Boot loader if it exists if(file_exists("/boot/gptzfsboot")) { $types['ZFS'] = "Zetabyte Filesystem"; $types['ZFS.eli'] = "Encrypted Zetabyte Filesystem"; } // fstype form field $custom_txt .= "\n"; $custom_txt .= ""; // Disk selection form field $custom_txt .= "\n"; // Slice size $custom_txt .= ""; // Encryption password $custom_txt .= ""; $custom_txt .= ""; $custom_txt .= ""; // Add Rowhelper + button if($rownum > 0) $custom_txt .= "\"\""; $custom_txt .= ""; return $custom_txt; } ?>