#!/usr/bin/perl -w ##=====================================================================## ## Copyright (c) 2001-2004 Stephen Zarkos. All rights reserved. ## Obsid@Sentry.net ## ## Please see file: COPYRIGHT for further copyright information and ## disclaimer. Or online at http://www.SentryFirewall.com/files/COPYRIGHT ##=====================================================================## ## File: floppy.pl ##------------------------------------------------------------------------------------## ## Sub create_floppy(). ## Formats/mounts floppy and copies the configuration files. sub create_floppy { my $dev = defined($_[0]) ? $_[0] : '/dev/fd0'; my ($filename,$status) = (); my $mnt_point = '/floppy'; $dev =~ s/[\s\t]+//g; $dev =~ s/^\/dev\/?//; return 0 if (!(-e "/dev/${dev}")); ## Create a new floppy to put configuration stuff on. ## FIXME: add support to format other devices. system("dialog --title \"$title\" --menu \"Please insert a floppy disk to continue.\\n\\n\" 0 0 0 \"CONTINUE\" \"Continue Without Format.\" \"FORMAT\" \"Format 1.44MB Floppy\" \"CANCEL\" \"Return to Main Menu\" 2>${output_file}"); &get_output; if ($output eq 'FORMAT') { $status = &format_floppy('ext2', "/dev/${dev}"); return 0 if ($status == 2); if ($status == 0) { system("dialog --title \"$title\" --msgbox \"An error occurred while formatting floppy:\\n\\n$!\\n$?\\n\" 0 0 2>${output_file}"); return 0; } } elsif ($output eq 'CANCEL') { return 0; } ## Mount floppy. &infobox("Mounting /dev/${dev} on ${mnt_point}...", "0 0", "1"); my @out = `/bin/cat /etc/mtab`; if (grep(/\/dev\/${dev}[\s\t]/, @out)) { system("dialog --title \"$title\" --yesno \"ERROR: /dev/${dev} appears to already be mounted.\nContinue anyway?\" 0 0 2>${output_file}"); if ($? != 0) { return 0; } else { foreach (@out) { if ($_ =~ \/dev\/${dev}[\s\t]/) { $mnt_point = chomp($_); last; } } $mnt_point =~ s/[\s\t]*\/dev\/${dev}[\s\t]+//; $mnt_point =~ s/[\s\t]+.+//; } } else { $status = &mount('mount', "/dev/${dev}", "$mnt_point"); if ($status ne "1") { system("dialog --title \"$title\" --msgbox \"ERROR: An error occurred while attempting to mount /dev/${dev} on ${mnt_point}.\\n\" 0 0 2>${output_file}"); return 0; } else { push(@mounted, "/dev/${dev}"); } } $status = &cp_config("$mnt_point"); (return 0) if ($status != 1); system("dialog --title \"$title\" --menu \"Finished creating configuration floppy.:\\n\\n\" 0 0 0 \"Back\" \"Return to Main Menu.\" \"Exit\" \"Exit Program\" 2>${output_file}"); &get_output; (return 0) if ($output eq 'Back'); return 1; } ## End sub create_floppy(). ##------------------------------------------------------------------------------------## ##------------------------------------------------------------------------------------## ## sub mk_image() ## Formats/mounts floppy image and copies the configuration files. sub mk_image { my ($path, $filename, $status) = (); my $mnt_point = '/floppy'; system("dialog --title \"$title\" --max-input 1000 --inputbox \"Save image as: \" 0 0 \"/tmp/ext2-144.img\" 2>${output_file}"); (return 0) if ($? == 1); &get_output; $path = $output; $path =~ s/[\s\t]//g; $path =~ s/([\w\-\.]+)$//; $filename = $1; $path =~ s/\/+$//; ## Check if file exists and is writeable if (-f "${path}/${filename}") { system("dialog --title \"$title\" --yesno \"File exists, overwrite?\" 0 0 2>${output_file}"); (return 0) if ($? != 0); if (-w "${path}/${filename}") { system("rm -f ${path}/${filename}"); if ($? > 0) { system("dialog --aspect 80 --msgbox --title \"$title\" --msgbox \"Error: unable to overwrite file:\\n\\n$!\\n$?\" 0 0 2>${output_file}"); return 0; } } else { system("dialog --title \"$title\" --msgbox \"Error: ${path}/${filename} is not writable.\\n\" 0 0 2>${output_file}"); return 0; } } ## Zero out file to use for image. &infobox("Zeroing out file: ${path}/${filename}...", "0 0", "1"); system("dd if=/dev/zero of=${path}/${filename} bs=512 count=2880 1>/dev/null 2>/dev/null"); if ($? > 0) { system("dialog --aspect 80 --title \"$title\" --msgbox \"Error: unable to overwrite file:\\n\\n$!\n$?\\n\" 0 0 2>${output_file}"); return 0; } ## Format ${path}/${filename}. &infobox("Formatting ${path}/${filename}...", "0 0", "1"); system("mke2fs -Fq -c -m0 -i2048 ${path}/${filename} 1>/dev/null 2>/dev/null"); if ($? > 0) { system("dialog --aspect 80 --title \"$title\" --msgbox \"Error during format:\\n\\n$!\\n$?\\n\" 0 0 2>${output_file}"); return 0; } ## Mount image. &infobox("Mounting ${path}/${filename} on ${mnt_point}...", "0 0", "1"); my @out = `/bin/cat /etc/mtab`; if (grep(/[\s\t]+${mnt_point}[\s\t]/,@out)) { system("dialog --title \"$title\" --msgbox \"Error, unable to mount ${path}/${filename} on ${mnt_point}.\\n\" 0 0 2>${output_file}"); return 0; } system("mount -o loop -t ext2 ${path}/${filename} $mnt_point 1>/dev/null 2>/dev/null"); if ($? > 0) { system("dialog --aspect 80 --title \"$title\" --msgbox \"Error, unable to mount ${path}/${filename} on ${mnt_point}:\\n\\n$!\\n$?\\n\" 0 0 2>${output_file}"); return 0; } $status = &cp_config("$mnt_point"); (return 0) if ($status != 1); system("dialog --title \"$title\" --yesno \"Finished copying files, would you like me to unmount ${mnt_point}?\" 0 0 2>${output_file}"); if ($? == 0) { push(@mounted, "$mnt_point"); } system("dialog --title \"$title\" --menu \"Finished creating configuration floppy.:\\n\\n\" 0 0 0 \"Back\" \"Return to Main Menu.\" \"Exit\" \"Exit Program\" 2>${output_file}"); &get_output; (return 0) if ($output eq 'Back'); return 1; } ## End Sub mk_image(). ##------------------------------------------------------------------------------------## ##------------------------------------------------------------------------------------## ## Function format_floppy() ## Formats a new floppy. sub format_floppy { my $fs = "$_[0]"; system("dialog --title \"$title\" --menu \"\\nPlease insert a floppy disk and click \\nWARNING: All data currently on floppy will be overwritten!\\n\\n\" 0 0 0 \"CONTINUE\" \"Format 1.44MB Floppy\" \"CANCEL\" \"Back to Main Menu\" 2>${output_file}"); &get_output; return 2 if ($output eq 'CANCEL'); infobox("Formatting floppy disk in /dev/fd0, please wait...", "0 0", "1"); if ($fs eq 'ext2') { system("mke2fs -Fq -m0 -i1024 /dev/fd0 1>/dev/null 2>/dev/null"); if ($? > 0) { return 0; } } elsif ($fs eq 'dos') { system("mkdosfs /dev/fd0 1>/dev/null 2>/dev/null"); if ("$?" > "0") { return 0; } } return 1; } ## End sub format_floppy() ##------------------------------------------------------------------------------------## return 1; ## _EOF_ ##