Handle hostnames with upper-case letters
[webmin.git] / net / macos-lib.pl
1 # macos-lib.pl
2 # Networking functions for OSX
3
4 $virtual_netmask = "255.255.255.255";   # Netmask for virtual interfaces
5
6 $iftab_file = "/etc/iftab";
7 $hostconfig_file = "/etc/hostconfig";
8
9 # active_interfaces()
10 # Returns a list of currently ifconfig'd interfaces
11 sub active_interfaces
12 {
13 local(@rv, @lines, $l);
14 &open_execute_command(IFC, "ifconfig -a", 1, 1);
15 while(<IFC>) {
16         s/\r|\n//g;
17         if (/^\S+:/) { push(@lines, $_); }
18         elsif (@lines) { $lines[$#lines] .= $_; }
19         }
20 close(IFC);
21 foreach $l (@lines) {
22         local %ifc;
23         $l =~ /^([^:\s]+):/;
24         $ifc{'name'} = $ifc{'fullname'} = $1;
25         if ($l =~ /^(\S+):(\d+):\s/) { $ifc{'virtual'} = $2; }
26         if ($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)//) {
27                 $ifc{'address'} = $1;
28                 $ifc{'netmask'} = &parse_hex($2);
29                 $ifc{'broadcast'} = $3;
30                 }
31         elsif ($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)//) {
32                 $ifc{'address'} = $1;
33                 $ifc{'netmask'} = &parse_hex($2);
34                 }
35         else { next; }
36         if ($l =~ /ether\s+(\S+)/) { $ifc{'ether'} = $1; }
37         if ($l =~ /mtu\s+(\S+)/) { $ifc{'mtu'} = $1; }
38         $ifc{'up'}++ if ($l =~ /\<UP/);
39         $ifc{'edit'} = &iface_type($ifc{'name'}) =~ /ethernet|loopback/i;
40         $ifc{'index'} = scalar(@rv);
41         if ($ifc{'ether'}) {
42                 $ifc{'ether'} = join(":", map { sprintf "%2.2d", $_ }
43                                               split(/:/, $ifc{'ether'}));
44                 }
45         push(@rv, \%ifc);
46
47         # Add aliases as virtual interfaces
48         local $v = 0;
49         while($l =~ s/inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)//) {
50                 local %vifc = %ifc;
51                 $vifc{'address'} = $1;
52                 $vifc{'netmask'} = &parse_hex($2);
53                 $vifc{'broadcast'} = $3;
54                 $vifc{'up'} = 1;
55                 $vifc{'edit'} = $ifc{'edit'};
56                 $vifc{'virtual'} = $v++;
57                 $vifc{'fullname'} = $vifc{'name'}.':'.$vifc{'virtual'};
58                 $vifc{'index'} = scalar(@rv);
59                 push(@rv, \%vifc);
60                 }
61         }
62 return @rv;
63 }
64
65 # activate_interface(&details)
66 # Create or modify an interface
67 sub activate_interface
68 {
69 local %act;
70 map { $act{$_->{'fullname'}} = $_ } &active_interfaces();
71 local $old = $act{$_[0]->{'fullname'}};
72 $act{$_[0]->{'fullname'}} = $_[0];
73 &interface_sync(\%act, $_[0]->{'name'}, $_[0]->{'fullname'});
74 }
75
76 # deactivate_interface(&details)
77 # Deactive an interface
78 sub deactivate_interface
79 {
80 local %act;
81 local @act = &active_interfaces();
82 if ($_[0]->{'virtual'} eq '') {
83         @act = grep { $_->{'name'} ne $_[0]->{'name'} } @act;
84         }
85 else {
86         @act = grep { $_->{'fullname'} ne $_[0]->{'fullname'} } @act;
87         }
88 map { $act{$_->{'fullname'}} = $_ } @act;
89 &interface_sync(\%act, $_[0]->{'name'}, $_[0]->{'fullname'});
90 }
91
92 # interface_sync(interfaces, name, changee)
93 sub interface_sync
94 {
95 # Remove all IP addresses except for the primary one (unless it is being edited)
96 local $pri = $_[0]->{$_[1]};
97 local $ifconfig = &has_command("ifconfig");
98 while(1) {
99         local $out;
100         &execute_command("$ifconfig $_[1]", undef, \$out);
101         last if ($out !~ /([\000-\377]*)\s+inet\s+(\d+\.\d+\.\d+\.\d+)/);
102         last if ($2 eq $pri->{'address'} && $_[2] ne $pri->{'fullname'});
103         &system_logged("$ifconfig $_[1] delete $2 >/dev/null 2>&1");
104         }
105
106 # Add them back again, except for the primary unless it is being changed
107 foreach $a (sort { $a->{'fullname'} cmp $b->{'fullname'} }
108                  grep { $_->{'name'} eq $_[1] } values(%{$_[0]})) {
109         next if ($a->{'fullname'} eq $pri->{'fullname'} &&
110                  $_[2] ne $pri->{'fullname'});
111         local $cmd = "$ifconfig $a->{'name'}";
112         if ($a->{'virtual'} ne '') {
113                 $cmd .= " alias $a->{'address'}";
114                 }
115         else {
116                 $cmd .= " $a->{'address'}";
117                 }
118         if ($a->{'netmask'}) { $cmd .= " netmask $a->{'netmask'}"; }
119         if ($a->{'broadcast'}) { $cmd .= " broadcast $a->{'broadcast'}"; }
120         if ($a->{'mtu'}) { $cmd .= " mtu $a->{'mtu'}"; }
121         local $out = &backquote_logged("$cmd 2>&1");
122         #if ($? && $out !~ /file exists/i) {
123         if ($?) {
124                 &error($out);
125                 }
126         if ($a->{'virtual'} eq '') {
127                 if ($a->{'up'}) { $out = &backquote_logged("$ifconfig $a->{'name'} up 2>&1"); }
128                 else { $out = &backquote_logged("$ifconfig $a->{'name'} down 2>&1"); }
129                 &error($out) if ($?);
130                 }
131         }
132 }
133
134 # boot_interfaces()
135 # Returns a list of interfaces brought up at boot time
136 sub boot_interfaces
137 {
138 local @rv;
139 local %virtual_count;
140 local $lnum = 0;
141 &open_readfile(IFTAB, $iftab_file);
142 while(<IFTAB>) {
143         s/\r|\n//g;
144         s/#.*$//;
145         if (/^(\S+)\s+(inet)\s+(.*)/i) {
146                 local $ifc = { 'name' => $1,
147                                'index' => scalar(@rv),
148                                'line' => $lnum };
149                 local $opts = $3;
150                 next if ($opts =~ /^!/);
151                 $ifc->{'edit'} = 1 if ($ifc->{'name'} =~ /^[a-z]+[0-9]*$/i);
152                 if ($opts eq "-AUTOMATIC-" || $opts eq "-BOOTP-") {
153                         $ifc->{'bootp'} = 1;
154                         $ifc->{'up'} = 1;
155                         }
156                 elsif ($opts eq "-DHCP-") {
157                         $ifc->{'dhcp'} = 1;
158                         $ifc->{'up'} = 1;
159                         }
160                 else {
161                         # Parse the ifconfig params
162                         if ($opts =~ /^([0-9\.]+)/) {
163                                 $ifc->{'address'} = $1;
164                                 }
165                         local @a = split(/\./, $ifc->{'address'});
166                         if ($opts =~ /netmask\s+([0-9\.]+)/) {
167                                 $ifc->{'netmask'} = $1;
168                                 }
169                         else {
170                                 $ifc{'netmask'} = $a[0] >= 192 ? "255.255.255.0" :
171                                                   $a[0] >= 128 ? "255.255.0.0" :
172                                                                  "255.0.0.0";
173                                 }
174                         if ($opts =~ /broadcast\s+([0-9\.]+)/) {
175                                 $ifc->{'broadcast'} = $1;
176                                 }
177                         else {
178                                 local @n = split(/\./, $ifc->{'netmask'});
179                                 $ifc->{'broadcast'} = sprintf "%d.%d.%d.%d",
180                                                 ($a[0] | ~int($n[0]))&0xff,
181                                                 ($a[1] | ~int($n[1]))&0xff,
182                                                 ($a[2] | ~int($n[2]))&0xff,
183                                                 ($a[3] | ~int($n[3]))&0xff;
184                                 }
185                         if ($opts =~ /mtu\s+([0-9\.]+)/) {
186                                 $ifc->{'mtu'} = $1;
187                                 }
188                         if ($opts =~ /\s+up/) {
189                                 $ifc->{'up'} = 1;
190                                 }
191                         if ($opts =~ /\s+alias/) {
192                                 $ifc->{'up'} = 1;
193                                 $ifc->{'virtual'} =
194                                         int($virtual_count{$ifc->{'name'}}++);
195                                 }
196                         }
197                 $ifc->{'fullname'} = $ifc->{'name'};
198                 $ifc->{'fullname'} .= ":$ifc->{'virtual'}"
199                         if ($ifc->{'virtual'} ne "");
200                 $ifc->{'file'} = $iftab_file;
201                 push(@rv, $ifc);
202                 }
203         $lnum++;
204         }
205 close(IFTAB);
206 return @rv;
207 }
208
209 # save_interface(&details)
210 # Create or update a boot-time interface
211 sub save_interface
212 {
213 local $str = "$_[0]->{'name'} inet";
214 if ($_[0]->{'dhcp'}) {
215         $str .= " -DHCP-";
216         }
217 elsif ($_[0]->{'bootp'}) {
218         $str .= " -AUTOMATIC-";
219         }
220 else {
221         $str .= " $_[0]->{'address'}";
222         $str .= " netmask $_[0]->{'netmask'}" if ($_[0]->{'netmask'});
223         $str .= " broadcast $_[0]->{'broadcast'}" if ($_[0]->{'broadcast'});
224         $str .= " mtu $_[0]->{'mtu'}" if ($_[0]->{'mtu'});
225         if ($_[0]->{'virtual'} eq '') {
226                 $str .= " up";
227                 }
228         else {
229                 $str .= " alias";
230                 }
231         }
232 &lock_file($iftab_file);
233 local $lref = &read_file_lines($iftab_file);
234 local @boot = &boot_interfaces();
235 local ($old) = grep { $_->{'fullname'} eq $_[0]->{'fullname'} } @boot;
236 if ($old) {
237         # Replacing existing interface
238         $lref->[$old->{'line'}] = $str;
239         }
240 else {
241         # Adding new interface
242         push(@$lref, $str);
243         if ($_[0]->{'virtual'} ne '') {
244                 # Work out new virtual num
245                 $_[0]->{'virtual'} = 0;
246                 foreach $b (@boot) {
247                         if ($b->{'name'} eq $_[0]->{'name'} &&
248                             $b->{'virtual'} ne '' &&
249                             $b->{'virtual'} >= $_[0]->{'virtual'}) {
250                                 $_[0]->{'virtual'} = $b->{'virtual'}+1;
251                                 }
252                         }
253                 $_[0]->{'fullname'} = $_[0]->{'name'}.':'.$_[0]->{'virtual'};
254                 }
255         }
256 &flush_file_lines();
257 &unlock_file($iftab_file);
258 }
259
260 # delete_interface(&details)
261 # Delete a boot-time interface
262 sub delete_interface
263 {
264 &lock_file($iftab_file);
265 local $lref = &read_file_lines($iftab_file);
266 splice(@$lref, $_[0]->{'line'}, 1);
267 &flush_file_lines();
268 &unlock_file($iftab_file);
269 }
270
271 # iface_type(name)
272 # Returns a human-readable interface type name
273 sub iface_type
274 {
275 return  $_[0] =~ /^ppp/ ? "PPP" :
276         $_[0] =~ /^pppoe/ ? "PPPoE" :
277         $_[0] =~ /^lo/ ? "Loopback" :
278         $_[0] eq "*" ? $text{'ifcs_all'} :
279         $_[0] =~ /^en/ ? "Ethernet" : $text{'ifcs_unknown'};
280 }
281
282 # iface_hardware(name)
283 # Does some interface have an editable hardware address
284 sub iface_hardware
285 {
286 return 0;
287 }
288
289 # can_edit(what)
290 # Can some boot-time interface parameter be edited?
291 sub can_edit
292 {
293 return $_[0] =~ /netmask|broadcast|dhcp|bootp/;
294 }
295
296 # valid_boot_address(address)
297 # Is some address valid for a bootup interface
298 sub valid_boot_address
299 {
300 return &check_ipaddress($_[0]);
301 }
302
303 # get_dns_config()
304 # Returns a hashtable containing keys nameserver, domain, search & order
305 sub get_dns_config
306 {
307 local $dns;
308 &open_readfile(RESOLV, "/etc/resolv.conf");
309 while(<RESOLV>) {
310         s/\r|\n//g;
311         s/#.*$//g;
312         if (/nameserver\s+(.*)/) {
313                 push(@{$dns->{'nameserver'}}, split(/\s+/, $1));
314                 }
315         elsif (/domain\s+(\S+)/) {
316                 $dns->{'domain'} = [ $1 ];
317                 }
318         elsif (/search\s+(.*)/) {
319                 $dns->{'domain'} = [ split(/\s+/, $1) ];
320                 }
321         }
322 close(RESOLV);
323 $dns->{'files'} = [ "/etc/resolv.conf" ];
324 return $dns;
325 }
326
327 # save_dns_config(&config)
328 # Writes out the resolv.conf file
329 sub save_dns_config
330 {
331 &lock_file("/etc/resolv.conf");
332 &open_readfile(RESOLV, "/etc/resolv.conf");
333 local @resolv = <RESOLV>;
334 close(RESOLV);
335 &open_tempfile(RESOLV, ">/etc/resolv.conf");
336 foreach (@{$_[0]->{'nameserver'}}) {
337         &print_tempfile(RESOLV, "nameserver $_\n");
338         }
339 if ($_[0]->{'domain'}) {
340         if ($_[0]->{'domain'}->[1]) {
341                 &print_tempfile(RESOLV, "search ",join(" ", @{$_[0]->{'domain'}}),"\n");
342                 }
343         else {
344                 &print_tempfile(RESOLV, "domain $_[0]->{'domain'}->[0]\n");
345                 }
346         }
347 foreach (@resolv) {
348         &print_tempfile(RESOLV, $_) if (!/^\s*(nameserver|domain|search)\s+/);
349         }
350 &close_tempfile(RESOLV);
351 &unlock_file("/etc/resolv.conf");
352 }
353
354 $max_dns_servers = 3;
355
356 # order_input(&dns)
357 # Returns HTML for selecting the name resolution order
358 sub order_input
359 {
360 return undef;
361 }
362
363 # parse_order(&dns)
364 # Parses the form created by order_input()
365 sub parse_order
366 {
367 return undef;
368 }
369
370 # get_hostname()
371 sub get_hostname
372 {
373 local $hc = &read_hostconfig();
374 if ($hc->{'HOSTNAME'}) {
375         return $hc->{'HOSTNAME'};
376         }
377 return &get_system_hostname();
378 }
379
380 # save_hostname(name)
381 sub save_hostname
382 {
383 &system_logged("hostname $_[0] >/dev/null 2>&1");
384 &lock_file($hostconfig);
385 &set_hostconfig("HOSTNAME", $_[0]);
386 &unlock_file($hostconfig);
387 undef(@main::get_system_hostname);      # clear cache
388 }
389
390 sub routing_config_files
391 {
392 return ( $hostconfig_file );
393 }
394
395 sub routing_input
396 {
397 local $hc = &read_hostconfig();
398 local $r = $hc->{'ROUTER'};
399 local $mode = $r eq "-AUTOMATIC-" ? 1 : $r ? 2 : 0;
400
401 # Default router
402 print &ui_table_row($text{'routes_default'},
403         &ui_radio("router_mode", $mode,
404                   [ [ 0, $text{'routes_none2'} ],
405                     [ 1, $text{'routes_auto'} ],
406                     [ 2, &ui_textbox("router", $mode == 2 ? $r : "", 20) ] ]));
407
408 # Forward traffic?
409 local $f = $hc->{'IPFORWARDING'};
410 print &ui_table_row($text{'routes_forward'},
411         &ui_yesno_radio("forward", $f eq '-YES-'));
412 }
413
414 sub parse_routing
415 {
416 local $r;
417 if ($in{'router_mode'} == 0) {
418         $r = undef;
419         }
420 elsif ($in{'router_mode'} == 1) {
421         $r = "-AUTOMATIC-";
422         }
423 else {
424         $r = $in{'router'};
425         &check_ipaddress($r) || &error(&text('routes_edefault', $r));
426         }
427 &lock_file($hostconfig_file);
428 &set_hostconfig("ROUTER", $r);
429 &set_hostconfig("IPFORWARDING", $in{'forward'} ? "-YES-" : "-NO-");
430 &unlock_file($hostconfig_file);
431 }
432
433 # set_hostconfig(name, value)
434 # Add or update an entry in the hostconfig file
435 sub set_hostconfig
436 {
437 local $lref = &read_file_lines($hostconfig_file);
438 local ($i, $found);
439 for($i=0; $i<@$lref; $i++) {
440         if ($lref->[$i] =~ /^(\S+)\s*=/ && lc($1) eq lc($_[0])) {
441                 $lref->[$i] = "$_[0]=$_[1]";
442                 $found++;
443                 }
444         }
445 if (!$found) {
446         push(@$lref, "$_[0]=$_[1]");
447         }
448 &flush_file_lines();
449 }
450
451 # read_hostconfig()
452 # Returns a hash of hostconfig file values
453 sub read_hostconfig
454 {
455 local %rv;
456 &open_readfile(HOST, $hostconfig_file);
457 while(<HOST>) {
458         s/\r|\n//g;
459         s/#.*$//;
460         if (/^(\S+)\s*=\s*(.*)/) {
461                 $rv{$1} = $2;
462                 }
463         }
464 close(HOST);
465 return \%rv;
466 }
467
468 sub os_feedback_files
469 {
470 return ( "/etc/hostconfig", "/etc/resolv.conf", "/etc/iftab" );
471 }
472
473 # apply_network()
474 # Apply the interface and routing settings
475 #sub apply_network
476 #{
477 #system("killall ipconfigd && ipconfigd </dev/null >/dev/null 2>&1 &");
478 #system("ipconfig waitall >/dev/null 2>&1");
479 #local $hc = &read_hostconfig();
480 #system("killall -HUP netinfod >/dev/null 2>&1");
481 #system("killall -HUP lookupd >/dev/null 2>&1");
482 #}
483
484 # supports_address6([&iface])
485 # Returns 1 if managing IPv6 interfaces is supported
486 sub supports_address6
487 {
488 local ($iface) = @_;
489 return 0;
490 }
491
492 1;
493