/usr/share/perl5/EBox/Network/Model/GatewayTable.pm is in zentyal-network 2.3.13+quantal1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# 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 program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package EBox::Network::Model::GatewayTable;
use EBox::Global;
use EBox::Gettext;
use EBox::Validate qw(:all);
use EBox::Exceptions::External;
use EBox::Types::Int;
use EBox::Types::Boolean;
use EBox::Types::Select;
use EBox::Types::IPAddr;
use EBox::Types::Union;
use EBox::Types::Text;
use EBox::Network::Types::Text::AutoReadOnly;
use EBox::Network::View::GatewayTableCustomizer;
use EBox::Sudo;
use Net::ARP;
use strict;
use warnings;
use base 'EBox::Model::DataTable';
use constant MAC_FETCH_TRIES => 3;
sub new
{
my $class = shift;
my %parms = @_;
my $self = $class->SUPER::new(@_);
bless($self, $class);
return $self;
}
sub weights
{
my @options;
for my $weight (1..15) {
push @options, { 'value' => $weight,
'printableValue' => $weight};
}
return \@options;
}
# Method: syncRows
#
# Overrides <EBox::Model::DataTable::syncRows>
#
sub syncRows
{
my ($self, $currentRows) = @_;
my $conf = $self->{'confmodule'};
my $network = EBox::Global->modInstance('network');
my %dynamicGws;
my $state = $conf->get_state();
foreach my $iface (@{$network->dhcpIfaces()}) {
my $gw = $state->{dhcp}->{$iface}->{gateway};
if ($gw) {
$dynamicGws{$iface} = $gw;
}
}
foreach my $iface (@{$network->pppIfaces()}) {
my $addr = $state->{interfaces}->{$iface}->{ppp_addr};
my $ppp_iface = $state->{interfaces}->{$iface}->{ppp_iface};
if ($addr and $ppp_iface) {
$dynamicGws{$iface} = "$ppp_iface/$addr";
}
}
my %currentIfaces =
map { $self->row($_)->valueByName('interface') => $_ } @{$currentRows};
my $modified = 0;
my @ifacesToAdd = grep { not exists $currentIfaces{$_} } keys %dynamicGws;
my @ifacesToDel = grep { not exists $dynamicGws{$_} } keys %currentIfaces;
my @ifacesToModify = grep { exists $dynamicGws{$_} } keys %currentIfaces;
# If we are going to add new gateways and there is no previous default
# set, we will set the first added one as default
if (scalar (@ifacesToAdd) > 0) {
my $needDefault = 1;
foreach my $id (@{$currentRows}) {
my $row = $self->row($id);
if ($row->valueByName('default')) {
$needDefault = 0;
last;
}
}
foreach my $iface (@ifacesToAdd) {
my $method = $network->ifaceMethod($iface);
$self->add(name => "$method-gw-$iface",
interface => $iface, ip => $dynamicGws{$iface},
default => $needDefault, auto => 1);
$needDefault = 0;
$modified = 1;
}
}
foreach my $iface (@ifacesToModify) {
my $row = $self->row($currentIfaces{$iface});
next unless $row->valueByName('auto');
my $ip = $row->elementByName('ip');
my $newIP = $dynamicGws{$iface};
my $oldIP = $ip->value();
unless (defined ($oldIP) and ($newIP eq $oldIP)) {
$ip->setValue($newIP);
$row->storeElementByName('ip');
$modified = 1;
}
}
foreach my $iface (@ifacesToDel) {
my $id = $currentIfaces{$iface};
my $row = $self->row($id);
next unless $row->valueByName('auto');
$self->removeRow($id, 1);
$modified = 1;
}
return $modified;
}
sub _table
{
my @tableHead =
(
new EBox::Types::Boolean(
'fieldName' => 'auto',
'printableName' => __('Auto'),
'defaultValue' => 0,
'hidden' => 1
),
new EBox::Network::Types::Text::AutoReadOnly(
'fieldName' => 'name',
'printableName' => __('Name'),
'size' => '12',
'unique' => 1,
'editable' => 1
),
new EBox::Network::Types::Text::AutoReadOnly(
'fieldName' => 'ip',
'printableName' => __('IP address'),
'size' => '16',
'unique' => 0, # Uniqueness is checked in validateRow
'editable' => 1
),
new EBox::Types::Text(
'fieldName' => 'interface',
'printableName' => __('Interface'),
'editable' => 0,
'hiddenOnSetter' => 1,
'optional' => 1,
'help' => __('Interface connected to this gateway')
),
new EBox::Types::Select(
'fieldName' => 'weight',
'printableName' => __('Weight'),
'defaultValue' => 1,
'size' => '2',
'populate' => \&weights,
'editable' => 1,
'help' => __('This field is only useful if you have ' .
'more than one router and the balance ' .
'traffic feature is enabled.')
),
new EBox::Types::Boolean(
'fieldName' => 'default',
'printableName' => __('Default'),
'size' => '1',
'editable' => 1,
'HTMLViewer' => '/ajax/viewer/booleanViewer.mas',
)
);
my $dataTable =
{
'tableName' => 'GatewayTable',
'printableTableName' => __('Gateways List'),
'automaticRemove' => 1,
'enableProperty' => 1,
'defaultEnabledValue' => 1,
'defaultController' =>
'/Network/Controller/GatewayTable',
'defaultActions' =>
[
'add', 'del',
'move', 'editField',
'changeView'
],
'tableDescription' => \@tableHead,
'menuNamespace' => 'Network/View/GatewayTable',
'class' => 'dataTable',
'order' => 0,
'help' => __x('You can add as many gateways as you want. This is very useful if you want to split your Internet traffic through several links. Note that if you configure interfaces as DHCP or PPPoE their gateways are added here automatically.'),
'rowUnique' => 0,
'printableRowName' => __('gateway'),
};
return $dataTable;
}
# Method: validateRow
#
# Implementation note:
# this is validateRow and not typedValidateRow because we dont want to execute
# this when failover watcher do a $row->store() call, this could be also done
# using the 'force' parameter but I want not to risk to break something in the ecent
#
# Override <EBox::Model::DataTable::validateRow> method
#
sub validateRow
{
my ($self, $action, %params) = @_;
my $currentRow = $self->row($params{'id'});
my $auto = 0;
my $oldIP = '';
if ($currentRow) {
$auto = $currentRow->valueByName('auto');
$oldIP = $currentRow->valueByName('ip');
}
if (exists $params{name}) {
$self->checkGWName($params{name});
}
my $network = EBox::Global->modInstance('network');
# Do not check for valid IP in case of auto-added ifaces
unless ($auto) {
my $printableName = __('IP address');
my $ip = $params{'ip'};
unless ($ip) {
throw EBox::Exceptions::MissingArgument($printableName);
}
checkIP($ip, $printableName);
# Check uniqueness
if ($oldIP ne $ip) {
if ($self->find('ip' => $ip)) {
throw EBox::Exceptions::DataExists('data' => $printableName,
'value' => $ip);
}
}
my $iface = $network->gatewayReachable($params{ip});
if ($iface) {
# Only check if gateway is reachable on static interfaces
unless ($network->ifaceMethod($iface) eq 'static') {
if ($action eq 'add') {
throw EBox::Exceptions::External(__('You can not manually add a gateway for DHCP or PPPoE interfaces'));
} else {
throw EBox::Exceptions::External(__x("Gateway {gw} must be reachable by a static interface. "
. "Currently it is reachable by {iface} which is not static",
gw => $ip, iface => $iface));
}
}
} else {
throw EBox::Exceptions::External(__x("Gateway {gw} not reachable", gw => $ip));
}
if (($action eq 'add') and ($self->size() == 0)) {
if (not $params{default}) {
throw EBox::Exceptions::External(__('Since you have not gateways you should add the first one as default'))
}
}
}
my $currentIsDefault = 0;
if ($currentRow) {
$currentIsDefault = $currentRow->valueByName('default');
}
if ($params{default}) {
# remove existent default mark in other row if needed
if (not $currentIsDefault) {
my $defaultRow = $self->find('default' => 1);
if ($defaultRow) {
my $default = $defaultRow->elementByName('default');
$default->setValue(undef);
$defaultRow->storeElementByName('default');
}
}
} elsif ($currentIsDefault) {
throw EBox::Exceptions::External(__('You cannot remove the default attribute, if you want to change it assign it to another gaterway'));
}
}
sub validateRowRemoval
{
my ($self, $row, $force) = @_;
if ( $row->valueByName('auto') and not $force) {
throw EBox::Exceptions::External(__('Automatically added gateways can not be manually deleted'));
}
}
# Method: addedRowNotify
#
# Overrides:
#
# <EBox::Model::DataTable::addedRowNotify>
#
sub addedRowNotify
{
my ($self, $row) = @_;
$self->_autoDetectInterface($row);
if ($row->valueByName('default')) {
my $network = $self->parentModule();
$network->storeSelectedDefaultGateway($row->id());
}
}
# Method: updatedRowNotify
#
# Overrides:
#
# <EBox::Model::DataTable::updatedRowNotify>
#
sub updatedRowNotify
{
my ($self, $row, $oldRow, $force) = @_;
$self->_autoDetectInterface($row);
return if ($force); # failover event can force changes
my $network = $self->parentModule();
my $id = $row->id();
if ($row->valueByName('default')) {
$network->storeSelectedDefaultGateway($id);
} else {
if ($id eq $network->selectedDefaultGateway()) {
$network->storeSelectedDefaultGateway('');
}
}
}
# Method: deletedRowNotify
#
# Overrides:
#
# <EBox::Model::DataTable::deletedRowNotify>
#
sub deletedRowNotify
{
my ($self, $row, $force) = @_;
if ($row->valueByName('default')) {
my $network = $self->parentModule();
my $size = $self->size();
if ($size == 0) {
# no preferred gateway sicne there are not gws!
$network->storeSelectedDefaultGateway('');
} else {
# choose another gw
my $newDefaultRow = $self->find(enabled => 1);
if (not $newDefaultRow) {
# no enabled, gw choosing another
my ($id) = @{ $self->ids() };
$newDefaultRow = $self->row($id);
}
$newDefaultRow->elementByName('default')->setValue(1);
$newDefaultRow->store(); # this does not upgrade preferred default gw
$network->storeSelectedDefaultGateway($newDefaultRow->id());
}
}
}
# Method: viewCustomizer
#
# Overrides <EBox::Model::DataTable::viewCustomizer>
#
sub viewCustomizer
{
my ($self) = @_;
my $customizer = new EBox::Network::View::GatewayTableCustomizer();
$customizer->setModel($self);
return $customizer;
}
# Returns default gateway
sub defaultGateway()
{
my ($self) = @_;
my $row = $self->find('default' => 1);
if ($row) {
return $row->valueByName('ip');
} else {
return undef;
}
}
sub marksForRouters
{
my ($self) = @_;
my $ids = $self->ids();
my $marks;
my $i = 1;
for my $id (@{$ids}) {
$marks->{$id} = $i;
$i++;
}
return $marks;
}
# Returns only enabled gateways
sub gateways
{
my ($self) = @_;
$self->_gateways(0);
}
# Returns all gateways
sub allGateways
{
my ($self) = @_;
$self->_gateways(1);
}
sub _gateways
{
my ($self, $all) = @_;
my @gateways;
my $balanceModel = $self->parentModule()->model('BalanceGateways');
my %balanceEnabled =
map { $balanceModel->row($_)->valueByName('name') => 1 } @{$balanceModel->enabledRows()};
foreach my $id (@{$all ? $self->ids() : $self->enabledRows()}) {
my $gw = $self->row($id);
my $name = $gw->valueByName('name');
push (@gateways, {
id => $id,
auto => $gw->valueByName('auto'),
name => $name,
ip => $gw->valueByName('ip'),
weight => $gw->valueByName('weight'),
default => $gw->valueByName('default'),
interface => $gw->valueByName('interface'),
enabled => $gw->valueByName('enabled'),
balance => $balanceEnabled{$name},
});
}
return \@gateways;
}
sub gatewaysWithMac
{
my ($self) = @_;
my @gateways = @{$self->allGateways()};
foreach my $gw (@gateways) {
# Skip mac detection for auto-added gateways (dhcp and pppoe)
if ($gw->{'auto'}) {
$gw->{'mac'} = undef;
} else {
$gw->{'mac'} = _getRouterMac($gw->{'ip'});
}
}
return \@gateways;
}
sub _getRouterMac
{
my ($ip) = @_;
my $macif = '';
my $mac;
for (0..MAC_FETCH_TRIES) {
system("ping -c 1 -W 3 $ip > /dev/null 2> /dev/null");
$mac = Net::ARP::arp_lookup($macif, $ip);
return $mac if ($mac ne '00:00:00:00:00:00');
}
return $mac;
}
# XXX fudge bz ModelManager does not delete gateway removal in tables AND we
# don't have a removeRowValidate method
# TODO improve this if ModelManager gets rewritten with that remove-check
# feature added
sub removeRow
{
my ($self, $id, $force) = @_;
unless (defined($id)) {
throw EBox::Exceptions::MissingArgument(
"Missing row identifier to remove")
}
if (not $force) {
my $row = $self->row($id);
$row or
throw EBox::Exceptions::Internal("Invalid row id $id");
my $gw = $row->valueByName('name');
my $global = EBox::Global->getInstance($self->{confmodule}->{ro});
my @mods = @{$global->modInstancesOfType('EBox::NetworkObserver')};
foreach my $mod (@mods) {
if ($mod->gatewayDelete($gw)) {
throw EBox::Exceptions::DataInUse(
__x(q|The gateway '{name}' is being used by {mod}|,
name => $gw,
mod => $mod->name()
)
);
}
}
}
$self->SUPER::removeRow($id, $force);
}
sub checkGWName
{
my ($self, $name) = @_;
if (($name =~ m/^-/) or ($name =~ m/-$/)) {
throw EBox::Exceptions::InvalidData(
data => __('Gateway name'),
value => $name,
advice => __(q{Gateways names cannot begin or end with '-'})
);
}
unless ($name =~ m/^[a-z0-9\-]+$/) {
throw EBox::Exceptions::InvalidData(
data => __('Gateway name'),
value => $name,
advice => __(q{Gateways names can only be composed of lowercase ASCII english letters, digits and '-'}),
);
}
}
sub _autoDetectInterface
{
my ($self, $row) = @_;
return if ($row->valueByName('auto'));
my $network = $self->parentModule();
my $iface = $network->gatewayReachable($row->valueByName('ip'));
if ($iface) {
$row->elementByName('interface')->setValue($iface);
$row->store();
}
}
1;
|