Handle hostnames with upper-case letters
[webmin.git] / net / unixware-lib.pl
1 # unixware-lib.pl
2 # Networking functions for UnixWare
3
4 # active_interfaces()
5 # Returns a list of currently ifconfig'd interfaces
6 sub active_interfaces
7 {
8 local(@rv, @lines, $l);
9 &open_execute_command(IFC, "ifconfig -a", 1, 1);
10 while(<IFC>) {
11         s/\r|\n//g;
12         if (/^\S+:/) { push(@lines, $_); }
13         else { $lines[$#lines] .= $_; }
14         }
15 close(IFC);
16 foreach $l (@lines) {
17         local %ifc;
18         $l =~ /^([^:\s]+):/; $ifc{'name'} = $1;
19         $l =~ /^(\S+):/; $ifc{'fullname'} = $1;
20         if ($l =~ /^(\S+):(\d+):\s/) { $ifc{'virtual'} = $2; }
21         if ($l =~ /inet\s+(\S+)/) { $ifc{'address'} = $1; }
22         if ($l =~ /netmask\s+(\S+)/) { $ifc{'netmask'} = &parse_hex($1); }
23         if ($l =~ /broadcast\s+(\S+)/) { $ifc{'broadcast'} = $1; }
24         if ($l =~ /ether\s+(\S+)/) { $ifc{'ether'} = $1; }
25         if ($l =~ /mtu\s+(\S+)/) { $ifc{'mtu'} = $1; }
26         $ifc{'up'}++ if ($l =~ /\<UP/);
27         $ifc{'edit'} = ($ifc{'name'} !~ /ipdptp|ppp/);
28         $ifc{'index'} = scalar(@rv);
29         if ($ifc{'ether'}) {
30                 $ifc{'ether'} = join(":", map { sprintf "%2.2d", $_ }
31                                               split(/:/, $ifc{'ether'}));
32                 }
33         push(@rv, \%ifc);
34         }
35 return @rv;
36 }
37
38 # activate_interface(&details)
39 # Create or modify an interface
40 sub activate_interface
41 {
42 local $a = $_[0];
43 if ($a->{'virtual'} eq "") {
44         local $out = &backquote_logged("ifconfig $a->{'name'} plumb 2>&1");
45         if ($out) { &error("Interface '$a->{'name'}' does not exist"); }
46         }
47 local $cmd = "ifconfig $a->{'name'}";
48 if ($a->{'virtual'} ne "") { $cmd .= ":$a->{'virtual'}"; }
49 $cmd .= " $a->{'address'}";
50 if ($a->{'netmask'}) { $cmd .= " netmask $a->{'netmask'}"; }
51 else { $cmd .= " netmask +"; }
52 if ($a->{'broadcast'}) { $cmd .= " broadcast $a->{'broadcast'}"; }
53 else { $cmd .= " broadcast +"; }
54 if ($a->{'mtu'}) { $cmd .= " mtu $a->{'mtu'}"; }
55 if ($a->{'up'}) { $cmd .= " up"; }
56 else { $cmd .= " down"; }
57 local $out = &backquote_logged("$cmd 2>&1");
58 if ($?) { &error($out); }
59 if ($a->{'ether'}) {
60         $out = &backquote_logged(
61                 "ifconfig $a->{'name'} ether $a->{'ether'} 2>&1");
62         if ($? && $out !~ /Device busy/) { &error($out); }
63         }
64 }
65
66 # deactivate_interface(&details)
67 # Deactive an interface
68 sub deactivate_interface
69 {
70 local $cmd;
71 if ($a->{'virtual'} eq "") {
72         $cmd = "ifconfig $a->{'name'} unplumb";
73         }
74 else {
75         $cmd = "ifconfig $a->{'name'}:$a->{'virtual'} 0.0.0.0 down";
76         }
77 local $out = &backquote_logged("$cmd 2>&1");
78 if ($?) { &error($out); }
79 }
80
81 # boot_interfaces()
82 # Returns a list of interfaces brought up at boot time
83 sub boot_interfaces
84 {
85 local (@rv, $f, %mask);
86 push(@rv, { 'name' => 'lo0',
87             'fullname' => 'lo0',
88             'address' => '127.0.0.1',
89             'netmask' => '255.0.0.0',
90             'up' => 1,
91             'edit' => 0 });
92 open(MASK, "/etc/netmasks");
93 while(<MASK>) {
94         s/\r|\n//g;
95         s/#.*$//g;
96         if (/([0-9\.]+)\s+([0-9\.]+)/) {
97                 $mask{$1} = $2;
98                 }
99         }
100 close(MASK);
101 opendir(ETC, "/etc");
102 while($f = readdir(ETC)) {
103         if ($f =~ /^hostname.(\S+):(\d+)$/ || $f =~ /^hostname.(\S+)/) {
104                 local %ifc;
105                 $ifc{'fullname'} = $ifc{'name'} = $1;
106                 $ifc{'virtual'} = $2 if (defined($2));
107                 $ifc{'fullname'} .= ":$2" if (defined($2));
108                 $ifc{'index'} = scalar(@rv);
109                 $ifc{'edit'}++;
110                 $ifc{'file'} = "/etc/$f";
111                 open(FILE, "/etc/$f");
112                 chop($ifc{'address'} = <FILE>);
113                 close(FILE);
114                 if ($ifc{'address'}) {
115                         &to_ipaddress($ifc{'address'})
116                                 =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
117                         if ($mask{"$1.$2.$3.0"})
118                                 { $ifc{'netmask'} = $mask{"$1.$2.$3.0"}; }
119                         elsif ($mask{"$1.$2.0.0"})
120                                 { $ifc{'netmask'} = $mask{"$1.$2.0.0"}; }
121                         elsif ($mask{"$1.0.0.0"})
122                                 { $ifc{'netmask'} = $mask{"$1.0.0.0"}; }
123                         else
124                                 { $ifc{'netmask'} = "255.255.255.0"; }
125                         local ($a1, $a2, $a3, $a4) = ($1, $2, $3, $4);
126                         $ifc{'netmask'} =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
127                         $ifc{'broadcast'} = sprintf "%d.%d.%d.%d",
128                                                 ($a1 | ~int($1))&0xff,
129                                                 ($a2 | ~int($2))&0xff,
130                                                 ($a3 | ~int($3))&0xff,
131                                                 ($a4 | ~int($4))&0xff;
132                         }
133                 else {
134                         $ifc{'netmask'} = "Automatic";
135                         $ifc{'broadcast'} = "Automatic";
136                         $ifc{'dhcp'}++;
137                         }
138                 $ifc{'up'}++;
139                 push(@rv, \%ifc);
140                 }
141         }
142 closedir(ETC);
143 return @rv;
144 }
145
146 # save_interface(&details)
147 # Create or update a boot-time interface
148 sub save_interface
149 {
150 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
151                                        : $_[0]->{'name'};
152 &open_lock_tempfile(IFACE, ">/etc/hostname.$name");
153 if (!$_[0]->{'dhcp'}) {
154         &print_tempfile(IFACE, $_[0]->{'address'},"\n");
155         }
156 &close_tempfile(IFACE);
157 }
158
159 # delete_interface(&details)
160 # Delete a boot-time interface
161 sub delete_interface
162 {
163 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
164                                        : $_[0]->{'name'};
165 &unlink_logged("/etc/hostname.$name");
166 }
167
168 # iface_type(name)
169 # Returns a human-readable interface type name
170 sub iface_type
171 {
172 return "Fast Ethernet" if ($_[0] =~ /^hme/);
173 return "Loopback" if ($_[0] =~ /^lo/);
174 return "Token Ring" if ($_[0] =~ /^tr/);
175 return "PPP" if ($_[0] =~ /^ipdptp/ || $_[0] =~ /^ppp/);
176 return "Ethernet";
177 }
178
179 # iface_hardware(name)
180 # Does some interface have an editable hardware address
181 sub iface_hardware
182 {
183 return $_[0] !~ /^(lo|ipdptp|ppp)/;
184 }
185
186 # can_edit(what)
187 # Can some boot-time interface parameter be edited?
188 sub can_edit
189 {
190 return $_[0] eq "dhcp";
191 }
192
193 # valid_boot_address(address)
194 # Is some address valid for a bootup interface
195 sub valid_boot_address
196 {
197 return &to_ipaddress($_[0]) ? 1 : 0;
198 }
199
200 # get_dns_config()
201 # Returns a hashtable containing keys nameserver, domain, search & order
202 sub get_dns_config
203 {
204 local $dns;
205 open(RESOLV, "/etc/resolv.conf");
206 while(<RESOLV>) {
207         s/\r|\n//g;
208         s/#.*$//g;
209         if (/nameserver\s+(.*)/) {
210                 push(@{$dns->{'nameserver'}}, split(/\s+/, $1));
211                 }
212         elsif (/domain\s+(\S+)/) {
213                 $dns->{'domain'} = [ $1 ];
214                 }
215         elsif (/search\s+(.*)/) {
216                 $dns->{'domain'} = [ split(/\s+/, $1) ];
217                 }
218         }
219 close(RESOLV);
220 open(SWITCH, "/etc/nsswitch.conf");
221 while(<SWITCH>) {
222         s/\r|\n//g;
223         if (/hosts:\s+(.*)/) {
224                 $dns->{'order'} = $1;
225                 }
226         }
227 close(SWITCH);
228 $dns->{'files'} = [ "/etc/resolv.conf", "/etc/nsswitch.conf" ];
229 return $dns;
230 }
231
232 # save_dns_config(&config)
233 # Writes out the resolv.conf and nsswitch.conf files
234 sub save_dns_config
235 {
236 &lock_file("/etc/resolv.conf");
237 open(RESOLV, "/etc/resolv.conf");
238 local @resolv = <RESOLV>;
239 close(RESOLV);
240 &open_tempfile(RESOLV, ">/etc/resolv.conf");
241 foreach (@{$_[0]->{'nameserver'}}) {
242         &print_tempfile(RESOLV, "nameserver $_\n");
243         }
244 if ($_[0]->{'domain'}) {
245         if ($_[0]->{'domain'}->[1]) {
246                 &print_tempfile(RESOLV, "search ",join(" ", @{$_[0]->{'domain'}}),"\n");
247                 }
248         else {
249                 &print_tempfile(RESOLV, "domain $_[0]->{'domain'}->[0]\n");
250                 }
251         }
252 foreach (@resolv) {
253         &print_tempfile(RESOLV, $_) if (!/^\s*(nameserver|domain|search)\s+/);
254         }
255 &close_tempfile(RESOLV);
256 &unlock_file("/etc/resolv.conf");
257
258 &lock_file("/etc/nsswitch.conf");
259 open(SWITCH, "/etc/nsswitch.conf");
260 local @switch = <SWITCH>;
261 close(SWITCH);
262 &open_tempfile(SWITCH, ">/etc/nsswitch.conf");
263 foreach (@switch) {
264         if (/hosts:\s+/) {
265                 &print_tempfile(SWITCH, "hosts:\t$_[0]->{'order'}\n");
266                 }
267         else {
268                 &print_tempfile(SWITCH, $_);
269                 }
270         }
271 &close_tempfile(SWITCH);
272 &unlock_file("/etc/nsswitch.conf");
273 }
274
275 $max_dns_servers = 3;
276
277 # order_input(&dns)
278 # Returns HTML for selecting the name resolution order
279 sub order_input
280 {
281 return &common_order_input("order", $_[0]->{'order'},
282         [ [ "files", "Hosts" ], [ "dns", "DNS" ], [ "nis", "NIS" ],
283           [ "nisplus", "NIS+" ] ]);
284 }
285
286 # parse_order(&dns)
287 # Parses the form created by order_input()
288 sub parse_order
289 {
290 if (defined($in{'order'})) {
291         $in{'order'} =~ /\S/ || &error($text{'dns_eorder'});
292         $_[0]->{'order'} = $in{'order'};
293         }
294 else {
295         local($i, @order);
296         for($i=0; defined($in{"order_$i"}); $i++) {
297                 push(@order, $in{"order_$i"}) if ($in{"order_$i"});
298                 }
299         $_[0]->{'order'} = join(" ", @order);
300         }
301 }
302
303 sub get_hostname
304 {
305 return &get_system_hostname();
306 }
307
308 # save_hostname(name)
309 sub save_hostname
310 {
311 &system_logged("hostname $_[0] >/dev/null 2>&1");
312 undef(@main::get_system_hostname);      # clear cache
313 }
314
315 # get_domainname()
316 sub get_domainname
317 {
318 local $d;
319 &execute_command("domainname", undef, \$d, undef);
320 chop($d);
321 return $d;
322 }
323
324 # save_domainname(domain)
325 sub save_domainname
326 {
327 &system_logged("domainname ".quotemeta($_[0]));
328 &lock_file("/etc/defaultdomain");
329 if ($_[0]) {
330         &open_tempfile(DOMAIN, ">/etc/defaultdomain");
331         &print_tempfile(DOMAIN, $_[0],"\n");
332         &close_tempfile(DOMAIN);
333         }
334 else {
335         &unlink_file("/etc/defaultdomain");
336         }
337 &unlock_file("/etc/defaultdomain");
338 }
339
340 sub routing_config_files
341 {
342 return ( "/etc/defaultrouter", "/etc/notrouter", "/etc/gateways" );
343 }
344
345 sub routing_input
346 {
347 # show default router(s) input
348 local(@defrt);
349 &open_readfile(DEFRT, "/etc/defaultrouter");
350 while(<DEFRT>) {
351         s/#.*$//g;
352         if (/(\S+)/) { push(@defrt, $1); }
353         }
354 close(DEFRT);
355 print &ui_table_row($text{'routes_defaults'},
356         &ui_textarea("defrt", join("\n", @defrt), 3, 40));
357
358 # show router input
359 local $notrt = (-r "/etc/notrouter");
360 local $gatew = (-r "/etc/gateways");
361 print &ui_table_row($text{'routes_forward'},
362         &ui_radio("router", $gatew && !$notrt ? 0 :
363                             !$gatew && !$notrt ? 1 : 2,
364                   [ [ 0, $text{'yes'} ],
365                     [ 1, $text{'routes_possible'} ],
366                     [ 2, $text{'no'} ] ]));
367 }
368
369 sub parse_routing
370 {
371 local @defrt = split(/\s+/, $in{'defrt'});
372 foreach my $d (@defrt) {
373         &to_ipaddress($d) || &error(&text('routes_edefault', $d));
374         }
375 &lock_file("/etc/defaultrouter");
376 if (@defrt) {
377         &open_tempfile(DEFRT, ">/etc/defaultrouter");
378         foreach $d (@defrt) { &print_tempfile(DEFRT, $d,"\n"); }
379         &close_tempfile(DEFRT);
380         }
381 else {
382         &unlink_file("/etc/defaultrouter");
383         }
384 &unlock_file("/etc/defaultrouter");
385
386 &lock_file("/etc/gateways");
387 &lock_file("/etc/notrouter");
388 if ($in{'router'} == 0) {
389         &create_empty_file("/etc/gateways");
390         &unlink_file("/etc/notrouter");
391         }
392 elsif ($in{'router'} == 2) {
393         &create_empty_file("/etc/notrouter");
394         &unlink_file("/etc/gateways");
395         }
396 else {
397         &unlink_file("/etc/gateways");
398         &unlink_file("/etc/notrouter");
399         }
400 &unlock_file("/etc/gateways");
401 &unlock_file("/etc/notrouter");
402 }
403
404 # create_empty_file(filename)
405 sub create_empty_file
406 {
407 if (!-r $_[0]) {
408         &open_tempfile(EMPTY,">$_[0]");
409         &close_tempfile(EMPTY);
410         }
411 }
412
413 sub os_feedback_files
414 {
415 opendir(DIR, "/etc");
416 local @f = map { "/etc/$_" } grep { /^hostname\./ } readdir(DIR);
417 closedir(DIR);
418 return ( @f, "/etc/netmasks", "/etc/resolv.conf", "/etc/nsswitch.conf",
419          "/etc/defaultrouter", "/etc/notrouter", "/etc/gateways" );
420 }
421
422 # supports_address6([&iface])
423 # Returns 1 if managing IPv6 interfaces is supported
424 sub supports_address6
425 {
426 local ($iface) = @_;
427 return 0;
428 }
429
430 1;
431