| 1 | |||
| 2 | ################################################################################### | ||
| 3 | # | ||
| 4 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 6 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 7 | # | ||
| 8 | # You may distribute under the terms of either the GNU General Public | ||
| 9 | # License or the Artistic License, as specified in the Perl README file. | ||
| 10 | # | ||
| 11 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 12 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 13 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | ################################################################################### | ||
| 16 | |||
| 17 | |||
| 18 | package Embperl::Form::Validate::FQDN_IPv4_IPv6Addr ; | ||
| 19 | |||
| 20 | use base qw(Embperl::Form::Validate::Default); | ||
| 21 | use utf8 ; | ||
| 22 | |||
| 23 | my %errutf8 = | ||
| 24 | ( | ||
| 25 | validate_fqdn_ipv46addr => 'Feld %0: "%1" ist keine gültiger Hostname oder IP-Adresse.', | ||
| 26 | ) ; | ||
| 27 | |||
| 28 | no utf8 ; | ||
| 29 | |||
| 30 | my %error_messages = | ||
| 31 | ( | ||
| 32 | de => | ||
| 33 | { | ||
| 34 | validate_fqdn_ipv46addr => 'Feld %0: "%1" ist keine gütiger Hostname oder IP-Adresse.', | ||
| 35 | }, | ||
| 36 | |||
| 37 | 'de.utf-8' => \%errutf8, | ||
| 38 | |||
| 39 | en => | ||
| 40 | { | ||
| 41 | validate_fqdn_ipv46addr => 'Field %0: "%1" isn\\\'t a valid hostname or ip-address.', | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | |||
| 45 | |||
| 46 | # -------------------------------------------------------------- | ||
| 47 | |||
| 48 | sub getmsg | ||
| 49 | { | ||
| 50 | my ($self, $id, $language, $default_language) = @_ ; | ||
| 51 | |||
| 52 | return $error_messages{$language}{$id} || | ||
| 53 | $error_messages{$default_language}{$id} || | ||
| 54 | $self -> SUPER::getmsg ($id, $language, $default_language) ; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | # -------------------------------------------------------------- | ||
| 59 | |||
| 60 | =pod | ||
| 61 | |||
| 62 | see https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses | ||
| 63 | |||
| 64 | # IPv6 RegEx | ||
| 65 | ( | ||
| 66 | ([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}| # 1:2:3:4:5:6:7:8 | ||
| 67 | ([0-9a-fA-F]{1,4}:){1,7}:| # 1:: 1:2:3:4:5:6:7:: | ||
| 68 | ([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}| # 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8 | ||
| 69 | ([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}| # 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8 | ||
| 70 | ([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}| # 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8 | ||
| 71 | ([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}| # 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8 | ||
| 72 | ([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}| # 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8 | ||
| 73 | [0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})| # 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8 | ||
| 74 | :((:[0-9a-fA-F]{1,4}){1,7}|:)| # ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 :: | ||
| 75 | fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}| # fe80::7:8%eth0 fe80::7:8%1 (link-local IPv6 addresses with zone index) | ||
| 76 | ::(ffff(:0{1,4}){0,1}:){0,1} | ||
| 77 | ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3} | ||
| 78 | (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])| # ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses) | ||
| 79 | ([0-9a-fA-F]{1,4}:){1,4}: | ||
| 80 | ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3} | ||
| 81 | (25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]) # 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33 (IPv4-Embedded IPv6 Address) | ||
| 82 | ) | ||
| 83 | |||
| 84 | # IPv4 RegEx | ||
| 85 | ((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]) | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | |||
| 90 | sub validate | ||
| 91 | { | ||
| 92 | my ($self, $key, $value, $fdat, $pref) = @_ ; | ||
| 93 | |||
| 94 | if ($value =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) | ||
| 95 | { | ||
| 96 | if ($1 < 0 || $1 > 255 || | ||
| 97 | $2 < 0 || $2 > 255 || | ||
| 98 | $3 < 0 || $3 > 255 || | ||
| 99 | $4 < 0 || $4 > 255) | ||
| 100 | { | ||
| 101 | return ['validate_fqdn_ipv46addr', $value] ; | ||
| 102 | } | ||
| 103 | else | ||
| 104 | { | ||
| 105 | return undef ; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | return undef if ($value =~ /^[-.a-zA-Z0-9]+$/) ; | ||
| 110 | |||
| 111 | if ($value !~ /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/) | ||
| 112 | { | ||
| 113 | return ['validate_fqdn_ipv46addr', $value] ; | ||
| 114 | } | ||
| 115 | return undef; | ||
| 116 | } | ||
| 117 | |||
| 118 | # -------------------------------------------------------------- | ||
| 119 | |||
| 120 | sub getscript_validate | ||
| 121 | { | ||
| 122 | my ($self, $arg, $pref) = @_ ; | ||
| 123 | |||
| 124 | return ('obj.value.search(/^[-.:a-zA-Z0-9]+$/) >= 0 ', ['validate_fqdn_ipv46addr', "'+obj.value+'"]) ; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | 1; |