#!/usr/bin/perl5 require "/usr/OnRamp/lib/OnRamp.pm"; $conf = "/etc/gateways"; $dummy = "/etc/gateways.dummy"; $myname = "gateways-config.cgi"; $title = "Gateway Configuration"; print "Content-type: text/html\n\n"; &title_block($title); &get_fields; &getGateways; $delete = 0; if (%fld) { $help = $ENV{"DOCUMENT_ROOT"} . $ENV{"SCRIPT_NAME"}; $help =~ s/cgi$/hlp/; exec $help if ($fld{'help'} eq "Help"); $fld{'gateway'} =~ /([\w.]+)/; $fld{'gateway'} = $1; if ($fld{"add"}) { &validAdd; $val{'metric'} = 1; &getAdd; exit 0; } if ($fld{"doAdd"}) { &valid(0); &doAdd; $fld{'destination'} = ""; &getGateways; } if ($fld{"doEdit"}) { &valid(1); &doEdit; &getGateways; } if ($fld{"edit"}) { &error(2,"To edit an existing gateway, first choose one from the list, then click the edit button.") if !$fld{'gateway'}; &getGateways($fld{'gateway'}); &putEdit; &getEdit; exit 0; } if ($fld{"delete"}) { &error(2,"To delete an existing gateway, first choose one from the list, then click the delete button.") if !$fld{'gateway'}; $delete = 1; $message = qq|Click "Ok" to save changes.|; } if ($fld{"doit"}) { if ($erd ne $fld{'erd'}) { &changeStatus; } if ($fld{'delGateway'}) { &doDelete; }; &getGateways; } if (!$message) { if ($fld{'destination'}) { $message = qq|Click "Add New Gateway" to go to the add form.|; } else { $message = "No change made."; } } %val = %fld; } &generic; sub valid { &error($_[0],"Invalid route address.") if !$fld{'route'} || &check_ipaddr($fld{'route'}); &error($_[0],"Number of hops must be an integer.") if $fld{'metric'} =~ /[^0-9-]/; &error($_[0],"Number of hops must be greater than zero.") if $fld{'metric'} < 1; &error($_[0],"Number of hops must be no greater than 16.") if $fld{'metric'} > 16; } sub validAdd { &error(2,"You must provide a gateway address.") if !$fld{'destination'}; &error(2,"Invalid gateway address.") if &check_ipaddr($fld{'destination'}); } sub error { &error_block($_[1]); %val = %fld; if ($_[0] == 0) { &getAdd; } elsif ($_[0] == 1) { &getEdit; } else { &generic; } exit 0; } sub changeStatus { if ($fld{'erd'} eq 'Yes') { `/etc/chkconfig routed on`; `/etc/killall routed`; open(IN,"< /etc/config/routed.options"); while() { chop($_); $configFile .= " $_"; } close(IN); `/usr/etc/routed $configFile > /dev/null 2>&1`; $message = "Dynamic routing enabled."; } else { `/etc/chkconfig routed off`; `/etc/killall routed`; $message = "Dynamic routing disabled."; } } sub doDelete { open(IN,"< $conf"); open(OUT,"> $dummy"); while() { @items = split(/\s+/); if ($items[1] eq $fld{'delGateway'}) { print OUT "# $_"; } else { print OUT $_; } } close(IN); close(OUT); rename($dummy,$conf); $message = "Gateway deleted."; } sub doAdd { open(OUT,">> $conf"); print OUT "$fld{'type'} $fld{'destination'} gateway $fld{'route'} "; print OUT "metric $fld{'metric'} $fld{'mode'}\n"; close(OUT); $message = "Gateway added."; } sub putEdit { $val{'type'} = $type; $val{'mode'} = $mode; $val{'route'} = $rout; $val{'metric'} = $metr; } sub doEdit { open(IN,"< $conf"); open(OUT,"> $dummy"); while() { @items = split(/\s+/); if ($items[1] eq $fld{'gateway'}) { print OUT "# $_"; } else { print OUT $_; } } print OUT "$fld{'type'} $fld{'gateway'} gateway $fld{'route'} "; print OUT "metric $fld{'metric'} $fld{'mode'}\n"; close(IN); close(OUT); rename($dummy,$conf); $message = "Gateway edited."; } sub getEdit { &header_block("Edit Gateway"); print "
"; print ""; print "
"; print ""; print ""; print ""; print ""; print ""; print "
IP address of gateway destination: $fld{'gateway'}
IP address of route interface:", &text('route',$val{'route'},16),"
Type of network gateway:", &select('type',$val{'type'},'net','host'),"
Mode of network gateway:", &select('mode',$val{'mode'},'active','passive'),"
Number of hops from route interface to destination:", &text('metric',$val{'metric'},5),"
"; print "
", &buttons('doEdit','Ok'); print '
'; } sub getAdd { &header_block("Add New Gateway"); print "
"; print ""; print "
"; print ""; print ""; print ""; print ""; print ""; print "
IP address of gateway destination: $fld{'destination'}
IP address of route interface:", &text('route',$val{'route'},16),"
Type of network gateway:", &select('type',$val{'type'},'net','host'),"
Mode of network gateway:", &select('mode',$val{'mode'},'active','passive'),"
Number of hops from route interface to destination:", &text('metric',$val{'metric'},5),"
"; print "
", &buttons('doAdd','Ok'); print '
'; } sub generic { &header_block($title); print "$message"; print "
"; print "
"; print ""; print "|; print ""; if ($delete) { undef @locList; foreach $arg (@gateways) { if ($arg ne $fld{'gateway'}) { push(@locList,$arg); } } print ""; } else { @locList = @gateways; } print "|; print ""; print "|; print "
Enable dynamic routing:", &radio('erd',$erd,'Yes','No'),"
"; print qq|"; print "
"; print qq|", &choice_list(*locList,"gateway",20), "
"; print qq|
"; print "
", &buttons('doit','Ok'); print '
'; } sub getGateways { undef @gateways; open(GATE, "< $conf"); while () { @items = split(/\s+/); if ($items[0] ne 'net' && $items[0] ne 'host') { next; } push(@gateways,$items[1]); if ($items[1] eq $_[0]) { $type = $items[0]; $rout = $items[3]; $metr = $items[5]; $mode = $items[6]; } } close(GATE); `/etc/chkconfig routed`; $erd = $? >> 8; if ($erd) { $erd = "No"; } else { $erd = "Yes"; } }