terom@259: #!/usr/bin/env python terom@259: terom@259: """ terom@259: Manipulate host definitions for dns/dhcp. terom@259: """ terom@259: terom@259: import pvl.args, optparse terom@259: import pvl.dns.zone terom@259: import pvl.dhcp.config terom@261: import pvl.ldap.args terom@259: terom@259: import collections terom@259: import re terom@259: import logging; log = logging.getLogger('main') terom@259: terom@259: __version__ = '0.1' terom@259: terom@259: def parse_options (argv) : terom@259: """ terom@259: Parse command-line arguments. terom@259: """ terom@259: terom@259: parser = optparse.OptionParser( terom@259: prog = argv[0], terom@259: usage = '%prog: [options]', terom@259: version = __version__, terom@259: terom@259: # module docstring terom@259: description = __doc__, terom@259: ) terom@259: terom@259: # logging terom@259: parser.add_option_group(pvl.args.parser(parser)) terom@261: parser.add_option_group(pvl.ldap.args.parser(parser)) terom@259: terom@259: parser.add_option('-c', '--input-charset', metavar='CHARSET', default='utf-8', terom@259: help="Encoding used for input files") terom@259: terom@259: parser.add_option('--output-charset', metavar='CHARSET', default='utf-8', terom@259: help="Encoding used for output files") terom@259: terom@259: # input terom@259: parser.add_option('--import-zone-hosts', metavar='FILE', terom@259: help="Load hosts from DNS zone") terom@259: terom@259: parser.add_option('--import-dhcp-hosts', metavar='FILE', terom@259: help="Load hosts from DHCP config") terom@259: terom@261: parser.add_option('--dump-host-comments', action='store_true', terom@261: help="Dump out info on imported host comments") terom@261: terom@259: # defaults terom@259: parser.add_option('--hosts-domain', metavar='DOMAIN', terom@259: help="Default domain for hosts") terom@259: terom@259: parser.add_option('--zone-unused', metavar='HOST', terom@259: help="DNS name for unallocated hosts") terom@259: terom@259: # output terom@259: parser.add_option('--output-hosts', metavar='FILE', terom@259: help="Output hosts file") terom@259: terom@259: # defaults terom@259: parser.set_defaults( terom@259: terom@259: ) terom@259: terom@259: # parse terom@259: options, args = parser.parse_args(argv[1:]) terom@259: terom@259: # apply terom@259: pvl.args.apply(options, argv[0]) terom@259: terom@259: return options, args terom@259: terom@259: def process_zone_hosts (options, file) : terom@259: """ terom@259: Yield host info from zonefile records. terom@259: """ terom@259: terom@259: for rr in pvl.dns.zone.ZoneRecord.load(file) : terom@259: if options.zone_unused and rr.name == options.zone_unused : terom@259: log.debug("%s: skip %s", rr.name, rr) terom@259: continue terom@259: terom@259: elif rr.type == 'A' : terom@259: ip, = rr.data terom@259: terom@259: yield rr.name, 'ip', ip terom@259: terom@259: if rr.comment : terom@260: yield rr.name, 'comment', rr.comment terom@259: terom@259: elif rr.type == 'CNAME' : terom@259: host, = rr.data terom@259: terom@259: yield host, 'alias', rr.name terom@259: terom@259: else : terom@259: log.warn("%s: unknown rr: %s", rr.name, rr) terom@259: terom@259: def process_dhcp_host (options, host, items) : terom@259: """ terom@259: Yield host infos from a dhcp host ... { ... } terom@259: """ terom@259: terom@259: hostname = None terom@259: ethernet = [] terom@259: fixed_address = None terom@259: terom@259: for item in items : terom@259: item, args = item[0], item[1:] terom@259: terom@259: if item == 'hardware' : terom@259: _ethernet, ethernet = args terom@259: assert _ethernet == 'ethernet' terom@259: elif item == 'fixed-address' : terom@259: fixed_address, = args terom@259: elif item == 'option' : terom@259: option = args.pop(0) terom@259: terom@259: if option == 'host-name' : terom@259: hostname, = args terom@259: else : terom@259: log.warn("host %s: ignore unknown option: %s", host, option) terom@259: else : terom@259: log.warn("host %s: ignore unknown item: %s", host, item) terom@259: terom@259: # determine hostname terom@259: if hostname : terom@259: pass terom@259: elif fixed_address and not re.match(r'\d+\.\d+\.\d+.\d+', fixed_address) : terom@259: hostname, domain = fixed_address.split('.', 1) terom@259: elif '-' in host : terom@259: hostname, suffix = host.rsplit('-', 1) terom@259: else : terom@259: log.warn("%s: guess hostname: %s", host, host) terom@259: hostname = host terom@259: terom@259: if hostname : terom@259: yield hostname, 'ethernet', ethernet terom@259: terom@259: def process_dhcp_hosts (options, blocks) : terom@259: """ terom@259: Process hosts from a parsed block terom@259: """ terom@259: terom@259: for block, items, blocks in blocks : terom@259: log.info("%s", block) terom@259: terom@259: block, args = block[0], block[1:] terom@259: terom@259: if block == 'group' : terom@259: for info in process_dhcp_hosts(options, blocks) : terom@259: yield info terom@259: elif block == 'host' : terom@259: host, = args terom@259: terom@259: try : terom@259: for info in process_dhcp_host(options, host, items) : terom@259: yield info terom@259: except ValueError as error : terom@259: log.warn("%s: invalid host: %s", host, error) terom@259: else: terom@259: log.warn("ignore unknown block: %s", block) terom@259: terom@259: def process_dhcp_conf (options, file) : terom@259: items, blocks = pvl.dhcp.config.DHCPConfigParser().load(file) terom@259: terom@259: for item in items : terom@259: item, args = item[0], item[1:] terom@259: terom@259: if item == 'include' : terom@259: include, = args terom@259: for info in process_dhcp_conf(options, pvl.args.apply_file(include)) : terom@259: yield info terom@259: else : terom@259: log.warn("ignore unknown item: %s", item) terom@259: terom@259: for info in process_dhcp_hosts(options, blocks) : terom@259: yield info terom@259: terom@259: def apply_hosts_import (options) : terom@259: """ terom@259: Import host infos from given files. terom@259: """ terom@259: terom@259: if options.import_zone_hosts: terom@259: for info in process_zone_hosts(options, terom@259: pvl.args.apply_file(options.import_zone_hosts)) : terom@259: yield info terom@259: terom@259: if options.import_dhcp_hosts: terom@259: for info in process_dhcp_conf(options, terom@259: pvl.args.apply_file(options.import_dhcp_hosts)) : terom@259: yield info terom@260: terom@260: ZONE_COMMENTS = ( terom@260: re.compile(r'(?P[^/]+)\s*-\s+(?P.+)'), terom@260: re.compile(r'(?P.+?)\s*/\s*(?P.+)\s+[/-]\s+(?P.+)'), terom@260: re.compile(r'(?P.+?)\s*/\s*(?P.+)\s+[(]\s*(?P.+)[)]'), terom@260: re.compile(r'(?P.+?)\s*/\s*(?P.+)'), terom@260: re.compile(r'(?P.+)'), terom@260: ) terom@260: terom@260: ZONE_OWNER_MAIL = re.compile(r'(?P.*?)\s*<(?P.+?)>') terom@260: terom@260: def process_zone_comment (options, hostname, comment) : terom@260: """ terom@260: Attempt to parse a host comment field... :D terom@260: terom@260: Yields (field, value) bits terom@260: """ terom@260: terom@260: for regex in ZONE_COMMENTS : terom@260: match = regex.match(comment) terom@260: terom@260: if match : terom@260: break terom@260: else : terom@260: log.warn("%s: unparsed comment: %s", hostname, comment) terom@260: return terom@260: terom@260: matches = match.groupdict() terom@260: owner = matches.pop('owner', None) terom@260: terom@260: if owner : terom@260: mail_match = ZONE_OWNER_MAIL.match(owner) terom@260: terom@260: if mail_match : terom@260: mail_matches = mail_match.groupdict() terom@260: terom@260: owner = mail_matches['owner'] terom@261: yield 'mail', mail_matches['mail'].strip() terom@260: terom@261: yield 'owner', owner.strip() terom@260: terom@260: for field, value in matches.iteritems() : terom@260: if value : terom@261: yield field, value.strip() terom@261: terom@261: HOST_OWNERS = { terom@261: u'tech': 'root', terom@261: u'atk': 'root', terom@261: u'toimisto': 'root', terom@261: } terom@261: terom@261: def process_host_owner (options, host, info) : terom@261: """ terom@261: Yield guesses for user from LDAP. terom@261: """ terom@261: terom@261: if info.get('owner').lower() in HOST_OWNERS : terom@261: yield HOST_OWNERS[info.get('owner').lower()] terom@261: terom@261: if info.get('mail') : terom@261: for user in options.ldap.users.filter( terom@261: { 'mailLocalAddress': info['mail'] }, terom@261: { 'uid': info['mail'] }, terom@261: ) : terom@261: yield user['uid'] terom@261: terom@261: if info.get('group') and info.get('owner') : terom@261: groups = options.ldap.groups.filter(cn=info['group']) terom@261: terom@261: for group in groups : terom@261: for user in options.ldap.users.filter({ terom@261: 'gidNumber': group['gidNumber'], terom@261: 'cn': info['owner'], terom@261: }) : terom@261: yield user['uid'] terom@261: terom@261: if info.get('owner') : terom@261: for user in options.ldap.users.filter({ terom@261: 'cn': info['owner'], terom@261: }) : terom@261: yield user['uid'] terom@261: terom@261: def process_host_comments (options, host, info) : terom@261: """ terom@261: Process host fields from comment. terom@261: terom@261: Attempts to find owner from LDAP.. terom@261: """ terom@261: terom@261: log.debug("%s: %s", host, info) terom@260: terom@261: for owner in process_host_owner(options, host, info) : terom@261: log.info("%s: %s", host, owner) terom@261: terom@261: yield 'owner', owner, terom@261: terom@261: # only use the first match terom@261: break terom@261: else : terom@261: log.warn("%s: no owner: %s", host, info) terom@262: terom@262: if info.get('host') : terom@262: yield 'comment', info['host'] terom@260: terom@260: def process_hosts_comments (options, import_hosts) : terom@260: """ terom@260: Parse out comments from host imports.. terom@260: """ terom@260: terom@260: for host, field, value in import_hosts : terom@260: if field != 'comment': terom@260: yield host, field, value terom@260: continue terom@260: terom@260: fields = dict(process_zone_comment(options, host, value)) terom@261: terom@261: if options.dump_host_comments : terom@261: print u"{host:20} {comment:80} = {group:15} / {owner:20} <{mail:20}> / {hostinfo}".format( terom@261: host = host, terom@261: comment = value, terom@261: group = fields.get('group', ''), terom@261: owner = fields.get('owner', ''), terom@261: mail = fields.get('mail', ''), terom@261: hostinfo = fields.get('host', ''), terom@261: ).encode('utf-8') terom@261: terom@260: terom@261: for field, value in process_host_comments(options, host, fields) : terom@261: yield host, field, value terom@261: terom@259: def process_hosts_import (options, import_hosts) : terom@259: """ terom@259: Import host definitions from given infos terom@259: """ terom@259: terom@259: hosts = collections.defaultdict(lambda: collections.defaultdict(list)) terom@259: terom@259: for host, field, value in import_hosts : terom@259: hosts[host][field].append(value) terom@259: terom@259: return hosts.iteritems() terom@259: terom@259: terom@259: def main (argv) : terom@259: options, args = parse_options(argv) terom@261: terom@261: options.ldap = pvl.ldap.args.apply(options) terom@259: terom@259: if args : terom@259: # direct from file terom@259: hosts = pvl.args.apply_files(args, 'r', options.input_charset) terom@259: else : terom@259: # import terom@259: import_hosts = apply_hosts_import(options) terom@260: import_hosts = process_hosts_comments(options, import_hosts) terom@259: hosts = process_hosts_import(options, import_hosts) terom@259: terom@259: # output terom@259: if options.output_hosts : terom@259: for host, fields in hosts : terom@259: print host terom@259: terom@259: for field, values in fields.iteritems() : terom@259: for value in values : terom@259: print "\t", field, "\t", value.encode(options.output_charset) terom@259: terom@259: return 0 terom@259: terom@259: if __name__ == '__main__': terom@259: pvl.args.main(main)