#!/usr/bin/perl -w ##=====================================================================## ## Copyright (C) 2001-2002 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: get_config.pl $ENV{PATH} = "/bin:/sbin"; umask 022; ##-------------------------------------------------------------------------## sub get_config { my($config) = "$_[0]"; my($status) = "0"; ## Attempt to mount /dev/fd0 $m_point = '/floppy'; $device = '/dev/fd0'; ## Test to see if there is a floppy in the drive. ## Thanks to Stefan Andersson for this. if (system("/bin/dd if=$device of=/dev/null bs=1 count=1 1>/dev/null 2>/dev/null") == "0") { ## Disk present, attempt to mount $device on $m_point print "Attempting to mount $device, attempt 1..."; $status = &do_command("/sbin/mount -r -n $device $m_point", '10'); if ($status eq "0") { print " ERROR.\n"; } elsif ($status eq "1") { print " done.\n"; } elsif ($status eq "2") { print " timed out.\n"; } else { print " \n"; } if (($status eq "0") || ($status eq "2")) { ## Attempt to mount again print "Attempting to mount $device, attempt 2..."; $status = &do_command("/sbin/mount -r -n $device $m_point", '10'); if ($status eq "0") { print " ERROR.\n"; } elsif ($status eq "1") { print " done.\n"; } elsif ($status eq "2") { print " timed out.\n"; } else { print " \n"; } } } else { ## Disk not present $status = "0"; } ## If unable to mount floppy, try mounting /dev/hda1. if ($status ne '1') { $m_point = '/mnt'; $device = '/dev/hda1'; if (system("/bin/dd if=$device of=/dev/null bs=1 count=1 1>/dev/null 2>/dev/null") == "0") { ## Disk present, attempt to mount $device on $m_point print "Attempting to mount $device, attempt 1..."; $status = &do_command("/sbin/mount -r -n $device $m_point", '10'); if ($status eq "0") { print " ERROR.\n"; } elsif ($status eq "1") { print " done.\n"; } elsif ($status eq "2") { print " timed out.\n"; } else { print " \n"; } } else { ## Disk not present $status = "0"; } } ## Check $status now. If it's not 1, then we've failed ## to mount anything. if ($status ne '1') { $device = ''; return 0; } ## I guess mount succeeded. &do_log("${device} successfully mounted on ${m_point}."); ## Try to retrieve the config file. open(CONF,"<${m_point}/${config}") or return 0; @conf = ; close(CONF); return 1; } ## End sub get_config ##-------------------------------------------------------------------------## ##-------------------------------------------------------------------------## sub do_command { ## Function attempts to execute a command passed to it, with a timeout, also ## passed. Returns 0 on error, 1 on success, and 2 if operation timed out. local $SIG{ALRM} = sub { die "TIMEOUT"; }; local $SIG{CHLD} = 'IGNORE'; return 0 if (!(defined($_[0])) || ($_[0] eq '')); my @command = split(/\s+/, $_[0]); my $timeout = "$_[1]"; ## Round and set timeout to "10" if number is bogus or not between 1 and 499. $timeout = sprintf("%.0f", $timeout); $timeout = (($timeout > "0") && ($timeout < 500)) ? $timeout : "10"; ## Fork and exec. my $pid = open(PH, "-|"); if ($pid) { eval { local $SIG{CHLD} = sub { wait; die if ($? > "0"); }; alarm($timeout); while () { } alarm(0); }; } else { open (STDERR, '>/dev/null') or die; (exec("@command")) or die; ## Dies if @command unsuccessful. } if ($@) { if ($@ =~ /TIMEOUT/) { ## Command timed out kill(15, $pid); sleep 1; kill(9, $pid); close(PH); return 2; ## Timed Out } else { close(PH); alarm(0); return 0; ## Return Error } } else { close(PH); sleep 1; return 1; Return Success } } ## End sub do_command return 1; ## _EOF_ ##