/usr/share/perl5/EBox/Validate.pm is in zentyal-common 2.3.3.
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 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 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | # 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::Validate;
use strict;
use warnings;
use EBox::Config;
use EBox::Exceptions::InvalidData;
use EBox::Gettext;
use EBox::NetWrappers qw();
use Net::IP;
use Mail::RFC822::Address;
use Data::Validate::Domain qw(is_hostname);
use constant IFNAMSIZ => 16; #Max length name for interfaces
BEGIN {
use Exporter ();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@ISA = qw(Exporter);
@EXPORT = qw();
%EXPORT_TAGS = (all => [qw{ checkCIDR checkIP checkNetmask
checkIPNetmask
checkProtocol checkPort
checkName checkMAC checkVifaceName
checkDomainName checkHost
isIPInNetwork
checkVlanID isPrivateDir isANumber
isAPositiveNumber
checkFilePath checkAbsoluteFilePath
} ],
);
@EXPORT_OK = qw();
Exporter::export_ok_tags('all');
$VERSION = EBox::Config::version;
}
# Function: isIPInNetwork
#
# Checks if an IP is within a given network address and its masks
#
# Parameters:
#
# network_ip - network address
# network_mask - network mask for above address
# host_ip - host address to check it belongs to the given network
#
# Returns:
#
# boolean - True if the address is within the network, false otherwise
#
sub isIPInNetwork # net_ip, net_mask, host_ip
{
my ($net_ip, $net_mask, $host_ip) = @_;
my $net_net = EBox::NetWrappers::ip_network($net_ip, $net_mask);
my $bits = EBox::NetWrappers::bits_from_mask($net_mask);
my $ip = new Net::IP("$net_net/$bits");
my $ip2 = new Net::IP($host_ip);
return ($ip2->overlaps($ip)==$IP_A_IN_B_OVERLAP);
}
# Function: checkCIDR
#
# Check the validity for a given CIDR block
#
# Parameters:
#
# cidr - CIDR block to check
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if the cidr is correct, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# <EBox::Exceptions::InvalidData> - CIDR is incorrect
#
sub checkCIDR # (cidr, name?)
{
my $cidr = shift;
my $name = shift;
my $ip;
my @values = split(/\//, $cidr);
if(@values == 2) {
my ($address,$mask) = @values;
if(checkIP($address)) {
my $netmask = EBox::NetWrappers::mask_from_bits($mask);
if($netmask){
my $network = EBox::NetWrappers::ip_network($address, $netmask);
if ($network eq $address) {
$ip = new Net::IP("$network/$mask");
}
}
}
}
unless($ip) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $cidr);
} else {
return undef;
}
}
return 1;
}
# Function: checkIP
#
# Checks if the string param that holds an ip address is a valid
# IPv4 address.
#
# Parameters:
#
# ip - IPv4 address
# name - ip's name to be used when throwing an Exception (optional)
#
# Returns:
#
# boolean - True if it is a valid IPv4 address, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - IP is invalid
#
sub checkIP # (ip, name?)
{
my $ip = shift;
my $name = shift;
if("$ip\." =~ m/^(([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){4}$/){
my $first = (split(/\./, $ip))[0];
if(($first != 0) and ($first < 224)) {
return 1;
}
}
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $ip);
} else {
return undef;
}
}
# Function: checkIP6
#
# Checks if the string param that holds an ip address is a valid
# IPv6 address.
#
# Parameters:
#
# ip - IPv6 address
# name - ip's name to be used when throwing an Exception (optional)
#
# Returns:
#
# boolean - True if it is a valid IPv4 address, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - IP is invalid
#
sub checkIP6 # (ip, name?)
{
my ($ip, $name) = @_;
if (Net::IP::ip_is_ipv6($ip)) {
return 1;
}
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $ip);
} else {
return undef;
}
}
# Function: checkNetmask
#
# Checks if the string param that holds a network mask is valid .
#
# Parameters:
#
# nmask - network mask
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid network mask, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - mask is incorrect
#
sub checkNetmask # (mask, name?)
{
my $nmask = shift;
my $name = shift;
my $error;
if("$nmask\." =~ m/^(([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){4}$/){
my $bits;
foreach (split(/\./, $nmask)){
$bits .= unpack( "B*", pack( "C", $_ ));
}
unless ($bits =~ /^((0+)|(1+0*))$/){
$error = 1;
}
} else {
$error = 1;
}
if ($error) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $nmask);
} else {
return undef;
}
}
return 1;
}
#
# Function: checkIPNetmask
#
# Checks if the IP and the mask are valid and that the IP is not a
# network or broadcast address with the given mask.
#
# Note that both name_ip and name_mask should be set, or not set at all
#
#
# Parameters:
#
# ip - IPv4 address
# mask - network mask address
# name_ip - Data's name to be used when throwing an Exception
# name_mask - Data's name to be used when throwing an Exception
# Returns:
#
# boolean - True if it is a valid IPv4 address, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - ip/mask is incorrect
#
sub checkIPNetmask # (ip, mask, name_ip?, name_mask?)
{
my ($ip,$mask,$name_ip, $name_mask) = @_;
my $error = 0;
checkIP($ip,$name_ip);
if ($mask eq '255.255.255.255') {
return 1;
}
checkNetmask($mask,$name_mask);
my $ip_bpack = pack("CCCC", split(/\./, $ip));
my $mask_bpack = pack("CCCC", split(/\./, $mask));
my $net_bits .= unpack("B*", $ip_bpack & (~$mask_bpack));
my $broad_bits .= unpack("B*", $ip_bpack | $mask_bpack);
if(($net_bits =~ /^0+$/) or ($broad_bits =~ /^1+$/)){
$error = 1;
}
if ($error) {
if ($name_ip) {
throw EBox::Exceptions::InvalidData
('data' => $name_ip . "/" . $name_mask,
'value' => $ip . "/" . $mask);
} else {
return undef;
}
}
return 1;
}
# Function: checkPort
#
# Check if the given port is valid
#
# Parameters:
#
# port - port number
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid port, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - ip/mask is incorrect
#
sub checkPort # (port, name?)
{
my $pnumber = shift;
my $name = shift;
unless($pnumber =~/^\d+$/){
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $pnumber);
} else {
return undef;
}
}
if (($pnumber > 0) and ($pnumber <= 65535)) {
return 1;
} else {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $pnumber);
} else {
return undef;
}
}
}
#
# Function: checkVlanID
#
# Checks if the given vlan identifier is valid. The valid identifers are
# numbers betwwen 01 and 4096 (both included)
#
# Parameters:
#
# id - vlan idintifier
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid vlan id, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - id is incorrect
#
sub checkVlanID # (id, name?)
{
my $id = shift;
my $name = shift;
unless($id =~/^\d+$/){
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name,
'value' => $id,
'advice' =>
__('Must be a number between 1 and 4096')
);
} else {
return undef;
}
}
if (($id > 0) && ($id <= 4096)) {
return 1;
} else {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $id,
'advice' =>
__('Must be a number between 1 and 4096')
);
} else {
return undef;
}
}
}
# Function: checkProtocol
#
# Checks if the given protocol is valid (tcp or udp)
#
# Parameters:
#
# proto - protocolo's name
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid protocol , false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - protocol is incorrect
#
sub checkProtocol # (protocol, name?)
{
my $proto = shift;
my $name = shift;
# FIXME: Ask for them to <EBox::Types::Service> -> Double dependency
if ($proto eq 'tcp' ) {
return 1;
} elsif ($proto eq 'udp' ) {
return 1;
} elsif ($proto eq 'all' ) {
return 1;
} elsif ($proto eq 'icmp') {
return 1;
} elsif ($proto eq 'gre' ) {
return 1;
}
else {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $proto);
} else {
return undef;
}
}
}
#
# Function: checkMAC
#
# Checks if the given mac address is valid
#
# Parameters:
#
# mac - mac address
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid mac address, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - protocol is incorrect
#
sub checkMAC # (mac, name?)
{
my ($origMac, $name) = @_;
my $mac = $origMac . ':';
unless ($mac =~ /^([0-9a-fA-F]{2}:){6}$/) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $origMac);
} else {
return undef;
}
}
return 1;
}
# Function: checkVifaceName
#
# Checks if a virtual interface name is correct. The whole name's length
# (real + virtual interface) must be no longer than IFMASIZ. Only
# alphanumeric characters are allowed.
#
# Parameters:
#
# real - real interface (i.e: eth0, eth1..)
# virtual - virtual interface (i.e: foo, bar)
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid virtual interface, false otherwise
#
# Exceptions:
#
# If name is passed an exception could be raised
#
# InvalidData - protocol is incorrect
#
sub checkVifaceName # (real, virtual, name?)
{
my $iface = shift;
my $viface = shift;
my $name = shift;
my $fullname = $iface . ":" . $viface;
unless (($viface =~ /^\w+$/) and (length($fullname) < IFNAMSIZ)){
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $viface);
} else {
return undef;
}
}
return 1;
}
# Function: checkName
#
# Checks if a given name is valid.
#
# To be a valid name it must fulfil these requirements
#
# - starts with a letter
# - contains only letters, numbers and '_'
# - isn't longer than 20 characters
#
# Parameters:
#
# name - name to check
#
# Returns:
#
# boolean - True if it is a valid name, false otherwise
#
sub checkName # (name)
{
my $name = shift;
(length($name) <= 20) or return undef;
(length($name) > 0) or return undef;
($name =~ /^[\d_]/) and return undef;
($name =~ /^\w/) or return undef;
($name =~ /\W/) and return undef;
return 1;
}
# TODO: Remove this once the call from the mail wizard is changed
# to the checkDomainName public function
sub _checkDomainName
{
my ($domain) = @_;
$domain =~ s/\.$//;
return is_hostname($domain);
}
# Function: checkDomainName
#
# Checks if a given domain name is valid.
#
# Parameters:
#
# domain - domain to check
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if it is a valid domain name, false otherwise
#
sub checkDomainName # (domain, name?)
{
my ($domain, $name) = @_;
$domain =~ s/\.$//;
unless (is_hostname($domain)) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $domain);
} else {
return undef;
}
}
return 1;
}
# Function: checkHost
#
# Checks if a given host is valid. It is considered valid either a valid
# no-CIDR IP address or a valid hostname
#
#
# Parameters:
#
# host - host to check
# name - Data's name to be used when throwing an Exception.
#
# Returns:
#
# boolean - True if it is a valid domain name, false otherwise
#
sub checkHost # (domain, name?)
{
my ($host, $name) = @_;
# if the host is made only of numbers and points we check it
# as a IP address otherwise we check it as a hostname
if ( $host =~ m/^[\d.]+$/ ) {
return checkIP($host, $name);
}
else {
return checkDomainName($host, $name);
}
}
# Function: checkEmailAddress
#
# Check the validity for a given FQDN email address
#
# Parameters:
#
# address - email address to check
# name - Data's name to be used when throwing an Exception
#
# Returns:
#
# boolean - True if the address is correct. False on failure when
# parameter name is NOT defined
#
# Exceptions:
#
# If name is passed an exception will be raised on failure
#
# <EBox::Exceptions::InvalidData> - address is incorrect
#
sub checkEmailAddress
{
my ($address, $name) = @_;
unless (Mail::RFC822::Address::valid($address)) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $address);
} else {
return undef;
}
}
return 1;
}
# Function: isPrivateDir
#
# Check if the given directory is private and owned by the current user
#
# Parameters:
#
# dir - The directory
# throwException - wether to throw a exception if the check fails (default: false)
#
# Returns:
# true if the parameter is a number, undef otherwise.
#
sub isPrivateDir
{
my ($dir, $throwException) = @_;
my @stat = stat($dir) ;
if (@stat == 0) {
throw EBox::Exceptions::External (__x("Cannot stat dir: {dir}. This may mean that the directory does not exist or the permissions forbid access to it", dir => $dir)) if $throwException;
return undef;
}
if ($< != $stat[4]) {
throw EBox::Exceptions::External(__x('The directory {dir} is not private; because it is owned by another user', dir => $dir)) if $throwException;
}
my $perm = sprintf ("%04o\n", $stat[2] & 07777);
unless ($perm =~ /.700/) {
throw EBox::Exceptions::External(('The directory {dir} is not private; because it has not restrictive permissions', dir => $dir)) if $throwException;
return undef;
}
}
# Function: isANumber
#
# Check if the parameter is a number.
#
# Parameters:
#
# value - The parameter to test.
#
# Returns
# true if the parameter is a number, undef otherwise.
#
sub isANumber # (value)
{
my $value = shift;
($value =~ /^-?[\d]+$/) and return 1;
return undef;
}
# Function: isZeroOrNaturalNumber
#
# Check if the parameter is a positive number or zero.
#
# Parameters:
#
# value - The parameter to test.
#
# Returns:
# true if the parameter is a postive number, undef otherwise.
#
sub isZeroOrNaturalNumber # (value)
{
my $value = shift;
($value =~ /^[\d]+$/) and return 1;
return undef;
}
# Function: checkFilePath
#
# checks if a given file path is sintaxically correct
#
# Parameters:
# $filePath - file path to check
# $name - if this parameter is present we will throw a exception when given a non-correct path using this as name of the data
#
# Returns:
# true if the parameter is sintaxically correct, undef otherwise.
sub checkFilePath # (filePath, name)
{
my ($filePath, $name) = @_;
# see Regexp::Common::URI::RFC1738 in CPAN for inspiration in the regex
my $fpart = q{[:alpha:\-\$_.+!*(),][|\#]+ }; # there are missing character but this will suffice for now...
my $fPathRegex = "($fpart)?(/$fpart)*";
if ( $filePath =~ m/$fPathRegex/ ) {
return 1;
}
else {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $filePath, 'advice' => __("The file path supplied is not valid. (Currently not all of the valid file's characters are supported) ") );
}
else {
return undef;
}
}
}
# Function: checkAbsoluteFilePath
#
# checks if a given absolute file path is sintaxically correct
#
# Parameters:
# $filePath - file path to check
# $name - if this parameter is present we will throw a exception when given a non-correct path using this as name of the data
#
# Returns:
# true if the parameter is sintaxically correct and an absolute path, undef otherwise.
sub checkAbsoluteFilePath
{
my ($filePath, $name) = @_;
my $isValidPath = checkFilePath($filePath, $name);
$isValidPath or return undef;
if ( ( $filePath =~ m{^[^/]} ) or ( $filePath =~ m{/\.+/} ) ) {
if ($name) {
throw EBox::Exceptions::InvalidData
('data' => $name, 'value' => $filePath, 'advice' => __("The file path must be absolute") );
}
else {
return undef;
}
}
return 1;
}
1;
|