summaryrefslogtreecommitdiffstats
path: root/perl-install/network.pm
blob: 7c5ca5056353b036c32d89493e23cc272d5fe9fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package network;

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use Socket;

use common qw(:common :file :system :functional);
use detect_devices;
use run_program;
use log;

#-######################################################################################
#- Functions
#-######################################################################################
sub read_conf {
    my ($file) = @_;
    my %netc = getVarsFromSh($file);
    $netc{dnsServer} = delete $netc{NS0};
    \%netc;
}

sub read_resolv_conf {
    my ($file) = @_;
    my %netc;
    my @l;

    local *F;
    open F, $file or die "cannot open $file: $!";
    foreach (<F>) {
	push @l, $1 if (/^\s*nameserver\s+([^\s]+)/);
    }

    $netc{$_} = shift @l foreach qw(dnsServer dnsServer2 dnsServer3);
    \%netc;
}

sub read_interface_conf {
    my ($file) = @_;
    my %intf = getVarsFromSh($file) or die "cannot open file $file: $!";

    $intf{BOOTPROTO} ||= 'static';
    $intf{isPtp} = $intf{NETWORK} eq '255.255.255.255';
    $intf{isUp} = 1;
    \%intf;
}

sub up_it {
    my ($prefix, $intfs) = @_;
    $_->{isUp} and return foreach @$intfs;
    my $f = "/etc/resolv.conf"; symlink "$prefix/$f", $f;
    run_program::rooted($prefix, "/etc/rc.d/init.d/network", "start");
    $_->{isUp} = 1 foreach @$intfs;
}
sub down_it {
    my ($prefix, $intfs) = @_;
    run_program::rooted($prefix, "/etc/rc.d/init.d/network", "stop");
    $_->{isUp} = 1 foreach @$intfs;
}

sub write_conf {
    my ($file, $netc) = @_;

    add2hash($netc, {
		     NETWORKING => "yes",
		     FORWARD_IPV4 => "false",
		     HOSTNAME => "localhost.localdomain",
		     });
    add2hash($netc, { DOMAINNAME => $netc->{HOSTNAME} =~ /\.(.*)/ });

    setVarsInSh($file, $netc, qw(NETWORKING FORWARD_IPV4 HOSTNAME DOMAINNAME GATEWAY GATEWAYDEV NISDOMAIN));
}

sub write_resolv_conf {
    my ($file, $netc) = @_;

    #- We always write these, even if they were autoconfigured. Otherwise, the reverse name lookup in the install doesn't work.
    unless ($netc->{DOMAINNAME} || dnsServers($netc)) {
	unlink($file);
	log::l("neither domain name nor dns server are configured");
	return 0;
    }
    my @l = cat_($file);

    local *F;
    open F, "> $file" or die "cannot write $file: $!";
    print F "search $netc->{DOMAINNAME}\n" if $netc->{DOMAINNAME};
    print F "nameserver $_\n" foreach dnsServers($netc);
    print F "#$_" foreach @l;

    #-res_init();		# reinit the resolver so DNS changes take affect
    1;
}

sub write_interface_conf {
    my ($file, $intf) = @_;

    my @ip = split '\.', $intf->{IPADDR};
    my @mask = split '\.', $intf->{NETMASK};
    add2hash($intf, {
		     BROADCAST => join('.', mapn { int $_[0] | ~int $_[1] & 255 } \@ip, \@mask),
		     NETWORK   => join('.', mapn { int $_[0] &      $_[1]       } \@ip, \@mask),
		     ONBOOT => bool2yesno(!$::o->{pcmcia}),
		    });
    setVarsInSh($file, $intf, qw(DEVICE BOOTPROTO IPADDR NETMASK NETWORK BROADCAST ONBOOT));
}

sub add2hosts {
    my ($file, $hostname, @ips) = @_;
    my %l;
    $l{$_} = $hostname foreach @ips;

    local *F;
    if (-e $file) {
	open F, $file or die "cannot open $file: $!";
	/\s*(\S+)(.*)/ and $l{$1} = $2 foreach <F>;
    }
    log::l("writing host information to $file");
    open F, ">$file" or die "cannot write $file: $!";
    while (my ($ip, $v) = each %l) {
	$ip or next;
	print F "$ip";
	if ($v =~ /^\s/) {
	    print F $v;
	} else {
	    print F "\t\t$v";
	    print F " $1" if $v =~ /(.*?)\./;
	}
	print F "\n";
    }
}

