#!/usr/local/bin/perl
# save_ifaces.cgi
# Save network interfaces on which the DHCP server is started

require './dhcpd-lib.pl';
%access = &get_module_acl();
$access{'noconfig'} && &error($text{'iface_ecannot'});
&ReadParse();

# Save in config file
@iface = split(/\s+|\0/, $in{'iface'});
@iface || &error($text{'iface_enone'});
$iface = join(" ", @iface);
$config{'interfaces'} = join(" ", @iface);
&write_file("$module_config_directory/config", \%config);

if ($config{'interfaces_type'} eq 'mandrake') {
	# Write to Mandrake linuxconf file
	local $lref = &read_file_lines("/etc/conf.linuxconf");
	for($i=0; $i<@$lref; $i++) {
		$secline = $i if ($lref->[$i] =~ /\[dhcpd\]/);
		$ifaceline = $i if ($lref->[$i] =~ /DHCP.interface/);
		}
	$line = "DHCP.interface $iface";
	if (defined($ifaceline)) {
		$lref->[$ifaceline] = $line;
		}
	elsif (defined($secline)) {
		splice(@$lref, $secline+1, 0, $line);
		}
	else {
		push(@$lref, "[dhcpd]", $line);
		}
	&flush_file_lines();
	}
elsif ($config{'interfaces_type'} eq 'redhat') {
	# Write to the Redhat environment file
	&read_env_file("/etc/sysconfig/dhcpd", \%dhcpd);
	$dhcpd{'DHCPDARGS'} = $iface;
	&write_env_file("/etc/sysconfig/dhcpd", \%dhcpd);
	}
elsif ($config{'interfaces_type'} eq 'suse') {
	# Write to the SuSE/United environment file
	&read_env_file("/etc/sysconfig/dhcpd", \%dhcpd);
	$dhcpd{'DHCPD_INTERFACE'} = $iface;
	&write_env_file("/etc/sysconfig/dhcpd", \%dhcpd);
	}
elsif ($config{'interfaces_type'} eq 'debian') {
	# Write to the debian init script
	$lref = &read_file_lines("/etc/init.d/dhcp");
	for($i=0; $i<@$lref; $i++) {
		if ($lref->[$i] =~ /INTERFACES\s*=\s*'([^']+)'/ ||
		    $lref->[$i] =~ /INTERFACES\s*=\s*"([^"]+)"/ ||
		    $lref->[$i] =~ /INTERFACES\s*=\s*(\S+)/) {
			$lref->[$i] = "INTERFACES=\"$iface\"";
			}
		}
	&flush_file_lines();
	}
elsif ($config{'interfaces_type'} eq 'caldera') {
	# Interfaces are set in the Caldera daemons directory file
	&read_env_file("/etc/sysconfig/daemons/dhcpd", \%dhcpd);
	@other = grep { !/^(lo|[a-z]+\d+)$/ } split(/\s+/, $dhcpd{'OPTIONS'});
	$dhcpd{'OPTIONS'} = join(" ", @other).($iface ? " $iface" : "");
	&write_env_file("/etc/sysconfig/daemons/dhcpd", \%dhcpd);
	}


&redirect("");

