]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - html/cgi-bin/dhcp.cgi
samba.cgi: Use new perl system functions
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / dhcp.cgi
index 3eb5349a9f70ec7efee12ff0a40f2f5ae11041c7..4c9fff8353c3988382439c07efe4240ff1a45adc 100644 (file)
@@ -20,6 +20,7 @@
 ###############################################################################
 
 use strict;
+use experimental 'smartmatch';
 
 # enable only the following on debugging purpose
 #use warnings;
@@ -73,6 +74,7 @@ foreach my $itf (@ITFs) {
     $dhcpsettings{"DNS_UPDATE_KEY_NAME_${itf}"} = '';
     $dhcpsettings{"DNS_UPDATE_KEY_SECRET_${itf}"} = '';
     $dhcpsettings{"DNS_UPDATE_KEY_ALGO_${itf}"} = '';
+    $dhcpsettings{"DENY_KNOWN_CLIENTS_${itf}"} = 'off';
 }
 
 $dhcpsettings{'SORT_FLEASELIST'} = 'FIPADDR';
@@ -109,7 +111,7 @@ foreach my $itf (@ITFs) {
 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
 &General::readhash("${General::swroot}/time/settings", \%timesettings);
-&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
+&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
 
 #Get GUI values
 &Header::getcgihash(\%dhcpsettings);
@@ -128,6 +130,15 @@ open(FILE, "$filename2") or die 'Unable to open fixed leases file.';
 our @current2 = <FILE>;
 close(FILE);
 
+# Open and read-in file which contains the list of allowed advanced options.
+open(FILE, $filename3) or die "Could not open $filename3. $!\n"
+
+# Grab file content.
+my @advoptions_list = <FILE>;
+
+# Close file handle.
+close(FILE);
+
 # Check Settings1 first because they are needed by &buildconf
 if ($dhcpsettings{'ACTION'} eq $Lang::tr{'save'}) {
     foreach my $itf (@ITFs) {
@@ -174,6 +185,13 @@ if ($dhcpsettings{'ACTION'} eq $Lang::tr{'save'}) {
                }
            }
 
+           if ($dhcpsettings{"DENY_KNOWN_CLIENTS_${itf}"} eq 'on') {
+               if (($dhcpsettings{"START_ADDR_${itf}"}) eq '' && ($dhcpsettings{"END_ADDR_${itf}"}) eq '') {
+                       $errormessage = "DHCP on ${itf}: " . $Lang::tr{'dhcp valid range required when deny known clients checked'};
+                       goto ERROR;
+               }
+           }
+
            if (!($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} =~ /^\d+$/)) {
                $errormessage = "DHCP on ${itf}: " . $Lang::tr{'invalid default lease time'} . $dhcpsettings{'DEFAULT_LEASE_TIME_${itf}'};
                goto ERROR;
@@ -329,7 +347,7 @@ if ($dhcpsettings{'ACTION'} eq $Lang::tr{'add'}.'1' &&
        map ($dhcpsettings{"ADVOPT_SCOPE_$_"} = 'off', @ITFs);  # force global
     } elsif (ValidNewOption ($dhcpsettings{'ADVOPT_NAME'} . ' ' . $dhcpsettings{'ADVOPT_DATA'})) {
        #was a new option
-    } elsif (! `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`) {
+    } elsif (! grep(/option $dhcpsettings{'ADVOPT_NAME'}/, @advoptions_list)) {
        $errormessage=$Lang::tr{'dhcp advopt unknown'}.': '.$dhcpsettings{'ADVOPT_NAME'};
     }
 
@@ -443,18 +461,23 @@ if ($dhcpsettings{'ACTION'} eq $Lang::tr{'add'}.'2') {
        $dhcpsettings{'FIX_ROOTPATH'} = &Header::cleanhtml($dhcpsettings{'FIX_ROOTPATH'});
        if ($dhcpsettings{'KEY2'} eq '') { #add or edit ?
            unshift (@current2, "$dhcpsettings{'FIX_MAC'},$dhcpsettings{'FIX_ADDR'},$dhcpsettings{'FIX_ENABLED'},$dhcpsettings{'FIX_NEXTADDR'},$dhcpsettings{'FIX_FILENAME'},$dhcpsettings{'FIX_ROOTPATH'},$dhcpsettings{'FIX_REMARK'}\n");
+           open(FILE, ">$filename2") or die 'Unable to open fixed lease file.';
+           print FILE @current2;
+           close(FILE);
            &General::log($Lang::tr{'fixed ip lease added'});
 
            # Enter edit mode
-           $dhcpsettings{'KEY2'} = $key;
+           $dhcpsettings{'KEY2'} = 0;
        } else {
            @current2[$dhcpsettings{'KEY2'}] = "$dhcpsettings{'FIX_MAC'},$dhcpsettings{'FIX_ADDR'},$dhcpsettings{'FIX_ENABLED'},$dhcpsettings{'FIX_NEXTADDR'},$dhcpsettings{'FIX_FILENAME'},$dhcpsettings{'FIX_ROOTPATH'},$dhcpsettings{'FIX_REMARK'}\n";
            $dhcpsettings{'KEY2'} = '';       # End edit mode
            &General::log($Lang::tr{'fixed ip lease modified'});
+
+           # sort newly added/modified entry
+           &sortcurrent2;
        }
 
         #Write changes to dhcpd.conf.
-        &sortcurrent2;    # sort newly added/modified entry
         &buildconf;       # before calling buildconf which use fixed lease file !
     }
 }
@@ -521,7 +544,8 @@ if ($dhcpsettings{'ACTION'} eq '' ) { # First launch from GUI
        }
     }
     $dhcpsettings{'FIX_ENABLED'} = 'on';