# The interface/gateway needs to be configured before this will work!
sub guessHostname {
    my ($prefix, $netc, $intf) = @_;

    $intf->{isUp} && dnsServers($netc) or return 0;
    $netc->{HOSTNAME} && $netc->{DOMAINNAME} and return 1;

    write_resolv_conf("$prefix/etc/resolv.conf", $netc);

    my $name = gethostbyaddr(Socket::inet_aton($intf->{IPADDR}), AF_INET) or log::l("reverse name lookup failed"), return 0;

    log::l("reverse name lookup worked");

    add2hash($netc, { HOSTNAME => $name });
    1;
}

sub addDefaultRoute {
    my ($netc) = @_;
    c::addDefaultRoute($netc->{GATEWAY}) if $netc->{GATEWAY};
}

sub sethostname {
    my ($netc) = @_;
    syscall_('sethostname', $netc->{HOSTNAME}, length $netc->{HOSTNAME}) or log::l("sethostname failed: $!");
}

sub resolv($) {
    my ($name) = @_;
    is_ip($name) and return $name;
    my $a = join(".", unpack "C4", (gethostbyname $name)[4]);
    #-log::l("resolved $name in $a");
    $a;
}

sub dnsServers {
    my ($netc) = @_;
    grep { $_ } map { $netc->{$_} } qw(dnsServer dnsServer2 dnsServer3);
}

sub findIntf {
    my ($intf, $device) = @_;
    my ($l) = grep { $_->{DEVICE} eq $device } @$intf;
    push @$intf, $l = { DEVICE => $device } unless $l;
    $l;
}
#PAD \s* a la fin
my $ip_regexp = qr/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
sub is_ip {
    my ($ip) = @_;
    return 0 unless $ip =~ $ip_regexp;
    my @fields = ($1, $2, $3, $4);
    foreach (@fields) {
	return 0 if $_ < 0 || $_ > 255;
    }
    return 1;
}

sub netmask {
    my ($ip) = @_;
    return "255.255.255.0" unless is_ip($ip);
    $ip =~ $ip_regexp;
    if ($1 >= 1 && $1 < 127) {
	return "255.0.0.0";    #-1.0.0.0 to 127.0.0.0
    } elsif ($1  >= 128 && $1 <= 191 ){
	return "255.255.0.0";  #-128.0.0.0 to 191.255.0.0
    } elsif ($1 >= 192 && $1 <= 223) {
	return "255.255.255.0";
    } else {
	return "255.255.255.255"; #-experimental classes
    }
}

sub masked_ip {
    my ($ip) = @_;
    return "" unless is_ip($ip);
    my @mask = netmask($ip) =~ $ip_regexp;
    my @ip   = $ip          =~ $ip_regexp;
    for (my $i = 0; $i < @ip; $i++) {
	$ip[$i] &= int $mask[$i];
    }
    join(".", @ip);
}

sub dns {
    my ($ip) = @_;
    my $mask = masked_ip($ip);
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 1;
    join (".", @masked);

}
sub gateway {
    my ($ip) = @_;
    my @masked = masked_ip($ip) =~ $ip_regexp;
    $masked[3]  = 254;
    join (".", @masked);

}

