1# slackware-linux-lib.pl
2# Networking functions for slackware linux
3# To support boot-time interfaces, ifconfig commands are added to rc.local so
4# that additional virtual interfaces can be created
5
6do 'linux-lib.pl';
7%iconfig = &foreign_config("init");
8$interfaces_file = $iconfig{'local_script'} || $iconfig{'extra_init'};
9$rc_init = "/etc/rc.d/rc.inet1";
10$dhcp_init = "/etc/rc.d/rc.dhcpd";
11
12# boot_interfaces()
13# Returns a list of interfaces brought up at boot time
14sub boot_interfaces
15{
16local @rv;
17
18# Look in rc.init1 file for master interface
19local $iface = { 'up' => 1,
20		 'edit' => 1,
21		 'index' => 0,
22		 'init' => 1,
23		 'name' => 'eth0',
24		 'fullname' => 'eth0',
25		 'file' => $rc_init };
26local $gotdevice;
27&open_readfile(INIT, $rc_init);
28while(<INIT>) {
29	s/\r|\n//g;
30	s/#.*$//;
31	if (/^\s*IPADDR\s*=\s*["']?([0-9\.]+)/) {
32		$iface->{'address'} = $1;
33		}
34	elsif (/^\s*DEVICE\s*=\s*["']?([0-9\.]+)/) {
35		$iface->{'name'} = $iface->{'fullname'} = $1;
36		$gotdevice++;
37		}
38	elsif (/^\s*NETMASK\s*=\s*["']?([0-9\.]+)/) {
39		$iface->{'netmask'} = $1;
40		}
41	elsif (/^\s*BROADCAST\s*=\s*["']?([0-9\.]+)/) {
42		$iface->{'broadcast'} = $1;
43		}
44	elsif (/^\s*DHCP\s*=\s*["']?([0-9\.]+)/) {
45		$iface->{'dhcp'} = ($1 eq "yes");
46		}
47	elsif (/^\s*ifconfig\s+(\S+)\s+.*IPADDR.*/ && !$gotdevice) {
48		$iface->{'name'} = $iface->{'fullname'} = $1;
49		}
50	}
51close(INIT);
52local @st1 = stat($rc_init);
53local @st2 = stat($dhcp_init);
54if ($st1[7] == $st2[7]) {
55	# Looks like rc.dhcpd script has been copied to rc.inet1 - assume DHCP
56	$iface->{'dhcp'} = 1;
57	}
58push(@rv, $iface) if ($iface->{'address'} || $iface->{'dhcp'});
59
60# Read extra init script for virtual interfaces
61local $lnum = 0;
62&open_readfile(IFACES, $interfaces_file);
63while(<IFACES>) {
64	s/\r|\n//g;
65	if (/^(#*)\s*(\S*ifconfig)\s+(\S+)\s+(\S+)(\s+netmask\s+(\S+))?(\s+broadcast\s+(\S+))?(\s+mtu\s+(\d+))?\s+up$/) {
66		# Found a usable interface line
67		local $b = { 'fullname' => $3,
68			     'up' => !$1,
69			     'address' => $4,
70			     'netmask' => $6,
71			     'broadcast' => $8,
72			     'mtu' => $10,
73			     'edit' => 1,
74			     'line' => $lnum,
75			     'file' => $interfaces_file,
76			     'index' => scalar(@rv) };
77		if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
78			$b->{'name'} = $1;
79			$b->{'virtual'} = $2;
80			}
81		else {
82			$b->{'name'} = $b->{'fullname'};
83			}
84		push(@rv, $b);
85		}
86	$lnum++;
87	}
88close(IFACES);
89return @rv;
90}
91
92# save_interface(&details)
93# Create or update a boot-time interface's ifconfig command
94sub save_interface
95{
96if ($_[0]->{'index'} == 0 && $_[0]->{'fullname'} eq 'eth0') {
97	# Modifying the primary interface
98	&lock_file($rc_init);
99	if ($_[0]->{'dhcp'} && -r $dhcp_init) {
100		# Just copy rc.dhcpd to rc.inet1
101		&system_logged("cp $dhcp_init $rc_init");
102		}
103	else {
104		# Is the current file rc.dhcpd?
105		if (!$_[0]->{'dhcp'}) {
106			local @st1 = stat($rc_init);
107			local @st2 = stat($dhcp_init);
108			if ($st1[7] == $st2[7]) {
109				# Yes! Use built-in static IP version
110				&system_logged("cp $module_root_directory/rc.inet1 $rc_init");
111				}
112			}
113
114		# Update init script with new settings
115		local $lref = &read_file_lines($rc_init);
116		foreach $l (@$lref) {
117			if ($l =~ /^(\s*)IPADDR\s*=\s*(\S+)(.*)/) {
118				$l = $1."IPADDR=\"".$_[0]->{'address'}."\"".$3;
119				}
120			elsif ($l =~ /^(\s*)NETMASK\s*=\s*(\S+)(.*)/) {
121				$l = $1."NETMASK=\"".$_[0]->{'netmask'}."\"".$3;
122				}
123			elsif ($l =~ /^(\s*)BROADCAST\s*=\s*(\S+)(.*)/) {
124				$l = $1."BROADCAST=\"".$_[0]->{'broadcast'}."\"".$3;
125				}
126			if ($l =~ /^(\s*)DHCP\s*=\s*(\S+)(.*)/) {
127				$l = $1."DHCP=\"".($_[0]->{'dhcp'} ? "yes" : "no")."\"".$3;
128				}
129			}
130		&flush_file_lines();
131		}
132	&unlock_file($rc_init);
133	}
134else {
135	# Modifying or adding some other interface
136	$_[0]->{'dhcp'} && &error($text{'bifc_edhcpmain'});
137	&lock_file($interfaces_file);
138	local $lref = &read_file_lines($interfaces_file);
139	local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
140					       : &interface_lnum($_[0]);
141	if (defined($lnum)) {
142		$lref->[$lnum] = &interface_line($_[0]);
143		}
144	else {
145		push(@$lref, &interface_line($_[0]));
146		}
147	&flush_file_lines();
148	&unlock_file($interfaces_file);
149	}
150}
151
152# delete_interface(&details)
153# Delete a boot-time interface's ifconfig command
154sub delete_interface
155{
156if ($_[0]->{'init'}) {
157	&error("The primary network interface cannot be deleted");
158	}
159else {
160	&lock_file($interfaces_file);
161	local $lref = &read_file_lines($interfaces_file);
162	local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
163					       : &interface_lnum($_[0]);
164	if (defined($lnum)) {
165		splice(@$lref, $lnum, 1);
166		}
167	&flush_file_lines();
168	&unlock_file($interfaces_file);
169	}
170}
171
172sub interface_lnum
173{
174local @boot = &boot_interfaces();
175local ($found) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
176return $found ? $found->{'line'} : undef;
177}
178
179sub interface_line
180{
181local $str;
182$str .= "# " if (!$_[0]->{'up'});
183$str .= &has_command("ifconfig");
184if (!$_[0]->{'fullname'}) {
185	$_[0]->{'fullname'} = $_[0]->{'virtual'} ne "" ?
186		$_[0]->{'name'}.":".$_[0]->{'virtual'} : $_[0]->{'name'};
187	}
188$str .= " $_[0]->{'fullname'} $_[0]->{'address'}";
189if ($_[0]->{'netmask'}) {
190	$str .= " netmask $_[0]->{'netmask'}";
191	}
192if ($_[0]->{'broadcast'}) {
193	$str .= " broadcast $_[0]->{'broadcast'}";
194	}
195if ($_[0]->{'mtu'}) {
196	$str .= " mtu $_[0]->{'mtu'}";
197	}
198$str .= " up";
199return $str;
200}
201
202# can_edit(what)
203# Can some boot-time interface parameter be edited?
204sub can_edit
205{
206return $_[0] ne "bootp";
207}
208
209sub can_broadcast_def
210{
211return 0;
212}
213
214# valid_boot_address(address)
215# Is some address valid for a bootup interface
216sub valid_boot_address
217{
218return &check_ipaddress($_[0]);
219}
220
221# get_hostname()
222sub get_hostname
223{
224local $hn = &read_file_contents("/etc/HOSTNAME");
225$hn =~ s/\r|\n//g;
226if ($hn) {
227	return $hn;
228	}
229return &get_system_hostname();
230}
231
232# save_hostname(name)
233sub save_hostname
234{
235&system_logged("hostname $_[0] >/dev/null 2>&1");
236&open_lock_tempfile(HOST, ">/etc/HOSTNAME");
237&print_tempfile(HOST, $_[0],"\n");
238&close_tempfile(HOST);
239undef(@main::get_system_hostname);      # clear cache
240}
241
242sub routing_config_files
243{
244return ( $rc_init );
245}
246
247sub routing_input
248{
249&open_readfile(INIT, $rc_init);
250while(<INIT>) {
251	s/\r|\n//g;
252        s/#.*$//;
253	if (/^\s*GATEWAY\s*=\s*["']?([0-9\.]+)/) {
254		$gw = $1;
255		}
256	}
257close(INIT);
258print &ui_table_row($text{'routes_default'},
259	&ui_opt_textbox("gw", $gw, 20, $text{'routes_none'},
260			$text{'routes_gateway'}));
261}
262
263sub parse_routing
264{
265local $gw = "";
266if (!$in{'gw_def'}) {
267	&check_ipaddress($in{'gw'}) ||
268		&error(&text('routes_edefault', &html_escape($in{'gw'})));
269	$gw = $in{'gw'};
270	}
271&lock_file($rc_init);
272local $lref = &read_file_lines($rc_init);
273foreach $l (@$lref) {
274	if ($l =~ /^(\s*)GATEWAY\s*=\s*(\S+)(.*)/) {
275		$l = $1."GATEWAY=\"".$gw."\"".$3;
276		}
277	}
278&flush_file_lines();
279&unlock_file($rc_init);
280}
281
282
283# supports_address6([&iface])
284# Returns 1 if managing IPv6 interfaces is supported
285sub supports_address6
286{
287local ($iface) = @_;
288return 0;
289}
290
291
2921;
293
294