-}
+    $dhcpsettings{'ADVOPT_ENABLED'} = 'on';
+    }
 
 &Header::openpage($Lang::tr{'dhcp configuration'}, 1, '');
 &Header::openbigbox('100%', 'left', '', $errormessage);
@@ -542,6 +566,7 @@ foreach my $itf (@ITFs) {
     my %checked=();
     $checked{'ENABLE'}{'on'} = ( $dhcpsettings{"ENABLE_${itf}"} ne 'on') ? '' : "checked='checked'";
     $checked{'ENABLEBOOTP'}{'on'} = ( $dhcpsettings{"ENABLEBOOTP_${itf}"} ne 'on') ? '' : "checked='checked'";
+    $checked{'DENY_KNOWN_CLIENTS'}{'on'} = ( $dhcpsettings{"DENY_KNOWN_CLIENTS_${itf}"} ne 'on') ? '' : "checked='checked'";
 
     if ($netsettings{"${itf}_DEV"} ne '' ) { # Show only defined interface
        my $lc_itf=lc($itf);
@@ -557,6 +582,9 @@ print <<END
     <td width='25%'><input type='text' name='START_ADDR_${itf}' value='$dhcpsettings{"START_ADDR_${itf}"}' /></td>
     <td width='25%' class='base'>$Lang::tr{'end address'}&nbsp;<img src='/blob.gif' alt='*' /></td>
     <td width='25%'><input type='text' name='END_ADDR_${itf}' value='$dhcpsettings{"END_ADDR_${itf}"}' /></td>
+</tr><tr>
+    <td class='base'>$Lang::tr{'dhcp deny known clients:'}</td>
+    <td><input type='checkbox' name='DENY_KNOWN_CLIENTS_${itf}' $checked{'DENY_KNOWN_CLIENTS'}{'on'} /></td>
 </tr><tr>
     <td class='base'>$Lang::tr{'default lease time'}&nbsp;<img src='/blob.gif' alt='*' /></td>
     <td><input type='text' name='DEFAULT_LEASE_TIME_${itf}' value='$dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"}' /></td>
@@ -615,7 +643,7 @@ $checked{'DNS_UPDATE_ENABLED'}{'on'} = ( $dhcpsettings{'DNS_UPDATE_ENABLED'} ne
 print <<END
 <table  width='100%'>
        <tr>
-               <td width='25%' class='boldbase'>$Lang::tr{'dhcp dns enable update'}</td>
+               <td width='30%' class='boldbase'>$Lang::tr{'dhcp dns enable update'}</td>
                <td class='base'><input type='checkbox' name='DNS_UPDATE_ENABLED' $checked{'DNS_UPDATE_ENABLED'}{'on'}>
                </td>
        <tr>
@@ -695,7 +723,11 @@ if ($dhcpsettings{'KEY1'} ne '') {
 }
 
 #search if the 'option' is in the list and print the syntax model
-my $opt = `grep "\$option $dhcpsettings{'ADVOPT_NAME'} " $filename3`;
+my @opt = grep(/option $dhcpsettings{'ADVOPT_NAME'}/, @advoptions_list);
+
+# Assign array element to variable and remove newlines.
+my $opt = chomp(@opt[0]);
+
 if ($opt ne '') {
    $opt =~ s/option $dhcpsettings{'ADVOPT_NAME'}/Syntax:/;  # "option xyz abc" => "syntax: abc"
    $opt =~ s/;//;
@@ -1258,7 +1290,12 @@ sub buildconf {
        if ($dhcpsettings{"ENABLE_${itf}"} eq 'on' ){
            print FILE "subnet " . $netsettings{"${itf}_NETADDRESS"} . " netmask ". $netsettings{"${itf}_NETMASK"} . " #$itf\n";
            print FILE "{\n";
-           print FILE "\trange " . $dhcpsettings{"START_ADDR_${itf}"} . ' ' . $dhcpsettings{"END_ADDR_${itf}"}.";\n" if ($dhcpsettings{"START_ADDR_${itf}"});
+           if ($dhcpsettings{"START_ADDR_${itf}"}) {
+               print FILE "pool {\n";
+               print FILE "\trange " . $dhcpsettings{"START_ADDR_${itf}"} . ' ' . $dhcpsettings{"END_ADDR_${itf}"}.";\n";
+               print FILE "\tdeny known-clients;\n" if ($dhcpsettings{"DENY_KNOWN_CLIENTS_${itf}"} eq 'on');
+               print FILE "     }\n"; # pool
+           }
            print FILE "\toption subnet-mask "   . $netsettings{"${itf}_NETMASK"} . ";\n";
            print FILE "\toption domain-name \"" . $dhcpsettings{"DOMAIN_NAME_${itf}"} . "\";\n";
            print FILE "\toption routers " . $netsettings{"${itf}_ADDRESS"} . ";\n";
@@ -1272,7 +1309,7 @@ sub buildconf {
            print FILE ", " . $dhcpsettings{"WINS2_${itf}"}                            if ($dhcpsettings{"WINS2_${itf}"});
            print FILE ";\n"                                                           if ($dhcpsettings{"WINS1_${itf}"});
            print FILE "\tnext-server " . $dhcpsettings{"NEXT_${itf}"} . ";\n" if ($dhcpsettings{"NEXT_${itf}"});
-           print FILE "\tfilename \"" . $dhcpsettings{"FILE_${itf}"} . "\";\n" if ($dhcpsettings{"FILE_${itf}"});
+           print FILE "\tfilename \"" . &EscapeFilename($dhcpsettings{"FILE_${itf}"}) . "\";\n" if ($dhcpsettings{"FILE_${itf}"});
            print FILE "\tdefault-lease-time " . ($dhcpsettings{"DEFAULT_LEASE_TIME_${itf}"} * 60). ";\n";
            print FILE "\tmax-lease-time "     . ($dhcpsettings{"MAX_LEASE_TIME_${itf}"} * 60)    . ";\n";
            print FILE "\tallow bootp;\n" if ($dhcpsettings{"ENABLEBOOTP_${itf}"} eq 'on');
@@ -1306,7 +1343,7 @@ sub buildconf {
                print FILE "}\n\n";
            }
 
-           system ('/usr/bin/touch', "${General::swroot}/dhcp/enable_${lc_itf}");
+           &General::system('/usr/bin/touch', "${General::swroot}/dhcp/enable_${lc_itf}");
            &General::log("DHCP on ${itf}: " . $Lang::tr{'dhcp server enabled'})
        } else {
            unlink "${General::swroot}/dhcp/enable_${lc_itf}";
@@ -1325,7 +1362,7 @@ sub buildconf {
            print FILE "\thardware ethernet $temp[0];\n";
            print FILE "\tfixed-address $temp[1];\n";
            print FILE "\tnext-server $temp[3];\n"          if ($temp[3]);
-           print FILE "\tfilename \"$temp[4]\";\n"         if ($temp[4]);
+           print FILE "\tfilename \"" . &EscapeFilename($temp[4]) . "\";\n" if ($temp[4]);
            print FILE "\toption root-path \"$temp[5]\";\n" if ($temp[5]);
            print FILE "}\n";
            $key++;
@@ -1333,9 +1370,9 @@ sub buildconf {
     }
     print FILE "include \"${General::swroot}/dhcp/dhcpd.conf.local\";\n";
     close FILE;
-    if ( $dhcpsettings{"ENABLE_GREEN"} eq 'on' || $dhcpsettings{"ENABLE_BLUE"} eq 'on' ) {system '/usr/local/bin/dhcpctrl enable >/dev/null 2>&1';}
-    else {system '/usr/local/bin/dhcpctrl disable >/dev/null 2>&1';}
-    system '/usr/local/bin/dhcpctrl restart >/dev/null 2>&1';
+    if ( $dhcpsettings{"ENABLE_GREEN"} eq 'on' || $dhcpsettings{"ENABLE_BLUE"} eq 'on' ) {&General::system('/usr/local/bin/dhcpctrl', 'enable');}
+    else {&General::system('/usr/local/bin/dhcpctrl', 'disable');}
+    &General::system_background('/usr/local/bin/dhcpctrl', 'restart');
 }
 
 #
@@ -1392,3 +1429,12 @@ sub IsUsedNewOptionDefinition {
     }
     return 0;
 }
+
+sub EscapeFilename($) {
+       my $filename = shift;
+
+       # Replace all single / by \/
+       $filename =~ s/\//\\\//g;
+
+       return $filename;
+}