Handle hostnames with upper-case letters
[webmin.git] / net / slackware-linux-9.1-ALL-lib.pl
1 # slackware-linux-9.1-lib.pl
2 # Networking functions for slackware linux 9.1 and above. Unlike older releases
3 # of slackware, this one actually has a networking config file!!
4
5 do 'linux-lib.pl';
6 $inet_conf = "/etc/rc.d/rc.inet1.conf";
7 %iconfig = &foreign_config("init");
8 $interfaces_file = $iconfig{'local_script'} || $iconfig{'extra_init'};
9
10 # boot_interfaces()
11 # Returns a list of interfaces brought up at boot time
12 sub boot_interfaces
13 {
14 local @rv;
15 local $iface;
16
17 # Add loopback
18 push(@rv, { 'up' => 1,
19             'init' => 1,
20             'edit' => 0,
21             'name' => 'lo',
22             'fullname' => 'lo',
23             'address' => '127.0.0.1',
24             'netmask' => '255.0.0.0' } );
25
26 # Look in inet1.conf file for master interfaces
27 local $lnum = 0;
28 &open_readfile(CONF, $inet_conf);
29 while(<CONF>) {
30         if (/^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/) {
31                 push(@rv, { 'up' => 1,
32                             'init' => 1,
33                             'edit' => 1,
34                             'address' => $2,
35                             'line' => $lnum,
36                             'eline' => $lnum,
37                             'number' => $1,
38                             'file' => $inet_conf,
39                             'name' => 'eth'.$1,
40                             'fullname' => 'eth'.$1 });
41                 }
42         elsif (/^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
43                 $rv[$#rv]->{'netmask'} = $2;
44                 if ($2 =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/ &&
45                     $rv[$#rv]->{'address'}) {
46                         local ($a1, $a2, $a3, $a4) =
47                                 split(/\./, $rv[$#rv]->{'address'});
48                         $rv[$#rv]->{'broadcast'} = sprintf "%d.%d.%d.%d",
49                                                 ($a1 | ~int($1))&0xff,
50                                                 ($a2 | ~int($2))&0xff,
51                                                 ($a3 | ~int($3))&0xff,
52                                                 ($a4 | ~int($4))&0xff;
53                         }
54                 $rv[$#rv]->{'eline'} = $lnum;
55                 }
56         elsif (/^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
57                 $rv[$#rv]->{'dhcp'} = 1 if (lc($2) eq "yes");
58                 $rv[$#rv]->{'eline'} = $lnum;
59                 }
60         elsif (/^\s*\S+\[(\d+)\]\s*=\s*"(.*)"/ && @rv) {
61                 # Some other directive in the current section
62                 $rv[$#rv]->{'eline'} = $lnum;
63                 }
64         $lnum++;
65         }
66 close(CONF);
67
68 # Filter out any unset
69 @rv = grep { $_->{'address'} || $_->{'dhcp'} } @rv;
70 local $i;
71 for($i=0; $i<@rv; $i++) {
72         $rv[$i]->{'index'} = $i;
73         }
74
75 # Read extra init script for virtual interfaces
76 local $lnum = 0;
77 &open_readfile(IFACES, $interfaces_file);
78 while(<IFACES>) {
79         s/\r|\n//g;
80         if (/^(#*)\s*(\S*ifconfig)\s+(\S+)\s+(\S+)(\s+netmask\s+(\S+))?(\s+broadcast\s+(\S+))?(\s+mtu\s+(\d+))?\s+up$/) {
81                 # Found a usable interface line
82                 local $b = { 'fullname' => $3,
83                              'up' => !$1,
84                              'address' => $4,
85                              'netmask' => $6,
86                              'broadcast' => $8,
87                              'mtu' => $10,
88                              'edit' => 1,
89                              'line' => $lnum,
90                              'index' => scalar(@rv) };
91                 if ($b->{'fullname'} =~ /(\S+):(\d+)/) {
92                         $b->{'name'} = $1;
93                         $b->{'virtual'} = $2;
94                         }
95                 else {
96                         $b->{'name'} = $b->{'fullname'};
97                         }
98                 push(@rv, $b);
99                 }
100         $lnum++;
101         }
102 close(IFACES);
103 return @rv;
104 }
105
106 # save_interface(&details)
107 # Create or update a boot-time interface's ifconfig command
108 sub save_interface
109 {
110 # Find in existing config
111 local @boot = &boot_interfaces();
112 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
113
114 if ($old && $old->{'init'}) {
115         # Modifying in inet1.conf file
116         &lock_file($inet_conf);
117         local $lref = &read_file_lines($inet_conf);
118         local $i;
119         for($i=$old->{'line'}; $i<=$old->{'eline'}; $i++) {
120                 if ($lref->[$i] =~ /^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/) {
121                         $lref->[$i] = "IPADDR\[$1\]=\"$_[0]->{'address'}\"";
122                         }
123                 elsif ($lref->[$i] =~ /^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/) {
124                         $lref->[$i] = "NETMASK\[$1\]=\"$_[0]->{'netmask'}\"";
125                         }
126                 elsif ($lref->[$i] =~ /^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/) {
127                         local $dhcp = $_[0]->{'dhcp'} ? "yes" : "";
128                         $lref->[$i] = "USE_DHCP\[$1\]=\"$dhcp\"";
129                         }
130                 }
131         &flush_file_lines();
132         &unlock_file($inet_conf);
133         }
134 elsif (!$old && $_[0]->{'fullname'} =~ /^eth([0-3])$/) {
135         # Adding to inet1.conf file, in the appropriate empty section
136         local $num = $1;
137         &lock_file($inet_conf);
138         local $lref = &read_file_lines($inet_conf);
139         local $i;
140         for($i=0; $i<@$lref; $i++) {
141                 if ($lref->[$i] =~ /^\s*IPADDR\[(\d+)\]\s*=\s*"(.*)"/ &&
142                     $1 == $num) {
143                         $lref->[$i] = "IPADDR\[$1\]=\"$_[0]->{'address'}\"";
144                         }
145                 elsif ($lref->[$i] =~ /^\s*NETMASK\[(\d+)\]\s*=\s*"(.*)"/ &&
146                        $1 == $num) {
147                         $lref->[$i] = "NETMASK\[$1\]=\"$_[0]->{'netmask'}\"";
148                         }
149                 elsif ($lref->[$i] =~ /^\s*USE_DHCP\[(\d+)\]\s*=\s*"(.*)"/ &&
150                        $1 == $num) {
151                         local $dhcp = $_[0]->{'dhcp'} ? "yes" : "";
152                         $lref->[$i] = "USE_DHCP\[$1\]=\"$dhcp\"";
153                         }
154                 }
155         &flush_file_lines();
156         &unlock_file($inet_conf);
157         }
158 else {
159         # Modifying or adding some other interface in separate file
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                 $lref->[$lnum] = &interface_line($_[0]);
166                 }
167         else {
168                 push(@$lref, &interface_line($_[0]));
169                 }
170         &flush_file_lines();
171         &unlock_file($interfaces_file);
172         }
173 }
174
175 # delete_interface(&details)
176 # Delete a boot-time interface's ifconfig command
177 sub delete_interface
178 {
179 # Find in existing config
180 local @boot = &boot_interfaces();
181 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
182
183 if ($old && $old->{'init'}) {
184         # Deleting from inet1.conf file .. just set to blank
185         &lock_file($inet_conf);
186         local $lref = &read_file_lines($inet_conf);
187         local $i;
188         for($i=$old->{'line'}; $i<=$old->{'eline'}; $i++) {
189                 if ($lref->[$i] =~ /^\s*(\S+)\[(\d+)\]\s*=\s*"(.*)"/) {
190                         $lref->[$i] = "$1\[$2\]=\"\"";
191                         }
192                 }
193         &flush_file_lines();
194         &unlock_file($inet_conf);
195         }
196 else {
197         # Deleting from separate file
198         &lock_file($interfaces_file);
199         local $lref = &read_file_lines($interfaces_file);
200         local $lnum = defined($_[0]->{'line'}) ? $_[0]->{'line'}
201                                                : &interface_lnum($_[0]);
202         if (defined($lnum)) {
203                 splice(@$lref, $lnum, 1);
204                 }
205         &flush_file_lines();
206         &unlock_file($interfaces_file);
207         }
208 }
209
210 sub interface_lnum
211 {
212 local @boot = &boot_interfaces();
213 local ($found) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
214 return $found ? $found->{'line'} : undef;
215 }
216
217 sub interface_line
218 {
219 local $str;
220 $str .= "# " if (!$_[0]->{'up'});
221 $str .= &has_command("ifconfig");
222 if (!$_[0]->{'fullname'}) {
223         $_[0]->{'fullname'} = $_[0]->{'virtual'} ne "" ?
224                 $_[0]->{'name'}.":".$_[0]->{'virtual'} : $_[0]->{'name'};
225         }
226 $str .= " $_[0]->{'fullname'} $_[0]->{'address'}";
227 if ($_[0]->{'netmask'}) {
228         $str .= " netmask $_[0]->{'netmask'}";
229         }
230 if ($_[0]->{'broadcast'}) {
231         $str .= " broadcast $_[0]->{'broadcast'}";
232         }
233 if ($_[0]->{'mtu'}) {
234         $str .= " mtu $_[0]->{'mtu'}";
235         }
236 $str .= " up";
237 return $str;
238 }
239
240 # can_edit(what)
241 # Can some boot-time interface parameter be edited?
242 sub can_edit
243 {
244 return $_[0] ne "bootp" && $_[0] ne "mtu";
245 }
246
247 # valid_boot_address(address)
248 # Is some address valid for a bootup interface
249 sub valid_boot_address
250 {
251 return &check_ipaddress($_[0]);
252 }
253
254 # get_hostname()
255 sub get_hostname
256 {
257 local $hn = &read_file_contents("/etc/HOSTNAME");
258 $hn =~ s/\r|\n//g;
259 if ($hn) {
260         return $hn;
261         }
262 return &get_system_hostname(1);
263 }
264
265 # save_hostname(name)
266 sub save_hostname
267 {
268 &system_logged("hostname $_[0] >/dev/null 2>&1");
269 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
270 &print_tempfile(HOST, $_[0],"\n");
271 &close_tempfile(HOST);
272 undef(@main::get_system_hostname);      # clear cache
273 }
274
275 sub routing_config_files
276 {
277 return ( $inet_conf );
278 }
279
280 sub routing_input
281 {
282 &open_readfile(INIT, $inet_conf);
283 while(<INIT>) {
284         s/\r|\n//g;
285         s/#.*$//;
286         if (/^\s*GATEWAY\s*=\s*"(.*)"/) {
287                 $gw = $1;
288                 }
289         }
290 close(INIT);
291 print &ui_table_row($text{'routes_default'},
292         &ui_opt_textbox("gw", $gw, 20, $text{'routes_none'},
293                         $text{'routes_gateway'}));
294 }
295
296 sub parse_routing
297 {
298 local $gw = "";
299 if (!$in{'gw_def'}) {
300         &check_ipaddress($in{'gw'}) ||
301                 &error(&text('routes_edefault', $in{'gw'}));
302         $gw = $in{'gw'};
303         }
304 &lock_file($inet_conf);
305 local $lref = &read_file_lines($inet_conf);
306 foreach $l (@$lref) {
307         if ($l =~ /^(\s*)GATEWAY\s*=\s*"(.*)"(.*)/) {
308                 $l = $1."GATEWAY=\"".$gw."\"".$3;
309                 }
310         }
311 &flush_file_lines();
312 &unlock_file($inet_conf);
313 }
314
315 # supports_address6([&iface])
316 # Returns 1 if managing IPv6 interfaces is supported
317 sub supports_address6
318 {
319 local ($iface) = @_;
320 return 0;
321 }
322
323