Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > dca483b59ba61f3fa092de932ddd570e > files > 806

nuface-2.0.14-2mdv2009.1.i586.rpm

# Copyright(C) 2005 INL
# Written by Jean Gillaux <jean@inl.fr>
#
# 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, version 3 of the License.
#
#  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, see <http://www.gnu.org/licenses/>.

from IPy import IP
from socket import getaddrinfo, gaierror, AF_UNSPEC, SOCK_STREAM
from nupyf.nuelt import Elt, EltSet, EltGrp

def try_getaddrinfo(addr):
    try:
        ip = IP(addr)
    except ValueError:
        try:
            info = getaddrinfo(addr, '0', AF_UNSPEC, SOCK_STREAM)
            t1,t2,t3,t4,sa = info[0]
            return sa[0]
        except gaierror, err:
            s='Error cannot resolv %s: %s' %(addr, err)
            raise ValueError(s)
    else:
        return addr

_IP_PROTOCOLS = {
    'icmp': 1,
    'tcp': 6,
    'udp': 17,
}

def proto_number(protonum):
    return _IP_PROTOCOLS[protonum]

def check_elt(elt):
    if elt.get('mark')!='' and elt.get('net')=='':
        raise ValueError('Error, element id %s has attribute mark %s defined and no net attribute'%(elt.get('ID'),elt.get('mark')))

def xml_new_elt(xmlelt, l7connmark={}):
    """Build a EltIpv4, EltNu or EltLink object from an DOM Element object
    """
    etype = xmlelt.getAttribute('type')
    atts = xmlelt.attributes
    e = Elt()
    h={}
    for ind in range(0,atts.length):
        name = atts.item(ind).name
        value = atts.item(ind).value
        e.set(name, value)
        h[name] = value
    check_elt(e)
    if e.get('net'):
        e.set('numnet',try_getaddrinfo(e.get('net')))
    if etype == 'local_ipv4':
        e.set('is_local', '1')
    if etype == 'proto' and e.get('l7rule'):
        mark = l7connmark.get(e.get('l7rule'))
        if mark:
            e.set('l7connmark', "0x%08X/0x%08X" %(mark, l7connmark['mask']))
    return e

def xml_new_eltgrp(ID, name, elts, l7connmark={}):
    """Builds a EltGrp Object from an list of DOM Element objects
    """
    assert isinstance(ID, int)
    egrp = EltGrp(ID,name,'or')
    for e in elts:
        myelt = xml_new_elt(e, l7connmark)
        myelt.set('parent_name',name)
        if myelt.get('type') in ("ipv4", "local_ipv4", "nufw", "proto", "app", "os", "periodicity", "duration"):
            eltset = EltSet([])
            eltset.add_elt(myelt)
            egrp.add_eltset(eltset)
        elif(myelt.get('type')=="link"):
            eltsetlink = EltSet([])
            eltsetlink.add_elt(myelt)
            egrp.add_eltlink(eltsetlink)
    return egrp

def parse_groups(doc):
    l = {}
    groups = doc.getElementsByTagName('groups')
    groups = groups[0]
    for group in groups.getElementsByTagName('group'):
        id = int(group.getAttribute('ID'))
        enabled = group.getAttribute('enabled')
        st = 0
        if enabled == '1':
            st = 1
        l[id] = st
    return l