#-######################################################################################
#- Wonderful perl :(
#-######################################################################################
1;
lass="hl kwb">$info }); } qw(0 1); } my @ide = ls120s() and eval { modules::load("ide-floppy") }; eval { modules::load("usb-storage") } if usbStorage(); my @scsi = grep { $_->{media_type} eq 'fd' } getSCSI(); @ide, @scsi, @fds; } sub floppies_dev() { map { $_->{device} } floppies() } sub floppy() { first(floppies_dev()) } #- example ls120, model = "LS-120 SLIM 02 UHD Floppy" sub removables() { floppies(), cdroms(), zips(); } sub get_sys_cdrom_info { my (@drives) = @_; my @drives_order; foreach (cat_("/proc/sys/dev/cdrom/info")) { my ($t, $l) = split ':'; my @l; @l = split(' ', $l) if $l; if ($t eq 'drive name') { @drives_order = map { my $dev = $_; find { $_->{device} eq $dev } @drives; } @l; } else { my $capacity; if ($t eq 'Can write CD-R') { $capacity = 'burner'; } elsif ($t eq 'Can read DVD') { $capacity = 'DVD'; } if ($capacity) { each_index { ($drives_order[$::i] || {})->{capacity} .= "$capacity " if $_; } @l; } } } } sub get_usb_storage_info_24 { my (@l) = @_; my %usbs = map { my $s = cat_(glob_("$_/*")); my ($host) = $s =~ /^\s*Host scsi(\d+):/m; #-# my ($vendor_name) = $s =~ /^\s*Vendor: (.*)/m; my ($vendor, $id) = $s =~ /^\s*GUID: (....)(....)/m; if_(defined $host, $host => { vendor_name => $vendor_name, usb_vendor => hex $vendor, usb_id => hex $id }); } glob_('/proc/scsi/usb-storage-*') or return; #- only the entries matching the following conditions can be usb-storage devices @l = grep { $_->{channel} == 0 && $_->{id} == 0 && $_->{lun} == 0 } @l; my %l; push @{$l{$_->{host}}}, $_ foreach @l; foreach my $host (keys %usbs) { my @choices = @{$l{$host} || []} or log::l("weird, host$host from /proc/scsi/usb-storage-*/* is not in /proc/scsi/scsi"), next; if (@choices > 1) { @choices = grep { $_->{info} =~ /^\Q$usbs{$host}{vendor_name}/ } @choices; @choices or log::l("weird, can not find the good entry host$host from /proc/scsi/usb-storage-*/* in /proc/scsi/scsi"), next; @choices == 1 or log::l("argh, can not determine the good entry host$host from /proc/scsi/usb-storage-*/* in /proc/scsi/scsi"), next; } add2hash($choices[0], $usbs{$host}); } complete_usb_storage_info(@l); @l; } sub complete_usb_storage_info { my (@l) = @_; my @usb = grep { exists $_->{usb_vendor} } @l; foreach my $usb (usb_probe()) { if (my $e = find { $_->{usb_vendor} == $usb->{vendor} && $_->{usb_id} == $usb->{id} } @usb) { $e->{"usb_$_"} = $usb->{$_} foreach keys %$usb; } } } sub get_devfs_devices { my (@l) = @_; my %h = (cdrom => 'cd', hd => 'disc'); foreach (@l) { $_->{devfs_prefix} = sprintf('scsi/host%d/bus%d/target%d/lun%d', $_->{host}, $_->{channel}, $_->{id}, $_->{lun}) if $_->{bus} eq 'SCSI'; my $t = $h{$_->{media_type}} or next; $_->{devfs_device} = $_->{devfs_prefix} . '/' . $t; } } sub isBurner { my ($e) = @_; $e->{capacity} =~ /burner/ and return 1; #- do not work for SCSI my $f = tryOpen($e->{device}); #- SCSI burner are not detected this way. $f && c::isBurner(fileno($f)); } sub isDvdDrive { my ($e) = @_; $e->{capacity} =~ /DVD/ || $e->{info} =~ /DVD/ and return 1; #- do not work for SCSI my $f = tryOpen($e->{device}); $f && c::isDvdDrive(fileno($f)); } sub isZipDrive { $_[0]{info} =~ /ZIP\s+\d+/ } #- accept ZIP 100, untested for bigger ZIP drive. sub isJazzDrive { $_[0]{info} =~ /\bJAZZ?\b/i } #- accept "iomega jaz 1GB" sub isLS120Drive { $_[0]{info} =~ /LS-?120|144MB/ } sub isRemovableUsb { begins_with($_[0]{usb_media_type} || '', 'Mass Storage') && usb2removable($_[0]) } sub isKeyUsb { begins_with($_[0]{usb_media_type} || '', 'Mass Storage') && $_[0]{media_type} eq 'hd' } sub isFloppyUsb { $_[0]{usb_driver} && $_[0]{usb_driver} eq 'Removable:floppy' } sub may_be_a_hd { my ($e) = @_; $e->{media_type} eq 'hd' && !( isZipDrive($e) || isLS120Drive($e) || begins_with($e->{usb_media_type} || '', 'Mass Storage|Floppy (UFI)') ); } sub get_scsi_driver { my (@l) = @_; my %host2driver = map { if_(m!.*/(.*)/(.*)!, $2, $1) } glob("/proc/scsi/*/*"); foreach (@l) { if (my $driver = $host2driver{$_->{host}}) { $_->{driver} = $driver; } } } sub getSCSI_24() { my $err = sub { log::l("ERROR: unexpected line in /proc/scsi/scsi: $_[0]") }; my ($first, @l) = common::join_lines(cat_("/proc/scsi/scsi")) or return; $first =~ /^Attached devices:/ or $err->($first); @l = map_index { my ($host, $channel, $id, $lun) = m/^Host: scsi(\d+) Channel: (\d+) Id: (\d+) Lun: (\d+)/ or $err->($_); my ($vendor, $model) = /^\s*Vendor:\s*(.*?)\s+Model:\s*(.*?)\s+Rev:/m or $err->($_); my ($type) = /^\s*Type:\s*(.*)/m or $err->($_); { info => "$vendor $model", host => $host, channel => $channel, id => $id, lun => $lun, device => "sg$::i", raw_type => $type, bus => 'SCSI' }; } @l; get_usb_storage_info_24(@l); each_index { my $dev = "sd" . chr($::i + ord('a')); put_in_hash $_, { device => $dev, media_type => isFloppyUsb($_) ? 'fd' : 'hd' }; } grep { $_->{raw_type} =~ /Direct-Access|Optical Device/ } @l; each_index { put_in_hash $_, { device => "st$::i", media_type => 'tape' }; } grep { $_->{raw_type} =~ /Sequential-Access/ } @l; each_index { put_in_hash $_, { device => "sr$::i", media_type => 'cdrom' }; } grep { $_->{raw_type} =~ /CD-ROM|WORM/ } @l; # Old hp scanners report themselves as "Processor"s # (see linux/include/scsi/scsi.h and sans-find-scanner.1) each_index { put_in_hash $_, { media_type => 'scanner' }; } grep { $_->{raw_type} =~ /Scanner/ || $_->{raw_type} =~ /Processor / } @l; delete $_->{raw_type} foreach @l; get_devfs_devices(@l); get_sys_cdrom_info(@l); get_scsi_driver(@l); @l; } sub getSCSI_26() { my $dev_dir = '/sys/bus/scsi/devices'; my @scsi_types = ( "Direct-Access", "Sequential-Access", "Printer", "Processor", "WORM", "CD-ROM", "Scanner", "Optical Device", "Medium Changer", "Communications", ); my @l = map { my ($host, $channel, $id, $lun) = split ':' or log::l("bad entry in $dev_dir: $_"), next; my $dir = "$dev_dir/$_"; my $get = sub { my $s = cat_("$dir/$_[0]"); $s =~ s/\s+$//; $s; }; my $usb_dir = readlink("$dir/block/device") =~ m!/usb! && "$dir/block/device/../../.."; my $get_usb = sub { chomp_(cat_("$usb_dir/$_[0]")) }; my ($device) = readlink("$dir/block") =~ m!/block/(.*)!; my $media_type = ${{ st => 'tape', sr => 'cdrom', sd => 'hd' }}{substr($device, 0, 2)}; # Old hp scanners report themselves as "Processor"s # (see linux/include/scsi/scsi.h and sans-find-scanner.1) my $raw_type = $scsi_types[$get->('type')]; $media_type ||= 'scanner' if $raw_type =~ /Scanner|Processor/; { info => $get->('vendor') . ' ' . $get->('model'), host => $host, channel => $channel, id => $id, lun => $lun, bus => 'SCSI', media_type => $media_type, device => $device, $usb_dir ? ( usb_vendor => hex($get_usb->('idVendor')), usb_id => hex($get_usb->('idProduct')), ) : (), }; } all($dev_dir); @l = sort { $a->{host} <=> $b->{host} || $a->{channel} <=> $b->{channel} || $a->{id} <=> $b->{id} || $a->{lun} <=> $b->{lun} } @l; complete_usb_storage_info(@l); foreach (@l) { $_->{media_type} = 'fd' if $_->{media_type} eq 'hd' && isFloppyUsb($_); } get_devfs_devices(@l); get_sys_cdrom_info(@l); get_scsi_driver(@l); @l; } sub getSCSI() { c::kernel_version() =~ /^\Q2.6/ ? getSCSI_26() : getSCSI_24() } my %eide_hds = ( "ASUS" => "Asus", "CD-ROM CDU" => "Sony", "CD-ROM Drive/F5D" => "ASUSTeK",