#!/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: networking.pl ## &networking: Utilizes $prefs{device*} to set up an interface and allow ## configuration to retr files via http(s)/ftp/scp/sftp. ## &retr_file: Retrieves file via ftp or http, puts it in /tmp, and returns ## the path/filename of retrieved file if retrieval was successful. $ENV{PATH} = "/bin:/sbin"; umask 022; ##-------------------------------------------------------------------------## ## sub networking() temporarily sets up networking device(s) to configure ## some system parameters over a network. sub networking { ## Mode: ## 1|NET_UP: Setup networking ## 2|NET_DOWN: Take down networking ## Returns: ## 0: Syntax error or some generic failure. ## 1: Success ## 2: Failure initializing device or assigning an IP address. my $mode = "$_[0]"; my $network = "0"; my ($device,$driver,$ip,$nmask,$num) = ''; my ($stat,$ismodule) = ''; my $kernel = `/bin/uname -r`; my (@drivers,@builtin_drivers) = (); ($mode = "1") if ($mode eq 'NET_UP'); ($mode = "2") if ($mode eq 'NET_DOWN'); (return 0) if (($mode ne "1") && ($mode ne "2")); ## Define list of available modules. ## At the moment only 10/100 NICs and a couple gigabit ethernet cards are supported. if ($kernel =~ /^2\.2/) { ## List of modules that are available with the 2.2.x kernel(s) distributed with the ## Sentry CD. @drivers = ('3c501', '3c503', '3c507', '3c505', '3c509', '3c515', 'lance', 'wd', 'smc-ultra', 'smc9194', 'smc-ultra32', 'ni5010', 'ni65', 'ni52', 'rtl8139', 'at1700', 'e2100', 'depca', 'ewrk3', 'eexpress', 'eepro', 'fmv18x', 'hp-plus', 'hp100', 'hp', 'eth16i', 'ne', 'starfire', 'ac3200', 'cs89x0', 'dmfe', 'dgrs', 'lne390', 'ne3210', 'tlan', 'sis900', 'es3210', 'epic100', 'yellowfin', 'acenic', 'sk98lin', 'rtl8139', '3c59x', 'pcnet32', 'de4x5', 'tulip', 'eepro100', 'ne2k-pci', 'via-rhine', '8139too'); @builtin_drivers = (); $kernel = '2.2'; } elsif ($kernel =~ /^2\.4/) { ## List of modules that are available with the 2.4.x kernels distributed with the ## Sentry CD. @drivers = ('3c501', '3c507', '3c505', '3c503', '3c509', '3c515', 'lance', 'wd', 'smc9194', 'smc-ultra', 'smc-ultra32', 'ni5010', 'ni65', 'ni52', 'at1700', 'depca', 'hp100', 'e2100', 'ewrk3', 'eexpress', 'eepro', 'hp', 'hp-plus', 'eth16i', 'ne', 'starfire', 'ac3200', 'cs89x', 'dgrs', 'dmfe', 'lne390', 'ne3210', 'es3210', 'sis900', 'epic100', 'winbond-840', 'acenic', 'hamachi', 'yellowfin', 'sundance', 'tlan', '3c59x', 'natsemi', 'sunhme', 'via-rhine', '8139too', 'ne2k-pci', 'eepro100', 'tulip', 'pcnet32', 'de4x5', 'typhoon', 'amd8111e', 'e100', '8139cp', 'fealnx'); @builtin_drivers = (); $kernel = '2.4'; } else { return 0; } ## Unknown kernel version. ## Global hash to handle module dependencies. if ($kernel eq '2.2') { %depend = (); } elsif ($kernel eq '2.4') { %depend = ( '8139too' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'via-rhine' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'starfire' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'epic100' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'winbond-840' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'sundance' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'pcnet32' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, '8139cp' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'fealnx' => { 'dep1' => 'mii', 'dep2' => 'crc32', }, 'smc9194' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'at1700' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'depca' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'ewrk3' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'dmfe' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'sis900' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'yellowfin' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'de4x5' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'tulip' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'typhoon' => { 'dep1' => 'crc32', 'dep2' => 'NULL', }, 'hamachi' => { 'dep1' => 'mii', 'dep2' => 'NULL', }, 'eepro100' => { 'dep1' => 'mii', 'dep2' => 'NULL', }, '3c503' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'wd' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'smc-ultra' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'smc-ultra32' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'e2100' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'hp' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'hp-plus' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'ne' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'ac3200' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'lne390' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'ne3210' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'es3210' => { 'dep1' => 'crc32', 'dep2' => '8390', }, 'ne2k-pci' => { 'dep1' => 'crc32', 'dep2' => '8390', }, ); ## End %depend hash. } ## "device" directive stuff. Setup networking. foreach $num (1..10) { next if !(exists($prefs{"device${num}"})); if ($prefs{"device${num}"} eq '') { delete($prefs{"device${num}"}); next; } &do_log("INFO: \'device${num}\' directive found, setting up device.") if ($mode eq '1'); &do_log("INFO: \'device${num}\' directive found, taking down device.") if ($mode eq '2'); if ((exists($prefs{'debug'})) && ($prefs{'debug'} eq '1')) { my $temp = $prefs{"device${num}"}; &do_log("DEBUG: \'device${num}\' = ${temp}"); } $prefs{"device${num}"} =~ s/[\t\s]+//g; ($device,$driver,$ip) = split(/:/,$prefs{"device${num}"},3); if ($device !~ /^eth\d$/) { &do_log("ERROR: Syntax error in \'device${num}\' directive"); delete($prefs{"device${num}"}); next; } $driver =~ s/\.o$//; $ismodule = ''; foreach (@drivers) { next if ("$driver" ne "$_"); if ($mode eq "1") { if (grep(/^$driver[\s\t]+/, `/sbin/lsmod`)) { ## Kernel module is already loaded. $ismodule = "1"; last; } ## Get driver from tar file. if ($kernel eq '2.2') { system("/bin/tar -zx -C /tmp/drivers -f/tmp/drivers/drivers-2.2.tar.gz ${driver}.o 1>/dev/null 2>/dev/null"); } elsif ($kernel eq '2.4') { system("/bin/tar -zx -C /tmp/drivers -f/tmp/drivers/drivers-2.4.tar.gz ${driver}.o 1>/dev/null 2>/dev/null"); } if ("$?" > "0") { delete($prefs{"device${num}"}); &do_log("ERROR: Unable to untar driver \'${driver}.o\'"); last; } ## Find and untar other required modules for this driver. if (exists($depend{"$driver"})) { if ($depend{"$driver"}->{'dep1'} ne 'NULL') { system("/bin/tar -zx -C /tmp/drivers -f/tmp/drivers/drivers-2.4.tar.gz $depend{$driver}->{dep1}.o 1>/dev/null 2>/dev/null"); if ("$?" > "0") { delete($prefs{"device${num}"}); &do_log("ERROR: Unable to untar driver dependency \'$depend{$driver}->{dep1}.o\'"); last; } } if ($depend{"$driver"}->{'dep2'} ne 'NULL') { system("/bin/tar -zx -C /tmp/drivers -f/tmp/drivers/drivers-2.4.tar.gz $depend{$driver}->{dep2}.o 1>/dev/null 2>/dev/null"); if ("$?" > "0") { delete($prefs{"device${num}"}); &do_log("ERROR: Unable to untar driver dependency \'$depend{$driver}->{dep1}.o\'"); last; } } } ## Attempt to insmod driver. last if (!(-f "/tmp/drivers/${driver}.o")); $stat = &load_mods("$driver"); if ($stat ne '1') { if (exists($depend{"$driver"})) { if ($depend{"$driver"}->{'dep1'} ne 'NULL') { unlink("/tmp/drivers/$depend{$driver}->{'dep1'}.o"); } if ($depend{"$driver"}->{'dep2'} ne 'NULL') { unlink("/tmp/drivers/$depend{$driver}->{'dep2'}.o"); } } unlink("/tmp/drivers/${driver}.o"); delete($prefs{"device${num}"}); last; } else { $stat = ''; $ismodule = "1"; } ## Done loading modules last; ## Modules loaded, end foreach loop. } elsif ($mode eq "2") { ## Module exists, and may be loaded. Used later to unload driver. $ismodule = "1"; } } ## End foreach loop ## Check for builtin drivers. if ($ismodule eq '') { foreach (@builtin_drivers) { if ("$driver" eq "$_") { $ismodule = "0"; ## Driver found, but built into kernel. last; } } } if ($ismodule eq '') { ## No driver found, either built in or as a module. &do_log("ERROR: Unable to load driver for \'device${num}\'"); delete($prefs{"device${num}"}); next; } ## Silly ip address checks if ($ip !~ /^dhcp/) { my $tmpgw = ''; if ($ip =~ /\|/) { ($ip,$tmpgw) = split(/\|/,$ip,2); if ($tmpgw !~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) { &do_log("Syntax error in \'device${num}\' directive."); if ($ismodule eq "1") { &remove_mods("$driver"); } delete($prefs{"device${num}"}); next; } } if ($ip =~ /\/\d{1,2}$/) { ($ip,$nmask) = split(/\//,$ip,2); } else { $ip =~ s/\/.*//; $nmask = 'NULL'; } if ($ip !~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/) { &do_log("Syntax error in \'device${num}\' directive."); if ($ismodule eq "1") { &remove_mods("$driver"); } delete($prefs{"device${num}"}); next; } my($tmpvar) = ''; foreach $tmpvar ($ip,$tmpgw) { my @tmpip = split(/\./,$tmpvar,4); foreach (@tmpip) { if (("$_" < "0") || ("$_" > "255")) { &do_log("Syntax error in \'device${num}\' directive."); if ($ismodule eq "1") { &remove_mods("$driver"); } delete($prefs{"device${num}"}); last; } } last if !(exists($prefs{"device${num}"})); } next if !(exists($prefs{"device${num}"})); ## Set a netmask if one was not declared. if ($nmask =~ /^\d{1,2}$/) { $ip .= '/' . "$nmask"; } else { $ip .= '/24'; } if ($mode eq "1") { ## Attempt to set an IP Address. if (!(grep(/\s+${ip}\s/, `/sbin/ip address show dev ${device}`))) { system("/sbin/ip address add ${ip} dev ${device} 1>/dev/null 2>/dev/null"); if ((exists($prefs{'debug'})) && ($prefs{'debug'} eq '1')) { &do_log("DEBUG: /sbin/ip address add ${ip} dev ${device}"); } if ("$?" > "0") { system("/sbin/ip address delete ${ip} dev ${device} 1>/dev/null 2>/dev/null"); &do_log("ERROR: Unable to set up ip address for \'device${num}\'"); if ($ismodule eq "1") { &remove_mods("$driver"); } delete($prefs{"device${num}"}); next; } } ## Bring $device up. system("/sbin/ip link set dev ${device} up 1>/dev/null 2>/dev/null"); if ((exists($prefs{'debug'})) && ($prefs{'debug'} eq '1')) { &do_log("DEBUG: /sbin/ip link set dev ${device} up"); } if ("$?" > "0") { system("/sbin/ip address delete ${ip} dev ${device} 1>/dev/null 2>/dev/null"); system("/sbin/ip link set dev ${device} down 1>/dev/null 2>/dev/null"); if ($ismodule eq "1") { &remove_mods("$driver"); } &do_log("ERROR: Unable to bring \'device${num}\' up."); delete($prefs{"device${num}"}); next; } ## Setup a default gateway if one was declared. if (($tmpgw) && ($tmpgw ne '')) { if (grep(/default\s+via\s/,`/sbin/ip route show`)) { ## Default route already set up, can't have two... &do_log("ERROR: Unable to set up default gateway \'${tmpgw}\', a default gateway already exists."); print "ERROR: Unable to set up default gateway \'${tmpgw}\', a default gateway already exists.\n"; } else { system("/sbin/ip route add default via ${tmpgw} 1>/dev/null 2>/dev/null"); if ((exists($prefs{'debug'})) && ($prefs{'debug'} eq '1')) { &do_log("DEBUG: /sbin/ip route add default via ${tmpgw}"); } if ("$?" > "0") { &do_log("ERROR: Unable to set up default gateway \'${tmpgw}\'."); print "ERROR: Unable to set up default gateway \'${tmpgw}\'.\n"; } } } ${network}++; } elsif ($mode eq "2") { ## Remove default gateway. if (($tmpgw) && ($tmpgw ne '')) { system("/sbin/ip route delete default via ${tmpgw} 1>/dev/null 2>/dev/null"); } ## Unbind IP addy. system("/sbin/ip address flush dev ${device} 1>/dev/null 2>/dev/null"); ## Take down device. system("/sbin/ip link set dev ${device} down 1>/dev/null 2>/dev/null"); if ($ismodule eq "1") { $stat = &remove_mods("$driver"); if ($stat ne '1') { delete($prefs{"device${num}"}); next; } } ${network}--; } } ## End block for bringin interface up/down with IP static address(without dhcp). ## Bring interface up/down with DHCP. elsif ($ip =~ /^dhcp/) { my $optargs = '-t 30'; $ip =~ s/^dhcp[\|]*//; if ("$ip" ne '') { $optargs .= " -h $ip"; } if (grep(/default\s+via\s/,`/sbin/ip route show`)) { ## Default route already set up, can't have two... &do_log("WARNING: Running dhcpcd with -G, since a default gateway already exists."); $optargs .= " -G"; } if ($mode eq "1") { ## Attempt to run dhcp to get IP address if (!(-f "/var/run/dhcpcd-${device}.pid")) { system("/sbin/dhcpcd $optargs ${device} 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: dhcpcd returned error."); delete($prefs{"device${num}"}); next; } } ${network}++; } ## Kill dhcpcd. elsif ($mode eq '2') { system("/sbin/dhcpcd -k ${device} 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: An error occurred while attempting to kill dhcpcd."); delete($prefs{"device${num}"}); } ## To be safe, flush and take down the interface. system("/sbin/ip address flush ${device} 1>/dev/null 2>/dev/null"); system("/sbin/ip link set ${device} down 1>/dev/null 2>/dev/null"); if ($ismodule eq "1") { $stat = &remove_mods("$driver"); if ($stat ne '1') { delete($prefs{"device${num}"}); next; } } ${network}--; } } else { &do_log("ERROR: Syntax error in device${num} directive."); delete($prefs{"device${num}"}); next; } } ## End foreach loop ## Finally, let's just make sure there aren't any undead dhcpcd ## process running around. if ($mode eq '2') { system("/bin/killall -s 9 dhcpcd 1>/dev/null 2>/dev/null"); system("/bin/rm -f /var/run/dhcpcd-eth*.pid 1>/dev/null 2>/dev/null"); } if ($network > "0") { return 1; } else { return 0; } } ## End sub networking() ##-------------------------------------------------------------------------## ##-------------------------------------------------------------------------## ## Function to retrieve config files via http(s)/ftp/scp/sftp. sub retr_file { my ($filename,$location) = ("$_[0]", "$_[1]"); my $user = $pass = $status = ''; $filename =~ s/[\s\t]+//g; $location =~ s/[\s\t]+//g; $location =~ s/\/{2,}$/\//; ## Remove any extra '/' characters. ## wget parameters. my ($common_options,$http_options,$ftp_options,$scp_options,$sftp_options) =''; ## HTTP(S)/FTP/scp/sftp Options. if (($filename eq 'xinetd_dir') || ($filename eq 'sysconf_dir')) { $filename = "/tmp/${filename}"; $common_options = "-q -t2 -T300 --directory-prefix=${filename} --no-host-directories"; $http_options = '--no-parent -r --level=4 -R"robots.txt,index.*,default.*" -A"*" --cookies=off --cache=off'; $ftp_options = '--no-parent -r --level=4 -R"robots.txt,index.*,default.*" -A"*" --cache=off'; $scp_options = '-qp -oStrictHostKeyChecking=no -oNumberOfPasswordPrompts=1 -r'; $sftp_options = '-oStrictHostKeyChecking=no -oNumberOfPasswordPrompts=1'; if ($filename eq '/tmp/sysconf_dir') { $sftp_options .= ' -b /tmp/sftp-batch'; } ## We need to give wget an accurate --cut-dirs argument, otherwise it will either ## throw all the files in a single directory, or make directories we don't want to ## mirror the URL location. my $count = "0"; while ($location =~ /\//gi) { ## Count the number of '/' characters. ++$count; } if ($location =~ /\/$/) { $count -= "3"; } else { $count -= "2"; } if ($count ne "0") { $common_options .= " --cut-dirs=${count}"; } mkdir ("$filename", 0755); ## Make /tmp/{sysconf_dir,xinetd_dir} return 0 if ("$?" > "0"); } else { if ($filename !~ /\//) { $filename = "/tmp/${filename}"; } $common_options = "-q -t2 -T300 -O${filename}"; $http_options = '--cookies=off --cache=off'; $ftp_options = '--cache=off'; $scp_options = '-qp -oStrictHostKeyChecking=no -oNumberOfPasswordPrompts=1'; $sftp_options = '-oStrictHostKeyChecking=no -oNumberOfPasswordPrompts=1'; } ## Export HTTP proxy settings. if (exists($prefs{'http_proxy'})) { if (($prefs{'http_proxy'} =~ /^[hH]{1}[tT]{2}[pP]{1}:\/\//) && ($prefs{'http_proxy'} =~ /:{1}\d+\/?$/)) { $ENV{'http_proxy'} = "$prefs{'http_proxy'}"; $http_options .= ' --proxy=on'; if ((exists($prefs{'proxy-user'})) && (exists($prefs{'proxy-passwd'}))) { $http_options .= " --proxy-user=$prefs{'proxy-user'} --proxy-passwd=$prefs{'proxy-passwd'}"; } } } ## Export FTP proxy settings. if (exists($prefs{'ftp_proxy'})) { if (($prefs{'ftp_proxy'} =~ /^[hH]{1}[tT]{2}[pP]{1}:\/\//) && ($prefs{'ftp_proxy'} =~ /:{1}\d+\/?$/)) { $ENV{'ftp_proxy'} = "$prefs{'ftp_proxy'}"; if ((exists($prefs{'proxy-user'})) && (exists($prefs{'proxy-passwd'}))) { $ftp_options .= " --proxy-user=$prefs{'proxy-user'} --proxy-passwd=$prefs{'proxy-passwd'}"; } } } ## Passive FTP support. if (exists($prefs{'passive-ftp'})) { if (($prefs{'passive-ftp'} eq 'yes') || ($prefs{'passive-ftp'} eq 'on')) { $ftp_options .= ' --passive-ftp'; } } ## Retrieve file via HTTP(s)?. if ($location =~ /^[hH]{1}[tT]{2}[pP]{1}[sS]?:\/\//) { ## FIXME: Potential problems here. If a default file, like index.htm[l]?, exists, then a ## directory listing will not be available, and thus mirroring the directory structure will ## not be possible. This is only an issue with xinetd_dir and sysconf_dir directives. $status = &do_command("/bin/wget ${common_options} ${http_options} ${location}", '120'); } ## Retrieve file via FTP. elsif ($location =~ /^[fF]{1}[tT]{1}[pP]{1}:\/\//) { $status = &do_command("/bin/wget ${common_options} ${ftp_options} ${location}", '120'); } ## Retrieve file via SCP. elsif ($location =~ /^[sS]{1}[cC]{1}[pP]{1}:\/\//) { $location =~ s/^[sS]{1}[cC]{1}[pP]{1}:\/\///; return 0 if ($location !~ /.+:.+\@/); if ($location =~ /\@{2,}/) { $location =~ s/^.*\@//; $user = "$&"; $user =~ s/\@{1}$//; } else { ($user,$location) = split(/\@/, $location, 2); } ($user,$pass) = split(/:/, $user, 2); $location =~ s/\//:\//; if (($filename eq 'xinetd_dir') || ($filename eq 'sysconf_dir')) { $location .= '/*'; $location =~ s/\/{2,}/\//g; } return 0 if ((! defined($user)) || (! defined($pass))); return 0 if (($user eq '') || ($pass eq '')); $status = &do_command("/bin/scp ${scp_options} -opassword=${pass} ${user}\@${location} $filename", '120'); } ## Retrieve file via SFTP. elsif ($location =~ /^[sS]{1}[fF]{1}[tT]{1}[pP]{1}:\/\//) { $location =~ s/^[sS]{1}[fF]{1}[tT]{1}[pP]{1}:\/\///; return 0 if ($location !~ /.+:.+\@/); if ($location =~ /\@{2,}/) { $location =~ s/^.*\@//; $user = "$&"; $user =~ s/\@{1}$//; } else { ($user,$location) = split(/\@/, $location, 2); } ($user,$pass) = split(/:/, $user, 2); $location =~ s/\//:\//; ## sftp doesn't have a recursive -r option, so when dealing with the sysconf_dir directive, ## we need to manually grab the stuff in the subdirectories... a bit of a pain. if ($filename eq 'sysconf_dir') { if (&mk_batch("$location") == 1) { $location =~ s/:.*//; $status = &do_command("/bin/sftp ${sftp_options} -opassword=${pass} ${user}\@${location}", '120'); } else { return 0; } } return 0 if ((! defined($user)) || (! defined($pass))); return 0 if (($user eq '') || ($pass eq '')); if ($filename eq 'xinetd_dir') { $location .= '/*'; $location =~ s/\/{2,}/\//g; } $status = &do_command("/bin/sftp ${sftp_options} -opassword=${pass} ${user}\@${location} $filename", '120'); } if (($status == "0") || ($status == "2")) { if (($filename eq '/tmp/xinetd_dir') || ($filename eq '/tmp/sysconf_dir')) { system('rm', '-rf', "$filename"); } elsif (-f "$filename") { unlink("$filename"); } return($status); } ## chmod rc files so that they're executable after download. if (($filename =~ /\/rc\./) && ($filename !~ /\/$/)) { if (!(-x "$filename")) { my $tmp = "$filename"; $tmp =~ s/.+\///; if ($tmp =~ /^rc\./) { chmod (0755, "$filename"); } } } ## Remove proxy settings delete($ENV{'http_proxy'}); delete($ENV{'ftp_proxy'}); ## Return file name only on success. return("$filename") if (($status == "1") && (-e "$filename")); return 0; } ## End sub retr_file() ##-------------------------------------------------------------------------## ##-------------------------------------------------------------------------## ## Function to create a batch file for use with sftp. sub mk_batch { my $dirname = "$_[0]"; (return 0) if ($dirname eq ''); my $basedir = '/tmp/sysconf_dir'; (return 0) if (!(-d "$basedir")); my $batch_file = '/tmp/sftp-batch'; my @dirs = ('', 'apm-scripts', 'cbq', 'console', 'network-scripts', 'networking', 'networking/devices', 'networking/profiles', 'networking/profiles/default'); $dirname =~ s/:.*//; $dirname = "$&"; $dirname =~ s/://g; if (open(FH, ">$batch_file")) { flock(FH,2); foreach (@dirs) { print FH "cd ${dirname}/${_}\n"; print FH "get -P * ${basedir}/${_}/\n\n"; } print FH "quit\n"; close(FH); } else { &do_log("ERROR: Unable to open file \"$batch_file\""); return 0; } return 1; } ## End sub mk_batch() ##-------------------------------------------------------------------------## ##-------------------------------------------------------------------------## ## Function to unload network card drivers. sub remove_mods { my $driver = "$_[0]"; my $status = '1'; system("/sbin/rmmod ${driver} 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to rmmod driver \'${driver}.o\'."); $status = '0'; } if (exists($depend{"$driver"})) { if ($depend{"$driver"}->{'dep1'} ne 'NULL') { system("/sbin/rmmod $depend{$driver}->{dep1} 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to rmmod driver dependency \'$depend{$driver}->{dep1}.o\'."); $status = '0'; } } if ($depend{"$driver"}->{'dep2'} ne 'NULL') { system("/sbin/rmmod $depend{$driver}->{dep2} 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to rmmod driver dependency \'$depend{$driver}->{dep2}.o\'."); $status = '0'; } } } (return 0) if ($status eq '0'); return 1; } ## End sub remove_mods() ##-------------------------------------------------------------------------## ##-------------------------------------------------------------------------## ## Function to load network card drivers. sub load_mods { my $driver = "$_[0]"; ## Insmod driver dependencies. if (exists($depend{"$driver"})) { if ($depend{"$driver"}->{'dep1'} ne 'NULL') { ## Check if module is already loaded. if (!(grep(/^($depend{"$driver"}->{'dep1'})[\s\t]+/, `/sbin/lsmod`))) { (return 0) if (!(-f "/tmp/drivers/$depend{$driver}->{dep1}.o")); system("/sbin/insmod -q /tmp/drivers/$depend{$driver}->{dep1}.o 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to insmod driver dependency \'$depend{$driver}->{dep1}.o\'."); return 0; } else { unlink("/tmp/drivers/$depend{$driver}->{'dep1'}.o"); &do_log("INFO: Module \'$depend{$driver}->{dep1}.o\' loaded successfully."); } } } if ($depend{"$driver"}->{'dep2'} ne 'NULL') { ## Check if module is already loaded. if (!(grep(/^($depend{"$driver"}->{'dep2'})[\s\t]+/, `/sbin/lsmod`))) { (return 0) if (!(-f "/tmp/drivers/$depend{$driver}->{dep2}.o")); system("/sbin/insmod -q /tmp/drivers/$depend{$driver}->{dep2}.o 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to insmod driver dependency \'$depend{$driver}->{dep2}.o\'."); return 0; } else { unlink("/tmp/drivers/$depend{$driver}->{'dep2'}.o"); &do_log("INFO: Module \'$depend{$driver}->{dep2}.o\' loaded successfully."); } } } } ## Done insmod-ing dependencies. ## Insmod driver. ## Module is not loaded(checked already in networking()). (return 0) if (!(-f "/tmp/drivers/${driver}.o")); system("/sbin/insmod -q /tmp/drivers/${driver}.o 1>/dev/null 2>/dev/null"); if ("$?" > "0") { &do_log("ERROR: Unable to insmod driver \'${driver}.o\'."); return 0; } else { unlink("/tmp/drivers/${driver}.o"); &do_log("INFO: Module \'${driver}.o\' loaded successfully."); } return 1; } ## End sub load_mods() ##-------------------------------------------------------------------------## return 1; ## _EOF_ ##