1
 6Y    	#! /usr/bin/python3
# coding: utf-8

# getent.py - program for querying nslcd
#
# Copyright (C) 2013-2019 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

import argparse
import re
import socket
import struct
import sys

import constants
from cmdline import VersionAction
from nslcd import NslcdClient


epilog = '''
supported databases:
  aliases, ethers, group, group.bymember, hosts, hostsv4, hostsv6,
  netgroup, netgroup.norec, networks, networksv4, networksv6, passwd,
  protocols, rpc, services, shadow

Report bugs to <%s>.
'''.strip() % constants.PACKAGE_BUGREPORT

# set up command line parser
parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description='Query information in %s via nslcd.' %
    constants.MODULE_NAME.upper(),
    epilog=epilog)
parser.add_argument('-V', '--version', action=VersionAction)
parser.add_argument('database', metavar='DATABASE',
                    help='any database supported by nslcd')
parser.add_argument('keys', metavar='KEY', nargs='*',
                    help='filter returned database values by key')


def write_aliases(con):
    while con.get_response() == constants.NSLCD_RESULT_BEGIN:
        print('%-16s%s' % (
            con.read_string() + ': ',
            ', '.join(con.read_stringlist())))


def getent_aliases(database, keys=None):
    if not keys:
        write_aliases(NslcdClient(constants.NSLCD_ACTION_ALIAS_ALL))
        return
    for key in keys:
        con = NslcdClient(constants.NSLCD_ACTION_ALIAS_BYNAME)
        con.write_string(key)
        write_aliases(con)


def write_ethers(con):
    while con.get_response() == constants.NSLCD_RESULT_BEGIN:
        name = con.read_string()
        ether = con.read_ether()
        print('%s %s' % (ether, name))


def getent_ethers(database, keys=None):
    if not keys:
        write_ethers(NslcdClient(constants.NSLCD_ACTION_ETHER_ALL))
        return
    for key in keys:
        if re.match('^[0-9a-fA-F]{1,2}(:[0-9a-fA-F]{1,2}){5}$', key):
            con = NslcdClient(constants.NSLCD_ACTION_ETHER_BYETHER)
            con.write_ether(key)
        else:
            con = NslcdClient(constants.NSLCD_ACTION_ETHER_BYNAME)
            con.write_string(key)
        write_ethers(con)


def write_group(con):
    while con.get_response() == constants.NSLCD_RESULT_BEGIN:
        print('%s:%s:%d:%s' % (
            con.read_string(),
            con.read_string(),
            con.read_int32(),
            ','.join(con.read_stringlist())))


def getent_group(database, keys=None):
    if not keys:
        write_group(NslcdClient(constants.NSLCD_ACTION_GROUP_ALL))
        return
    for key in keys:
        if database == 'group.bymember':
            con = NslcdClient(constants.NSLCD_ACTION_GROUP_BYMEMBER)
            con.write_string(key)
        elif re.match(r'^\d+$', key):
            con = NslcdClient(constants.NSLCD_ACTION_GROUP_BYGID)
            con.write_int32(int(key))
        else:
            con = NslcdClient(constants.NSLCD_ACTION_GROUP_BYNAME)
            con.write_string(key)
        write_group(con)


def _get_ipv4(value):
    try:
        return socket.inet_pton(socket.AF_INET, value)
    except socket.error:
        return None


def _get_ipv6(value):
    try:
        return socket.inet_pton(socket.AF_INET6, value)
    except socket.error:
        return None


def _get_af(database):
 