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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
package Ceni::Backend;
use strict;
use warnings;
use Carp;
use Data::Dumper;
use Expect;
use Fcntl 'O_RDONLY';
use Tie::File;
our $VERSION = '1';
sub new {
my ($class, $opts) = @_;
my $self = {};
while (my ($key, $value) = each %{$opts}) {
$self->{$key} = $value;
}
bless($self, $class);
return $self;
}
sub is_iface_wireless {
my ($self, $iface) = (shift, shift);
my $retval = 0;
if (-d "/sys/class/net/$iface/phy80211") {
$retval++;
}
elsif (-x "/sbin/iwgetid") {
open my $iwgetid, '-|', "/sbin/iwgetid --protocol " . $iface
or carp "W: could not execute iwgetid --protocol $iface: $!";
while (<$iwgetid>) {
chomp;
m/^$iface/ and $retval++;
}
close $iwgetid;
}
return $retval;
}
sub nic_info {
my ($self) = (shift);
my %i;
my $udevinfo_cmd = (-x '/sbin/udevadm') ? '/sbin/udevadm info' : 'udevinfo';
$i{$_}{'sysfs'} = '/sys/class/net/' . $_ for map {
s|.*/||;
grep(!/^(lo|br|sit|tap|vmnet)\d*/, $_);
} </sys/class/net/*>;
for my $if (sort keys %i) {
my ($bus, $desc);
open my $udevinfo, '-|', "$udevinfo_cmd -a -p " . $i{$if}{'sysfs'}
or carp "E: could not execute $udevinfo_cmd -a -p "
. $i{$if}{'sysfs'} . ": $!";
while (<$udevinfo>) {
chomp;
$self->debug($_);
if (m/^\s+([A-Z]+(\{(.+)\})?)=="([^"]+)"$/) {
$3 ? $i{$if}{ lc $3 } ||= $4 : $i{$if}{ lc $1 } ||= $4;
# ssb first KERNELS is useless to us, we want the second
($1 eq 'KERNELS')&&($4=~/^ssb/)&&(delete($i{$if}{'kernels'}));
}
}
close $udevinfo;
if ($i{$if}{'subsystems'}) {
$bus = $i{$if}{'subsystems'};
$i{$if}{'connection_type'} =
$self->is_iface_wireless($if) ? 'wireless' : 'ethernet';
}
else {
delete $i{$if};
next;
}
open $udevinfo, '-|', "$udevinfo_cmd -p " . $i{$if}{'sysfs'}
or carp "E: could not execute $udevinfo_cmd -p "
. $i{$if}{'sysfs'} . ": $!";
while (<$udevinfo>) {
chomp;
$self->debug($_);
s/^[A-Z]\:\s//;
if (m/^([A-Z_]+)=(.+?)$/) {
$i{$if}{ lc $1 } ||= $2;
}
}
close $udevinfo;
if (-e $i{$if}{'sysfs'}."/address") {
open my $sysinfo, $i{$if}{'sysfs'}."/address"
or carp "E: could not open ".$i{$if}{'sysfs'}."/address: $!";
my $address=<$sysinfo>;
close($sysinfo);
chomp($address);
$i{$if}{'address'}=$address;
}
if ($bus eq 'pci' or $bus eq 'ssb') {
$desc = `lspci -s $i{$if}{'kernels'} 2>/dev/null | head -n1`;
$desc ||= "PCI device ".$i{$if}{'kernels'};
chomp($desc);
$desc =~ s/^.+:\s+//;
}
elsif ($bus eq 'usb' or $bus eq 'pcmcia') {
my ($manu, $prod) = @{ $i{$if} }{ 'id_vendor_from_database', 'id_model_from_database' };
if ($manu =~ m/^linux/i or $prod =~ m/^$manu/i) {
$desc = $prod;
}
else {
$desc = "$manu $prod";
}
}
elsif ($bus eq 'virtio') {
$desc = "KVM Virtio network device";
}
# FireWire IEEE 1394 Ethernet <- who cares?
if ($desc) {
$desc =~ s/(\s+)?(adapter|corporation|communications|connection|controller|ethernet|integrated|manufacturer|network|semiconductor|systems|technologies|technology|group|inc\.|ltd\.|co\.|\(.+\)),?//gi;
$desc =~ s/\s+/\ /g;
$desc =~ s/^\s+//;
$desc =~ s/\s+$//;
$i{$if}{'desc'} = $desc;
}
else {
$i{$if}{'desc'} = "Unknown description";
}
}
$self->debug(\%i, "i");
%{ $self->{'_data'}->{'nicinfo'} } = %i;
return 1;
}
sub get_iface_info {
my ($self, $iface) = (shift, shift);
if ($iface and $self->{'_data'}->{'nicinfo'}->{$iface}) {
return $self->{'_data'}->{'nicinfo'}->{$iface};
}
elsif ($self->{'_data'}->{'nicinfo'}) {
return $self->{'_data'}->{'nicinfo'};
}
return undef;
}
sub is_iface_valid {
my ($self, $iface) = (shift, shift);
if ($iface and $self->{'_data'}->{'nicinfo'}->{$iface}) {
return 1;
}
return 0;
}
sub parse_eni {
my $self = shift;
$self->{'file'} ||= '/etc/network/interfaces';
tie(my @eni, 'Tie::File', $self->{'file'}, mode => O_RDONLY)
or croak "E: failed to open " . $self->{'file'} . ": $!";
my %e;
my $l = 0;
while (defined $eni[$l]) {
$self->debug("| $l " . $eni[$l]);
if ($eni[$l] =~ m/^\s*#/) {
;
}
elsif ($eni[$l] =~ m/^(auto|allow-.+)\s+(.+)/) {
$e{$_}{'class'} = $1 for (split /\s/, $2);
}
elsif ($eni[$l] =~ m/^iface\s+(.+)\s+inet\s+(.+)/) {
my ($i, $m) = ($1, $2);
$e{$i}{'method'} = $m;
$self->debug("+ $l");
while (defined $eni[ ++$l ]) {
$self->debug("> $l " . $eni[$l]);
if ($eni[$l] =~ m/^#?\s*(iface|auto|allow-.+|mapping)/) {
$self->debug("- $l");
$l-- and last;
}
elsif ($eni[$l] =~ m/^\s*#/) {
push @{ $e{$i}{'comment'} }, $eni[$l];
}
elsif ($eni[$l] =~ m/^\s*(pre-|post-)?(up|down)\s+(.+)/) {
if ($1) {
push @{ $e{$i}{ $1 . $2 } }, $3;
}
else {
push @{ $e{$i}{$2} }, $3;
}
}
elsif ($eni[$l] =~ m/^\s*([^\s]+)\s+(.+)/) {
my ($k, $v) = ($1, $2);
$k =~ s/_/-/g;
$e{$i}{'stanza'}{$k} = $v;
}
}
}
}
continue {
$l++;
}
$self->debug(\%e, "e");
%{ $self->{'_data'}->{'eni'} } = %e;
return 1;
}
sub get_iface_conf {
my ($self, $iface) = (shift, shift);
if ($iface and $self->{'_data'}->{'eni'}->{$iface}) {
return $self->{'_data'}->{'eni'}->{$iface};
}
elsif (not $iface) {
return $self->{'_data'}->{'eni'};
}
return undef;
}
sub is_iface_configured {
my ($self, $iface) = (shift, shift);
if ($self->get_iface_conf($iface) and
$self->get_iface_conf($iface)->{'method'}) {
return 1;
}
return 0;
}
sub set_iface_conf {
my ($self, $iface, $conf) = (shift, shift, shift);
$self->{'file'} ||= '/etc/network/interfaces';
tie(my @eni, 'Tie::File', $self->{'file'})
or carp "E: failed to open " . $self->{'file'} . ": $!";
my @block;
if ($conf->{'class'} and $conf->{'class'} ne 'manual') {
push @block, $conf->{'class'} . " $iface";
delete $conf->{'class'};
}
if ($conf->{'method'}) {
push @block, "iface $iface inet " . $conf->{'method'};
delete $conf->{'method'};
}
if ($conf->{'stanza'}) {
for my $k (sort keys %{ $conf->{'stanza'} }) {
push @block, "\t$k " . $conf->{'stanza'}->{$k};
}
delete $conf->{'stanza'};
}
for my $p ('pre-up', 'up', 'post-up', 'pre-down', 'down', 'post-down') {
if ($conf->{$p}) {
for my $c (@{ $conf->{$p} }) {
push @block, "\t$p $c";
}
delete $conf->{$p};
}
}
if ($conf->{'comment'}) {
for my $c (@{ $conf->{'comment'} }) {
push @block, $c;
}
delete $conf->{'comment'};
}
if (@block) {
push @block, '';
}
$self->debug(\@block, 'block');
my $l = 0;
while (defined $eni[$l]) {
$self->debug("| $l " . $eni[$l]);
if ($eni[$l] =~ m/^\s*#/) {
;
}
elsif ($eni[$l] =~ m/^(auto|allow-.+).*\s+$iface/) {
$eni[$l] =~ s/\s*$iface//;
$self->debug("> $l " . $eni[$l]);
if (not $eni[$l] =~ m/^(auto|allow-.+)\s+.+$/) {
$self->debug("* $l");
splice @eni, $l--, 1;
}
}
elsif ($eni[$l] =~ m/^iface\s+$iface\s+inet\s+(.+)$/) {
$self->debug("* $l " . $eni[$l]);
splice @eni, $l--, 1;
while (defined $eni[ ++$l ]) {
if ($eni[$l] =~ m/^#?\s*(iface|auto|allow-.+|mapping)/) {
$self->debug("- $l");
$l-- and last;
}
else {
$self->debug("* $l " . $eni[$l]);
splice @eni, $l--, 1;
}
}
if (@block) {
$l++;
for my $b (@block) {
$self->debug("+ $l " . $b);
splice @eni, $l++, 0, $b;
}
@block = ();
}
}
}
continue {
$l++;
}
if (@eni and @block) {
if ($eni[-1] =~ m/^\s*\S.*$/) {
push @eni, '';
}
push @eni, @block;
}
chmod 0640, $self->{'file'}
or carp "E: failed to chmod " . $self->{'file'} . ": $!";
}
sub rem_iface_conf {
my ($self, $iface) = (shift, shift);
$self->set_iface_conf($iface, {});
}
sub ifupdown {
my ($self, $iface, $action) = (shift, shift, shift);
my ($ret, @cmd);
if ($action eq 'down' and $self->wpa_action($iface, 'check') == 0) {
$ret = $self->{'act'} ? $self->wpa_action($iface, 'down') : 0;
}
elsif ($self->{'_data'}->{'eni'}->{$iface}->{'method'}) {
@cmd = ("/sbin/if" . $action, $iface, "-i", $self->{'file'}, "-v");
$self->{'act'} or push @cmd, "-n";
$ret = system(@cmd);
}
else {
carp "$iface has no ifupdown method";
}
if ($ret != 0) {
if (($? & 127) == 2) {
carp "W: $action $iface interupted";
}
else {
carp "W: $action $iface failed due to error";
}
}
else {
$self->{'_data'}->{'ifupdown'}->{$iface} = $action;
}
sleep 1;
return $ret;
}
sub ifup {
my ($self, $iface) = (shift, shift);
return $self->ifupdown($iface, 'up');
}
sub ifdown {
my ($self, $iface) = (shift, shift);
return $self->ifupdown($iface, 'down');
}
sub ifstate {
my ($self, $iface) = (shift, shift);
if ($iface and $self->{'_data'}->{'ifupdown'}->{$iface}) {
return $self->{'_data'}->{'ifupdown'}->{$iface};
}
return undef;
}
sub wireless_scan {
my ($self, $iface) = (shift, shift);
my ($wpacli, $ret, $cmd, %scan, @ver, $driver);
my ($wpasup_pid_fh, $wpasup_pid);
#return 1 unless $self->{'act'};
# Recent versions of wpa_supplicant support cycling through driver
# wrappers until one succeeds, so try nl80211 first and fallback to
# wext.
@ver = split(/\./, `/sbin/wpa_supplicant -v | sed -n "s/^wpa_supplicant v//p"`);
$driver = ($ver[0] == 0 and $ver[1] <= 6) ? 'wext' : 'nl80211,wext';
# Spawn an unconfigured wpa_supplicant process to prepare the
# interface for scanning. Using the background (-B) option ensures
# return value indicates if the interface was successfully setup
# (or not). The use of system(), however, means we must also determine
# the process id from a pid file.
$cmd = "/sbin/wpa_supplicant -B -i $iface -D $driver " .
"-P /run/wpa_supplicant.$iface.pid " .
"-C /run/wpa_supplicant";
$ret = system($cmd);
if ($ret != 0) {
if (($? & 127) == 2) {
carp "W: '$cmd' interupted\n";
}
else {
carp "W: '$cmd' failed due to error: $!";
}
}
# Grab the wpa_supplicant process id from pid file. We have to wait
# for it to be created ... a bit sloppy.
sleep 1 until -s "/run/wpa_supplicant.$iface.pid";
open $wpasup_pid_fh, '<', "/run/wpa_supplicant.$iface.pid"
or carp "W: failed to open /run/wpa_supplicant.$iface.pid: $!";
$wpasup_pid = <$wpasup_pid_fh>;
close $wpasup_pid_fh;
chomp $wpasup_pid;
# Start wpa_cli interactive session and communicate with it via Expect
$wpacli = new Expect;
$wpacli->raw_pty(1);
$wpacli->log_stdout(0);
$wpacli->log_file("/tmp/ceni.wpacli.log", "w") if $self->{'debug'};
$wpacli->spawn("/sbin/wpa_cli", "-i", $iface);
# Trigger a scan and wait for scan result notification for up to 30s
$wpacli->send("SCAN\n");
$wpacli->expect(30, -re => ".*CTRL-EVENT-SCAN-RESULTS");
# Gather scan data per BSS
for my $bss (0..100) {
my ($reply, %data);
# Clear previously accumulated output
$wpacli->clear_accum();
# Request BSS data
$wpacli->send("BSS $bss\n");
$wpacli->expect(1, -re => "^>");
# Process reply
$reply = $wpacli->exp_before();
last if length($reply) == 0;
for (split "\n", $reply) {
next unless m/=/;
my ($key, undef) = split(/=/);
my $val = substr($_, length($key) + 1);
$data{$key} = $val;
}
$scan{sprintf "%02d", $bss} = \%data;
}
# Kill wpa_cli process
$wpacli->hard_close();
# Kill wpa_supplicant process.
kill 'TERM', $wpasup_pid;
$self->debug(\%scan, 'scan');
%{ $self->{'_data'}->{'scan'} } = %scan;
return 1;
}
sub get_scan_res {
my ($self, $cell) = (shift, shift);
if (defined $cell and $self->{'_data'}->{'scan'}->{$cell}) {
return $self->{'_data'}->{'scan'}->{$cell};
}
elsif ($self->{'_data'}->{'scan'}) {
return $self->{'_data'}->{'scan'};
}
return undef;
}
sub wpa_action {
my ($self, $iface, $action) = (shift, shift, shift);
my $ret;
$ret = $self->{'act'} ? system("wpa_action $iface check") : 0;
if ($ret != 0) {
if (($? & 127) == 2) {
carp "W: ifconfig $iface up interupted";
}
}
return $ret;
}
sub wpa_mappings {
my ($self, $wpa_roam_cf) = (shift, shift);
my @mappings;
open my $wpa, '<', $wpa_roam_cf
or carp "E: failed to read $wpa_roam_cf: $!";
while (<$wpa>) {
chomp;
if (m/^\s*id_str="?([^"\s]+)"?/) {
push @mappings, $1;
}
}
close $wpa;
return @mappings;
}
sub prep_wpa_roam {
my ($self, $wpa_roam_ex, $wpa_roam_cf) = (shift, shift, shift);
if (not -s $wpa_roam_ex) {
croak "W: wpa-roam template not found: " . $wpa_roam_ex;
}
if (not -s $wpa_roam_cf) {
tie(my @wpa, 'Tie::File', $wpa_roam_cf)
or return 0;
open my $example, '<', $wpa_roam_ex
or return 0;
while (<$example>) {
chomp;
s/^#\s*update_config=.*/update_config=1/;
push @wpa, $_;
}
close $example;
chmod 0640, $wpa_roam_cf
or carp "E: failed to chmod $wpa_roam_cf: $!";
}
return 1;
}
sub debug {
my ($self, $data, $name) = (shift, shift, shift);
return unless $self->{'debug'};
if (ref $data) {
print STDERR "D: ";
print STDERR Data::Dumper->Dump([$data], ["*$name"]);
}
else {
print STDERR "D: $data\n";
}
}
1;
__END__
=head1 NAME
Ceni::Backend - Perl extension for Ceni
=head1 SYNOPSIS
use Ceni::Backend;
my $ceni = Ceni::Backend->new( \%options );
=head1 DESCRIPTION
This is the backend for Ceni(8), it is not provided for use by scripts
not provided by the ceni package, therefore remains undocuemnted.
=head1 AUTHOR
Kel Modderman, E<lt>kel@otaku42.deE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2007 - 2010 by Kel Modderman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this package; if not, see <http://www.gnu.org/licenses>
On Debian GNU/Linux systems, the text of the GPL-2 license can be
found in /usr/share/common-licenses/GPL-2.
=cut
|