Esempio n. 1
0
def netgroup(netgroup,host=None,user=None,domain=None):
    """Perform a netgroup lookup.

    Enumeration is not supported on netgroup, so either one or three keys must be provided.

    To lookup one group by netgroup name::

        >>> ng = netgroup(netgroupname)
        >>> print ng.name
        'netgroupname'

    """
    host,user,domain = c_char_p(None),c_char_p(None),c_char_p(None)

    # If netgroup is None, fatal error
    if netgroup is None:
        return Netgroup(False)
        #return None

    else:
        # True or False
        netgrp = setnetgrent(netgroup)
        members = []
        while netgrp:
            # getnetgrent need pointers to be written
            netgrp = getnetgrent(_byref(host), _byref(user), _byref(domain))
            if netgrp:
                #res.append(Netgroup(netgrp))
                members.append({ 'host':host.value,'user':user.value,'domain':domain.value })
        endnetgrent()
        return Netgroup({'name':netgroup,'members':members})
Esempio n. 2
0
def service(search=None, protocol=None):
    """Perform a service lookup.

    To lookup all services::

        >>> for item in service():
        ...

    To lookup one service by port number::

        >>> http = service(0, 'tcp')
        >>> print http.port
        80

    Or by service name::

        >>> smtp = service('smtp', 'tcp')
        >>> print smtp.port
        25

    Or by short notation::

        >>> snmp = service('udp/snmp')
        >>> print snmp.port
        161

    """
    if search is None:
        setservent()
        srv = True
        res = []
        while srv:
            srv = getservent()
            if srv:
                res.append(Service(srv))
        endservent()
        return res

    else:
        search = str(search)
        if not protocol and '/' in search:
            protocol, search = search.split('/')
        if proto not in ['tcp', 'udp']:
            raise ValueError('Unsupported protocol "%s"' % (str(protocol),))
        if search.isdigit():
            srv = getservbyport(uid_t(int(search)), c_char_p(protocol))
        else:
            srv = getservbyname(c_char_p(search), c_char_p(protocol))

        if bool(srv):
            return Service(srv)
Esempio n. 3
0
def network(search=None):
    """Perform a network lookup.

    To lookup all services::

        >>> for item in network():
        ...

    To lookup one network by name::

        >>> net = network('link-local')

    """
    if search is None:
        setnetent()
        net = True
        res = []
        while net:
            net = getnetent()
            if net:
                res.append(Network(net))
        endnetent()
        return res

    else:
        net = getnetbyname(c_char_p(search))
        if bool(net):
            return Network(net)
Esempio n. 4
0
        def lookup():
            addr = create_string_buffer(IN6ADDRSZ)

            # Test if input is an IPv6 address
            if inet_pton(AF_INET6, c_char_p(search), pointer(addr)) > 0:
                host = gethostbyaddr(addr, IN6ADDRSZ, AF_INET6)
                return host

            # Test if input is an IPv4 address
            if inet_pton(AF_INET, c_char_p(search), pointer(addr)) > 0:
                host = gethostbyaddr(addr, INADDRSZ, AF_INET)
                return host

            # Test if input is a hostname with an IPv6 address
            host = gethostbyname2(c_char_p(search), socket.AF_INET6)
            if host:
                return host

            # Test if input is a hostname with an IPv4 address
            host = gethostbyname2(c_char_p(search), socket.AF_INET)
            if host:
                return host
Esempio n. 5
0
def passwd(search=None):
    """Perform a passwd lookup.

    To lookup all passwd entries::

        >>> for item in passwd():
        ...

    To lookup one user by user id (uid)::

        >>> root = passwd(0)
        >>> print root.name
        'root'

    To lookup one user by name::

        >>> root = passwd('root')
        >>> print root.uid
        0

    """
    if not callable(setpwent):
        raise NotImplementedError

    # Iterate over all passwd entries
    if search is None:
        setpwent()
        pwd = True
        res = []
        while pwd:
            pwd = getpwent()
            if pwd:
                res.append(Passwd(pwd))
        endpwent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            pwd = getpwuid(uid_t(int(search)))
        else:
            pwd = getpwnam(c_char_p(search))

        if bool(pwd):
            return Passwd(pwd)
Esempio n. 6
0
def group(search=None):
    """Perform a group lookup.

    To lookup all groups::

        >>> for item in group():
        ...

    To lookup one group by group id (gid)::

        >>> root = group(0)
        >>> print root.name
        'root'

    To lookup one group by name::

        >>> root = group('root')
        >>> print root.gid
        0

    """
    # Iterate over all group entries
    if search is None:
        setgrent()
        grp = True
        res = []
        while grp:
            grp = getgrent()
            if grp:
                res.append(Group(grp))
        endgrent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            grp = getgrgid(gid_t(int(search)))
        else:
            grp = getgrnam(c_char_p(search))

        if bool(grp):
            return Group(grp)
Esempio n. 7
0
def rpc(search=None):
    """Perform a remote procedure call lookup.

    To lookup all rpc services::

        >>> for item in rpc():
        ...

    To lookup one rpc service by name::

        >>> nfs = rpc('nfs')
        >>> print nfs.number
        100003
        >>> print nfs.aliases
        ['portmap', 'sunrpc']

    """
    if not callable(setrpcent):
        raise NotImplementedError

    if search is None:
        setrpcent()
        ent = True
        res = []
        while ent:
            ent = getrpcent()
            if ent:
                res.append(RPC(ent))
        endrpcent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            ent = getrpcbynumber(uid_t(int(search)))
        else:
            ent = getrpcbyname(c_char_p(search))

        if bool(ent):
            return RPC(ent)
Esempio n. 8
0
def proto(search=None):
    """Perform a protocol lookup.

    To lookup all protocols::

        >>> for item in proto():
        ...

    To lookup a single protocol number::

        >>> tcp = proto('tcp')
        >>> print tcp.proto
        6
    """
    if not callable(setprotoent):
        raise NotImplementedError

    if search is None:
        setprotoent()
        prt = True
        res = []
        while prt:
            prt = getprotoent()
            if prt:
                res.append(Proto(prt))
        endprotoent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            prt = getprotobynumber(uid_t(int(search)))
        else:
            prt = getprotobyname(c_char_p(search))

        if bool(prt):
            return Proto(prt)