2 # Networking functions for UnixWare
5 # Returns a list of currently ifconfig'd interfaces
8 local(@rv, @lines, $l);
9 &open_execute_command(IFC, "ifconfig -a", 1, 1);
12 if (/^\S+:/) { push(@lines, $_); }
13 else { $lines[$#lines] .= $_; }
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);
30 $ifc{'ether'} = join(":", map { sprintf "%2.2d", $_ }
31 split(/:/, $ifc{'ether'}));
38 # activate_interface(&details)
39 # Create or modify an interface
40 sub activate_interface
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"); }
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); }
60 $out = &backquote_logged(
61 "ifconfig $a->{'name'} ether $a->{'ether'} 2>&1");
62 if ($? && $out !~ /Device busy/) { &error($out); }
66 # deactivate_interface(&details)
67 # Deactive an interface
68 sub deactivate_interface
71 if ($a->{'virtual'} eq "") {
72 $cmd = "ifconfig $a->{'name'} unplumb";
75 $cmd = "ifconfig $a->{'name'}:$a->{'virtual'} 0.0.0.0 down";
77 local $out = &backquote_logged("$cmd 2>&1");
78 if ($?) { &error($out); }
82 # Returns a list of interfaces brought up at boot time
85 local (@rv, $f, %mask);
86 push(@rv, { 'name' => 'lo0',
88 'address' => '127.0.0.1',
89 'netmask' => '255.0.0.0',
92 open(MASK, "/etc/netmasks");
96 if (/([0-9\.]+)\s+([0-9\.]+)/) {
101 opendir(ETC, "/etc");
102 while($f = readdir(ETC)) {
103 if ($f =~ /^hostname.(\S+):(\d+)$/ || $f =~ /^hostname.(\S+)/) {
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);
110 $ifc{'file'} = "/etc/$f";
111 open(FILE, "/etc/$f");
112 chop($ifc{'address'} = <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"}; }
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;
134 $ifc{'netmask'} = "Automatic";
135 $ifc{'broadcast'} = "Automatic";
146 # save_interface(&details)
147 # Create or update a boot-time interface
150 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
152 &open_lock_tempfile(IFACE, ">/etc/hostname.$name");
153 if (!$_[0]->{'dhcp'}) {
154 &print_tempfile(IFACE, $_[0]->{'address'},"\n");
156 &close_tempfile(IFACE);
159 # delete_interface(&details)
160 # Delete a boot-time interface
163 local $name = $_[0]->{'virtual'} ne "" ? $_[0]->{'name'}.":".$_[0]->{'virtual'}
165 &unlink_logged("/etc/hostname.$name");
169 # Returns a human-readable interface type name
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/);
179 # iface_hardware(name)
180 # Does some interface have an editable hardware address
183 return $_[0] !~ /^(lo|ipdptp|ppp)/;
187 # Can some boot-time interface parameter be edited?
190 return $_[0] eq "dhcp";
193 # valid_boot_address(address)
194 # Is some address valid for a bootup interface
195 sub valid_boot_address
197 return &to_ipaddress($_[0]) ? 1 : 0;
201 # Returns a hashtable containing keys nameserver, domain, search & order
205 open(RESOLV, "/etc/resolv.conf");
209 if (/nameserver\s+(.*)/) {
210 push(@{$dns->{'nameserver'}}, split(/\s+/, $1));
212 elsif (/domain\s+(\S+)/) {
213 $dns->{'domain'} = [ $1 ];
215 elsif (/search\s+(.*)/) {
216 $dns->{'domain'} = [ split(/\s+/, $1) ];
220 open(SWITCH, "/etc/nsswitch.conf");
223 if (/hosts:\s+(.*)/) {
224 $dns->{'order'} = $1;
228 $dns->{'files'} = [ "/etc/resolv.conf", "/etc/nsswitch.conf" ];
232 # save_dns_config(&config)
233 # Writes out the resolv.conf and nsswitch.conf files
236 &lock_file("/etc/resolv.conf");
237 open(RESOLV, "/etc/resolv.conf");
238 local @resolv = <RESOLV>;
240 &open_tempfile(RESOLV, ">/etc/resolv.conf");
241 foreach (@{$_[0]->{'nameserver'}}) {
242 &print_tempfile(RESOLV, "nameserver $_\n");
244 if ($_[0]->{'domain'}) {
245 if ($_[0]->{'domain'}->[1]) {
246 &print_tempfile(RESOLV, "search ",join(" ", @{$_[0]->{'domain'}}),"\n");
249 &print_tempfile(RESOLV, "domain $_[0]->{'domain'}->[0]\n");
253 &print_tempfile(RESOLV, $_) if (!/^\s*(nameserver|domain|search)\s+/);
255 &close_tempfile(RESOLV);
256 &unlock_file("/etc/resolv.conf");
258 &lock_file("/etc/nsswitch.conf");
259 open(SWITCH, "/etc/nsswitch.conf");
260 local @switch = <SWITCH>;
262 &open_tempfile(SWITCH, ">/etc/nsswitch.conf");
265 &print_tempfile(SWITCH, "hosts:\t$_[0]->{'order'}\n");
268 &print_tempfile(SWITCH, $_);
271 &close_tempfile(SWITCH);
272 &unlock_file("/etc/nsswitch.conf");
275 $max_dns_servers = 3;
278 # Returns HTML for selecting the name resolution order
281 return &common_order_input("order", $_[0]->{'order'},
282 [ [ "files", "Hosts" ], [ "dns", "DNS" ], [ "nis", "NIS" ],
283 [ "nisplus", "NIS+" ] ]);
287 # Parses the form created by order_input()
290 if (defined($in{'order'})) {
291 $in{'order'} =~ /\S/ || &error($text{'dns_eorder'});
292 $_[0]->{'order'} = $in{'order'};
296 for($i=0; defined($in{"order_$i"}); $i++) {
297 push(@order, $in{"order_$i"}) if ($in{"order_$i"});
299 $_[0]->{'order'} = join(" ", @order);
305 return &get_system_hostname();
308 # save_hostname(name)
311 &system_logged("hostname $_[0] >/dev/null 2>&1");
312 undef(@main::get_system_hostname); # clear cache
319 &execute_command("domainname", undef, \$d, undef);
324 # save_domainname(domain)
327 &system_logged("domainname ".quotemeta($_[0]));
328 &lock_file("/etc/defaultdomain");
330 &open_tempfile(DOMAIN, ">/etc/defaultdomain");
331 &print_tempfile(DOMAIN, $_[0],"\n");
332 &close_tempfile(DOMAIN);
335 &unlink_file("/etc/defaultdomain");
337 &unlock_file("/etc/defaultdomain");
340 sub routing_config_files
342 return ( "/etc/defaultrouter", "/etc/notrouter", "/etc/gateways" );
347 # show default router(s) input
349 &open_readfile(DEFRT, "/etc/defaultrouter");
352 if (/(\S+)/) { push(@defrt, $1); }
355 print &ui_table_row($text{'routes_defaults'},
356 &ui_textarea("defrt", join("\n", @defrt), 3, 40));
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'} ] ]));
371 local @defrt = split(/\s+/, $in{'defrt'});
372 foreach my $d (@defrt) {
373 &to_ipaddress($d) || &error(&text('routes_edefault', $d));
375 &lock_file("/etc/defaultrouter");
377 &open_tempfile(DEFRT, ">/etc/defaultrouter");
378 foreach $d (@defrt) { &print_tempfile(DEFRT, $d,"\n"); }
379 &close_tempfile(DEFRT);
382 &unlink_file("/etc/defaultrouter");
384 &unlock_file("/etc/defaultrouter");
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");
392 elsif ($in{'router'} == 2) {
393 &create_empty_file("/etc/notrouter");
394 &unlink_file("/etc/gateways");
397 &unlink_file("/etc/gateways");
398 &unlink_file("/etc/notrouter");
400 &unlock_file("/etc/gateways");
401 &unlock_file("/etc/notrouter");
404 # create_empty_file(filename)
405 sub create_empty_file
408 &open_tempfile(EMPTY,">$_[0]");
409 &close_tempfile(EMPTY);
413 sub os_feedback_files
415 opendir(DIR, "/etc");
416 local @f = map { "/etc/$_" } grep { /^hostname\./ } readdir(DIR);
418 return ( @f, "/etc/netmasks", "/etc/resolv.conf", "/etc/nsswitch.conf",
419 "/etc/defaultrouter", "/etc/notrouter", "/etc/gateways" );
422 # supports_address6([&iface])
423 # Returns 1 if managing IPv6 interfaces is supported
424 sub supports_address6