2 # Networking functions for openlinux
4 $net_scripts_dir = "/etc/sysconfig/network-scripts";
5 $network_config = "/etc/sysconfig/network";
6 $static_route_config = "/etc/sysconfig/network-scripts/ifcfg-routes";
7 $nis_conf = "/etc/nis.conf";
12 # Returns a list of interfaces brought up at boot time
16 opendir(CONF, &translate_filename($net_scripts_dir));
17 while($f = readdir(CONF)) {
18 next if ($f !~ /^ifcfg-(\S+)/ || $f eq 'ifcfg-routes' ||
21 &read_env_file("$net_scripts_dir/$f", \%conf);
22 $b->{'fullname'} = $conf{'DEVICE'} ? $conf{'DEVICE'} : $1;
23 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
27 else { $b->{'name'} = $b->{'fullname'}; }
28 $b->{'up'} = ($conf{'ONBOOT'} eq 'yes');
29 $b->{'address'} = $conf{'IPADDR'} ? $conf{'IPADDR'} : "Automatic";
30 $b->{'netmask'} = $conf{'NETMASK'} ? $conf{'NETMASK'} : "Automatic";
31 $b->{'broadcast'} = $conf{'BROADCAST'} ? $conf{'BROADCAST'}
33 $b->{'dhcp'} = $conf{'DYNAMIC'} eq 'dhcp';
34 $b->{'edit'} = ($b->{'name'} !~ /^ppp|plip/);
35 $b->{'desc'} = $conf{'NAME'};
36 $b->{'index'} = scalar(@rv);
37 $b->{'file'} = "$net_scripts_dir/$f";
44 # save_interface(&details)
45 # Create or update a boot-time interface
49 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
51 &lock_file("$net_scripts_dir/ifcfg-$name");
52 &read_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
53 $conf{'DEVICE'} = $name;
54 if ($_[0]->{'dhcp'}) {
55 $conf{'DYNAMIC'} = 'dhcp';
58 $conf{'IPADDR'} = $_[0]->{'address'};
59 delete($conf{'DYNAMIC'});
61 local($ip1, $ip2, $ip3, $ip4) = split(/\./, $_[0]->{'address'});
62 $conf{'NETMASK'} = $_[0]->{'netmask'};
63 local($nm1, $nm2, $nm3, $nm4) = split(/\./, $_[0]->{'netmask'});
64 $conf{'NETWORK'} = sprintf "%d.%d.%d.%d",
65 ($ip1 & int($nm1))&0xff,
66 ($ip2 & int($nm2))&0xff,
67 ($ip3 & int($nm3))&0xff,
68 ($ip4 & int($nm4))&0xff;
69 $conf{'BROADCAST'} = $_[0]->{'broadcast'};
70 $conf{'ONBOOT'} = $_[0]->{'up'} ? "yes" : "no";
71 $conf{'NAME'} = $_[0]->{'desc'};
72 &write_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
73 &unlock_file("$net_scripts_dir/ifcfg-$name");
76 # delete_interface(&details)
77 # Delete a boot-time interface
80 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
82 &unlink_logged("$net_scripts_dir/ifcfg-$name");
86 # Can some boot-time interface parameter be edited?
89 return $_[0] ne "bootp" && $_[0] ne "mtu";
92 # can_iface_desc([&iface])
93 # Returns 1 if boot-interfaces can have comments
99 # valid_boot_address(address)
100 # Is some address valid for a bootup interface
101 sub valid_boot_address
103 return &check_ipaddress($_[0]);
109 &read_env_file($network_config, \%conf);
110 if ($conf{'HOSTNAME'}) {
111 return $conf{'HOSTNAME'};
113 return &get_system_hostname(1);
116 # save_hostname(name)
120 &system_logged("hostname $_[0] >/dev/null 2>&1");
121 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
122 &print_tempfile(HOST, $_[0],"\n");
123 &close_tempfile(HOST);
124 &lock_file($network_config);
125 &read_file($network_config, \%conf);
126 $conf{'HOSTNAME'} = $_[0];
127 &write_file($network_config, \%conf);
128 &unlock_file($network_config);
129 undef(@main::get_system_hostname); # clear cache
136 &execute_command("domainname", undef, \$d, undef);
141 # save_domainname(domain)
145 &execute_command("domainname ".quotemeta($_[0]));
146 &read_env_file($network_config, \%conf);
148 $conf{'NISDOMAIN'} = $_[0];
151 delete($conf{'NISDOMAIN'});
153 &write_env_file($network_config, \%conf);
156 sub routing_config_files
158 return ( $network_config,
159 map { $_->{'file'} } &boot_interfaces() );
164 local (%conf, %ifc, $f, $gateway, $gatewaydev);
165 &read_file($network_config, \%conf);
166 local ($gateway, $gatewaydev) = &get_default_gateway();
168 # Default router and device
169 print &ui_table_row($text{'routes_default'},
170 &ui_radio("gateway_def", $gateway ? 0 : 1,
171 [ [ 1, $text{'routes_none'} ],
172 [ 0, $text{'routes_gateway'}." ".
173 &ui_textbox("gateway", $gateway, 15)." ".
174 $text{'routes_device'}." ".
175 &ui_textbox("gatewaydev", $gatewaydev, 6) ] ]));
178 print &ui_table_row($text{'routes_forward'},
179 &ui_yesno_radio("forward", $conf{'IPFORWARDING'} =~ /yes|true/i));
181 # Additional routes script
182 print &ui_table_row($text{'routes_script'},
183 &ui_textarea("script", &read_file_contents($static_route_config),
190 &lock_file($network_config);
191 &read_file($network_config, \%conf);
192 if ($in{'forward'}) { $conf{'IPFORWARDING'} = 'yes'; }
193 else { delete($conf{'IPFORWARDING'}); }
194 local %ifcs = map { $_->{'fullname'}, 1 } &all_interfaces();
196 if (!$in{'gateway_def'}) {
197 &to_ipaddress($in{'gateway'}) ||
198 &error(&text('routes_edefault', $in{'gateway'}));
199 $ifcs{$in{'gatewaydev'}} ||
200 &error(&text('routes_edevice', $in{'gatewaydev'}));
203 &set_default_gateway($in{'gateway_def'} ? ( ) :
204 ( $in{'gateway'}, $in{'gatewaydev'} ) );
206 &write_file($network_config, \%conf);
207 &unlock_file($network_config);
209 &open_lock_tempfile(SCRIPT, ">$static_route_config");
210 $in{'script'} =~ s/\r//g;
211 &print_tempfile(SCRIPT, $in{'script'});
212 &close_tempfile(SCRIPT);
213 &system_logged("chmod +x $static_route_config");
216 sub os_feedback_files
218 opendir(DIR, $net_scripts_dir);
219 local @f = readdir(DIR);
221 return ( (map { "$net_scripts_dir/$_" } grep { /^ifcfg-/ } @f),
222 $network_config, $static_route_config, $nis_conf, "/etc/resolv.conf",
223 "/etc/nsswitch.conf", "/etc/HOSTNAME" );
227 # Apply the interface and routing settings
230 &system_logged("(cd / ; /etc/rc.d/init.d/network stop ; /etc/rc.d/init.d/network start) >/dev/null 2>&1");
233 # apply_interface(&iface)
234 # Calls an OS-specific function to make a boot-time interface active
237 local $out = &backquote_logged("cd / ; ifup '$_[0]->{'fullname'}' 2>&1 </dev/null");
238 return $? ? $out : undef;
241 # get_default_gateway()
242 # Returns the default gateway IP (if one is set) and device (if set) boot time
244 sub get_default_gateway
246 &read_file($network_config, \%conf);
247 opendir(CONF, &translate_filename($net_scripts_dir));
249 while($f = readdir(CONF)) {
250 next if ($f !~ /^ifcfg-(\S+)/);
252 &read_file("$net_scripts_dir/$f", \%ifc);
253 if (&check_ipaddress($ifc{'GATEWAY'})) {
254 return ( $ifc{'GATEWAY'}, $ifc{'DEVICE'} );
261 # set_default_gateway([gateway, device])
262 # Sets the default gateway to the given IP accessible via the given device,
263 # in the boot time settings.
264 sub set_default_gateway
266 opendir(CONF, &translate_filename($net_scripts_dir));
268 while($f = readdir(CONF)) {
269 next if ($f !~ /^ifcfg-(\S+)/);
271 &lock_file("$net_scripts_dir/$f");
272 &read_file("$net_scripts_dir/$f", \%ifc);
273 if (!$_[0] || $ifc{'DEVICE'} ne $_[1]) {
274 delete($ifc{'GATEWAY'});
277 $ifc{'GATEWAY'} = $_[0];
279 &write_file("$net_scripts_dir/$f", \%ifc);
280 &unlock_file("$net_scripts_dir/$f");
285 # supports_address6([&iface])
286 # Returns 1 if managing IPv6 interfaces is supported
287 sub supports_address6