#!/usr/bin/perl $debug = 1; BEGIN {push @INC, '/www/cgi-bin'}; use perlfunc; use ucifunc; use tunfunc; $VPNVER="1.0"; $config = nvram_get("config"); $node = nvram_get("node"); $node = "NOCALL" if $node eq ""; $unode = uc $node; # UPPER CASE NODENAME #$vpnconnfile = "/etc/vtun.connections"; #$tmpdir = "/tmp/web/vpn"; #$tmpvtundconf = "${tmpdir}/vtund.conf.tmp"; #$tmpvtundservice = "${tmpdir}/vtund.service.tmp"; # contains connection rows #$tmpvtund = "${tmpdir}/vtund.tmp"; #$tmpconnfile = "${tmpdir}/vpnconnections"; &get_model(); read_postdata(); @active_tun=&get_active_tun(); ################# # page checks ################# if($parms{button_reboot}) { system "/sbin/reboot"; } &reboot_required() if($config eq "" or -e "/tmp/reboot-required"); &vpn_setup_required() unless(-e "/usr/sbin/vtund" ); ################# # If RESET or FIRST TIME, load servers from file into parms ################# if($parms{button_reset} or not $parms{reload}) { # load connections from UCI &get_connection_info(); # initialize the "add" entries to clear them foreach $var (qw(conn_add_enabled conn_add_host conn_add_passwd conn_add_netip)) { $parms{$var} = ""; $parms{$var} = "0" if($var eq 'conn_add_enabled'); } } else { #&DEBUGEXIT($parms{client1_enable}); } ################# # load connections from FORM and validate ################# for($i =1 , @list = (); $i <= $parms{conn_num}; ++$i) { push @list, $i } push @list, "_add"; $conn_num = 0; foreach $val (@list) { foreach $var (qw(enabled host passwd netip)) { $varname = "conn${val}_$var"; $parms{$varname} = "0" if($val eq "enabled" and $parms{$varname} eq ""); $parms{$varname} = "" unless $parms{$varname}; $parms{$varname} =~ s/^\s+//; $parms{$varname} =~ s/\s+$//; if($val ne "_add") { if($parms{$varname} eq "" and ($var eq "enabled")) { $parms{$varname} = "0"; } } eval sprintf("\$%s = \$parms{%s}", $var, $varname); } # Validate ADDed values if($val eq "_add") { next unless ($enabled or $host or $passwd or $netip) and ($parms{conn_add} or $parms{button_save}) } else { next if $parms{"conn${val}_del"} } if($val eq "_add" and $parm{button_save}) { push @conn_err, "$val this connection must be added or cleared out before saving changes"; next; } push @conn_err, "A connection server is required" if($host eq ""); push @conn_err, "A connection password is required" if($passwd eq ""); next if $val eq "_add" and @cli_err and $cli_err[-1] =~ /^$val /; # Commit the data for this connection ++$conn_num; $parms{"conn${conn_num}_enabled"} = $enabled; $parms{"conn${conn_num}_host"} = $host; $parms{"conn${conn_num}_passwd"} = $passwd; $parms{"conn${conn_num}_netip"} = $netip; # Clear out the ADD values if($val eq "_add") { foreach $var (qw(enabled host passwd netip)) { $parms{"conn_add_${var}"} = ""; } } } ################# # SAVE the clients the UCI vtun file ################# $parms{conn_num} = $conn_num; if($parms{button_save} and not (@conn_err or @serv_err)) { # ADD/SET all server type entries for($i=1; $i<=$conn_num; $i++) { foreach $var (qw(enabled host passwd netip)) { eval sprintf("\$val = \$parms{conn%s_%s}",$i,$var); $idx=$i*(-1); push(@errors,"Problem adding server entry into config file") if &uci_set_indexed_option("vtun","server",$idx,$var,$val); } # TODO: calculate clientip and serverip # # TODO: uci_set_indexed_option("vtun","server",-1,"clientip",$clientip); # TODO: uci_set_indexed_option("vtun","server",-1,"clientip",$serverip); # TODO: uci_set_indexed_option("vtun","server",-1,"node",$nodename) } push(@errors,"Problem committing server entry into config file") if system "uci commit vtun > /dev/null 2>&1"; # COPY /etc/config/vtun to /etc/config.mesh/vtun (to workaround node-setup script issues) system "cp /etc/config/vtun /etc/config.mesh/vtun"; # STOP vtun clients system "/etc/init.d/vtund stop > /dev/null 2>&1"; # START VTUND /etc/init.d/vtund start unless($debug == 3) { push(@errors,"Problem starting vtund") if system "/etc/init.d/vtund start > /dev/null 2>&1"; } } ###################################################################################### # generate the page ###################################################################################### http_header() unless $debug == 2; html_header("$node setup", 1); print "