#! /usr/bin/env python

# This file is part of IVRE.
# Copyright 2011 - 2014 Pierre LALET <pierre.lalet@cea.fr>
#
# IVRE 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, either version 3 of the License, or
# (at your option) any later version.
#
# IVRE 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 IVRE. If not, see <http://www.gnu.org/licenses/>.

import ivre.utils
import ivre.keys
import re
import sys


def scan_rsa_keys(keys, known={}):
    for k in keys:
        rec = known.get(k['modulus'], [])
        rec.append(k)
        known[k['modulus']] = rec
    return known


def check_keys(keys):
    for modulus in keys:
        if len(keys[modulus]) > 1:
            sys.stdout.write('%x' % modulus)
            for i in keys[modulus]:
                sys.stdout.write(' %s:%d' % (ivre.utils.int2ip(i['host']),
                                             i['port']))
            sys.stdout.write('\n')

if __name__ == '__main__':
    known = {}
    allkeys = [ivre.keys.SSHRSAKey(), ivre.keys.SSLRSAKey(),
               ivre.keys.PassiveSSLRSAKey()]
    for a in allkeys:
        known = scan_rsa_keys(a.get_keys(), known=known)
    check_keys(known)
