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
|
#Copyright (c) 2011 Erich Schubert erich@debian.org
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
import re, inspect, socket
from exception import PyromanException
class Util:
"""
Some utility functions for the firewall system
"""
# regexp used for splitting strings
splitter = re.compile("(?:,\s*|\s+)")
# regexp to ignore lines in manual rules
ignoreline = re.compile("^\s*(#|$)")
# nicknames may only contain these chars
namefilter = re.compile("^[a-zA-Z0-9]+$")
namefilter_service = re.compile("^[a-zA-Z0-9/]+$")
def get_callee(depth=3):
"""
Return information about the calling function
"""
frame = inspect.stack(depth)
return "%s:%d" % (frame[depth-1][1], frame[depth-1][2])
get_callee = staticmethod(get_callee)
def verify_ip4(ip):
"""
Verify that the given string is an IPv4 address
"""
try:
socket.inet_pton(socket.AF_INET, ip)
return True
except socket.error:
return False
verify_ip4 = staticmethod(verify_ip4)
def verify_ip6(ip):
"""
Verify that the given string is an IPv6 address
"""
try:
socket.inet_pton(socket.AF_INET6, ip)
return True
except socket.error:
return False
verify_ip6 = staticmethod(verify_ip6)
def verify_ip4net(ip):
"""
Verify that the given string describes an IPv4 network
"""
l = ip.split("/", 1)
if len(l) == 0:
return False
if not Util.verify_ip4(l[0]):
return False
if len(l) == 1:
return True
n = int(l[1])
return (n >= 0) and (n <= 32)
verify_ip4net = staticmethod(verify_ip4net)
def verify_ip6net(ip):
"""
Verify that the given string describes an IPv6 network
"""
l = ip.split("/", 1)
if len(l) == 0:
return False
if not Util.verify_ip6(l[0]):
return False
if len(l) == 1:
return True
n = int(l[1])
return (n >= 0) and (n <= 128)
verify_ip6net = staticmethod(verify_ip6net)
def verify_ip(ip):
"""
Verify that the given string is an IPv4 or IPv6 address
"""
return Util.verify_ip4(ip) or Util.verify_ip6(ip)
verify_ip = staticmethod(verify_ip)
def verify_ipnet(ip):
"""
Verify that the given string is an IPv4 or IPv6 address
"""
return Util.verify_ip4net(ip) or Util.verify_ip6net(ip)
verify_ipnet = staticmethod(verify_ipnet)
def verify_name(name, servicename=False):
"""
Verify that a name only contains a certain set of characters
to avoid problems with rule names derived from it.
name -- name to be checked
servicename -- Set to true to allow the / char additionally
"""
if servicename:
m = Util.namefilter_service.match(name)
if m:
return True
else:
m = Util.namefilter.match(name)
if m:
return True
return False
verify_name = staticmethod(verify_name)
def compare_versions(ver1, ver2):
"""
Compare to version numbers
returns True if ver1 is less or equal to ver2
ver1 -- first version number
ver2 -- second version number
"""
def compsplit(s):
"""
Split a version number component into a pair of (int,str)
"""
if not s[0].isdigit():
return (None, s)
for i in range(1,len(s)):
if not s[i].isdigit():
return (int(s[:i]),s[i:])
return (int(s),"")
v1c = ver1.split(".")
v2c = ver2.split(".")
minl = min(len(v1c),len(v2c))
for i in range(minl):
(v1, v1s) = compsplit(v1c[i])
(v2, v2s) = compsplit(v2c[i])
# one has digits, one hasn't?
if not v1: return -1
if not v2: return +1
if v1 != v2:
assert(cmp(v1,v2) != 0)
return cmp(v1, v2)
# compare remaining string
c = cmp(v1s,v2s)
if c != 0: return c
if len(v1c) < len(v2c): return -1
if len(v1c) > len(v2c): return +1
return 0
compare_versions = staticmethod(compare_versions)
|