1# united-linux-lib.pl
2# Networking functions for united linux
3
4$net_scripts_dir = "/etc/sysconfig/network";
5$routes_config = "/etc/sysconfig/network/routes";
6$sysctl_config = "/etc/sysconfig/sysctl";
7
8do 'linux-lib.pl';
9
10# boot_interfaces()
11# Returns a list of interfaces brought up at boot time
12sub boot_interfaces
13{
14local(@rv, $f);
15opendir(CONF, &translate_filename($net_scripts_dir));
16while($f = readdir(CONF)) {
17	next if ($f !~ /^ifcfg-([a-z0-9:\.]+)$/);
18	local (%conf, $b);
19	$b->{'fullname'} = $1;
20	&read_env_file("$net_scripts_dir/$f", \%conf);
21	if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
22		$b->{'name'} = $1;
23		$b->{'virtual'} = $2;
24		}
25	else { $b->{'name'} = $b->{'fullname'}; }
26	$b->{'up'} = ($conf{'STARTMODE'} eq 'onboot');
27	local $pfx;
28	if ($conf{'IPADDR'} =~ /^(\S+)\/(\d+)$/) {
29		$b->{'address'} = $1;
30		$pfx = $2;
31		}
32	else {
33		$b->{'address'} = $conf{'IPADDR'};
34		}
35	$pfx = $conf{'PREFIXLEN'} if (!$pfx);
36	if ($pfx) {
37		$b->{'netmask'} = &prefix_to_mask($pfx);
38		}
39	else {
40		$b->{'netmask'} = $conf{'NETMASK'};
41		}
42	$b->{'broadcast'} = $conf{'BROADCAST'};
43	$b->{'dhcp'} = ($conf{'BOOTPROTO'} eq 'dhcp');
44	$b->{'edit'} = ($b->{'name'} !~ /^ppp|irlan/);
45	$b->{'index'} = scalar(@rv);
46	$b->{'file'} = "$net_scripts_dir/$f";
47	push(@rv, $b);
48	}
49closedir(CONF);
50return @rv;
51}
52
53# save_interface(&details)
54# Create or update a boot-time interface
55sub save_interface
56{
57local(%conf);
58local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
59				       : $_[0]->{'name'};
60&lock_file("$net_scripts_dir/ifcfg-$name");
61&read_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
62$conf{'IPADDR'} = $_[0]->{'address'};
63local($ip1, $ip2, $ip3, $ip4) = split(/\./, $_[0]->{'address'});
64$conf{'NETMASK'} = $_[0]->{'netmask'};
65local($nm1, $nm2, $nm3, $nm4) = split(/\./, $_[0]->{'netmask'});
66if ($_[0]->{'address'} && $_[0]->{'netmask'}) {
67	$conf{'NETWORK'} = sprintf "%d.%d.%d.%d",
68				($ip1 & int($nm1))&0xff,
69				($ip2 & int($nm2))&0xff,
70				($ip3 & int($nm3))&0xff,
71				($ip4 & int($nm4))&0xff;
72	}
73else {
74	$conf{'NETWORK'} = '';
75	}
76delete($conf{'PREFIXLEN'});
77$conf{'BROADCAST'} = $_[0]->{'broadcast'};
78$conf{'STARTMODE'} = $_[0]->{'up'} ? "onboot" :
79		     $conf{'STARTMODE'} eq "onboot" ? "manual" :
80						      $conf{'STARTMODE'};
81$conf{'BOOTPROTO'} = $_[0]->{'dhcp'} ? "dhcp" : "static";
82$conf{'UNIQUE'} = time();
83&write_env_file("$net_scripts_dir/ifcfg-$name", \%conf);
84&unlock_file("$net_scripts_dir/ifcfg-$name");
85}
86
87# delete_interface(&details)
88# Delete a boot-time interface
89sub delete_interface
90{
91local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
92				       : $_[0]->{'name'};
93&unlink_logged("$net_scripts_dir/ifcfg-$name");
94}
95
96# can_edit(what)
97# Can some boot-time interface parameter be edited?
98sub can_edit
99{
100return $_[0] ne "mtu" && $_[0] ne "bootp";
101}
102
103sub can_broadcast_def
104{
105return 0;
106}
107
108# valid_boot_address(address)
109# Is some address valid for a bootup interface
110sub valid_boot_address
111{
112return &check_ipaddress($_[0]);
113}
114
115# get_hostname()
116sub get_hostname
117{
118local %conf;
119&read_env_file($network_config, \%conf);
120if ($conf{'HOSTNAME'}) {
121	return $conf{'HOSTNAME'};
122	}
123return &get_system_hostname();
124}
125
126# save_hostname(name)
127sub save_hostname
128{
129local %conf;
130&system_logged("hostname $_[0] >/dev/null 2>&1");
131&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
132&print_tempfile(HOST, $_[0],"\n");
133&close_tempfile(HOST);
134&lock_file($network_config);
135&read_env_file($network_config, \%conf);
136$conf{'HOSTNAME'} = $_[0];
137&write_env_file($network_config, \%conf);
138&unlock_file($network_config);
139undef(@main::get_system_hostname);      # clear cache
140}
141
142# get_domainname()
143sub get_domainname
144{
145local $d;
146&execute_command("domainname", undef, \$d, undef);
147chop($d);
148return $d;
149}
150
151# save_domainname(domain)
152sub save_domainname
153{
154local %conf;
155&execute_command("domainname ".quotemeta($_[0]));
156&read_env_file($network_config, \%conf);
157if ($_[0]) {
158	$conf{'NISDOMAIN'} = $_[0];
159	}
160else {
161	delete($conf{'NISDOMAIN'});
162	}
163&write_env_file($network_config, \%conf);
164}
165
166sub routing_config_files
167{
168return ( $routes_config, $sysctl_config );
169}
170
171sub routing_input
172{
173local (@routes, $i);
174&open_readfile(ROUTES, $routes_config);
175while(<ROUTES>) {
176	s/#.*$//;
177	s/\r|\n//g;
178	local @r = map { $_ eq '-' ? undef : $_ } split(/\s+/, $_);
179	push(@routes, \@r) if (@r);
180	}
181close(ROUTES);
182
183# show default router and device
184local ($def) = grep { $_->[0] eq "default" } @routes;
185print &ui_table_row($text{'routes_default'},
186        &ui_opt_textbox("gateway", $def->[1], 15, $text{'routes_none'}));
187
188print &ui_table_row($text{'routes_device2'},
189        &ui_opt_textbox("gatewaydev", $def->[3], 6, $text{'routes_none'}));
190
191# Forwarding enabled?
192&read_env_file($sysctl_config, \%sysctl);
193print &ui_table_row($text{'routes_forward'},
194        &ui_yesno_radio("forward", $sysctl{'IP_FORWARD'} eq 'yes'));
195
196# show static network routes
197my $i = 0;
198my @table;
199foreach my $r (@routes, [ ]) {
200        next if ($r eq $def);
201        push(@table, [ &ui_textbox("dev_$i", $r->[3], 6),
202                       &ui_textbox("net_$i", $r->[0], 15),
203                       &ui_textbox("netmask_$i", $r->[2], 15),
204                       &ui_textbox("gw_$i", $r->[1], 15),
205                       &ui_textbox("type_$i", $r->[4], 10) ]);
206        }
207print &ui_table_row($text{'routes_static'},         &ui_columns_table([ $text{'routes_ifc'}, $text{'routes_net'},
208                            $text{'routes_mask'}, $text{'routes_gateway'},
209                            $text{'routes_type'} ],
210                          undef, \@table, undef, 1));
211}
212
213sub parse_routing
214{
215# Parse route inputs
216local (@routes, $r, $i);
217if (!$in{'gateway_def'}) {
218	&to_ipaddress($in{'gateway'}) ||
219		&error(&text('routes_edefault', &html_escape($in{'gateway'})));
220	local @def = ( "default", $in{'gateway'}, undef, undef );
221	if (!$in{'gatewaydev_def'}) {
222		$in{'gatewaydev'} =~ /^\S+$/ ||
223			&error(&text('routes_edevice', &html_escape($in{'gatewaydev'})));
224		$def[3] = $in{'gatewaydev'};
225		}
226	push(@routes, \@def);
227	}
228for($i=0; defined($in{"dev_$i"}); $i++) {
229	next if (!$in{"net_$i"});
230	&check_ipaddress($in{"net_$i"}) ||
231		$in{"net_$i"} =~ /^(\S+)\/(\d+)$/ && &check_ipaddress($1) ||
232		&error(&text('routes_enet', &html_escape($in{"net_$i"})));
233	$in{"dev_$i"} =~ /^\S*$/ || &error(&text('routes_edevice', &html_escape($dev)));
234	!$in{"netmask_$i"} || &check_ipaddress($in{"netmask_$i"}) ||
235		&error(&text('routes_emask', &html_escape($in{"netmask_$i"})));
236	!$in{"gw_$i"} || &check_ipaddress($in{"gw_$i"}) ||
237		&error(&text('routes_egateway', &html_escape($in{"gw_$i"})));
238	$in{"type_$i"} =~ /^\S*$/ ||
239		&error(&text('routes_etype', &html_escape($in{"type_$i"})));
240	push(@routes, [ $in{"net_$i"}, $in{"gw_$i"}, $in{"netmask_$i"},
241			$in{"dev_$i"}, $in{"type_$i"} ] );
242	}
243
244# Save routes and routing option
245&open_tempfile(ROUTES, ">$routes_config");
246foreach $r (@routes) {
247	&print_tempfile(ROUTES,join(" ", map { $_ eq '' ? "-" : $_ } @$r),"\n");
248	}
249&close_tempfile(ROUTES);
250local $lref = &read_file_lines($sysctl_config);
251for($i=0; $i<@$lref; $i++) {
252	if ($lref->[$i] =~ /^\s*IP_FORWARD\s*=/) {
253		$lref->[$i] = "IP_FORWARD=".($in{'forward'} ? "yes" : "no");
254		}
255	}
256&flush_file_lines();
257}
258
259# apply_network()
260# Apply the interface and routing settings
261sub apply_network
262{
263&system_logged("(cd / ; /etc/init.d/network stop ; /etc/init.d/network start) >/dev/null 2>&1");
264}
265
266# supports_address6([&iface])
267# Returns 1 if managing IPv6 interfaces is supported
268sub supports_address6
269{
270local ($iface) = @_;
271return 0;
272}
273
2741;
275
276