Handle hostnames with upper-case letters
[webmin.git] / net / slackware-linux-lib.pl
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
6 do '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
14 sub boot_interfaces
15 {
16 local @rv;
17
18 # Look in rc.init1 file for master interface
19 local $iface = { 'up' => 1,
20                  'edit' => 1,
21                  'index' => 0,
22                  'init' => 1,
23                  'name' => 'eth0',
24                  'fullname' => 'eth0',
25                  'file' => $rc_init };
26 local $gotdevice;
27 &open_readfile(INIT, $rc_init);
28 while(<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         }
51 close(INIT);
52 local @st1 = stat($rc_init);
53 local @st2 = stat($dhcp_init);
54 if ($st1[7] == $st2[7]) {
55         # Looks like rc.dhcpd script has been copied to rc.inet1 - assume DHCP
56         $iface->{'dhcp'} = 1;
57         }
58 push(@rv, $iface) if ($iface->{'address'} || $iface->{'dhcp'});
59
60 # Read extra init script for virtual interfaces
61 local $lnum = 0;
62 &open_readfile(IFACES, $interfaces_file);
63 while(<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         }
88 close(IFACES);
89 return @rv;
90 }
91
92 # save_interface(&details)
93 # Create or update a boot-time interface's ifconfig command
94 sub save_interface
95 {
96 if ($_[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         }
134 else {
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
154 sub delete_interface
155 {
156 if ($_[0]->{'init'}) {
157         &error("The primary network interface cannot be deleted");
158         }
159 else {
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
172 sub interface_lnum
173 {
174 local @boot = &boot_interfaces();
175 local ($found) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
176 return $found ? $found->{'line'} : undef;
177 }
178
179 sub interface_line
180 {
181 local $str;
182 $str .= "# " if (!$_[0]->{'up'});
183 $str .= &has_command("ifconfig");
184 if (!$_[0]->{'fullname'}) {
185         $_[0]->{'fullname'} = $_[0]->{'virtual'} ne "" ?
186                 $_[0]->{'name'}.":".$_[0]->{'virtual'} : $_[0]->{'name'};
187         }
188 $str .= " $_[0]->{'fullname'} $_[0]->{'address'}";
189 if ($_[0]->{'netmask'}) {
190         $str .= " netmask $_[0]->{'netmask'}";
191         }
192 if ($_[0]->{'broadcast'}) {
193         $str .= " broadcast $_[0]->{'broadcast'}";
194         }
195 if ($_[0]->{'mtu'}) {
196         $str .= " mtu $_[0]->{'mtu'}";
197         }
198 $str .= " up";
199 return $str;
200 }
201
202 # can_edit(what)
203 # Can some boot-time interface parameter be edited?
204 sub can_edit
205 {
206 return $_[0] ne "bootp";
207 }
208
209 # valid_boot_address(address)
210 # Is some address valid for a bootup interface
211 sub valid_boot_address
212 {
213 return &check_ipaddress($_[0]);
214 }
215
216 # get_hostname()
217 sub get_hostname
218 {
219 local $hn = &read_file_contents("/etc/HOSTNAME");
220 $hn =~ s/\r|\n//g;
221 if ($hn) {
222         return $hn;
223         }
224 return &get_system_hostname(1);
225 }
226
227 # save_hostname(name)
228 sub save_hostname
229 {
230 &system_logged("hostname $_[0] >/dev/null 2>&1");
231 &open_lock_tempfile(HOST, ">/etc/HOSTNAME");
232 &print_tempfile(HOST, $_[0],"\n");
233 &close_tempfile(HOST);
234 undef(@main::get_system_hostname);      # clear cache
235 }
236
237 sub routing_config_files
238 {
239 return ( $rc_init );
240 }
241
242 sub routing_input
243 {
244 &open_readfile(INIT, $rc_init);
245 while(<INIT>) {
246         s/\r|\n//g;
247         s/#.*$//;
248         if (/^\s*GATEWAY\s*=\s*["']?([0-9\.]+)/) {
249                 $gw = $1;
250                 }
251         }
252 close(INIT);
253 print &ui_table_row($text{'routes_default'},
254         &ui_opt_textbox("gw", $gw, 20, $text{'routes_none'},
255                         $text{'routes_gateway'}));
256 }
257
258 sub parse_routing
259 {
260 local $gw = "";
261 if (!$in{'gw_def'}) {
262         &check_ipaddress($in{'gw'}) ||
263                 &error(&text('routes_edefault', $in{'gw'}));
264         $gw = $in{'gw'};
265         }
266 &lock_file($rc_init);
267 local $lref = &read_file_lines($rc_init);
268 foreach $l (@$lref) {
269         if ($l =~ /^(\s*)GATEWAY\s*=\s*(\S+)(.*)/) {
270                 $l = $1."GATEWAY=\"".$gw."\"".$3;
271                 }
272         }
273 &flush_file_lines();
274 &unlock_file($rc_init);
275 }
276
277
278 # supports_address6([&iface])
279 # Returns 1 if managing IPv6 interfaces is supported
280 sub supports_address6
281 {
282 local ($iface) = @_;
283 return 0;
284 }
285
286
287 1;
288